File: wxedit.cpp

package info (click to toggle)
golly 3.3-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 20,176 kB
  • sloc: cpp: 72,638; ansic: 25,919; python: 7,921; sh: 4,245; objc: 3,721; java: 2,781; xml: 1,362; makefile: 530; javascript: 279; perl: 69
file content (972 lines) | stat: -rw-r--r-- 32,808 bytes parent folder | download | duplicates (2)
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
// This file is part of Golly.
// See docs/License.html for the copyright notice.

#include "wx/wxprec.h"      // for compilers that support precompilation
#ifndef WX_PRECOMP
    #include "wx/wx.h"      // for all others include the necessary headers
#endif

#include "wx/dcbuffer.h"    // for wxBufferedPaintDC
#if wxUSE_TOOLTIPS
    #include "wx/tooltip.h" // for wxToolTip
#endif

#include "bigint.h"
#include "lifealgo.h"

#include "wxgolly.h"       // for viewptr, statusptr, mainptr
#include "wxmain.h"        // for mainptr->...
#include "wxutils.h"       // for Fatal
#include "wxprefs.h"       // for showedit, showallstates, etc
#include "wxstatus.h"      // for statusptr->...
#include "wxview.h"        // for viewptr->...
#include "wxrender.h"      // for DrawOneIcon
#include "wxlayer.h"       // for currlayer, LayerBarHeight, SetLayerColors
#include "wxundo.h"        // for currlayer->undoredo->...
#include "wxtimeline.h"    // for TimelineExists
#include "wxedit.h"

// -----------------------------------------------------------------------------

enum {
    // ids for bitmap buttons in edit bar
    UNDO_BUTT = 0,
    REDO_BUTT,
    DRAW_BUTT,
    PICK_BUTT,
    SELECT_BUTT,
    MOVE_BUTT,
    ZOOMIN_BUTT,
    ZOOMOUT_BUTT,
    ALLSTATES_BUTT,
    NUM_BUTTONS,      // must be after all buttons
    STATE_BAR
};

// bitmaps for edit bar buttons
#include "bitmaps/undo.xpm"
#include "bitmaps/redo.xpm"
#include "bitmaps/draw.xpm"
#include "bitmaps/pick.xpm"
#include "bitmaps/select.xpm"
#include "bitmaps/move.xpm"
#include "bitmaps/zoomin.xpm"
#include "bitmaps/zoomout.xpm"
#include "bitmaps/allstates.xpm"
// bitmaps for down state of toggle buttons
#include "bitmaps/draw_down.xpm"
#include "bitmaps/pick_down.xpm"
#include "bitmaps/select_down.xpm"
#include "bitmaps/move_down.xpm"
#include "bitmaps/zoomin_down.xpm"
#include "bitmaps/zoomout_down.xpm"
#include "bitmaps/allstates_down.xpm"

// width and height of bitmap buttons
#if defined(__WXOSX_COCOA__) && wxCHECK_VERSION(3,0,0)
    const int BUTTON_WD = 24;
    const int BUTTON_HT = 24;
#elif defined(__WXOSX_COCOA__) || defined(__WXGTK__)
    const int BUTTON_WD = 28;
    const int BUTTON_HT = 28;
#else
    const int BUTTON_WD = 24;
    const int BUTTON_HT = 24;
#endif

// -----------------------------------------------------------------------------

// Define edit bar window:

// derive from wxPanel so we get current theme's background color on Windows
class EditBar : public wxPanel
{
public:
    EditBar(wxWindow* parent, wxCoord xorg, wxCoord yorg, int wd, int ht);
    ~EditBar();
    
    // add a bitmap button to edit bar
    void AddButton(int id, const wxString& tip);
    
    // add gap between buttons
    void AddSeparator();
    
    // enable/disable button
    void EnableButton(int id, bool enable);
    
    // set state of a toggle button
    void SelectButton(int id, bool select);
    
    // update scroll bar
    void UpdateScrollBar();
    
    // detect press and release of a bitmap button
    void OnButtonDown(wxMouseEvent& event);
    void OnButtonUp(wxMouseEvent& event);
    void OnKillFocus(wxFocusEvent& event);
    
private:
    // any class wishing to process wxWidgets events must use this macro
    DECLARE_EVENT_TABLE()
    
    // event handlers
    void OnPaint(wxPaintEvent& event);
    void OnMouseDown(wxMouseEvent& event);
    void OnButton(wxCommandEvent& event);
    void OnScroll(wxScrollEvent& event);
    
    void SetEditFont(wxDC& dc);
    void DisplayText(wxDC& dc, const wxString& s, wxCoord x, wxCoord y);
    void DrawAllStates(wxDC& dc, int wd);
    void DrawEditBar(wxDC& dc, int wd, int ht);
    
