misc minor fixes I forgot to commit before
[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& WXUNUSED(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 #ifndef __WXMOTIF__
188 wxDialog::OnPaint(event);
189 #endif
190
191 wxPaintDC dc(this);
192
193 PaintBasicColours(dc);
194 PaintCustomColours(dc);
195 PaintCustomColour(dc);
196 PaintHighlight(dc, TRUE);
197 }
198
199 void wxGenericColourDialog::CalculateMeasurements(void)
200 {
201 smallRectangleSize.x = 18;
202 smallRectangleSize.y = 14;
203 customRectangleSize.x = 40;
204 customRectangleSize.y = 40;
205
206 gridSpacing = 6;
207 sectionSpacing = 15;
208
209 standardColoursRect.x = 10;
210 standardColoursRect.y = 15;
211 standardColoursRect.width = (8*smallRectangleSize.x) + (7*gridSpacing);
212 standardColoursRect.height = (6*smallRectangleSize.y) + (5*gridSpacing);
213
214 customColoursRect.x = standardColoursRect.x;
215 customColoursRect.y = standardColoursRect.y + standardColoursRect.height + 20;
216 customColoursRect.width = (8*smallRectangleSize.x) + (7*gridSpacing);
217 customColoursRect.height = (2*smallRectangleSize.y) + (1*gridSpacing);
218
219 singleCustomColourRect.x = customColoursRect.width + customColoursRect.x + sectionSpacing;
220 singleCustomColourRect.y = 80;
221 singleCustomColourRect.width = customRectangleSize.x;
222 singleCustomColourRect.height = customRectangleSize.y;
223
224 okButtonX = 10;
225 customButtonX = singleCustomColourRect.x ;
226 buttonY = customColoursRect.y + customColoursRect.height + 10;
227 }
228
229 void wxGenericColourDialog::CreateWidgets(void)
230 {
231 wxBeginBusyCursor();
232
233 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"), wxPoint(okButtonX, buttonY), wxSize(75,-1) );
234 int bw, bh;
235 okButton->GetSize(&bw, &bh);
236
237 (void) new wxButton(this, wxID_CANCEL, _("Cancel"), wxPoint(okButtonX + bw + 20, buttonY), wxSize(75,-1));
238 (void) new wxButton(this, wxID_ADD_CUSTOM, _("Add to custom colours"),
239 wxPoint(customButtonX, buttonY));
240
241 int sliderX = singleCustomColourRect.x + singleCustomColourRect.width + sectionSpacing;
242 #ifdef __X__
243 int sliderSpacing = 75;
244 int sliderHeight = 160;
245 #else
246 int sliderSpacing = 45;
247 int sliderHeight = 160;
248 #endif
249
250 redSlider = new wxSlider(this, wxID_RED_SLIDER, 0, 0, 255,
251 wxPoint(sliderX, 10), wxSize(-1, sliderHeight), wxVERTICAL|wxSL_LABELS);
252 greenSlider = new wxSlider(this, wxID_GREEN_SLIDER, 0, 0, 255,
253 wxPoint(sliderX + sliderSpacing, 10), wxSize(-1, sliderHeight), wxVERTICAL|wxSL_LABELS);
254 blueSlider = new wxSlider(this, wxID_BLUE_SLIDER, 0, 0, 255,
255 wxPoint(sliderX + 2*sliderSpacing, 10), wxSize(-1, sliderHeight), wxVERTICAL|wxSL_LABELS);
256
257 SetClientSize(sliderX + 3*sliderSpacing, buttonY + 40);
258 okButton->SetDefault();
259
260 Centre(wxBOTH);
261
262 wxEndBusyCursor();
263 }
264
265 void wxGenericColourDialog::InitializeColours(void)
266 {
267 int i;
268 for (i = 0; i < 48; i++)
269 {
270 wxColour *col = wxTheColourDatabase->FindColour(wxColourDialogNames[i]);
271 if (col)
272 standardColours[i].Set(col->Red(), col->Green(), col->Blue());
273 else
274 standardColours[i].Set(0, 0, 0);
275 }
276
277 for (i = 0; i < 16; i++)
278 customColours[i] =
279 /*
280 #ifndef __VMS__
281 (wxColour&)
282 #endif
283 */
284 colourData.GetCustomColour(i);
285
286 singleCustomColour.Set(0, 0, 0);
287 }
288
289 void wxGenericColourDialog::PaintBasicColours(wxDC& dc)
290 {
291 dc.BeginDrawing();
292
293 int i;
294 for (i = 0; i < 6; i++)
295 {
296 int j;
297 for (j = 0; j < 8; j++)
298 {
299 int ptr = i*8 + j;
300
301 int x = (j*(smallRectangleSize.x+gridSpacing) + standardColoursRect.x);
302 int y = (i*(smallRectangleSize.y+gridSpacing) + standardColoursRect.y);
303
304 dc.SetPen(*wxBLACK_PEN);
305 wxBrush brush(standardColours[ptr], wxSOLID);
306 dc.SetBrush(brush);
307
308 dc.DrawRectangle( x, y, smallRectangleSize.x, smallRectangleSize.y);
309 }
310 }
311 dc.EndDrawing();
312 }
313
314 void wxGenericColourDialog::PaintCustomColours(wxDC& dc)
315 {
316 dc.BeginDrawing();
317
318 int i;
319 for (i = 0; i < 2; i++)
320 {
321 int j;
322 for (j = 0; j < 8; j++)
323 {
324 int ptr = i*8 + j;
325
326 int x = (j*(smallRectangleSize.x+gridSpacing)) + customColoursRect.x;
327 int y = (i*(smallRectangleSize.y+gridSpacing)) + customColoursRect.y;
328
329 dc.SetPen(*wxBLACK_PEN);
330
331 wxBrush brush(customColours[ptr], wxSOLID);
332 dc.SetBrush(brush);
333
334 dc.DrawRectangle( x, y, smallRectangleSize.x, smallRectangleSize.y);
335 }
336 }
337 dc.EndDrawing();
338 }
339
340 void wxGenericColourDialog::PaintHighlight(wxDC& dc, bool draw)
341 {
342 dc.BeginDrawing();
343
344 // Number of pixels bigger than the standard rectangle size
345 // for drawing a highlight
346 int deltaX = 2;
347 int deltaY = 2;
348
349 if (whichKind == 1)
350 {
351 // Standard colours
352 int y = (int)(colourSelection / 8);
353 int x = (int)(colourSelection - (y*8));
354
355 x = (x*(smallRectangleSize.x + gridSpacing) + standardColoursRect.x) - deltaX;
356 y = (y*(smallRectangleSize.y + gridSpacing) + standardColoursRect.y) - deltaY;
357
358 if (draw)
359 dc.SetPen(*wxBLACK_PEN);
360 else
361 dc.SetPen(*wxLIGHT_GREY_PEN);
362
363 dc.SetBrush(*wxTRANSPARENT_BRUSH);
364 dc.DrawRectangle( x, y, (smallRectangleSize.x + (2*deltaX)), (smallRectangleSize.y + (2*deltaY)));
365 }
366 else
367 {
368 // User-defined colours
369 int y = (int)(colourSelection / 8);
370 int x = (int)(colourSelection - (y*8));
371
372 x = (x*(smallRectangleSize.x + gridSpacing) + customColoursRect.x) - deltaX;
373 y = (y*(smallRectangleSize.y + gridSpacing) + customColoursRect.y) - deltaY;
374
375 if (draw)
376 dc.SetPen(*wxBLACK_PEN);
377 else
378 dc.SetPen(*wxLIGHT_GREY_PEN);
379
380 dc.SetBrush(*wxTRANSPARENT_BRUSH);
381 dc.DrawRectangle( x, y, (smallRectangleSize.x + (2*deltaX)), (smallRectangleSize.y + (2*deltaY)));
382 }
383
384 dc.EndDrawing();
385 }
386
387 void wxGenericColourDialog::PaintCustomColour(wxDC& dc)
388 {
389 dc.BeginDrawing();
390
391 dc.SetPen(*wxBLACK_PEN);
392
393 wxBrush *brush = new wxBrush(singleCustomColour, wxSOLID);
394 dc.SetBrush(*brush);
395
396 dc.DrawRectangle( singleCustomColourRect.x, singleCustomColourRect.y,
397 customRectangleSize.x, customRectangleSize.y);
398
399 dc.SetBrush(wxNullBrush);
400 delete brush;
401
402 dc.EndDrawing();
403 }
404
405 void wxGenericColourDialog::OnBasicColourClick(int which)
406 {
407 wxClientDC dc(this);
408
409 PaintHighlight(dc, FALSE);
410 whichKind = 1;
411 colourSelection = which;
412 colourData.SetColour(standardColours[colourSelection]);
413
414 PaintHighlight(dc, TRUE);
415 }
416
417 void wxGenericColourDialog::OnCustomColourClick(int which)
418 {
419 wxClientDC dc(this);
420 PaintHighlight(dc, FALSE);
421 whichKind = 2;
422 colourSelection = which;
423 colourData.SetColour(customColours[colourSelection]);
424
425 PaintHighlight(dc, TRUE);
426 }
427
428 /*
429 void wxGenericColourDialog::OnOk(void)
430 {
431 Show(FALSE);
432 }
433
434 void wxGenericColourDialog::OnCancel(void)
435 {
436 colourDialogCancelled = TRUE;
437 Show(FALSE);
438 }
439 */
440
441 void wxGenericColourDialog::OnAddCustom(wxCommandEvent& WXUNUSED(event))
442 {
443 wxClientDC dc(this);
444 if (whichKind != 2)
445 {
446 PaintHighlight(dc, FALSE);
447 whichKind = 2;
448 colourSelection = 0;
449 PaintHighlight(dc, TRUE);
450 }
451
452 customColours[colourSelection].Set(singleCustomColour.Red(), singleCustomColour.Green(), singleCustomColour.Blue());
453 colourData.SetColour(customColours[colourSelection]);
454 colourData.SetCustomColour(colourSelection, customColours[colourSelection]);
455
456 PaintCustomColours(dc);
457 }
458
459 void wxGenericColourDialog::OnRedSlider(wxCommandEvent& WXUNUSED(event))
460 {
461 if (!redSlider)
462 return;
463
464 wxClientDC dc(this);
465 singleCustomColour.Set(redSlider->GetValue(), singleCustomColour.Green(), singleCustomColour.Blue());
466 PaintCustomColour(dc);
467 }
468
469 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent& WXUNUSED(event))
470 {
471 if (!greenSlider)
472 return;
473
474 wxClientDC dc(this);
475 singleCustomColour.Set(singleCustomColour.Red(), greenSlider->GetValue(), singleCustomColour.Blue());
476 PaintCustomColour(dc);
477 }
478
479 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent& WXUNUSED(event))
480 {
481 if (!blueSlider)
482 return;
483
484 wxClientDC dc(this);
485 singleCustomColour.Set(singleCustomColour.Red(), singleCustomColour.Green(), blueSlider->GetValue());
486 PaintCustomColour(dc);
487 }
488
489