1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/clrpicker.cpp
3 // Purpose: implementation of wxColourButton
4 // Author: Francesco Montorsi
5 // Modified By:
6 // Created: 15/04/2006
7 // Copyright: (c) Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #if wxUSE_COLOURPICKERCTRL
20
21 #include "wx/clrpicker.h"
22
23 #include "wx/gtk/private/wrapgtk.h"
24
25 // ============================================================================
26 // implementation
27 // ============================================================================
28
29 //-----------------------------------------------------------------------------
30 // "color-set"
31 //-----------------------------------------------------------------------------
32
33 extern "C" {
gtk_clrbutton_setcolor_callback(GtkColorButton * widget,wxColourButton * p)34 static void gtk_clrbutton_setcolor_callback(GtkColorButton *widget,
35 wxColourButton *p)
36 {
37 // update the m_colour member of the wxColourButton
38 wxASSERT(p);
39 #ifdef __WXGTK4__
40 GdkRGBA gdkColor;
41 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), &gdkColor);
42 #elif defined(__WXGTK3__)
43 wxGCC_WARNING_SUPPRESS(deprecated-declarations)
44 GdkRGBA gdkColor;
45 gtk_color_button_get_rgba(widget, &gdkColor);
46 wxGCC_WARNING_RESTORE()
47 #else
48 GdkColor gdkColor;
49 gtk_color_button_get_color(widget, &gdkColor);
50 #endif
51 p->GTKSetColour(gdkColor);
52
53 // Fire the corresponding event: note that we want it to appear as
54 // originating from our parent, which is the user-visible window, and not
55 // this button itself, which is just an implementation detail.
56 wxWindow* const parent = p->GetParent();
57 wxColourPickerEvent event(parent, parent->GetId(), p->GetColour());
58 p->HandleWindowEvent(event);
59 }
60 }
61
62 //-----------------------------------------------------------------------------
63 // wxColourButton
64 //-----------------------------------------------------------------------------
65
66 wxIMPLEMENT_DYNAMIC_CLASS(wxColourButton, wxButton);
67
Create(wxWindow * parent,wxWindowID id,const wxColour & col,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)68 bool wxColourButton::Create( wxWindow *parent, wxWindowID id,
69 const wxColour &col,
70 const wxPoint &pos, const wxSize &size,
71 long style, const wxValidator& validator,
72 const wxString &name )
73 {
74 if (!PreCreation( parent, pos, size ) ||
75 !wxControl::CreateBase(parent, id, pos, size, style, validator, name))
76 {
77 wxFAIL_MSG( wxT("wxColourButton creation failed") );
78 return false;
79 }
80
81 m_colour = col;
82 #ifdef __WXGTK3__
83 m_widget = gtk_color_button_new_with_rgba(m_colour);
84 #else
85 m_widget = gtk_color_button_new_with_color( m_colour.GetColor() );
86 #endif
87 g_object_ref(m_widget);
88
89 // Display opacity slider
90 g_object_set(G_OBJECT(m_widget), "use-alpha",
91 static_cast<bool>(style & wxCLRP_SHOW_ALPHA), NULL);
92 // GtkColourButton signals
93 g_signal_connect(m_widget, "color-set",
94 G_CALLBACK(gtk_clrbutton_setcolor_callback), this);
95
96
97 m_parent->DoAddChild( this );
98
99 PostCreation(size);
100 SetInitialSize(size);
101
102 return true;
103 }
104
~wxColourButton()105 wxColourButton::~wxColourButton()
106 {
107 }
108
UpdateColour()109 void wxColourButton::UpdateColour()
110 {
111 #ifdef __WXGTK4__
112 gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(m_widget), m_colour);
113 #elif defined(__WXGTK3__)
114 wxGCC_WARNING_SUPPRESS(deprecated-declarations)
115 gtk_color_button_set_rgba(GTK_COLOR_BUTTON(m_widget), m_colour);
116 wxGCC_WARNING_RESTORE()
117 #else
118 gtk_color_button_set_color(GTK_COLOR_BUTTON(m_widget), m_colour.GetColor());
119 #endif
120 }
121
122 #endif // wxUSE_COLOURPICKERCTRL
123