    // bitmaps for normal or down state
    wxBitmap normbutt[NUM_BUTTONS];
    wxBitmap downbutt[NUM_BUTTONS];
    
#ifdef __WXMSW__
    // on Windows we need bitmaps for disabled buttons
    wxBitmap disnormbutt[NUM_BUTTONS];
    wxBitmap disdownbutt[NUM_BUTTONS];
#endif
    
    // remember state of toggle buttons to avoid unnecessary drawing;
    // 0 = not yet initialized, 1 = selected, -1 = not selected
    int buttstate[NUM_BUTTONS];
    
    // positioning data used by AddButton and AddSeparator
    int ypos, xpos, smallgap, biggap;
    
    wxBitmap* editbitmap;         // edit bar bitmap
    int editbitmapwd;             // width of edit bar bitmap
    int editbitmapht;             // height of edit bar bitmap
    
    wxRect colorbox;              // box showing current color
    wxRect iconbox;               // box showing current icon
    
    wxScrollBar* drawbar;         // scroll bar for changing drawing state
    int firststate;               // first visible state (if showing all states)
    
    int h_col1;                   // horizontal position of labels
    int h_col2;                   // horizontal position of info for state 0
    int digitwd;                  // width of digit in edit bar font
    int digitht;                  // height of digit in edit bar font
    int textascent;               // vertical adjustment used in DrawText calls
    wxFont* editfont;             // edit bar font
};

BEGIN_EVENT_TABLE(EditBar, wxPanel)
EVT_PAINT            (              EditBar::OnPaint)
EVT_LEFT_DOWN        (              EditBar::OnMouseDown)
EVT_LEFT_DCLICK      (              EditBar::OnMouseDown)
EVT_BUTTON           (wxID_ANY,     EditBar::OnButton)
EVT_COMMAND_SCROLL   (STATE_BAR,    EditBar::OnScroll)
END_EVENT_TABLE()

// -----------------------------------------------------------------------------

EditBar* editbarptr = NULL;         // global pointer to edit bar
const int BIGHT = 80;               // height of edit bar if showallstates
const int SMALLHT = 32;             // height of edit bar if not showallstates
static int editbarht;               // current height (BIGHT or SMALLHT)

const int LINEHT = 14;              // distance between each baseline
const int BASELINE1 = SMALLHT+LINEHT-1;   // baseline of 1st line
const int BASELINE2 = BASELINE1+LINEHT;   // baseline of 2nd line
const int BASELINE3 = BASELINE2+LINEHT;   // baseline of 3rd line
const int COLWD = 22;               // column width of state/color/icon info
const int BOXWD = 9;                // width (and height) of small color/icon boxes
const int BOXSIZE = 17;             // width and height of colorbox and iconbox
const int BOXGAP = 8;               // gap between colorbox and iconbox
const int PAGESIZE = 10;            // scroll amount when paging

// edit bar buttons (must be global to use Connect/Disconnect on Windows)
wxBitmapButton* ebbutt[NUM_BUTTONS];

// -----------------------------------------------------------------------------

