]> git.saurik.com Git - wxWidgets.git/blame - src/generic/colrdlgg.cpp
final (touch wood) tidy after things went pearshaped on line 666
[wxWidgets.git] / src / generic / colrdlgg.cpp
CommitLineData
c801d85f
KB
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
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__
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"
4130b487
RR
35#include "wx/sizer.h"
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;
121 colourSelection = 0;
122}
123
124wxGenericColourDialog::wxGenericColourDialog(wxWindow *parent, wxColourData *data):
58c837a4 125 wxDialog(parent, -1, wxT("Colour"), wxPoint(0, 0), wxSize(900, 900), wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
c801d85f
KB
126{
127 whichKind = 1;
128 colourSelection = 0;
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{
143 dialogParent = parent;
c35414db 144
c801d85f
KB
145 if (data)
146 colourData = *data;
147
148 InitializeColours();
149 CalculateMeasurements();
150 CreateWidgets();
c35414db 151
c801d85f
KB
152 return TRUE;
153}
154
4130b487 155int wxGenericColourDialog::ShowModal()
c801d85f
KB
156{
157 return wxDialog::ShowModal();
158}
159
160
161// Internal functions
162void wxGenericColourDialog::OnMouseEvent(wxMouseEvent& event)
163{
164 if (event.ButtonDown(1))
165 {
166 int x = (int)event.GetX();
167 int y = (int)event.GetY();
168
169 if ((x >= standardColoursRect.x && x <= (standardColoursRect.x + standardColoursRect.width)) &&
170 (y >= standardColoursRect.y && y <= (standardColoursRect.y + standardColoursRect.height)))
171 {
172 int selX = (int)(x - standardColoursRect.x)/(smallRectangleSize.x + gridSpacing);
173 int selY = (int)(y - standardColoursRect.y)/(smallRectangleSize.y + gridSpacing);
174 int ptr = (int)(selX + selY*8);
175 OnBasicColourClick(ptr);
176 }
177 else if ((x >= customColoursRect.x && x <= (customColoursRect.x + customColoursRect.width)) &&
178 (y >= customColoursRect.y && y <= (customColoursRect.y + customColoursRect.height)))
179 {
180 int selX = (int)(x - customColoursRect.x)/(smallRectangleSize.x + gridSpacing);
181 int selY = (int)(y - customColoursRect.y)/(smallRectangleSize.y + gridSpacing);
182 int ptr = (int)(selX + selY*8);
183 OnCustomColourClick(ptr);
184 }
185 }
186}
187
188void wxGenericColourDialog::OnPaint(wxPaintEvent& event)
189{
d75638f8 190#ifndef __WXMOTIF__
c801d85f 191 wxDialog::OnPaint(event);
d75638f8 192#endif
c801d85f
KB
193
194 wxPaintDC dc(this);
195
196 PaintBasicColours(dc);
197 PaintCustomColours(dc);
198 PaintCustomColour(dc);
199 PaintHighlight(dc, TRUE);
200}
201
4130b487 202void wxGenericColourDialog::CalculateMeasurements()
c801d85f
KB
203{
204 smallRectangleSize.x = 18;
205 smallRectangleSize.y = 14;
206 customRectangleSize.x = 40;
207 customRectangleSize.y = 40;
208
209 gridSpacing = 6;
210 sectionSpacing = 15;
211
212 standardColoursRect.x = 10;
213 standardColoursRect.y = 15;
214 standardColoursRect.width = (8*smallRectangleSize.x) + (7*gridSpacing);
215 standardColoursRect.height = (6*smallRectangleSize.y) + (5*gridSpacing);
216
217 customColoursRect.x = standardColoursRect.x;
218 customColoursRect.y = standardColoursRect.y + standardColoursRect.height + 20;
219 customColoursRect.width = (8*smallRectangleSize.x) + (7*gridSpacing);
220 customColoursRect.height = (2*smallRectangleSize.y) + (1*gridSpacing);
221
222 singleCustomColourRect.x = customColoursRect.width + customColoursRect.x + sectionSpacing;
223 singleCustomColourRect.y = 80;
224 singleCustomColourRect.width = customRectangleSize.x;
225 singleCustomColourRect.height = customRectangleSize.y;
226
227 okButtonX = 10;
228 customButtonX = singleCustomColourRect.x ;
229 buttonY = customColoursRect.y + customColoursRect.height + 10;
230}
231
4130b487 232void wxGenericColourDialog::CreateWidgets()
c801d85f 233{
4130b487 234 wxBeginBusyCursor();
c801d85f 235
4130b487
RR
236 int sliderX = singleCustomColourRect.x + singleCustomColourRect.width + sectionSpacing;
237#if defined(__WXMOTIF__)
238 int sliderSpacing = 65;
239 int sliderHeight = 160;
c801d85f 240#else
4130b487
RR
241 int sliderSpacing = 45;
242 int sliderHeight = 160;
243#endif
244
245 redSlider = new wxSlider(this, wxID_RED_SLIDER, 0, 0, 255,
246 wxPoint(sliderX, 10), wxSize(-1, sliderHeight), wxVERTICAL|wxSL_LABELS);
247 greenSlider = new wxSlider(this, wxID_GREEN_SLIDER, 0, 0, 255,
248 wxPoint(sliderX + sliderSpacing, 10), wxSize(-1, sliderHeight), wxVERTICAL|wxSL_LABELS);
249 blueSlider = new wxSlider(this, wxID_BLUE_SLIDER, 0, 0, 255,
250 wxPoint(sliderX + 2*sliderSpacing, 10), wxSize(-1, sliderHeight), wxVERTICAL|wxSL_LABELS);
251
252 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
253
254 // 1) space for explicitly layouted controls
255 topsizer->Add( sliderX + 3*sliderSpacing, sliderHeight+25 );
256
257#if wxUSE_STATLINE
258 // 2) static line
259 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
c801d85f 260#endif
c35414db 261
4130b487
RR
262 // 3) buttons
263 wxSizer *buttonsizer = CreateButtonSizer( wxOK|wxCANCEL );
264 buttonsizer->Add( new wxButton(this, wxID_ADD_CUSTOM, _("Add to custom colours") ), 0, wxLEFT|wxRIGHT, 10 );
265 topsizer->Add( buttonsizer, 0, wxCENTRE | wxALL, 10 );
c801d85f 266
4130b487
RR
267 SetAutoLayout( TRUE );
268 SetSizer( topsizer );
269
270 topsizer->SetSizeHints( this );
271 topsizer->Fit( this );
c801d85f 272
4130b487 273 Centre( wxBOTH );
c801d85f 274
4130b487 275 wxEndBusyCursor();
c801d85f
KB
276}
277
278void wxGenericColourDialog::InitializeColours(void)
279{
280 int i;
281 for (i = 0; i < 48; i++)
282 {
283 wxColour *col = wxTheColourDatabase->FindColour(wxColourDialogNames[i]);
284 if (col)
285 standardColours[i].Set(col->Red(), col->Green(), col->Blue());
286 else
287 standardColours[i].Set(0, 0, 0);
288 }
289
290 for (i = 0; i < 16; i++)
291 customColours[i] =
292/*
293#ifndef __VMS__
294 (wxColour&)
295#endif
296*/
297 colourData.GetCustomColour(i);
298
299 singleCustomColour.Set(0, 0, 0);
300}
301
302void wxGenericColourDialog::PaintBasicColours(wxDC& dc)
303{
304 dc.BeginDrawing();
305
306 int i;
307 for (i = 0; i < 6; i++)
308 {
309 int j;
310 for (j = 0; j < 8; j++)
311 {
312 int ptr = i*8 + j;
c35414db 313
c801d85f
KB
314 int x = (j*(smallRectangleSize.x+gridSpacing) + standardColoursRect.x);
315 int y = (i*(smallRectangleSize.y+gridSpacing) + standardColoursRect.y);
316
317 dc.SetPen(*wxBLACK_PEN);
318 wxBrush brush(standardColours[ptr], wxSOLID);
319 dc.SetBrush(brush);
320
321 dc.DrawRectangle( x, y, smallRectangleSize.x, smallRectangleSize.y);
322 }
323 }
324 dc.EndDrawing();
325}
326
327void wxGenericColourDialog::PaintCustomColours(wxDC& dc)
328{
329 dc.BeginDrawing();
330
331 int i;
332 for (i = 0; i < 2; i++)
333 {
334 int j;
335 for (j = 0; j < 8; j++)
336 {
337 int ptr = i*8 + j;
c35414db 338
c801d85f
KB
339 int x = (j*(smallRectangleSize.x+gridSpacing)) + customColoursRect.x;
340 int y = (i*(smallRectangleSize.y+gridSpacing)) + customColoursRect.y;
341
342 dc.SetPen(*wxBLACK_PEN);
343
344 wxBrush brush(customColours[ptr], wxSOLID);
345 dc.SetBrush(brush);
346
347 dc.DrawRectangle( x, y, smallRectangleSize.x, smallRectangleSize.y);
348 }
349 }
350 dc.EndDrawing();
351}
352
353void wxGenericColourDialog::PaintHighlight(wxDC& dc, bool draw)
354{
355 dc.BeginDrawing();
356
357 // Number of pixels bigger than the standard rectangle size
358 // for drawing a highlight
359 int deltaX = 2;
360 int deltaY = 2;
361
362 if (whichKind == 1)
363 {
364 // Standard colours
365 int y = (int)(colourSelection / 8);
366 int x = (int)(colourSelection - (y*8));
367
368 x = (x*(smallRectangleSize.x + gridSpacing) + standardColoursRect.x) - deltaX;
369 y = (y*(smallRectangleSize.y + gridSpacing) + standardColoursRect.y) - deltaY;
370
371 if (draw)
372 dc.SetPen(*wxBLACK_PEN);
373 else
374 dc.SetPen(*wxLIGHT_GREY_PEN);
375
376 dc.SetBrush(*wxTRANSPARENT_BRUSH);
377 dc.DrawRectangle( x, y, (smallRectangleSize.x + (2*deltaX)), (smallRectangleSize.y + (2*deltaY)));
378 }
379 else
380 {
381 // User-defined colours
382 int y = (int)(colourSelection / 8);
383 int x = (int)(colourSelection - (y*8));
384
385 x = (x*(smallRectangleSize.x + gridSpacing) + customColoursRect.x) - deltaX;
386 y = (y*(smallRectangleSize.y + gridSpacing) + customColoursRect.y) - deltaY;
387
388 if (draw)
389 dc.SetPen(*wxBLACK_PEN);
390 else
391 dc.SetPen(*wxLIGHT_GREY_PEN);
c35414db 392
c801d85f
KB
393 dc.SetBrush(*wxTRANSPARENT_BRUSH);
394 dc.DrawRectangle( x, y, (smallRectangleSize.x + (2*deltaX)), (smallRectangleSize.y + (2*deltaY)));
395 }
c35414db 396
c801d85f
KB
397 dc.EndDrawing();
398}
399
400void wxGenericColourDialog::PaintCustomColour(wxDC& dc)
401{
402 dc.BeginDrawing();
c35414db 403
c801d85f
KB
404 dc.SetPen(*wxBLACK_PEN);
405
406 wxBrush *brush = new wxBrush(singleCustomColour, wxSOLID);
407 dc.SetBrush(*brush);
408
409 dc.DrawRectangle( singleCustomColourRect.x, singleCustomColourRect.y,
410 customRectangleSize.x, customRectangleSize.y);
411
412 dc.SetBrush(wxNullBrush);
413 delete brush;
414
415 dc.EndDrawing();
416}
417
418void wxGenericColourDialog::OnBasicColourClick(int which)
419{
420 wxClientDC dc(this);
421
422 PaintHighlight(dc, FALSE);
423 whichKind = 1;
424 colourSelection = which;
425 colourData.SetColour(standardColours[colourSelection]);
426
427 PaintHighlight(dc, TRUE);
428}
429
430void wxGenericColourDialog::OnCustomColourClick(int which)
431{
432 wxClientDC dc(this);
433 PaintHighlight(dc, FALSE);
434 whichKind = 2;
435 colourSelection = which;
436 colourData.SetColour(customColours[colourSelection]);
437
438 PaintHighlight(dc, TRUE);
439}
440
441/*
442void wxGenericColourDialog::OnOk(void)
443{
444 Show(FALSE);
445}
446
447void wxGenericColourDialog::OnCancel(void)
448{
449 colourDialogCancelled = TRUE;
450 Show(FALSE);
451}
452*/
453
454void wxGenericColourDialog::OnAddCustom(wxCommandEvent& WXUNUSED(event))
455{
456 wxClientDC dc(this);
457 if (whichKind != 2)
458 {
459 PaintHighlight(dc, FALSE);
460 whichKind = 2;
461 colourSelection = 0;
462 PaintHighlight(dc, TRUE);
463 }
464
465 customColours[colourSelection].Set(singleCustomColour.Red(), singleCustomColour.Green(), singleCustomColour.Blue());
466 colourData.SetColour(customColours[colourSelection]);
467 colourData.SetCustomColour(colourSelection, customColours[colourSelection]);
c35414db 468
c801d85f
KB
469 PaintCustomColours(dc);
470}
471
472void wxGenericColourDialog::OnRedSlider(wxCommandEvent& WXUNUSED(event))
473{
474 if (!redSlider)
475 return;
c35414db 476
c801d85f
KB
477 wxClientDC dc(this);
478 singleCustomColour.Set(redSlider->GetValue(), singleCustomColour.Green(), singleCustomColour.Blue());
479 PaintCustomColour(dc);
480}
481
482void wxGenericColourDialog::OnGreenSlider(wxCommandEvent& WXUNUSED(event))
483{
484 if (!greenSlider)
485 return;
486
487 wxClientDC dc(this);
488 singleCustomColour.Set(singleCustomColour.Red(), greenSlider->GetValue(), singleCustomColour.Blue());
489 PaintCustomColour(dc);
490}
491
492void wxGenericColourDialog::OnBlueSlider(wxCommandEvent& WXUNUSED(event))
493{
494 if (!blueSlider)
495 return;
496
497 wxClientDC dc(this);
498 singleCustomColour.Set(singleCustomColour.Red(), singleCustomColour.Green(), blueSlider->GetValue());
499 PaintCustomColour(dc);
500}
501
502