]> git.saurik.com Git - wxWidgets.git/blame - src/generic/colrdlgg.cpp
refresh widget after changing its colour
[wxWidgets.git] / src / generic / colrdlgg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: generic/colrdlgg.cpp
c801d85f
KB
3// Purpose: Choice dialogs
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
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__
1e6feb95 20 #pragma hdrstop
c801d85f
KB
21#endif
22
853bbd9e 23#if wxUSE_COLOURDLG && (!defined(__WXGTK20__) || defined(__WXUNIVERSAL__))
1e6feb95 24
c801d85f 25#ifndef WX_PRECOMP
1e6feb95
VZ
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"
4130b487
RR
36#endif
37
38#if wxUSE_STATLINE
39 #include "wx/statline.h"
c801d85f
KB
40#endif
41
42#include "wx/generic/colrdlgg.h"
43
c801d85f
KB
44IMPLEMENT_DYNAMIC_CLASS(wxGenericColourDialog, wxDialog)
45
46BEGIN_EVENT_TABLE(wxGenericColourDialog, wxDialog)
c35414db
VZ
47 EVT_BUTTON(wxID_ADD_CUSTOM, wxGenericColourDialog::OnAddCustom)
48 EVT_SLIDER(wxID_RED_SLIDER, wxGenericColourDialog::OnRedSlider)
49 EVT_SLIDER(wxID_GREEN_SLIDER, wxGenericColourDialog::OnGreenSlider)
50 EVT_SLIDER(wxID_BLUE_SLIDER, wxGenericColourDialog::OnBlueSlider)
51 EVT_PAINT(wxGenericColourDialog::OnPaint)
52 EVT_MOUSE_EVENTS(wxGenericColourDialog::OnMouseEvent)
53 EVT_CLOSE(wxGenericColourDialog::OnCloseWindow)
c801d85f
KB
54END_EVENT_TABLE()
55
c801d85f
KB
56
57/*
58 * Generic wxColourDialog
59 */
60
61138de0
VZ
61// don't change the number of elements (48) in this array, the code below is
62// hardcoded to use it
63static const wxChar *wxColourDialogNames[] =
64{
65 wxT("ORANGE"),
66 wxT("GOLDENROD"),
67 wxT("WHEAT"),
68 wxT("SPRING GREEN"),
69 wxT("SKY BLUE"),
70 wxT("SLATE BLUE"),
71 wxT("MEDIUM VIOLET RED"),
72 wxT("PURPLE"),
73
74 wxT("RED"),
75 wxT("YELLOW"),
76 wxT("MEDIUM SPRING GREEN"),
77 wxT("PALE GREEN"),
78 wxT("CYAN"),
79 wxT("LIGHT STEEL BLUE"),
80 wxT("ORCHID"),
81 wxT("LIGHT MAGENTA"),
82
83 wxT("BROWN"),
84 wxT("YELLOW"),
85 wxT("GREEN"),
86 wxT("CADET BLUE"),
87 wxT("MEDIUM BLUE"),
88 wxT("MAGENTA"),
89 wxT("MAROON"),
90 wxT("ORANGE RED"),
91
92 wxT("FIREBRICK"),
93 wxT("CORAL"),
94 wxT("FOREST GREEN"),
95 wxT("AQUAMARINE"),
96 wxT("BLUE"),
97 wxT("NAVY"),
98 wxT("THISTLE"),
99 wxT("MEDIUM VIOLET RED"),
100
101 wxT("INDIAN RED"),
102 wxT("GOLD"),
103 wxT("MEDIUM SEA GREEN"),
104 wxT("MEDIUM BLUE"),
105 wxT("MIDNIGHT BLUE"),
106 wxT("GREY"),
107 wxT("PURPLE"),
108 wxT("KHAKI"),
109
110 wxT("BLACK"),
111 wxT("MEDIUM FOREST GREEN"),
112 wxT("KHAKI"),
113 wxT("DARK GREY"),
114 wxT("SEA GREEN"),
115 wxT("LIGHT GREY"),
116 wxT("MEDIUM SLATE BLUE"),
117 wxT("WHITE")
118};
c801d85f 119
4130b487 120wxGenericColourDialog::wxGenericColourDialog()
c801d85f
KB
121{
122 dialogParent = NULL;
123 whichKind = 1;
ea8a9699 124 colourSelection = -1;
c801d85f
KB
125}
126
f6bcfd97
BP
127wxGenericColourDialog::wxGenericColourDialog(wxWindow *parent,
128 wxColourData *data)
c801d85f
KB
129{
130 whichKind = 1;
ea8a9699 131 colourSelection = -1;
c801d85f
KB
132 Create(parent, data);
133}
134
4130b487 135wxGenericColourDialog::~wxGenericColourDialog()
c801d85f
KB
136{
137}
138
74e3313b 139void wxGenericColourDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
c801d85f 140{
e3065973 141 EndModal(wxID_CANCEL);
c801d85f
KB
142}
143
144bool wxGenericColourDialog::Create(wxWindow *parent, wxColourData *data)
145{
ca65c044 146 if ( !wxDialog::Create(parent, wxID_ANY, _("Choose colour"),
2a21ac15 147 wxPoint(0, 0), wxSize(900, 900)) )
ca65c044 148 return false;
c35414db 149
f6bcfd97 150 dialogParent = parent;
c801d85f 151
f6bcfd97
BP
152 if (data)
153 colourData = *data;
c35414db 154
f6bcfd97
BP
155 InitializeColours();
156 CalculateMeasurements();
157 CreateWidgets();
158
ca65c044 159 return true;
c801d85f
KB
160}
161
4130b487 162int wxGenericColourDialog::ShowModal()
c801d85f 163{
984b0d8b 164 return wxDialog::ShowModal();
c801d85f
KB
165}
166
167
168// Internal functions
169void wxGenericColourDialog::OnMouseEvent(wxMouseEvent& event)
170{
171 if (event.ButtonDown(1))
172 {
173 int x = (int)event.GetX();
174 int y = (int)event.GetY();
175
b1f9d7bf
DW
176#ifdef __WXPM__
177 // Handle OS/2's reverse coordinate system and account for the dialog title
178 int nClientHeight;
179
180 GetClientSize(NULL, &nClientHeight);
181 y = (nClientHeight - y) + 20;
182#endif
c801d85f
KB
183 if ((x >= standardColoursRect.x && x <= (standardColoursRect.x + standardColoursRect.width)) &&
184 (y >= standardColoursRect.y && y <= (standardColoursRect.y + standardColoursRect.height)))
185 {
186 int selX = (int)(x - standardColoursRect.x)/(smallRectangleSize.x + gridSpacing);
187 int selY = (int)(y - standardColoursRect.y)/(smallRectangleSize.y + gridSpacing);
188 int ptr = (int)(selX + selY*8);
189 OnBasicColourClick(ptr);
190 }
191 else if ((x >= customColoursRect.x && x <= (customColoursRect.x + customColoursRect.width)) &&
192 (y >= customColoursRect.y && y <= (customColoursRect.y + customColoursRect.height)))
193 {
194 int selX = (int)(x - customColoursRect.x)/(smallRectangleSize.x + gridSpacing);
195 int selY = (int)(y - customColoursRect.y)/(smallRectangleSize.y + gridSpacing);
196 int ptr = (int)(selX + selY*8);
197 OnCustomColourClick(ptr);
198 }
c10920de
VS
199 else
200 event.Skip();
c801d85f 201 }
c10920de
VS
202 else
203 event.Skip();
c801d85f
KB
204}
205
206void wxGenericColourDialog::OnPaint(wxPaintEvent& event)
207{
bec1ace6 208#if !defined(__WXMOTIF__) && !defined(__WXMAC__) && !defined(__WXPM__) && !defined(__WXCOCOA__)
c801d85f 209 wxDialog::OnPaint(event);
d75638f8 210#endif
c801d85f
KB
211
212 wxPaintDC dc(this);
213
214 PaintBasicColours(dc);
215 PaintCustomColours(dc);
216 PaintCustomColour(dc);
ca65c044 217 PaintHighlight(dc, true);
c801d85f
KB
218}
219
4130b487 220void wxGenericColourDialog::CalculateMeasurements()
c801d85f
KB
221{
222 smallRectangleSize.x = 18;
223 smallRectangleSize.y = 14;
224 customRectangleSize.x = 40;
225 customRectangleSize.y = 40;
226
227 gridSpacing = 6;
228 sectionSpacing = 15;
229
230 standardColoursRect.x = 10;
b1f9d7bf
DW
231#ifdef __WXPM__
232 standardColoursRect.y = 15 + 20; /* OS/2 needs to account for dialog titlebar */
233#else
c801d85f 234 standardColoursRect.y = 15;
b1f9d7bf 235#endif
c801d85f
KB
236 standardColoursRect.width = (8*smallRectangleSize.x) + (7*gridSpacing);
237 standardColoursRect.height = (6*smallRectangleSize.y) + (5*gridSpacing);
238
239 customColoursRect.x = standardColoursRect.x;
240 customColoursRect.y = standardColoursRect.y + standardColoursRect.height + 20;
241 customColoursRect.width = (8*smallRectangleSize.x) + (7*gridSpacing);
242 customColoursRect.height = (2*smallRectangleSize.y) + (1*gridSpacing);
243
244 singleCustomColourRect.x = customColoursRect.width + customColoursRect.x + sectionSpacing;
245 singleCustomColourRect.y = 80;
246 singleCustomColourRect.width = customRectangleSize.x;
247 singleCustomColourRect.height = customRectangleSize.y;
248
249 okButtonX = 10;
250 customButtonX = singleCustomColourRect.x ;
251 buttonY = customColoursRect.y + customColoursRect.height + 10;
252}
253
4130b487 254void wxGenericColourDialog::CreateWidgets()
c801d85f 255{
4130b487 256 wxBeginBusyCursor();
c801d85f 257
f79a46f8
WS
258 const int sliderX = singleCustomColourRect.x + singleCustomColourRect.width + sectionSpacing;
259 const int sliderHeight = 160;
4130b487 260
984b0d8b 261 redSlider = new wxSlider(this, wxID_RED_SLIDER, colourData.m_dataColour.Red(), 0, 255,
f79a46f8 262 wxDefaultPosition, wxSize(wxDefaultCoord, sliderHeight), wxVERTICAL|wxSL_LABELS);
984b0d8b 263 greenSlider = new wxSlider(this, wxID_GREEN_SLIDER, colourData.m_dataColour.Green(), 0, 255,
f79a46f8 264 wxDefaultPosition, wxSize(wxDefaultCoord, sliderHeight), wxVERTICAL|wxSL_LABELS);
984b0d8b 265 blueSlider = new wxSlider(this, wxID_BLUE_SLIDER, colourData.m_dataColour.Blue(), 0, 255,
f79a46f8
WS
266 wxDefaultPosition, wxSize(wxDefaultCoord, sliderHeight), wxVERTICAL|wxSL_LABELS);
267
268 wxBoxSizer *sliderSizer = new wxBoxSizer( wxHORIZONTAL );
269
270 // 1) space for sliders
271 sliderSizer->Add( sliderX, sliderHeight );
272 sliderSizer->Add( redSlider, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL | wxALL, 10 );
273 sliderSizer->Add( greenSlider, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL | wxALL, 10 );
274 sliderSizer->Add( blueSlider, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL | wxALL, 10 );
4130b487 275
f79a46f8 276 wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
4130b487 277
f79a46f8 278 topSizer->Add( sliderSizer, 0, wxCENTRE | wxALL, 10 );
6c34d0ed 279
4130b487
RR
280#if wxUSE_STATLINE
281 // 2) static line
f79a46f8 282 topSizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
c801d85f 283#endif
c35414db 284
4130b487
RR
285 // 3) buttons
286 wxSizer *buttonsizer = CreateButtonSizer( wxOK|wxCANCEL );
287 buttonsizer->Add( new wxButton(this, wxID_ADD_CUSTOM, _("Add to custom colours") ), 0, wxLEFT|wxRIGHT, 10 );
f79a46f8 288 topSizer->Add( buttonsizer, 0, wxCENTRE | wxALL, 10 );
c801d85f 289
ca65c044 290 SetAutoLayout( true );
f79a46f8 291 SetSizer( topSizer );
6c34d0ed 292
f79a46f8
WS
293 topSizer->SetSizeHints( this );
294 topSizer->Fit( this );
c801d85f 295
4130b487 296 Centre( wxBOTH );
c801d85f 297
4130b487 298 wxEndBusyCursor();
c801d85f
KB
299}
300
301void wxGenericColourDialog::InitializeColours(void)
302{
a4ba2eec 303 size_t i;
ea8a9699 304
61138de0 305 for (i = 0; i < WXSIZEOF(wxColourDialogNames); i++)
ea8a9699 306 {
564a150b
VZ
307 wxColour col = wxTheColourDatabase->Find(wxColourDialogNames[i]);
308 if (col.Ok())
309 standardColours[i].Set(col.Red(), col.Green(), col.Blue());
ea8a9699
VZ
310 else
311 standardColours[i].Set(0, 0, 0);
312 }
c801d85f 313
564a150b 314 for (i = 0; i < WXSIZEOF(customColours); i++)
ea8a9699 315 {
a2b99ff5
VS
316 wxColour c = colourData.GetCustomColour(i);
317 if (c.Ok())
318 customColours[i] = colourData.GetCustomColour(i);
319 else
320 customColours[i] = wxColour(255, 255, 255);
ea8a9699 321 }
c801d85f 322
ea8a9699
VZ
323 wxColour curr = colourData.GetColour();
324 if ( curr.Ok() )
325 {
ca65c044 326 bool initColourFound = false;
ea8a9699 327
61138de0 328 for (i = 0; i < WXSIZEOF(wxColourDialogNames); i++)
ea8a9699
VZ
329 {
330 if ( standardColours[i] == curr && !initColourFound )
331 {
332 whichKind = 1;
333 colourSelection = i;
ca65c044 334 initColourFound = true;
ea8a9699
VZ
335 break;
336 }
337 }
338 if ( !initColourFound )
339 {
564a150b 340 for ( i = 0; i < WXSIZEOF(customColours); i++ )
ea8a9699
VZ
341 {
342 if ( customColours[i] == curr )
343 {
344 whichKind = 2;
345 colourSelection = i;
ea8a9699
VZ
346 break;
347 }
348 }
349 }
984b0d8b 350 colourData.m_dataColour.Set( curr.Red(), curr.Green(), curr.Blue() );
ea8a9699
VZ
351 }
352 else
353 {
354 whichKind = 1;
355 colourSelection = 0;
984b0d8b 356 colourData.m_dataColour.Set( 0, 0, 0 );
ea8a9699 357 }
c801d85f
KB
358}
359
360void wxGenericColourDialog::PaintBasicColours(wxDC& dc)
361{
362 dc.BeginDrawing();
363
364 int i;
365 for (i = 0; i < 6; i++)
366 {
367 int j;
368 for (j = 0; j < 8; j++)
369 {
370 int ptr = i*8 + j;
c35414db 371
c801d85f
KB
372 int x = (j*(smallRectangleSize.x+gridSpacing) + standardColoursRect.x);
373 int y = (i*(smallRectangleSize.y+gridSpacing) + standardColoursRect.y);
374
375 dc.SetPen(*wxBLACK_PEN);
376 wxBrush brush(standardColours[ptr], wxSOLID);
377 dc.SetBrush(brush);
378
379 dc.DrawRectangle( x, y, smallRectangleSize.x, smallRectangleSize.y);
380 }
381 }
382 dc.EndDrawing();
383}
384
385void wxGenericColourDialog::PaintCustomColours(wxDC& dc)
386{
387 dc.BeginDrawing();
388
389 int i;
390 for (i = 0; i < 2; i++)
391 {
392 int j;
393 for (j = 0; j < 8; j++)
394 {
395 int ptr = i*8 + j;
c35414db 396
c801d85f
KB
397 int x = (j*(smallRectangleSize.x+gridSpacing)) + customColoursRect.x;
398 int y = (i*(smallRectangleSize.y+gridSpacing)) + customColoursRect.y;
399
400 dc.SetPen(*wxBLACK_PEN);
401
402 wxBrush brush(customColours[ptr], wxSOLID);
403 dc.SetBrush(brush);
404
405 dc.DrawRectangle( x, y, smallRectangleSize.x, smallRectangleSize.y);
406 }
407 }
408 dc.EndDrawing();
409}
410
411void wxGenericColourDialog::PaintHighlight(wxDC& dc, bool draw)
412{
ea8a9699
VZ
413 if ( colourSelection < 0 )
414 return;
415
c801d85f
KB
416 dc.BeginDrawing();
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 (whichKind == 1)
424 {
425 // Standard colours
426 int y = (int)(colourSelection / 8);
427 int x = (int)(colourSelection - (y*8));
428
429 x = (x*(smallRectangleSize.x + gridSpacing) + standardColoursRect.x) - deltaX;
430 y = (y*(smallRectangleSize.y + gridSpacing) + 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, (smallRectangleSize.x + (2*deltaX)), (smallRectangleSize.y + (2*deltaY)));
439 }
440 else
441 {
442 // User-defined colours
443 int y = (int)(colourSelection / 8);
444 int x = (int)(colourSelection - (y*8));
445
446 x = (x*(smallRectangleSize.x + gridSpacing) + customColoursRect.x) - deltaX;
447 y = (y*(smallRectangleSize.y + gridSpacing) + customColoursRect.y) - deltaY;
448
449 if (draw)
450 dc.SetPen(*wxBLACK_PEN);
451 else
452 dc.SetPen(*wxLIGHT_GREY_PEN);
c35414db 453
c801d85f
KB
454 dc.SetBrush(*wxTRANSPARENT_BRUSH);
455 dc.DrawRectangle( x, y, (smallRectangleSize.x + (2*deltaX)), (smallRectangleSize.y + (2*deltaY)));
456 }
c35414db 457
c801d85f
KB
458 dc.EndDrawing();
459}
460
461void wxGenericColourDialog::PaintCustomColour(wxDC& dc)
462{
463 dc.BeginDrawing();
c35414db 464
c801d85f
KB
465 dc.SetPen(*wxBLACK_PEN);
466
984b0d8b 467 wxBrush *brush = new wxBrush(colourData.m_dataColour, wxSOLID);
c801d85f
KB
468 dc.SetBrush(*brush);
469
470 dc.DrawRectangle( singleCustomColourRect.x, singleCustomColourRect.y,
471 customRectangleSize.x, customRectangleSize.y);
472
473 dc.SetBrush(wxNullBrush);
474 delete brush;
475
476 dc.EndDrawing();
477}
478
479void wxGenericColourDialog::OnBasicColourClick(int which)
480{
ca65c044 481 wxClientDC dc(this);
c801d85f 482
ca65c044
WS
483 PaintHighlight(dc, false);
484 whichKind = 1;
485 colourSelection = which;
984b0d8b 486
ca65c044 487 redSlider->SetValue( standardColours[colourSelection].Red() );
ea8a9699
VZ
488 greenSlider->SetValue( standardColours[colourSelection].Green() );
489 blueSlider->SetValue( standardColours[colourSelection].Blue() );
984b0d8b
RN
490
491 colourData.m_dataColour.Set(standardColours[colourSelection].Red(),
492 standardColours[colourSelection].Green(),
493 standardColours[colourSelection].Blue());
c801d85f 494
ca65c044
WS
495 PaintCustomColour(dc);
496 PaintHighlight(dc, true);
c801d85f
KB
497}
498
499void wxGenericColourDialog::OnCustomColourClick(int which)
500{
ca65c044
WS
501 wxClientDC dc(this);
502 PaintHighlight(dc, false);
503 whichKind = 2;
504 colourSelection = which;
984b0d8b 505
ca65c044 506 redSlider->SetValue( customColours[colourSelection].Red() );
ea8a9699
VZ
507 greenSlider->SetValue( customColours[colourSelection].Green() );
508 blueSlider->SetValue( customColours[colourSelection].Blue() );
984b0d8b
RN
509
510 colourData.m_dataColour.Set(customColours[colourSelection].Red(),
511 customColours[colourSelection].Green(),
512 customColours[colourSelection].Blue());
513
ca65c044
WS
514 PaintCustomColour(dc);
515 PaintHighlight(dc, true);
c801d85f
KB
516}
517
518/*
519void wxGenericColourDialog::OnOk(void)
520{
ca65c044 521 Show(false);
c801d85f
KB
522}
523
524void wxGenericColourDialog::OnCancel(void)
525{
ca65c044
WS
526 colourDialogCancelled = true;
527 Show(false);
c801d85f
KB
528}
529*/
530
531void wxGenericColourDialog::OnAddCustom(wxCommandEvent& WXUNUSED(event))
532{
533 wxClientDC dc(this);
534 if (whichKind != 2)
535 {
ca65c044 536 PaintHighlight(dc, false);
c801d85f
KB
537 whichKind = 2;
538 colourSelection = 0;
ca65c044 539 PaintHighlight(dc, true);
c801d85f
KB
540 }
541
984b0d8b
RN
542 customColours[colourSelection].Set(colourData.m_dataColour.Red(),
543 colourData.m_dataColour.Green(),
544 colourData.m_dataColour.Blue());
545
c801d85f 546 colourData.SetCustomColour(colourSelection, customColours[colourSelection]);
c35414db 547
c801d85f
KB
548 PaintCustomColours(dc);
549}
550
551void wxGenericColourDialog::OnRedSlider(wxCommandEvent& WXUNUSED(event))
552{
553 if (!redSlider)
554 return;
c35414db 555
c801d85f 556 wxClientDC dc(this);
984b0d8b 557 colourData.m_dataColour.Set((unsigned char)redSlider->GetValue(), colourData.m_dataColour.Green(), colourData.m_dataColour.Blue());
c801d85f
KB
558 PaintCustomColour(dc);
559}
560
561void wxGenericColourDialog::OnGreenSlider(wxCommandEvent& WXUNUSED(event))
562{
563 if (!greenSlider)
564 return;
565
566 wxClientDC dc(this);
984b0d8b 567 colourData.m_dataColour.Set(colourData.m_dataColour.Red(), (unsigned char)greenSlider->GetValue(), colourData.m_dataColour.Blue());
c801d85f
KB
568 PaintCustomColour(dc);
569}
570
571void wxGenericColourDialog::OnBlueSlider(wxCommandEvent& WXUNUSED(event))
572{
573 if (!blueSlider)
574 return;
575
576 wxClientDC dc(this);
984b0d8b 577 colourData.m_dataColour.Set(colourData.m_dataColour.Red(), colourData.m_dataColour.Green(), (unsigned char)blueSlider->GetValue());
c801d85f
KB
578 PaintCustomColour(dc);
579}
580
a2b99ff5 581#endif // wxUSE_COLOURDLG && !defined(__WXGTK20__)
c801d85f 582