-
Notifications
You must be signed in to change notification settings - Fork 34
/
wxTurtleGraphics.cpp
1288 lines (1058 loc) · 35.5 KB
/
wxTurtleGraphics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include "wxTurtleGraphics.h"
#include "TextEditor.h"
#include <wx/stdpaths.h>
#define WX_TURTLEGRAPHICS_CPP 1
using namespace std;
#define SCREEN_WIDTH 1
#define SCREEN_HEIGHT 2
#define BACK_GROUND 3
#define IN_SPLITSCREEN 4
#define IN_GRAPHICS_MODE 5
// ----------------------------------------------------------------------------
// Custom Events
// ----------------------------------------------------------------------------
/* The edit event */
#if 0
BEGIN_DECLARE_EVENT_TYPES()
DECLARE_EVENT_TYPE(wxEVT_EDIT_CUSTOM_COMMAND, 7777)
END_DECLARE_EVENT_TYPES()
DEFINE_EVENT_TYPE(wxEVT_EDIT_CUSTOM_COMMAND)
#define EVT_EDIT_CUSTOM_COMMAND(id, fn) \
DECLARE_EVENT_TABLE_ENTRY( \
wxEVT_EDIT_CUSTOM_COMMAND, id, -2, \
(wxObjectEventFunction)(wxEventFunction)(wxCommandEventFunction)&fn, \
(wxObject *) NULL \
),
#endif
/* Used for multithread event handling, as well as
for a dummy event otherwise */
BEGIN_DECLARE_EVENT_TYPES()
DECLARE_EVENT_TYPE(wxEVT_LOGO_CUSTOM_COMMAND, 7777)
END_DECLARE_EVENT_TYPES()
DEFINE_EVENT_TYPE(wxEVT_LOGO_CUSTOM_COMMAND)
#define EVT_LOGO_CUSTOM_COMMAND(id, fn) \
DECLARE_EVENT_TABLE_ENTRY( \
wxEVT_LOGO_CUSTOM_COMMAND, id, -1, \
(wxObjectEventFunction)(wxEventFunction)(wxCommandEventFunction)&fn, \
(wxObject *) NULL \
),
// ----------------------------------------------------------------------------
// Globals
// ----------------------------------------------------------------------------
float fillScale;
pen_info p;
int TurtleFrame::back_ground = 0;
int TurtleFrame::screen_height = 0;
int TurtleFrame::screen_width = 0;
int TurtleFrame::in_graphics_mode = 0;
int TurtleFrame::in_splitscreen = 0;
pen_info TurtleFrame::xgr_pen = p;
int drawToWindow = 0; // for redraw_graphics from gui "thread"
wxDC *windowDC = 0;
wxMemoryDC *m_memDC;
#define USE_MEMDC 1
int pictureleft = 0, pictureright = 0, picturetop = 0, picturebottom = 0;
// Keep track of max range for printing.
int TurtleCanvas::mousePosition_x;
int TurtleCanvas::mousePosition_y;
int TurtleCanvas::clickPosition_x;
int TurtleCanvas::clickPosition_y;
int TurtleCanvas::mouse_down_left;
int TurtleCanvas::mouse_down_middle;
int TurtleCanvas::mouse_down_right;
int TurtleCanvas::mouse_down_last;
wxColour TurtleCanvas::colors[NUMCOLORS+SPECIAL_COLORS];
int R, G, B;
// Global print data, to remember settings during the session
wxPrintData *g_printData = (wxPrintData*) NULL ;
// Global page setup data
wxPageSetupDialogData* g_pageSetupData = (wxPageSetupDialogData*) NULL;
// Used for printing
TurtleWindowPrintout *turtlePrintout;
extern "C" int drawToPrinter;
wxDC *printerDC;
wxBitmap *tempBitmap;
// have already called prepareToDraw
int prepared = 0;
TurtleFrame *turtleFrame;
int turtleIndex = 0;
int putInQueue = 0;
#if 0
wxCommandEvent editEvent = wxCommandEvent(wxEVT_EDIT_CUSTOM_COMMAND);
#endif
char * file;
// the location of the turtle
extern "C" int turtlePosition_x;
extern "C" int turtlePosition_y;
#define LINEPAUSE 30
// ----------------------------------------------------------------------------
// Debug Functions
// ----------------------------------------------------------------------------
/* We'll use 16 named colors for now (see xgraphics.h).
The ordering here corresponds to the zero-based argument
to setpencolor that gives that color -- pink is 12,
turquoise is 10 etc.
*/
/*
void PrintLines(){
struct line l;
unsigned int turtleIndex = 0;
for (; turtleIndex<lines.size();turtleIndex++) {
l = lines[turtleIndex];
wxdprintf("Pen color = %s ",
TurtleCanvas::colors[l.color+SPECIAL_COLORS]);
// dc.SetPen( wxPen(TurtleCanvas::colors[l.color+SPECIAL_COLORS],
1, wxSOLID) );
if(l.pm==PEN_REVERSE)
{wxdprintf("and reversed \n");}
else if(l.pm==PEN_ERASE)
{wxdprintf("and erased \n");}
else wxdprintf("and copy \n");
}
}*/
// ----------------------------------------------------------------------------
// TurtleCanvas Class
// ----------------------------------------------------------------------------
// this is used to make the logo thread freeze until the event
// is handled by the UI thread to avoid concurrency issues
int alreadyDone = 0;
/* The TurtleCanvas event table*/
BEGIN_EVENT_TABLE(TurtleCanvas, wxWindow)
EVT_PAINT (TurtleCanvas::OnPaint)
EVT_SIZE (TurtleCanvas::OnSize)
EVT_MOTION (TurtleCanvas::OnMouseMove)
#if 0
EVT_EDIT_CUSTOM_COMMAND(-1, TurtleCanvas::editCall)
#endif
EVT_SET_FOCUS( TurtleCanvas::OnFocus)
EVT_KILL_FOCUS(TurtleCanvas::LoseFocus)
EVT_LEFT_DOWN(TurtleCanvas::OnLeftDown)
EVT_LEFT_UP(TurtleCanvas::OnLeftUp)
EVT_MIDDLE_DOWN(TurtleCanvas::OnMiddleDown)
EVT_MIDDLE_UP(TurtleCanvas::OnMiddleUp)
EVT_RIGHT_DOWN(TurtleCanvas::OnRightDown)
EVT_RIGHT_UP(TurtleCanvas::OnRightUp)
EVT_ERASE_BACKGROUND(TurtleCanvas::OnEraseBackGround)
EVT_CHAR(TurtleCanvas::OnChar)
END_EVENT_TABLE()
/* The TurtleCanvas class is what the turtle is drawn on */
TurtleCanvas::TurtleCanvas(wxFrame *parent)
: wxWindow(parent, -1, wxDefaultPosition, wxDefaultSize,
wxNO_FULL_REPAINT_ON_RESIZE)
{
m_owner = parent;
m_show = Show_Lines;
m_clip = FALSE;
tempBitmap=0;
g_printData = new wxPrintData;
g_pageSetupData = new wxPageSetupDialogData;
// copy over initial paper size from print record
(*g_pageSetupData) = *g_printData;
// Set some initial page margins in mm.
g_pageSetupData->SetMarginTopLeft(wxPoint(15, 15));
g_pageSetupData->SetMarginBottomRight(wxPoint(15, 15));
#ifndef __WXMAC__ /* needed for wxWidgets 2.6 */
wxSetWorkingDirectory(wxStandardPaths::Get().GetDocumentsDir());
#endif
mousePosition_x = 0;
mousePosition_y = 0;
clickPosition_x = 0;
clickPosition_y = 0;
mouse_down_left = 0;
mouse_down_middle = 0;
mouse_down_right = 0;
mouse_down_last = 0;
// initialize the TurtleCanvas::colors
int i;
TurtleCanvas::colors[SPECIAL_COLORS] = wxColour(0, 0, 0);
TurtleCanvas::colors[SPECIAL_COLORS+1] = wxColour(0, 0, 255);
TurtleCanvas::colors[SPECIAL_COLORS+2] = wxColour(0, 255, 0);
TurtleCanvas::colors[SPECIAL_COLORS+3] = wxColour(0, 255, 255);
TurtleCanvas::colors[SPECIAL_COLORS+4] = wxColour(255, 0, 0);
TurtleCanvas::colors[SPECIAL_COLORS+5] = wxColour(255, 0, 255);
TurtleCanvas::colors[SPECIAL_COLORS+6] = wxColour(255, 255, 0);
TurtleCanvas::colors[SPECIAL_COLORS+7] = wxColour(255, 255, 255);
TurtleCanvas::colors[SPECIAL_COLORS+8] = wxColour(155, 96, 59);
TurtleCanvas::colors[SPECIAL_COLORS+9] = wxColour(197, 136, 18);
TurtleCanvas::colors[SPECIAL_COLORS+10] = wxColour(100, 162, 64);
TurtleCanvas::colors[SPECIAL_COLORS+11] = wxColour(120, 187, 187);
TurtleCanvas::colors[SPECIAL_COLORS+12] = wxColour(255, 149, 119);
TurtleCanvas::colors[SPECIAL_COLORS+13] = wxColour(144, 113, 208);
TurtleCanvas::colors[SPECIAL_COLORS+14] = wxColour(255, 163, 0);
TurtleCanvas::colors[SPECIAL_COLORS+15] = wxColour(183, 183, 183);
for(i=SPECIAL_COLORS+16;i<NUMCOLORS+SPECIAL_COLORS;i++){
TurtleCanvas::colors[i] =
TurtleCanvas::colors[(i-SPECIAL_COLORS)%NUMINITCOLORS+SPECIAL_COLORS];
}
turtleFrame->xgr_pen.vis = 0;
int screen_width, screen_height;
parent->GetSize(&screen_width, &screen_height);
setInfo(SCREEN_WIDTH, screen_width);
setInfo(SCREEN_HEIGHT, screen_height);
turtleFrame->xgr_pen.xpos = screen_width/2;
turtleFrame->xgr_pen.ypos = screen_height/2;
pictureleft = pictureright = screen_width/2;
picturetop = picturebottom = screen_height/2;
turtleFrame->xgr_pen.color = 7;
turtleFrame->xgr_pen.pw = 1;
turtleFrame->xgr_pen.pen_mode = PEN_DOWN;
#if USE_MEMDC
m_memDC=new wxMemoryDC();
//make a bitmap the maximum size of the screen (the monitor, not the drawing area)
int m_w,m_h; //monitor screen width and height
wxDisplaySize(&m_w,&m_h);
m_bitmap = new wxBitmap(m_w, m_h);
PrepareDC(*m_memDC);
wxBrush myBrush(TurtleCanvas::colors[turtleFrame->back_ground
+SPECIAL_COLORS], wxBRUSHSTYLE_SOLID);
m_memDC->SelectObject(*m_bitmap);
m_memDC->SetBackgroundMode( wxSOLID );
m_memDC->SetBackground( myBrush );
m_memDC->Clear();
//compute origin based on screen size vs monitor size
//(BIGWIDTH - SMALLWIDTH)/2
m_memDC->SetDeviceOrigin( (m_w - screen_width)/2,
(m_h - screen_height)/2 );
wxFont f(FONT_CFG(wx_font_face, wx_font_size));
m_memDC->SetFont(f);
#endif
}
TurtleCanvas::~TurtleCanvas() {
turtleGraphics = 0;
}
void TurtleCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
{
wxdprintf("OnPaint starts\n");
wxPaintDC dc(this);
OnDraw(dc);
wxdprintf("OnPaint ends\n");
}
void TurtleCanvas::OnSize(wxSizeEvent& event) {
wxdprintf("OnSize starts\n");
int m_w,m_h;
int screen_width, screen_height;
wxDisplaySize(&m_w,&m_h);
logoFrame->GetSize(&screen_width, &screen_height);
setInfo(SCREEN_WIDTH, screen_width);
setInfo(SCREEN_HEIGHT, screen_height);
#if USE_MEMDC
m_memDC->SetDeviceOrigin(m_w/2 - screen_width/2,
m_h/2 - screen_height/2);
#endif
wxClientDC dc(this);
OnDraw(dc);
wxdprintf("OnSize ends\n");
}
extern FLONUM turtle_x, turtle_y, turtle_heading;
extern "C" FLONUM x_scale, y_scale;
extern "C" BOOLEAN user_turtle_shown;
extern "C" FIXNUM g_round(FLONUM n);
void TurtleCanvas::DrawTurtle(wxDC &dc) {
FLONUM real_heading;
int left_x, left_y, right_x, right_y, top_x, top_y;
double cos_real_heading, sin_real_heading;
FLONUM delta_x, delta_y;
if (!user_turtle_shown) {
return;
}
const FLONUM degrad = 0.017453292520;
const int screen_width = wxGetInfo(SCREEN_WIDTH);
const int screen_height = wxGetInfo(SCREEN_HEIGHT);
const int screen_top = 0;
const int screen_left = 0;
const FLONUM turtle_side = 19.0;
const FLONUM turtle_half_bottom = 6.0;
const int screen_x_center = (screen_left + (screen_width)/2);
const int screen_y_center = (screen_top + (screen_height)/2);
real_heading = -turtle_heading + 90.0;
cos_real_heading = cos((FLONUM)(real_heading*degrad));
sin_real_heading = sin((FLONUM)(real_heading*degrad));
delta_x = x_scale*(FLONUM)(sin_real_heading*turtle_half_bottom);
delta_y = y_scale*(FLONUM)(cos_real_heading*turtle_half_bottom);
left_x = g_round(turtle_x - delta_x);
left_y = g_round(turtle_y + delta_y);
right_x = g_round(turtle_x + delta_x);
right_y = g_round(turtle_y - delta_y);
top_x = g_round(turtle_x + x_scale*(FLONUM)(cos_real_heading*turtle_side));
top_y = g_round(turtle_y + y_scale*(FLONUM)(sin_real_heading*turtle_side));
/* move to right, draw to left, draw to top, draw to right */
//move_to(screen_x_center + right_x, screen_y_center - right_y);
//line_to(screen_x_center + left_x, screen_y_center - left_y);
dc.SetPen(wxPen(colors[turtleFrame->xgr_pen.color+SPECIAL_COLORS],
turtleFrame->xgr_pen.pw, wxPENSTYLE_SOLID));
dc.DrawLine(screen_x_center + right_x, screen_y_center - right_y,
screen_x_center + left_x, screen_y_center - left_y);
//line_to(screen_x_center + top_x, screen_y_center - top_y);
dc.DrawLine(screen_x_center + left_x, screen_y_center - left_y,
screen_x_center + top_x, screen_y_center - top_y);
//line_to(screen_x_center + right_x, screen_y_center - right_y);
dc.DrawLine(screen_x_center + top_x, screen_y_center - top_y,
screen_x_center + right_x, screen_y_center - right_y);
}
void TurtleCanvas::OnDraw(wxDC &dc) {
int x, y;
//dc.DestroyClippingRegion();
GetSize(&x, &y);
#if USE_MEMDC
dc.Blit(0,0,x,y,m_memDC,0,0);
DrawTurtle(dc);
return;
#endif
int unset_windowDC = 0;
if(windowDC == 0) { //OnSize may be triggered multiple times...
windowDC = &dc;
unset_windowDC++;
}
drawToWindow++;
redraw_graphics();
drawToWindow--;
if(unset_windowDC) {
windowDC = 0;
unset_windowDC--;
}
}
void TurtleCanvas::OnFocus (wxFocusEvent & event){
wxTerminal::terminal->SetFocus();
}
void TurtleCanvas::LoseFocus (wxFocusEvent & event){
}
void TurtleCanvas::SetOwner(wxFrame * owner) {
m_owner = owner;
}
wxFrame * TurtleCanvas::GetOwner() {
return m_owner;
}
wxDC * TurtleCanvas::GetDC() {
return dc;
}
void TurtleCanvas::OnEraseBackGround(wxEraseEvent& event) {
wxdprintf("Executing OnEraseBackGround\n");
}
void TurtleCanvas::drawOneLine(struct line *l, wxDC *dc) {
wxPen myPen;
wxColour xorColor;
if (l->pm==PEN_ERASE) {
myPen = wxPen(TurtleCanvas::colors[turtleFrame->back_ground+
SPECIAL_COLORS],
l->pw, wxPENSTYLE_SOLID);
} else if (l->pm==PEN_REVERSE) {
unsigned int pr, pg, pb, br, bg, bb;
get_palette(l->color, &pr, &pg, &pb);
get_palette(turtleFrame->back_ground, &br, &bg, &bb);
xorColor=wxColour((pr^br)/256, (pg^bg)/256, (pb^bb)/256);
myPen = wxPen(xorColor, l->pw, wxPENSTYLE_SOLID);
} else if(drawToPrinter && turtleFrame->back_ground==0 && l->color==7){
myPen = wxPen( wxT("black"), l->pw, wxPENSTYLE_SOLID);
} else {
myPen = wxPen(TurtleCanvas::colors[l->color+SPECIAL_COLORS],
l->pw, wxPENSTYLE_SOLID);
}
dc->SetPen(myPen);
if(l->pm==PEN_REVERSE){
dc->SetLogicalFunction(wxXOR);
} else {
dc->SetLogicalFunction(wxCOPY);
}
if(l->vis) {
dc->DrawLine(l->x1,l->y1,l->x2,l->y2);
dc->DrawPoint(l->x2,l->y2); /* draw endpoint */
if (!drawToPrinter && !drawToWindow) {
if (l->x2 < pictureleft) pictureleft = l->x2;
if (l->x2 > pictureright) pictureright = l->x2;
if (l->y2 < picturetop) picturetop = l->y2;
if (l->y2 > picturebottom) picturebottom = l->y2;
}
}
turtlePosition_x = l->x2;
turtlePosition_y = l->y2;
dc->SetPen(wxNullPen);
}
extern "C" int turtle_shown;
extern "C" void draw_turtle();
extern int editor_active; //from TextEditor.cpp
void TurtleCanvas::editCall(){
editor_active = 1;
editWindow->Clear();
topsizer->Show(wxTerminal::terminal, 0);
topsizer->Show(turtleGraphics, 0);
topsizer->Show(editWindow, 1);
logoFrame->SetUpEditMenu();
topsizer->Layout();
editWindow->SetFocus();
FILE * filestream;
filestream = fopen(file, "r");
if (filestream == NULL) {
filestream = fopen(file, "w");
}
fclose(filestream);
editWindow->Load(wxString(file,wxConvUTF8));
//need to busy wait and handle events...
while(editor_active) {
if(check_wx_stop(1, 0))
break;
wxMilliSleep(10);
}
}
extern "C" void mouse_down(int);
void
TurtleCanvas::OnLeftDown(wxMouseEvent& event) {
mouse_down_left = 1;
mouse_down_last = 1;
clickPosition_x = mousePosition_x;
clickPosition_y = mousePosition_y;
mouse_down(0);
event.Skip(); //allow native handler to set focus
}
void TurtleCanvas::OnLeftUp(wxMouseEvent& event) {
mouse_down_left = 0;
event.Skip(); //allow native handler to set focus
}
void TurtleCanvas::OnMiddleDown(wxMouseEvent& event) {
mouse_down_middle = 3;
mouse_down_last = 3;
clickPosition_x = mousePosition_x;
clickPosition_y = mousePosition_y;
mouse_down(0);
event.Skip(); //allow native handler to set focus
}
void TurtleCanvas::OnMiddleUp(wxMouseEvent& event) {
mouse_down_middle = 0;
event.Skip(); //allow native handler to set focus
}
void TurtleCanvas::OnRightDown(wxMouseEvent& event) {
mouse_down_right = 2;
mouse_down_last = 2;
clickPosition_x = mousePosition_x;
clickPosition_y = mousePosition_y;
mouse_down(0);
event.Skip(); //allow native handler to set focus
}
void TurtleCanvas::OnRightUp(wxMouseEvent& event) {
mouse_down_right = 0;
event.Skip(); //allow native handler to set focus
}
void TurtleCanvas::OnMouseMove(wxMouseEvent& event){
mousePosition_x = event.m_x;
mousePosition_y = event.m_y;
event.Skip(); //allow native handler to set focus
}
extern "C" pen_info* getPen();
void TurtleCanvas::realClearScreen(wxDC *dc) {
wxBrush myBrush(TurtleCanvas::colors[turtleFrame->back_ground+
SPECIAL_COLORS], wxBRUSHSTYLE_SOLID);
if(drawToPrinter && turtleFrame->back_ground==0){
myBrush.SetColour(_T("white"));
}
dc->SetBackgroundMode( wxSOLID );
dc->SetBackground( myBrush );
dc->Clear();
if (!drawToPrinter && !drawToWindow) {
pictureleft = pictureright = getInfo(SCREEN_WIDTH)/2;
picturetop = picturebottom = getInfo(SCREEN_HEIGHT)/2;
}
}
void TurtleCanvas::realFloodFill(int color, wxDC *dc) {
wxColour c;
//fprintf(stderr, "realFloodFill: (x,y): (%d, %d)\n", turtlePosition_x, turtlePosition_y);
dc->GetPixel(turtlePosition_x, turtlePosition_y, &c);
wxBrush brush(TurtleCanvas::colors[color+SPECIAL_COLORS]);
dc->SetBrush(brush);
if (drawToPrinter) {
dc->SetUserScale(1.0, 1.0);
dc->FloodFill((long)(turtlePosition_x*fillScale),
(long)(turtlePosition_y*fillScale) , c);
dc->SetUserScale(fillScale, fillScale);
} else {
dc->FloodFill(turtlePosition_x, turtlePosition_y , c);
}
// dc->FloodFill(dc->LogicalToDeviceX(turtlePosition_x), dc->LogicalToDeviceY(turtlePosition_y) , c);
}
void TurtleCanvas::realdoFilled(int fillcolor, int count,
struct mypoint *points, wxDC *dc) {
wxPen myPen;
wxPoint *wxpoints = (wxPoint*)malloc(count*sizeof(wxPoint));
wxPoint *wxptr;
struct mypoint *ptr;
for (wxptr = wxpoints, ptr = points; ptr < &points[count];
wxptr++, ptr++) {
wxptr->x = ptr->x;
wxptr->y = ptr->y;
}
if(drawToPrinter && turtleFrame->back_ground==0 &&
turtleFrame->xgr_pen.color==7){
myPen = wxPen( wxT("black"), turtleFrame->xgr_pen.pw, wxPENSTYLE_SOLID);
} else {
myPen = wxPen(colors[turtleFrame->xgr_pen.color+SPECIAL_COLORS],
turtleFrame->xgr_pen.pw, wxPENSTYLE_SOLID);
}
dc->SetPen(myPen);
wxBrush brush(TurtleCanvas::colors[fillcolor+SPECIAL_COLORS], wxBRUSHSTYLE_SOLID);
dc->SetBrush(brush);
dc->DrawPolygon(count, wxpoints);
free(wxpoints);
}
extern "C" FLONUM y_scale;
extern "C" void wx_get_label_size(int *w, int *h) {
/* returns size in pixels; converted to turtle steps in wxterm.c */
int descent, extlead;
m_memDC->GetTextExtent(wxString("M", wxConvUTF8, wxString::npos), w, h, &descent, &extlead);
}
extern "C" void wx_adjust_label_height() {
//take the font name, change the size based on scrunch
int px_height = y_scale * label_height;
int descent, extlead;
int cw,ch;
wxFont label_font = m_memDC->GetFont();
//now, initial guess for pt size is height / 1.5
int font_size = px_height * 3 / 2;
label_font.SetPointSize(font_size);
m_memDC->SetFont(label_font);
m_memDC->GetTextExtent(wxString("M", wxConvUTF8, wxString::npos), &cw, &ch, &descent, &extlead);
//now... first figure out whether we undershot or overshot...
//this determines which direction to change the size
int tmp_height = ch;
wxFont tmp_font = label_font;
if(tmp_height < px_height) {
//increase the point size until we get two consecutive font sizes
//where one is below px_height and one is above px_height
int expected;
while(tmp_height < px_height) {
expected = tmp_font.GetPointSize() + 1;
while(tmp_font.GetPointSize() != expected && expected <= 100){
expected++;
tmp_font.SetPointSize(expected);
}
if (expected == 100) break;
m_memDC->SetFont(tmp_font);
m_memDC->GetTextExtent(wxString("M", wxConvUTF8, wxString::npos), &cw, &tmp_height, &descent, &extlead);
if(tmp_height >= px_height) break;
label_font.SetPointSize(expected);
ch = tmp_height;
}
}
else {
//same as above, but decrease instead
int expected;
while(tmp_height > px_height) {
expected = tmp_font.GetPointSize() - 1;
while(tmp_font.GetPointSize() != expected && expected >= 2){
expected--;
tmp_font.SetPointSize(expected);
}
if (expected == 2) break;
m_memDC->SetFont(tmp_font);
m_memDC->GetTextExtent(wxString("M", wxConvUTF8, wxString::npos), &cw, &tmp_height, &descent, &extlead);
if(tmp_height <= px_height) break;
label_font.SetPointSize(expected);
ch = tmp_height;
}
}
//now we have two fonts and two heights, we pick the closest one!
int curr_diff = ch - px_height;
int tmp_diff = tmp_height - px_height;
if(curr_diff < 0) curr_diff = -curr_diff;
if(tmp_diff < 0) tmp_diff = -tmp_diff;
// fprintf(stderr, "ph: %d, cpt: %d, ch: %d, tpt: %d, th: %d \n", px_height, label_font.GetPointSize(), ch, tmp_font.GetPointSize(), tmp_height);
if(curr_diff < tmp_diff) {
m_memDC->SetFont(label_font);
}
else if(curr_diff > tmp_diff) {
m_memDC->SetFont(tmp_font);
}
else {
// if difference are the same, pick the smaller one
if(ch < tmp_height) {
m_memDC->SetFont(label_font);
}
else {
m_memDC->SetFont(tmp_font);
}
}
}
bool TurtleCanvas::SetFont(const wxFont &f) {
m_memDC->SetFont(f);
wx_adjust_label_height();
return TRUE;
}
void TurtleCanvas::realDrawLabel(char *data, wxDC *dc) {
wxString s(data, wxConvUTF8);
wxCoord wid, ht;
dc->GetTextExtent(s, &wid, &ht);
dc->SetBackgroundMode(wxTRANSPARENT);
if (turtleFrame->back_ground == 0 && drawToPrinter) {
dc->SetTextBackground(TurtleCanvas::colors[SPECIAL_COLORS+7]);
if (turtleFrame->xgr_pen.color == 7)
dc->SetTextForeground(TurtleCanvas::colors[SPECIAL_COLORS+0]);
else
dc->SetTextForeground(TurtleCanvas::colors
[turtleFrame->xgr_pen.color+SPECIAL_COLORS]);
} else {
dc->SetTextBackground(TurtleCanvas::colors[turtleFrame->back_ground+
SPECIAL_COLORS]);
dc->SetTextForeground(TurtleCanvas::colors[turtleFrame->xgr_pen.color+
SPECIAL_COLORS]);
}
dc->DrawText(s, getPen()->xpos, getPen()->ypos-ht);
if (!drawToPrinter) {
if (getPen()->xpos < pictureleft) pictureleft = getPen()->xpos;
if (getPen()->xpos+wid > pictureright)
pictureright = getPen()->xpos+wid;
if (getPen()->ypos-ht < picturetop) picturetop = getPen()->ypos-ht;
if (getPen()->ypos > picturebottom) picturebottom = getPen()->ypos;
}
}
/* A setter function for various turtle graphics properties */
void TurtleCanvas::setInfo(int type, int val){
switch (type){
case SCREEN_WIDTH:
turtleFrame->screen_width = val;
break;
case SCREEN_HEIGHT:
turtleFrame->screen_height = val;
break;
case BACK_GROUND:
turtleFrame->back_ground = val;
break;
case IN_SPLITSCREEN:
turtleFrame->in_splitscreen = val;
break;
case IN_GRAPHICS_MODE:
turtleFrame->in_graphics_mode = val;
break;
}
}
/* A getter function for various turtle graphics properties */
int TurtleCanvas::getInfo(int type){
switch (type){
case SCREEN_WIDTH:
return turtleFrame->screen_width;
break;
case SCREEN_HEIGHT:
return turtleFrame->screen_height;
break;
case BACK_GROUND:
return turtleFrame->back_ground;
break;
case IN_SPLITSCREEN:
return turtleFrame->in_splitscreen;
break;
case IN_GRAPHICS_MODE:
return turtleFrame->in_graphics_mode;
break;
}
return -1;
}
void TurtleCanvas::OnPageSetup(wxCommandEvent& WXUNUSED(event))
{
(*g_pageSetupData) = *g_printData;
wxPageSetupDialog pageSetupDialog(this, g_pageSetupData);
pageSetupDialog.ShowModal();
(*g_printData) = pageSetupDialog.GetPageSetupDialogData().GetPrintData();
(*g_pageSetupData) = pageSetupDialog.GetPageSetupDialogData();
}
void TurtleCanvas::PrintTurtleWindow(wxCommandEvent& WXUNUSED(event)) {
wxPrintDialogData printDialogData(* g_printData);
wxPrinter printer(& printDialogData);
TurtleWindowPrintout printout(_T("Turtle Graphics"));
if (!printer.Print(turtleFrame, &printout, true /*prompt*/)) {
if (wxPrinter::GetLastError() == wxPRINTER_ERROR)
wxMessageBox(_T("There was a problem printing.\nPerhaps your current printer is not set correctly?"), _T("Printing"), wxOK);
} else {
(*g_printData) = printer.GetPrintDialogData().GetPrintData();
}
}
void TurtleCanvas::TurtlePrintPreview(wxCommandEvent& WXUNUSED(event)) {
// Pass two printout objects: for preview, and possible printing.
wxPrintDialogData printDialogData(* g_printData);
wxPrintPreview *preview = new wxPrintPreview(new TurtleWindowPrintout, new TurtleWindowPrintout, & printDialogData);
if (!preview->Ok())
{
delete preview;
wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK);
return;
}
preview->SetZoom(100);
wxPreviewFrame *frame = new wxPreviewFrame(preview, logoFrame, _T("Turtle Graphics Preview"), wxPoint(100, 100), wxSize(600, 650));
frame->Centre(wxBOTH);
frame->Initialize();
frame->Show();
}
// ----------------------------------------------------------------------------
// TurtleFrame Class
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(TurtleFrame, wxFrame)
END_EVENT_TABLE()
// frame constructor
TurtleFrame::TurtleFrame(wxFrame * parent, const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(/*(wxFrame *)NULL*/ parent, -1, title, pos, size,
wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
{
m_canvas = new TurtleCanvas( this );
}
void TurtleFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(TRUE);
}
TurtleCanvas * TurtleFrame::GetCanvas() {
return m_canvas;
}
// ----------------------------------------------------------------------------
// Functions called from the interpreter thread
// ----------------------------------------------------------------------------
extern "C" void nop() {
}
extern "C" void set_palette(int color, unsigned int r, unsigned int g, unsigned int b){
TurtleCanvas::colors[color+SPECIAL_COLORS] =
wxColour(r/256,g/256,b/256);
}
extern "C" void get_palette(int color, unsigned int *r, unsigned int *g, unsigned int *b){
wxColour colour(TurtleCanvas::colors[color+SPECIAL_COLORS]);
*r = colour.Red()*256;
*g = colour.Green()*256;
*b = colour.Blue()*256;
}
extern "C" void save_pen(pen_info *p) {
memcpy(((char *)(p)),((char *)(&turtleFrame->xgr_pen)),
sizeof(pen_info));
}
extern "C" void restore_pen(pen_info *p) {
memcpy(((char *)(&turtleFrame->xgr_pen)),((char *)(p)),
sizeof(pen_info));
}
extern "C" void set_pen_patter(){
nop();
}
extern "C" void logofill() {
if (drawToPrinter)
TurtleCanvas::realFloodFill(turtleFrame->xgr_pen.color, printerDC);
else if (drawToWindow)
TurtleCanvas::realFloodFill(turtleFrame->xgr_pen.color, windowDC);
else {
#if USE_MEMDC
TurtleCanvas::realFloodFill(turtleFrame->xgr_pen.color, m_memDC);
#else
wxDC *dc = new wxClientDC(turtleGraphics);
TurtleCanvas::realFloodFill(turtleFrame->xgr_pen.color, dc);
delete dc;
#endif
}
}
extern "C" void wx_refresh() {
#if USE_MEMDC
if(turtleGraphics) {
turtleGraphics->Refresh();
turtleGraphics->Update();
}
#endif
}
/* Clear the turtle graphics screen, and put in splitscreen if we are
currently in full text mode */
extern "C" void wx_clear() {
if (drawToPrinter)
TurtleCanvas::realClearScreen(printerDC);
else if (drawToWindow)
TurtleCanvas::realClearScreen(windowDC);
else {
#if USE_MEMDC
TurtleCanvas::realClearScreen(m_memDC);
#else
wxDC *dc = new wxClientDC(turtleGraphics);
TurtleCanvas::realClearScreen(dc);
delete dc;
#endif
if(!TurtleFrame::in_graphics_mode)
wxSplitScreen();
}
return;
}
extern "C" char record_buffer[];
extern "C" void wxPrepare(){
if (drawToPrinter || drawToWindow) {
return;
}
if(!turtleFrame->in_graphics_mode) {
wxSplitScreen();
}
if(!prepared){
record_buffer[sizeof(int)] = 0;
wx_clear();
prepared = 1;
}
return;
}
/* Have turtle graphics draw the given line */
extern "C" void wxDrawLine(int x1, int y1, int x2, int y2, int vis){
static int numLines = 0;
if (!drawToPrinter && !drawToWindow) {
if (numLines == LINEPAUSE) {
wxMilliSleep(1);
numLines = 0;
}
else
numLines++;
}
wxDC *dc;
struct line l;
l.x1 = x1;
l.y1 = y1;
l.x2 = x2;
l.y2 = y2;
l.vis = vis;
l.color = turtleFrame->xgr_pen.color;
l.pm = turtleFrame->xgr_pen.pen_mode;
l.pw = turtleFrame->xgr_pen.pw;
if (drawToPrinter)
TurtleCanvas::drawOneLine(&l, printerDC);
else if (drawToWindow)
TurtleCanvas::drawOneLine(&l, windowDC);
else {
#if USE_MEMDC
TurtleCanvas::drawOneLine(&l, m_memDC);
#else
dc = new wxClientDC(turtleGraphics);
TurtleCanvas::drawOneLine(&l, dc);
delete dc;
#endif
}
return;
}