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
,
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 if ( !wxDialog::Create(parent
, -1, wxT("Colour"),
144 wxPoint(0, 0), wxSize(900, 900),
145 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
) )
148 dialogParent
= parent
;
154 CalculateMeasurements();
160 int wxGenericColourDialog::ShowModal()
162 return wxDialog::ShowModal();
166 // Internal functions
167 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent
& event
)
169 if (event
.ButtonDown(1))
171 int x
= (int)event
.GetX();
172 int y
= (int)event
.GetY();
174 if ((x
>= standardColoursRect
.x
&& x
<= (standardColoursRect
.x
+ standardColoursRect
.width
)) &&
175 (y
>= standardColoursRect
.y
&& y
<= (standardColoursRect
.y
+ standardColoursRect
.height
)))
177 int selX
= (int)(x
- standardColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
178 int selY
= (int)(y
- standardColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
179 int ptr
= (int)(selX
+ selY
*8);
180 OnBasicColourClick(ptr
);
182 else if ((x
>= customColoursRect
.x
&& x
<= (customColoursRect
.x
+ customColoursRect
.width
)) &&
183 (y
>= customColoursRect
.y
&& y
<= (customColoursRect
.y
+ customColoursRect
.height
)))
185 int selX
= (int)(x
- customColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
186 int selY
= (int)(y
- customColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
187 int ptr
= (int)(selX
+ selY
*8);
188 OnCustomColourClick(ptr
);
193 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
195 #if !defined(__WXMOTIF__) && !defined(__WXMAC__)
196 wxDialog::OnPaint(event
);
201 PaintBasicColours(dc
);
202 PaintCustomColours(dc
);
203 PaintCustomColour(dc
);
204 PaintHighlight(dc
, TRUE
);
207 void wxGenericColourDialog::CalculateMeasurements()
209 smallRectangleSize
.x
= 18;
210 smallRectangleSize
.y
= 14;
211 customRectangleSize
.x
= 40;
212 customRectangleSize
.y
= 40;
217 standardColoursRect
.x
= 10;
218 standardColoursRect
.y
= 15;
219 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
220 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
222 customColoursRect
.x
= standardColoursRect
.x
;
223 customColoursRect
.y
= standardColoursRect
.y
+ standardColoursRect
.height
+ 20;
224 customColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
225 customColoursRect
.height
= (2*smallRectangleSize
.y
) + (1*gridSpacing
);
227 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
228 singleCustomColourRect
.y
= 80;
229 singleCustomColourRect
.width
= customRectangleSize
.x
;
230 singleCustomColourRect
.height
= customRectangleSize
.y
;
233 customButtonX
= singleCustomColourRect
.x
;
234 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
237 void wxGenericColourDialog::CreateWidgets()
241 int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
242 #if defined(__WXMOTIF__)
243 int sliderSpacing
= 65;
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 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
259 // 1) space for explicitly layouted controls
260 topsizer
->Add( sliderX
+ 3*sliderSpacing
, sliderHeight
+25 );
264 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
268 wxSizer
*buttonsizer
= CreateButtonSizer( wxOK
|wxCANCEL
);
269 buttonsizer
->Add( new wxButton(this, wxID_ADD_CUSTOM
, _("Add to custom colours") ), 0, wxLEFT
|wxRIGHT
, 10 );
270 topsizer
->Add( buttonsizer
, 0, wxCENTRE
| wxALL
, 10 );
272 SetAutoLayout( TRUE
);
273 SetSizer( topsizer
);
275 topsizer
->SetSizeHints( this );
276 topsizer
->Fit( this );
283 void wxGenericColourDialog::InitializeColours(void)
286 for (i
= 0; i
< 48; i
++)
288 wxColour
*col
= wxTheColourDatabase
->FindColour(wxColourDialogNames
[i
]);
290 standardColours
[i
].Set(col
->Red(), col
->Green(), col
->Blue());
292 standardColours
[i
].Set(0, 0, 0);
295 for (i
= 0; i
< 16; i
++)
302 colourData
.GetCustomColour(i
);
304 singleCustomColour
.Set(0, 0, 0);
307 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
312 for (i
= 0; i
< 6; i
++)
315 for (j
= 0; j
< 8; j
++)
319 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
320 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
322 dc
.SetPen(*wxBLACK_PEN
);
323 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
326 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
332 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
337 for (i
= 0; i
< 2; i
++)
340 for (j
= 0; j
< 8; j
++)
344 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
345 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
347 dc
.SetPen(*wxBLACK_PEN
);
349 wxBrush
brush(customColours
[ptr
], wxSOLID
);
352 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
358 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
362 // Number of pixels bigger than the standard rectangle size
363 // for drawing a highlight
370 int y
= (int)(colourSelection
/ 8);
371 int x
= (int)(colourSelection
- (y
*8));
373 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
374 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.y
) - deltaY
;
377 dc
.SetPen(*wxBLACK_PEN
);
379 dc
.SetPen(*wxLIGHT_GREY_PEN
);
381 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
382 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
386 // User-defined colours
387 int y
= (int)(colourSelection
/ 8);
388 int x
= (int)(colourSelection
- (y
*8));
390 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
391 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
394 dc
.SetPen(*wxBLACK_PEN
);
396 dc
.SetPen(*wxLIGHT_GREY_PEN
);
398 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
399 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
405 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
409 dc
.SetPen(*wxBLACK_PEN
);
411 wxBrush
*brush
= new wxBrush(singleCustomColour
, wxSOLID
);
414 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
415 customRectangleSize
.x
, customRectangleSize
.y
);
417 dc
.SetBrush(wxNullBrush
);
423 void wxGenericColourDialog::OnBasicColourClick(int which
)
427 PaintHighlight(dc
, FALSE
);
429 colourSelection
= which
;
430 colourData
.SetColour(standardColours
[colourSelection
]);
432 PaintHighlight(dc
, TRUE
);
435 void wxGenericColourDialog::OnCustomColourClick(int which
)
438 PaintHighlight(dc
, FALSE
);
440 colourSelection
= which
;
441 colourData
.SetColour(customColours
[colourSelection
]);
443 PaintHighlight(dc
, TRUE
);
447 void wxGenericColourDialog::OnOk(void)
452 void wxGenericColourDialog::OnCancel(void)
454 colourDialogCancelled = TRUE;
459 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
464 PaintHighlight(dc
, FALSE
);
467 PaintHighlight(dc
, TRUE
);
470 customColours
[colourSelection
].Set(singleCustomColour
.Red(), singleCustomColour
.Green(), singleCustomColour
.Blue());
471 colourData
.SetColour(customColours
[colourSelection
]);
472 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
474 PaintCustomColours(dc
);
477 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
483 singleCustomColour
.Set(redSlider
->GetValue(), singleCustomColour
.Green(), singleCustomColour
.Blue());
484 PaintCustomColour(dc
);
487 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
493 singleCustomColour
.Set(singleCustomColour
.Red(), greenSlider
->GetValue(), singleCustomColour
.Blue());
494 PaintCustomColour(dc
);
497 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
503 singleCustomColour
.Set(singleCustomColour
.Red(), singleCustomColour
.Green(), blueSlider
->GetValue());
504 PaintCustomColour(dc
);