]> git.saurik.com Git - wxWidgets.git/blob - src/generic/colrdlgg.cpp
patch from Janos Vegh for improved template handling
[wxWidgets.git] / src / generic / colrdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: colrdlgg.cpp
3 // Purpose: Choice dialogs
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "colrdlgg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include <stdio.h>
26 #include "wx/utils.h"
27 #include "wx/intl.h"
28 #include "wx/dialog.h"
29 #include "wx/listbox.h"
30 #include "wx/button.h"
31 #include "wx/stattext.h"
32 #include "wx/layout.h"
33 #include "wx/dcclient.h"
34 #include "wx/slider.h"
35 #endif
36
37 #include "wx/generic/colrdlgg.h"
38
39 #if !USE_SHARED_LIBRARY
40 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourDialog, wxDialog)
41
42 BEGIN_EVENT_TABLE(wxGenericColourDialog, wxDialog)
43 EVT_BUTTON(wxID_ADD_CUSTOM, wxGenericColourDialog::OnAddCustom)
44 EVT_SLIDER(wxID_RED_SLIDER, wxGenericColourDialog::OnRedSlider)
45 EVT_SLIDER(wxID_GREEN_SLIDER, wxGenericColourDialog::OnGreenSlider)
46 EVT_SLIDER(wxID_BLUE_SLIDER, wxGenericColourDialog::OnBlueSlider)
47 EVT_PAINT(wxGenericColourDialog::OnPaint)
48 EVT_MOUSE_EVENTS(wxGenericColourDialog::OnMouseEvent)
49 EVT_CLOSE(wxGenericColourDialog::OnCloseWindow)
50 END_EVENT_TABLE()
51
52 #endif
53
54 /*
55 * Generic wxColourDialog
56 */
57
58 #define NUM_COLS 48
59 static wxString wxColourDialogNames[NUM_COLS]={_T("ORANGE"),
60 _T("GOLDENROD"),
61 _T("WHEAT"),
62 _T("SPRING GREEN"),
63 _T("SKY BLUE"),
64 _T("SLATE BLUE"),
65 _T("MEDIUM VIOLET RED"),
66 _T("PURPLE"),
67
68 _T("RED"),
69 _T("YELLOW"),
70 _T("MEDIUM SPRING GREEN"),
71 _T("PALE GREEN"),
72 _T("CYAN"),
73 _T("LIGHT STEEL BLUE"),
74 _T("ORCHID"),
75 _T("LIGHT MAGENTA"),
76
77 _T("BROWN"),
78 _T("YELLOW"),
79 _T("GREEN"),
80 _T("CADET BLUE"),
81 _T("MEDIUM BLUE"),
82 _T("MAGENTA"),
83 _T("MAROON"),
84 _T("ORANGE RED"),
85
86 _T("FIREBRICK"),
87 _T("CORAL"),
88 _T("FOREST GREEN"),
89 _T("AQUAMARINE"),
90 _T("BLUE"),
91 _T("NAVY"),
92 _T("THISTLE"),
93 _T("MEDIUM VIOLET RED"),
94
95 _T("INDIAN RED"),
96 _T("GOLD"),
97 _T("MEDIUM SEA GREEN"),
98 _T("MEDIUM BLUE"),
99 _T("MIDNIGHT BLUE"),
100 _T("GREY"),
101 _T("PURPLE"),
102 _T("KHAKI"),
103
104 _T("BLACK"),
105 _T("MEDIUM FOREST GREEN"),
106 _T("KHAKI"),
107 _T("DARK GREY"),
108 _T("SEA GREEN"),
109 _T("LIGHT GREY"),
110 _T("MEDIUM SLATE BLUE"),
111 _T("WHITE")
112 };
113
114 wxGenericColourDialog::wxGenericColourDialog(void)
115 {
116 dialogParent = NULL;
117 whichKind = 1;
118 colourSelection = 0;
119 }
120
121 wxGenericColourDialog::wxGenericColourDialog(wxWindow *parent, wxColourData *data):
122 wxDialog(parent, -1, "Colour", wxPoint(0, 0), wxSize(900, 900), wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
123 {
124 whichKind = 1;
125 colourSelection = 0;
126 Create(parent, data);
127 }
128
129 wxGenericColourDialog::~wxGenericColourDialog(void)
130 {
131 }
132
133 void wxGenericColourDialog::OnCloseWindow(wxCloseEvent& event)
134 {
135 EndModal(wxID_CANCEL);
136 }
137
138 bool wxGenericColourDialog::Create(wxWindow *parent, wxColourData *data)
139 {
140 dialogParent = parent;
141
142 if (data)
143 colourData = *data;
144
145 InitializeColours();
146 CalculateMeasurements();
147 CreateWidgets();
148
149 return TRUE;
150 }
151
152 int wxGenericColourDialog::ShowModal(void)
153 {
154 return wxDialog::ShowModal();
155 }
156
157
158 // Internal functions
159 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent& event)
160 {
161 if (event.ButtonDown(1))
162 {
163 int x = (int)event.GetX();
164 int y = (int)event.GetY();
165
166 if ((x >= standardColoursRect.x && x <= (standardColoursRect.x + standardColoursRect.width)) &&
167 (y >= standardColoursRect.y && y <= (standardColoursRect.y + standardColoursRect.height)))
168 {
169 int selX = (int)(x - standardColoursRect.x)/(smallRectangleSize.x + gridSpacing);
170 int selY = (int)(y - standardColoursRect.y)/(smallRectangleSize.y + gridSpacing);
171 int ptr = (int)(selX + selY*8);
172 OnBasicColourClick(ptr);
173 }
174 else if ((x >= customColoursRect.x && x <= (customColoursRect.x + customColoursRect.width)) &&
175 (y >= customColoursRect.y && y <= (customColoursRect.y + customColoursRect.height)))
176 {
177 int selX = (int)(x - customColoursRect.x)/(smallRectangleSize.x + gridSpacing);
178 int selY = (int)(y - customColoursRect.y)/(smallRectangleSize.y + gridSpacing);
179 int ptr = (int)(selX + selY*8);
180 OnCustomColourClick(ptr);
181 }
182 }
183 }
184
185 void wxGenericColourDialog::OnPaint(wxPaintEvent& event)
186 {
187 wxDialog::OnPaint(event);
188
189 wxPaintDC dc(this);
190
191 PaintBasicColours(dc);
192 PaintCustomColours(dc);
193 PaintCustomColour(dc);
194 PaintHighlight(dc, TRUE);
195 }
196
197 void wxGenericColourDialog::CalculateMeasurements(void)
198 {
199 smallRectangleSize.x = 18;
200 smallRectangleSize.y = 14;
201 customRectangleSize.x = 40;
202 customRectangleSize.y = 40;
203
204 gridSpacing = 6;
205 sectionSpacing = 15;
206
207 standardColoursRect.x = 10;
208 standardColoursRect.y = 15;
209 standardColoursRect.width = (8*smallRectangleSize.x) + (7*gridSpacing);
210 standardColoursRect.height = (6*smallRectangleSize.y) + (5*gridSpacing);
211
212 customColoursRect.x = standardColoursRect.x;
213 customColoursRect.y = standardColoursRect.y + standardColoursRect.height + 20;
214 customColoursRect.width = (8*smallRectangleSize.x) + (7*gridSpacing);
215 customColoursRect.height = (2*smallRectangleSize.y) + (1*gridSpacing);
216
217 singleCustomColourRect.x = customColoursRect.width + customColoursRect.x + sectionSpacing;
218 singleCustomColourRect.y = 80;
219 singleCustomColourRect.width = customRectangleSize.x;
220 singleCustomColourRect.height = customRectangleSize.y;
221
222 okButtonX = 10;
223 customButtonX = singleCustomColourRect.x ;
224 buttonY = customColoursRect.y + customColoursRect.height + 10;
225 }
226
227 void wxGenericColourDialog::CreateWidgets(void)
228 {
229 wxBeginBusyCursor();
230
231 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"), wxPoint(okButtonX, buttonY), wxSize(75,-1) );
232 int bw, bh;
233 okButton->GetSize(&bw, &bh);
234
235 (void) new wxButton(this, wxID_CANCEL, _("Cancel"), wxPoint(okButtonX + bw + 20, buttonY), wxSize(75,-1));
236 (void) new wxButton(this, wxID_ADD_CUSTOM, _("Add to custom colours"),
237 wxPoint(customButtonX, buttonY));
238
239 int sliderX = singleCustomColourRect.x + singleCustomColourRect.width + sectionSpacing;
240 #ifdef __X__
241 int sliderSpacing = 75;
242 int sliderHeight = 160;
243 #else
244 int sliderSpacing = 45;
245 int sliderHeight = 160;
246 #endif
247
248 redSlider = new wxSlider(this, wxID_RED_SLIDER, 0, 0, 255,
249 wxPoint(sliderX, 10), wxSize(-1, sliderHeight), wxVERTICAL|wxSL_LABELS);
250 greenSlider = new wxSlider(this, wxID_GREEN_SLIDER, 0, 0, 255,
251 wxPoint(sliderX + sliderSpacing, 10), wxSize(-1, sliderHeight), wxVERTICAL|wxSL_LABELS);
252 blueSlider = new wxSlider(this, wxID_BLUE_SLIDER, 0, 0, 255,
253 wxPoint(sliderX + 2*sliderSpacing, 10), wxSize(-1, sliderHeight), wxVERTICAL|wxSL_LABELS);
254
255 SetClientSize(sliderX + 3*sliderSpacing, buttonY + 40);
256 okButton->SetDefault();
257
258 Centre(wxBOTH);
259
260 wxEndBusyCursor();
261 }
262
263 void wxGenericColourDialog::InitializeColours(void)
264 {
265 int i;
266 for (i = 0; i < 48; i++)
267 {
268 wxColour *col = wxTheColourDatabase->FindColour(wxColourDialogNames[i]);
269 if (col)
270 standardColours[i].Set(col->Red(), col->Green(), col->Blue());
271 else
272 standardColours[i].Set(0, 0, 0);
273 }
274
275 for (i = 0; i < 16; i++)
276 customColours[i] =
277 /*
278 #ifndef __VMS__
279 (wxColour&)
280 #endif
281 */
282 colourData.GetCustomColour(i);
283
284 singleCustomColour.Set(0, 0, 0);
285 }
286
287 void wxGenericColourDialog::PaintBasicColours(wxDC& dc)
288 {
289 dc.BeginDrawing();
290
291 int i;
292 for (i = 0; i < 6; i++)
293 {
294 int j;
295 for (j = 0; j < 8; j++)
296 {
297 int ptr = i*8 + j;
298
299 int x = (j*(smallRectangleSize.x+gridSpacing) + standardColoursRect.x);
300 int y = (i*(smallRectangleSize.y+gridSpacing) + standardColoursRect.y);
301
302 dc.SetPen(*wxBLACK_PEN);
303 wxBrush brush(standardColours[ptr], wxSOLID);
304 dc.SetBrush(brush);
305
306 dc.DrawRectangle( x, y, smallRectangleSize.x, smallRectangleSize.y);
307 }
308 }
309 dc.EndDrawing();
310 }
311
312 void wxGenericColourDialog::PaintCustomColours(wxDC& dc)
313 {
314 dc.BeginDrawing();
315
316 int i;
317 for (i = 0; i < 2; i++)
318 {
319 int j;
320 for (j = 0; j < 8; j++)
321 {
322 int ptr = i*8 + j;
323
324 int x = (j*(smallRectangleSize.x+gridSpacing)) + customColoursRect.x;
325 int y = (i*(smallRectangleSize.y+gridSpacing)) + customColoursRect.y;
326
327 dc.SetPen(*wxBLACK_PEN);
328
329 wxBrush brush(customColours[ptr], wxSOLID);
330 dc.SetBrush(brush);
331
332 dc.DrawRectangle( x, y, smallRectangleSize.x, smallRectangleSize.y);
333 }
334 }
335 dc.EndDrawing();
336 }
337
338 void wxGenericColourDialog::PaintHighlight(wxDC& dc, bool draw)
339 {
340 dc.BeginDrawing();
341
342 // Number of pixels bigger than the standard rectangle size
343 // for drawing a highlight
344 int deltaX = 2;
345 int deltaY = 2;
346
347 if (whichKind == 1)
348 {
349 // Standard colours
350 int y = (int)(colourSelection / 8);
351 int x = (int)(colourSelection - (y*8));
352
353 x = (x*(smallRectangleSize.x + gridSpacing) + standardColoursRect.x) - deltaX;
354 y = (y*(smallRectangleSize.y + gridSpacing) + standardColoursRect.y) - deltaY;
355
356 if (draw)
357 dc.SetPen(*wxBLACK_PEN);
358 else
359 dc.SetPen(*wxLIGHT_GREY_PEN);
360
361 dc.SetBrush(*wxTRANSPARENT_BRUSH);
362 dc.DrawRectangle( x, y, (smallRectangleSize.x + (2*deltaX)), (smallRectangleSize.y + (2*deltaY)));
363 }
364 else
365 {
366 // User-defined colours
367 int y = (int)(colourSelection / 8);
368 int x = (int)(colourSelection - (y*8));
369
370 x = (x*(smallRectangleSize.x + gridSpacing) + customColoursRect.x) - deltaX;
371 y = (y*(smallRectangleSize.y + gridSpacing) + customColoursRect.y) - deltaY;
372
373 if (draw)
374 dc.SetPen(*wxBLACK_PEN);
375 else
376 dc.SetPen(*wxLIGHT_GREY_PEN);
377
378 dc.SetBrush(*wxTRANSPARENT_BRUSH);
379 dc.DrawRectangle( x, y, (smallRectangleSize.x + (2*deltaX)), (smallRectangleSize.y + (2*deltaY)));
380 }
381
382 dc.EndDrawing();
383 }
384
385 void wxGenericColourDialog::PaintCustomColour(wxDC& dc)
386 {
387 dc.BeginDrawing();
388
389 dc.SetPen(*wxBLACK_PEN);
390
391 wxBrush *brush = new wxBrush(singleCustomColour, wxSOLID);
392 dc.SetBrush(*brush);
393
394 dc.DrawRectangle( singleCustomColourRect.x, singleCustomColourRect.y,
395 customRectangleSize.x, customRectangleSize.y);
396
397 dc.SetBrush(wxNullBrush);
398 delete brush;
399
400 dc.EndDrawing();
401 }
402
403 void wxGenericColourDialog::OnBasicColourClick(int which)
404 {
405 wxClientDC dc(this);
406
407 PaintHighlight(dc, FALSE);
408 whichKind = 1;
409 colourSelection = which;
410 colourData.SetColour(standardColours[colourSelection]);
411
412 PaintHighlight(dc, TRUE);
413 }
414
415 void wxGenericColourDialog::OnCustomColourClick(int which)
416 {
417 wxClientDC dc(this);
418 PaintHighlight(dc, FALSE);
419 whichKind = 2;
420 colourSelection = which;
421 colourData.SetColour(customColours[colourSelection]);
422
423 PaintHighlight(dc, TRUE);
424 }
425
426 /*
427 void wxGenericColourDialog::OnOk(void)
428 {
429 Show(FALSE);
430 }
431
432 void wxGenericColourDialog::OnCancel(void)
433 {
434 colourDialogCancelled = TRUE;
435 Show(FALSE);
436 }
437 */
438
439 void wxGenericColourDialog::OnAddCustom(wxCommandEvent& WXUNUSED(event))
440 {
441 wxClientDC dc(this);
442 if (whichKind != 2)
443 {
444 PaintHighlight(dc, FALSE);
445 whichKind = 2;
446 colourSelection = 0;
447 PaintHighlight(dc, TRUE);
448 }
449
450 customColours[colourSelection].Set(singleCustomColour.Red(), singleCustomColour.Green(), singleCustomColour.Blue());
451 colourData.SetColour(customColours[colourSelection]);
452 colourData.SetCustomColour(colourSelection, customColours[colourSelection]);
453
454 PaintCustomColours(dc);
455 }
456
457 void wxGenericColourDialog::OnRedSlider(wxCommandEvent& WXUNUSED(event))
458 {
459 if (!redSlider)
460 return;
461
462 wxClientDC dc(this);
463 singleCustomColour.Set(redSlider->GetValue(), singleCustomColour.Green(), singleCustomColour.Blue());
464 PaintCustomColour(dc);
465 }
466
467 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent& WXUNUSED(event))
468 {
469 if (!greenSlider)
470 return;
471
472 wxClientDC dc(this);
473 singleCustomColour.Set(singleCustomColour.Red(), greenSlider->GetValue(), singleCustomColour.Blue());
474 PaintCustomColour(dc);
475 }
476
477 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent& WXUNUSED(event))
478 {
479 if (!blueSlider)
480 return;
481
482 wxClientDC dc(this);
483 singleCustomColour.Set(singleCustomColour.Red(), singleCustomColour.Green(), blueSlider->GetValue());
484 PaintCustomColour(dc);
485 }
486
487