EditBar::EditBar(wxWindow* parent, wxCoord xorg, wxCoord yorg, int wd, int ht)
: wxPanel(parent, wxID_ANY, wxPoint(xorg,yorg), wxSize(wd,ht),
#ifdef __WXMSW__
            // need this to avoid buttons flashing on Windows
            wxNO_FULL_REPAINT_ON_RESIZE
#else
            // better for Mac and Linux
            wxFULL_REPAINT_ON_RESIZE
#endif
            )
{
#ifdef __WXGTK__
    // avoid erasing background on GTK+
    SetBackgroundStyle(wxBG_STYLE_CUSTOM);
#endif
    
    // init bitmaps for normal state
    normbutt[UNDO_BUTT] =      XPM_BITMAP(undo);
    normbutt[REDO_BUTT] =      XPM_BITMAP(redo);
    normbutt[DRAW_BUTT] =      XPM_BITMAP(draw);
    normbutt[PICK_BUTT] =      XPM_BITMAP(pick);
    normbutt[SELECT_BUTT] =    XPM_BITMAP(select);
    normbutt[MOVE_BUTT] =      XPM_BITMAP(move);
    normbutt[ZOOMIN_BUTT] =    XPM_BITMAP(zoomin);
    normbutt[ZOOMOUT_BUTT] =   XPM_BITMAP(zoomout);
    normbutt[ALLSTATES_BUTT] = XPM_BITMAP(allstates);
    
    // toggle buttons also have a down state
    downbutt[DRAW_BUTT] =      XPM_BITMAP(draw_down);
    downbutt[PICK_BUTT] =      XPM_BITMAP(pick_down);
    downbutt[SELECT_BUTT] =    XPM_BITMAP(select_down);
    downbutt[MOVE_BUTT] =      XPM_BITMAP(move_down);
    downbutt[ZOOMIN_BUTT] =    XPM_BITMAP(zoomin_down);
    downbutt[ZOOMOUT_BUTT] =   XPM_BITMAP(zoomout_down);
    downbutt[ALLSTATES_BUTT] = XPM_BITMAP(allstates_down);
    
#ifdef __WXMSW__
    for (int i = 0; i < NUM_BUTTONS; i++) {
        CreatePaleBitmap(normbutt[i], disnormbutt[i]);
    }
    CreatePaleBitmap(downbutt[DRAW_BUTT],       disdownbutt[DRAW_BUTT]);
    CreatePaleBitmap(downbutt[PICK_BUTT],       disdownbutt[PICK_BUTT]);
    CreatePaleBitmap(downbutt[SELECT_BUTT],     disdownbutt[SELECT_BUTT]);
    CreatePaleBitmap(downbutt[MOVE_BUTT],       disdownbutt[MOVE_BUTT]);
    CreatePaleBitmap(downbutt[ZOOMIN_BUTT],     disdownbutt[ZOOMIN_BUTT]);
    CreatePaleBitmap(downbutt[ZOOMOUT_BUTT],    disdownbutt[ZOOMOUT_BUTT]);
    CreatePaleBitmap(downbutt[ALLSTATES_BUTT],  disdownbutt[ALLSTATES_BUTT]);
#endif
    
    for (int i = 0; i < NUM_BUTTONS; i++) {
        buttstate[i] = 0;
    }
    
    // init position variables used by AddButton and AddSeparator
#ifdef __WXGTK__
    // buttons are a different size in wxGTK
    xpos = 2;
    ypos = 2;
    smallgap = 6;
#else
    xpos = 4;
    ypos = (32 - BUTTON_HT) / 2;
    smallgap = 4;
#endif
    biggap = 16;
    
    // add buttons
    AddButton(UNDO_BUTT,       _("Undo"));
    AddButton(REDO_BUTT,       _("Redo"));
    AddSeparator();
    AddButton(DRAW_BUTT,       _("Draw"));
    AddButton(PICK_BUTT,       _("Pick"));
    AddButton(SELECT_BUTT,     _("Select"));
    AddButton(MOVE_BUTT,       _("Move"));
    AddButton(ZOOMIN_BUTT,     _("Zoom in"));
    AddButton(ZOOMOUT_BUTT,    _("Zoom out"));
    AddSeparator();
    AddButton(ALLSTATES_BUTT,  _("Show/hide all states"));
    
    // create font for text in edit bar and set textascent for use in DisplayText
#ifdef __WXMSW__
    // use smaller, narrower font on Windows
    editfont = wxFont::New(8, wxDEFAULT, wxNORMAL, wxNORMAL);
    int major, minor;
    wxGetOsVersion(&major, &minor);
    if ( major > 5 || (major == 5 && minor >= 1) ) {
        // 5.1+ means XP or later (Vista if major >= 6)
        textascent = 11;
    } else {
        textascent = 10;
    }
#elif defined(__WXGTK__)
    // use smaller font on GTK
    editfont = wxFont::New(8, wxMODERN, wxNORMAL, wxNORMAL);
    textascent = 11;
#elif defined(__WXOSX_COCOA__)
    // we need to specify facename to get Monaco instead of Courier
    editfont = wxFont::New(10, wxMODERN, wxNORMAL, wxNORMAL, false, wxT("Monaco"));
    textascent = 10;
#else
    editfont = wxFont::New(10, wxMODERN, wxNORMAL, wxNORMAL);
    textascent = 10;
#endif
    if (editfont == NULL) Fatal(_("Failed to create edit bar font!"));
    
    // determine horizontal offsets for info in edit bar
    wxClientDC dc(this);
    int textwd, textht;
    SetEditFont(dc);
    h_col1 = 4;
    dc.GetTextExtent(_("State:"), &textwd, &textht);
    h_col2 = h_col1 + textwd + 4;
    dc.GetTextExtent(_("9"), &digitwd, &digitht);
    digitht -= 4;
    
    editbitmap = NULL;
    editbitmapwd = -1;
    editbitmapht = -1;
    
    // add scroll bar
    int scrollbarwd = 100;
#ifdef __WXMAC__
    int scrollbarht = 15;   // must be this height on Mac
#else
    int scrollbarht = BOXSIZE;
#endif
    int x = xpos + 3*digitwd + BOXGAP + 2*(BOXSIZE + BOXGAP);
    int y = (SMALLHT - (scrollbarht + 1)) / 2;
    drawbar = new wxScrollBar(this, STATE_BAR, wxPoint(x, y),
                              wxSize(scrollbarwd, scrollbarht),
                              wxSB_HORIZONTAL);
    if (drawbar == NULL) Fatal(_("Failed to create scroll bar!"));
    firststate = 0;
}

