]>
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
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
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/colourdata.h"
39 #include "wx/generic/colrdlgg.h"
41 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourDialog
, wxDialog
)
43 BEGIN_EVENT_TABLE(wxGenericColourDialog
, wxDialog
)
44 EVT_BUTTON(wxID_ADD_CUSTOM
, wxGenericColourDialog::OnAddCustom
)
46 EVT_SLIDER(wxID_RED_SLIDER
, wxGenericColourDialog::OnRedSlider
)
47 EVT_SLIDER(wxID_GREEN_SLIDER
, wxGenericColourDialog::OnGreenSlider
)
48 EVT_SLIDER(wxID_BLUE_SLIDER
, wxGenericColourDialog::OnBlueSlider
)
50 EVT_PAINT(wxGenericColourDialog::OnPaint
)
51 EVT_MOUSE_EVENTS(wxGenericColourDialog::OnMouseEvent
)
52 EVT_CLOSE(wxGenericColourDialog::OnCloseWindow
)
57 * Generic wxColourDialog
60 // don't change the number of elements (48) in this array, the code below is
61 // hardcoded to use it
62 static const wxChar
*const wxColourDialogNames
[] =
70 wxT("MEDIUM VIOLET RED"),
75 wxT("MEDIUM SPRING GREEN"),
78 wxT("LIGHT STEEL BLUE"),
98 wxT("MEDIUM VIOLET RED"),
102 wxT("MEDIUM SEA GREEN"),
104 wxT("MIDNIGHT BLUE"),
110 wxT("MEDIUM FOREST GREEN"),
115 wxT("MEDIUM SLATE BLUE"),
119 wxGenericColourDialog::wxGenericColourDialog()
122 m_colourSelection
= -1;
125 wxGenericColourDialog::wxGenericColourDialog(wxWindow
*parent
,
129 m_colourSelection
= -1;
130 Create(parent
, data
);
133 wxGenericColourDialog::~wxGenericColourDialog()
137 void wxGenericColourDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
139 EndModal(wxID_CANCEL
);
142 bool wxGenericColourDialog::Create(wxWindow
*parent
, wxColourData
*data
)
144 if ( !wxDialog::Create(GetParentForModalDialog(parent
, 0), wxID_ANY
,
146 wxPoint(0, 0), wxSize(900, 900)) )
150 m_colourData
= *data
;
153 CalculateMeasurements();
159 int wxGenericColourDialog::ShowModal()
161 return wxDialog::ShowModal();
165 // Internal functions
166 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent
& event
)
168 if (event
.ButtonDown(1))
170 int x
= (int)event
.GetX();
171 int y
= (int)event
.GetY();
174 // Handle OS/2's reverse coordinate system and account for the dialog title
177 GetClientSize(NULL
, &nClientHeight
);
178 y
= (nClientHeight
- y
) + 20;
180 if ((x
>= m_standardColoursRect
.x
&& x
<= (m_standardColoursRect
.x
+ m_standardColoursRect
.width
)) &&
181 (y
>= m_standardColoursRect
.y
&& y
<= (m_standardColoursRect
.y
+ m_standardColoursRect
.height
)))
183 int selX
= (int)(x
- m_standardColoursRect
.x
)/(m_smallRectangleSize
.x
+ m_gridSpacing
);
184 int selY
= (int)(y
- m_standardColoursRect
.y
)/(m_smallRectangleSize
.y
+ m_gridSpacing
);
185 int ptr
= (int)(selX
+ selY
*8);
186 OnBasicColourClick(ptr
);
188 else if ((x
>= m_customColoursRect
.x
&& x
<= (m_customColoursRect
.x
+ m_customColoursRect
.width
)) &&
189 (y
>= m_customColoursRect
.y
&& y
<= (m_customColoursRect
.y
+ m_customColoursRect
.height
)))
191 int selX
= (int)(x
- m_customColoursRect
.x
)/(m_smallRectangleSize
.x
+ m_gridSpacing
);
192 int selY
= (int)(y
- m_customColoursRect
.y
)/(m_smallRectangleSize
.y
+ m_gridSpacing
);
193 int ptr
= (int)(selX
+ selY
*8);
194 OnCustomColourClick(ptr
);
203 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
205 #if !defined(__WXMOTIF__) && !defined(__WXPM__) && !defined(__WXCOCOA__) && !defined(__WXOSX__)
206 wxDialog::OnPaint(event
);
213 PaintBasicColours(dc
);
214 PaintCustomColours(dc
);
215 PaintCustomColour(dc
);
216 PaintHighlight(dc
, true);
219 void wxGenericColourDialog::CalculateMeasurements()
221 m_smallRectangleSize
.x
= 18;
222 m_smallRectangleSize
.y
= 14;
223 m_customRectangleSize
.x
= 40;
224 m_customRectangleSize
.y
= 40;
227 m_sectionSpacing
= 15;
229 m_standardColoursRect
.x
= 10;
231 m_standardColoursRect
.y
= 15 + 20; /* OS/2 needs to account for dialog titlebar */
233 m_standardColoursRect
.y
= 15;
235 m_standardColoursRect
.width
= (8*m_smallRectangleSize
.x
) + (7*m_gridSpacing
);
236 m_standardColoursRect
.height
= (6*m_smallRectangleSize
.y
) + (5*m_gridSpacing
);
238 m_customColoursRect
.x
= m_standardColoursRect
.x
;
239 m_customColoursRect
.y
= m_standardColoursRect
.y
+ m_standardColoursRect
.height
+ 20;
240 m_customColoursRect
.width
= (8*m_smallRectangleSize
.x
) + (7*m_gridSpacing
);
241 m_customColoursRect
.height
= (2*m_smallRectangleSize
.y
) + (1*m_gridSpacing
);
243 m_singleCustomColourRect
.x
= m_customColoursRect
.width
+ m_customColoursRect
.x
+ m_sectionSpacing
;
244 m_singleCustomColourRect
.y
= 80;
245 m_singleCustomColourRect
.width
= m_customRectangleSize
.x
;
246 m_singleCustomColourRect
.height
= m_customRectangleSize
.y
;
249 m_customButtonX
= m_singleCustomColourRect
.x
;
250 m_buttonY
= m_customColoursRect
.y
+ m_customColoursRect
.height
+ 10;
253 void wxGenericColourDialog::CreateWidgets()
257 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
259 const int sliderHeight
= 160;
263 const int sliderX
= m_singleCustomColourRect
.x
+ m_singleCustomColourRect
.width
+ m_sectionSpacing
;
265 m_redSlider
= new wxSlider(this, wxID_RED_SLIDER
, m_colourData
.m_dataColour
.Red(), 0, 255,
266 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
267 m_greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, m_colourData
.m_dataColour
.Green(), 0, 255,
268 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
269 m_blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, m_colourData
.m_dataColour
.Blue(), 0, 255,
270 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
272 wxBoxSizer
*sliderSizer
= new wxBoxSizer( wxHORIZONTAL
);
274 sliderSizer
->Add(sliderX
, sliderHeight
);
276 wxSizerFlags flagsRight
;
277 flagsRight
.Align(wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
).DoubleBorder();
279 sliderSizer
->Add(m_redSlider
, flagsRight
);
280 sliderSizer
->Add(m_greenSlider
,flagsRight
);
281 sliderSizer
->Add(m_blueSlider
,flagsRight
);
283 topSizer
->Add(sliderSizer
, wxSizerFlags().Centre().DoubleBorder());
285 topSizer
->Add(1, sliderHeight
, wxSizerFlags(1).Centre().TripleBorder());
286 #endif // wxUSE_SLIDER
288 // then the custom button
289 topSizer
->Add(new wxButton(this, wxID_ADD_CUSTOM
,
290 _("Add to custom colours") ),
291 wxSizerFlags().DoubleHorzBorder());
293 // then the standard buttons
294 wxSizer
*buttonsizer
= CreateSeparatedButtonSizer(wxOK
| wxCANCEL
);
297 topSizer
->Add(buttonsizer
, wxSizerFlags().Expand().DoubleBorder());
300 SetAutoLayout( true );
301 SetSizer( topSizer
);
303 topSizer
->SetSizeHints( this );
304 topSizer
->Fit( this );
311 void wxGenericColourDialog::InitializeColours(void)
315 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
317 wxColour col
= wxTheColourDatabase
->Find(wxColourDialogNames
[i
]);
319 m_standardColours
[i
].Set(col
.Red(), col
.Green(), col
.Blue());
321 m_standardColours
[i
].Set(0, 0, 0);
324 for (i
= 0; i
< WXSIZEOF(m_customColours
); i
++)
326 wxColour c
= m_colourData
.GetCustomColour(i
);
328 m_customColours
[i
] = m_colourData
.GetCustomColour(i
);
330 m_customColours
[i
] = wxColour(255, 255, 255);
333 wxColour curr
= m_colourData
.GetColour();
336 bool m_initColourFound
= false;
338 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
340 if ( m_standardColours
[i
] == curr
&& !m_initColourFound
)
343 m_colourSelection
= i
;
344 m_initColourFound
= true;
348 if ( !m_initColourFound
)
350 for ( i
= 0; i
< WXSIZEOF(m_customColours
); i
++ )
352 if ( m_customColours
[i
] == curr
)
355 m_colourSelection
= i
;
360 m_colourData
.m_dataColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
365 m_colourSelection
= 0;
366 m_colourData
.m_dataColour
.Set( 0, 0, 0 );
370 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
373 for (i
= 0; i
< 6; i
++)
376 for (j
= 0; j
< 8; j
++)
380 int x
= (j
*(m_smallRectangleSize
.x
+m_gridSpacing
) + m_standardColoursRect
.x
);
381 int y
= (i
*(m_smallRectangleSize
.y
+m_gridSpacing
) + m_standardColoursRect
.y
);
383 dc
.SetPen(*wxBLACK_PEN
);
384 wxBrush
brush(m_standardColours
[ptr
]);
387 dc
.DrawRectangle( x
, y
, m_smallRectangleSize
.x
, m_smallRectangleSize
.y
);
392 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
395 for (i
= 0; i
< 2; i
++)
398 for (j
= 0; j
< 8; j
++)
402 int x
= (j
*(m_smallRectangleSize
.x
+m_gridSpacing
)) + m_customColoursRect
.x
;
403 int y
= (i
*(m_smallRectangleSize
.y
+m_gridSpacing
)) + m_customColoursRect
.y
;
405 dc
.SetPen(*wxBLACK_PEN
);
407 wxBrush
brush(m_customColours
[ptr
]);
410 dc
.DrawRectangle( x
, y
, m_smallRectangleSize
.x
, m_smallRectangleSize
.y
);
415 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
417 if ( m_colourSelection
< 0 )
420 // Number of pixels bigger than the standard rectangle size
421 // for drawing a highlight
425 if (m_whichKind
== 1)
428 int y
= (int)(m_colourSelection
/ 8);
429 int x
= (int)(m_colourSelection
- (y
*8));
431 x
= (x
*(m_smallRectangleSize
.x
+ m_gridSpacing
) + m_standardColoursRect
.x
) - deltaX
;
432 y
= (y
*(m_smallRectangleSize
.y
+ m_gridSpacing
) + m_standardColoursRect
.y
) - deltaY
;
435 dc
.SetPen(*wxBLACK_PEN
);
437 dc
.SetPen(*wxLIGHT_GREY_PEN
);
439 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
440 dc
.DrawRectangle( x
, y
, (m_smallRectangleSize
.x
+ (2*deltaX
)), (m_smallRectangleSize
.y
+ (2*deltaY
)));
444 // User-defined colours
445 int y
= (int)(m_colourSelection
/ 8);
446 int x
= (int)(m_colourSelection
- (y
*8));
448 x
= (x
*(m_smallRectangleSize
.x
+ m_gridSpacing
) + m_customColoursRect
.x
) - deltaX
;
449 y
= (y
*(m_smallRectangleSize
.y
+ m_gridSpacing
) + m_customColoursRect
.y
) - deltaY
;
452 dc
.SetPen(*wxBLACK_PEN
);
454 dc
.SetPen(*wxLIGHT_GREY_PEN
);
456 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
457 dc
.DrawRectangle( x
, y
, (m_smallRectangleSize
.x
+ (2*deltaX
)), (m_smallRectangleSize
.y
+ (2*deltaY
)));
461 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
463 dc
.SetPen(*wxBLACK_PEN
);
465 wxBrush
*brush
= new wxBrush(m_colourData
.m_dataColour
);
468 dc
.DrawRectangle( m_singleCustomColourRect
.x
, m_singleCustomColourRect
.y
,
469 m_customRectangleSize
.x
, m_customRectangleSize
.y
);
471 dc
.SetBrush(wxNullBrush
);
475 void wxGenericColourDialog::OnBasicColourClick(int which
)
479 PaintHighlight(dc
, false);
481 m_colourSelection
= which
;
484 m_redSlider
->SetValue( m_standardColours
[m_colourSelection
].Red() );
485 m_greenSlider
->SetValue( m_standardColours
[m_colourSelection
].Green() );
486 m_blueSlider
->SetValue( m_standardColours
[m_colourSelection
].Blue() );
487 #endif // wxUSE_SLIDER
489 m_colourData
.m_dataColour
.Set(m_standardColours
[m_colourSelection
].Red(),
490 m_standardColours
[m_colourSelection
].Green(),
491 m_standardColours
[m_colourSelection
].Blue());
493 PaintCustomColour(dc
);
494 PaintHighlight(dc
, true);
497 void wxGenericColourDialog::OnCustomColourClick(int which
)
500 PaintHighlight(dc
, false);
502 m_colourSelection
= which
;
505 m_redSlider
->SetValue( m_customColours
[m_colourSelection
].Red() );
506 m_greenSlider
->SetValue( m_customColours
[m_colourSelection
].Green() );
507 m_blueSlider
->SetValue( m_customColours
[m_colourSelection
].Blue() );
508 #endif // wxUSE_SLIDER
510 m_colourData
.m_dataColour
.Set(m_customColours
[m_colourSelection
].Red(),
511 m_customColours
[m_colourSelection
].Green(),
512 m_customColours
[m_colourSelection
].Blue());
514 PaintCustomColour(dc
);
515 PaintHighlight(dc
, true);
519 void wxGenericColourDialog::OnOk(void)
524 void wxGenericColourDialog::OnCancel(void)
526 colourDialogCancelled = true;
531 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
534 if (m_whichKind
!= 2)
536 PaintHighlight(dc
, false);
538 m_colourSelection
= 0;
539 PaintHighlight(dc
, true);
542 m_customColours
[m_colourSelection
].Set(m_colourData
.m_dataColour
.Red(),
543 m_colourData
.m_dataColour
.Green(),
544 m_colourData
.m_dataColour
.Blue());
546 m_colourData
.SetCustomColour(m_colourSelection
, m_customColours
[m_colourSelection
]);
548 PaintCustomColours(dc
);
553 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
559 m_colourData
.m_dataColour
.Set((unsigned char)m_redSlider
->GetValue(), m_colourData
.m_dataColour
.Green(), m_colourData
.m_dataColour
.Blue());
560 PaintCustomColour(dc
);
563 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
569 m_colourData
.m_dataColour
.Set(m_colourData
.m_dataColour
.Red(), (unsigned char)m_greenSlider
->GetValue(), m_colourData
.m_dataColour
.Blue());
570 PaintCustomColour(dc
);
573 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
579 m_colourData
.m_dataColour
.Set(m_colourData
.m_dataColour
.Red(), m_colourData
.m_dataColour
.Green(), (unsigned char)m_blueSlider
->GetValue());
580 PaintCustomColour(dc
);
583 #endif // wxUSE_SLIDER
585 #endif // wxUSE_COLOURDLG