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
)
49 EVT_CLOSE(wxGenericColourDialog::OnCloseWindow
)
55 * Generic wxColourDialog
59 static wxString wxColourDialogNames
[NUM_COLS
]={_T("ORANGE"),
65 _T("MEDIUM VIOLET RED"),
70 _T("MEDIUM SPRING GREEN"),
73 _T("LIGHT STEEL BLUE"),
93 _T("MEDIUM VIOLET RED"),
97 _T("MEDIUM SEA GREEN"),
105 _T("MEDIUM FOREST GREEN"),
110 _T("MEDIUM SLATE BLUE"),
114 wxGenericColourDialog::wxGenericColourDialog(void)
121 wxGenericColourDialog::wxGenericColourDialog(wxWindow
*parent
, wxColourData
*data
):
122 wxDialog(parent
, -1, "Colour", wxPoint(0, 0), wxSize(900, 900), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
126 Create(parent
, data
);
129 wxGenericColourDialog::~wxGenericColourDialog(void)
133 void wxGenericColourDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
135 EndModal(wxID_CANCEL
);
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
)
188 wxDialog::OnPaint(event
);
193 PaintBasicColours(dc
);
194 PaintCustomColours(dc
);
195 PaintCustomColour(dc
);
196 PaintHighlight(dc
, TRUE
);
199 void wxGenericColourDialog::CalculateMeasurements(void)
201 smallRectangleSize
.x
= 18;
202 smallRectangleSize
.y
= 14;
203 customRectangleSize
.x
= 40;
204 customRectangleSize
.y
= 40;
209 standardColoursRect
.x
= 10;
210 standardColoursRect
.y
= 15;
211 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
212 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
214 customColoursRect
.x
= standardColoursRect
.x
;
215 customColoursRect
.y
= standardColoursRect
.y
+ standardColoursRect
.height
+ 20;
216 customColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
217 customColoursRect
.height
= (2*smallRectangleSize
.y
) + (1*gridSpacing
);
219 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
220 singleCustomColourRect
.y
= 80;
221 singleCustomColourRect
.width
= customRectangleSize
.x
;
222 singleCustomColourRect
.height
= customRectangleSize
.y
;
225 customButtonX
= singleCustomColourRect
.x
;
226 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
229 void wxGenericColourDialog::CreateWidgets(void)
233 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(okButtonX
, buttonY
), wxSize(75,-1) );
235 okButton
->GetSize(&bw
, &bh
);
237 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(okButtonX
+ bw
+ 20, buttonY
), wxSize(75,-1));
238 (void) new wxButton(this, wxID_ADD_CUSTOM
, _("Add to custom colours"),
239 wxPoint(customButtonX
, buttonY
));
241 int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
243 int sliderSpacing
= 75;
244 int sliderHeight
= 160;
246 int sliderSpacing
= 45;
247 int sliderHeight
= 160;
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
);
257 SetClientSize(sliderX
+ 3*sliderSpacing
, buttonY
+ 40);
258 okButton
->SetDefault();
265 void wxGenericColourDialog::InitializeColours(void)
268 for (i
= 0; i
< 48; i
++)
270 wxColour
*col
= wxTheColourDatabase
->FindColour(wxColourDialogNames
[i
]);
272 standardColours
[i
].Set(col
->Red(), col
->Green(), col
->Blue());
274 standardColours
[i
].Set(0, 0, 0);
277 for (i
= 0; i
< 16; i
++)
284 colourData
.GetCustomColour(i
);
286 singleCustomColour
.Set(0, 0, 0);
289 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
294 for (i
= 0; i
< 6; i
++)
297 for (j
= 0; j
< 8; j
++)
301 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
302 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
304 dc
.SetPen(*wxBLACK_PEN
);
305 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
308 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
314 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
319 for (i
= 0; i
< 2; i
++)
322 for (j
= 0; j
< 8; j
++)
326 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
327 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
329 dc
.SetPen(*wxBLACK_PEN
);
331 wxBrush
brush(customColours
[ptr
], wxSOLID
);
334 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
340 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
344 // Number of pixels bigger than the standard rectangle size
345 // for drawing a highlight
352 int y
= (int)(colourSelection
/ 8);
353 int x
= (int)(colourSelection
- (y
*8));
355 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
356 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.y
) - deltaY
;
359 dc
.SetPen(*wxBLACK_PEN
);
361 dc
.SetPen(*wxLIGHT_GREY_PEN
);
363 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
364 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
368 // User-defined colours
369 int y
= (int)(colourSelection
/ 8);
370 int x
= (int)(colourSelection
- (y
*8));
372 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
373 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
376 dc
.SetPen(*wxBLACK_PEN
);
378 dc
.SetPen(*wxLIGHT_GREY_PEN
);
380 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
381 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
387 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
391 dc
.SetPen(*wxBLACK_PEN
);
393 wxBrush
*brush
= new wxBrush(singleCustomColour
, wxSOLID
);
396 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
397 customRectangleSize
.x
, customRectangleSize
.y
);
399 dc
.SetBrush(wxNullBrush
);
405 void wxGenericColourDialog::OnBasicColourClick(int which
)
409 PaintHighlight(dc
, FALSE
);
411 colourSelection
= which
;
412 colourData
.SetColour(standardColours
[colourSelection
]);
414 PaintHighlight(dc
, TRUE
);
417 void wxGenericColourDialog::OnCustomColourClick(int which
)
420 PaintHighlight(dc
, FALSE
);
422 colourSelection
= which
;
423 colourData
.SetColour(customColours
[colourSelection
]);
425 PaintHighlight(dc
, TRUE
);
429 void wxGenericColourDialog::OnOk(void)
434 void wxGenericColourDialog::OnCancel(void)
436 colourDialogCancelled = TRUE;
441 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
446 PaintHighlight(dc
, FALSE
);
449 PaintHighlight(dc
, TRUE
);
452 customColours
[colourSelection
].Set(singleCustomColour
.Red(), singleCustomColour
.Green(), singleCustomColour
.Blue());
453 colourData
.SetColour(customColours
[colourSelection
]);
454 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
456 PaintCustomColours(dc
);
459 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
465 singleCustomColour
.Set(redSlider
->GetValue(), singleCustomColour
.Green(), singleCustomColour
.Blue());
466 PaintCustomColour(dc
);
469 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
475 singleCustomColour
.Set(singleCustomColour
.Red(), greenSlider
->GetValue(), singleCustomColour
.Blue());
476 PaintCustomColour(dc
);
479 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
485 singleCustomColour
.Set(singleCustomColour
.Red(), singleCustomColour
.Green(), blueSlider
->GetValue());
486 PaintCustomColour(dc
);