// -----------------------------------------------------------------------------

EditBar::~EditBar()
{
    delete editfont;
    delete editbitmap;
    delete drawbar;
}

// -----------------------------------------------------------------------------

void EditBar::SetEditFont(wxDC& dc)
{
    dc.SetFont(*editfont);
    dc.SetTextForeground(*wxBLACK);
    dc.SetBrush(*wxBLACK_BRUSH);
    dc.SetBackgroundMode(wxTRANSPARENT);
}

// -----------------------------------------------------------------------------

void EditBar::DisplayText(wxDC& dc, const wxString& s, wxCoord x, wxCoord y)
{
    // DrawText's y parameter is top of text box but we pass in baseline
    // so adjust by textascent which depends on platform and OS version -- yuk!
    dc.DrawText(s, x, y - textascent);
}

// -----------------------------------------------------------------------------

void EditBar::DrawAllStates(wxDC& dc, int wd)
{
    DisplayText(dc, _("State:"), h_col1, BASELINE1);
    DisplayText(dc, _("Color:"), h_col1, BASELINE2);
    DisplayText(dc, _("Icon:"),  h_col1, BASELINE3);
    
    wxBitmap** iconmaps = currlayer->icons7x7;
    
    dc.SetPen(*wxBLACK_PEN);
    
    // calculate number of (completely) visible states
    int visstates = (wd - h_col2) / COLWD;
    if (visstates >= currlayer->algo->NumCellStates()) {
        // all states are visible
        firststate = 0;
        visstates = currlayer->algo->NumCellStates();
    } else {
        // change firststate if necessary so that drawing state is visible
        if (currlayer->drawingstate < firststate) {
            firststate = currlayer->drawingstate;
        } else if (currlayer->drawingstate >= firststate + visstates) {
            firststate = currlayer->drawingstate - visstates + 1;
        }
        // may need to reduce firststate if window width has increased
        if (firststate + visstates >= currlayer->algo->NumCellStates()) {
            firststate = currlayer->algo->NumCellStates() - visstates;
        }
    }
    
    // add 1 to visstates so we see partial box at right edge
    for (int i = firststate; i < firststate + visstates + 1; i++) {
        
        // this test is needed because we added 1 to visstates
        if (i >= currlayer->algo->NumCellStates()) break;
        
        wxString strbuf;
        wxRect r;
        int x;
        
        // draw state value
        strbuf.Printf(_("%d"), i);
        x = (int)(h_col2 + (i - firststate) * COLWD + (COLWD - strbuf.length() * digitwd) / 2);
        DisplayText(dc, strbuf, x, BASELINE1);
        
        // draw color box
        x = 1 + h_col2 + (i - firststate) * COLWD + (COLWD - BOXWD) / 2;
        wxColor color(currlayer->cellr[i], currlayer->cellg[i], currlayer->cellb[i]);
        r = wxRect(x, BASELINE2 - BOXWD, BOXWD, BOXWD);
        dc.SetBrush(wxBrush(color));
        dc.DrawRectangle(r);
        dc.SetBrush(wxNullBrush);
        
        // draw icon box
        r = wxRect(x, BASELINE3 - BOXWD, BOXWD, BOXWD);
        if (iconmaps && iconmaps[i]) {
            dc.SetBrush(*wxTRANSPARENT_BRUSH);
            dc.DrawRectangle(r);
            dc.SetBrush(wxNullBrush);
            DrawOneIcon(dc, x + 1, BASELINE3 - BOXWD + 1, iconmaps[i],
                        currlayer->cellr[0], currlayer->cellg[0], currlayer->cellb[0],
                        currlayer->cellr[i], currlayer->cellg[i], currlayer->cellb[i],
                        currlayer->multicoloricons);
        } else {
            dc.SetBrush(wxBrush(color));
            dc.DrawRectangle(r);
            dc.SetBrush(wxNullBrush);
        }
    }
    
    // draw rect around current drawing state
    if (currlayer->drawingstate >= firststate &&
        currlayer->drawingstate <= firststate + visstates) {
        int x = 1 + h_col2 + (currlayer->drawingstate - firststate) * COLWD;
#ifdef __WXGTK__
        wxRect r(x, SMALLHT + 1, COLWD - 1, BIGHT - SMALLHT - 5);
#else
        wxRect r(x, SMALLHT + 2, COLWD - 1, BIGHT - SMALLHT - 5);
#endif
        dc.SetBrush(*wxTRANSPARENT_BRUSH);
        dc.DrawRectangle(r);
        dc.SetBrush(wxNullBrush);
    }
    
    dc.SetPen(wxNullPen);
}

// -----------------------------------------------------------------------------

