add m_ prefix to the member variables and remove unneeded dialogParent field (part...
[wxWidgets.git] / src / generic / colrdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_COLOURDLG && (!defined(__WXGTK20__) || defined(__WXUNIVERSAL__))
20
21 #ifndef WX_PRECOMP
22 #include "wx/utils.h"
23 #include "wx/intl.h"
24 #include "wx/dialog.h"
25 #include "wx/listbox.h"
26 #include "wx/button.h"
27 #include "wx/stattext.h"
28 #include "wx/layout.h"
29 #include "wx/dcclient.h"
30 #include "wx/sizer.h"
31 #include "wx/slider.h"
32 #endif
33
34 #if wxUSE_STATLINE
35 #include "wx/statline.h"
36 #endif
37
38 #include "wx/generic/colrdlgg.h"
39
40 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourDialog, wxDialog)
41
42 BEGIN_EVENT_TABLE(wxGenericColourDialog, wxDialog)
43 EVT_BUTTON(wxID_ADD_CUSTOM, wxGenericColourDialog::OnAddCustom)
44 #if wxUSE_SLIDER
45 EVT_SLIDER(wxID_RED_SLIDER, wxGenericColourDialog::OnRedSlider)
46 EVT_SLIDER(wxID_GREEN_SLIDER, wxGenericColourDialog::OnGreenSlider)
47 EVT_SLIDER(wxID_BLUE_SLIDER, wxGenericColourDialog::OnBlueSlider)
48 #endif
49 EVT_PAINT(wxGenericColourDialog::OnPaint)
50 EVT_MOUSE_EVENTS(wxGenericColourDialog::OnMouseEvent)
51 EVT_CLOSE(wxGenericColourDialog::OnCloseWindow)
52 END_EVENT_TABLE()
53
54
55 /*
56 * Generic wxColourDialog
57 */
58
59 // don't change the number of elements (48) in this array, the code below is
60 // hardcoded to use it
61 static const wxChar *wxColourDialogNames[] =
62 {
63 wxT("ORANGE"),
64 wxT("GOLDENROD"),
65 wxT("WHEAT"),
66 wxT("SPRING GREEN"),
67 wxT("SKY BLUE"),
68 wxT("SLATE BLUE"),
69 wxT("MEDIUM VIOLET RED"),
70 wxT("PURPLE"),
71
72 wxT("RED"),
73 wxT("YELLOW"),
74 wxT("MEDIUM SPRING GREEN"),
75 wxT("PALE GREEN"),
76 wxT("CYAN"),
77 wxT("LIGHT STEEL BLUE"),
78 wxT("ORCHID"),
79 wxT("LIGHT MAGENTA"),
80
81 wxT("BROWN"),
82 wxT("YELLOW"),
83 wxT("GREEN"),
84 wxT("CADET BLUE"),
85 wxT("MEDIUM BLUE"),
86 wxT("MAGENTA"),
87 wxT("MAROON"),
88 wxT("ORANGE RED"),
89
90 wxT("FIREBRICK"),
91 wxT("CORAL"),
92 wxT("FOREST GREEN"),
93 wxT("AQUAMARINE"),
94 wxT("BLUE"),
95 wxT("NAVY"),
96 wxT("THISTLE"),
97 wxT("MEDIUM VIOLET RED"),
98
99 wxT("INDIAN RED"),
100 wxT("GOLD"),
101 wxT("MEDIUM SEA GREEN"),
102 wxT("MEDIUM BLUE"),
103 wxT("MIDNIGHT BLUE"),
104 wxT("GREY"),
105 wxT("PURPLE"),
106 wxT("KHAKI"),
107
108 wxT("BLACK"),
109 wxT("MEDIUM FOREST GREEN"),
110 wxT("KHAKI"),
111 wxT("DARK GREY"),
112 wxT("SEA GREEN"),
113 wxT("LIGHT GREY"),
114 wxT("MEDIUM SLATE BLUE"),
115 wxT("WHITE")
116 };
117
118 wxGenericColourDialog::wxGenericColourDialog()
119 {
120 m_whichKind = 1;
121 m_colourSelection = -1;
122 }
123
124 wxGenericColourDialog::wxGenericColourDialog(wxWindow *parent,
125 wxColourData *data)
126 {
127 m_whichKind = 1;
128 m_colourSelection = -1;
129 Create(parent, data);
130 }
131
132 wxGenericColourDialog::~wxGenericColourDialog()
133 {
134 }
135
136 void wxGenericColourDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
137 {
138 EndModal(wxID_CANCEL);
139 }
140
141 bool wxGenericColourDialog::Create(wxWindow *parent, wxColourData *data)
142 {
143 if ( !wxDialog::Create(parent, wxID_ANY, _("Choose colour"),
144 wxPoint(0,0), wxSize(900, 900)) )
145 return false;
146
147 if (data)
148 m_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 #ifdef __WXPM__
172 // Handle OS/2's reverse coordinate system and account for the dialog title
173 int nClientHeight;
174
175 GetClientSize(NULL, &nClientHeight);
176 y = (nClientHeight - y) + 20;
177 #endif
178 if ((x >= m_standardColoursRect.x && x <= (m_standardColoursRect.x + m_standardColoursRect.width)) &&
179 (y >= m_standardColoursRect.y && y <= (m_standardColoursRect.y + m_standardColoursRect.height)))
180 {
181 int selX = (int)(x - m_standardColoursRect.x)/(m_smallRectangleSize.x + m_gridSpacing);
182 int selY = (int)(y - m_standardColoursRect.y)/(m_smallRectangleSize.y + m_gridSpacing);
183 int ptr = (int)(selX + selY*8);
184 OnBasicColourClick(ptr);
185 }
186 else if ((x >= m_customColoursRect.x && x <= (m_customColoursRect.x + m_customColoursRect.width)) &&
187 (y >= m_customColoursRect.y && y <= (m_customColoursRect.y + m_customColoursRect.height)))
188 {
189 int selX = (int)(x - m_customColoursRect.x)/(m_smallRectangleSize.x + m_gridSpacing);
190 int selY = (int)(y - m_customColoursRect.y)/(m_smallRectangleSize.y + m_gridSpacing);
191 int ptr = (int)(selX + selY*8);
192 OnCustomColourClick(ptr);
193 }
194 else
195 event.Skip();
196 }
197 else
198 event.Skip();
199 }
200
201 void wxGenericColourDialog::OnPaint(wxPaintEvent& event)
202 {
203 #if !defined(__WXMOTIF__) && !defined(__WXPM__) && !defined(__WXCOCOA__)
204 wxDialog::OnPaint(event);
205 #else
206 wxUnusedVar(event);
207 #endif
208
209 wxPaintDC dc(this);
210
211 PaintBasicColours(dc);
212 PaintCustomColours(dc);
213 PaintCustomColour(dc);
214 PaintHighlight(dc, true);
215 }
216
217 void wxGenericColourDialog::CalculateMeasurements()
218 {
219 m_smallRectangleSize.x = 18;
220 m_smallRectangleSize.y = 14;
221 m_customRectangleSize.x = 40;
222 m_customRectangleSize.y = 40;
223
224 m_gridSpacing = 6;
225 m_sectionSpacing = 15;
226
227 m_standardColoursRect.x = 10;
228 #ifdef __WXPM__
229 m_standardColoursRect.y = 15 + 20; /* OS/2 needs to account for dialog titlebar */
230 #else
231 m_standardColoursRect.y = 15;
232 #endif
233 m_standardColoursRect.width = (8*m_smallRectangleSize.x) + (7*m_gridSpacing);
234 m_standardColoursRect.height = (6*m_smallRectangleSize.y) + (5*m_gridSpacing);
235
236 m_customColoursRect.x = m_standardColoursRect.x;
237 m_customColoursRect.y = m_standardColoursRect.y + m_standardColoursRect.height + 20;
238 m_customColoursRect.width = (8*m_smallRectangleSize.x) + (7*m_gridSpacing);
239 m_customColoursRect.height = (2*m_smallRectangleSize.y) + (1*m_gridSpacing);
240
241 m_singleCustomColourRect.x = m_customColoursRect.width + m_customColoursRect.x + m_sectionSpacing;
242 m_singleCustomColourRect.y = 80;
243 m_singleCustomColourRect.width = m_customRectangleSize.x;
244 m_singleCustomColourRect.height = m_customRectangleSize.y;
245
246 m_okButtonX = 10;
247 m_customButtonX = m_singleCustomColourRect.x ;
248 m_buttonY = m_customColoursRect.y + m_customColoursRect.height + 10;
249 }
250
251 void wxGenericColourDialog::CreateWidgets()
252 {
253 wxBeginBusyCursor();
254
255 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
256
257 const int sliderHeight = 160;
258
259 // first sliders
260 #if wxUSE_SLIDER
261 const int sliderX = m_singleCustomColourRect.x + m_singleCustomColourRect.width + m_sectionSpacing;
262
263 m_redSlider = new wxSlider(this, wxID_RED_SLIDER, m_colourData.m_dataColour.Red(), 0, 255,
264 wxDefaultPosition, wxSize(wxDefaultCoord, sliderHeight), wxSL_VERTICAL|wxSL_LABELS|wxSL_INVERSE);
265 m_greenSlider = new wxSlider(this, wxID_GREEN_SLIDER, m_colourData.m_dataColour.Green(), 0, 255,
266 wxDefaultPosition, wxSize(wxDefaultCoord, sliderHeight), wxSL_VERTICAL|wxSL_LABELS|wxSL_INVERSE);
267 m_blueSlider = new wxSlider(this, wxID_BLUE_SLIDER, m_colourData.m_dataColour.Blue(), 0, 255,
268 wxDefaultPosition, wxSize(wxDefaultCoord, sliderHeight), wxSL_VERTICAL|wxSL_LABELS|wxSL_INVERSE);
269
270 wxBoxSizer *sliderSizer = new wxBoxSizer( wxHORIZONTAL );
271
272 sliderSizer->Add(sliderX, sliderHeight );
273
274 wxSizerFlags flagsRight;
275 flagsRight.Align(wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL).DoubleBorder();
276
277 sliderSizer->Add(m_redSlider, flagsRight);
278 sliderSizer->Add(m_greenSlider,flagsRight);
279 sliderSizer->Add(m_blueSlider,flagsRight);
280
281 topSizer->Add(sliderSizer, wxSizerFlags().Centre().DoubleBorder());
282 #else
283 topSizer->Add(1, sliderHeight, wxSizerFlags(1).Centre().TripleBorder());
284 #endif // wxUSE_SLIDER
285
286 // then the custom button
287 topSizer->Add(new wxButton(this, wxID_ADD_CUSTOM,
288 _("Add to custom colours") ),
289 wxSizerFlags().DoubleHorzBorder());
290
291 // then the standard buttons
292 wxSizer *buttonsizer = CreateSeparatedButtonSizer(wxOK | wxCANCEL);
293 if ( buttonsizer )
294 {
295 topSizer->Add(buttonsizer, wxSizerFlags().Expand().DoubleBorder());
296 }
297
298 SetAutoLayout( true );
299 SetSizer( topSizer );
300
301 topSizer->SetSizeHints( this );
302 topSizer->Fit( this );
303
304 Centre( wxBOTH );
305
306 wxEndBusyCursor();
307 }
308
309 void wxGenericColourDialog::InitializeColours(void)
310 {
311 size_t i;
312
313 for (i = 0; i < WXSIZEOF(wxColourDialogNames); i++)
314 {
315 wxColour col = wxTheColourDatabase->Find(wxColourDialogNames[i]);
316 if (col.Ok())
317 m_standardColours[i].Set(col.Red(), col.Green(), col.Blue());
318 else
319 m_standardColours[i].Set(0, 0, 0);
320 }
321
322 for (i = 0; i < WXSIZEOF(m_customColours); i++)
323 {
324 wxColour c = m_colourData.GetCustomColour(i);
325 if (c.Ok())
326 m_customColours[i] = m_colourData.GetCustomColour(i);
327 else
328 m_customColours[i] = wxColour(255, 255, 255);
329 }
330
331 wxColour curr = m_colourData.GetColour();
332 if ( curr.Ok() )
333 {
334 bool m_initColourFound = false;
335
336 for (i = 0; i < WXSIZEOF(wxColourDialogNames); i++)
337 {
338 if ( m_standardColours[i] == curr && !m_initColourFound )
339 {
340 m_whichKind = 1;
341 m_colourSelection = i;
342 m_initColourFound = true;
343 break;
344 }
345 }
346 if ( !m_initColourFound )
347 {
348 for ( i = 0; i < WXSIZEOF(m_customColours); i++ )
349 {
350 if ( m_customColours[i] == curr )
351 {
352 m_whichKind = 2;
353 m_colourSelection = i;
354 break;
355 }
356 }
357 }
358 m_colourData.m_dataColour.Set( curr.Red(), curr.Green(), curr.Blue() );
359 }
360 else
361 {
362 m_whichKind = 1;
363 m_colourSelection = 0;
364 m_colourData.m_dataColour.Set( 0, 0, 0 );
365 }
366 }
367
368 void wxGenericColourDialog::PaintBasicColours(wxDC& dc)
369 {
370 int i;
371 for (i = 0; i < 6; i++)
372 {
373 int j;
374 for (j = 0; j < 8; j++)
375 {
376 int ptr = i*8 + j;
377
378 int x = (j*(m_smallRectangleSize.x+m_gridSpacing) + m_standardColoursRect.x);
379 int y = (i*(m_smallRectangleSize.y+m_gridSpacing) + m_standardColoursRect.y);
380
381 dc.SetPen(*wxBLACK_PEN);
382 wxBrush brush(m_standardColours[ptr], wxSOLID);
383 dc.SetBrush(brush);
384
385 dc.DrawRectangle( x, y, m_smallRectangleSize.x, m_smallRectangleSize.y);
386 }
387 }
388 }
389
390 void wxGenericColourDialog::PaintCustomColours(wxDC& dc)
391 {
392 int i;
393 for (i = 0; i < 2; i++)
394 {
395 int j;
396 for (j = 0; j < 8; j++)
397 {
398 int ptr = i*8 + j;
399
400 int x = (j*(m_smallRectangleSize.x+m_gridSpacing)) + m_customColoursRect.x;
401 int y = (i*(m_smallRectangleSize.y+m_gridSpacing)) + m_customColoursRect.y;
402
403 dc.SetPen(*wxBLACK_PEN);
404
405 wxBrush brush(m_customColours[ptr], wxSOLID);
406 dc.SetBrush(brush);
407
408 dc.DrawRectangle( x, y, m_smallRectangleSize.x, m_smallRectangleSize.y);
409 }
410 }
411 }
412
413 void wxGenericColourDialog::PaintHighlight(wxDC& dc, bool draw)
414 {
415 if ( m_colourSelection < 0 )
416 return;
417
418 // Number of pixels bigger than the standard rectangle size
419 // for drawing a highlight
420 int deltaX = 2;
421 int deltaY = 2;
422
423 if (m_whichKind == 1)
424 {
425 // Standard colours
426 int y = (int)(m_colourSelection / 8);
427 int x = (int)(m_colourSelection - (y*8));
428
429 x = (x*(m_smallRectangleSize.x + m_gridSpacing) + m_standardColoursRect.x) - deltaX;
430 y = (y*(m_smallRectangleSize.y + m_gridSpacing) + m_standardColoursRect.y) - deltaY;
431
432 if (draw)
433 dc.SetPen(*wxBLACK_PEN);
434 else
435 dc.SetPen(*wxLIGHT_GREY_PEN);
436
437 dc.SetBrush(*wxTRANSPARENT_BRUSH);
438 dc.DrawRectangle( x, y, (m_smallRectangleSize.x + (2*deltaX)), (m_smallRectangleSize.y + (2*deltaY)));
439 }
440 else
441 {
442 // User-defined colours
443 int y = (int)(m_colourSelection / 8);
444 int x = (int)(m_colourSelection - (y*8));
445
446 x = (x*(m_smallRectangleSize.x + m_gridSpacing) + m_customColoursRect.x) - deltaX;
447 y = (y*(m_smallRectangleSize.y + m_gridSpacing) + m_customColoursRect.y) - deltaY;
448
449 if (draw)
450 dc.SetPen(*wxBLACK_PEN);
451 else
452 dc.SetPen(*wxLIGHT_GREY_PEN);
453
454 dc.SetBrush(*wxTRANSPARENT_BRUSH);
455 dc.DrawRectangle( x, y, (m_smallRectangleSize.x + (2*deltaX)), (m_smallRectangleSize.y + (2*deltaY)));
456 }
457 }
458
459 void wxGenericColourDialog::PaintCustomColour(wxDC& dc)
460 {
461 dc.SetPen(*wxBLACK_PEN);
462
463 wxBrush *brush = new wxBrush(m_colourData.m_dataColour, wxSOLID);
464 dc.SetBrush(*brush);
465
466 dc.DrawRectangle( m_singleCustomColourRect.x, m_singleCustomColourRect.y,
467 m_customRectangleSize.x, m_customRectangleSize.y);
468
469 dc.SetBrush(wxNullBrush);
470 delete brush;
471 }
472
473 void wxGenericColourDialog::OnBasicColourClick(int which)
474 {
475 wxClientDC dc(this);
476
477 PaintHighlight(dc, false);
478 m_whichKind = 1;
479 m_colourSelection = which;
480
481 #if wxUSE_SLIDER
482 m_redSlider->SetValue( m_standardColours[m_colourSelection].Red() );
483 m_greenSlider->SetValue( m_standardColours[m_colourSelection].Green() );
484 m_blueSlider->SetValue( m_standardColours[m_colourSelection].Blue() );
485 #endif // wxUSE_SLIDER
486
487 m_colourData.m_dataColour.Set(m_standardColours[m_colourSelection].Red(),
488 m_standardColours[m_colourSelection].Green(),
489 m_standardColours[m_colourSelection].Blue());
490
491 PaintCustomColour(dc);
492 PaintHighlight(dc, true);
493 }
494
495 void wxGenericColourDialog::OnCustomColourClick(int which)
496 {
497 wxClientDC dc(this);
498 PaintHighlight(dc, false);
499 m_whichKind = 2;
500 m_colourSelection = which;
501
502 #if wxUSE_SLIDER
503 m_redSlider->SetValue( m_customColours[m_colourSelection].Red() );
504 m_greenSlider->SetValue( m_customColours[m_colourSelection].Green() );
505 m_blueSlider->SetValue( m_customColours[m_colourSelection].Blue() );
506 #endif // wxUSE_SLIDER
507
508 m_colourData.m_dataColour.Set(m_customColours[m_colourSelection].Red(),
509 m_customColours[m_colourSelection].Green(),
510 m_customColours[m_colourSelection].Blue());
511
512 PaintCustomColour(dc);
513 PaintHighlight(dc, true);
514 }
515
516 /*
517 void wxGenericColourDialog::OnOk(void)
518 {
519 Show(false);
520 }
521
522 void wxGenericColourDialog::OnCancel(void)
523 {
524 colourDialogCancelled = true;
525 Show(false);
526 }
527 */
528
529 void wxGenericColourDialog::OnAddCustom(wxCommandEvent& WXUNUSED(event))
530 {
531 wxClientDC dc(this);
532 if (m_whichKind != 2)
533 {
534 PaintHighlight(dc, false);
535 m_whichKind = 2;
536 m_colourSelection = 0;
537 PaintHighlight(dc, true);
538 }
539
540 m_customColours[m_colourSelection].Set(m_colourData.m_dataColour.Red(),
541 m_colourData.m_dataColour.Green(),
542 m_colourData.m_dataColour.Blue());
543
544 m_colourData.SetCustomColour(m_colourSelection, m_customColours[m_colourSelection]);
545
546 PaintCustomColours(dc);
547 }
548
549 #if wxUSE_SLIDER
550
551 void wxGenericColourDialog::OnRedSlider(wxCommandEvent& WXUNUSED(event))
552 {
553 if (!m_redSlider)
554 return;
555
556 wxClientDC dc(this);
557 m_colourData.m_dataColour.Set((unsigned char)m_redSlider->GetValue(), m_colourData.m_dataColour.Green(), m_colourData.m_dataColour.Blue());
558 PaintCustomColour(dc);
559 }
560
561 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent& WXUNUSED(event))
562 {
563 if (!m_greenSlider)
564 return;
565
566 wxClientDC dc(this);
567 m_colourData.m_dataColour.Set(m_colourData.m_dataColour.Red(), (unsigned char)m_greenSlider->GetValue(), m_colourData.m_dataColour.Blue());
568 PaintCustomColour(dc);
569 }
570
571 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent& WXUNUSED(event))
572 {
573 if (!m_blueSlider)
574 return;
575
576 wxClientDC dc(this);
577 m_colourData.m_dataColour.Set(m_colourData.m_dataColour.Red(), m_colourData.m_dataColour.Green(), (unsigned char)m_blueSlider->GetValue());
578 PaintCustomColour(dc);
579 }
580
581 #endif // wxUSE_SLIDER
582
583 #endif // wxUSE_COLOURDLG && !defined(__WXGTK20__)