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