void EditBar::DrawEditBar(wxDC& dc, int wd, int ht)
{
    wxRect r = wxRect(0, 0, wd, ht);
    
#ifdef __WXMAC__
    wxBrush brush(wxColor(202,202,202));
    FillRect(dc, r, brush);
#endif

#ifdef __WXMSW__
    // use theme background color on Windows
    wxBrush brush(GetBackgroundColour());
    FillRect(dc, r, brush);
#endif
    
    // draw gray border line at bottom edge
#if defined(__WXMSW__)
    dc.SetPen(*wxGREY_PEN);
#elif defined(__WXMAC__)
    wxPen linepen(wxColor(140,140,140));
    dc.SetPen(linepen);
#else
    dc.SetPen(*wxLIGHT_GREY_PEN);
#endif
    dc.DrawLine(0, r.GetBottom(), r.width, r.GetBottom());
    dc.SetPen(wxNullPen);
    
    // reset drawing state in case it's no longer valid (due to algo/rule change)
    if (currlayer->drawingstate >= currlayer->algo->NumCellStates()) {
        currlayer->drawingstate = 1;
    }
    
    SetEditFont(dc);  // for DisplayText calls
    
    if (showallstates) DrawAllStates(dc, wd);
    
    dc.SetPen(*wxBLACK_PEN);
    
    // draw current drawing state
    int state = currlayer->drawingstate;
    int x = xpos;
    int y = SMALLHT - 8;
    wxString strbuf;
    if (state < 10) x += digitwd;
    if (state < 100) x += digitwd;
    strbuf.Printf(_("%d"), state);
    DisplayText(dc, strbuf, x, y - (BOXSIZE - digitht)/2);
    
    wxColor cellcolor(currlayer->cellr[state], currlayer->cellg[state], currlayer->cellb[state]);
    
    // draw color box
    x = xpos + 3*digitwd + BOXGAP;
    colorbox = wxRect(x, y - BOXSIZE, BOXSIZE, BOXSIZE);
    dc.SetBrush(wxBrush(cellcolor));
    dc.DrawRectangle(colorbox);
    dc.SetBrush(wxNullBrush);
    
    // draw icon box
    wxBitmap** iconmaps = currlayer->icons15x15;
    x += BOXSIZE + BOXGAP;
    iconbox = wxRect(x, y - BOXSIZE, BOXSIZE, BOXSIZE);
    if (iconmaps && iconmaps[state]) {
        dc.SetBrush(*wxTRANSPARENT_BRUSH);
        dc.DrawRectangle(iconbox);
        dc.SetBrush(wxNullBrush);
        DrawOneIcon(dc, x + 1, y - BOXSIZE + 1, iconmaps[state],
                    currlayer->cellr[0],     currlayer->cellg[0],     currlayer->cellb[0],
                    currlayer->cellr[state], currlayer->cellg[state], currlayer->cellb[state],
                    currlayer->multicoloricons);
    } else {
        dc.SetBrush(wxBrush(cellcolor));
        dc.DrawRectangle(iconbox);
        dc.SetBrush(wxNullBrush);
    }
    
    // show whether color or icon mode is selected
    dc.SetBrush(*wxTRANSPARENT_BRUSH);
    if (showicons) {
        iconbox.Inflate(2,2);
        dc.DrawRectangle(iconbox);
        iconbox.Inflate(-2,-2);
    } else {
        colorbox.Inflate(2,2);
        dc.DrawRectangle(colorbox);
        colorbox.Inflate(-2,-2);
    }
    dc.SetBrush(wxNullBrush);
    
    dc.SetPen(wxNullPen);
}

// -----------------------------------------------------------------------------

void EditBar::OnPaint(wxPaintEvent& WXUNUSED(event))
{
    int wd, ht;
    GetClientSize(&wd, &ht);
    // wd or ht might be < 1 on Windows
    if (wd < 1) wd = 1;
    if (ht < 1) ht = 1;
    
#if defined(__WXMAC__) || defined(__WXGTK__)
    // windows on Mac OS X and GTK+ 2.0 are automatically buffered
    wxPaintDC dc(this);
#else
    // use wxWidgets buffering to avoid flicker
    if (wd != editbitmapwd || ht != editbitmapht) {
        // need to create a new bitmap for edit bar
        delete editbitmap;
        editbitmap = new wxBitmap(wd, ht);
        editbitmapwd = wd;
        editbitmapht = ht;
    }
    if (editbitmap == NULL) Fatal(_("Not enough memory to render edit bar!"));
    wxBufferedPaintDC dc(this, *editbitmap);
#endif
    
    if (showedit) DrawEditBar(dc, wd, ht);
}

// -----------------------------------------------------------------------------

