comparison dwtestoo.cpp @ 2917:77e5d6743013

C++: Implement most of Page 2 (Render) except the actual rendering.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 28 Dec 2022 09:32:27 +0000
parents fe43f8667d3d
children e609aa6a5b93
comparison
equal deleted inserted replaced
2916:fe43f8667d3d 2917:77e5d6743013
169 if(lp[i][len - 1] == '\n') 169 if(lp[i][len - 1] == '\n')
170 lp[i][len - 1] = '\0'; 170 lp[i][len - 1] = '\0';
171 } 171 }
172 num_lines = i; 172 num_lines = i;
173 fclose(fp); 173 fclose(fp);
174 #if 0 174
175 hscrollbar->SetRange(max_linewidth, cols); 175 hscrollbar->SetRange(max_linewidth, cols);
176 hscrollbar->SetPos(0); 176 hscrollbar->SetPos(0);
177 vscrollbar->SetRange(num_lines, rows); 177 vscrollbar->SetRange(num_lines, rows);
178 vscrollbar->SetPos(0); 178 vscrollbar->SetPos(0);
179 #endif
180 } 179 }
181 #ifdef __ANDROID__ 180 #ifdef __ANDROID__
182 if(fd != -1) 181 if(fd != -1)
183 close(fd); 182 close(fd);
184 #endif 183 #endif
412 return TRUE; 411 return TRUE;
413 }); 412 });
414 413
415 cursortogglebutton->ConnectClicked([this, cursortogglebutton] () -> int 414 cursortogglebutton->ConnectClicked([this, cursortogglebutton] () -> int
416 { 415 {
417 this->cursor_arrow = !this->cursor_arrow;
418 cursortogglebutton->SetText(this->cursor_arrow ? "Set Cursor pointer - ARROW" : 416 cursortogglebutton->SetText(this->cursor_arrow ? "Set Cursor pointer - ARROW" :
419 "Set Cursor pointer - CLOCK"); 417 "Set Cursor pointer - CLOCK");
420 this->SetPointer(this->cursor_arrow ? DW_POINTER_CLOCK : DW_POINTER_DEFAULT); 418 this->SetPointer(this->cursor_arrow ? DW_POINTER_CLOCK : DW_POINTER_DEFAULT);
419 this->cursor_arrow = !this->cursor_arrow;
421 return FALSE; 420 return FALSE;
422 }); 421 });
423 422
424 colorchoosebutton->ConnectClicked([this]() -> int 423 colorchoosebutton->ConnectClicked([this]() -> int
425 { 424 {
426 this->current_color = this->app->ColorChoose(this->current_color); 425 this->current_color = this->app->ColorChoose(this->current_color);
427 return FALSE; 426 return FALSE;
428 }); 427 });
428 }
429
430 void CreateRender(DW::Box *notebookbox) {
431 int vscrollbarwidth, hscrollbarheight;
432 wchar_t widestring[100] = L"DWTest Wide";
433 char *utf8string = dw_wchar_to_utf8(widestring);
434
435 // create a box to pack into the notebook page
436 DW::Box *pagebox = new DW::Box(DW_HORZ, 2);
437 notebookbox->PackStart(pagebox, 0, 0, TRUE, TRUE, 0);
438
439 // now a status area under this box
440 DW::Box *hbox = new DW::Box(DW_HORZ, 1);
441 notebookbox->PackStart(hbox, 100, 20, TRUE, FALSE, 1);
442
443 DW::StatusText *status1 = new DW::StatusText();
444 hbox->PackStart(status1, 100, DW_SIZE_AUTO, TRUE, FALSE, 1);
445
446 DW::StatusText *status2 = new DW::StatusText();
447 hbox->PackStart(status2, 100, DW_SIZE_AUTO, TRUE, FALSE, 1);
448 // a box with combobox and button
449 hbox = new DW::Box(DW_HORZ, 1 );
450 notebookbox->PackStart(hbox, 100, 25, TRUE, FALSE, 1);
451
452 DW::ComboBox *rendcombo = new DW::ComboBox( "Shapes Double Buffered");
453 hbox->PackStart(rendcombo, 80, 25, TRUE, TRUE, 0);
454 rendcombo->Append("Shapes Double Buffered");
455 rendcombo->Append("Shapes Direct");
456 rendcombo->Append("File Display");
457
458 DW::Text *label = new DW::Text("Image X:");
459 label->SetStyle(DW_DT_VCENTER | DW_DT_CENTER);
460 hbox->PackStart(label, DW_SIZE_AUTO, 25, FALSE, TRUE, 0);
461
462 DW::SpinButton *imagexspin = new DW::SpinButton("20");
463 hbox->PackStart(imagexspin, 25, 25, TRUE, TRUE, 0);
464
465 label = new DW::Text("Y:");
466 label->SetStyle(DW_DT_VCENTER | DW_DT_CENTER);
467 hbox->PackStart(label, DW_SIZE_AUTO, 25, FALSE, TRUE, 0);
468
469 DW::SpinButton *imageyspin = new DW::SpinButton("20");
470 hbox->PackStart(imageyspin, 25, 25, TRUE, TRUE, 0);
471 imagexspin->SetLimits(2000, 0);
472 imageyspin->SetLimits(2000, 0);
473 imagexspin->SetPos(20);
474 imageyspin->SetPos(20);
475
476 DW::CheckBox *imagestretchcheck = new DW::CheckBox("Stretch");
477 hbox->PackStart(imagestretchcheck, DW_SIZE_AUTO, 25, FALSE, TRUE, 0);
478
479 DW::Button *refreshbutton = new DW::Button("Refresh");
480 hbox->PackStart(refreshbutton, DW_SIZE_AUTO, 25, FALSE, TRUE, 0);
481
482 DW::Button *printbutton = new DW::Button("Print");
483 hbox->PackStart(printbutton, DW_SIZE_AUTO, 25, FALSE, TRUE, 0);
484
485 // Pre-create the scrollbars so we can query their sizes
486 vscrollbar = new DW::ScrollBar(DW_VERT, 50);
487 hscrollbar = new DW::ScrollBar(DW_HORZ, 50);
488 vscrollbar->GetPreferredSize(&vscrollbarwidth, NULL);
489 hscrollbar->GetPreferredSize(NULL, &hscrollbarheight);
490
491 // On GTK with overlay scrollbars enabled this returns us 0...
492 // so in that case we need to give it some real values.
493 if(!vscrollbarwidth)
494 vscrollbarwidth = 8;
495 if(!hscrollbarheight)
496 hscrollbarheight = 8;
497
498 // Create render box for line number pixmap
499 DW::Render *render1 = new DW::Render();
500 render1->SetFont(FIXEDFONT);
501 render1->GetTextExtents("(g", &font_width, &font_height);
502 font_width = font_width / 2;
503
504 DW::Box *vscrollbox = new DW::Box(DW_VERT, 0);
505 vscrollbox->PackStart(render1, font_width*width1, font_height*rows, FALSE, TRUE, 0);
506 vscrollbox->PackStart(DW_NOHWND, (font_width*(width1+1)), hscrollbarheight, FALSE, FALSE, 0);
507 pagebox->PackStart(vscrollbox, 0, 0, FALSE, TRUE, 0);
508
509 // pack empty space 1 character wide
510 pagebox->PackStart(DW_NOHWND, font_width, 0, FALSE, TRUE, 0);
511
512 // create box for filecontents and horz scrollbar
513 DW::Box *textbox = new DW::Box(DW_VERT,0 );
514 pagebox->PackStart(textbox, 0, 0, TRUE, TRUE, 0);
515
516 // create render box for filecontents pixmap
517 DW::Render *render2 = new DW::Render();
518 textbox->PackStart(render2, 10, 10, TRUE, TRUE, 0);
519 render2->SetFont(FIXEDFONT);
520 // create horizonal scrollbar
521 textbox->PackStart(hscrollbar, TRUE, FALSE, 0);
522
523 // create vertical scrollbar
524 vscrollbox = new DW::Box(DW_VERT, 0);
525 vscrollbox->PackStart(vscrollbar, FALSE, TRUE, 0);
526 // Pack an area of empty space of the scrollbar dimensions
527 vscrollbox->PackStart(DW_NOHWND, vscrollbarwidth, hscrollbarheight, FALSE, FALSE, 0);
528 pagebox->PackStart(vscrollbox, 0, 0, FALSE, TRUE, 0);
529
530 pixmap1 = new DW::Pixmap(render1, font_width*width1, font_height*rows);
531 pixmap2 = new DW::Pixmap(render2, font_width*cols, font_height*rows);
532 image = new DW::Pixmap(render1, "image/test");
533 if(!image || !image->GetHPIXMAP())
534 image = new DW::Pixmap(render1, "~/test");
535 if(!image || !image->GetHPIXMAP())
536 {
537 char *appdir = app->GetDir();
538 char pathbuff[1025] = {0};
539 int pos = (int)strlen(appdir);
540
541 strncpy(pathbuff, appdir, 1024);
542 pathbuff[pos] = DW_DIR_SEPARATOR;
543 pos++;
544 strncpy(&pathbuff[pos], "test", 1024-pos);
545 image = new DW::Pixmap(render1, pathbuff);
546 }
547 if(image)
548 image->SetTransparentColor(DW_CLR_WHITE);
549
550 app->MessageBox(utf8string ? utf8string : "DWTest", DW_MB_OK|DW_MB_INFORMATION, "Width: %d Height: %d\n", font_width, font_height);
551 if(utf8string)
552 app->Free(utf8string);
553 pixmap1->DrawRect(DW_DRAW_FILL | DW_DRAW_NOAA, 0, 0, font_width*width1, font_height*rows);
554 pixmap2->DrawRect(DW_DRAW_FILL | DW_DRAW_NOAA, 0, 0, font_width*cols, font_height*rows);
555
556 // Signal handler lambdas
557 render1->ConnectButtonPress([this](int x, int y, int buttonmask) -> int
558 {
559 DW::Menu *menu = new DW::Menu();
560 DW::MenuItem *menuitem = menu->AppendItem("~Quit");
561 long px, py;
562
563 menuitem->ConnectClicked([this] () -> int
564 {
565 if(this->app->MessageBox(APP_TITLE, DW_MB_YESNO | DW_MB_QUESTION, APP_EXIT) != 0) {
566 this->app->MainQuit();
567 }
568 return TRUE;
569 });
570
571
572 menu->AppendItem(DW_MENU_SEPARATOR);
573 menuitem = menu->AppendItem("~Show Window");
574 menuitem->ConnectClicked([this]() -> int
575 {
576 this->Show();
577 this->Raise();
578 return TRUE;
579 });
580
581 this->app->GetPointerPos(&px, &py);
582 menu->Popup(this, (int)px, (int)py);
583 return TRUE;
584 });
585
586 render1->ConnectExpose([this](DWExpose *exp) -> int
587 {
588 return TRUE;
589 });
590
591 render2->ConnectExpose([this](DWExpose *exp) -> int
592 {
593 return TRUE;
594 });
595
596 render2->ConnectKeyPress([this, status1](char ch, int vk, int state, char *utf8) -> int
597 {
598 char tmpbuf[101] = {0};
599 if(ch)
600 snprintf(tmpbuf, 100, "Key: %c(%d) Modifiers: %s(%d) utf8 %s", ch, ch, this->ResolveKeyModifiers(state), state, utf8);
601 else
602 snprintf(tmpbuf, 100, "Key: %s(%d) Modifiers: %s(%d) utf8 %s", this->ResolveKeyName(vk), vk, ResolveKeyModifiers(state), state, utf8);
603 status1->SetText(tmpbuf);
604 return FALSE;
605 });
606
607 hscrollbar->ConnectValueChanged([this, status1](int value) -> int
608 {
609 char tmpbuf[101] = {0};
610
611 this->current_col = value;
612 snprintf(tmpbuf, 100, "Row:%d Col:%d Lines:%d Cols:%d", current_row,current_col,num_lines,max_linewidth);
613 status1->SetText(tmpbuf);
614 this->RenderDraw();
615 return TRUE;
616 });
617
618 vscrollbar->ConnectValueChanged([this, status1](int value) -> int
619 {
620 char tmpbuf[101] = {0};
621
622 this->current_row = value;
623 snprintf(tmpbuf, 100, "Row:%d Col:%d Lines:%d Cols:%d", current_row,current_col,num_lines,max_linewidth);
624 status1->SetText(tmpbuf);
625 this->RenderDraw();
626 return TRUE;
627 });
628
629 render2->ConnectMotionNotify([this, status2](int x, int y, int buttonmask) -> int
630 {
631 char buf[201] = {0};
632
633 snprintf(buf, 200, "motion_notify: %dx%d buttons %d", x, y, buttonmask);
634 status2->SetText(buf);
635 return FALSE;
636 });
637
638 render2->ConnectButtonPress([this, status2](int x, int y, int buttonmask) -> int
639 {
640 char buf[201] = {0};
641
642 snprintf(buf, 200, "button_press: %dx%d buttons %d", x, y, buttonmask);
643 status2->SetText(buf);
644 return FALSE;
645 });
646
647 render2->ConnectConfigure([this, render1, render2](int width, int height) -> int
648 {
649 DW::Pixmap *old1 = this->pixmap1, *old2 = this->pixmap2;
650
651 rows = height / font_height;
652 cols = width / font_width;
653
654 // Create new pixmaps with the current sizes
655 this->pixmap1 = new DW::Pixmap(render1, (unsigned long)(font_width*(width1)), (unsigned long)height);
656 this->pixmap2 = new DW::Pixmap(render2, (unsigned long)width, (unsigned long)height);
657
658 // Make sure the side area is cleared
659 this->pixmap1->SetForegroundColor(DW_CLR_WHITE);
660 this->pixmap1->DrawRect(DW_DRAW_FILL | DW_DRAW_NOAA, 0, 0, (int)this->pixmap1->GetWidth(), (int)this->pixmap1->GetHeight());
661
662 // Destroy the old pixmaps
663 delete old1;
664 delete old2;
665
666 // Update scrollbar ranges with new values
667 this->hscrollbar->SetRange(max_linewidth, cols);
668 this->vscrollbar->SetRange(num_lines, rows);
669
670 // Redraw the render widgets
671 this->RenderDraw();
672 return TRUE;
673 });
674
675 imagestretchcheck->ConnectClicked([this]() -> int
676 {
677 this->RenderDraw();
678 return TRUE;
679 });
680
681 refreshbutton->ConnectClicked([this]() -> int
682 {
683 this->RenderDraw();
684 return TRUE;
685 });
686
687 printbutton->ConnectClicked([this]() -> int
688 {
689 #if 0 // TODO
690 DW::Print *print = new DW::Print("DWTest Job", 0, 2, [this](DW::Pixmap *pixmap, int page_num) -> int
691 {
692 pixmap->SetFont(FIXEDFONT);
693 if(page_num == 0)
694 {
695 this->DrawShapes(FALSE, pixmap);
696 }
697 else if(page_num == 1)
698 {
699 /* Get the font size for this printer context... */
700 int fheight, fwidth;
701
702 /* If we have a file to display... */
703 if(current_file)
704 {
705 int nrows;
706
707 /* Calculate new dimensions */
708 pixmap->GetTextExtents("(g", NULL, &fheight);
709 nrows = (int)(pixmap->GetHeight() / fheight);
710
711 /* Do the actual drawing */
712 this->DrawFile(0, 0, nrows, fheight, pixmap);
713 }
714 else
715 {
716 /* We don't have a file so center an error message on the page */
717 const char *text = "No file currently selected!";
718 int posx, posy;
719
720 pixmap->GetTextExtents(text, &fwidth, &fheight);
721
722 posx = (int)(pixmap->GetWidth() - fwidth)/2;
723 posy = (int)(pixmap->GetHeight() - fheight)/2;
724
725 pixmap->SetColor(DW_CLR_BLACK, DW_CLR_WHITE);
726 pixmap->DrawText(posx, posy, text);
727 }
728 }
729 return TRUE;
730 });
731 print->Run(0);
732 #endif
733 return TRUE;
734 });
735
736 app->TaskBarInsert(render1, fileicon, "DWTest");
429 } 737 }
430 public: 738 public:
431 // Constructor creates the application 739 // Constructor creates the application
432 DWTest(const char *title): DW::Window(title) { 740 DWTest(const char *title): DW::Window(title) {
433 char fileiconpath[1025] = "file"; 741 char fileiconpath[1025] = "file";
489 { 797 {
490 this->app->Debug("DW_SIGNAL_SWITCH_PAGE: PageNum: %u\n", page_num); 798 this->app->Debug("DW_SIGNAL_SWITCH_PAGE: PageNum: %u\n", page_num);
491 return TRUE; 799 return TRUE;
492 }); 800 });
493 801
494 // Create Notebook page 1 802 // Create Notebook Page 1 - Input
495 notebookbox = new DW::Box(DW_VERT, 5); 803 notebookbox = new DW::Box(DW_VERT, 5);
496 CreateInput(notebookbox); 804 CreateInput(notebookbox);
497 unsigned long notebookpage = notebook->PageNew(0, TRUE); 805 unsigned long notebookpage = notebook->PageNew(0, TRUE);
498 notebook->Pack(notebookpage, notebookbox); 806 notebook->Pack(notebookpage, notebookbox);
499 notebook->PageSetText(notebookpage, "buttons and entry"); 807 notebook->PageSetText(notebookpage, "buttons and entry");
500 808
809 // Create Notebook Page 2 - Render
810 notebookbox = new DW::Box(DW_VERT, 5);
811 CreateRender(notebookbox);
812 notebookpage = notebook->PageNew(0, TRUE);
813 notebook->Pack(notebookpage, notebookbox);
814 notebook->PageSetText(notebookpage, "render");
815
501 // Finalize the window 816 // Finalize the window
502 this->SetSize(640, 550); 817 this->SetSize(640, 550);
503 818
504 timer = new DW::Timer(2000, [this]() -> int 819 timer = new DW::Timer(2000, [this]() -> int
505 { 820 {
516 DW::Timer *timer; 831 DW::Timer *timer;
517 int cursor_arrow = TRUE; 832 int cursor_arrow = TRUE;
518 unsigned long current_color; 833 unsigned long current_color;
519 834
520 // Page 2 835 // Page 2
521 HPIXMAP text1pm,text2pm,image; 836 DW::Pixmap *pixmap1, *pixmap2, *image;
837 DW::ScrollBar *hscrollbar, *vscrollbar;
522 int image_x = 20, image_y = 20, image_stretch = 0; 838 int image_x = 20, image_y = 20, image_stretch = 0;
523 839
524 int font_width = 8, font_height=12; 840 int font_width = 8, font_height=12;
525 int rows=10,width1=6,cols=80; 841 int rows=10,width1=6,cols=80;
526 int num_lines=0, max_linewidth=0; 842 int num_lines=0, max_linewidth=0;
527 int current_row=0,current_col=0; 843 int current_row=0,current_col=0;
528 int render_type = SHAPES_DOUBLE_BUFFERED; 844 int render_type = SHAPES_DOUBLE_BUFFERED;
529 845
530 FILE *fp=NULL; 846 FILE *fp=NULL;
531 char **lp; 847 char **lp;
848 char *current_file = NULL;
532 849
533 // Page 4 850 // Page 4
534 int mle_point=-1; 851 int mle_point=-1;
535 852
536 // Miscellaneous 853 // Miscellaneous
537 int menu_enabled = 1; 854 int menu_enabled = TRUE;
538 char *current_file = NULL;
539 HICN fileicon,foldericon; 855 HICN fileicon,foldericon;
540 856
541 int OnDelete() override { 857 int OnDelete() override {
542 if(app->MessageBox(APP_TITLE, DW_MB_YESNO | DW_MB_QUESTION, APP_EXIT) != 0) { 858 if(app->MessageBox(APP_TITLE, DW_MB_YESNO | DW_MB_QUESTION, APP_EXIT) != 0) {
543 app->MainQuit(); 859 app->MainQuit();