]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/colrdlgg.cpp
98d59a9ca0a82e2eafe1a50751e73f448b14e982
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "colrdlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #if wxUSE_COLOURDLG && (!defined(__WXGTK20__) || defined(__WXUNIVERSAL__))
28 #include "wx/dialog.h"
29 #include "wx/listbox.h"
30 #include "wx/button.h"
31 #include "wx/stattext.h"
32 #include "wx/layout.h"
33 #include "wx/dcclient.h"
34 #include "wx/slider.h"
39 #include "wx/statline.h"
42 #include "wx/generic/colrdlgg.h"
44 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourDialog
, wxDialog
)
46 BEGIN_EVENT_TABLE(wxGenericColourDialog
, wxDialog
)
47 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
)
51 EVT_PAINT(wxGenericColourDialog::OnPaint
)
52 EVT_MOUSE_EVENTS(wxGenericColourDialog::OnMouseEvent
)
53 EVT_CLOSE(wxGenericColourDialog::OnCloseWindow
)
58 * Generic wxColourDialog
61 // don't change the number of elements (48) in this array, the code below is
62 // hardcoded to use it
63 static const wxChar
*wxColourDialogNames
[] =
71 wxT("MEDIUM VIOLET RED"),
76 wxT("MEDIUM SPRING GREEN"),
79 wxT("LIGHT STEEL BLUE"),
99 wxT("MEDIUM VIOLET RED"),
103 wxT("MEDIUM SEA GREEN"),
105 wxT("MIDNIGHT BLUE"),
111 wxT("MEDIUM FOREST GREEN"),
116 wxT("MEDIUM SLATE BLUE"),
120 wxGenericColourDialog::wxGenericColourDialog()
124 colourSelection
= -1;
127 wxGenericColourDialog::wxGenericColourDialog(wxWindow
*parent
,
131 colourSelection
= -1;
132 Create(parent
, data
);
135 wxGenericColourDialog::~wxGenericColourDialog()
139 void wxGenericColourDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
141 EndModal(wxID_CANCEL
);
144 bool wxGenericColourDialog::Create(wxWindow
*parent
, wxColourData
*data
)
146 if ( !wxDialog::Create(parent
, wxID_ANY
, _("Choose colour"),
147 wxPoint(0, 0), wxSize(900, 900)) )
150 dialogParent
= parent
;
156 CalculateMeasurements();
162 int wxGenericColourDialog::ShowModal()
164 int nResult
= wxDialog::ShowModal();
165 colourData
.SetColour(singleCustomColour
);
170 // Internal functions
171 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent
& event
)
173 if (event
.ButtonDown(1))
175 int x
= (int)event
.GetX();
176 int y
= (int)event
.GetY();
179 // Handle OS/2's reverse coordinate system and account for the dialog title
182 GetClientSize(NULL
, &nClientHeight
);
183 y
= (nClientHeight
- y
) + 20;
185 if ((x
>= standardColoursRect
.x
&& x
<= (standardColoursRect
.x
+ standardColoursRect
.width
)) &&
186 (y
>= standardColoursRect
.y
&& y
<= (standardColoursRect
.y
+ standardColoursRect
.height
)))
188 int selX
= (int)(x
- standardColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
189 int selY
= (int)(y
- standardColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
190 int ptr
= (int)(selX
+ selY
*8);
191 OnBasicColourClick(ptr
);
193 else if ((x
>= customColoursRect
.x
&& x
<= (customColoursRect
.x
+ customColoursRect
.width
)) &&
194 (y
>= customColoursRect
.y
&& y
<= (customColoursRect
.y
+ customColoursRect
.height
)))
196 int selX
= (int)(x
- customColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
197 int selY
= (int)(y
- customColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
198 int ptr
= (int)(selX
+ selY
*8);
199 OnCustomColourClick(ptr
);
208 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
210 #if !defined(__WXMOTIF__) && !defined(__WXMAC__) && !defined(__WXPM__) && !defined(__WXCOCOA__)
211 wxDialog::OnPaint(event
);
216 PaintBasicColours(dc
);
217 PaintCustomColours(dc
);
218 PaintCustomColour(dc
);
219 PaintHighlight(dc
, true);
222 void wxGenericColourDialog::CalculateMeasurements()
224 smallRectangleSize
.x
= 18;
225 smallRectangleSize
.y
= 14;
226 customRectangleSize
.x
= 40;
227 customRectangleSize
.y
= 40;
232 standardColoursRect
.x
= 10;
234 standardColoursRect
.y
= 15 + 20; /* OS/2 needs to account for dialog titlebar */
236 standardColoursRect
.y
= 15;
238 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
239 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
241 customColoursRect
.x
= standardColoursRect
.x
;
242 customColoursRect
.y
= standardColoursRect
.y
+ standardColoursRect
.height
+ 20;
243 customColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
244 customColoursRect
.height
= (2*smallRectangleSize
.y
) + (1*gridSpacing
);
246 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
247 singleCustomColourRect
.y
= 80;
248 singleCustomColourRect
.width
= customRectangleSize
.x
;
249 singleCustomColourRect
.height
= customRectangleSize
.y
;
252 customButtonX
= singleCustomColourRect
.x
;
253 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
256 void wxGenericColourDialog::CreateWidgets()
260 const int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
261 const int sliderHeight
= 160;
263 redSlider
= new wxSlider(this, wxID_RED_SLIDER
, singleCustomColour
.Red(), 0, 255,
264 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
265 greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, singleCustomColour
.Green(), 0, 255,
266 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
267 blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, singleCustomColour
.Blue(), 0, 255,
268 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
270 wxBoxSizer
*sliderSizer
= new wxBoxSizer( wxHORIZONTAL
);
272 // 1) space for sliders
273 sliderSizer
->Add( sliderX
, sliderHeight
);
274 sliderSizer
->Add( redSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
275 sliderSizer
->Add( greenSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
276 sliderSizer
->Add( blueSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
278 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
280 topSizer
->Add( sliderSizer
, 0, wxCENTRE
| wxALL
, 10 );
284 topSizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
288 wxSizer
*buttonsizer
= CreateButtonSizer( wxOK
|wxCANCEL
);
289 buttonsizer
->Add( new wxButton(this, wxID_ADD_CUSTOM
, _("Add to custom colours") ), 0, wxLEFT
|wxRIGHT
, 10 );
290 topSizer
->Add( buttonsizer
, 0, wxCENTRE
| wxALL
, 10 );
292 SetAutoLayout( true );
293 SetSizer( topSizer
);
295 topSizer
->SetSizeHints( this );
296 topSizer
->Fit( this );
303 void wxGenericColourDialog::InitializeColours(void)
307 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
309 wxColour col
= wxTheColourDatabase
->Find(wxColourDialogNames
[i
]);
311 standardColours
[i
].Set(col
.Red(), col
.Green(), col
.Blue());
313 standardColours
[i
].Set(0, 0, 0);
316 for (i
= 0; i
< WXSIZEOF(customColours
); i
++)
318 wxColour c
= colourData
.GetCustomColour(i
);
320 customColours
[i
] = colourData
.GetCustomColour(i
);
322 customColours
[i
] = wxColour(255, 255, 255);
325 wxColour curr
= colourData
.GetColour();
328 bool initColourFound
= false;
330 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
332 if ( standardColours
[i
] == curr
&& !initColourFound
)
336 initColourFound
= true;
340 if ( !initColourFound
)
342 for ( i
= 0; i
< WXSIZEOF(customColours
); i
++ )
344 if ( customColours
[i
] == curr
)
352 singleCustomColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
358 singleCustomColour
.Set( 0, 0, 0 );
362 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
367 for (i
= 0; i
< 6; i
++)
370 for (j
= 0; j
< 8; j
++)
374 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
375 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
377 dc
.SetPen(*wxBLACK_PEN
);
378 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
381 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
387 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
392 for (i
= 0; i
< 2; i
++)
395 for (j
= 0; j
< 8; j
++)
399 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
400 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
402 dc
.SetPen(*wxBLACK_PEN
);
404 wxBrush
brush(customColours
[ptr
], wxSOLID
);
407 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
413 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
415 if ( colourSelection
< 0 )
420 // Number of pixels bigger than the standard rectangle size
421 // for drawing a highlight
428 int y
= (int)(colourSelection
/ 8);
429 int x
= (int)(colourSelection
- (y
*8));
431 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
432 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.y
) - deltaY
;
435 dc
.SetPen(*wxBLACK_PEN
);
437 dc
.SetPen(*wxLIGHT_GREY_PEN
);
439 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
440 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
444 // User-defined colours
445 int y
= (int)(colourSelection
/ 8);
446 int x
= (int)(colourSelection
- (y
*8));
448 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
449 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
452 dc
.SetPen(*wxBLACK_PEN
);
454 dc
.SetPen(*wxLIGHT_GREY_PEN
);
456 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
457 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
463 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
467 dc
.SetPen(*wxBLACK_PEN
);
469 wxBrush
*brush
= new wxBrush(singleCustomColour
, wxSOLID
);
472 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
473 customRectangleSize
.x
, customRectangleSize
.y
);
475 dc
.SetBrush(wxNullBrush
);
481 void wxGenericColourDialog::OnBasicColourClick(int which
)
485 PaintHighlight(dc
, false);
487 colourSelection
= which
;
488 redSlider
->SetValue( standardColours
[colourSelection
].Red() );
489 greenSlider
->SetValue( standardColours
[colourSelection
].Green() );
490 blueSlider
->SetValue( standardColours
[colourSelection
].Blue() );
491 singleCustomColour
.Set(standardColours
[colourSelection
].Red(), standardColours
[colourSelection
].Green(), standardColours
[colourSelection
].Blue());
493 PaintCustomColour(dc
);
494 PaintHighlight(dc
, true);
497 void wxGenericColourDialog::OnCustomColourClick(int which
)
500 PaintHighlight(dc
, false);
502 colourSelection
= which
;
503 redSlider
->SetValue( customColours
[colourSelection
].Red() );
504 greenSlider
->SetValue( customColours
[colourSelection
].Green() );
505 blueSlider
->SetValue( customColours
[colourSelection
].Blue() );
506 singleCustomColour
.Set(customColours
[colourSelection
].Red(), customColours
[colourSelection
].Green(), customColours
[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
))
529 PaintHighlight(dc
, false);
532 PaintHighlight(dc
, true);
535 customColours
[colourSelection
].Set(singleCustomColour
.Red(), singleCustomColour
.Green(), singleCustomColour
.Blue());
536 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
538 PaintCustomColours(dc
);
541 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
547 singleCustomColour
.Set((unsigned char)redSlider
->GetValue(), singleCustomColour
.Green(), singleCustomColour
.Blue());
548 PaintCustomColour(dc
);
551 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
557 singleCustomColour
.Set(singleCustomColour
.Red(), (unsigned char)greenSlider
->GetValue(), singleCustomColour
.Blue());
558 PaintCustomColour(dc
);
561 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
567 singleCustomColour
.Set(singleCustomColour
.Red(), singleCustomColour
.Green(), (unsigned char)blueSlider
->GetValue());
568 PaintCustomColour(dc
);
571 #endif // wxUSE_COLOURDLG && !defined(__WXGTK20__)