void EditBar::OnMouseDown(wxMouseEvent& event)
{
    // on Win/Linux we need to reset keyboard focus to viewport window
    viewptr->SetFocus();
    
    mainptr->showbanner = false;
    statusptr->ClearMessage();
    
    int x = event.GetX();
    int y = event.GetY();
    
    if (showallstates) {
        // user can change drawing state by clicking in appropriate box
        int right = h_col2 + COLWD * currlayer->algo->NumCellStates();
        int box = -1;
        
        if (x > h_col2 && x < right && y > SMALLHT) {
            box = (x - h_col2) / COLWD + firststate;
        }
        
        if (box >= 0 && box < currlayer->algo->NumCellStates() &&
            currlayer->drawingstate != box) {
            currlayer->drawingstate = box;
            Refresh(false);
            UpdateScrollBar();
            return;
        }
    }
    
    if (event.LeftDClick()) {
        // open Set Layer Colors dialog if user double-clicks in color/icon box
        if (colorbox.Contains(x,y) || iconbox.Contains(x,y)) {
            SetLayerColors();
        }
    } else {
        // user can change color/icon mode by clicking in color/icon box
        if (colorbox.Contains(x,y) && showicons) {
            viewptr->ToggleCellIcons();
        } else if (iconbox.Contains(x,y) && !showicons) {
            viewptr->ToggleCellIcons();
        }
    }
}

// -----------------------------------------------------------------------------

void EditBar::OnButton(wxCommandEvent& event)
{
#ifdef __WXMAC__
    // close any open tool tip window (fixes wxMac bug?)
    wxToolTip::RemoveToolTips();
#endif
    
    mainptr->showbanner = false;
    statusptr->ClearMessage();
    
    int id = event.GetId();
    
    int cmdid;
    switch (id) {
        case UNDO_BUTT:      cmdid = ID_UNDO; break;
        case REDO_BUTT:      cmdid = ID_REDO; break;
        case DRAW_BUTT:      cmdid = ID_DRAW; break;
        case PICK_BUTT:      cmdid = ID_PICK; break;
        case SELECT_BUTT:    cmdid = ID_SELECT; break;
        case MOVE_BUTT:      cmdid = ID_MOVE; break;
        case ZOOMIN_BUTT:    cmdid = ID_ZOOMIN; break;
        case ZOOMOUT_BUTT:   cmdid = ID_ZOOMOUT; break;
        case ALLSTATES_BUTT: cmdid = ID_ALL_STATES; break;
        default:             Warning(_("Unexpected button id!")); return;
    }
    
    // call MainFrame::OnMenu after OnButton finishes
    wxCommandEvent cmdevt(wxEVT_COMMAND_MENU_SELECTED, cmdid);
    wxPostEvent(mainptr->GetEventHandler(), cmdevt);
    
    // avoid possible problems
    viewptr->SetFocus();
}

// -----------------------------------------------------------------------------

void EditBar::OnScroll(wxScrollEvent& event)
{
    WXTYPE type = event.GetEventType();
    
    if (type == wxEVT_SCROLL_LINEUP) {
        currlayer->drawingstate--;
        if (currlayer->drawingstate < 0)
            currlayer->drawingstate = 0;
        Refresh(false);
        
    } else if (type == wxEVT_SCROLL_LINEDOWN) {
        currlayer->drawingstate++;
        if (currlayer->drawingstate >= currlayer->algo->NumCellStates())
            currlayer->drawingstate = currlayer->algo->NumCellStates() - 1;
        Refresh(false);
        
    } else if (type == wxEVT_SCROLL_PAGEUP) {
        currlayer->drawingstate -= PAGESIZE;
        if (currlayer->drawingstate < 0)
            currlayer->drawingstate = 0;
        Refresh(false);
        
    } else if (type == wxEVT_SCROLL_PAGEDOWN) {
        currlayer->drawingstate += PAGESIZE;
        if (currlayer->drawingstate >= currlayer->algo->NumCellStates())
            currlayer->drawingstate = currlayer->algo->NumCellStates() - 1;
        Refresh(false);
        
    } else if (type == wxEVT_SCROLL_THUMBTRACK) {
        currlayer->drawingstate = event.GetPosition();
        if (currlayer->drawingstate < 0)
            currlayer->drawingstate = 0;
        if (currlayer->drawingstate >= currlayer->algo->NumCellStates())
            currlayer->drawingstate = currlayer->algo->NumCellStates() - 1;
        Refresh(false);
        
    } else if (type == wxEVT_SCROLL_THUMBRELEASE) {
        UpdateScrollBar();
    }
    
#ifndef __WXMAC__
    viewptr->SetFocus();    // need on Win/Linux
#endif
}

// -----------------------------------------------------------------------------

void EditBar::OnKillFocus(wxFocusEvent& event)
{
    int id = event.GetId();
    ebbutt[id]->SetFocus();   // don't let button lose focus
}

// -----------------------------------------------------------------------------

