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