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 #if !USE_SHARED_LIBRARY
45 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourDialog
, wxDialog
)
47 BEGIN_EVENT_TABLE(wxGenericColourDialog
, wxDialog
)
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
)
60 * Generic wxColourDialog
64 static wxString wxColourDialogNames
[NUM_COLS
]={_T("ORANGE"),
70 _T("MEDIUM VIOLET RED"),
75 _T("MEDIUM SPRING GREEN"),
78 _T("LIGHT STEEL BLUE"),
98 _T("MEDIUM VIOLET RED"),
102 _T("MEDIUM SEA GREEN"),
110 _T("MEDIUM FOREST GREEN"),
115 _T("MEDIUM SLATE BLUE"),
119 wxGenericColourDialog::wxGenericColourDialog()
126 wxGenericColourDialog::wxGenericColourDialog(wxWindow
*parent
, wxColourData
*data
):
127 wxDialog(parent
, -1, "Colour", wxPoint(0, 0), wxSize(900, 900), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
131 Create(parent
, data
);
134 wxGenericColourDialog::~wxGenericColourDialog()
138 void wxGenericColourDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
140 EndModal(wxID_CANCEL
);
143 bool wxGenericColourDialog::Create(wxWindow
*parent
, wxColourData
*data
)
145 dialogParent
= parent
;
151 CalculateMeasurements();
157 int wxGenericColourDialog::ShowModal()
159 return wxDialog::ShowModal();
163 // Internal functions
164 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent
& event
)
166 if (event
.ButtonDown(1))
168 int x
= (int)event
.GetX();
169 int y
= (int)event
.GetY();
171 if ((x
>= standardColoursRect
.x
&& x
<= (standardColoursRect
.x
+ standardColoursRect
.width
)) &&
172 (y
>= standardColoursRect
.y
&& y
<= (standardColoursRect
.y
+ standardColoursRect
.height
)))
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
);
179 else if ((x
>= customColoursRect
.x
&& x
<= (customColoursRect
.x
+ customColoursRect
.width
)) &&
180 (y
>= customColoursRect
.y
&& y
<= (customColoursRect
.y
+ customColoursRect
.height
)))
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
);
190 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
193 wxDialog::OnPaint(event
);
198 PaintBasicColours(dc
);
199 PaintCustomColours(dc
);
200 PaintCustomColour(dc
);
201 PaintHighlight(dc
, TRUE
);
204 void wxGenericColourDialog::CalculateMeasurements()
206 smallRectangleSize
.x
= 18;
207 smallRectangleSize
.y
= 14;
208 customRectangleSize
.x
= 40;
209 customRectangleSize
.y
= 40;
214 standardColoursRect
.x
= 10;
215 standardColoursRect
.y
= 15;
216 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
217 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
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
);
224 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
225 singleCustomColourRect
.y
= 80;
226 singleCustomColourRect
.width
= customRectangleSize
.x
;
227 singleCustomColourRect
.height
= customRectangleSize
.y
;
230 customButtonX
= singleCustomColourRect
.x
;
231 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
234 void wxGenericColourDialog::CreateWidgets()
238 int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
239 #if defined(__WXMOTIF__)
240 int sliderSpacing
= 65;
241 int sliderHeight
= 160;
243 int sliderSpacing
= 45;
244 int sliderHeight
= 160;
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
);
254 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
256 // 1) space for explicitly layouted controls
257 topsizer
->Add( sliderX
+ 3*sliderSpacing
, sliderHeight
+25 );
261 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
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 );
269 SetAutoLayout( TRUE
);
270 SetSizer( topsizer
);
272 topsizer
->SetSizeHints( this );
273 topsizer
->Fit( this );
280 void wxGenericColourDialog::InitializeColours(void)
283 for (i
= 0; i
< 48; i
++)
285 wxColour
*col
= wxTheColourDatabase
->FindColour(wxColourDialogNames
[i
]);
287 standardColours
[i
].Set(col
->Red(), col
->Green(), col
->Blue());
289 standardColours
[i
].Set(0, 0, 0);
292 for (i
= 0; i
< 16; i
++)
299 colourData
.GetCustomColour(i
);
301 singleCustomColour
.Set(0, 0, 0);
304 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
309 for (i
= 0; i
< 6; i
++)
312 for (j
= 0; j
< 8; j
++)
316 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
317 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
319 dc
.SetPen(*wxBLACK_PEN
);
320 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
323 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
329 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
334 for (i
= 0; i
< 2; i
++)
337 for (j
= 0; j
< 8; j
++)
341 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
342 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
344 dc
.SetPen(*wxBLACK_PEN
);
346 wxBrush
brush(customColours
[ptr
], wxSOLID
);
349 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
355 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
359 // Number of pixels bigger than the standard rectangle size
360 // for drawing a highlight
367 int y
= (int)(colourSelection
/ 8);
368 int x
= (int)(colourSelection
- (y
*8));
370 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
371 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.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
)));
383 // User-defined colours
384 int y
= (int)(colourSelection
/ 8);
385 int x
= (int)(colourSelection
- (y
*8));
387 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
388 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
391 dc
.SetPen(*wxBLACK_PEN
);
393 dc
.SetPen(*wxLIGHT_GREY_PEN
);
395 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
396 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
402 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
406 dc
.SetPen(*wxBLACK_PEN
);
408 wxBrush
*brush
= new wxBrush(singleCustomColour
, wxSOLID
);
411 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
412 customRectangleSize
.x
, customRectangleSize
.y
);
414 dc
.SetBrush(wxNullBrush
);
420 void wxGenericColourDialog::OnBasicColourClick(int which
)
424 PaintHighlight(dc
, FALSE
);
426 colourSelection
= which
;
427 colourData
.SetColour(standardColours
[colourSelection
]);
429 PaintHighlight(dc
, TRUE
);
432 void wxGenericColourDialog::OnCustomColourClick(int which
)
435 PaintHighlight(dc
, FALSE
);
437 colourSelection
= which
;
438 colourData
.SetColour(customColours
[colourSelection
]);
440 PaintHighlight(dc
, TRUE
);
444 void wxGenericColourDialog::OnOk(void)
449 void wxGenericColourDialog::OnCancel(void)
451 colourDialogCancelled = TRUE;
456 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
461 PaintHighlight(dc
, FALSE
);
464 PaintHighlight(dc
, TRUE
);
467 customColours
[colourSelection
].Set(singleCustomColour
.Red(), singleCustomColour
.Green(), singleCustomColour
.Blue());
468 colourData
.SetColour(customColours
[colourSelection
]);
469 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
471 PaintCustomColours(dc
);
474 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
480 singleCustomColour
.Set(redSlider
->GetValue(), singleCustomColour
.Green(), singleCustomColour
.Blue());
481 PaintCustomColour(dc
);
484 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
490 singleCustomColour
.Set(singleCustomColour
.Red(), greenSlider
->GetValue(), singleCustomColour
.Blue());
491 PaintCustomColour(dc
);
494 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
500 singleCustomColour
.Set(singleCustomColour
.Red(), singleCustomColour
.Green(), blueSlider
->GetValue());
501 PaintCustomColour(dc
);