void EditBar::OnButtonDown(wxMouseEvent& event)
{
    // edit bar button has been pressed
    int id = event.GetId();
    
    // connect a handler that keeps focus with the pressed button
    ebbutt[id]->Connect(id, wxEVT_KILL_FOCUS, wxFocusEventHandler(EditBar::OnKillFocus));
    
    event.Skip();
}

// -----------------------------------------------------------------------------

void EditBar::OnButtonUp(wxMouseEvent& event)
{
    // edit bar button has been released
    int id = event.GetId();
    
    wxPoint pt = ebbutt[id]->ScreenToClient( wxGetMousePosition() );
    
    int wd, ht;
    ebbutt[id]->GetClientSize(&wd, &ht);
    wxRect r(0, 0, wd, ht);
    
    // diconnect kill-focus handler
    ebbutt[id]->Disconnect(id, wxEVT_KILL_FOCUS, wxFocusEventHandler(EditBar::OnKillFocus));
    viewptr->SetFocus();
    
    if (r.Contains(pt)) {
        // call OnButton
        wxCommandEvent buttevt(wxEVT_COMMAND_BUTTON_CLICKED, id);
        buttevt.SetEventObject(ebbutt[id]);
        ebbutt[id]->GetEventHandler()->ProcessEvent(buttevt);
    }
}

// -----------------------------------------------------------------------------

void EditBar::AddButton(int id, const wxString& tip)
{
    ebbutt[id] = new wxBitmapButton(this, id, normbutt[id], wxPoint(xpos,ypos),
#if defined(__WXOSX_COCOA__) && wxCHECK_VERSION(3,0,0)
                                    wxSize(BUTTON_WD, BUTTON_HT), wxBORDER_SIMPLE
#else
                                    wxSize(BUTTON_WD, BUTTON_HT)
#endif
                                    );
    if (ebbutt[id] == NULL) {
        Fatal(_("Failed to create edit bar button!"));
    } else {
        xpos += BUTTON_WD + smallgap;
        ebbutt[id]->SetToolTip(tip);
#ifdef __WXMSW__
        // fix problem with edit bar buttons when generating/inscript
        // due to focus being changed to viewptr
        ebbutt[id]->Connect(id, wxEVT_LEFT_DOWN, wxMouseEventHandler(EditBar::OnButtonDown));
        ebbutt[id]->Connect(id, wxEVT_LEFT_UP, wxMouseEventHandler(EditBar::OnButtonUp));
#endif
    }
}

// -----------------------------------------------------------------------------

void EditBar::AddSeparator()
{
    xpos += biggap - smallgap;
}

// -----------------------------------------------------------------------------

void EditBar::EnableButton(int id, bool enable)
{
    if (enable == ebbutt[id]->IsEnabled()) return;
    
#ifdef __WXMSW__
    if (id == DRAW_BUTT && currlayer->curs == curs_pencil) {
        ebbutt[id]->SetBitmapDisabled(disdownbutt[id]);
        
    } else if (id == PICK_BUTT && currlayer->curs == curs_pick) {
        ebbutt[id]->SetBitmapDisabled(disdownbutt[id]);
        
    } else if (id == SELECT_BUTT && currlayer->curs == curs_cross) {
        ebbutt[id]->SetBitmapDisabled(disdownbutt[id]);
        
    } else if (id == MOVE_BUTT && currlayer->curs == curs_hand) {
        ebbutt[id]->SetBitmapDisabled(disdownbutt[id]);
        
    } else if (id == ZOOMIN_BUTT && currlayer->curs == curs_zoomin) {
        ebbutt[id]->SetBitmapDisabled(disdownbutt[id]);
        
    } else if (id == ZOOMOUT_BUTT && currlayer->curs == curs_zoomout) {
        ebbutt[id]->SetBitmapDisabled(disdownbutt[id]);
        
    } else if (id == ALLSTATES_BUTT && showallstates) {
        ebbutt[id]->SetBitmapDisabled(disdownbutt[id]);
        
    } else {
        ebbutt[id]->SetBitmapDisabled(disnormbutt[id]);
    }
#endif
    
    ebbutt[id]->Enable(enable);
}

// -----------------------------------------------------------------------------

void EditBar::SelectButton(int id, bool select)
{
    if (select) {
        if (buttstate[id] == 1) return;
        buttstate[id] = 1;
        ebbutt[id]->SetBitmapLabel(downbutt[id]);
    } else {
        if (buttstate[id] == -1) return;
        buttstate[id] = -1;
        ebbutt[id]->SetBitmapLabel(normbutt[id]);
    }
    
    ebbutt[id]->Refresh(false);
}

// -----------------------------------------------------------------------------

void EditBar::UpdateScrollBar()
{
    drawbar->SetScrollbar(currlayer->drawingstate, 1,
                          currlayer->algo->NumCellStates(), PAGESIZE, true);
}

// -----------------------------------------------------------------------------

