]>
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 #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"
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)) )
150 dialogParent
= parent
;
156 CalculateMeasurements();
162 int wxGenericColourDialog::ShowModal()
164 return wxDialog::ShowModal();
168 // Internal functions
169 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent
& event
)
171 if (event
.ButtonDown(1))
173 int x
= (int)event
.GetX();
174 int y
= (int)event
.GetY();
177 // Handle OS/2's reverse coordinate system and account for the dialog title
180 GetClientSize(NULL
, &nClientHeight
);
181 y
= (nClientHeight
- y
) + 20;
183 if ((x
>= standardColoursRect
.x
&& x
<= (standardColoursRect
.x
+ standardColoursRect
.width
)) &&
184 (y
>= standardColoursRect
.y
&& y
<= (standardColoursRect
.y
+ standardColoursRect
.height
)))
186 int selX
= (int)(x
- standardColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
187 int selY
= (int)(y
- standardColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
188 int ptr
= (int)(selX
+ selY
*8);
189 OnBasicColourClick(ptr
);
191 else if ((x
>= customColoursRect
.x
&& x
<= (customColoursRect
.x
+ customColoursRect
.width
)) &&
192 (y
>= customColoursRect
.y
&& y
<= (customColoursRect
.y
+ customColoursRect
.height
)))
194 int selX
= (int)(x
- customColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
195 int selY
= (int)(y
- customColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
196 int ptr
= (int)(selX
+ selY
*8);
197 OnCustomColourClick(ptr
);
206 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
208 #if !defined(__WXMOTIF__) && !defined(__WXMAC__) && !defined(__WXPM__) && !defined(__WXCOCOA__)
209 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 int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
259 #if defined(__WXMOTIF__)
260 int sliderSpacing
= 65;
261 int sliderHeight
= 160;
263 int sliderSpacing
= 45;
264 int sliderHeight
= 160;
267 redSlider
= new wxSlider(this, wxID_RED_SLIDER
, singleCustomColour
.Red(), 0, 255,
268 wxPoint(sliderX
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
269 greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, singleCustomColour
.Green(), 0, 255,
270 wxPoint(sliderX
+ sliderSpacing
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
271 blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, singleCustomColour
.Blue(), 0, 255,
272 wxPoint(sliderX
+ 2*sliderSpacing
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
274 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
276 // 1) space for explicitly layouted controls
277 topsizer
->Add( sliderX
+ 3*sliderSpacing
, sliderHeight
+25 );
281 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
285 wxSizer
*buttonsizer
= CreateButtonSizer( wxOK
|wxCANCEL
);
286 buttonsizer
->Add( new wxButton(this, wxID_ADD_CUSTOM
, _("Add to custom colours") ), 0, wxLEFT
|wxRIGHT
, 10 );
287 topsizer
->Add( buttonsizer
, 0, wxCENTRE
| wxALL
, 10 );
289 SetAutoLayout( TRUE
);
290 SetSizer( topsizer
);
292 topsizer
->SetSizeHints( this );
293 topsizer
->Fit( this );
300 void wxGenericColourDialog::InitializeColours(void)
304 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
306 wxColour col
= wxTheColourDatabase
->Find(wxColourDialogNames
[i
]);
308 standardColours
[i
].Set(col
.Red(), col
.Green(), col
.Blue());
310 standardColours
[i
].Set(0, 0, 0);
313 for (i
= 0; i
< WXSIZEOF(customColours
); i
++)
315 customColours
[i
] = colourData
.GetCustomColour(i
);
318 wxColour curr
= colourData
.GetColour();
321 bool initColourFound
= FALSE
;
323 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
325 if ( standardColours
[i
] == curr
&& !initColourFound
)
329 initColourFound
= TRUE
;
333 if ( !initColourFound
)
335 for ( i
= 0; i
< WXSIZEOF(customColours
); i
++ )
337 if ( customColours
[i
] == curr
)
345 singleCustomColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
351 singleCustomColour
.Set( 0, 0, 0 );
355 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
360 for (i
= 0; i
< 6; i
++)
363 for (j
= 0; j
< 8; j
++)
367 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
368 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
370 dc
.SetPen(*wxBLACK_PEN
);
371 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
374 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
380 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
385 for (i
= 0; i
< 2; i
++)
388 for (j
= 0; j
< 8; j
++)
392 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
393 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
395 dc
.SetPen(*wxBLACK_PEN
);
397 wxBrush
brush(customColours
[ptr
], wxSOLID
);
400 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
406 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
408 if ( colourSelection
< 0 )
413 // Number of pixels bigger than the standard rectangle size
414 // for drawing a highlight
421 int y
= (int)(colourSelection
/ 8);
422 int x
= (int)(colourSelection
- (y
*8));
424 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
425 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.y
) - deltaY
;
428 dc
.SetPen(*wxBLACK_PEN
);
430 dc
.SetPen(*wxLIGHT_GREY_PEN
);
432 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
433 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
437 // User-defined colours
438 int y
= (int)(colourSelection
/ 8);
439 int x
= (int)(colourSelection
- (y
*8));
441 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
442 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
445 dc
.SetPen(*wxBLACK_PEN
);
447 dc
.SetPen(*wxLIGHT_GREY_PEN
);
449 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
450 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
456 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
460 dc
.SetPen(*wxBLACK_PEN
);
462 wxBrush
*brush
= new wxBrush(singleCustomColour
, wxSOLID
);
465 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
466 customRectangleSize
.x
, customRectangleSize
.y
);
468 dc
.SetBrush(wxNullBrush
);
474 void wxGenericColourDialog::OnBasicColourClick(int which
)
478 PaintHighlight(dc
, FALSE
);
480 colourSelection
= which
;
481 colourData
.SetColour(standardColours
[colourSelection
]);
482 redSlider
->SetValue( standardColours
[colourSelection
].Red() );
483 greenSlider
->SetValue( standardColours
[colourSelection
].Green() );
484 blueSlider
->SetValue( standardColours
[colourSelection
].Blue() );
485 singleCustomColour
.Set(standardColours
[colourSelection
].Red(), standardColours
[colourSelection
].Green(), standardColours
[colourSelection
].Blue());
487 PaintCustomColour(dc
);
488 PaintHighlight(dc
, TRUE
);
491 void wxGenericColourDialog::OnCustomColourClick(int which
)
494 PaintHighlight(dc
, FALSE
);
496 colourSelection
= which
;
497 colourData
.SetColour(customColours
[colourSelection
]);
498 redSlider
->SetValue( customColours
[colourSelection
].Red() );
499 greenSlider
->SetValue( customColours
[colourSelection
].Green() );
500 blueSlider
->SetValue( customColours
[colourSelection
].Blue() );
501 singleCustomColour
.Set(customColours
[colourSelection
].Red(), customColours
[colourSelection
].Green(), customColours
[colourSelection
].Blue());
502 PaintCustomColour(dc
);
503 PaintHighlight(dc
, TRUE
);
507 void wxGenericColourDialog::OnOk(void)
512 void wxGenericColourDialog::OnCancel(void)
514 colourDialogCancelled = TRUE;
519 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
524 PaintHighlight(dc
, FALSE
);
527 PaintHighlight(dc
, TRUE
);
530 customColours
[colourSelection
].Set(singleCustomColour
.Red(), singleCustomColour
.Green(), singleCustomColour
.Blue());
531 colourData
.SetColour(customColours
[colourSelection
]);
532 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
534 PaintCustomColours(dc
);
537 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
543 singleCustomColour
.Set(redSlider
->GetValue(), singleCustomColour
.Green(), singleCustomColour
.Blue());
544 PaintCustomColour(dc
);
547 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
553 singleCustomColour
.Set(singleCustomColour
.Red(), greenSlider
->GetValue(), singleCustomColour
.Blue());
554 PaintCustomColour(dc
);
557 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
563 singleCustomColour
.Set(singleCustomColour
.Red(), singleCustomColour
.Green(), blueSlider
->GetValue());
564 PaintCustomColour(dc
);
567 #endif // wxUSE_COLOURDLG