/
toby_wxwidgets.cpp
1722 lines (1403 loc) · 50.3 KB
/
toby_wxwidgets.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
/*
* Toby -- A programming language for learning.
* Copyright (C) 2007 Ryan C. Gordon.
*
* Please refer to LICENSE.txt in the root directory of the source
* distribution for licensing details.
*/
#if PLATFORM_MACOSX
#include <Carbon/Carbon.h>
#endif
#include <wx/wx.h>
#include <wx/config.h>
#include <wx/wfstream.h>
#include <wx/aboutdlg.h>
#include <wx/splitter.h>
#include <wx/filename.h>
#include <wx/print.h>
#include <wx/printdlg.h>
#include "toby_app.h"
// define something.
#if ((!TOBY_WX_BUILD_STANDALONE) && (!TOBY_WX_BUILD_IDE))
#error Nothing to build here.
#elif ((TOBY_WX_BUILD_STANDALONE) && (!TOBY_WX_BUILD_IDE))
#define TOBY_WX_STANDALONE_DEFAULT true
#else // Both IDE and standalone available, or just IDE.
#define TOBY_WX_STANDALONE_DEFAULT false
#endif
// Interfaces ...
// TurtleSpace is the canvas we draw on...it manages a backing store for
// repaints, if necessary for a given wxWidgets target, and handles
// the canvas if the window resizes.
class TurtleSpace : public wxWindow
{
public:
TurtleSpace(wxWindow *parent);
virtual ~TurtleSpace();
inline void startRun(bool runForPrinting);
inline void stopRun();
inline const wxFont *getFont() const { return &this->font; }
inline wxBitmap *getBacking() const;
inline void scaleXY(lua_Number &x, lua_Number &y) const;
bool drawString(lua_Number x, lua_Number y, const wxString &str,
lua_Number angle, int r, int g, int b);
void drawLine(lua_Number x1, lua_Number y1, lua_Number x2, lua_Number y2,
int r, int g, int b);
void drawTurtle(const Turtle *turtle, void *data);
void cleanup(int r, int g, int b, bool force=false);
// wxWidgets event handlers...
void onResize(wxSizeEvent &evt);
void onErase(wxEraseEvent &evt);
void onPaint(wxPaintEvent &evt);
private:
inline void nukeDC(wxMemoryDC **dc) { delete *dc; *dc = NULL; }
inline wxMemoryDC *getBackingDC() const { return this->backingDC; }
inline void constructBackingDC();
inline void calcOffset(int &xoff, int &yoff) const;
inline void clipDC(wxDC *dc, int xoff, int yoff) const;
const wxFont font;
int clientW; // width of physical window (changes on resize event).
int clientH; // height of physical window (changes on resize event).
int backingW; // width of backing store (changes on startRun()).
int backingH; // height of backing store (changes on startRun()).
wxBitmap *backing;
wxMemoryDC *backingDC;
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(TurtleSpace, wxWindow)
EVT_SIZE(TurtleSpace::onResize)
EVT_PAINT(TurtleSpace::onPaint)
EVT_ERASE_BACKGROUND(TurtleSpace::onErase)
END_EVENT_TABLE()
// Printing support class...
class TobyPrintout: public wxPrintout
{
public:
TobyPrintout(const wxChar *title = _T("Toby")) : wxPrintout(title) {}
virtual bool OnPrintPage(int page);
virtual bool HasPage(int page);
virtual void GetPageInfo(int *minPg, int *maxPg, int *selFrom, int *selTo);
};
enum TobyMenuCommands
{
// start with standard menu items, since using the wxIDs will map them
// to sane things in the platform's UI (gnome icons in GTK+, moves the
// about and quit items to the Apple menu on Mac OS X, etc).
MENUCMD_About = wxID_ABOUT,
MENUCMD_Quit = wxID_EXIT,
MENUCMD_Open = wxID_OPEN,
MENUCMD_New = wxID_NEW,
MENUCMD_Save = wxID_SAVE,
MENUCMD_SaveAs = wxID_SAVEAS,
MENUCMD_PageSetup = wxID_PAGE_SETUP,
MENUCMD_PrintPreview = wxID_PRINT_SETUP,
MENUCMD_Print = wxID_PRINT,
// non-standard menu items go here.
MENUCMD_Run = wxID_HIGHEST,
MENUCMD_RunForPrinting,
MENUCMD_Stop,
MENUCMD_Step,
MENUCMD_SaveAsImage,
MENUCMD_Cleanup,
MENUCMD_Website,
MENUCMD_License,
};
// TobyFrame is a standard wxFrame, but adds an interface for getting
// the actual TurtleSpace canvas and other highlevel wankery...there are two
// subclasses of this: one is a standalone window that only contains a
// turtlespace, and one is a full layout of controls for editing, debugging,
// etc.
// So to get the TurtleSpace to draw on from any arbitrary place, you'd do
// this: TurtleSpace *ts = wxGetApp().getTobyFrame()->getTurtleSpace();
class TobyFrame : public wxFrame
{
public:
TobyFrame();
virtual ~TobyFrame();
TurtleSpace *getTurtleSpace() const { return this->turtleSpace; }
static const wxPoint getPreviousPos();
static const wxSize getPreviousSize();
void openFile(const wxString &path);
void startRun();
void stopRun();
inline void pumpEvents();
inline void repaintTurtlespace();
inline void runProgram(bool printing, bool _breakAtStart=false);
void toggleWidgetsRunnable(bool readyToRun);
inline void requestQuit();
inline bool isQuitting() const { return this->quitting; }
// subclasses fill in these.
virtual void pauseReached(int line, int stopped, int bp, int ticks) {}
virtual void toggleWidgetsRunnableImpl(bool readyToRun) {}
virtual bool shouldVetoClose() { return false; }
virtual void closeImpl() {}
virtual void openFileImpl(char *buf) = 0;
virtual char *getProgramImpl() = 0;
virtual void onContinueImpl() {}
// wxWidgets event handlers...
void onIdle(wxIdleEvent &evt);
void onClose(wxCloseEvent &evt);
void onResize(wxSizeEvent &evt);
void onMove(wxMoveEvent &evt);
void onMenuQuit(wxCommandEvent &evt);
void onMenuPageSetup(wxCommandEvent &evt);
void onMenuPrintPreview(wxCommandEvent &evt);
void onMenuPrint(wxCommandEvent &evt);
void onMenuAbout(wxCommandEvent &evt);
void onMenuWebsite(wxCommandEvent &evt);
void onMenuLicense(wxCommandEvent &evt);
void onMenuOpen(wxCommandEvent &evt);
void onMenuSaveAsImage(wxCommandEvent &evt);
void onMenuCleanup(wxCommandEvent &evt);
void onMenuRun(wxCommandEvent &evt);
void onMenuStop(wxCommandEvent &evt);
void onMenuStep(wxCommandEvent &evt);
void onMenuRunForPrinting(wxCommandEvent &evt);
protected:
void updateTitleBar();
wxStopWatch profileStopwatch;
TurtleSpace *turtleSpace;
wxString filename;
bool modified;
int nonMaximizedX;
int nonMaximizedY;
int nonMaximizedWidth;
int nonMaximizedHeight;
bool quitting;
bool runForPrinting;
bool breakAtStart;
char *execProgram;
wxMenu *fileMenu;
wxMenu *runMenu;
wxMenu *helpMenu;
wxMenuBar *menuBar;
private:
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(TobyFrame, wxFrame)
EVT_IDLE(TobyFrame::onIdle)
EVT_CLOSE(TobyFrame::onClose)
EVT_SIZE(TobyFrame::onResize)
EVT_MOVE(TobyFrame::onMove)
EVT_MENU(MENUCMD_Open, TobyFrame::onMenuOpen)
EVT_MENU(MENUCMD_Quit, TobyFrame::onMenuQuit)
EVT_MENU(MENUCMD_SaveAsImage, TobyFrame::onMenuSaveAsImage)
EVT_MENU(MENUCMD_PageSetup, TobyFrame::onMenuPageSetup)
EVT_MENU(MENUCMD_PrintPreview, TobyFrame::onMenuPrintPreview)
EVT_MENU(MENUCMD_Print, TobyFrame::onMenuPrint)
EVT_MENU(MENUCMD_About, TobyFrame::onMenuAbout)
EVT_MENU(MENUCMD_Website, TobyFrame::onMenuWebsite)
EVT_MENU(MENUCMD_License, TobyFrame::onMenuLicense)
EVT_MENU(MENUCMD_Cleanup, TobyFrame::onMenuCleanup)
EVT_MENU(MENUCMD_Run, TobyFrame::onMenuRun)
EVT_MENU(MENUCMD_RunForPrinting, TobyFrame::onMenuRunForPrinting)
EVT_MENU(MENUCMD_Stop, TobyFrame::onMenuStop)
EVT_MENU(MENUCMD_Step, TobyFrame::onMenuStep)
END_EVENT_TABLE()
#if TOBY_WX_BUILD_STANDALONE
// This is a TobyFrame that only provides a TurtleSpace, basic menu items,
// and no other UI.
class TobyStandaloneFrame : public TobyFrame
{
public:
TobyStandaloneFrame();
virtual ~TobyStandaloneFrame() { delete[] this->sourceCode; }
virtual void openFileImpl(char *prog);
virtual char *getProgramImpl();
private:
char *sourceCode;
};
#endif
#if TOBY_WX_BUILD_IDE
// This is a TobyFrame that provides text editing and debugging facilities.
class TobyIDEFrame : public TobyFrame
{
public:
TobyIDEFrame();
virtual void openFileImpl(char *prog);
virtual char *getProgramImpl();
virtual bool shouldVetoClose();
virtual void toggleWidgetsRunnableImpl(bool readyToRun);
virtual void pauseReached(int line, int stopped, int bp, int ticks);
virtual void closeImpl();
virtual void onContinueImpl();
// wxWidgets event handlers...
void onMenuOpen(wxCommandEvent &evt);
void onMenuSave(wxCommandEvent &evt);
void onMenuSaveAs(wxCommandEvent &evt);
void onTextModified(wxCommandEvent &evt);
void onCallstackSelected(wxCommandEvent &evt);
private:
void updateVariablesCtrl(int frame);
wxSplitterWindow *topSplit;
wxSplitterWindow *ideSplit;
wxSplitterWindow *dbgSplit;
wxTextCtrl *textCtrl;
wxListBox *callstackCtrl;
wxListBox *variablesCtrl;
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(TobyIDEFrame, TobyFrame)
EVT_MENU(MENUCMD_Open, TobyIDEFrame::onMenuOpen)
EVT_MENU(MENUCMD_Save, TobyIDEFrame::onMenuSave)
EVT_MENU(MENUCMD_SaveAs, TobyIDEFrame::onMenuSaveAs)
EVT_TEXT(wxID_ANY, TobyIDEFrame::onTextModified) // !!! FIXME: not ID_ANY!
EVT_LISTBOX(wxID_ANY, TobyIDEFrame::onCallstackSelected) // !!! FIXME: not ID_ANY!
END_EVENT_TABLE()
#endif
class TobyLicenseDialog : public wxDialog
{
public:
TobyLicenseDialog(wxWindow *parent);
void onOK(wxCommandEvent &evt);
enum
{
dlgFlags = wxRESIZE_BORDER | wxSYSTEM_MENU | wxCLOSE_BOX |
wxMAXIMIZE_BOX | wxMINIMIZE_BOX | wxCAPTION
};
private:
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(TobyLicenseDialog, wxDialog)
EVT_BUTTON(wxID_OK, TobyLicenseDialog::onOK)
END_EVENT_TABLE()
// This is the the Application itself.
class TobyWxApp : public wxApp
{
public:
TobyWxApp() : mainWindow(NULL), printData(NULL) { /* no-op. */ }
virtual bool OnInit();
virtual int OnExit();
TobyFrame *getTobyFrame() const { return this->mainWindow; }
inline wxPrintData *getPrintData();
inline long getTicks() { return this->processStopwatch.Time(); }
private:
TobyFrame *mainWindow;
wxPrintData *printData;
wxStopWatch processStopwatch;
};
DECLARE_APP(TobyWxApp)
// implementations of callbacks during execution of Toby programs...
long TOBY_getTicks(void)
{
return wxGetApp().getTicks();
} // TOBY_getTicks
void TOBY_yieldCPU(int ms)
{
::wxMilliSleep(ms);
} // TOBY_yieldCPU
void TOBY_startRun()
{
wxGetApp().getTobyFrame()->startRun();
} // TOBY_startRun
void TOBY_stopRun()
{
wxGetApp().getTobyFrame()->stopRun();
} // TOBY_stopRun
void TOBY_pumpEvents()
{
wxGetApp().getTobyFrame()->pumpEvents();
} // TOBY_pumpEvents
void TOBY_putToScreen()
{
wxGetApp().getTobyFrame()->repaintTurtlespace();
} // TOBY_putToScreen
int TOBY_drawString(lua_Number x, lua_Number y, const char *utf8str,
lua_Number angle, int r, int g, int b)
{
const wxString wxstr(utf8str, wxConvUTF8);
TurtleSpace *tspace = wxGetApp().getTobyFrame()->getTurtleSpace();
return tspace->drawString(x, y, wxstr, angle, r, g, b) ? 1 : 0;
} // TOBY_drawString
void TOBY_drawLine(lua_Number x1, lua_Number y1, lua_Number x2, lua_Number y2,
int r, int g, int b)
{
wxGetApp().getTobyFrame()->getTurtleSpace()->drawLine(x1,y1,x2,y2,r,g,b);
} // TOBY_drawLine
void TOBY_drawTurtle(const Turtle *turtle, void *data)
{
wxGetApp().getTobyFrame()->getTurtleSpace()->drawTurtle(turtle, data);
} // TOBY_drawTurtle
void TOBY_pauseReached(int line, int fullstop, int breakpoint, int ticks)
{
wxGetApp().getTobyFrame()->pauseReached(line, fullstop, breakpoint, ticks);
} // TOBY_pauseReached
void TOBY_cleanup(int r, int g, int b)
{
wxGetApp().getTobyFrame()->getTurtleSpace()->cleanup(r, g, b);
} // TOBY_cleanup
void TOBY_messageBox(const char *msg)
{
::wxMessageBox(wxString(msg, wxConvUTF8), wxString(wxT("Toby")));
} // TOBY_messageBox
// The rest of the application...
TurtleSpace::TurtleSpace(wxWindow *parent)
: wxWindow(parent, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
, font(12, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL)
, clientW(1)
, clientH(1)
, backingW(1)
, backingH(1)
, backing(NULL)
, backingDC(NULL)
{
// no-op
} // TurtleSpace::TurtleSpace
TurtleSpace::~TurtleSpace()
{
delete this->backingDC;
delete this->backing;
} // TurtleSpace::~TurtleSpace
void TurtleSpace::constructBackingDC()
{
wxASSERT(this->backingDC == NULL);
this->backingDC = new wxMemoryDC(*this->backing);
this->backingDC->SetFont(this->font);
} // TurtleSpace::constructBackingDC
wxBitmap *TurtleSpace::getBacking() const
{
wxASSERT(this->backingDC == NULL);
return this->backing;
} // TurtleSpace::getBacking
void TurtleSpace::scaleXY(lua_Number &x, lua_Number &y) const
{
// !!! FIXME: Fixed point?
x = (((lua_Number) this->backingW) * (x / N(1000)));
y = (((lua_Number) this->backingH) * (y / N(1000)));
} // TurtleSpace::scaleXY
void TurtleSpace::calcOffset(int &xoff, int &yoff) const
{
xoff = (this->clientW - this->backingW) / 2;
yoff = (this->clientH - this->backingH) / 2;
} // TurtleSpace::calcOffset
void TurtleSpace::clipDC(wxDC *dc, int xoff, int yoff) const
{
const int w = this->backingW;
const int h = this->backingH;
if ((w < this->clientW) || (h < this->clientH))
{
dc->DestroyClippingRegion();
dc->SetClippingRegion(xoff, yoff, w, h);
} // if
} // TurtleSpace::clipDC
void TurtleSpace::drawLine(lua_Number x1, lua_Number y1,
lua_Number x2, lua_Number y2,
int r, int g, int b)
{
wxMemoryDC *dc = this->getBackingDC();
if (dc != NULL)
{
this->scaleXY(x1, y1);
this->scaleXY(x2, y2);
dc->SetPen(wxPen(wxColour(r, g, b)));
dc->DrawLine((wxCoord) x1, (wxCoord) y1, (wxCoord) x2, (wxCoord) y2);
} // if
} // TurtleSpace::drawLine
bool TurtleSpace::drawString(lua_Number x, lua_Number y, const wxString &str,
lua_Number angle, int r, int g, int b)
{
if (!this->getFont()->IsOk())
return false;
wxMemoryDC *dc = this->getBackingDC();
if (dc != NULL)
{
this->scaleXY(x, y);
const wxColour color(r, g, b);
dc->SetTextForeground(color);
dc->DrawRotatedText(str, (wxCoord) x, (wxCoord) y, angle);
} // if
return true;
} // TurtleSpace::drawString
void TurtleSpace::drawTurtle(const Turtle *turtle, void *data)
{
wxDC *dc = ( (data != NULL) ? ((wxDC *) data) : this->getBackingDC() );
if (dc != NULL)
{
int xoff = 0;
int yoff = 0;
if (data != NULL) // not the backing store? Clip it.
{
this->calcOffset(xoff, yoff);
this->clipDC(dc, xoff, yoff);
} // if
TurtlePoint tpts[4];
const lua_Number tx = turtle->pos.x;
const lua_Number ty = turtle->pos.y;
tpts[0].x = turtle->points[0].x + tx;
tpts[0].y = turtle->points[0].y + ty;
tpts[1].x = turtle->points[1].x + tx;
tpts[1].y = turtle->points[1].y + ty;
tpts[2].x = turtle->points[2].x + tx;
tpts[2].y = turtle->points[2].y + ty;
tpts[3].x = turtle->points[3].x + tx;
tpts[3].y = turtle->points[3].y + ty;
this->scaleXY(tpts[0].x, tpts[0].y);
this->scaleXY(tpts[1].x, tpts[1].y);
this->scaleXY(tpts[2].x, tpts[2].y);
this->scaleXY(tpts[3].x, tpts[3].y);
wxPoint points[3] =
{
wxPoint((wxCoord) tpts[0].x, (wxCoord) tpts[0].y),
wxPoint((wxCoord) tpts[1].x, (wxCoord) tpts[1].y),
wxPoint((wxCoord) tpts[2].x, (wxCoord) tpts[2].y),
};
const wxColor color(0, 255, 0); // full green.
dc->SetPen(wxPen(color));
dc->SetBrush(wxBrush(color));
dc->DrawPolygon(3, points, xoff, yoff);
dc->SetPen(wxPen(wxColor(0, 0, 255))); // full blue.
dc->DrawLine( ((wxCoord)tpts[0].x) + ((wxCoord)xoff),
((wxCoord)tpts[0].y) + ((wxCoord)yoff),
((wxCoord)tpts[3].x) + ((wxCoord)xoff),
((wxCoord)tpts[3].y) + ((wxCoord)yoff) );
} // if
} // TurtleSpace::drawTurtle
void TurtleSpace::cleanup(int r, int g, int b, bool force)
{
wxMemoryDC *dc = this->backingDC;
const bool haveDC = (dc != NULL);
if ((!haveDC) && (force))
{
this->constructBackingDC();
dc = this->backingDC;
} // if
if (dc != NULL)
{
dc->SetBackground(wxBrush(wxColour(r, g, b)));
dc->Clear();
} // if
if (!haveDC)
this->nukeDC(&this->backingDC);
} // TurtleSpace::cleanup
void TurtleSpace::startRun(bool runForPrinting)
{
int w = this->clientW;
int h = this->clientH;
if (runForPrinting)
{
// !!! FIXME
// Run with a target of 300dpi on 8x11 paper. This isn't really
// right, but it's easy for now. Ideally we throw up a printer
// dialog and get the exact dimensions from the DC, but that adds
// all sorts of event queue tapdancing...
w = (8 * 300);
h = (11 * 300);
} // if
if (w > h)
w = h;
else
h = w;
// (this may be NULL already, that's okay.)
this->nukeDC(&this->backingDC);
// resized since last run?
if ((w != this->backingW) || (h != this->backingH))
{
delete this->backing; // (this may be NULL already, that's okay.)
this->backing = NULL;
} // if
// Build new backing store if necessary...
if (this->backing == NULL)
{
this->backing = new wxBitmap(w, h);
this->backingW = w;
this->backingH = h;
} // if
this->constructBackingDC();
} // TurtleSpace::startRun
void TurtleSpace::stopRun()
{
this->nukeDC(&this->backingDC); // flush to bitmap.
} // TurtleSpace::stopRun
void TurtleSpace::onResize(wxSizeEvent &evt)
{
// Just cache the dimensions, since we're spending an enormous amount of
// time looking them up over and over during program execution.
this->GetClientSize(&this->clientW, &this->clientH);
} // TurtleSpace::onResize
void TurtleSpace::onErase(wxEraseEvent &evt)
{
// We don't want to do anything here, since we redraw the whole window
// in the Paint event...catching the erase event and doing nothing
// prevents flicker on some platforms, though.
(void) evt.GetDC();
} // TurtleSpace::onErase
void TurtleSpace::onPaint(wxPaintEvent &evt)
{
wxPaintDC dc(this);
int xoff, yoff;
this->calcOffset(xoff, yoff);
int r, g, b;
TOBY_background(&r, &g, &b);
dc.SetBackground(wxBrush(wxColour(r, g, b)));
if (this->backing == NULL)
dc.Clear(); // no backing store, just dump the whole thing.
else
{
// !!! FIXME: clear to something other than the background color?
// delete in-progress backingDC to force flush of rendered data...
const bool hasBackingDC = (this->backingDC != NULL);
this->nukeDC(&this->backingDC);
dc.DrawBitmap(*this->backing, xoff, yoff, false);
TOBY_renderAllTurtles(&dc);
if (hasBackingDC)
this->constructBackingDC();
// If there's some space in the window that isn't covered by the
// bitmap, blank it out. We do a lot of tapdancing to try and clip
// out exactly what will need clearing.
if (yoff > 0)
{
const wxRect top(0, 0, this->clientW, yoff);
dc.DestroyClippingRegion();
dc.SetClippingRegion(top);
dc.Clear();
const wxRect bottom(0, this->backingH+yoff, this->clientW, yoff+1);
dc.DestroyClippingRegion();
dc.SetClippingRegion(bottom);
dc.Clear();
} // if
if (xoff > 0)
{
const wxRect left(0, 0, xoff, this->clientH);
dc.DestroyClippingRegion();
dc.SetClippingRegion(left);
dc.Clear();
const wxRect right(xoff+this->backingW, 0, xoff+1, this->clientH);
dc.DestroyClippingRegion();
dc.SetClippingRegion(right);
dc.Clear();
} // if
} // else
} // TurtleSpace::onPaint
bool TobyPrintout::OnPrintPage(int page)
{
const TobyFrame *tframe = wxGetApp().getTobyFrame();
wxASSERT(!TOBY_isRunning());
wxBitmap *bmp = tframe->getTurtleSpace()->getBacking();
if (bmp != NULL)
{
wxDC *dc = this->GetDC();
if (dc != NULL)
{
wxCoord dcw, dch;
dc->GetSize(&dcw, &dch);
int w = dcw;
int h = dch;
if (w > h) w = h; else h = w; // keep it square.
wxImage img(bmp->ConvertToImage());
img.Rescale(w, h, wxIMAGE_QUALITY_HIGH);
dc->DrawBitmap(wxBitmap(img), (dcw-w)/2, (dch-h)/2, false);
return true;
} // if
} // if
return false;
} // TobyPrintout::OnPrintPage
void TobyPrintout::GetPageInfo(int *minPage, int *maxPage,
int *selPageFrom, int *selPageTo)
{
*minPage = *maxPage = *selPageFrom = *selPageTo = 1;
} // TobyPrintout::GetPageInfo
bool TobyPrintout::HasPage(int pageNum)
{
return (pageNum == 1);
} // TobyPrint/out::HasPage
TobyFrame::TobyFrame()
: wxFrame(NULL, -1, wxT("Toby"), getPreviousPos(), getPreviousSize())
, turtleSpace(new TurtleSpace(this))
, modified(false)
, nonMaximizedX(0)
, nonMaximizedY(0)
, nonMaximizedWidth(0)
, nonMaximizedHeight(0)
, quitting(false)
, runForPrinting(false)
, breakAtStart(false)
, execProgram(NULL)
, fileMenu(new wxMenu)
, runMenu(new wxMenu)
, helpMenu(new wxMenu)
, menuBar(new wxMenuBar)
{
this->GetPosition(&this->nonMaximizedX, &this->nonMaximizedY);
this->GetSize(&this->nonMaximizedWidth, &this->nonMaximizedHeight);
// ...don't forget to resize turtleSpace somewhere in your subclass!
// !!! FIXME: overflowing 80 chars here...
// Set up common menu items. Subclasses will add more menus and menu items.
this->fileMenu->Append(MENUCMD_Open, wxT("&Open\tCtrl-O"));
this->fileMenu->Append(MENUCMD_SaveAsImage, wxT("Save As &Image"))->Enable(false);
this->fileMenu->Append(MENUCMD_PageSetup, wxT("Pa&ge Setup"));
this->fileMenu->Append(MENUCMD_PrintPreview, wxT("P&rint Preview"))->Enable(false);
this->fileMenu->Append(MENUCMD_Print, wxT("&Print\tCtrl-P"))->Enable(false);
this->fileMenu->AppendSeparator();
this->fileMenu->Append(MENUCMD_Quit, wxT("E&xit\tCtrl-X"));
this->runMenu->Append(MENUCMD_Run, wxT("&Run Program\tF5"))->Enable(false);
this->runMenu->Append(MENUCMD_Stop, wxT("&Stop Program\tF6"))->Enable(false);
this->runMenu->Append(MENUCMD_RunForPrinting, wxT("R&un Program for Printing"))->Enable(false);
this->runMenu->Append(MENUCMD_Cleanup, wxT("&Clean up TurtleSpace"))->Enable(false);
this->helpMenu->Append(MENUCMD_About, wxT("&About\tF1"));
this->helpMenu->Append(MENUCMD_License, wxT("&License"));
this->helpMenu->Append(MENUCMD_Website, wxT("Toby on the &Web"));
this->menuBar->Append(this->fileMenu, wxT("&File"));
this->menuBar->Append(this->runMenu, wxT("&Run"));
this->menuBar->Append(this->helpMenu, wxT("&Help"));
this->SetMenuBar(this->menuBar);
long mx = 0;
wxConfigBase *cfg = wxConfig::Get();
if ((cfg != NULL) && (cfg->Read(wxT("Maximized"), &mx)) && (mx))
this->Maximize();
this->updateTitleBar();
} // TobyFrame::TobyFrame
TobyFrame::~TobyFrame()
{
delete[] this->execProgram;
// this->turtleSpace should be deleted by the subclass (because it usually
// wants to be killed by a wxSizer ...)
// this->fileMenu, etc is deleted by wxFrame when it cleans up the menubar.
} // TobyFrame::~TobyFrame
const wxPoint TobyFrame::getPreviousPos()
{
int dpyw, dpyh;
::wxDisplaySize(&dpyw, &dpyh);
long winx, winy, winw, winh;
wxConfigBase *cfg = wxConfig::Get();
if ( (cfg == NULL) ||
(!cfg->Read(wxT("WindowX"), &winx)) ||
(!cfg->Read(wxT("WindowY"), &winy)) ||
(!cfg->Read(wxT("WindowW"), &winw)) ||
(!cfg->Read(wxT("WindowH"), &winh)) )
return wxPoint(dpyw / 8, dpyh / 8);
if (winw > dpyw) { winw = dpyw; winx = 0; }
else if (winw < 50) winw = 50;
if (winh > dpyh) { winh = dpyh; winy = 0; }
else if (winh < 50) winh = 50;
if (winx+winw < 10) winx = 0;
else if (winx > dpyw-10) winx = dpyw-10;
if (winy < 0) winy = 0;
else if (winy > dpyh-10) winy = dpyh-10;
return wxPoint(winx, winy);
} // TobyFrame::getPreviousPos
const wxSize TobyFrame::getPreviousSize()
{
int dpyw, dpyh;
::wxDisplaySize(&dpyw, &dpyh);
long winw, winh;
wxConfigBase *cfg = wxConfig::Get();
if ( (cfg == NULL) ||
(!cfg->Read(wxT("WindowW"), &winw)) ||
(!cfg->Read(wxT("WindowH"), &winh)) )
return wxSize(dpyw - (dpyw / 4), dpyh - (dpyh / 4));
if (winw > dpyw) winw = dpyw;
else if (winw < 50) winw = 50;
if (winh > dpyh) winh = dpyh;
else if (winh < 50) winh = 50;
return wxSize(winw, winh);
} // TobyFrame::getPreviousSize
void TobyFrame::pumpEvents()
{
while (wxGetApp().Pending())
wxGetApp().Dispatch();
} // TobyFrame::pumpEvents
void TobyFrame::repaintTurtlespace()
{
TurtleSpace *tspace = this->turtleSpace;
tspace->Refresh(false);
tspace->Update(); // force repaint.
} // TobyFrame::repaintTurtlespace
void TobyFrame::toggleWidgetsRunnable(bool readyToRun)
{
wxMenuBar *mb = this->menuBar;
mb->FindItem(MENUCMD_Run)->Enable(readyToRun);
mb->FindItem(MENUCMD_Stop)->Enable(!readyToRun);
mb->FindItem(MENUCMD_RunForPrinting)->Enable(readyToRun);
mb->FindItem(MENUCMD_SaveAsImage)->Enable(readyToRun);
mb->FindItem(MENUCMD_Print)->Enable(readyToRun);
mb->FindItem(MENUCMD_PrintPreview)->Enable(readyToRun);
mb->FindItem(MENUCMD_Cleanup)->Enable(readyToRun);
this->toggleWidgetsRunnableImpl(readyToRun);
} // TobyFrame::toggleWidgetsRunnable
void TobyFrame::startRun()
{
wxASSERT(!TOBY_isRunning());
this->toggleWidgetsRunnable(false);
this->turtleSpace->startRun(this->runForPrinting);
if (this->breakAtStart)
TOBY_stepProgram();
this->SetStatusText(wxT("Now running..."));
this->profileStopwatch.Start(0);
} // TobyFrame::startRun
void TobyFrame::stopRun()
{
wxASSERT(TOBY_isRunning());
const long ms = this->profileStopwatch.Time();
this->SetStatusText(wxString::Format(wxT("...ran %ld milliseconds."), ms));
this->toggleWidgetsRunnable(true);
this->turtleSpace->stopRun();
} // TobyFrame::stopRun
void TobyFrame::requestQuit()
{
TOBY_haltProgram();
this->quitting = true;
} // TobyFrame::requestQuit
void TobyFrame::runProgram(bool _runForPrinting, bool _breakAtStart)
{
TOBY_haltProgram(); // stop the current run as soon as possible.
// This gets kicked off in the next idle event.
this->runForPrinting = _runForPrinting;
delete[] this->execProgram;
this->execProgram = this->getProgramImpl();
this->breakAtStart = _breakAtStart;
} // TobyFrame::runProgram
void TobyFrame::onIdle(wxIdleEvent &evt)
{
if (this->isQuitting())
{
if (TOBY_isRunning())
TOBY_haltProgram();
else
{
this->quitting = false;
this->Close(false);
} // else
} // if
else if (this->execProgram != NULL)
{
if (TOBY_isRunning())
TOBY_haltProgram();
else
{
char *prog = this->execProgram;
this->execProgram = NULL;
TOBY_runProgram(prog, this->runForPrinting);
delete[] prog;
} // else
} // else if
} // TobyFrame::onIdle
void TobyFrame::updateTitleBar()
{
wxString fnstr;
if (!this->filename.IsEmpty())
fnstr = wxFileName(this->filename).GetFullName();
else
fnstr = wxT("New file");
wxString modstr(this->modified ? wxT("*") : wxT(""));
this->SetTitle(wxString::Format(wxT("Toby - %s%s"),
fnstr.c_str(), modstr.c_str()));
} // TobyFrame::updateTitleBar
void TobyFrame::openFile(const wxString &path)
{
wxFileInputStream strm(path);
if (strm.IsOk())
{
size_t len = strm.GetLength();
char *buf = new char[len + 1];
if (!strm.Read(buf, len).IsOk())
{
TOBY_messageBox("Could not read file");
delete[] buf;
} // if
else
{
buf[len] = '\0';
this->filename = path;
this->modified = false;
this->updateTitleBar();
this->openFileImpl(buf); // openFileImpl will delete[]!
} // else
} // else
} // TobyFrame::openFile
void TobyFrame::onMenuOpen(wxCommandEvent& evt)
{
wxASSERT(!TOBY_isRunning());
TOBY_haltProgram(); // just in case.
// !!! FIXME: localization.
wxFileDialog dlg(this, wxT("Choose a file to open"), wxT(""), wxT(""),
wxT("Toby Programs (*.toby)|*.toby"),
wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (dlg.ShowModal() == wxID_OK)
openFile(dlg.GetPath());
} // TobyFrame::onMenuOpen