void CreateEditBar(wxWindow* parent)
{
    // create edit bar underneath layer bar
    int wd, ht;
    parent->GetClientSize(&wd, &ht);
    
    editbarht = showallstates ? BIGHT : SMALLHT;
    editbarptr = new EditBar(parent, 0, LayerBarHeight(), wd, editbarht);
    if (editbarptr == NULL) Fatal(_("Failed to create edit bar!"));
    
    editbarptr->Show(showedit);
}

// -----------------------------------------------------------------------------

int EditBarHeight() {
    return (showedit ? editbarht : 0);
}

// -----------------------------------------------------------------------------

void ResizeEditBar(int wd)
{
    if (editbarptr && showedit) {
        editbarptr->SetSize(wd, editbarht);
    }
}

// -----------------------------------------------------------------------------

void UpdateEditBar()
{
    if (editbarptr && showedit && !mainptr->IsIconized()) {
        bool active = !viewptr->waitingforclick;
        bool timeline = TimelineExists();
        
        // set state of toggle buttons
        editbarptr->SelectButton(DRAW_BUTT,       currlayer->curs == curs_pencil);
        editbarptr->SelectButton(PICK_BUTT,       currlayer->curs == curs_pick);
        editbarptr->SelectButton(SELECT_BUTT,     currlayer->curs == curs_cross);
        editbarptr->SelectButton(MOVE_BUTT,       currlayer->curs == curs_hand);
        editbarptr->SelectButton(ZOOMIN_BUTT,     currlayer->curs == curs_zoomin);
        editbarptr->SelectButton(ZOOMOUT_BUTT,    currlayer->curs == curs_zoomout);
        editbarptr->SelectButton(ALLSTATES_BUTT,  showallstates);
        
        // CanUndo() returns false if drawing/selecting cells so the user can't undo
        // while in those modes (by pressing a key), but we want the Undo button to
        // appear to be active
        bool canundo = (allowundo && (viewptr->drawingcells || viewptr->selectingcells))
                       || currlayer->undoredo->CanUndo();
        editbarptr->EnableButton(UNDO_BUTT,       active && !timeline && canundo);
        editbarptr->EnableButton(REDO_BUTT,       active && !timeline &&
                                                  currlayer->undoredo->CanRedo());
        editbarptr->EnableButton(DRAW_BUTT,       active);
        editbarptr->EnableButton(PICK_BUTT,       active);
        editbarptr->EnableButton(SELECT_BUTT,     active);
        editbarptr->EnableButton(MOVE_BUTT,       active);
        editbarptr->EnableButton(ZOOMIN_BUTT,     active);
        editbarptr->EnableButton(ZOOMOUT_BUTT,    active);
        editbarptr->EnableButton(ALLSTATES_BUTT,  active);
        
        editbarptr->Refresh(false);
        
        // drawing state might have changed
        editbarptr->UpdateScrollBar();
        
        #ifdef __WXGTK__
            // avoid bug that can cause buttons to lose their bitmaps
            editbarptr->Update();
        #endif
    }
}

// -----------------------------------------------------------------------------

void ToggleEditBar()
{
    showedit = !showedit;
    mainptr->ResizeBigView();
    editbarptr->Show(showedit);    // needed on Windows
    if (showlayer) {
        // line at bottom of layer bar may need to be added/removed
        RedrawLayerBar();
    }
    mainptr->UpdateEverything();
}

// -----------------------------------------------------------------------------

void ToggleAllStates()
{
    showallstates = !showallstates;
    editbarht = showallstates ? BIGHT : SMALLHT;
    if (showedit) {
        mainptr->ResizeBigView();
        mainptr->UpdateEverything();
    } else if (showallstates) {
        // show the edit bar using new height
        ToggleEditBar();
    } else {
        mainptr->UpdateMenuItems();
    }
}

// -----------------------------------------------------------------------------

void ShiftEditBar(int yamount)
{
    int x, y;
    editbarptr->GetPosition(&x, &y);
    editbarptr->Move(x, y + yamount);
}

// -----------------------------------------------------------------------------

void CycleDrawingState(bool higher)
{
    if (viewptr->drawingcells) return;
    
    int maxstate = currlayer->algo->NumCellStates() - 1;
    
    if (higher) {
        if (currlayer->drawingstate == maxstate)
            currlayer->drawingstate = 0;
        else
            currlayer->drawingstate++;
    } else {
        if (currlayer->drawingstate == 0)
            currlayer->drawingstate = maxstate;
        else
            currlayer->drawingstate--;
    }
    
    if (showedit && !mainptr->IsIconized()) {
        editbarptr->Refresh(false);
        editbarptr->UpdateScrollBar();
        #ifdef __WXGTK__
            // avoid bug that can cause buttons to lose their bitmaps
            editbarptr->Update();
        #endif
    }
}