-
Notifications
You must be signed in to change notification settings - Fork 1
/
XPMColorPicker.cpp
1911 lines (1696 loc) · 67.2 KB
/
XPMColorPicker.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
/***************************************************************
* Plugin: XPMEditor for Code::Blocks
* Name: XPMColorPicker.cpp
* Purpose: a simple wxWidget control presenting a panel of colours - code
* Author: Seb ([email protected])
* Created: 2009-04-23
* Copyright: Seb ()
* License: GPL 3.0
**************************************************************/
#include "XPMColorPicker.h"
#include <wx/gdicmn.h>
#include <wx/dcclient.h>
#include <wx/dcbuffer.h>
#include <wx/brush.h>
#include <wx/pen.h>
#include <wx/msgdlg.h>
#include <wx/colordlg.h>
#include <wx/renderer.h>
#include <wx/settings.h>
DEFINE_EVENT_TYPE(wxEVT_TRANSPARENT_COLOR_CHANGED)
DEFINE_EVENT_TYPE(wxEVT_LINE_COLOR_CHANGED)
DEFINE_EVENT_TYPE(wxEVT_FILL_COLOR_CHANGED)
const long XPMColorPicker::ID_SCROLLBAR_HORIZ = wxNewId();
const long XPMColorPicker::ID_SCROLLBAR_VERT = wxNewId();
/** CONSTRUCTOR
*/
XPMColorPicker::XPMColorPicker(wxWindow* parent,
wxWindowID id,
const wxPoint& pos,
const wxSize& size,
int style,
const wxString& name
)
{
//constructor
bComputed = false;
Create(parent, id, pos, size, style, name);
SetBackgroundStyle(wxBG_STYLE_CUSTOM);
sbHorizScrollBar = new wxScrollBar(this, ID_SCROLLBAR_HORIZ,
wxDefaultPosition, wxDefaultSize,
wxSB_HORIZONTAL, wxDefaultValidator,
_T("ID_SCROLLBAR_HORIZ")
);
sbVertScrollBar = new wxScrollBar(this, ID_SCROLLBAR_VERT,
wxDefaultPosition, wxDefaultSize,
wxSB_VERTICAL, wxDefaultValidator,
_T("ID_SCROLLBAR_VERT")
);
//initialize the colour array
init_colours();
//initialize sizing parameters
wxFont font;
font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
SetFont(font);
iOrientation = XPM_AUTO_ORIENTATION;
iIdealNbRowsOrCols = 2;
bScrollbars = false;
iUnitX = -1;
iUnitY = -1;
bSquare = false;
iMinimalSize = XPM_MINIMAL_SIZE;
//initialize the Best size information
wxSize sBestSize;
sBestSize = DoGetBestSize();
SetMinSize(sBestSize);
bLineOn = true;
//initializations
iScrollBarPos = 0;
wxSize sClientSize;
sClientSize = GetClientSize();
ComputeDimensions(sClientSize);
iLineColor = 0; //black
iFillColor = XPM_MAX_COLOR - 1; //white
//event handlers
Connect(wxEVT_SIZE,(wxObjectEventFunction)&XPMColorPicker::OnEventSize);
Connect(wxEVT_PAINT,(wxObjectEventFunction)&XPMColorPicker::OnEventPaint);
Connect(wxEVT_ERASE_BACKGROUND, (wxObjectEventFunction)&XPMColorPicker::OnEraseBackground);
//scrollbar events
if (sbHorizScrollBar)
{
Connect(ID_SCROLLBAR_HORIZ, wxEVT_SCROLL_TOP, (wxObjectEventFunction)&XPMColorPicker::OnScrollTop);
Connect(ID_SCROLLBAR_HORIZ, wxEVT_SCROLL_BOTTOM, (wxObjectEventFunction)&XPMColorPicker::OnScrollBottom);
Connect(ID_SCROLLBAR_HORIZ, wxEVT_SCROLL_LINEUP, (wxObjectEventFunction)&XPMColorPicker::OnScrollLineUp);
Connect(ID_SCROLLBAR_HORIZ, wxEVT_SCROLL_LINEDOWN, (wxObjectEventFunction)&XPMColorPicker::OnScrollLineDown);
Connect(ID_SCROLLBAR_HORIZ, wxEVT_SCROLL_PAGEUP, (wxObjectEventFunction)&XPMColorPicker::OnScrollPageUp);
Connect(ID_SCROLLBAR_HORIZ, wxEVT_SCROLL_PAGEDOWN, (wxObjectEventFunction)&XPMColorPicker::OnScrollPageDown);
Connect(ID_SCROLLBAR_HORIZ, wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)&XPMColorPicker::OnScrollThumbTrack);
Connect(ID_SCROLLBAR_HORIZ, wxEVT_SCROLL_THUMBRELEASE, (wxObjectEventFunction)&XPMColorPicker::OnScrollThumbRelease);
Connect(ID_SCROLLBAR_HORIZ, wxEVT_SCROLL_CHANGED, (wxObjectEventFunction)&XPMColorPicker::OnScrollChanged);
}
if (sbVertScrollBar)
{
Connect(ID_SCROLLBAR_VERT, wxEVT_SCROLL_TOP, (wxObjectEventFunction)&XPMColorPicker::OnScrollTop);
Connect(ID_SCROLLBAR_VERT, wxEVT_SCROLL_BOTTOM, (wxObjectEventFunction)&XPMColorPicker::OnScrollBottom);
Connect(ID_SCROLLBAR_VERT, wxEVT_SCROLL_LINEUP, (wxObjectEventFunction)&XPMColorPicker::OnScrollLineUp);
Connect(ID_SCROLLBAR_VERT, wxEVT_SCROLL_LINEDOWN, (wxObjectEventFunction)&XPMColorPicker::OnScrollLineDown);
Connect(ID_SCROLLBAR_VERT, wxEVT_SCROLL_PAGEUP, (wxObjectEventFunction)&XPMColorPicker::OnScrollPageUp);
Connect(ID_SCROLLBAR_VERT, wxEVT_SCROLL_PAGEDOWN, (wxObjectEventFunction)&XPMColorPicker::OnScrollPageDown);
Connect(ID_SCROLLBAR_VERT, wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)&XPMColorPicker::OnScrollThumbTrack);
Connect(ID_SCROLLBAR_VERT, wxEVT_SCROLL_THUMBRELEASE, (wxObjectEventFunction)&XPMColorPicker::OnScrollThumbRelease);
Connect(ID_SCROLLBAR_VERT, wxEVT_SCROLL_CHANGED, (wxObjectEventFunction)&XPMColorPicker::OnScrollChanged);
}
//mouse events handlers
Connect(wxEVT_MOTION, (wxObjectEventFunction)&XPMColorPicker::OnMouseMove);
Connect(wxEVT_ENTER_WINDOW,(wxObjectEventFunction)&XPMColorPicker::OnMouseEnter);
Connect(wxEVT_LEAVE_WINDOW,(wxObjectEventFunction)&XPMColorPicker::OnMouseLeave);
Connect(wxEVT_SET_CURSOR,(wxObjectEventFunction)&XPMColorPicker::OnSetCursor);
Connect(wxEVT_LEFT_DOWN,(wxObjectEventFunction)&XPMColorPicker::OnMouseClick);
Connect(wxEVT_RIGHT_DOWN,(wxObjectEventFunction)&XPMColorPicker::OnMouseRClick);
Connect(wxEVT_LEFT_DCLICK,(wxObjectEventFunction)&XPMColorPicker::OnMouseDClick);
}
/** Initialize default colour values
*/
void XPMColorPicker::init_colours(void)
{
//init the array of colours
iPaletteSize = XPM_MAX_COLOR;
ColourArray = new wxColour[iPaletteSize];
if (!ColourArray) return;
//Black
ColourArray[0] = wxTheColourDatabase->Find(_("BLACK"));
//purple hues
ColourArray[1] = wxTheColourDatabase->Find(_("BLUE VIOLET"));
ColourArray[2] = wxTheColourDatabase->Find(_("MAGENTA"));
ColourArray[3] = wxTheColourDatabase->Find(_("VIOLET"));
ColourArray[4] = wxTheColourDatabase->Find(_("VIOLET RED"));
ColourArray[5] = wxTheColourDatabase->Find(_("MEDIUM VIOLET RED"));
ColourArray[6] = wxTheColourDatabase->Find(_("PINK"));
ColourArray[7] = wxTheColourDatabase->Find(_("PLUM"));
ColourArray[8] = wxTheColourDatabase->Find(_("PURPLE"));
ColourArray[9] = wxTheColourDatabase->Find(_("SALMON"));
//blue hues
ColourArray[10] = wxTheColourDatabase->Find(_("BLUE"));
ColourArray[11] = wxTheColourDatabase->Find(_("AQUAMARINE"));
ColourArray[12] = wxTheColourDatabase->Find(_("CADET BLUE"));
ColourArray[13] = wxTheColourDatabase->Find(_("CORAL"));
ColourArray[14] = wxTheColourDatabase->Find(_("CORNFLOWER BLUE"));
ColourArray[15] = wxTheColourDatabase->Find(_("MEDIUM AQUAMARINE"));
ColourArray[16] = wxTheColourDatabase->Find(_("MEDIUM BLUE"));
ColourArray[17] = wxTheColourDatabase->Find(_("CYAN"));
ColourArray[18] = wxTheColourDatabase->Find(_("DARK SLATE BLUE"));
ColourArray[19] = wxTheColourDatabase->Find(_("DARK TURQUOISE"));
ColourArray[20] = wxTheColourDatabase->Find(_("LIGHT BLUE"));
ColourArray[21] = wxTheColourDatabase->Find(_("LIGHT STEEL BLUE"));
ColourArray[22] = wxTheColourDatabase->Find(_("MEDIUM SLATE BLUE"));
ColourArray[23] = wxTheColourDatabase->Find(_("MEDIUM TURQUOISE"));
ColourArray[24] = wxTheColourDatabase->Find(_("MIDNIGHT BLUE"));
ColourArray[25] = wxTheColourDatabase->Find(_("NAVY"));
ColourArray[26] = wxTheColourDatabase->Find(_("SKY BLUE"));
ColourArray[27] = wxTheColourDatabase->Find(_("SLATE BLUE"));
ColourArray[28] = wxTheColourDatabase->Find(_("STEEL BLUE"));
ColourArray[29] = wxTheColourDatabase->Find(_("TURQUOISE"));
//green hues
ColourArray[30] = wxTheColourDatabase->Find(_("KHAKI"));
ColourArray[31] = wxTheColourDatabase->Find(_("DARK GREEN"));
ColourArray[32] = wxTheColourDatabase->Find(_("DARK OLIVE GREEN"));
ColourArray[33] = wxTheColourDatabase->Find(_("FOREST GREEN"));
ColourArray[34] = wxTheColourDatabase->Find(_("MEDIUM FOREST GREEN"));
ColourArray[35] = wxTheColourDatabase->Find(_("GREEN"));
ColourArray[36] = wxTheColourDatabase->Find(_("GREEN YELLOW"));
ColourArray[37] = wxTheColourDatabase->Find(_("LIME GREEN"));
ColourArray[38] = wxTheColourDatabase->Find(_("MEDIUM SEA GREEN"));
ColourArray[39] = wxTheColourDatabase->Find(_("MEDIUM SPRING GREEN"));
ColourArray[40] = wxTheColourDatabase->Find(_("SPRING GREEN"));
ColourArray[41] = wxTheColourDatabase->Find(_("PALE GREEN"));
ColourArray[42] = wxTheColourDatabase->Find(_("SEA GREEN"));
//yellow / orange hues
ColourArray[43] = wxTheColourDatabase->Find(_("YELLOW"));
ColourArray[44] = wxTheColourDatabase->Find(_("YELLOW GREEN"));
ColourArray[45] = wxTheColourDatabase->Find(_("GOLD"));
ColourArray[46] = wxTheColourDatabase->Find(_("GOLDENROD"));
ColourArray[47] = wxTheColourDatabase->Find(_("MEDIUM GOLDENROD"));
ColourArray[48] = wxTheColourDatabase->Find(_("ORANGE"));
ColourArray[49] = wxTheColourDatabase->Find(_("ORANGE RED"));
ColourArray[50] = wxTheColourDatabase->Find(_("WHEAT"));
//red hues
ColourArray[51] = wxTheColourDatabase->Find(_("RED"));
ColourArray[52] = wxTheColourDatabase->Find(_("ORCHID"));
ColourArray[53] = wxTheColourDatabase->Find(_("DARK ORCHID"));
ColourArray[54] = wxTheColourDatabase->Find(_("FIREBRICK"));
ColourArray[55] = wxTheColourDatabase->Find(_("INDIAN RED"));
ColourArray[56] = wxTheColourDatabase->Find(_("MEDIUM ORCHID"));
//brown hues
ColourArray[57] = wxTheColourDatabase->Find(_("BROWN"));
ColourArray[58] = wxTheColourDatabase->Find(_("MAROON"));
ColourArray[59] = wxTheColourDatabase->Find(_("THISTLE"));
ColourArray[60] = wxTheColourDatabase->Find(_("SIENNA"));
ColourArray[61] = wxTheColourDatabase->Find(_("TAN"));
//greys & white hues
ColourArray[62] = wxTheColourDatabase->Find(_("DARK GREY"));
ColourArray[63] = wxTheColourDatabase->Find(_("DARK SLATE GREY"));
ColourArray[64] = wxTheColourDatabase->Find(_("DIM GREY"));
ColourArray[65] = wxTheColourDatabase->Find(_("GREY"));
ColourArray[66] = wxTheColourDatabase->Find(_("LIGHT GREY"));
ColourArray[67] = wxTheColourDatabase->Find(_("WHITE"));
cTransparent = *wxLIGHT_GREY;
}
/** DESTRUCTOR
*/
XPMColorPicker::~XPMColorPicker()
{
//dtor
if (ColourArray) delete[] ColourArray;
}
//------ EVENTS HANDLERS -------------
//scroll event handlers
/** Scrollbar event wxEVT_SCROLL_LINEUP
* \param event : the event descriptor
*/
void XPMColorPicker::OnScrollLineUp(wxScrollEvent& event)
{
iScrollBarPos = event.GetPosition();
Repaint();
event.Skip();
}
/** Scrollbar event wxEVT_SCROLL_LINEDOWN
* \param event : the event descriptor
*/
void XPMColorPicker::OnScrollLineDown(wxScrollEvent& event)
{
iScrollBarPos = event.GetPosition();
Repaint();
event.Skip();
}
/** Scrollbar event wxEVT_SCROLL_PAGEUP
* \param event : the event descriptor
*/
void XPMColorPicker::OnScrollPageUp(wxScrollEvent& event)
{
int iOrientation2;
int iPageSize;
iOrientation2 = event.GetOrientation();
if ((sbHorizScrollBar) && (iOrientation2 == wxHORIZONTAL))
{
iPageSize = sbHorizScrollBar->GetPageSize();
iScrollBarPos = sbHorizScrollBar->GetThumbPosition() - iPageSize;
if (iScrollBarPos < 0) iScrollBarPos = 0;
sbHorizScrollBar->SetThumbPosition(iScrollBarPos);
}
if ((sbVertScrollBar) && (iOrientation2 == wxVERTICAL))
{
iPageSize = sbVertScrollBar->GetPageSize();
iScrollBarPos = sbVertScrollBar->GetThumbPosition() - iPageSize;
if (iScrollBarPos < 0) iScrollBarPos = 0;
sbVertScrollBar->SetThumbPosition(iScrollBarPos);
}
Repaint();
event.Skip();
}
/** Scrollbar event wxEVT_SCROLL_PAGEDOWN
* \param event : the event descriptor
*/
void XPMColorPicker::OnScrollPageDown(wxScrollEvent& event)
{
int iPageSize, iScrollBarMaxPos;
int iOrientation2;
iOrientation2 = event.GetOrientation();
if ((sbHorizScrollBar) && (iOrientation2 == wxHORIZONTAL))
{
iPageSize = sbHorizScrollBar->GetPageSize();
iScrollBarMaxPos = sbHorizScrollBar->GetRange() - sbHorizScrollBar->GetThumbSize();
iScrollBarPos = sbHorizScrollBar->GetThumbPosition() + iPageSize;
if (iScrollBarPos > iScrollBarMaxPos) iScrollBarPos = iScrollBarMaxPos;
sbHorizScrollBar->SetThumbPosition(iScrollBarPos);
}
if ((sbVertScrollBar) && (iOrientation2 == wxVERTICAL))
{
iPageSize = sbVertScrollBar->GetPageSize();
iScrollBarMaxPos = sbVertScrollBar->GetRange() - sbVertScrollBar->GetThumbSize();
iScrollBarPos = sbVertScrollBar->GetThumbPosition() + iPageSize;
if (iScrollBarPos > iScrollBarMaxPos) iScrollBarPos = iScrollBarMaxPos;
sbVertScrollBar->SetThumbPosition(iScrollBarPos);
}
Repaint();
event.Skip();
}
/** Scrollbar event wxEVT_SCROLL_TOP
* \param event : the event descriptor
*/
void XPMColorPicker::OnScrollTop(wxScrollEvent& event)
{
int iOrientation2;
iOrientation2 = event.GetOrientation();
iScrollBarPos = 0;
if ((sbHorizScrollBar) && (iOrientation2 == wxHORIZONTAL)) sbHorizScrollBar->SetThumbPosition(0);
if ((sbVertScrollBar) && (iOrientation2 == wxVERTICAL)) sbVertScrollBar->SetThumbPosition(0);
Repaint();
event.Skip();
}
/** Scrollbar event wxEVT_SCROLL_BOTTOM
* \param event : the event descriptor
*/
void XPMColorPicker::OnScrollBottom(wxScrollEvent& event)
{
int iOrientation2;
iOrientation2 = event.GetOrientation();
if ((sbHorizScrollBar) && (iOrientation2 == wxHORIZONTAL))
{
iScrollBarPos = sbHorizScrollBar->GetRange() - sbHorizScrollBar->GetThumbSize();
sbHorizScrollBar->SetThumbPosition(iScrollBarPos);
}
if ((sbVertScrollBar) && (iOrientation2 == wxVERTICAL))
{
iScrollBarPos = sbVertScrollBar->GetRange() - sbVertScrollBar->GetThumbSize();
sbVertScrollBar->SetThumbPosition(iScrollBarPos);
}
Repaint();
event.Skip();
}
/** Scrollbar event wxEVT_SCROLL_THUMBTRACK
* \param event : the event descriptor
*/
void XPMColorPicker::OnScrollThumbTrack(wxScrollEvent& event)
{
iScrollBarPos = event.GetPosition();
Repaint();
event.Skip();
}
/** Scrollbar event wxEVT_SCROLL_THUMBRELEASE
* \param event : the event descriptor
*/
void XPMColorPicker::OnScrollThumbRelease(wxScrollEvent& event)
{
iScrollBarPos = event.GetPosition();
Repaint();
event.Skip();
}
/** Scrollbar event wxEVT_SCROLL_CHANGED
* \param event : the event descriptor
*/
void XPMColorPicker::OnScrollChanged(wxScrollEvent& event)
{
event.Skip(); //do nothing to prevent double handling
}
/** Override background events - to avoid flicker
*/
void XPMColorPicker::OnEraseBackground(wxEraseEvent)
{
}
/** Handles paint event
*/
void XPMColorPicker::OnEventPaint(wxPaintEvent& event)
{
//Paint event handler
wxBufferedPaintDC dc(this);
Paint(dc);
}
/** Repaint the window
* \param dc : a pointer to the DC to use
*/
void XPMColorPicker::Paint(wxDC &dc)
{
int iWidth, iHeight, iOrientation2;
wxSize sClientSize;
sClientSize = GetClientSize();
iWidth = sClientSize.GetWidth();
iHeight = sClientSize.GetHeight();
iOrientation2 = GetActualOrientation();
if (!bComputed)
{
ComputeDimensions(sClientSize);
}
//draw the double square containing the current selection
PaintDisplaySquare(dc, wxRect(0,0, iDisplayWidth, iDisplayHeight));
if (iOrientation2 == XPM_HORIZONTAL_ORIENTATION)
{
//horizontal layout
PaintTransparentColor(dc, wxRect(iDisplayWidth, 0, iDisplayWidth, iDisplayHeight));
PaintColorSquares(dc, wxRect(2*iDisplayWidth, 0, iWidth - 2 * iDisplayWidth, iHeight));
}
else
{
//vertical layout
PaintTransparentColor(dc, wxRect(0, iDisplayHeight, iDisplayWidth, iDisplayHeight));
PaintColorSquares(dc, wxRect(0, 2 * iDisplayHeight, iWidth, iHeight - 2 * iDisplayHeight));
}
dc.SetBrush(wxNullBrush);
dc.SetPen(wxNullPen);
}
/** Paint the display square : the fill colour + the line colour
* \param dc : the wxDC on which to draw
* \param rect : the bounding rect on which to draw
*/
void XPMColorPicker::PaintDisplaySquare(wxDC &dc, wxRect rect)
{
//paint the display square
wxPen pBorderPen(wxSystemSettings::GetColour(wxSYS_COLOUR_ACTIVEBORDER), 1, wxPENSTYLE_SOLID);
wxBrush bBackBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE), wxBRUSHSTYLE_SOLID);
dc.SetPen(pBorderPen);
dc.SetBrush(bBackBrush);
dc.DrawRectangle(rect.GetLeft(),rect.GetTop(), rect.GetWidth(), rect.GetHeight());
//draw in this square the rectangle with the current outline colour and filling colour
wxColour cFillColour;
wxColour cLineColour;
cFillColour = GetFillColour();
cLineColour = GetLineColour();
wxPen pOutlinePen(cLineColour, 2, wxPENSTYLE_SOLID);
wxBrush bFillBrush(cFillColour, wxBRUSHSTYLE_SOLID);
dc.SetPen(pOutlinePen);
dc.SetBrush(bFillBrush);
dc.DrawRectangle(rect.GetLeft() + 4, rect.GetTop() + 4,
rect.GetWidth() - 7, rect.GetHeight() - 7
);
//draw the sunken border
PaintSunkenBorder(dc, rect);
dc.SetBrush(wxNullBrush);
dc.SetPen(wxNullPen);
}
/** Paint the rectangle which include the transparent colour and the push button
* \param dc : the wxDC on which to draw
* \param rect : the bounding rect on which to draw
*/
void XPMColorPicker::PaintTransparentColor(wxDC &dc, wxRect rect)
{
//paint the 2 frames (one for the transparent colour, one for the push button
wxPen pBorderPen(wxSystemSettings::GetColour(wxSYS_COLOUR_ACTIVEBORDER), 1, wxPENSTYLE_SOLID);
wxBrush bBackBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE), wxBRUSHSTYLE_SOLID);
dc.SetPen(pBorderPen);
dc.SetBrush(bBackBrush);
dc.DrawRectangle(rect.GetLeft(),rect.GetTop(), rect.GetWidth(), rect.GetHeight() / 2);
PaintSunkenBorder(dc, wxRect(rect.GetLeft(),rect.GetTop(), rect.GetWidth(), rect.GetHeight() / 2));
dc.SetBrush(wxNullBrush);
dc.DrawRectangle(rect.GetLeft(),rect.GetTop() + rect.GetHeight() / 2,
rect.GetWidth() , rect.GetHeight() - rect.GetHeight() / 2
);
PaintSunkenBorder(dc, wxRect(rect.GetLeft(),rect.GetTop() + rect.GetHeight() / 2,
rect.GetWidth() , rect.GetHeight() - rect.GetHeight() / 2
)
);
//paint the transparent colour
wxBrush bTransparentBrush(cTransparent, wxBRUSHSTYLE_SOLID);
wxPen pBackPen(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE), 1, wxPENSTYLE_SOLID);
dc.SetPen(pBackPen);
dc.SetBrush(bTransparentBrush);
dc.DrawRectangle(rect.GetLeft() + 2, rect.GetTop() + 2,
rect.GetWidth() - 4, rect.GetHeight() / 2 - 4);
//paint the push button
int flags;
wxString sText;
wxCoord x, y, x2, y2;
wxFont font;
if (!bLineOn)
{
flags = wxCONTROL_PRESSED;
sText = _("Fill");
}
else
{
flags = 0;
sText = _("Line");
}
wxRendererNative::Get().DrawPushButton(this, dc,
wxRect(rect.GetLeft() + 1,rect.GetTop() + rect.GetHeight() / 2 + 1,
rect.GetWidth() - 2 , rect.GetHeight() - rect.GetHeight() / 2 - 2),
flags
);
font = GetFont();
dc.SetFont(font);
dc.GetTextExtent(sText,&x,&y);
dc.GetTextExtent(_("FillLine"), &x2, &y2);
dc.DrawText(sText, rect.GetLeft() + 1 + (rect.GetWidth() - 2 - x) / 2 ,
rect.GetTop() + rect.GetHeight() / 2 + 1 + (rect.GetHeight() - rect.GetHeight() / 2 - 2 - y2) / 2);
dc.SetBrush(wxNullBrush);
dc.SetPen(wxNullPen);
}
void XPMColorPicker::PaintColorSquares(wxDC &dc, wxRect rect)
{
//paint all the colour squares
wxBrush bBackBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
wxPen pBackPen(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE), 1, wxPENSTYLE_SOLID);
int i, j, iIndex, iStart, jStart;
iIndex = 0;
if (bScrollbars)
{
int iOrientation2;
iOrientation2 = GetActualOrientation();
if (iOrientation2 == XPM_VERTICAL_ORIENTATION)
{
iStart = iScrollBarPos;
jStart = 0;
}
else
{
iStart = 0;
jStart = iScrollBarPos;
}
}
else
{
iStart = 0;
jStart = 0;
}
for(i=iStart;i<iNbRows;i++)
{
for(j=jStart;j<iNbColumns;j++)
{
iIndex = i * iNbColumns + j;
if (iIndex >= iPaletteSize)
{
dc.SetPen(pBackPen);
dc.SetBrush(bBackBrush);
dc.DrawRectangle(rect.GetLeft() + (j-jStart) * iColourWidth , (i-iStart) * iColourHeight + 1 + rect.GetTop(),
iColourWidth, iColourHeight
);
}
else
{
wxColour cColor;
cColor = GetPaletteColour(iIndex);
wxBrush bFBrush(cColor, wxBRUSHSTYLE_SOLID);
wxPen pFPen(cColor, 1, wxPENSTYLE_SOLID);
dc.SetBrush(bFBrush);
dc.SetPen(pFPen);
dc.DrawRectangle(rect.GetLeft() + (j-jStart) * iColourWidth , (i-iStart) * iColourHeight + 1 + rect.GetTop(),
iColourWidth, iColourHeight
);
PaintSunkenBorder(dc, wxRect(rect.GetLeft() + (j-jStart) * iColourWidth , (i-iStart) * iColourHeight + 1 + rect.GetTop(),
iColourWidth, iColourHeight
)
);
}
dc.SetBrush(wxNullBrush);
dc.SetPen(wxNullPen);
}
//paint the end of the row with the background colour
dc.SetPen(pBackPen);
dc.SetBrush(bBackBrush);
dc.DrawRectangle(rect.GetLeft() + (iNbColumns - jStart) * iColourWidth , (i-iStart) * iColourHeight + 1 + rect.GetTop(),
rect.GetWidth() - (iNbColumns - jStart) * iColourWidth, iColourHeight
);
}
//paint the bottom of the control with the background colour
dc.SetPen(pBackPen);
dc.SetBrush(bBackBrush);
dc.DrawRectangle(rect.GetLeft() , (iNbRows - iStart) * iColourHeight + 1 + rect.GetTop(),
rect.GetWidth(), rect.GetHeight() - (iNbRows - iStart) * iColourHeight
);
PaintSunkenBorder(dc, rect);
dc.SetBrush(wxNullBrush);
dc.SetPen(wxNullPen);
}
/** Paint a sunken border
* \param dc : the wxDC on which to draw
* \param rect : the bounding rect which must appear sunk.
* The border will be drawn on the edge and 1 pixel inside the rect
* (the border has a thickness of up 2 pixels)
*/
void XPMColorPicker::PaintSunkenBorder(wxDC &dc, wxRect rect)
{
//paint a sunken border
wxPen pDarkPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW), 1, wxPENSTYLE_SOLID);
wxPen pLightPen(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNHIGHLIGHT), 1, wxPENSTYLE_SOLID);
dc.SetPen(pDarkPen);
dc.DrawLine(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetTop());
dc.DrawLine(rect.GetLeft(), rect.GetTop(), rect.GetLeft(), rect.GetBottom());
dc.SetPen(pLightPen);
dc.DrawLine(rect.GetLeft(), rect.GetBottom(), rect.GetRight()+1, rect.GetBottom());
dc.DrawLine(rect.GetRight(), rect.GetTop(), rect.GetRight(), rect.GetBottom()+1);
dc.SetPen(wxNullPen);
}
/** immediate repainting of the widgets
*/
void XPMColorPicker::Repaint(void)
{
//immediate repainting of the widgets
wxClientDC dcclient(this);
wxBufferedDC dc(&dcclient);
Paint(dc);
}
/** Handles a size event
*/
void XPMColorPicker::OnEventSize(wxSizeEvent& event)
{
//Size event handler
wxSize s;
bComputed = false;
s = GetClientSize();
ComputeDimensions(s);
Refresh(false, NULL);
Update();
event.Skip();
}
/** Set the orientation of the picker: horizontal or vertical
* \param iNewOrientation : one of the following value:
* @XPM_HORIZONTAL_ORIENTATION : horizontal layout
* @XPM_VERTICAL_ORIENTATION : vertical layout
* @XPM_AUTO_ORIENTATION : let the control choose the best solution
* Default value is : @XPM_AUTO_ORIENTATION
*/
void XPMColorPicker::SetOrientation(int iNewOrientation)
{
if ( (iOrientation != iNewOrientation)
&& ( (iNewOrientation == XPM_HORIZONTAL_ORIENTATION)
||(iNewOrientation == XPM_VERTICAL_ORIENTATION)
)
)
{
wxSize sClientSize;
iOrientation = iNewOrientation;
sClientSize = GetClientSize();
ComputeDimensions(sClientSize);
Refresh(false, NULL);
Update();
}
}
/** Get the orientation of the picker: horizontal or vertical
* \return iNewOrientation : one of the following value:
* @XPM_HORIZONTAL_ORIENTATION : horizontal layout
* @XPM_VERTICAL_ORIENTATION : vertical layout
* @XPM_AUTO_ORIENTATION : let the control choose the best solution
*/
int XPMColorPicker::GetOrientation(void)
{
return(iOrientation);
}
/** Get the orientation of the picker: horizontal or vertical
* If the picker is in automatic mode, it will compute the best solution
* \return iNewOrientation : one of the following value:
* @XPM_HORIZONTAL_ORIENTATION : horizontal layout
* @XPM_VERTICAL_ORIENTATION : vertical layout
*/
int XPMColorPicker::GetActualOrientation(void) const
{
int iOrientation2;
wxSize sClientSize;
iOrientation2 = XPM_HORIZONTAL_ORIENTATION;
sClientSize = GetClientSize();
switch(iOrientation)
{
case XPM_HORIZONTAL_ORIENTATION:
case XPM_VERTICAL_ORIENTATION : iOrientation2 = iOrientation;
break;
default: if (sClientSize.GetHeight() > sClientSize.GetWidth())
{
iOrientation2 = XPM_VERTICAL_ORIENTATION;
}
else
{
iOrientation2 = XPM_HORIZONTAL_ORIENTATION;
}
break;
}
return(iOrientation2);
}
/** Sets the ideal number of rows or columns the colour picker should display
* See @SetOrientation to indicates if this value correspond to row number or column number
* If they are enough place, the picker will draw the colours using the specified number of
* rows or columns.
* Default value is 2.
* When the size reduces, the picker will first try to reduce the size of the squares, and
* when the squares reach the minimal size (3 x 3 pixels), it will use the available space.
* If no more space is available, scrollbars will be set
* \param iNewValue : the new value
*/
void XPMColorPicker::SetIdealNbLines(int iNewValue)
{
if (iNewValue > 0)
{
wxSize sClientSize;
iIdealNbRowsOrCols = iNewValue;
sClientSize = GetClientSize();
ComputeDimensions(sClientSize);
Refresh(false, NULL);
Update();
}
}
/** Gets the ideal number of rows or columns the colour picker should display
* See @GetOrientation to know if this value correspond to row number or column number
* If they are enough place, the picker will draw the colours using the specified number of
* rows or columns.
* When the size reduces, the picker will first try to reduce the size of the squares, and
* when the squares reach the minimal size (3 x 3 pixels), it will use the available space.
* If no more space is available, scrollbars will be set
* \return the value
*/
int XPMColorPicker::GetIdealNbLines(void)
{
return(iIdealNbRowsOrCols);
}
/** Set the minimal size for a colour square
* \param iNewValue : the new minimal size, in pixel
*/
void XPMColorPicker::SetMinimalSize(int iNewValue)
{
if (iNewValue > 2)
{
wxSize sClientSize;
iMinimalSize = iNewValue;
sClientSize = GetClientSize();
ComputeDimensions(sClientSize);
Refresh(false, NULL);
Update();
}
}
/** Get the minimal size for a colour square
* \return the minimal size for a colour square
*/
int XPMColorPicker::GetMinimalSize(void)
{
return(iMinimalSize);
}
/** Set if the picker should draw exact squares or rectangles
* \param bNewValue : if true, the control will draw exact squares for the colours
* if false, the control will draw rectangles if needed
*/
void XPMColorPicker::SetSquareDisplay(bool bNewValue)
{
if (bNewValue != bSquare)
{
wxSize sClientSize;
bSquare = bNewValue;
sClientSize = GetClientSize();
ComputeDimensions(sClientSize);
Refresh(false, NULL);
Update();
}
}
/** Get if the picker should draw exact squares or rectangles
* \return : if true, the control will draw exact squares for the colours
* if false, the control will draw rectangles if needed
*/
bool XPMColorPicker::GetSquareDisplay(void)
{
return(bSquare);
}
/** Compute the ideal dimensions of the colour squares
* The following parameters will be computed:
* 1 - the minimal size of the control. SetMinSize will be called
* 2 - the maximal size of the control. SetMaxSize will be called
* 3 - the number of rows and columns necessary to draw everything.
* Each colour square will be at least 3 pixels wide and tall
* 4 - The width and height of each colour squares
* 5 - The need of scrollbar or not.
* 6 - The width and height of the Display square
* 7 - the width and height of the Transparent square
* 8 - the width and height of the push button
*
* These values will be stored respectively in :
* 1 - Internal data of wxWindow. GetMinSize return it
* 2 - Internal data of wxWindow. GetMaxSize return it
* 3 - iNbRows and iNbColumns
* 4 - iColourWidth and iColourHeight respectively
* 5 - bScrollbars
* 6 - iDisplayWidth and iDisplayHeight respectively
* 7 - iDisplayWidth and iDisplayHeight / 2
* 8 - iDisplayWidth and iDisplayHeight / 2
*
* This method takes in parameter a client area size. It will try in this order:
* 1 - to use the available space + the ideal number of rows / columns
* 2 - to use the available space + any number of rows/columns
* 3 - to use as many rows or columns with a minimal size as long as space is available
* 4 - to reduce the size of the squares until they fit or they are too small (iMinimalSize x iMinimalSize pixels)
* 5 - to add scrollbars
*
* This function will have an impact on:
* 1 - painting
* 2 - hit test
* 3 - sizer fitting
*
* \param s : the size of the client area available for drawing
*/
void XPMColorPicker::ComputeDimensions(wxSize sClientSize)
{
int iOrientation2;
int iNbSquaresX, iNbSquaresY, iNbSquares;
int iHeight, iWidth, iComputedRowsOrCols, iSize;
bool bFinished;
//get the orientation of the control: vertical or horizontal
iOrientation2 = GetActualOrientation();
//Compute the dimensions units
GetLengthUnit();
iHeight = sClientSize.GetHeight();
iWidth = sClientSize.GetWidth();
//get the amount of rows and cols which fit in the current client area.
bFinished = false;
if (iOrientation2 == XPM_HORIZONTAL_ORIENTATION)
{
//horizontal layout
//1 - to use the available space for the ideal number of rows/columns
if (iMinimalSize < 1) iMinimalSize = 1;
if (iIdealNbRowsOrCols < 1) iIdealNbRowsOrCols = 1;
iColourHeight = iHeight / iIdealNbRowsOrCols; //the height of a square
iNbSquaresX = (iWidth - 2 * iDisplayWidth) / iMinimalSize; //how many squares fit on each row
iNbSquaresY = iIdealNbRowsOrCols;
if ((iColourHeight >= iMinimalSize) && (iNbSquaresX * iNbSquaresY >= iPaletteSize))
{
//there are enough place for rows.
//everything fit. finish the computation
bFinished = true;
bScrollbars = false;
iNbRows = iIdealNbRowsOrCols;
iNbColumns = iPaletteSize / iIdealNbRowsOrCols;
if (iNbColumns * iNbRows < iPaletteSize) iNbColumns++;
if (iNbColumns < 1) iNbColumns = 1;
if (iNbRows < 1) iNbRows = 1;
iColourWidth = (iWidth - 2 * iDisplayWidth) / iNbColumns;
iColourHeight = iHeight / iNbRows;
iDisplayWidth = iUnitX * 2;
iDisplayHeight = iHeight;
}
if (!bFinished)
{
//2 - to use the available space for any number of rows/columns
iComputedRowsOrCols = iHeight / iUnitY; //how many rows of ideal size fits in this
iNbSquaresX = (iWidth - 2 * iDisplayWidth) / iUnitX; //how many squares per row
if ((iComputedRowsOrCols >= iIdealNbRowsOrCols) && (iNbSquaresX * iComputedRowsOrCols >= iPaletteSize))
{
//there are enough place for rows.
//everything fit. finish the computation
bFinished = true;
bScrollbars = false;
iNbRows = iComputedRowsOrCols;
iNbColumns = iNbSquaresX;
if (iNbColumns < 1) iNbColumns = 1;
if (iNbRows < 1) iNbRows = 1;
iColourWidth = (iWidth - 2 * iDisplayWidth) / iNbColumns;
iColourHeight = iHeight / iNbRows;
iDisplayWidth = iUnitX * 2;
iDisplayHeight = iHeight;
}
}
if (!bFinished)
{
//3 - to use as many rows or columns with a minimal size as long as space is available
//compute how many squares can fit :
iNbSquaresX = (iWidth - 2 * iDisplayWidth) / iUnitX; //how many squares per row
iNbSquaresY = iHeight / iUnitY; //how many rows fits
iNbSquares = iNbSquaresX * iNbSquaresY;
if (iNbSquares >= iPaletteSize)
{
//everything fit. finish the computation
bFinished = true;
bScrollbars = false;
iNbRows = iNbSquaresY;
iNbColumns = iNbSquaresX;
if (iNbColumns < 1) iNbColumns = 1;
if (iNbRows < 1) iNbRows = 1;
iColourWidth = (iWidth - 2 * iDisplayWidth) / iNbColumns;
iColourHeight = iHeight / iNbRows;
iDisplayWidth = iUnitX * 2;
iDisplayHeight = iHeight;
}
}
if (!bFinished)
{
//4 - to reduce the size of the squares until they fit or they are too small (iMinimalSize x iMinimalSize pixels)
for(iSize = iUnitX; iSize >= iMinimalSize; iSize--)
{
iNbSquaresX = (iWidth - 2 * iDisplayWidth) / iSize; //how many squares per row
iNbSquaresY = iHeight / iSize; //how many rows fits
iNbSquares = iNbSquaresX * iNbSquaresY;
if (iNbSquares >= iPaletteSize)
{
//everything fit. finish the computation
bFinished = true;
bScrollbars = false;
iNbRows = iNbSquaresY;
iNbColumns = iNbSquaresX;
if (iNbColumns < 1) iNbColumns = 1;
if (iNbRows < 1) iNbRows = 1;
iColourWidth = (iWidth - 2 * iDisplayWidth) / iNbColumns;
iColourHeight = iHeight / iNbRows;
iDisplayWidth = iUnitX * 2;
iDisplayHeight = iHeight;
break;
}
}
if (iDisplayHeight < iHeight) iDisplayHeight = iHeight;
}
if (!bFinished)
{
//5 - to add scrollbars
int iHeightScrollBar;
iHeightScrollBar = wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y, this);
bFinished = true;
bScrollbars = true;
iNbRows = (iHeight - iHeightScrollBar) / XPM_IDEAL_SIZE;
if (iNbRows < 1) iNbRows = 1;
iNbColumns = iPaletteSize / iNbRows;
if (iNbRows * iNbColumns < iPaletteSize) iNbColumns++; //for avoiding round off errors.
if (iNbColumns < 1) iNbColumns = 1;
if (iNbRows < 1) iNbRows = 1;
iColourWidth = iUnitX;
iColourHeight = (iHeight - iHeightScrollBar) / iNbRows;
iDisplayWidth = iUnitX * 2;
iDisplayHeight = iHeight;