]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/colrdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/colrdlgg.cpp
3 // Purpose: Choice dialogs
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
23 #include "wx/dialog.h"
24 #include "wx/listbox.h"
25 #include "wx/button.h"
26 #include "wx/stattext.h"
27 #include "wx/layout.h"
28 #include "wx/dcclient.h"
30 #include "wx/slider.h"
34 #include "wx/statline.h"
37 #include "wx/colourdata.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
*const 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(GetParentForModalDialog(parent
, 0), wxID_ANY
,
145 wxPoint(0, 0), wxSize(900, 900)) )
149 m_colourData
= *data
;
152 CalculateMeasurements();
158 int wxGenericColourDialog::ShowModal()
160 return wxDialog::ShowModal();
164 // Internal functions
165 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent
& event
)
167 if (event
.ButtonDown(1))
169 int x
= (int)event
.GetX();
170 int y
= (int)event
.GetY();
173 // Handle OS/2's reverse coordinate system and account for the dialog title
176 GetClientSize(NULL
, &nClientHeight
);
177 y
= (nClientHeight
- y
) + 20;
179 if ((x
>= m_standardColoursRect
.x
&& x
<= (m_standardColoursRect
.x
+ m_standardColoursRect
.width
)) &&
180 (y
>= m_standardColoursRect
.y
&& y
<= (m_standardColoursRect
.y
+ m_standardColoursRect
.height
)))
182 int selX
= (int)(x
- m_standardColoursRect
.x
)/(m_smallRectangleSize
.x
+ m_gridSpacing
);
183 int selY
= (int)(y
- m_standardColoursRect
.y
)/(m_smallRectangleSize
.y
+ m_gridSpacing
);
184 int ptr
= (int)(selX
+ selY
*8);
185 OnBasicColourClick(ptr
);
187 else if ((x
>= m_customColoursRect
.x
&& x
<= (m_customColoursRect
.x
+ m_customColoursRect
.width
)) &&
188 (y
>= m_customColoursRect
.y
&& y
<= (m_customColoursRect
.y
+ m_customColoursRect
.height
)))
190 int selX
= (int)(x
- m_customColoursRect
.x
)/(m_smallRectangleSize
.x
+ m_gridSpacing
);
191 int selY
= (int)(y
- m_customColoursRect
.y
)/(m_smallRectangleSize
.y
+ m_gridSpacing
);
192 int ptr
= (int)(selX
+ selY
*8);
193 OnCustomColourClick(ptr
);
202 void wxGenericColourDialog::OnPaint(wxPaintEvent
& WXUNUSED(event
))
206 PaintBasicColours(dc
);
207 PaintCustomColours(dc
);
208 PaintCustomColour(dc
);
209 PaintHighlight(dc
, true);
212 void wxGenericColourDialog::CalculateMeasurements()
214 m_smallRectangleSize
.x
= 18;
215 m_smallRectangleSize
.y
= 14;
216 m_customRectangleSize
.x
= 40;
217 m_customRectangleSize
.y
= 40;
220 m_sectionSpacing
= 15;
222 m_standardColoursRect
.x
= 10;
224 m_standardColoursRect
.y
= 15 + 20; /* OS/2 needs to account for dialog titlebar */
226 m_standardColoursRect
.y
= 15;
228 m_standardColoursRect
.width
= (8*m_smallRectangleSize
.x
) + (7*m_gridSpacing
);
229 m_standardColoursRect
.height
= (6*m_smallRectangleSize
.y
) + (5*m_gridSpacing
);
231 m_customColoursRect
.x
= m_standardColoursRect
.x
;
232 m_customColoursRect
.y
= m_standardColoursRect
.y
+ m_standardColoursRect
.height
+ 20;
233 m_customColoursRect
.width
= (8*m_smallRectangleSize
.x
) + (7*m_gridSpacing
);
234 m_customColoursRect
.height
= (2*m_smallRectangleSize
.y
) + (1*m_gridSpacing
);
236 m_singleCustomColourRect
.x
= m_customColoursRect
.width
+ m_customColoursRect
.x
+ m_sectionSpacing
;
237 m_singleCustomColourRect
.y
= 80;
238 m_singleCustomColourRect
.width
= m_customRectangleSize
.x
;
239 m_singleCustomColourRect
.height
= m_customRectangleSize
.y
;
242 m_customButtonX
= m_singleCustomColourRect
.x
;
243 m_buttonY
= m_customColoursRect
.y
+ m_customColoursRect
.height
+ 10;
246 void wxGenericColourDialog::CreateWidgets()
250 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
252 const int sliderHeight
= 160;
256 const int sliderX
= m_singleCustomColourRect
.x
+ m_singleCustomColourRect
.width
+ m_sectionSpacing
;
258 m_redSlider
= new wxSlider(this, wxID_RED_SLIDER
, m_colourData
.m_dataColour
.Red(), 0, 255,
259 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
260 m_greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, m_colourData
.m_dataColour
.Green(), 0, 255,
261 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
262 m_blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, m_colourData
.m_dataColour
.Blue(), 0, 255,
263 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
265 wxBoxSizer
*sliderSizer
= new wxBoxSizer( wxHORIZONTAL
);
267 sliderSizer
->Add(sliderX
, sliderHeight
);
269 wxSizerFlags flagsRight
;
270 flagsRight
.Align(wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
).DoubleBorder();
272 sliderSizer
->Add(m_redSlider
, flagsRight
);
273 sliderSizer
->Add(m_greenSlider
,flagsRight
);
274 sliderSizer
->Add(m_blueSlider
,flagsRight
);
276 topSizer
->Add(sliderSizer
, wxSizerFlags().Centre().DoubleBorder());
278 topSizer
->Add(1, sliderHeight
, wxSizerFlags(1).Centre().TripleBorder());
279 #endif // wxUSE_SLIDER
281 // then the custom button
282 topSizer
->Add(new wxButton(this, wxID_ADD_CUSTOM
,
283 _("Add to custom colours") ),
284 wxSizerFlags().DoubleHorzBorder());
286 // then the standard buttons
287 wxSizer
*buttonsizer
= CreateSeparatedButtonSizer(wxOK
| wxCANCEL
);
290 topSizer
->Add(buttonsizer
, wxSizerFlags().Expand().DoubleBorder());
293 SetAutoLayout( true );
294 SetSizer( topSizer
);
296 topSizer
->SetSizeHints( this );
297 topSizer
->Fit( this );
304 void wxGenericColourDialog::InitializeColours(void)
308 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
310 wxColour col
= wxTheColourDatabase
->Find(wxColourDialogNames
[i
]);
312 m_standardColours
[i
].Set(col
.Red(), col
.Green(), col
.Blue());
314 m_standardColours
[i
].Set(0, 0, 0);
317 for (i
= 0; i
< WXSIZEOF(m_customColours
); i
++)
319 wxColour c
= m_colourData
.GetCustomColour(i
);
321 m_customColours
[i
] = m_colourData
.GetCustomColour(i
);
323 m_customColours
[i
] = wxColour(255, 255, 255);
326 wxColour curr
= m_colourData
.GetColour();
329 bool m_initColourFound
= false;
331 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
333 if ( m_standardColours
[i
] == curr
&& !m_initColourFound
)
336 m_colourSelection
= i
;
337 m_initColourFound
= true;
341 if ( !m_initColourFound
)
343 for ( i
= 0; i
< WXSIZEOF(m_customColours
); i
++ )
345 if ( m_customColours
[i
] == curr
)
348 m_colourSelection
= i
;
353 m_colourData
.m_dataColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
358 m_colourSelection
= 0;
359 m_colourData
.m_dataColour
.Set( 0, 0, 0 );
363 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
366 for (i
= 0; i
< 6; i
++)
369 for (j
= 0; j
< 8; j
++)
373 int x
= (j
*(m_smallRectangleSize
.x
+m_gridSpacing
) + m_standardColoursRect
.x
);
374 int y
= (i
*(m_smallRectangleSize
.y
+m_gridSpacing
) + m_standardColoursRect
.y
);
376 dc
.SetPen(*wxBLACK_PEN
);
377 wxBrush
brush(m_standardColours
[ptr
]);
380 dc
.DrawRectangle( x
, y
, m_smallRectangleSize
.x
, m_smallRectangleSize
.y
);
385 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
388 for (i
= 0; i
< 2; i
++)
391 for (j
= 0; j
< 8; j
++)
395 int x
= (j
*(m_smallRectangleSize
.x
+m_gridSpacing
)) + m_customColoursRect
.x
;
396 int y
= (i
*(m_smallRectangleSize
.y
+m_gridSpacing
)) + m_customColoursRect
.y
;
398 dc
.SetPen(*wxBLACK_PEN
);
400 wxBrush
brush(m_customColours
[ptr
]);
403 dc
.DrawRectangle( x
, y
, m_smallRectangleSize
.x
, m_smallRectangleSize
.y
);
408 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
410 if ( m_colourSelection
< 0 )
413 // Number of pixels bigger than the standard rectangle size
414 // for drawing a highlight
418 if (m_whichKind
== 1)
421 int y
= (int)(m_colourSelection
/ 8);
422 int x
= (int)(m_colourSelection
- (y
*8));
424 x
= (x
*(m_smallRectangleSize
.x
+ m_gridSpacing
) + m_standardColoursRect
.x
) - deltaX
;
425 y
= (y
*(m_smallRectangleSize
.y
+ m_gridSpacing
) + m_standardColoursRect
.y
) - deltaY
;
428 dc
.SetPen(*wxBLACK_PEN
);
430 dc
.SetPen(*wxLIGHT_GREY_PEN
);
432 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
433 dc
.DrawRectangle( x
, y
, (m_smallRectangleSize
.x
+ (2*deltaX
)), (m_smallRectangleSize
.y
+ (2*deltaY
)));
437 // User-defined colours
438 int y
= (int)(m_colourSelection
/ 8);
439 int x
= (int)(m_colourSelection
- (y
*8));
441 x
= (x
*(m_smallRectangleSize
.x
+ m_gridSpacing
) + m_customColoursRect
.x
) - deltaX
;
442 y
= (y
*(m_smallRectangleSize
.y
+ m_gridSpacing
) + m_customColoursRect
.y
) - deltaY
;
445 dc
.SetPen(*wxBLACK_PEN
);
447 dc
.SetPen(*wxLIGHT_GREY_PEN
);
449 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
450 dc
.DrawRectangle( x
, y
, (m_smallRectangleSize
.x
+ (2*deltaX
)), (m_smallRectangleSize
.y
+ (2*deltaY
)));
454 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
456 dc
.SetPen(*wxBLACK_PEN
);
458 wxBrush
*brush
= new wxBrush(m_colourData
.m_dataColour
);
461 dc
.DrawRectangle( m_singleCustomColourRect
.x
, m_singleCustomColourRect
.y
,
462 m_customRectangleSize
.x
, m_customRectangleSize
.y
);
464 dc
.SetBrush(wxNullBrush
);
468 void wxGenericColourDialog::OnBasicColourClick(int which
)
472 PaintHighlight(dc
, false);
474 m_colourSelection
= which
;
477 m_redSlider
->SetValue( m_standardColours
[m_colourSelection
].Red() );
478 m_greenSlider
->SetValue( m_standardColours
[m_colourSelection
].Green() );
479 m_blueSlider
->SetValue( m_standardColours
[m_colourSelection
].Blue() );
480 #endif // wxUSE_SLIDER
482 m_colourData
.m_dataColour
.Set(m_standardColours
[m_colourSelection
].Red(),
483 m_standardColours
[m_colourSelection
].Green(),
484 m_standardColours
[m_colourSelection
].Blue());
486 PaintCustomColour(dc
);
487 PaintHighlight(dc
, true);
490 void wxGenericColourDialog::OnCustomColourClick(int which
)
493 PaintHighlight(dc
, false);
495 m_colourSelection
= which
;
498 m_redSlider
->SetValue( m_customColours
[m_colourSelection
].Red() );
499 m_greenSlider
->SetValue( m_customColours
[m_colourSelection
].Green() );
500 m_blueSlider
->SetValue( m_customColours
[m_colourSelection
].Blue() );
501 #endif // wxUSE_SLIDER
503 m_colourData
.m_dataColour
.Set(m_customColours
[m_colourSelection
].Red(),
504 m_customColours
[m_colourSelection
].Green(),
505 m_customColours
[m_colourSelection
].Blue());
507 PaintCustomColour(dc
);
508 PaintHighlight(dc
, true);
512 void wxGenericColourDialog::OnOk(void)
517 void wxGenericColourDialog::OnCancel(void)
519 colourDialogCancelled = true;
524 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
527 if (m_whichKind
!= 2)
529 PaintHighlight(dc
, false);
531 m_colourSelection
= 0;
532 PaintHighlight(dc
, true);
535 m_customColours
[m_colourSelection
].Set(m_colourData
.m_dataColour
.Red(),
536 m_colourData
.m_dataColour
.Green(),
537 m_colourData
.m_dataColour
.Blue());
539 m_colourData
.SetCustomColour(m_colourSelection
, m_customColours
[m_colourSelection
]);
541 PaintCustomColours(dc
);
546 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
552 m_colourData
.m_dataColour
.Set((unsigned char)m_redSlider
->GetValue(), m_colourData
.m_dataColour
.Green(), m_colourData
.m_dataColour
.Blue());
553 PaintCustomColour(dc
);
556 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
562 m_colourData
.m_dataColour
.Set(m_colourData
.m_dataColour
.Red(), (unsigned char)m_greenSlider
->GetValue(), m_colourData
.m_dataColour
.Blue());
563 PaintCustomColour(dc
);
566 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
572 m_colourData
.m_dataColour
.Set(m_colourData
.m_dataColour
.Red(), m_colourData
.m_dataColour
.Green(), (unsigned char)m_blueSlider
->GetValue());
573 PaintCustomColour(dc
);
576 #endif // wxUSE_SLIDER
578 #endif // wxUSE_COLOURDLG