Warning fix.
[wxWidgets.git] / src / generic / clrpickerg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/clrpickerg.cpp
3 // Purpose: wxGenericColourButton class implementation
4 // Author: Francesco Montorsi (readapted code written by Vadim Zeitlin)
5 // Modified by:
6 // Created: 15/04/2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin, Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/window.h"
29 #endif //WX_PRECOMP
30
31 #include "wx/clrpicker.h"
32 #include "wx/colordlg.h"
33
34
35 // ============================================================================
36 // implementation
37 // ============================================================================
38
39 #if wxUSE_COLOURPICKERCTRL
40
41 wxColourData wxGenericColourButton::ms_data;
42 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourButton, wxButton)
43
44 // ----------------------------------------------------------------------------
45 // wxGenericColourButton
46 // ----------------------------------------------------------------------------
47
48 bool wxGenericColourButton::Create( wxWindow *parent, wxWindowID id,
49 const wxColour &col, const wxPoint &pos,
50 const wxSize &size, long style,
51 const wxValidator& validator, const wxString &name)
52 {
53 // create this button
54 if (!wxButton::Create( parent, id, wxEmptyString, pos,
55 size, style, validator, name ))
56 {
57 wxFAIL_MSG( wxT("wxGenericColourButton creation failed") );
58 return false;
59 }
60
61 // and handle user clicks on it
62 Connect(wxEVT_COMMAND_BUTTON_CLICKED,
63 wxCommandEventHandler(wxGenericColourButton::OnButtonClick),
64 NULL, this);
65
66 m_colour = col;
67 UpdateColour();
68 InitColourData();
69
70 return true;
71 }
72
73 void wxGenericColourButton::InitColourData()
74 {
75 ms_data.SetChooseFull(true);
76 unsigned char grey = 0;
77 for (int i = 0; i < 16; i++, grey += 16)
78 {
79 // fill with grey tones the custom colors palette
80 wxColour colour(grey, grey, grey);
81 ms_data.SetCustomColour(i, colour);
82 }
83 }
84
85 void wxGenericColourButton::OnButtonClick(wxCommandEvent& WXUNUSED(ev))
86 {
87 // update the wxColouData to be shown in the the dialog
88 ms_data.SetColour(m_colour);
89
90 // create the colour dialog and display it
91 wxColourDialog dlg(this, &ms_data);
92 if (dlg.ShowModal() == wxID_OK)
93 {
94 ms_data = dlg.GetColourData();
95 SetColour(ms_data.GetColour());
96
97 // fire an event
98 wxColourPickerEvent event(this, GetId(), m_colour);
99 GetEventHandler()->ProcessEvent(event);
100 }
101 }
102
103 void wxGenericColourButton::UpdateColour()
104 {
105 if ( !m_colour.Ok() )
106 {
107 if ( HasFlag(wxCLRP_SHOW_LABEL) )
108 SetLabel(wxEmptyString);
109 return;
110 }
111
112 // some combinations of the fg/bg colours may be unreadable, so we invert
113 // the colour to make sure fg colour is different enough from m_colour
114 wxColour colFg(~m_colour.Red(), ~m_colour.Green(), ~m_colour.Blue());
115
116 SetForegroundColour(colFg);
117 SetBackgroundColour(m_colour);
118
119 if ( HasFlag(wxCLRP_SHOW_LABEL) )
120 SetLabel(m_colour.GetAsString(wxC2S_HTML_SYNTAX));
121 }
122
123 #endif // wxUSE_COLOURPICKERCTRL