]>
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"
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()
122 colourSelection
= -1;
125 wxGenericColourDialog::wxGenericColourDialog(wxWindow
*parent
,
129 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(parent
, wxID_ANY
, _("Choose colour"),
145 wxPoint(0,0), wxSize(900, 900)) )
148 dialogParent
= parent
;
154 CalculateMeasurements();
160 int wxGenericColourDialog::ShowModal()
162 return wxDialog::ShowModal();
166 // Internal functions
167 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent
& event
)
169 if (event
.ButtonDown(1))
171 int x
= (int)event
.GetX();
172 int y
= (int)event
.GetY();
175 // Handle OS/2's reverse coordinate system and account for the dialog title
178 GetClientSize(NULL
, &nClientHeight
);
179 y
= (nClientHeight
- y
) + 20;
181 if ((x
>= standardColoursRect
.x
&& x
<= (standardColoursRect
.x
+ standardColoursRect
.width
)) &&
182 (y
>= standardColoursRect
.y
&& y
<= (standardColoursRect
.y
+ standardColoursRect
.height
)))
184 int selX
= (int)(x
- standardColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
185 int selY
= (int)(y
- standardColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
186 int ptr
= (int)(selX
+ selY
*8);
187 OnBasicColourClick(ptr
);
189 else if ((x
>= customColoursRect
.x
&& x
<= (customColoursRect
.x
+ customColoursRect
.width
)) &&
190 (y
>= customColoursRect
.y
&& y
<= (customColoursRect
.y
+ customColoursRect
.height
)))
192 int selX
= (int)(x
- customColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
193 int selY
= (int)(y
- customColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
194 int ptr
= (int)(selX
+ selY
*8);
195 OnCustomColourClick(ptr
);
204 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
206 #if !defined(__WXMOTIF__) && !defined(__WXPM__) && !defined(__WXCOCOA__)
207 wxDialog::OnPaint(event
);
214 PaintBasicColours(dc
);
215 PaintCustomColours(dc
);
216 PaintCustomColour(dc
);
217 PaintHighlight(dc
, true);
220 void wxGenericColourDialog::CalculateMeasurements()
222 smallRectangleSize
.x
= 18;
223 smallRectangleSize
.y
= 14;
224 customRectangleSize
.x
= 40;
225 customRectangleSize
.y
= 40;
230 standardColoursRect
.x
= 10;
232 standardColoursRect
.y
= 15 + 20; /* OS/2 needs to account for dialog titlebar */
234 standardColoursRect
.y
= 15;
236 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
237 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
239 customColoursRect
.x
= standardColoursRect
.x
;
240 customColoursRect
.y
= standardColoursRect
.y
+ standardColoursRect
.height
+ 20;
241 customColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
242 customColoursRect
.height
= (2*smallRectangleSize
.y
) + (1*gridSpacing
);
244 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
245 singleCustomColourRect
.y
= 80;
246 singleCustomColourRect
.width
= customRectangleSize
.x
;
247 singleCustomColourRect
.height
= customRectangleSize
.y
;
250 customButtonX
= singleCustomColourRect
.x
;
251 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
254 void wxGenericColourDialog::CreateWidgets()
258 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
260 const int sliderHeight
= 160;
263 const int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
265 redSlider
= new wxSlider(this, wxID_RED_SLIDER
, colourData
.m_dataColour
.Red(), 0, 255,
266 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
267 greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, colourData
.m_dataColour
.Green(), 0, 255,
268 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
269 blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, colourData
.m_dataColour
.Blue(), 0, 255,
270 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
272 wxBoxSizer
*sliderSizer
= new wxBoxSizer( wxHORIZONTAL
);
274 // 1) space for sliders
275 sliderSizer
->Add( sliderX
, sliderHeight
);
276 sliderSizer
->Add( redSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
277 sliderSizer
->Add( greenSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
278 sliderSizer
->Add( blueSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
280 topSizer
->Add( sliderSizer
, 0, wxCENTRE
| wxALL
, 10 );
282 topSizer
->Add( 1, sliderHeight
, 0, wxCENTRE
| wxALL
, 15 );
283 #endif // wxUSE_SLIDER
287 topSizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
291 wxSizer
*buttonsizer
= CreateButtonSizer( wxOK
|wxCANCEL
);
292 buttonsizer
->Add( new wxButton(this, wxID_ADD_CUSTOM
, _("Add to custom colours") ), 0, wxLEFT
|wxRIGHT
, 10 );
293 topSizer
->Add( buttonsizer
, 0, wxEXPAND
| wxALL
, 10 );
295 SetAutoLayout( true );
296 SetSizer( topSizer
);
298 topSizer
->SetSizeHints( this );
299 topSizer
->Fit( this );
306 void wxGenericColourDialog::InitializeColours(void)
310 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
312 wxColour col
= wxTheColourDatabase
->Find(wxColourDialogNames
[i
]);
314 standardColours
[i
].Set(col
.Red(), col
.Green(), col
.Blue());
316 standardColours
[i
].Set(0, 0, 0);
319 for (i
= 0; i
< WXSIZEOF(customColours
); i
++)
321 wxColour c
= colourData
.GetCustomColour(i
);
323 customColours
[i
] = colourData
.GetCustomColour(i
);
325 customColours
[i
] = wxColour(255, 255, 255);
328 wxColour curr
= colourData
.GetColour();
331 bool initColourFound
= false;
333 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
335 if ( standardColours
[i
] == curr
&& !initColourFound
)
339 initColourFound
= true;
343 if ( !initColourFound
)
345 for ( i
= 0; i
< WXSIZEOF(customColours
); i
++ )
347 if ( customColours
[i
] == curr
)
355 colourData
.m_dataColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
361 colourData
.m_dataColour
.Set( 0, 0, 0 );
365 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
368 for (i
= 0; i
< 6; i
++)
371 for (j
= 0; j
< 8; j
++)
375 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
376 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
378 dc
.SetPen(*wxBLACK_PEN
);
379 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
382 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
387 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
390 for (i
= 0; i
< 2; i
++)
393 for (j
= 0; j
< 8; j
++)
397 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
398 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
400 dc
.SetPen(*wxBLACK_PEN
);
402 wxBrush
brush(customColours
[ptr
], wxSOLID
);
405 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
410 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
412 if ( colourSelection
< 0 )
415 // Number of pixels bigger than the standard rectangle size
416 // for drawing a highlight
423 int y
= (int)(colourSelection
/ 8);
424 int x
= (int)(colourSelection
- (y
*8));
426 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
427 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.y
) - deltaY
;
430 dc
.SetPen(*wxBLACK_PEN
);
432 dc
.SetPen(*wxLIGHT_GREY_PEN
);
434 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
435 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
439 // User-defined colours
440 int y
= (int)(colourSelection
/ 8);
441 int x
= (int)(colourSelection
- (y
*8));
443 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
444 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
447 dc
.SetPen(*wxBLACK_PEN
);
449 dc
.SetPen(*wxLIGHT_GREY_PEN
);
451 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
452 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
456 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
458 dc
.SetPen(*wxBLACK_PEN
);
460 wxBrush
*brush
= new wxBrush(colourData
.m_dataColour
, wxSOLID
);
463 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
464 customRectangleSize
.x
, customRectangleSize
.y
);
466 dc
.SetBrush(wxNullBrush
);
470 void wxGenericColourDialog::OnBasicColourClick(int which
)
474 PaintHighlight(dc
, false);
476 colourSelection
= which
;
479 redSlider
->SetValue( standardColours
[colourSelection
].Red() );
480 greenSlider
->SetValue( standardColours
[colourSelection
].Green() );
481 blueSlider
->SetValue( standardColours
[colourSelection
].Blue() );
482 #endif // wxUSE_SLIDER
484 colourData
.m_dataColour
.Set(standardColours
[colourSelection
].Red(),
485 standardColours
[colourSelection
].Green(),
486 standardColours
[colourSelection
].Blue());
488 PaintCustomColour(dc
);
489 PaintHighlight(dc
, true);
492 void wxGenericColourDialog::OnCustomColourClick(int which
)
495 PaintHighlight(dc
, false);
497 colourSelection
= which
;
500 redSlider
->SetValue( customColours
[colourSelection
].Red() );
501 greenSlider
->SetValue( customColours
[colourSelection
].Green() );
502 blueSlider
->SetValue( customColours
[colourSelection
].Blue() );
503 #endif // wxUSE_SLIDER
505 colourData
.m_dataColour
.Set(customColours
[colourSelection
].Red(),
506 customColours
[colourSelection
].Green(),
507 customColours
[colourSelection
].Blue());
509 PaintCustomColour(dc
);
510 PaintHighlight(dc
, true);
514 void wxGenericColourDialog::OnOk(void)
519 void wxGenericColourDialog::OnCancel(void)
521 colourDialogCancelled = true;
526 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
531 PaintHighlight(dc
, false);
534 PaintHighlight(dc
, true);
537 customColours
[colourSelection
].Set(colourData
.m_dataColour
.Red(),
538 colourData
.m_dataColour
.Green(),
539 colourData
.m_dataColour
.Blue());
541 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
543 PaintCustomColours(dc
);
548 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
554 colourData
.m_dataColour
.Set((unsigned char)redSlider
->GetValue(), colourData
.m_dataColour
.Green(), colourData
.m_dataColour
.Blue());
555 PaintCustomColour(dc
);
558 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
564 colourData
.m_dataColour
.Set(colourData
.m_dataColour
.Red(), (unsigned char)greenSlider
->GetValue(), colourData
.m_dataColour
.Blue());
565 PaintCustomColour(dc
);
568 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
574 colourData
.m_dataColour
.Set(colourData
.m_dataColour
.Red(), colourData
.m_dataColour
.Green(), (unsigned char)blueSlider
->GetValue());
575 PaintCustomColour(dc
);
578 #endif // wxUSE_SLIDER
580 #endif // wxUSE_COLOURDLG && !defined(__WXGTK20__)