Mercurial > dwindows
comparison dwtestoo.cpp @ 2919:e609aa6a5b93
C++: Attempt to implement page 2 rendering...
Getting a crash on Mac, but want to commit before I sleep.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Wed, 28 Dec 2022 11:23:51 +0000 |
parents | 77e5d6743013 |
children | c6b699a441fe |
comparison
equal
deleted
inserted
replaced
2918:7d3276f80132 | 2919:e609aa6a5b93 |
---|---|
135 } | 135 } |
136 | 136 |
137 char *ReadFile(char *filename) | 137 char *ReadFile(char *filename) |
138 { | 138 { |
139 char *errors = NULL; | 139 char *errors = NULL; |
140 FILE *fp=NULL; | |
140 #ifdef __ANDROID__ | 141 #ifdef __ANDROID__ |
141 int fd = -1; | 142 int fd = -1; |
142 | 143 |
143 /* Special way to open for URIs on Android */ | 144 // Special way to open for URIs on Android |
144 if(strstr(filename, "://")) | 145 if(strstr(filename, "://")) |
145 { | 146 { |
146 fd = dw_file_open(filename, O_RDONLY); | 147 fd = dw_file_open(filename, O_RDONLY); |
147 fp = fdopen(fd, "r"); | 148 fp = fdopen(fd, "r"); |
148 } | 149 } |
154 else | 155 else |
155 { | 156 { |
156 int i,len; | 157 int i,len; |
157 | 158 |
158 lp = (char **)calloc(1000,sizeof(char *)); | 159 lp = (char **)calloc(1000,sizeof(char *)); |
159 /* should test for out of memory */ | 160 // should test for out of memory |
160 max_linewidth=0; | 161 max_linewidth=0; |
161 for(i=0; i<1000; i++) | 162 for(i=0; i<1000; i++) |
162 { | 163 { |
163 lp[i] = (char *)calloc(1, 1025); | 164 lp[i] = (char *)calloc(1, 1025); |
164 if (fgets(lp[i], 1024, fp) == NULL) | 165 if (fgets(lp[i], 1024, fp) == NULL) |
182 close(fd); | 183 close(fd); |
183 #endif | 184 #endif |
184 return errors; | 185 return errors; |
185 } | 186 } |
186 | 187 |
188 // When hpm is not NULL we are printing.. so handle things differently | |
189 void DrawFile(int row, int col, int nrows, int fheight, DW::Pixmap *hpm) | |
190 { | |
191 DW::Pixmap *pixmap = hpm ? hpm : pixmap2; | |
192 char buf[16] = {0}; | |
193 int i,y,fileline; | |
194 char *pLine; | |
195 | |
196 if(current_file) | |
197 { | |
198 pixmap->SetForegroundColor(DW_CLR_WHITE); | |
199 if(!hpm) | |
200 pixmap1->DrawRect(DW_DRAW_FILL | DW_DRAW_NOAA, 0, 0, (int)pixmap1->GetWidth(), (int)pixmap1->GetHeight()); | |
201 pixmap->DrawRect(DW_DRAW_FILL | DW_DRAW_NOAA, 0, 0, (int)pixmap->GetWidth(), (int)pixmap->GetHeight()); | |
202 | |
203 for(i = 0;(i < nrows) && (i+row < num_lines); i++) | |
204 { | |
205 fileline = i + row - 1; | |
206 y = i*fheight; | |
207 pixmap->SetColor(fileline < 0 ? DW_CLR_WHITE : fileline % 16, 1 + (fileline % 15)); | |
208 if(!hpm) | |
209 { | |
210 snprintf(buf, 15, "%6.6d", i+row); | |
211 pixmap->DrawText(0, y, buf); | |
212 } | |
213 pLine = lp[i+row]; | |
214 pixmap->DrawText(0, y, pLine+col); | |
215 } | |
216 } | |
217 } | |
218 | |
219 // When hpm is not NULL we are printing.. so handle things differently | |
220 void DrawShapes(int direct, DW::Pixmap *hpm) | |
221 { | |
222 DW::Pixmap *pixmap = hpm ? hpm : pixmap2; | |
223 int width = (int)pixmap->GetWidth(), height = (int)pixmap->GetHeight(); | |
224 int x[7] = { 20, 180, 180, 230, 180, 180, 20 }; | |
225 int y[7] = { 50, 50, 20, 70, 120, 90, 90 }; | |
226 DW::Drawable *drawable = direct ? static_cast<DW::Drawable *>(render2) : static_cast<DW::Drawable *>(pixmap); | |
227 | |
228 drawable->SetForegroundColor(DW_CLR_WHITE); | |
229 drawable->DrawRect(DW_DRAW_FILL | DW_DRAW_NOAA, 0, 0, width, height); | |
230 drawable->SetForegroundColor(DW_CLR_DARKPINK); | |
231 drawable->DrawRect(DW_DRAW_FILL | DW_DRAW_NOAA, 10, 10, width - 20, height - 20); | |
232 drawable->SetColor(DW_CLR_GREEN, DW_CLR_DARKRED); | |
233 drawable->DrawText(10, 10, "This should be aligned with the edges."); | |
234 drawable->SetForegroundColor(DW_CLR_YELLOW); | |
235 drawable->DrawLine(width - 10, 10, 10, height - 10); | |
236 drawable->SetForegroundColor(DW_CLR_BLUE); | |
237 drawable->DrawPolygon(DW_DRAW_FILL, 7, x, y); | |
238 drawable->SetForegroundColor(DW_CLR_BLACK); | |
239 drawable->DrawRect(DW_DRAW_FILL | DW_DRAW_NOAA, 80, 80, 80, 40); | |
240 drawable->SetForegroundColor(DW_CLR_CYAN); | |
241 // Bottom right corner | |
242 drawable->DrawArc(0, width - 30, height - 30, width - 10, height - 30, width - 30, height - 10); | |
243 // Top right corner | |
244 drawable->DrawArc(0, width - 30, 30, width - 30, 10, width - 10, 30); | |
245 // Bottom left corner | |
246 drawable->DrawArc(0, 30, height - 30, 30, height - 10, 10, height - 30); | |
247 // Full circle in the left top area | |
248 drawable->DrawArc(DW_DRAW_FULL, 120, 100, 80, 80, 160, 120); | |
249 if(image && image->GetHPIXMAP()) | |
250 { | |
251 if(imagestretchcheck->Get()) | |
252 drawable->BitBltStretch(0, 10, width - 20, height - 20, image, 0, 0, (int)image->GetWidth(), (int)image->GetHeight()); | |
253 else | |
254 drawable->BitBlt(imagexspin->GetPos(), imageyspin->GetPos(), (int)image->GetWidth(), (int)image->GetHeight(), image, 0, 0); | |
255 } | |
256 } | |
257 | |
258 void UpdateRender(void) | |
259 { | |
260 switch(render_type) | |
261 { | |
262 case SHAPES_DOUBLE_BUFFERED: | |
263 DrawShapes(FALSE, NULL); | |
264 break; | |
265 case SHAPES_DIRECT: | |
266 DrawShapes(TRUE, NULL); | |
267 break; | |
268 case DRAW_FILE: | |
269 DrawFile(current_row, current_col, rows, font_height, NULL); | |
270 break; | |
271 } | |
272 } | |
273 | |
274 // Request that the render widgets redraw... | |
275 // If not using direct rendering, call update_render() to | |
276 // redraw the in memory pixmaps. Then trigger the expose events. | |
277 // Expose will call update_render() to draw directly or bitblt the pixmaps. | |
187 void RenderDraw() { | 278 void RenderDraw() { |
188 // Draw page 2 | 279 // If we are double buffered, draw to the pixmaps |
280 if(render_type != SHAPES_DIRECT) | |
281 UpdateRender(); | |
282 // Trigger expose event | |
283 render1->Redraw(); | |
284 render2->Redraw(); | |
189 } | 285 } |
190 | 286 |
191 // Add the menus to the window | 287 // Add the menus to the window |
192 void CreateMenus() { | 288 void CreateMenus() { |
193 // Setup the menu | 289 // Setup the menu |
457 | 553 |
458 DW::Text *label = new DW::Text("Image X:"); | 554 DW::Text *label = new DW::Text("Image X:"); |
459 label->SetStyle(DW_DT_VCENTER | DW_DT_CENTER); | 555 label->SetStyle(DW_DT_VCENTER | DW_DT_CENTER); |
460 hbox->PackStart(label, DW_SIZE_AUTO, 25, FALSE, TRUE, 0); | 556 hbox->PackStart(label, DW_SIZE_AUTO, 25, FALSE, TRUE, 0); |
461 | 557 |
462 DW::SpinButton *imagexspin = new DW::SpinButton("20"); | 558 imagexspin = new DW::SpinButton("20"); |
463 hbox->PackStart(imagexspin, 25, 25, TRUE, TRUE, 0); | 559 hbox->PackStart(imagexspin, 25, 25, TRUE, TRUE, 0); |
464 | 560 |
465 label = new DW::Text("Y:"); | 561 label = new DW::Text("Y:"); |
466 label->SetStyle(DW_DT_VCENTER | DW_DT_CENTER); | 562 label->SetStyle(DW_DT_VCENTER | DW_DT_CENTER); |
467 hbox->PackStart(label, DW_SIZE_AUTO, 25, FALSE, TRUE, 0); | 563 hbox->PackStart(label, DW_SIZE_AUTO, 25, FALSE, TRUE, 0); |
468 | 564 |
469 DW::SpinButton *imageyspin = new DW::SpinButton("20"); | 565 imageyspin = new DW::SpinButton("20"); |
470 hbox->PackStart(imageyspin, 25, 25, TRUE, TRUE, 0); | 566 hbox->PackStart(imageyspin, 25, 25, TRUE, TRUE, 0); |
471 imagexspin->SetLimits(2000, 0); | 567 imagexspin->SetLimits(2000, 0); |
472 imageyspin->SetLimits(2000, 0); | 568 imageyspin->SetLimits(2000, 0); |
473 imagexspin->SetPos(20); | 569 imagexspin->SetPos(20); |
474 imageyspin->SetPos(20); | 570 imageyspin->SetPos(20); |
475 | 571 |
476 DW::CheckBox *imagestretchcheck = new DW::CheckBox("Stretch"); | 572 imagestretchcheck = new DW::CheckBox("Stretch"); |
477 hbox->PackStart(imagestretchcheck, DW_SIZE_AUTO, 25, FALSE, TRUE, 0); | 573 hbox->PackStart(imagestretchcheck, DW_SIZE_AUTO, 25, FALSE, TRUE, 0); |
478 | 574 |
479 DW::Button *refreshbutton = new DW::Button("Refresh"); | 575 DW::Button *refreshbutton = new DW::Button("Refresh"); |
480 hbox->PackStart(refreshbutton, DW_SIZE_AUTO, 25, FALSE, TRUE, 0); | 576 hbox->PackStart(refreshbutton, DW_SIZE_AUTO, 25, FALSE, TRUE, 0); |
481 | 577 |
494 vscrollbarwidth = 8; | 590 vscrollbarwidth = 8; |
495 if(!hscrollbarheight) | 591 if(!hscrollbarheight) |
496 hscrollbarheight = 8; | 592 hscrollbarheight = 8; |
497 | 593 |
498 // Create render box for line number pixmap | 594 // Create render box for line number pixmap |
499 DW::Render *render1 = new DW::Render(); | 595 render1 = new DW::Render(); |
500 render1->SetFont(FIXEDFONT); | 596 render1->SetFont(FIXEDFONT); |
501 render1->GetTextExtents("(g", &font_width, &font_height); | 597 render1->GetTextExtents("(g", &font_width, &font_height); |
502 font_width = font_width / 2; | 598 font_width = font_width / 2; |
503 | 599 |
504 DW::Box *vscrollbox = new DW::Box(DW_VERT, 0); | 600 DW::Box *vscrollbox = new DW::Box(DW_VERT, 0); |
512 // create box for filecontents and horz scrollbar | 608 // create box for filecontents and horz scrollbar |
513 DW::Box *textbox = new DW::Box(DW_VERT,0 ); | 609 DW::Box *textbox = new DW::Box(DW_VERT,0 ); |
514 pagebox->PackStart(textbox, 0, 0, TRUE, TRUE, 0); | 610 pagebox->PackStart(textbox, 0, 0, TRUE, TRUE, 0); |
515 | 611 |
516 // create render box for filecontents pixmap | 612 // create render box for filecontents pixmap |
517 DW::Render *render2 = new DW::Render(); | 613 render2 = new DW::Render(); |
518 textbox->PackStart(render2, 10, 10, TRUE, TRUE, 0); | 614 textbox->PackStart(render2, 10, 10, TRUE, TRUE, 0); |
519 render2->SetFont(FIXEDFONT); | 615 render2->SetFont(FIXEDFONT); |
520 // create horizonal scrollbar | 616 // create horizonal scrollbar |
521 textbox->PackStart(hscrollbar, TRUE, FALSE, 0); | 617 textbox->PackStart(hscrollbar, TRUE, FALSE, 0); |
522 | 618 |
583 return TRUE; | 679 return TRUE; |
584 }); | 680 }); |
585 | 681 |
586 render1->ConnectExpose([this](DWExpose *exp) -> int | 682 render1->ConnectExpose([this](DWExpose *exp) -> int |
587 { | 683 { |
684 if(render_type != SHAPES_DIRECT) | |
685 { | |
686 this->render1->BitBlt(0, 0, (int)pixmap1->GetWidth(), (int)pixmap1->GetHeight(), pixmap1, 0, 0); | |
687 render1->Flush(); | |
688 } | |
689 else | |
690 { | |
691 UpdateRender(); | |
692 } | |
588 return TRUE; | 693 return TRUE; |
589 }); | 694 }); |
590 | 695 |
591 render2->ConnectExpose([this](DWExpose *exp) -> int | 696 render2->ConnectExpose([this](DWExpose *exp) -> int |
592 { | 697 { |
698 if(render_type != SHAPES_DIRECT) | |
699 { | |
700 this->render2->BitBlt(0, 0, (int)pixmap2->GetWidth(), (int)pixmap2->GetHeight(), pixmap2, 0, 0); | |
701 render2->Flush(); | |
702 } | |
703 else | |
704 { | |
705 UpdateRender(); | |
706 } | |
593 return TRUE; | 707 return TRUE; |
594 }); | 708 }); |
595 | 709 |
596 render2->ConnectKeyPress([this, status1](char ch, int vk, int state, char *utf8) -> int | 710 render2->ConnectKeyPress([this, status1](char ch, int vk, int state, char *utf8) -> int |
597 { | 711 { |
624 status1->SetText(tmpbuf); | 738 status1->SetText(tmpbuf); |
625 this->RenderDraw(); | 739 this->RenderDraw(); |
626 return TRUE; | 740 return TRUE; |
627 }); | 741 }); |
628 | 742 |
629 render2->ConnectMotionNotify([this, status2](int x, int y, int buttonmask) -> int | 743 render2->ConnectMotionNotify([status2](int x, int y, int buttonmask) -> int |
630 { | 744 { |
631 char buf[201] = {0}; | 745 char buf[201] = {0}; |
632 | 746 |
633 snprintf(buf, 200, "motion_notify: %dx%d buttons %d", x, y, buttonmask); | 747 snprintf(buf, 200, "motion_notify: %dx%d buttons %d", x, y, buttonmask); |
634 status2->SetText(buf); | 748 status2->SetText(buf); |
635 return FALSE; | 749 return FALSE; |
636 }); | 750 }); |
637 | 751 |
638 render2->ConnectButtonPress([this, status2](int x, int y, int buttonmask) -> int | 752 render2->ConnectButtonPress([status2](int x, int y, int buttonmask) -> int |
639 { | 753 { |
640 char buf[201] = {0}; | 754 char buf[201] = {0}; |
641 | 755 |
642 snprintf(buf, 200, "button_press: %dx%d buttons %d", x, y, buttonmask); | 756 snprintf(buf, 200, "button_press: %dx%d buttons %d", x, y, buttonmask); |
643 status2->SetText(buf); | 757 status2->SetText(buf); |
644 return FALSE; | 758 return FALSE; |
645 }); | 759 }); |
646 | 760 |
647 render2->ConnectConfigure([this, render1, render2](int width, int height) -> int | 761 render2->ConnectConfigure([this](int width, int height) -> int |
648 { | 762 { |
649 DW::Pixmap *old1 = this->pixmap1, *old2 = this->pixmap2; | 763 DW::Pixmap *old1 = this->pixmap1, *old2 = this->pixmap2; |
650 | 764 |
651 rows = height / font_height; | 765 rows = height / font_height; |
652 cols = width / font_width; | 766 cols = width / font_width; |
653 | 767 |
654 // Create new pixmaps with the current sizes | 768 // Create new pixmaps with the current sizes |
655 this->pixmap1 = new DW::Pixmap(render1, (unsigned long)(font_width*(width1)), (unsigned long)height); | 769 this->pixmap1 = new DW::Pixmap(this->render1, (unsigned long)(font_width*(width1)), (unsigned long)height); |
656 this->pixmap2 = new DW::Pixmap(render2, (unsigned long)width, (unsigned long)height); | 770 this->pixmap2 = new DW::Pixmap(this->render2, (unsigned long)width, (unsigned long)height); |
657 | 771 |
658 // Make sure the side area is cleared | 772 // Make sure the side area is cleared |
659 this->pixmap1->SetForegroundColor(DW_CLR_WHITE); | 773 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()); | 774 this->pixmap1->DrawRect(DW_DRAW_FILL | DW_DRAW_NOAA, 0, 0, (int)this->pixmap1->GetWidth(), (int)this->pixmap1->GetHeight()); |
661 | 775 |
682 { | 796 { |
683 this->RenderDraw(); | 797 this->RenderDraw(); |
684 return TRUE; | 798 return TRUE; |
685 }); | 799 }); |
686 | 800 |
687 printbutton->ConnectClicked([this]() -> int | 801 printbutton->ConnectClicked([]() -> int |
688 { | 802 { |
689 #if 0 // TODO | 803 #if 0 // TODO |
690 DW::Print *print = new DW::Print("DWTest Job", 0, 2, [this](DW::Pixmap *pixmap, int page_num) -> int | 804 DW::Print *print = new DW::Print("DWTest Job", 0, 2, [this](DW::Pixmap *pixmap, int page_num) -> int |
691 { | 805 { |
692 pixmap->SetFont(FIXEDFONT); | 806 pixmap->SetFont(FIXEDFONT); |
731 print->Run(0); | 845 print->Run(0); |
732 #endif | 846 #endif |
733 return TRUE; | 847 return TRUE; |
734 }); | 848 }); |
735 | 849 |
850 rendcombo->ConnectListSelect([this](int index) -> int | |
851 { | |
852 if(index != this->render_type) | |
853 { | |
854 if(index == DRAW_FILE) | |
855 { | |
856 this->hscrollbar->SetRange(max_linewidth, cols); | |
857 this->hscrollbar->SetPos(0); | |
858 this->vscrollbar->SetRange(num_lines, rows); | |
859 this->vscrollbar->SetPos(0); | |
860 this->current_col = this->current_row = 0; | |
861 } | |
862 else | |
863 { | |
864 this->hscrollbar->SetRange(0, 0); | |
865 this->hscrollbar->SetPos(0); | |
866 this->vscrollbar->SetRange(0, 0); | |
867 this->vscrollbar->SetPos(0); | |
868 } | |
869 this->render_type = index; | |
870 this->RenderDraw(); | |
871 } | |
872 return FALSE; | |
873 }); | |
874 | |
736 app->TaskBarInsert(render1, fileicon, "DWTest"); | 875 app->TaskBarInsert(render1, fileicon, "DWTest"); |
737 } | 876 } |
738 public: | 877 public: |
739 // Constructor creates the application | 878 // Constructor creates the application |
740 DWTest(const char *title): DW::Window(title) { | 879 DWTest(const char *title): DW::Window(title) { |
831 DW::Timer *timer; | 970 DW::Timer *timer; |
832 int cursor_arrow = TRUE; | 971 int cursor_arrow = TRUE; |
833 unsigned long current_color; | 972 unsigned long current_color; |
834 | 973 |
835 // Page 2 | 974 // Page 2 |
975 DW::Render *render1, *render2; | |
836 DW::Pixmap *pixmap1, *pixmap2, *image; | 976 DW::Pixmap *pixmap1, *pixmap2, *image; |
837 DW::ScrollBar *hscrollbar, *vscrollbar; | 977 DW::ScrollBar *hscrollbar, *vscrollbar; |
838 int image_x = 20, image_y = 20, image_stretch = 0; | 978 DW::SpinButton *imagexspin, *imageyspin; |
979 DW::CheckBox *imagestretchcheck; | |
839 | 980 |
840 int font_width = 8, font_height=12; | 981 int font_width = 8, font_height=12; |
841 int rows=10,width1=6,cols=80; | 982 int rows=10,width1=6,cols=80; |
842 int num_lines=0, max_linewidth=0; | 983 int num_lines=0, max_linewidth=0; |
843 int current_row=0,current_col=0; | 984 int current_row=0,current_col=0; |
844 int render_type = SHAPES_DOUBLE_BUFFERED; | 985 int render_type = SHAPES_DOUBLE_BUFFERED; |
845 | 986 |
846 FILE *fp=NULL; | |
847 char **lp; | 987 char **lp; |
848 char *current_file = NULL; | 988 char *current_file = NULL; |
849 | 989 |
850 // Page 4 | 990 // Page 4 |
851 int mle_point=-1; | 991 int mle_point=-1; |