added wx{Colour|File|Dir|Font}PickerCtrl (patch 1472329 by Francesco)
[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 for (int i = 0; i < 16; i++)
77 {
78 // fill with grey tones the custom colors palette
79 wxColour colour(i*16, i*16, i*16);
80 ms_data.SetCustomColour(i, colour);
81 }
82 }
83
84 void wxGenericColourButton::OnButtonClick(wxCommandEvent& WXUNUSED(ev))
85 {
86 // update the wxColouData to be shown in the the dialog
87 ms_data.SetColour(m_colour);
88
89 // create the colour dialog and display it
90 wxColourDialog dlg(this, &ms_data);
91 if (dlg.ShowModal() == wxID_OK)
92 {
93 ms_data = dlg.GetColourData();
94 SetColour(ms_data.GetColour());
95
96 // fire an event
97 wxColourPickerEvent event(this, GetId(), m_colour);
98 GetEventHandler()->ProcessEvent(event);
99 }
100 }
101
102 void wxGenericColourButton::UpdateColour()
103 {
104 if ( !m_colour.Ok() )
105 {
106 if ( HasFlag(wxCLRP_SHOW_LABEL) )
107 SetLabel(wxEmptyString);
108 return;
109 }
110
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());
114
115 SetForegroundColour(colFg);
116 SetBackgroundColour(m_colour);
117
118 if ( HasFlag(wxCLRP_SHOW_LABEL) )
119 SetLabel(m_colour.GetAsString(wxC2S_HTML_SYNTAX));
120 }
121
122 #endif // wxUSE_COLOURPICKERCTRL