1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/clrpickerg.cpp
3 // Purpose: wxGenericColourButton class implementation
4 // Author: Francesco Montorsi (readapted code written by Vadim Zeitlin)
8 // Copyright: (c) Vadim Zeitlin, Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_COLOURPICKERCTRL
29 #include "wx/clrpicker.h"
31 #include "wx/colordlg.h"
34 // ============================================================================
36 // ============================================================================
38 wxColourData
wxGenericColourButton::ms_data
;
39 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourButton
, wxButton
)
41 // ----------------------------------------------------------------------------
42 // wxGenericColourButton
43 // ----------------------------------------------------------------------------
45 bool wxGenericColourButton::Create( wxWindow
*parent
, wxWindowID id
,
46 const wxColour
&col
, const wxPoint
&pos
,
47 const wxSize
&size
, long style
,
48 const wxValidator
& validator
, const wxString
&name
)
51 if (!wxButton::Create( parent
, id
, wxEmptyString
, pos
,
52 size
, style
, validator
, name
))
54 wxFAIL_MSG( wxT("wxGenericColourButton creation failed") );
58 // and handle user clicks on it
59 Connect(wxEVT_COMMAND_BUTTON_CLICKED
,
60 wxCommandEventHandler(wxGenericColourButton::OnButtonClick
),
70 void wxGenericColourButton::InitColourData()
72 ms_data
.SetChooseFull(true);
73 unsigned char grey
= 0;
74 for (int i
= 0; i
< 16; i
++, grey
+= 16)
76 // fill with grey tones the custom colors palette
77 wxColour
colour(grey
, grey
, grey
);
78 ms_data
.SetCustomColour(i
, colour
);
82 void wxGenericColourButton::OnButtonClick(wxCommandEvent
& WXUNUSED(ev
))
84 // update the wxColouData to be shown in the the dialog
85 ms_data
.SetColour(m_colour
);
87 // create the colour dialog and display it
88 wxColourDialog
dlg(this, &ms_data
);
89 if (dlg
.ShowModal() == wxID_OK
)
91 ms_data
= dlg
.GetColourData();
92 SetColour(ms_data
.GetColour());
95 wxColourPickerEvent
event(this, GetId(), m_colour
);
96 GetEventHandler()->ProcessEvent(event
);
100 void wxGenericColourButton::UpdateColour()
102 if ( !m_colour
.Ok() )
104 if ( HasFlag(wxCLRP_SHOW_LABEL
) )
105 SetLabel(wxEmptyString
);
109 // some combinations of the fg/bg colours may be unreadable, so we invert
110 // the colour to make sure fg colour is different enough from m_colour
111 wxColour
colFg(~m_colour
.Red(), ~m_colour
.Green(), ~m_colour
.Blue());
113 SetForegroundColour(colFg
);
114 SetBackgroundColour(m_colour
);
116 if ( HasFlag(wxCLRP_SHOW_LABEL
) )
117 SetLabel(m_colour
.GetAsString(wxC2S_HTML_SYNTAX
));
120 wxSize
wxGenericColourButton::DoGetBestSize() const
122 wxSize
sz(wxButton::DoGetBestSize());
123 if ( HasFlag(wxCLRP_SHOW_LABEL
) )
126 // if we have no label, then make this button a square
127 // (like e.g. native GTK version of this control)
128 sz
.SetWidth(sz
.GetHeight());
132 #endif // wxUSE_COLOURPICKERCTRL