1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/colrdlgg.cpp
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()
121 colourSelection
= -1;
124 wxGenericColourDialog::wxGenericColourDialog(wxWindow
*parent
,
128 colourSelection
= -1;
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
);
197 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
199 #if !defined(__WXMOTIF__) && !defined(__WXMAC__) && !defined(__WXPM__)
200 wxDialog::OnPaint(event
);
205 PaintBasicColours(dc
);
206 PaintCustomColours(dc
);
207 PaintCustomColour(dc
);
208 PaintHighlight(dc
, TRUE
);
211 void wxGenericColourDialog::CalculateMeasurements()
213 smallRectangleSize
.x
= 18;
214 smallRectangleSize
.y
= 14;
215 customRectangleSize
.x
= 40;
216 customRectangleSize
.y
= 40;
221 standardColoursRect
.x
= 10;
222 standardColoursRect
.y
= 15;
223 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
224 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
226 customColoursRect
.x
= standardColoursRect
.x
;
227 customColoursRect
.y
= standardColoursRect
.y
+ standardColoursRect
.height
+ 20;
228 customColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
229 customColoursRect
.height
= (2*smallRectangleSize
.y
) + (1*gridSpacing
);
231 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
232 singleCustomColourRect
.y
= 80;
233 singleCustomColourRect
.width
= customRectangleSize
.x
;
234 singleCustomColourRect
.height
= customRectangleSize
.y
;
237 customButtonX
= singleCustomColourRect
.x
;
238 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
241 void wxGenericColourDialog::CreateWidgets()
245 int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
246 #if defined(__WXMOTIF__)
247 int sliderSpacing
= 65;
248 int sliderHeight
= 160;
250 int sliderSpacing
= 45;
251 int sliderHeight
= 160;
254 redSlider
= new wxSlider(this, wxID_RED_SLIDER
, singleCustomColour
.Red(), 0, 255,
255 wxPoint(sliderX
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
256 greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, singleCustomColour
.Green(), 0, 255,
257 wxPoint(sliderX
+ sliderSpacing
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
258 blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, singleCustomColour
.Blue(), 0, 255,
259 wxPoint(sliderX
+ 2*sliderSpacing
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
261 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
263 // 1) space for explicitly layouted controls
264 topsizer
->Add( sliderX
+ 3*sliderSpacing
, sliderHeight
+25 );
268 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
272 wxSizer
*buttonsizer
= CreateButtonSizer( wxOK
|wxCANCEL
);
273 buttonsizer
->Add( new wxButton(this, wxID_ADD_CUSTOM
, _("Add to custom colours") ), 0, wxLEFT
|wxRIGHT
, 10 );
274 topsizer
->Add( buttonsizer
, 0, wxCENTRE
| wxALL
, 10 );
276 SetAutoLayout( TRUE
);
277 SetSizer( topsizer
);
279 topsizer
->SetSizeHints( this );
280 topsizer
->Fit( this );
287 void wxGenericColourDialog::InitializeColours(void)
291 for (i
= 0; i
< 48; i
++)
293 wxColour
*col
= wxTheColourDatabase
->FindColour(wxColourDialogNames
[i
]);
295 standardColours
[i
].Set(col
->Red(), col
->Green(), col
->Blue());
297 standardColours
[i
].Set(0, 0, 0);
300 for (i
= 0; i
< 16; i
++)
302 customColours
[i
] = colourData
.GetCustomColour(i
);
305 wxColour curr
= colourData
.GetColour();
308 bool initColourFound
= FALSE
;
310 for (i
= 0; i
< 48; i
++)
312 if ( standardColours
[i
] == curr
&& !initColourFound
)
316 initColourFound
= TRUE
;
320 if ( !initColourFound
)
322 for ( i
= 0; i
< 16; i
++ )
324 if ( customColours
[i
] == curr
)
328 initColourFound
= TRUE
;
333 singleCustomColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
339 singleCustomColour
.Set( 0, 0, 0 );
343 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
348 for (i
= 0; i
< 6; i
++)
351 for (j
= 0; j
< 8; j
++)
355 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
356 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
358 dc
.SetPen(*wxBLACK_PEN
);
359 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
362 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
368 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
373 for (i
= 0; i
< 2; i
++)
376 for (j
= 0; j
< 8; j
++)
380 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
381 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
383 dc
.SetPen(*wxBLACK_PEN
);
385 wxBrush
brush(customColours
[ptr
], wxSOLID
);
388 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
394 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
396 if ( colourSelection
< 0 )
401 // Number of pixels bigger than the standard rectangle size
402 // for drawing a highlight
409 int y
= (int)(colourSelection
/ 8);
410 int x
= (int)(colourSelection
- (y
*8));
412 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
413 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.y
) - deltaY
;
416 dc
.SetPen(*wxBLACK_PEN
);
418 dc
.SetPen(*wxLIGHT_GREY_PEN
);
420 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
421 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
425 // User-defined colours
426 int y
= (int)(colourSelection
/ 8);
427 int x
= (int)(colourSelection
- (y
*8));
429 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
430 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
433 dc
.SetPen(*wxBLACK_PEN
);
435 dc
.SetPen(*wxLIGHT_GREY_PEN
);
437 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
438 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
444 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
448 dc
.SetPen(*wxBLACK_PEN
);
450 wxBrush
*brush
= new wxBrush(singleCustomColour
, wxSOLID
);
453 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
454 customRectangleSize
.x
, customRectangleSize
.y
);
456 dc
.SetBrush(wxNullBrush
);
462 void wxGenericColourDialog::OnBasicColourClick(int which
)
466 PaintHighlight(dc
, FALSE
);
468 colourSelection
= which
;
469 colourData
.SetColour(standardColours
[colourSelection
]);
470 redSlider
->SetValue( standardColours
[colourSelection
].Red() );
471 greenSlider
->SetValue( standardColours
[colourSelection
].Green() );
472 blueSlider
->SetValue( standardColours
[colourSelection
].Blue() );
473 singleCustomColour
.Set(standardColours
[colourSelection
].Red(), standardColours
[colourSelection
].Green(), standardColours
[colourSelection
].Blue());
475 PaintCustomColour(dc
);
476 PaintHighlight(dc
, TRUE
);
479 void wxGenericColourDialog::OnCustomColourClick(int which
)
482 PaintHighlight(dc
, FALSE
);
484 colourSelection
= which
;
485 colourData
.SetColour(customColours
[colourSelection
]);
486 redSlider
->SetValue( customColours
[colourSelection
].Red() );
487 greenSlider
->SetValue( customColours
[colourSelection
].Green() );
488 blueSlider
->SetValue( customColours
[colourSelection
].Blue() );
489 singleCustomColour
.Set(customColours
[colourSelection
].Red(), customColours
[colourSelection
].Green(), customColours
[colourSelection
].Blue());
490 PaintCustomColour(dc
);
491 PaintHighlight(dc
, TRUE
);
495 void wxGenericColourDialog::OnOk(void)
500 void wxGenericColourDialog::OnCancel(void)
502 colourDialogCancelled = TRUE;
507 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
512 PaintHighlight(dc
, FALSE
);
515 PaintHighlight(dc
, TRUE
);
518 customColours
[colourSelection
].Set(singleCustomColour
.Red(), singleCustomColour
.Green(), singleCustomColour
.Blue());
519 colourData
.SetColour(customColours
[colourSelection
]);
520 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
522 PaintCustomColours(dc
);
525 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
531 singleCustomColour
.Set(redSlider
->GetValue(), singleCustomColour
.Green(), singleCustomColour
.Blue());
532 PaintCustomColour(dc
);
535 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
541 singleCustomColour
.Set(singleCustomColour
.Red(), greenSlider
->GetValue(), singleCustomColour
.Blue());
542 PaintCustomColour(dc
);
545 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
551 singleCustomColour
.Set(singleCustomColour
.Red(), singleCustomColour
.Green(), blueSlider
->GetValue());
552 PaintCustomColour(dc
);
555 #endif // wxUSE_COLOURDLG