]>
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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "colrdlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
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
, -1, wxT("Colour"),
147 wxPoint(0, 0), wxSize(900, 900),
148 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
) )
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();
177 if ((x
>= standardColoursRect
.x
&& x
<= (standardColoursRect
.x
+ standardColoursRect
.width
)) &&
178 (y
>= standardColoursRect
.y
&& y
<= (standardColoursRect
.y
+ standardColoursRect
.height
)))
180 int selX
= (int)(x
- standardColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
181 int selY
= (int)(y
- standardColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
182 int ptr
= (int)(selX
+ selY
*8);
183 OnBasicColourClick(ptr
);
185 else if ((x
>= customColoursRect
.x
&& x
<= (customColoursRect
.x
+ customColoursRect
.width
)) &&
186 (y
>= customColoursRect
.y
&& y
<= (customColoursRect
.y
+ customColoursRect
.height
)))
188 int selX
= (int)(x
- customColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
189 int selY
= (int)(y
- customColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
190 int ptr
= (int)(selX
+ selY
*8);
191 OnCustomColourClick(ptr
);
200 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
202 #if !defined(__WXMOTIF__) && !defined(__WXMAC__) && !defined(__WXPM__)
203 wxDialog::OnPaint(event
);
208 PaintBasicColours(dc
);
209 PaintCustomColours(dc
);
210 PaintCustomColour(dc
);
211 PaintHighlight(dc
, TRUE
);
214 void wxGenericColourDialog::CalculateMeasurements()
216 smallRectangleSize
.x
= 18;
217 smallRectangleSize
.y
= 14;
218 customRectangleSize
.x
= 40;
219 customRectangleSize
.y
= 40;
224 standardColoursRect
.x
= 10;
225 standardColoursRect
.y
= 15;
226 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
227 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
229 customColoursRect
.x
= standardColoursRect
.x
;
230 customColoursRect
.y
= standardColoursRect
.y
+ standardColoursRect
.height
+ 20;
231 customColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
232 customColoursRect
.height
= (2*smallRectangleSize
.y
) + (1*gridSpacing
);
234 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
235 singleCustomColourRect
.y
= 80;
236 singleCustomColourRect
.width
= customRectangleSize
.x
;
237 singleCustomColourRect
.height
= customRectangleSize
.y
;
240 customButtonX
= singleCustomColourRect
.x
;
241 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
244 void wxGenericColourDialog::CreateWidgets()
248 int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
249 #if defined(__WXMOTIF__)
250 int sliderSpacing
= 65;
251 int sliderHeight
= 160;
253 int sliderSpacing
= 45;
254 int sliderHeight
= 160;
257 redSlider
= new wxSlider(this, wxID_RED_SLIDER
, singleCustomColour
.Red(), 0, 255,
258 wxPoint(sliderX
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
259 greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, singleCustomColour
.Green(), 0, 255,
260 wxPoint(sliderX
+ sliderSpacing
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
261 blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, singleCustomColour
.Blue(), 0, 255,
262 wxPoint(sliderX
+ 2*sliderSpacing
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
264 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
266 // 1) space for explicitly layouted controls
267 topsizer
->Add( sliderX
+ 3*sliderSpacing
, sliderHeight
+25 );
271 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
275 wxSizer
*buttonsizer
= CreateButtonSizer( wxOK
|wxCANCEL
);
276 buttonsizer
->Add( new wxButton(this, wxID_ADD_CUSTOM
, _("Add to custom colours") ), 0, wxLEFT
|wxRIGHT
, 10 );
277 topsizer
->Add( buttonsizer
, 0, wxCENTRE
| wxALL
, 10 );
279 SetAutoLayout( TRUE
);
280 SetSizer( topsizer
);
282 topsizer
->SetSizeHints( this );
283 topsizer
->Fit( this );
290 void wxGenericColourDialog::InitializeColours(void)
294 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
296 wxColour
*col
= wxTheColourDatabase
->FindColour(wxColourDialogNames
[i
]);
298 standardColours
[i
].Set(col
->Red(), col
->Green(), col
->Blue());
300 standardColours
[i
].Set(0, 0, 0);
303 for (i
= 0; i
< 16; i
++)
305 customColours
[i
] = colourData
.GetCustomColour(i
);
308 wxColour curr
= colourData
.GetColour();
311 bool initColourFound
= FALSE
;
313 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
315 if ( standardColours
[i
] == curr
&& !initColourFound
)
319 initColourFound
= TRUE
;
323 if ( !initColourFound
)
325 for ( i
= 0; i
< 16; i
++ )
327 if ( customColours
[i
] == curr
)
331 initColourFound
= TRUE
;
336 singleCustomColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
342 singleCustomColour
.Set( 0, 0, 0 );
346 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
351 for (i
= 0; i
< 6; i
++)
354 for (j
= 0; j
< 8; j
++)
358 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
359 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
361 dc
.SetPen(*wxBLACK_PEN
);
362 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
365 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
371 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
376 for (i
= 0; i
< 2; i
++)
379 for (j
= 0; j
< 8; j
++)
383 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
384 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
386 dc
.SetPen(*wxBLACK_PEN
);
388 wxBrush
brush(customColours
[ptr
], wxSOLID
);
391 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
397 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
399 if ( colourSelection
< 0 )
404 // Number of pixels bigger than the standard rectangle size
405 // for drawing a highlight
412 int y
= (int)(colourSelection
/ 8);
413 int x
= (int)(colourSelection
- (y
*8));
415 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
416 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.y
) - deltaY
;
419 dc
.SetPen(*wxBLACK_PEN
);
421 dc
.SetPen(*wxLIGHT_GREY_PEN
);
423 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
424 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
428 // User-defined colours
429 int y
= (int)(colourSelection
/ 8);
430 int x
= (int)(colourSelection
- (y
*8));
432 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
433 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
436 dc
.SetPen(*wxBLACK_PEN
);
438 dc
.SetPen(*wxLIGHT_GREY_PEN
);
440 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
441 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
447 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
451 dc
.SetPen(*wxBLACK_PEN
);
453 wxBrush
*brush
= new wxBrush(singleCustomColour
, wxSOLID
);
456 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
457 customRectangleSize
.x
, customRectangleSize
.y
);
459 dc
.SetBrush(wxNullBrush
);
465 void wxGenericColourDialog::OnBasicColourClick(int which
)
469 PaintHighlight(dc
, FALSE
);
471 colourSelection
= which
;
472 colourData
.SetColour(standardColours
[colourSelection
]);
473 redSlider
->SetValue( standardColours
[colourSelection
].Red() );
474 greenSlider
->SetValue( standardColours
[colourSelection
].Green() );
475 blueSlider
->SetValue( standardColours
[colourSelection
].Blue() );
476 singleCustomColour
.Set(standardColours
[colourSelection
].Red(), standardColours
[colourSelection
].Green(), standardColours
[colourSelection
].Blue());
478 PaintCustomColour(dc
);
479 PaintHighlight(dc
, TRUE
);
482 void wxGenericColourDialog::OnCustomColourClick(int which
)
485 PaintHighlight(dc
, FALSE
);
487 colourSelection
= which
;
488 colourData
.SetColour(customColours
[colourSelection
]);
489 redSlider
->SetValue( customColours
[colourSelection
].Red() );
490 greenSlider
->SetValue( customColours
[colourSelection
].Green() );
491 blueSlider
->SetValue( customColours
[colourSelection
].Blue() );
492 singleCustomColour
.Set(customColours
[colourSelection
].Red(), customColours
[colourSelection
].Green(), customColours
[colourSelection
].Blue());
493 PaintCustomColour(dc
);
494 PaintHighlight(dc
, TRUE
);
498 void wxGenericColourDialog::OnOk(void)
503 void wxGenericColourDialog::OnCancel(void)
505 colourDialogCancelled = TRUE;
510 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
515 PaintHighlight(dc
, FALSE
);
518 PaintHighlight(dc
, TRUE
);
521 customColours
[colourSelection
].Set(singleCustomColour
.Red(), singleCustomColour
.Green(), singleCustomColour
.Blue());
522 colourData
.SetColour(customColours
[colourSelection
]);
523 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
525 PaintCustomColours(dc
);
528 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
534 singleCustomColour
.Set(redSlider
->GetValue(), singleCustomColour
.Green(), singleCustomColour
.Blue());
535 PaintCustomColour(dc
);
538 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
544 singleCustomColour
.Set(singleCustomColour
.Red(), greenSlider
->GetValue(), singleCustomColour
.Blue());
545 PaintCustomColour(dc
);
548 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
554 singleCustomColour
.Set(singleCustomColour
.Red(), singleCustomColour
.Green(), blueSlider
->GetValue());
555 PaintCustomColour(dc
);
558 #endif // wxUSE_COLOURDLG