]> git.saurik.com Git - wxWidgets.git/blame - src/generic/clrpickerg.cpp
avoid assert in DoGetBestSize() for an empty tree
[wxWidgets.git] / src / generic / clrpickerg.cpp
CommitLineData
ec376c8f
VZ
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
c757b5fe 27#if wxUSE_COLOURPICKERCTRL
ec376c8f
VZ
28
29#include "wx/clrpicker.h"
c757b5fe 30
ec376c8f
VZ
31#include "wx/colordlg.h"
32
33
34// ============================================================================
35// implementation
36// ============================================================================
37
ec376c8f
VZ
38wxColourData wxGenericColourButton::ms_data;
39IMPLEMENT_DYNAMIC_CLASS(wxGenericColourButton, wxButton)
40
41// ----------------------------------------------------------------------------
42// wxGenericColourButton
43// ----------------------------------------------------------------------------
44
45bool 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 // create this button
51 if (!wxButton::Create( parent, id, wxEmptyString, pos,
52 size, style, validator, name ))
53 {
54 wxFAIL_MSG( wxT("wxGenericColourButton creation failed") );
55 return false;
56 }
57
58 // and handle user clicks on it
59 Connect(wxEVT_COMMAND_BUTTON_CLICKED,
60 wxCommandEventHandler(wxGenericColourButton::OnButtonClick),
61 NULL, this);
62
63 m_colour = col;
64 UpdateColour();
65 InitColourData();
66
67 return true;
68}
69
70void wxGenericColourButton::InitColourData()
71{
72 ms_data.SetChooseFull(true);
7c808858
WS
73 unsigned char grey = 0;
74 for (int i = 0; i < 16; i++, grey += 16)
ec376c8f
VZ
75 {
76 // fill with grey tones the custom colors palette
7c808858 77 wxColour colour(grey, grey, grey);
ec376c8f
VZ
78 ms_data.SetCustomColour(i, colour);
79 }
80}
81
82void wxGenericColourButton::OnButtonClick(wxCommandEvent& WXUNUSED(ev))
83{
84 // update the wxColouData to be shown in the the dialog
85 ms_data.SetColour(m_colour);
86
87 // create the colour dialog and display it
88 wxColourDialog dlg(this, &ms_data);
89 if (dlg.ShowModal() == wxID_OK)
90 {
91 ms_data = dlg.GetColourData();
92 SetColour(ms_data.GetColour());
93
94 // fire an event
95 wxColourPickerEvent event(this, GetId(), m_colour);
96 GetEventHandler()->ProcessEvent(event);
97 }
98}
99
100void wxGenericColourButton::UpdateColour()
101{
102 if ( !m_colour.Ok() )
103 {
104 if ( HasFlag(wxCLRP_SHOW_LABEL) )
105 SetLabel(wxEmptyString);
106 return;
107 }
108
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());
112
113 SetForegroundColour(colFg);
114 SetBackgroundColour(m_colour);
115
116 if ( HasFlag(wxCLRP_SHOW_LABEL) )
117 SetLabel(m_colour.GetAsString(wxC2S_HTML_SYNTAX));
118}
119
e8427971
WS
120wxSize wxGenericColourButton::DoGetBestSize() const
121{
122 wxSize sz(wxButton::DoGetBestSize());
123 if ( HasFlag(wxCLRP_SHOW_LABEL) )
124 return sz;
125
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());
129 return sz;
130}
131
ec376c8f 132#endif // wxUSE_COLOURPICKERCTRL