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