]>
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
);
219 PaintBasicColours(dc
);
220 PaintCustomColours(dc
);
221 PaintCustomColour(dc
);
222 PaintHighlight(dc
, true);
225 void wxGenericColourDialog::CalculateMeasurements()
227 smallRectangleSize
.x
= 18;
228 smallRectangleSize
.y
= 14;
229 customRectangleSize
.x
= 40;
230 customRectangleSize
.y
= 40;
235 standardColoursRect
.x
= 10;
237 standardColoursRect
.y
= 15 + 20; /* OS/2 needs to account for dialog titlebar */
239 standardColoursRect
.y
= 15;
241 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
242 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
244 customColoursRect
.x
= standardColoursRect
.x
;
245 customColoursRect
.y
= standardColoursRect
.y
+ standardColoursRect
.height
+ 20;
246 customColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
247 customColoursRect
.height
= (2*smallRectangleSize
.y
) + (1*gridSpacing
);
249 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
250 singleCustomColourRect
.y
= 80;
251 singleCustomColourRect
.width
= customRectangleSize
.x
;
252 singleCustomColourRect
.height
= customRectangleSize
.y
;
255 customButtonX
= singleCustomColourRect
.x
;
256 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
259 void wxGenericColourDialog::CreateWidgets()
263 wxBoxSizer
*topSizer
= new wxBoxSizer( wxVERTICAL
);
265 const int sliderHeight
= 160;
268 const int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
270 redSlider
= new wxSlider(this, wxID_RED_SLIDER
, colourData
.m_dataColour
.Red(), 0, 255,
271 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
272 greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, colourData
.m_dataColour
.Green(), 0, 255,
273 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
274 blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, colourData
.m_dataColour
.Blue(), 0, 255,
275 wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
277 wxBoxSizer
*sliderSizer
= new wxBoxSizer( wxHORIZONTAL
);
279 // 1) space for sliders
280 sliderSizer
->Add( sliderX
, sliderHeight
);
281 sliderSizer
->Add( redSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
282 sliderSizer
->Add( greenSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
283 sliderSizer
->Add( blueSlider
, 0, wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
| wxALL
, 10 );
285 topSizer
->Add( sliderSizer
, 0, wxCENTRE
| wxALL
, 10 );
287 topSizer
->Add( 1, sliderHeight
, 0, wxCENTRE
| wxALL
, 15 );
288 #endif // wxUSE_SLIDER
292 topSizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
296 wxSizer
*buttonsizer
= CreateButtonSizer( wxOK
|wxCANCEL
);
297 buttonsizer
->Add( new wxButton(this, wxID_ADD_CUSTOM
, _("Add to custom colours") ), 0, wxLEFT
|wxRIGHT
, 10 );
298 topSizer
->Add( buttonsizer
, 0, wxEXPAND
| wxALL
, 10 );
300 SetAutoLayout( true );
301 SetSizer( topSizer
);
303 topSizer
->SetSizeHints( this );
304 topSizer
->Fit( this );
311 void wxGenericColourDialog::InitializeColours(void)
315 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
317 wxColour col
= wxTheColourDatabase
->Find(wxColourDialogNames
[i
]);
319 standardColours
[i
].Set(col
.Red(), col
.Green(), col
.Blue());
321 standardColours
[i
].Set(0, 0, 0);
324 for (i
= 0; i
< WXSIZEOF(customColours
); i
++)
326 wxColour c
= colourData
.GetCustomColour(i
);
328 customColours
[i
] = colourData
.GetCustomColour(i
);
330 customColours
[i
] = wxColour(255, 255, 255);
333 wxColour curr
= colourData
.GetColour();
336 bool initColourFound
= false;
338 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
340 if ( standardColours
[i
] == curr
&& !initColourFound
)
344 initColourFound
= true;
348 if ( !initColourFound
)
350 for ( i
= 0; i
< WXSIZEOF(customColours
); i
++ )
352 if ( customColours
[i
] == curr
)
360 colourData
.m_dataColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
366 colourData
.m_dataColour
.Set( 0, 0, 0 );
370 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
375 for (i
= 0; i
< 6; i
++)
378 for (j
= 0; j
< 8; j
++)
382 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
383 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
385 dc
.SetPen(*wxBLACK_PEN
);
386 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
389 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
395 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
400 for (i
= 0; i
< 2; i
++)
403 for (j
= 0; j
< 8; j
++)
407 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
408 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
410 dc
.SetPen(*wxBLACK_PEN
);
412 wxBrush
brush(customColours
[ptr
], wxSOLID
);
415 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
421 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
423 if ( colourSelection
< 0 )
428 // Number of pixels bigger than the standard rectangle size
429 // for drawing a highlight
436 int y
= (int)(colourSelection
/ 8);
437 int x
= (int)(colourSelection
- (y
*8));
439 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
440 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.y
) - deltaY
;
443 dc
.SetPen(*wxBLACK_PEN
);
445 dc
.SetPen(*wxLIGHT_GREY_PEN
);
447 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
448 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
452 // User-defined colours
453 int y
= (int)(colourSelection
/ 8);
454 int x
= (int)(colourSelection
- (y
*8));
456 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
457 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
460 dc
.SetPen(*wxBLACK_PEN
);
462 dc
.SetPen(*wxLIGHT_GREY_PEN
);
464 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
465 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
471 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
475 dc
.SetPen(*wxBLACK_PEN
);
477 wxBrush
*brush
= new wxBrush(colourData
.m_dataColour
, wxSOLID
);
480 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
481 customRectangleSize
.x
, customRectangleSize
.y
);
483 dc
.SetBrush(wxNullBrush
);
489 void wxGenericColourDialog::OnBasicColourClick(int which
)
493 PaintHighlight(dc
, false);
495 colourSelection
= which
;
498 redSlider
->SetValue( standardColours
[colourSelection
].Red() );
499 greenSlider
->SetValue( standardColours
[colourSelection
].Green() );
500 blueSlider
->SetValue( standardColours
[colourSelection
].Blue() );
501 #endif // wxUSE_SLIDER
503 colourData
.m_dataColour
.Set(standardColours
[colourSelection
].Red(),
504 standardColours
[colourSelection
].Green(),
505 standardColours
[colourSelection
].Blue());
507 PaintCustomColour(dc
);
508 PaintHighlight(dc
, true);
511 void wxGenericColourDialog::OnCustomColourClick(int which
)
514 PaintHighlight(dc
, false);
516 colourSelection
= which
;
519 redSlider
->SetValue( customColours
[colourSelection
].Red() );
520 greenSlider
->SetValue( customColours
[colourSelection
].Green() );
521 blueSlider
->SetValue( customColours
[colourSelection
].Blue() );
522 #endif // wxUSE_SLIDER
524 colourData
.m_dataColour
.Set(customColours
[colourSelection
].Red(),
525 customColours
[colourSelection
].Green(),
526 customColours
[colourSelection
].Blue());
528 PaintCustomColour(dc
);
529 PaintHighlight(dc
, true);
533 void wxGenericColourDialog::OnOk(void)
538 void wxGenericColourDialog::OnCancel(void)
540 colourDialogCancelled = true;
545 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
550 PaintHighlight(dc
, false);
553 PaintHighlight(dc
, true);
556 customColours
[colourSelection
].Set(colourData
.m_dataColour
.Red(),
557 colourData
.m_dataColour
.Green(),
558 colourData
.m_dataColour
.Blue());
560 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
562 PaintCustomColours(dc
);
567 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
573 colourData
.m_dataColour
.Set((unsigned char)redSlider
->GetValue(), colourData
.m_dataColour
.Green(), colourData
.m_dataColour
.Blue());
574 PaintCustomColour(dc
);
577 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
583 colourData
.m_dataColour
.Set(colourData
.m_dataColour
.Red(), (unsigned char)greenSlider
->GetValue(), colourData
.m_dataColour
.Blue());
584 PaintCustomColour(dc
);
587 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
593 colourData
.m_dataColour
.Set(colourData
.m_dataColour
.Red(), colourData
.m_dataColour
.Green(), (unsigned char)blueSlider
->GetValue());
594 PaintCustomColour(dc
);
597 #endif // wxUSE_SLIDER
599 #endif // wxUSE_COLOURDLG && !defined(__WXGTK20__)