]>
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),
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();
178 // Handle OS/2's reverse coordinate system and account for the dialog title
181 GetClientSize(NULL
, &nClientHeight
);
182 y
= (nClientHeight
- y
) + 20;
184 if ((x
>= standardColoursRect
.x
&& x
<= (standardColoursRect
.x
+ standardColoursRect
.width
)) &&
185 (y
>= standardColoursRect
.y
&& y
<= (standardColoursRect
.y
+ standardColoursRect
.height
)))
187 int selX
= (int)(x
- standardColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
188 int selY
= (int)(y
- standardColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
189 int ptr
= (int)(selX
+ selY
*8);
190 OnBasicColourClick(ptr
);
192 else if ((x
>= customColoursRect
.x
&& x
<= (customColoursRect
.x
+ customColoursRect
.width
)) &&
193 (y
>= customColoursRect
.y
&& y
<= (customColoursRect
.y
+ customColoursRect
.height
)))
195 int selX
= (int)(x
- customColoursRect
.x
)/(smallRectangleSize
.x
+ gridSpacing
);
196 int selY
= (int)(y
- customColoursRect
.y
)/(smallRectangleSize
.y
+ gridSpacing
);
197 int ptr
= (int)(selX
+ selY
*8);
198 OnCustomColourClick(ptr
);
207 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
)
209 #if !defined(__WXMOTIF__) && !defined(__WXMAC__) && !defined(__WXPM__) && !defined(__WXCOCOA__)
210 wxDialog::OnPaint(event
);
215 PaintBasicColours(dc
);
216 PaintCustomColours(dc
);
217 PaintCustomColour(dc
);
218 PaintHighlight(dc
, TRUE
);
221 void wxGenericColourDialog::CalculateMeasurements()
223 smallRectangleSize
.x
= 18;
224 smallRectangleSize
.y
= 14;
225 customRectangleSize
.x
= 40;
226 customRectangleSize
.y
= 40;
231 standardColoursRect
.x
= 10;
233 standardColoursRect
.y
= 15 + 20; /* OS/2 needs to account for dialog titlebar */
235 standardColoursRect
.y
= 15;
237 standardColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
238 standardColoursRect
.height
= (6*smallRectangleSize
.y
) + (5*gridSpacing
);
240 customColoursRect
.x
= standardColoursRect
.x
;
241 customColoursRect
.y
= standardColoursRect
.y
+ standardColoursRect
.height
+ 20;
242 customColoursRect
.width
= (8*smallRectangleSize
.x
) + (7*gridSpacing
);
243 customColoursRect
.height
= (2*smallRectangleSize
.y
) + (1*gridSpacing
);
245 singleCustomColourRect
.x
= customColoursRect
.width
+ customColoursRect
.x
+ sectionSpacing
;
246 singleCustomColourRect
.y
= 80;
247 singleCustomColourRect
.width
= customRectangleSize
.x
;
248 singleCustomColourRect
.height
= customRectangleSize
.y
;
251 customButtonX
= singleCustomColourRect
.x
;
252 buttonY
= customColoursRect
.y
+ customColoursRect
.height
+ 10;
255 void wxGenericColourDialog::CreateWidgets()
259 int sliderX
= singleCustomColourRect
.x
+ singleCustomColourRect
.width
+ sectionSpacing
;
260 #if defined(__WXMOTIF__)
261 int sliderSpacing
= 65;
262 int sliderHeight
= 160;
264 int sliderSpacing
= 45;
265 int sliderHeight
= 160;
268 redSlider
= new wxSlider(this, wxID_RED_SLIDER
, singleCustomColour
.Red(), 0, 255,
269 wxPoint(sliderX
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
270 greenSlider
= new wxSlider(this, wxID_GREEN_SLIDER
, singleCustomColour
.Green(), 0, 255,
271 wxPoint(sliderX
+ sliderSpacing
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
272 blueSlider
= new wxSlider(this, wxID_BLUE_SLIDER
, singleCustomColour
.Blue(), 0, 255,
273 wxPoint(sliderX
+ 2*sliderSpacing
, 10), wxSize(-1, sliderHeight
), wxVERTICAL
|wxSL_LABELS
);
275 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
277 // 1) space for explicitly layouted controls
278 topsizer
->Add( sliderX
+ 3*sliderSpacing
, sliderHeight
+25 );
282 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
286 wxSizer
*buttonsizer
= CreateButtonSizer( wxOK
|wxCANCEL
);
287 buttonsizer
->Add( new wxButton(this, wxID_ADD_CUSTOM
, _("Add to custom colours") ), 0, wxLEFT
|wxRIGHT
, 10 );
288 topsizer
->Add( buttonsizer
, 0, wxCENTRE
| wxALL
, 10 );
290 SetAutoLayout( TRUE
);
291 SetSizer( topsizer
);
293 topsizer
->SetSizeHints( this );
294 topsizer
->Fit( this );
301 void wxGenericColourDialog::InitializeColours(void)
305 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
307 wxColour col
= wxTheColourDatabase
->Find(wxColourDialogNames
[i
]);
309 standardColours
[i
].Set(col
.Red(), col
.Green(), col
.Blue());
311 standardColours
[i
].Set(0, 0, 0);
314 for (i
= 0; i
< WXSIZEOF(customColours
); i
++)
316 customColours
[i
] = colourData
.GetCustomColour(i
);
319 wxColour curr
= colourData
.GetColour();
322 bool initColourFound
= FALSE
;
324 for (i
= 0; i
< WXSIZEOF(wxColourDialogNames
); i
++)
326 if ( standardColours
[i
] == curr
&& !initColourFound
)
330 initColourFound
= TRUE
;
334 if ( !initColourFound
)
336 for ( i
= 0; i
< WXSIZEOF(customColours
); i
++ )
338 if ( customColours
[i
] == curr
)
346 singleCustomColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() );
352 singleCustomColour
.Set( 0, 0, 0 );
356 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
)
361 for (i
= 0; i
< 6; i
++)
364 for (j
= 0; j
< 8; j
++)
368 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
);
369 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
);
371 dc
.SetPen(*wxBLACK_PEN
);
372 wxBrush
brush(standardColours
[ptr
], wxSOLID
);
375 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
381 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
)
386 for (i
= 0; i
< 2; i
++)
389 for (j
= 0; j
< 8; j
++)
393 int x
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
;
394 int y
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
;
396 dc
.SetPen(*wxBLACK_PEN
);
398 wxBrush
brush(customColours
[ptr
], wxSOLID
);
401 dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
);
407 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
)
409 if ( colourSelection
< 0 )
414 // Number of pixels bigger than the standard rectangle size
415 // for drawing a highlight
422 int y
= (int)(colourSelection
/ 8);
423 int x
= (int)(colourSelection
- (y
*8));
425 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
;
426 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + standardColoursRect
.y
) - deltaY
;
429 dc
.SetPen(*wxBLACK_PEN
);
431 dc
.SetPen(*wxLIGHT_GREY_PEN
);
433 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
434 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
438 // User-defined colours
439 int y
= (int)(colourSelection
/ 8);
440 int x
= (int)(colourSelection
- (y
*8));
442 x
= (x
*(smallRectangleSize
.x
+ gridSpacing
) + customColoursRect
.x
) - deltaX
;
443 y
= (y
*(smallRectangleSize
.y
+ gridSpacing
) + customColoursRect
.y
) - deltaY
;
446 dc
.SetPen(*wxBLACK_PEN
);
448 dc
.SetPen(*wxLIGHT_GREY_PEN
);
450 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
451 dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x
+ (2*deltaX
)), (smallRectangleSize
.y
+ (2*deltaY
)));
457 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
)
461 dc
.SetPen(*wxBLACK_PEN
);
463 wxBrush
*brush
= new wxBrush(singleCustomColour
, wxSOLID
);
466 dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
,
467 customRectangleSize
.x
, customRectangleSize
.y
);
469 dc
.SetBrush(wxNullBrush
);
475 void wxGenericColourDialog::OnBasicColourClick(int which
)
479 PaintHighlight(dc
, FALSE
);
481 colourSelection
= which
;
482 colourData
.SetColour(standardColours
[colourSelection
]);
483 redSlider
->SetValue( standardColours
[colourSelection
].Red() );
484 greenSlider
->SetValue( standardColours
[colourSelection
].Green() );
485 blueSlider
->SetValue( standardColours
[colourSelection
].Blue() );
486 singleCustomColour
.Set(standardColours
[colourSelection
].Red(), standardColours
[colourSelection
].Green(), standardColours
[colourSelection
].Blue());
488 PaintCustomColour(dc
);
489 PaintHighlight(dc
, TRUE
);
492 void wxGenericColourDialog::OnCustomColourClick(int which
)
495 PaintHighlight(dc
, FALSE
);
497 colourSelection
= which
;
498 colourData
.SetColour(customColours
[colourSelection
]);
499 redSlider
->SetValue( customColours
[colourSelection
].Red() );
500 greenSlider
->SetValue( customColours
[colourSelection
].Green() );
501 blueSlider
->SetValue( customColours
[colourSelection
].Blue() );
502 singleCustomColour
.Set(customColours
[colourSelection
].Red(), customColours
[colourSelection
].Green(), customColours
[colourSelection
].Blue());
503 PaintCustomColour(dc
);
504 PaintHighlight(dc
, TRUE
);
508 void wxGenericColourDialog::OnOk(void)
513 void wxGenericColourDialog::OnCancel(void)
515 colourDialogCancelled = TRUE;
520 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
))
525 PaintHighlight(dc
, FALSE
);
528 PaintHighlight(dc
, TRUE
);
531 customColours
[colourSelection
].Set(singleCustomColour
.Red(), singleCustomColour
.Green(), singleCustomColour
.Blue());
532 colourData
.SetColour(customColours
[colourSelection
]);
533 colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]);
535 PaintCustomColours(dc
);
538 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
))
544 singleCustomColour
.Set(redSlider
->GetValue(), singleCustomColour
.Green(), singleCustomColour
.Blue());
545 PaintCustomColour(dc
);
548 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
))
554 singleCustomColour
.Set(singleCustomColour
.Red(), greenSlider
->GetValue(), singleCustomColour
.Blue());
555 PaintCustomColour(dc
);
558 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
))
564 singleCustomColour
.Set(singleCustomColour
.Red(), singleCustomColour
.Green(), blueSlider
->GetValue());
565 PaintCustomColour(dc
);
568 #endif // wxUSE_COLOURDLG