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