1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Choice dialogs
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "colrdlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.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"
39 #include "wx/statline.h"
42 #include "wx/generic/colrdlgg.h"
44 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourDialog
, wxDialog
)
46 BEGIN_EVENT_TABLE(wxGenericColourDialog
, wxDialog
)
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
)
58 * Generic wxColourDialog
62 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
68 wxT("MEDIUM VIOLET RED"),
73 wxT("MEDIUM SPRING GREEN"),
76 wxT("LIGHT STEEL BLUE"),
96 wxT("MEDIUM VIOLET RED"),
100 wxT("MEDIUM SEA GREEN"),
102 wxT("MIDNIGHT BLUE"),
108 wxT("MEDIUM FOREST GREEN"),
113 wxT("MEDIUM SLATE BLUE"),
117 wxGenericColourDialog::wxGenericColourDialog()
124 wxGenericColourDialog::wxGenericColourDialog(wxWindow
*parent
, wxColourData
*data
):
125 wxDialog(parent
, -1, wxT("Colour"), wxPoint(0, 0), wxSize(900, 900), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
129 Create(parent
, data
);
132 wxGenericColourDialog::~wxGenericColourDialog()
136 void wxGenericColourDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
138 EndModal(wxID_CANCEL
);
141 bool wxGenericColourDialog::Create(wxWindow
*parent
, wxColourData
*data
)
143 dialogParent
= parent
;
149 CalculateMeasurements();
155 int wxGenericColourDialog::ShowModal()
157 return wxDialog::ShowModal();
161 // Internal functions
162 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent
& event
)
164 if (event
.ButtonDown(1))
166 int x
= (int)event
.GetX();
167 int y
= (int)event
.GetY();
169 if ((x
>= standardColoursRect
.x
&& x
<= (standardColoursRect
.x
+ standardColoursRect
.width
)) &&
170 (y
>= standardColoursRect
.y
&& y
<= (standardColoursRect
.y
+ standardColoursRect
.height
)))
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
);
177 else if ((x
>= customColoursRect
.x
&& x
<= (customColoursRect
.x
+ customColoursRect
.width
)) &&
178 (y
>= customColoursRect
.y
&& y
<= (customColoursRect
.y
+ customColoursRect
.height
)))
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
);
188 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
191 wxDialog::OnPaint(event
);
196 PaintBasicColours(dc
);
197 PaintCustomColours(dc
);
198 PaintCustomColour(dc
);
199 PaintHighlight(dc
, TRUE
);
202 void wxGenericColourDialog::CalculateMeasurements()
204 smallRectangleSize
.x
= 18;
205 smallRectangleSize
.y
= 14;
206 customRectangleSize
.x
= 40;
207 customRectangleSize
.y
= 40;
212 standardColoursRect
.x
= 10;
213 standardColoursRect
.y
= 15;
214 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
215 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
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
);
222 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
223 singleCustomColourRect
.y
= 80;
224 singleCustomColourRect
.width
= customRectangleSize
.x
;
225 singleCustomColourRect
.height
= customRectangleSize
.y
;
228 customButtonX
= singleCustomColourRect
.x
;
229 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
232 void wxGenericColourDialog::CreateWidgets()
236 int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
237 #if defined(__WXMOTIF__)
238 int sliderSpacing
= 65;
239 int sliderHeight
= 160;
241 int sliderSpacing
= 45;
242 int sliderHeight
= 160;
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
);
252 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
254 // 1) space for explicitly layouted controls
255 topsizer
->Add( sliderX
+ 3*sliderSpacing
, sliderHeight
+25 );
259 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
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 );
267 SetAutoLayout( TRUE
);
268 SetSizer( topsizer
);
270 topsizer
->SetSizeHints( this );
271 topsizer
->Fit( this );
278 void wxGenericColourDialog::InitializeColours(void)
281 for (i
= 0; i
< 48; i
++)
283 wxColour
*col
= wxTheColourDatabase
->FindColour(wxColourDialogNames
[i
]);
285 standardColours
[i
].Set(col
->Red(), col
->Green(), col
->Blue());
287 standardColours
[i
].Set(0, 0, 0);
290 for (i
= 0; i
< 16; i
++)
297 colourData
.GetCustomColour(i
);
299 singleCustomColour
.Set(0, 0, 0);
302 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
307 for (i
= 0; i
< 6; i
++)
310 for (j
= 0; j
< 8; j
++)
314 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
315 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
317 dc
.SetPen(*wxBLACK_PEN
);
318 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
321 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
327 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
332 for (i
= 0; i
< 2; i
++)
335 for (j
= 0; j
< 8; j
++)
339 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
340 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
342 dc
.SetPen(*wxBLACK_PEN
);
344 wxBrush
brush(customColours
[ptr
], wxSOLID
);
347 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
353 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
357 // Number of pixels bigger than the standard rectangle size
358 // for drawing a highlight
365 int y
= (int)(colourSelection
/ 8);
366 int x
= (int)(colourSelection
- (y
*8));
368 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
369 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.y
) - deltaY
;
372 dc
.SetPen(*wxBLACK_PEN
);
374 dc
.SetPen(*wxLIGHT_GREY_PEN
);
376 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
377 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
381 // User-defined colours
382 int y
= (int)(colourSelection
/ 8);
383 int x
= (int)(colourSelection
- (y
*8));
385 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
386 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
389 dc
.SetPen(*wxBLACK_PEN
);
391 dc
.SetPen(*wxLIGHT_GREY_PEN
);
393 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
394 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
400 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
404 dc
.SetPen(*wxBLACK_PEN
);
406 wxBrush
*brush
= new wxBrush(singleCustomColour
, wxSOLID
);
409 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
410 customRectangleSize
.x
, customRectangleSize
.y
);
412 dc
.SetBrush(wxNullBrush
);
418 void wxGenericColourDialog::OnBasicColourClick(int which
)
422 PaintHighlight(dc
, FALSE
);
424 colourSelection
= which
;
425 colourData
.SetColour(standardColours
[colourSelection
]);
427 PaintHighlight(dc
, TRUE
);
430 void wxGenericColourDialog::OnCustomColourClick(int which
)
433 PaintHighlight(dc
, FALSE
);
435 colourSelection
= which
;
436 colourData
.SetColour(customColours
[colourSelection
]);
438 PaintHighlight(dc
, TRUE
);
442 void wxGenericColourDialog::OnOk(void)
447 void wxGenericColourDialog::OnCancel(void)
449 colourDialogCancelled = TRUE;
454 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
459 PaintHighlight(dc
, FALSE
);
462 PaintHighlight(dc
, TRUE
);
465 customColours
[colourSelection
].Set(singleCustomColour
.Red(), singleCustomColour
.Green(), singleCustomColour
.Blue());
466 colourData
.SetColour(customColours
[colourSelection
]);
467 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
469 PaintCustomColours(dc
);
472 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
478 singleCustomColour
.Set(redSlider
->GetValue(), singleCustomColour
.Green(), singleCustomColour
.Blue());
479 PaintCustomColour(dc
);
482 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
488 singleCustomColour
.Set(singleCustomColour
.Red(), greenSlider
->GetValue(), singleCustomColour
.Blue());
489 PaintCustomColour(dc
);
492 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
498 singleCustomColour
.Set(singleCustomColour
.Red(), singleCustomColour
.Green(), blueSlider
->GetValue());
499 PaintCustomColour(dc
);