]>
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"
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"
38 #include "wx/statline.h"
42 #include "wx/slider.h"
45 #include "wx/generic/colrdlgg.h"
47 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourDialog
, wxDialog
)
49 BEGIN_EVENT_TABLE(wxGenericColourDialog
, wxDialog
)
50 EVT_BUTTON(wxID_ADD_CUSTOM
, wxGenericColourDialog::OnAddCustom
)
52 EVT_SLIDER(wxID_RED_SLIDER
, wxGenericColourDialog::OnRedSlider
)
53 EVT_SLIDER(wxID_GREEN_SLIDER
, wxGenericColourDialog::OnGreenSlider
)
54 EVT_SLIDER(wxID_BLUE_SLIDER
, wxGenericColourDialog::OnBlueSlider
)
56 EVT_PAINT(wxGenericColourDialog::OnPaint
)
57 EVT_MOUSE_EVENTS(wxGenericColourDialog::OnMouseEvent
)
58 EVT_CLOSE(wxGenericColourDialog::OnCloseWindow
)
63 * Generic wxColourDialog
66 // don't change the number of elements (48) in this array, the code below is
67 // hardcoded to use it
68 static const wxChar
*wxColourDialogNames
[] =
76 wxT("MEDIUM VIOLET RED"),
81 wxT("MEDIUM SPRING GREEN"),
84 wxT("LIGHT STEEL BLUE"),
104 wxT("MEDIUM VIOLET RED"),
108 wxT("MEDIUM SEA GREEN"),
110 wxT("MIDNIGHT BLUE"),
116 wxT("MEDIUM FOREST GREEN"),
121 wxT("MEDIUM SLATE BLUE"),
125 wxGenericColourDialog::wxGenericColourDialog()
129 colourSelection
= -1;
132 wxGenericColourDialog::wxGenericColourDialog(wxWindow
*parent
,
136 colourSelection
= -1;
137 Create(parent
, data
);
140 wxGenericColourDialog::~wxGenericColourDialog()
144 void wxGenericColourDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
146 EndModal(wxID_CANCEL
);
149 bool wxGenericColourDialog::Create(wxWindow
*parent
, wxColourData
*data
)
151 if ( !wxDialog::Create(parent
, wxID_ANY
, _("Choose colour"),
152 wxPoint(0,0), wxSize(900, 900)) )
155 dialogParent
= parent
;
161 CalculateMeasurements();
167 int wxGenericColourDialog::ShowModal()
169 return wxDialog::ShowModal();
173 // Internal functions
174 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent
& event
)
176 if (event
.ButtonDown(1))
178 int x
= (int)event
.GetX();
179 int y
= (int)event
.GetY();
182 // Handle OS/2's reverse coordinate system and account for the dialog title
185 GetClientSize(NULL
, &nClientHeight
);
186 y
= (nClientHeight
- y
) + 20;
188 if ((x
>= standardColoursRect
.x
&& x
<= (standardColoursRect
.x
+ standardColoursRect
.width
)) &&
189 (y
>= standardColoursRect
.y
&& y
<= (standardColoursRect
.y
+ standardColoursRect
.height
)))
191 int selX
= (int)(x
- standardColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
192 int selY
= (int)(y
- standardColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
193 int ptr
= (int)(selX
+ selY
*8);
194 OnBasicColourClick(ptr
);
196 else if ((x
>= customColoursRect
.x
&& x
<= (customColoursRect
.x
+ customColoursRect
.width
)) &&
197 (y
>= customColoursRect
.y
&& y
<= (customColoursRect
.y
+ customColoursRect
.height
)))
199 int selX
= (int)(x
- customColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
200 int selY
= (int)(y
- customColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
201 int ptr
= (int)(selX
+ selY
*8);
202 OnCustomColourClick(ptr
);
211 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
213 #if !defined(__WXMOTIF__) && !defined(__WXPM__) && !defined(__WXCOCOA__)
214 wxDialog::OnPaint(event
);
221 PaintBasicColours(dc
);
222 PaintCustomColours(dc
);
223 PaintCustomColour(dc
);
224 PaintHighlight(dc
, true);
227 void wxGenericColourDialog::CalculateMeasurements()
229 smallRectangleSize
.x
= 18;
230 smallRectangleSize
.y
= 14;
231 customRectangleSize
.x
= 40;
232 customRectangleSize
.y
= 40;
237 standardColoursRect
.x
= 10;
239 standardColoursRect
.y
= 15 + 20; /* OS/2 needs to account for dialog titlebar */
241 standardColoursRect
.y
= 15;
243 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
244 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
246 customColoursRect
.x
= standardColoursRect
.x
;
247 customColoursRect
.y
= standardColoursRect
.y
+ standardColoursRect
.height
+ 20;
248 customColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
249 customColoursRect
.height
= (2*smallRectangleSize
.y
) + (1*gridSpacing
);
251 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
252 singleCustomColourRect
.y
= 80;
253 singleCustomColourRect
.width
= customRectangleSize
.x
;
254 singleCustomColourRect
.height
= customRectangleSize
.y
;
257 customButtonX
= singleCustomColourRect
.x
;
258 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
261 void wxGenericColourDialog::CreateWidgets()
265 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
267 const int sliderHeight
= 160;
270 const int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
272 redSlider
= new wxSlider(this, wxID_RED_SLIDER
, colourData
.m_dataColour
.Red(), 0, 255,
273 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
274 greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, colourData
.m_dataColour
.Green(), 0, 255,
275 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
276 blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, colourData
.m_dataColour
.Blue(), 0, 255,
277 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
);
279 wxBoxSizer
*sliderSizer
= new wxBoxSizer( wxHORIZONTAL
);
281 // 1) space for sliders
282 sliderSizer
->Add( sliderX
, sliderHeight
);
283 sliderSizer
->Add( redSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
284 sliderSizer
->Add( greenSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
285 sliderSizer
->Add( blueSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
287 topSizer
->Add( sliderSizer
, 0, wxCENTRE
| wxALL
, 10 );
289 topSizer
->Add( 1, sliderHeight
, 0, wxCENTRE
| wxALL
, 15 );
290 #endif // wxUSE_SLIDER
294 topSizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
298 wxSizer
*buttonsizer
= CreateButtonSizer( wxOK
|wxCANCEL
);
299 buttonsizer
->Add( new wxButton(this, wxID_ADD_CUSTOM
, _("Add to custom colours") ), 0, wxLEFT
|wxRIGHT
, 10 );
300 topSizer
->Add( buttonsizer
, 0, wxEXPAND
| wxALL
, 10 );
302 SetAutoLayout( true );
303 SetSizer( topSizer
);
305 topSizer
->SetSizeHints( this );
306 topSizer
->Fit( this );
313 void wxGenericColourDialog::InitializeColours(void)
317 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
319 wxColour col
= wxTheColourDatabase
->Find(wxColourDialogNames
[i
]);
321 standardColours
[i
].Set(col
.Red(), col
.Green(), col
.Blue());
323 standardColours
[i
].Set(0, 0, 0);
326 for (i
= 0; i
< WXSIZEOF(customColours
); i
++)
328 wxColour c
= colourData
.GetCustomColour(i
);
330 customColours
[i
] = colourData
.GetCustomColour(i
);
332 customColours
[i
] = wxColour(255, 255, 255);
335 wxColour curr
= colourData
.GetColour();
338 bool initColourFound
= false;
340 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
342 if ( standardColours
[i
] == curr
&& !initColourFound
)
346 initColourFound
= true;
350 if ( !initColourFound
)
352 for ( i
= 0; i
< WXSIZEOF(customColours
); i
++ )
354 if ( customColours
[i
] == curr
)
362 colourData
.m_dataColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
368 colourData
.m_dataColour
.Set( 0, 0, 0 );
372 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
377 for (i
= 0; i
< 6; i
++)
380 for (j
= 0; j
< 8; j
++)
384 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
385 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
387 dc
.SetPen(*wxBLACK_PEN
);
388 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
391 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
397 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
402 for (i
= 0; i
< 2; i
++)
405 for (j
= 0; j
< 8; j
++)
409 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
410 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
412 dc
.SetPen(*wxBLACK_PEN
);
414 wxBrush
brush(customColours
[ptr
], wxSOLID
);
417 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
423 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
425 if ( colourSelection
< 0 )
430 // Number of pixels bigger than the standard rectangle size
431 // for drawing a highlight
438 int y
= (int)(colourSelection
/ 8);
439 int x
= (int)(colourSelection
- (y
*8));
441 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
442 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.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
)));
454 // User-defined colours
455 int y
= (int)(colourSelection
/ 8);
456 int x
= (int)(colourSelection
- (y
*8));
458 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
459 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
462 dc
.SetPen(*wxBLACK_PEN
);
464 dc
.SetPen(*wxLIGHT_GREY_PEN
);
466 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
467 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
473 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
477 dc
.SetPen(*wxBLACK_PEN
);
479 wxBrush
*brush
= new wxBrush(colourData
.m_dataColour
, wxSOLID
);
482 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
483 customRectangleSize
.x
, customRectangleSize
.y
);
485 dc
.SetBrush(wxNullBrush
);
491 void wxGenericColourDialog::OnBasicColourClick(int which
)
495 PaintHighlight(dc
, false);
497 colourSelection
= which
;
500 redSlider
->SetValue( standardColours
[colourSelection
].Red() );
501 greenSlider
->SetValue( standardColours
[colourSelection
].Green() );
502 blueSlider
->SetValue( standardColours
[colourSelection
].Blue() );
503 #endif // wxUSE_SLIDER
505 colourData
.m_dataColour
.Set(standardColours
[colourSelection
].Red(),
506 standardColours
[colourSelection
].Green(),
507 standardColours
[colourSelection
].Blue());
509 PaintCustomColour(dc
);
510 PaintHighlight(dc
, true);
513 void wxGenericColourDialog::OnCustomColourClick(int which
)
516 PaintHighlight(dc
, false);
518 colourSelection
= which
;
521 redSlider
->SetValue( customColours
[colourSelection
].Red() );
522 greenSlider
->SetValue( customColours
[colourSelection
].Green() );
523 blueSlider
->SetValue( customColours
[colourSelection
].Blue() );
524 #endif // wxUSE_SLIDER
526 colourData
.m_dataColour
.Set(customColours
[colourSelection
].Red(),
527 customColours
[colourSelection
].Green(),
528 customColours
[colourSelection
].Blue());
530 PaintCustomColour(dc
);
531 PaintHighlight(dc
, true);
535 void wxGenericColourDialog::OnOk(void)
540 void wxGenericColourDialog::OnCancel(void)
542 colourDialogCancelled = true;
547 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
552 PaintHighlight(dc
, false);
555 PaintHighlight(dc
, true);
558 customColours
[colourSelection
].Set(colourData
.m_dataColour
.Red(),
559 colourData
.m_dataColour
.Green(),
560 colourData
.m_dataColour
.Blue());
562 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
564 PaintCustomColours(dc
);
569 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
575 colourData
.m_dataColour
.Set((unsigned char)redSlider
->GetValue(), colourData
.m_dataColour
.Green(), colourData
.m_dataColour
.Blue());
576 PaintCustomColour(dc
);
579 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
585 colourData
.m_dataColour
.Set(colourData
.m_dataColour
.Red(), (unsigned char)greenSlider
->GetValue(), colourData
.m_dataColour
.Blue());
586 PaintCustomColour(dc
);
589 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
595 colourData
.m_dataColour
.Set(colourData
.m_dataColour
.Red(), colourData
.m_dataColour
.Green(), (unsigned char)blueSlider
->GetValue());
596 PaintCustomColour(dc
);
599 #endif // wxUSE_SLIDER
601 #endif // wxUSE_COLOURDLG && !defined(__WXGTK20__)