1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/colrdlgg.cpp
3 // Purpose: Choice dialogs
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_COLOURDLG && (!defined(__WXGTK20__) || defined(__WXUNIVERSAL__))
24 #include "wx/dialog.h"
25 #include "wx/listbox.h"
26 #include "wx/button.h"
27 #include "wx/stattext.h"
28 #include "wx/layout.h"
29 #include "wx/dcclient.h"
31 #include "wx/slider.h"
35 #include "wx/statline.h"
38 #include "wx/generic/colrdlgg.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourDialog
, wxDialog
)
42 BEGIN_EVENT_TABLE(wxGenericColourDialog
, wxDialog
)
43 EVT_BUTTON(wxID_ADD_CUSTOM
, wxGenericColourDialog
::OnAddCustom
)
45 EVT_SLIDER(wxID_RED_SLIDER
, wxGenericColourDialog
::OnRedSlider
)
46 EVT_SLIDER(wxID_GREEN_SLIDER
, wxGenericColourDialog
::OnGreenSlider
)
47 EVT_SLIDER(wxID_BLUE_SLIDER
, wxGenericColourDialog
::OnBlueSlider
)
49 EVT_PAINT(wxGenericColourDialog
::OnPaint
)
50 EVT_MOUSE_EVENTS(wxGenericColourDialog
::OnMouseEvent
)
51 EVT_CLOSE(wxGenericColourDialog
::OnCloseWindow
)
56 * Generic wxColourDialog
59 // don't change the number of elements (48) in this array, the code below is
60 // hardcoded to use it
61 static const wxChar
*wxColourDialogNames
[] =
69 wxT("MEDIUM VIOLET RED"),
74 wxT("MEDIUM SPRING GREEN"),
77 wxT("LIGHT STEEL BLUE"),
97 wxT("MEDIUM VIOLET RED"),
101 wxT("MEDIUM SEA GREEN"),
103 wxT("MIDNIGHT BLUE"),
109 wxT("MEDIUM FOREST GREEN"),
114 wxT("MEDIUM SLATE BLUE"),
118 wxGenericColourDialog
::wxGenericColourDialog()
121 m_colourSelection
= -1;
124 wxGenericColourDialog
::wxGenericColourDialog(wxWindow
*parent
,
128 m_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
, wxID_ANY
, _("Choose colour"),
144 wxPoint(0,0), wxSize(900, 900)) )
148 m_colourData
= *data
;
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();
172 // Handle OS/2's reverse coordinate system and account for the dialog title
175 GetClientSize(NULL
, &nClientHeight
);
176 y
= (nClientHeight
- y
) + 20;
178 if ((x
>= m_standardColoursRect
.x
&& x
<= (m_standardColoursRect
.x
+ m_standardColoursRect
.width
)) &&
179 (y
>= m_standardColoursRect
.y
&& y
<= (m_standardColoursRect
.y
+ m_standardColoursRect
.height
)))
181 int selX
= (int)(x
- m_standardColoursRect
.x
)/(m_smallRectangleSize
.x
+ m_gridSpacing
);
182 int selY
= (int)(y
- m_standardColoursRect
.y
)/(m_smallRectangleSize
.y
+ m_gridSpacing
);
183 int ptr
= (int)(selX
+ selY
*8);
184 OnBasicColourClick(ptr
);
186 else if ((x
>= m_customColoursRect
.x
&& x
<= (m_customColoursRect
.x
+ m_customColoursRect
.width
)) &&
187 (y
>= m_customColoursRect
.y
&& y
<= (m_customColoursRect
.y
+ m_customColoursRect
.height
)))
189 int selX
= (int)(x
- m_customColoursRect
.x
)/(m_smallRectangleSize
.x
+ m_gridSpacing
);
190 int selY
= (int)(y
- m_customColoursRect
.y
)/(m_smallRectangleSize
.y
+ m_gridSpacing
);
191 int ptr
= (int)(selX
+ selY
*8);
192 OnCustomColourClick(ptr
);
201 void wxGenericColourDialog
::OnPaint(wxPaintEvent
& event
)
203 #if !defined(__WXMOTIF__) && !defined(__WXPM__) && !defined(__WXCOCOA__)
204 wxDialog
::OnPaint(event
);
211 PaintBasicColours(dc
);
212 PaintCustomColours(dc
);
213 PaintCustomColour(dc
);
214 PaintHighlight(dc
, true);
217 void wxGenericColourDialog
::CalculateMeasurements()
219 m_smallRectangleSize
.x
= 18;
220 m_smallRectangleSize
.y
= 14;
221 m_customRectangleSize
.x
= 40;
222 m_customRectangleSize
.y
= 40;
225 m_sectionSpacing
= 15;
227 m_standardColoursRect
.x
= 10;
229 m_standardColoursRect
.y
= 15 + 20; /* OS/2 needs to account for dialog titlebar */
231 m_standardColoursRect
.y
= 15;
233 m_standardColoursRect
.width
= (8*m_smallRectangleSize
.x
) + (7*m_gridSpacing
);
234 m_standardColoursRect
.height
= (6*m_smallRectangleSize
.y
) + (5*m_gridSpacing
);
236 m_customColoursRect
.x
= m_standardColoursRect
.x
;
237 m_customColoursRect
.y
= m_standardColoursRect
.y
+ m_standardColoursRect
.height
+ 20;
238 m_customColoursRect
.width
= (8*m_smallRectangleSize
.x
) + (7*m_gridSpacing
);
239 m_customColoursRect
.height
= (2*m_smallRectangleSize
.y
) + (1*m_gridSpacing
);
241 m_singleCustomColourRect
.x
= m_customColoursRect
.width
+ m_customColoursRect
.x
+ m_sectionSpacing
;
242 m_singleCustomColourRect
.y
= 80;
243 m_singleCustomColourRect
.width
= m_customRectangleSize
.x
;
244 m_singleCustomColourRect
.height
= m_customRectangleSize
.y
;
247 m_customButtonX
= m_singleCustomColourRect
.x
;
248 m_buttonY
= m_customColoursRect
.y
+ m_customColoursRect
.height
+ 10;
251 void wxGenericColourDialog
::CreateWidgets()
255 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
257 const int sliderHeight
= 160;
261 const int sliderX
= m_singleCustomColourRect
.x
+ m_singleCustomColourRect
.width
+ m_sectionSpacing
;
263 m_redSlider
= new wxSlider(this, wxID_RED_SLIDER
, m_colourData
.m_dataColour
.Red(), 0, 255,
264 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
265 m_greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, m_colourData
.m_dataColour
.Green(), 0, 255,
266 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
267 m_blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, m_colourData
.m_dataColour
.Blue(), 0, 255,
268 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
270 wxBoxSizer
*sliderSizer
= new wxBoxSizer( wxHORIZONTAL
);
272 sliderSizer
->Add(sliderX
, sliderHeight
);
274 wxSizerFlags flagsRight
;
275 flagsRight
.Align(wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
).DoubleBorder();
277 sliderSizer
->Add(m_redSlider
, flagsRight
);
278 sliderSizer
->Add(m_greenSlider
,flagsRight
);
279 sliderSizer
->Add(m_blueSlider
,flagsRight
);
281 topSizer
->Add(sliderSizer
, wxSizerFlags().Centre().DoubleBorder());
283 topSizer
->Add(1, sliderHeight
, wxSizerFlags(1).Centre().TripleBorder());
284 #endif // wxUSE_SLIDER
286 // then the custom button
287 topSizer
->Add(new wxButton(this, wxID_ADD_CUSTOM
,
288 _("Add to custom colours") ),
289 wxSizerFlags().DoubleHorzBorder());
291 // then the standard buttons
292 wxSizer
*buttonsizer
= CreateSeparatedButtonSizer(wxOK
| wxCANCEL
);
295 topSizer
->Add(buttonsizer
, wxSizerFlags().Expand().DoubleBorder());
298 SetAutoLayout( true );
299 SetSizer( topSizer
);
301 topSizer
->SetSizeHints( this );
302 topSizer
->Fit( this );
309 void wxGenericColourDialog
::InitializeColours(void)
313 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
315 wxColour col
= wxTheColourDatabase
->Find(wxColourDialogNames
[i
]);
317 m_standardColours
[i
].Set(col
.Red(), col
.Green(), col
.Blue());
319 m_standardColours
[i
].Set(0, 0, 0);
322 for (i
= 0; i
< WXSIZEOF(m_customColours
); i
++)
324 wxColour c
= m_colourData
.GetCustomColour(i
);
326 m_customColours
[i
] = m_colourData
.GetCustomColour(i
);
328 m_customColours
[i
] = wxColour(255, 255, 255);
331 wxColour curr
= m_colourData
.GetColour();
334 bool m_initColourFound
= false;
336 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
338 if ( m_standardColours
[i
] == curr
&& !m_initColourFound
)
341 m_colourSelection
= i
;
342 m_initColourFound
= true;
346 if ( !m_initColourFound
)
348 for ( i
= 0; i
< WXSIZEOF(m_customColours
); i
++ )
350 if ( m_customColours
[i
] == curr
)
353 m_colourSelection
= i
;
358 m_colourData
.m_dataColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
363 m_colourSelection
= 0;
364 m_colourData
.m_dataColour
.Set( 0, 0, 0 );
368 void wxGenericColourDialog
::PaintBasicColours(wxDC
& dc
)
371 for (i
= 0; i
< 6; i
++)
374 for (j
= 0; j
< 8; j
++)
378 int x
= (j
*(m_smallRectangleSize
.x
+m_gridSpacing
) + m_standardColoursRect
.x
);
379 int y
= (i
*(m_smallRectangleSize
.y
+m_gridSpacing
) + m_standardColoursRect
.y
);
381 dc
.SetPen(*wxBLACK_PEN
);
382 wxBrush
brush(m_standardColours
[ptr
], wxSOLID
);
385 dc
.DrawRectangle( x
, y
, m_smallRectangleSize
.x
, m_smallRectangleSize
.y
);
390 void wxGenericColourDialog
::PaintCustomColours(wxDC
& dc
)
393 for (i
= 0; i
< 2; i
++)
396 for (j
= 0; j
< 8; j
++)
400 int x
= (j
*(m_smallRectangleSize
.x
+m_gridSpacing
)) + m_customColoursRect
.x
;
401 int y
= (i
*(m_smallRectangleSize
.y
+m_gridSpacing
)) + m_customColoursRect
.y
;
403 dc
.SetPen(*wxBLACK_PEN
);
405 wxBrush
brush(m_customColours
[ptr
], wxSOLID
);
408 dc
.DrawRectangle( x
, y
, m_smallRectangleSize
.x
, m_smallRectangleSize
.y
);
413 void wxGenericColourDialog
::PaintHighlight(wxDC
& dc
, bool draw
)
415 if ( m_colourSelection
< 0 )
418 // Number of pixels bigger than the standard rectangle size
419 // for drawing a highlight
423 if (m_whichKind
== 1)
426 int y
= (int)(m_colourSelection
/ 8);
427 int x
= (int)(m_colourSelection
- (y
*8));
429 x
= (x
*(m_smallRectangleSize
.x
+ m_gridSpacing
) + m_standardColoursRect
.x
) - deltaX
;
430 y
= (y
*(m_smallRectangleSize
.y
+ m_gridSpacing
) + m_standardColoursRect
.y
) - deltaY
;
433 dc
.SetPen(*wxBLACK_PEN
);
435 dc
.SetPen(*wxLIGHT_GREY_PEN
);
437 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
438 dc
.DrawRectangle( x
, y
, (m_smallRectangleSize
.x
+ (2*deltaX
)), (m_smallRectangleSize
.y
+ (2*deltaY
)));
442 // User-defined colours
443 int y
= (int)(m_colourSelection
/ 8);
444 int x
= (int)(m_colourSelection
- (y
*8));
446 x
= (x
*(m_smallRectangleSize
.x
+ m_gridSpacing
) + m_customColoursRect
.x
) - deltaX
;
447 y
= (y
*(m_smallRectangleSize
.y
+ m_gridSpacing
) + m_customColoursRect
.y
) - deltaY
;
450 dc
.SetPen(*wxBLACK_PEN
);
452 dc
.SetPen(*wxLIGHT_GREY_PEN
);
454 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
455 dc
.DrawRectangle( x
, y
, (m_smallRectangleSize
.x
+ (2*deltaX
)), (m_smallRectangleSize
.y
+ (2*deltaY
)));
459 void wxGenericColourDialog
::PaintCustomColour(wxDC
& dc
)
461 dc
.SetPen(*wxBLACK_PEN
);
463 wxBrush
*brush
= new wxBrush(m_colourData
.m_dataColour
, wxSOLID
);
466 dc
.DrawRectangle( m_singleCustomColourRect
.x
, m_singleCustomColourRect
.y
,
467 m_customRectangleSize
.x
, m_customRectangleSize
.y
);
469 dc
.SetBrush(wxNullBrush
);
473 void wxGenericColourDialog
::OnBasicColourClick(int which
)
477 PaintHighlight(dc
, false);
479 m_colourSelection
= which
;
482 m_redSlider
->SetValue( m_standardColours
[m_colourSelection
].Red() );
483 m_greenSlider
->SetValue( m_standardColours
[m_colourSelection
].Green() );
484 m_blueSlider
->SetValue( m_standardColours
[m_colourSelection
].Blue() );
485 #endif // wxUSE_SLIDER
487 m_colourData
.m_dataColour
.Set(m_standardColours
[m_colourSelection
].Red(),
488 m_standardColours
[m_colourSelection
].Green(),
489 m_standardColours
[m_colourSelection
].Blue());
491 PaintCustomColour(dc
);
492 PaintHighlight(dc
, true);
495 void wxGenericColourDialog
::OnCustomColourClick(int which
)
498 PaintHighlight(dc
, false);
500 m_colourSelection
= which
;
503 m_redSlider
->SetValue( m_customColours
[m_colourSelection
].Red() );
504 m_greenSlider
->SetValue( m_customColours
[m_colourSelection
].Green() );
505 m_blueSlider
->SetValue( m_customColours
[m_colourSelection
].Blue() );
506 #endif // wxUSE_SLIDER
508 m_colourData
.m_dataColour
.Set(m_customColours
[m_colourSelection
].Red(),
509 m_customColours
[m_colourSelection
].Green(),
510 m_customColours
[m_colourSelection
].Blue());
512 PaintCustomColour(dc
);
513 PaintHighlight(dc
, true);
517 void wxGenericColourDialog::OnOk(void)
522 void wxGenericColourDialog::OnCancel(void)
524 colourDialogCancelled = true;
529 void wxGenericColourDialog
::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
532 if (m_whichKind
!= 2)
534 PaintHighlight(dc
, false);
536 m_colourSelection
= 0;
537 PaintHighlight(dc
, true);
540 m_customColours
[m_colourSelection
].Set(m_colourData
.m_dataColour
.Red(),
541 m_colourData
.m_dataColour
.Green(),
542 m_colourData
.m_dataColour
.Blue());
544 m_colourData
.SetCustomColour(m_colourSelection
, m_customColours
[m_colourSelection
]);
546 PaintCustomColours(dc
);
551 void wxGenericColourDialog
::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
557 m_colourData
.m_dataColour
.Set((unsigned char)m_redSlider
->GetValue(), m_colourData
.m_dataColour
.Green(), m_colourData
.m_dataColour
.Blue());
558 PaintCustomColour(dc
);
561 void wxGenericColourDialog
::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
567 m_colourData
.m_dataColour
.Set(m_colourData
.m_dataColour
.Red(), (unsigned char)m_greenSlider
->GetValue(), m_colourData
.m_dataColour
.Blue());
568 PaintCustomColour(dc
);
571 void wxGenericColourDialog
::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
577 m_colourData
.m_dataColour
.Set(m_colourData
.m_dataColour
.Red(), m_colourData
.m_dataColour
.Green(), (unsigned char)m_blueSlider
->GetValue());
578 PaintCustomColour(dc
);
581 #endif // wxUSE_SLIDER
583 #endif // wxUSE_COLOURDLG && !defined(__WXGTK20__)