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"
28 #include "wx/window.h"
31 #include "wx/clrpicker.h"
32 #include "wx/colordlg.h"
35 // ============================================================================
37 // ============================================================================
39 #if wxUSE_COLOURPICKERCTRL
41 wxColourData
wxGenericColourButton::ms_data
;
42 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourButton
, wxButton
)
44 // ----------------------------------------------------------------------------
45 // wxGenericColourButton
46 // ----------------------------------------------------------------------------
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
)
54 if (!wxButton::Create( parent
, id
, wxEmptyString
, pos
,
55 size
, style
, validator
, name
))
57 wxFAIL_MSG( wxT("wxGenericColourButton creation failed") );
61 // and handle user clicks on it
62 Connect(wxEVT_COMMAND_BUTTON_CLICKED
,
63 wxCommandEventHandler(wxGenericColourButton::OnButtonClick
),
73 void wxGenericColourButton::InitColourData()
75 ms_data
.SetChooseFull(true);
76 for (int i
= 0; i
< 16; i
++)
78 // fill with grey tones the custom colors palette
79 wxColour
colour(i
*16, i
*16, i
*16);
80 ms_data
.SetCustomColour(i
, colour
);
84 void wxGenericColourButton::OnButtonClick(wxCommandEvent
& WXUNUSED(ev
))
86 // update the wxColouData to be shown in the the dialog
87 ms_data
.SetColour(m_colour
);
89 // create the colour dialog and display it
90 wxColourDialog
dlg(this, &ms_data
);
91 if (dlg
.ShowModal() == wxID_OK
)
93 ms_data
= dlg
.GetColourData();
94 SetColour(ms_data
.GetColour());
97 wxColourPickerEvent
event(this, GetId(), m_colour
);
98 GetEventHandler()->ProcessEvent(event
);
102 void wxGenericColourButton::UpdateColour()
104 if ( !m_colour
.Ok() )
106 if ( HasFlag(wxCLRP_SHOW_LABEL
) )
107 SetLabel(wxEmptyString
);
111 // some combinations of the fg/bg colours may be unreadable, so we invert
112 // the colour to make sure fg colour is different enough from m_colour
113 wxColour
colFg(~m_colour
.Red(), ~m_colour
.Green(), ~m_colour
.Blue());
115 SetForegroundColour(colFg
);
116 SetBackgroundColour(m_colour
);
118 if ( HasFlag(wxCLRP_SHOW_LABEL
) )
119 SetLabel(m_colour
.GetAsString(wxC2S_HTML_SYNTAX
));
122 #endif // wxUSE_COLOURPICKERCTRL