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"
37 #include "wx/generic/colrdlgg.h"
39 #if !USE_SHARED_LIBRARY
40 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourDialog
, wxDialog
)
42 BEGIN_EVENT_TABLE(wxGenericColourDialog
, wxDialog
)
43 EVT_BUTTON(wxID_ADD_CUSTOM
, wxGenericColourDialog::OnAddCustom
)
44 EVT_SLIDER(wxID_RED_SLIDER
, wxGenericColourDialog::OnRedSlider
)
45 EVT_SLIDER(wxID_GREEN_SLIDER
, wxGenericColourDialog::OnGreenSlider
)
46 EVT_SLIDER(wxID_BLUE_SLIDER
, wxGenericColourDialog::OnBlueSlider
)
47 EVT_PAINT(wxGenericColourDialog::OnPaint
)
48 EVT_MOUSE_EVENTS(wxGenericColourDialog::OnMouseEvent
)
54 * Generic wxColourDialog
58 static wxString wxColourDialogNames
[NUM_COLS
]={"ORANGE",
69 "MEDIUM SPRING GREEN",
104 "MEDIUM FOREST GREEN",
113 wxGenericColourDialog::wxGenericColourDialog(void)
120 wxGenericColourDialog::wxGenericColourDialog(wxWindow
*parent
, wxColourData
*data
):
121 wxDialog(parent
, -1, "Colour", wxPoint(0, 0), wxSize(900, 900), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
125 Create(parent
, data
);
128 wxGenericColourDialog::~wxGenericColourDialog(void)
132 bool wxGenericColourDialog::OnClose(void)
138 bool wxGenericColourDialog::Create(wxWindow
*parent
, wxColourData
*data
)
140 dialogParent
= parent
;
146 CalculateMeasurements();
152 int wxGenericColourDialog::ShowModal(void)
154 return wxDialog::ShowModal();
158 // Internal functions
159 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent
& event
)
161 if (event
.ButtonDown(1))
163 int x
= (int)event
.GetX();
164 int y
= (int)event
.GetY();
166 if ((x
>= standardColoursRect
.x
&& x
<= (standardColoursRect
.x
+ standardColoursRect
.width
)) &&
167 (y
>= standardColoursRect
.y
&& y
<= (standardColoursRect
.y
+ standardColoursRect
.height
)))
169 int selX
= (int)(x
- standardColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
170 int selY
= (int)(y
- standardColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
171 int ptr
= (int)(selX
+ selY
*8);
172 OnBasicColourClick(ptr
);
174 else if ((x
>= customColoursRect
.x
&& x
<= (customColoursRect
.x
+ customColoursRect
.width
)) &&
175 (y
>= customColoursRect
.y
&& y
<= (customColoursRect
.y
+ customColoursRect
.height
)))
177 int selX
= (int)(x
- customColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
178 int selY
= (int)(y
- customColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
179 int ptr
= (int)(selX
+ selY
*8);
180 OnCustomColourClick(ptr
);
185 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
187 wxDialog::OnPaint(event
);
191 PaintBasicColours(dc
);
192 PaintCustomColours(dc
);
193 PaintCustomColour(dc
);
194 PaintHighlight(dc
, TRUE
);
197 void wxGenericColourDialog::CalculateMeasurements(void)
199 smallRectangleSize
.x
= 18;
200 smallRectangleSize
.y
= 14;
201 customRectangleSize
.x
= 40;
202 customRectangleSize
.y
= 40;
207 standardColoursRect
.x
= 10;
208 standardColoursRect
.y
= 15;
209 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
210 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
212 customColoursRect
.x
= standardColoursRect
.x
;
213 customColoursRect
.y
= standardColoursRect
.y
+ standardColoursRect
.height
+ 20;
214 customColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
215 customColoursRect
.height
= (2*smallRectangleSize
.y
) + (1*gridSpacing
);
217 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
218 singleCustomColourRect
.y
= 80;
219 singleCustomColourRect
.width
= customRectangleSize
.x
;
220 singleCustomColourRect
.height
= customRectangleSize
.y
;
223 customButtonX
= singleCustomColourRect
.x
;
224 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
227 void wxGenericColourDialog::CreateWidgets(void)
231 wxButton
*okButton
= new wxButton(this, wxID_OK
, "OK", wxPoint(okButtonX
, buttonY
));
233 okButton
->GetSize(&bw
, &bh
);
235 (void) new wxButton(this, wxID_CANCEL
, "Cancel", wxPoint(okButtonX
+ bw
+ 10, buttonY
));
236 (void) new wxButton(this, wxID_ADD_CUSTOM
, "Add to custom colours",
237 wxPoint(customButtonX
, buttonY
));
239 int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
241 int sliderSpacing
= 75;
242 int sliderHeight
= 160;
244 int sliderSpacing
= 45;
245 int sliderHeight
= 160;
248 redSlider
= new wxSlider(this, wxID_RED_SLIDER
, 0, 0, 255,
249 wxPoint(sliderX
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
250 greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, 0, 0, 255,
251 wxPoint(sliderX
+ sliderSpacing
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
252 blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, 0, 0, 255,
253 wxPoint(sliderX
+ 2*sliderSpacing
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
255 SetClientSize(sliderX
+ 3*sliderSpacing
, buttonY
+ 30);
256 okButton
->SetDefault();
263 void wxGenericColourDialog::InitializeColours(void)
266 for (i
= 0; i
< 48; i
++)
268 wxColour
*col
= wxTheColourDatabase
->FindColour(wxColourDialogNames
[i
]);
270 standardColours
[i
].Set(col
->Red(), col
->Green(), col
->Blue());
272 standardColours
[i
].Set(0, 0, 0);
275 for (i
= 0; i
< 16; i
++)
282 colourData
.GetCustomColour(i
);
284 singleCustomColour
.Set(0, 0, 0);
287 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
292 for (i
= 0; i
< 6; i
++)
295 for (j
= 0; j
< 8; j
++)
299 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
300 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
302 dc
.SetPen(*wxBLACK_PEN
);
303 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
306 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
312 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
317 for (i
= 0; i
< 2; i
++)
320 for (j
= 0; j
< 8; j
++)
324 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
325 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
327 dc
.SetPen(*wxBLACK_PEN
);
329 wxBrush
brush(customColours
[ptr
], wxSOLID
);
332 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
338 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
342 // Number of pixels bigger than the standard rectangle size
343 // for drawing a highlight
350 int y
= (int)(colourSelection
/ 8);
351 int x
= (int)(colourSelection
- (y
*8));
353 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
354 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.y
) - deltaY
;
357 dc
.SetPen(*wxBLACK_PEN
);
359 dc
.SetPen(*wxLIGHT_GREY_PEN
);
361 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
362 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
366 // User-defined colours
367 int y
= (int)(colourSelection
/ 8);
368 int x
= (int)(colourSelection
- (y
*8));
370 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
371 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
374 dc
.SetPen(*wxBLACK_PEN
);
376 dc
.SetPen(*wxLIGHT_GREY_PEN
);
378 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
379 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
385 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
389 dc
.SetPen(*wxBLACK_PEN
);
391 wxBrush
*brush
= new wxBrush(singleCustomColour
, wxSOLID
);
394 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
395 customRectangleSize
.x
, customRectangleSize
.y
);
397 dc
.SetBrush(wxNullBrush
);
403 void wxGenericColourDialog::OnBasicColourClick(int which
)
407 PaintHighlight(dc
, FALSE
);
409 colourSelection
= which
;
410 colourData
.SetColour(standardColours
[colourSelection
]);
412 PaintHighlight(dc
, TRUE
);
415 void wxGenericColourDialog::OnCustomColourClick(int which
)
418 PaintHighlight(dc
, FALSE
);
420 colourSelection
= which
;
421 colourData
.SetColour(customColours
[colourSelection
]);
423 PaintHighlight(dc
, TRUE
);
427 void wxGenericColourDialog::OnOk(void)
432 void wxGenericColourDialog::OnCancel(void)
434 colourDialogCancelled = TRUE;
439 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
444 PaintHighlight(dc
, FALSE
);
447 PaintHighlight(dc
, TRUE
);
450 customColours
[colourSelection
].Set(singleCustomColour
.Red(), singleCustomColour
.Green(), singleCustomColour
.Blue());
451 colourData
.SetColour(customColours
[colourSelection
]);
452 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
454 PaintCustomColours(dc
);
457 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
463 singleCustomColour
.Set(redSlider
->GetValue(), singleCustomColour
.Green(), singleCustomColour
.Blue());
464 PaintCustomColour(dc
);
467 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
473 singleCustomColour
.Set(singleCustomColour
.Red(), greenSlider
->GetValue(), singleCustomColour
.Blue());
474 PaintCustomColour(dc
);
477 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
483 singleCustomColour
.Set(singleCustomColour
.Red(), singleCustomColour
.Green(), blueSlider
->GetValue());
484 PaintCustomColour(dc
);