The rounded corners look really dumb at this size.
[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 // Copyright: (c) Vadim Zeitlin, Francesco Montorsi
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_COLOURPICKERCTRL
27
28 #include "wx/clrpicker.h"
29 #include "wx/colordlg.h"
30 #include "wx/dcmemory.h"
31
32
33 // ============================================================================
34 // implementation
35 // ============================================================================
36
37 wxColourData wxGenericColourButton::ms_data;
38 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourButton, wxBitmapButton)
39
40 // ----------------------------------------------------------------------------
41 // wxGenericColourButton
42 // ----------------------------------------------------------------------------
43
44 bool wxGenericColourButton::Create( wxWindow *parent, wxWindowID id,
45 const wxColour &col, const wxPoint &pos,
46 const wxSize &size, long style,
47 const wxValidator& validator, const wxString &name)
48 {
49 m_bitmap = wxBitmap( 60, 13 );
50
51 // create this button
52 if (!wxBitmapButton::Create( parent, id, m_bitmap, pos,
53 size, style | wxBU_AUTODRAW, validator, name ))
54 {
55 wxFAIL_MSG( wxT("wxGenericColourButton creation failed") );
56 return false;
57 }
58
59 // and handle user clicks on it
60 Connect(GetId(), wxEVT_BUTTON,
61 wxCommandEventHandler(wxGenericColourButton::OnButtonClick),
62 NULL, this);
63
64 m_colour = col;
65 UpdateColour();
66 InitColourData();
67
68 return true;
69 }
70
71 void wxGenericColourButton::InitColourData()
72 {
73 ms_data.SetChooseFull(true);
74 unsigned char grey = 0;
75 for (int i = 0; i < 16; i++, grey += 16)
76 {
77 // fill with grey tones the custom colors palette
78 wxColour colour(grey, grey, grey);
79 ms_data.SetCustomColour(i, colour);
80 }
81 }
82
83 void wxGenericColourButton::OnButtonClick(wxCommandEvent& WXUNUSED(ev))
84 {
85 // update the wxColouData to be shown in the dialog
86 ms_data.SetColour(m_colour);
87
88 // create the colour dialog and display it
89 wxColourDialog dlg(this, &ms_data);
90 if (dlg.ShowModal() == wxID_OK)
91 {
92 ms_data = dlg.GetColourData();
93 SetColour(ms_data.GetColour());
94
95 // fire an event
96 wxColourPickerEvent event(this, GetId(), m_colour);
97 GetEventHandler()->ProcessEvent(event);
98 }
99 }
100
101 void wxGenericColourButton::UpdateColour()
102 {
103 wxMemoryDC dc(m_bitmap);
104 dc.SetPen( *wxTRANSPARENT_PEN );
105 dc.SetBrush( wxBrush(m_colour) );
106 dc.DrawRectangle( 0,0,m_bitmap.GetWidth(),m_bitmap.GetHeight() );
107
108 if ( HasFlag(wxCLRP_SHOW_LABEL) )
109 {
110 wxColour col( ~m_colour.Red(), ~m_colour.Green(), ~m_colour.Blue() );
111 dc.SetTextForeground( col );
112 dc.SetFont( GetFont() );
113 dc.DrawText( m_colour.GetAsString(wxC2S_HTML_SYNTAX), 0, 0 );
114 }
115
116 dc.SelectObject( wxNullBitmap );
117 SetBitmapLabel( m_bitmap );
118 }
119
120 wxSize wxGenericColourButton::DoGetBestSize() const
121 {
122 wxSize sz(wxBitmapButton::DoGetBestSize());
123 #ifdef __WXMAC__
124 sz.y += 6;
125 #else
126 sz.y += 2;
127 #endif
128 sz.x += 30;
129 if ( HasFlag(wxCLRP_SHOW_LABEL) )
130 return sz;
131
132 // if we have no label, then make this button a square
133 // (like e.g. native GTK version of this control) ???
134 // sz.SetWidth(sz.GetHeight());
135 return sz;
136 }
137
138 #endif // wxUSE_COLOURPICKERCTRL