Restore MSW's colour picker, II
[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 #if wxUSE_COLOURPICKERCTRL
28
29 #include "wx/clrpicker.h"
30 #include "wx/colordlg.h"
31 #include "wx/dcmemory.h"
32
33
34 // ============================================================================
35 // implementation
36 // ============================================================================
37
38 wxColourData wxGenericColourButton::ms_data;
39 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourButton, wxBitmapButton)
40
41 // ----------------------------------------------------------------------------
42 // wxGenericColourButton
43 // ----------------------------------------------------------------------------
44
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)
49 {
50 m_bitmap = wxBitmap( 60, 13 );
51
52 // create this button
53 if (!wxBitmapButton::Create( parent, id, m_bitmap, pos,
54 size, style | wxBU_AUTODRAW, validator, name ))
55 {
56 wxFAIL_MSG( wxT("wxGenericColourButton creation failed") );
57 return false;
58 }
59
60 // and handle user clicks on it
61 Connect(wxEVT_COMMAND_BUTTON_CLICKED,
62 wxCommandEventHandler(wxGenericColourButton::OnButtonClick),
63 NULL, this);
64
65 m_colour = col;
66 UpdateColour();
67 InitColourData();
68
69 return true;
70 }
71
72 void wxGenericColourButton::InitColourData()
73 {
74 ms_data.SetChooseFull(true);
75 unsigned char grey = 0;
76 for (int i = 0; i < 16; i++, grey += 16)
77 {
78 // fill with grey tones the custom colors palette
79 wxColour colour(grey, grey, grey);
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 wxMemoryDC dc(m_bitmap);
105 dc.SetPen( *wxTRANSPARENT_PEN );
106 dc.SetBrush( wxBrush(m_colour) );
107 dc.DrawRectangle( 0,0,m_bitmap.GetWidth(),m_bitmap.GetHeight() );
108
109 if ( HasFlag(wxCLRP_SHOW_LABEL) )
110 {
111 wxColour col( ~m_colour.Red(), ~m_colour.Green(), ~m_colour.Blue() );
112 dc.SetTextForeground( col );
113 dc.SetFont( GetFont() );
114 dc.DrawText( m_colour.GetAsString(wxC2S_HTML_SYNTAX), 0, 0 );
115 }
116
117 dc.SelectObject( wxNullBitmap );
118 SetBitmapLabel( m_bitmap );
119 }
120
121 wxSize wxGenericColourButton::DoGetBestSize() const
122 {
123 wxSize sz(wxBitmapButton::DoGetBestSize());
124 #ifdef __WXMAC__
125 sz.y += 6;
126 #else
127 sz.y += 2;
128 #endif
129 sz.x += 30;
130 if ( HasFlag(wxCLRP_SHOW_LABEL) )
131 return sz;
132
133 // if we have no label, then make this button a square
134 // (like e.g. native GTK version of this control) ???
135 // sz.SetWidth(sz.GetHeight());
136 return sz;
137 }
138
139 #endif // wxUSE_COLOURPICKERCTRL