]>
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/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
& event
)
204 #if !defined(__WXMOTIF__) && !defined(__WXPM__) && !defined(__WXCOCOA__) && !defined(__WXOSX__)
205 wxDialog::OnPaint(event
);
212 PaintBasicColours(dc
);
213 PaintCustomColours(dc
);
214 PaintCustomColour(dc
);
215 PaintHighlight(dc
, true);
218 void wxGenericColourDialog::CalculateMeasurements()
220 m_smallRectangleSize
.x
= 18;
221 m_smallRectangleSize
.y
= 14;
222 m_customRectangleSize
.x
= 40;
223 m_customRectangleSize
.y
= 40;
226 m_sectionSpacing
= 15;
228 m_standardColoursRect
.x
= 10;
230 m_standardColoursRect
.y
= 15 + 20; /* OS/2 needs to account for dialog titlebar */
232 m_standardColoursRect
.y
= 15;
234 m_standardColoursRect
.width
= (8*m_smallRectangleSize
.x
) + (7*m_gridSpacing
);
235 m_standardColoursRect
.height
= (6*m_smallRectangleSize
.y
) + (5*m_gridSpacing
);
237 m_customColoursRect
.x
= m_standardColoursRect
.x
;
238 m_customColoursRect
.y
= m_standardColoursRect
.y
+ m_standardColoursRect
.height
+ 20;
239 m_customColoursRect
.width
= (8*m_smallRectangleSize
.x
) + (7*m_gridSpacing
);
240 m_customColoursRect
.height
= (2*m_smallRectangleSize
.y
) + (1*m_gridSpacing
);
242 m_singleCustomColourRect
.x
= m_customColoursRect
.width
+ m_customColoursRect
.x
+ m_sectionSpacing
;
243 m_singleCustomColourRect
.y
= 80;
244 m_singleCustomColourRect
.width
= m_customRectangleSize
.x
;
245 m_singleCustomColourRect
.height
= m_customRectangleSize
.y
;
248 m_customButtonX
= m_singleCustomColourRect
.x
;
249 m_buttonY
= m_customColoursRect
.y
+ m_customColoursRect
.height
+ 10;
252 void wxGenericColourDialog::CreateWidgets()
256 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
258 const int sliderHeight
= 160;
262 const int sliderX
= m_singleCustomColourRect
.x
+ m_singleCustomColourRect
.width
+ m_sectionSpacing
;
264 m_redSlider
= new wxSlider(this, wxID_RED_SLIDER
, m_colourData
.m_dataColour
.Red(), 0, 255,
265 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
266 m_greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, m_colourData
.m_dataColour
.Green(), 0, 255,
267 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
268 m_blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, m_colourData
.m_dataColour
.Blue(), 0, 255,
269 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
271 wxBoxSizer
*sliderSizer
= new wxBoxSizer( wxHORIZONTAL
);
273 sliderSizer
->Add(sliderX
, sliderHeight
);
275 wxSizerFlags flagsRight
;
276 flagsRight
.Align(wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
).DoubleBorder();
278 sliderSizer
->Add(m_redSlider
, flagsRight
);
279 sliderSizer
->Add(m_greenSlider
,flagsRight
);
280 sliderSizer
->Add(m_blueSlider
,flagsRight
);
282 topSizer
->Add(sliderSizer
, wxSizerFlags().Centre().DoubleBorder());
284 topSizer
->Add(1, sliderHeight
, wxSizerFlags(1).Centre().TripleBorder());
285 #endif // wxUSE_SLIDER
287 // then the custom button
288 topSizer
->Add(new wxButton(this, wxID_ADD_CUSTOM
,
289 _("Add to custom colours") ),
290 wxSizerFlags().DoubleHorzBorder());
292 // then the standard buttons
293 wxSizer
*buttonsizer
= CreateSeparatedButtonSizer(wxOK
| wxCANCEL
);
296 topSizer
->Add(buttonsizer
, wxSizerFlags().Expand().DoubleBorder());
299 SetAutoLayout( true );
300 SetSizer( topSizer
);
302 topSizer
->SetSizeHints( this );
303 topSizer
->Fit( this );
310 void wxGenericColourDialog::InitializeColours(void)
314 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
316 wxColour col
= wxTheColourDatabase
->Find(wxColourDialogNames
[i
]);
318 m_standardColours
[i
].Set(col
.Red(), col
.Green(), col
.Blue());
320 m_standardColours
[i
].Set(0, 0, 0);
323 for (i
= 0; i
< WXSIZEOF(m_customColours
); i
++)
325 wxColour c
= m_colourData
.GetCustomColour(i
);
327 m_customColours
[i
] = m_colourData
.GetCustomColour(i
);
329 m_customColours
[i
] = wxColour(255, 255, 255);
332 wxColour curr
= m_colourData
.GetColour();
335 bool m_initColourFound
= false;
337 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
339 if ( m_standardColours
[i
] == curr
&& !m_initColourFound
)
342 m_colourSelection
= i
;
343 m_initColourFound
= true;
347 if ( !m_initColourFound
)
349 for ( i
= 0; i
< WXSIZEOF(m_customColours
); i
++ )
351 if ( m_customColours
[i
] == curr
)
354 m_colourSelection
= i
;
359 m_colourData
.m_dataColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
364 m_colourSelection
= 0;
365 m_colourData
.m_dataColour
.Set( 0, 0, 0 );
369 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
372 for (i
= 0; i
< 6; i
++)
375 for (j
= 0; j
< 8; j
++)
379 int x
= (j
*(m_smallRectangleSize
.x
+m_gridSpacing
) + m_standardColoursRect
.x
);
380 int y
= (i
*(m_smallRectangleSize
.y
+m_gridSpacing
) + m_standardColoursRect
.y
);
382 dc
.SetPen(*wxBLACK_PEN
);
383 wxBrush
brush(m_standardColours
[ptr
]);
386 dc
.DrawRectangle( x
, y
, m_smallRectangleSize
.x
, m_smallRectangleSize
.y
);
391 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
394 for (i
= 0; i
< 2; i
++)
397 for (j
= 0; j
< 8; j
++)
401 int x
= (j
*(m_smallRectangleSize
.x
+m_gridSpacing
)) + m_customColoursRect
.x
;
402 int y
= (i
*(m_smallRectangleSize
.y
+m_gridSpacing
)) + m_customColoursRect
.y
;
404 dc
.SetPen(*wxBLACK_PEN
);
406 wxBrush
brush(m_customColours
[ptr
]);
409 dc
.DrawRectangle( x
, y
, m_smallRectangleSize
.x
, m_smallRectangleSize
.y
);
414 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
416 if ( m_colourSelection
< 0 )
419 // Number of pixels bigger than the standard rectangle size
420 // for drawing a highlight
424 if (m_whichKind
== 1)
427 int y
= (int)(m_colourSelection
/ 8);
428 int x
= (int)(m_colourSelection
- (y
*8));
430 x
= (x
*(m_smallRectangleSize
.x
+ m_gridSpacing
) + m_standardColoursRect
.x
) - deltaX
;
431 y
= (y
*(m_smallRectangleSize
.y
+ m_gridSpacing
) + m_standardColoursRect
.y
) - deltaY
;
434 dc
.SetPen(*wxBLACK_PEN
);
436 dc
.SetPen(*wxLIGHT_GREY_PEN
);
438 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
439 dc
.DrawRectangle( x
, y
, (m_smallRectangleSize
.x
+ (2*deltaX
)), (m_smallRectangleSize
.y
+ (2*deltaY
)));
443 // User-defined colours
444 int y
= (int)(m_colourSelection
/ 8);
445 int x
= (int)(m_colourSelection
- (y
*8));
447 x
= (x
*(m_smallRectangleSize
.x
+ m_gridSpacing
) + m_customColoursRect
.x
) - deltaX
;
448 y
= (y
*(m_smallRectangleSize
.y
+ m_gridSpacing
) + m_customColoursRect
.y
) - deltaY
;
451 dc
.SetPen(*wxBLACK_PEN
);
453 dc
.SetPen(*wxLIGHT_GREY_PEN
);
455 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
456 dc
.DrawRectangle( x
, y
, (m_smallRectangleSize
.x
+ (2*deltaX
)), (m_smallRectangleSize
.y
+ (2*deltaY
)));
460 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
462 dc
.SetPen(*wxBLACK_PEN
);
464 wxBrush
*brush
= new wxBrush(m_colourData
.m_dataColour
);
467 dc
.DrawRectangle( m_singleCustomColourRect
.x
, m_singleCustomColourRect
.y
,
468 m_customRectangleSize
.x
, m_customRectangleSize
.y
);
470 dc
.SetBrush(wxNullBrush
);
474 void wxGenericColourDialog::OnBasicColourClick(int which
)
478 PaintHighlight(dc
, false);
480 m_colourSelection
= which
;
483 m_redSlider
->SetValue( m_standardColours
[m_colourSelection
].Red() );
484 m_greenSlider
->SetValue( m_standardColours
[m_colourSelection
].Green() );
485 m_blueSlider
->SetValue( m_standardColours
[m_colourSelection
].Blue() );
486 #endif // wxUSE_SLIDER
488 m_colourData
.m_dataColour
.Set(m_standardColours
[m_colourSelection
].Red(),
489 m_standardColours
[m_colourSelection
].Green(),
490 m_standardColours
[m_colourSelection
].Blue());
492 PaintCustomColour(dc
);
493 PaintHighlight(dc
, true);
496 void wxGenericColourDialog::OnCustomColourClick(int which
)
499 PaintHighlight(dc
, false);
501 m_colourSelection
= which
;
504 m_redSlider
->SetValue( m_customColours
[m_colourSelection
].Red() );
505 m_greenSlider
->SetValue( m_customColours
[m_colourSelection
].Green() );
506 m_blueSlider
->SetValue( m_customColours
[m_colourSelection
].Blue() );
507 #endif // wxUSE_SLIDER
509 m_colourData
.m_dataColour
.Set(m_customColours
[m_colourSelection
].Red(),
510 m_customColours
[m_colourSelection
].Green(),
511 m_customColours
[m_colourSelection
].Blue());
513 PaintCustomColour(dc
);
514 PaintHighlight(dc
, true);
518 void wxGenericColourDialog::OnOk(void)
523 void wxGenericColourDialog::OnCancel(void)
525 colourDialogCancelled = true;
530 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
533 if (m_whichKind
!= 2)
535 PaintHighlight(dc
, false);
537 m_colourSelection
= 0;
538 PaintHighlight(dc
, true);
541 m_customColours
[m_colourSelection
].Set(m_colourData
.m_dataColour
.Red(),
542 m_colourData
.m_dataColour
.Green(),
543 m_colourData
.m_dataColour
.Blue());
545 m_colourData
.SetCustomColour(m_colourSelection
, m_customColours
[m_colourSelection
]);
547 PaintCustomColours(dc
);
552 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
558 m_colourData
.m_dataColour
.Set((unsigned char)m_redSlider
->GetValue(), m_colourData
.m_dataColour
.Green(), m_colourData
.m_dataColour
.Blue());
559 PaintCustomColour(dc
);
562 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
568 m_colourData
.m_dataColour
.Set(m_colourData
.m_dataColour
.Red(), (unsigned char)m_greenSlider
->GetValue(), m_colourData
.m_dataColour
.Blue());
569 PaintCustomColour(dc
);
572 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
578 m_colourData
.m_dataColour
.Set(m_colourData
.m_dataColour
.Red(), m_colourData
.m_dataColour
.Green(), (unsigned char)m_blueSlider
->GetValue());
579 PaintCustomColour(dc
);
582 #endif // wxUSE_SLIDER
584 #endif // wxUSE_COLOURDLG