]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/colrdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
34 #include "wx/statline.h"
38 #include "wx/slider.h"
41 #include "wx/generic/colrdlgg.h"
43 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourDialog
, wxDialog
)
45 BEGIN_EVENT_TABLE(wxGenericColourDialog
, wxDialog
)
46 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
)
52 EVT_PAINT(wxGenericColourDialog::OnPaint
)
53 EVT_MOUSE_EVENTS(wxGenericColourDialog::OnMouseEvent
)
54 EVT_CLOSE(wxGenericColourDialog::OnCloseWindow
)
59 * Generic wxColourDialog
62 // don't change the number of elements (48) in this array, the code below is
63 // hardcoded to use it
64 static const wxChar
*wxColourDialogNames
[] =
72 wxT("MEDIUM VIOLET RED"),
77 wxT("MEDIUM SPRING GREEN"),
80 wxT("LIGHT STEEL BLUE"),
100 wxT("MEDIUM VIOLET RED"),
104 wxT("MEDIUM SEA GREEN"),
106 wxT("MIDNIGHT BLUE"),
112 wxT("MEDIUM FOREST GREEN"),
117 wxT("MEDIUM SLATE BLUE"),
121 wxGenericColourDialog::wxGenericColourDialog()
125 colourSelection
= -1;
128 wxGenericColourDialog::wxGenericColourDialog(wxWindow
*parent
,
132 colourSelection
= -1;
133 Create(parent
, data
);
136 wxGenericColourDialog::~wxGenericColourDialog()
140 void wxGenericColourDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
142 EndModal(wxID_CANCEL
);
145 bool wxGenericColourDialog::Create(wxWindow
*parent
, wxColourData
*data
)
147 if ( !wxDialog::Create(parent
, wxID_ANY
, _("Choose colour"),
148 wxPoint(0,0), wxSize(900, 900)) )
151 dialogParent
= parent
;
157 CalculateMeasurements();
163 int wxGenericColourDialog::ShowModal()
165 return wxDialog::ShowModal();
169 // Internal functions
170 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent
& event
)
172 if (event
.ButtonDown(1))
174 int x
= (int)event
.GetX();
175 int y
= (int)event
.GetY();
178 // Handle OS/2's reverse coordinate system and account for the dialog title
181 GetClientSize(NULL
, &nClientHeight
);
182 y
= (nClientHeight
- y
) + 20;
184 if ((x
>= standardColoursRect
.x
&& x
<= (standardColoursRect
.x
+ standardColoursRect
.width
)) &&
185 (y
>= standardColoursRect
.y
&& y
<= (standardColoursRect
.y
+ standardColoursRect
.height
)))
187 int selX
= (int)(x
- standardColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
188 int selY
= (int)(y
- standardColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
189 int ptr
= (int)(selX
+ selY
*8);
190 OnBasicColourClick(ptr
);
192 else if ((x
>= customColoursRect
.x
&& x
<= (customColoursRect
.x
+ customColoursRect
.width
)) &&
193 (y
>= customColoursRect
.y
&& y
<= (customColoursRect
.y
+ customColoursRect
.height
)))
195 int selX
= (int)(x
- customColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
196 int selY
= (int)(y
- customColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
197 int ptr
= (int)(selX
+ selY
*8);
198 OnCustomColourClick(ptr
);
207 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
209 #if !defined(__WXMOTIF__) && !defined(__WXPM__) && !defined(__WXCOCOA__)
210 wxDialog::OnPaint(event
);
217 PaintBasicColours(dc
);
218 PaintCustomColours(dc
);
219 PaintCustomColour(dc
);
220 PaintHighlight(dc
, true);
223 void wxGenericColourDialog::CalculateMeasurements()
225 smallRectangleSize
.x
= 18;
226 smallRectangleSize
.y
= 14;
227 customRectangleSize
.x
= 40;
228 customRectangleSize
.y
= 40;
233 standardColoursRect
.x
= 10;
235 standardColoursRect
.y
= 15 + 20; /* OS/2 needs to account for dialog titlebar */
237 standardColoursRect
.y
= 15;
239 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
240 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
242 customColoursRect
.x
= standardColoursRect
.x
;
243 customColoursRect
.y
= standardColoursRect
.y
+ standardColoursRect
.height
+ 20;
244 customColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
245 customColoursRect
.height
= (2*smallRectangleSize
.y
) + (1*gridSpacing
);
247 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
248 singleCustomColourRect
.y
= 80;
249 singleCustomColourRect
.width
= customRectangleSize
.x
;
250 singleCustomColourRect
.height
= customRectangleSize
.y
;
253 customButtonX
= singleCustomColourRect
.x
;
254 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
257 void wxGenericColourDialog::CreateWidgets()
261 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
263 const int sliderHeight
= 160;
266 const int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
268 redSlider
= new wxSlider(this, wxID_RED_SLIDER
, colourData
.m_dataColour
.Red(), 0, 255,
269 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
270 greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, colourData
.m_dataColour
.Green(), 0, 255,
271 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
272 blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, colourData
.m_dataColour
.Blue(), 0, 255,
273 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
275 wxBoxSizer
*sliderSizer
= new wxBoxSizer( wxHORIZONTAL
);
277 // 1) space for sliders
278 sliderSizer
->Add( sliderX
, sliderHeight
);
279 sliderSizer
->Add( redSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
280 sliderSizer
->Add( greenSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
281 sliderSizer
->Add( blueSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
283 topSizer
->Add( sliderSizer
, 0, wxCENTRE
| wxALL
, 10 );
285 topSizer
->Add( 1, sliderHeight
, 0, wxCENTRE
| wxALL
, 15 );
286 #endif // wxUSE_SLIDER
290 topSizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
294 wxSizer
*buttonsizer
= CreateButtonSizer( wxOK
|wxCANCEL
);
295 buttonsizer
->Add( new wxButton(this, wxID_ADD_CUSTOM
, _("Add to custom colours") ), 0, wxLEFT
|wxRIGHT
, 10 );
296 topSizer
->Add( buttonsizer
, 0, wxEXPAND
| wxALL
, 10 );
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 standardColours
[i
].Set(col
.Red(), col
.Green(), col
.Blue());
319 standardColours
[i
].Set(0, 0, 0);
322 for (i
= 0; i
< WXSIZEOF(customColours
); i
++)
324 wxColour c
= colourData
.GetCustomColour(i
);
326 customColours
[i
] = colourData
.GetCustomColour(i
);
328 customColours
[i
] = wxColour(255, 255, 255);
331 wxColour curr
= colourData
.GetColour();
334 bool initColourFound
= false;
336 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
338 if ( standardColours
[i
] == curr
&& !initColourFound
)
342 initColourFound
= true;
346 if ( !initColourFound
)
348 for ( i
= 0; i
< WXSIZEOF(customColours
); i
++ )
350 if ( customColours
[i
] == curr
)
358 colourData
.m_dataColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
364 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
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
379 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
381 dc
.SetPen(*wxBLACK_PEN
);
382 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
385 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, 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
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
401 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
403 dc
.SetPen(*wxBLACK_PEN
);
405 wxBrush
brush(customColours
[ptr
], wxSOLID
);
408 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
413 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
415 if ( colourSelection
< 0 )
418 // Number of pixels bigger than the standard rectangle size
419 // for drawing a highlight
426 int y
= (int)(colourSelection
/ 8);
427 int x
= (int)(colourSelection
- (y
*8));
429 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
430 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.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
)));
442 // User-defined colours
443 int y
= (int)(colourSelection
/ 8);
444 int x
= (int)(colourSelection
- (y
*8));
446 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
447 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
450 dc
.SetPen(*wxBLACK_PEN
);
452 dc
.SetPen(*wxLIGHT_GREY_PEN
);
454 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
455 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
459 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
461 dc
.SetPen(*wxBLACK_PEN
);
463 wxBrush
*brush
= new wxBrush(colourData
.m_dataColour
, wxSOLID
);
466 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
467 customRectangleSize
.x
, customRectangleSize
.y
);
469 dc
.SetBrush(wxNullBrush
);
473 void wxGenericColourDialog::OnBasicColourClick(int which
)
477 PaintHighlight(dc
, false);
479 colourSelection
= which
;
482 redSlider
->SetValue( standardColours
[colourSelection
].Red() );
483 greenSlider
->SetValue( standardColours
[colourSelection
].Green() );
484 blueSlider
->SetValue( standardColours
[colourSelection
].Blue() );
485 #endif // wxUSE_SLIDER
487 colourData
.m_dataColour
.Set(standardColours
[colourSelection
].Red(),
488 standardColours
[colourSelection
].Green(),
489 standardColours
[colourSelection
].Blue());
491 PaintCustomColour(dc
);
492 PaintHighlight(dc
, true);
495 void wxGenericColourDialog::OnCustomColourClick(int which
)
498 PaintHighlight(dc
, false);
500 colourSelection
= which
;
503 redSlider
->SetValue( customColours
[colourSelection
].Red() );
504 greenSlider
->SetValue( customColours
[colourSelection
].Green() );
505 blueSlider
->SetValue( customColours
[colourSelection
].Blue() );
506 #endif // wxUSE_SLIDER
508 colourData
.m_dataColour
.Set(customColours
[colourSelection
].Red(),
509 customColours
[colourSelection
].Green(),
510 customColours
[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
))
534 PaintHighlight(dc
, false);
537 PaintHighlight(dc
, true);
540 customColours
[colourSelection
].Set(colourData
.m_dataColour
.Red(),
541 colourData
.m_dataColour
.Green(),
542 colourData
.m_dataColour
.Blue());
544 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
546 PaintCustomColours(dc
);
551 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
557 colourData
.m_dataColour
.Set((unsigned char)redSlider
->GetValue(), colourData
.m_dataColour
.Green(), colourData
.m_dataColour
.Blue());
558 PaintCustomColour(dc
);
561 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
567 colourData
.m_dataColour
.Set(colourData
.m_dataColour
.Red(), (unsigned char)greenSlider
->GetValue(), colourData
.m_dataColour
.Blue());
568 PaintCustomColour(dc
);
571 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
577 colourData
.m_dataColour
.Set(colourData
.m_dataColour
.Red(), colourData
.m_dataColour
.Green(), (unsigned char)blueSlider
->GetValue());
578 PaintCustomColour(dc
);
581 #endif // wxUSE_SLIDER
583 #endif // wxUSE_COLOURDLG && !defined(__WXGTK20__)