]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bmpbuttn.cpp
fixing pattern phase bugs
[wxWidgets.git] / src / gtk / bmpbuttn.cpp
CommitLineData
151ccd11 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/gtk/bmpbuttn.cpp
151ccd11
RR
3// Purpose:
4// Author: Robert Roebling
f96aa4d9 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
151ccd11
RR
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
dcf924a3
RR
13#if wxUSE_BMPBUTTON
14
1e6feb95
VZ
15#include "wx/bmpbuttn.h"
16
9e691f46 17#include "wx/gtk/private.h"
83624f79 18
151ccd11
RR
19//-----------------------------------------------------------------------------
20// classes
21//-----------------------------------------------------------------------------
22
23class wxBitmapButton;
24
66bd6b93
RR
25//-----------------------------------------------------------------------------
26// data
27//-----------------------------------------------------------------------------
28
29extern bool g_blockEventsOnDrag;
30
151ccd11 31//-----------------------------------------------------------------------------
e1e955e1 32// "clicked"
151ccd11
RR
33//-----------------------------------------------------------------------------
34
865bb325 35extern "C" {
66bd6b93 36static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
151ccd11 37{
f6bcfd97 38 if (g_isIdle)
bf9e3e73 39 wxapp_install_idle_handler();
acfd422a 40
a2053b27 41 if (!button->m_hasVMT) return;
43a18898 42 if (g_blockEventsOnDrag) return;
f6bcfd97 43
43a18898
RR
44 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
45 event.SetEventObject(button);
46 button->GetEventHandler()->ProcessEvent(event);
47}
865bb325 48}
43a18898
RR
49
50//-----------------------------------------------------------------------------
51// "enter"
52//-----------------------------------------------------------------------------
53
865bb325 54extern "C" {
43a18898
RR
55static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
56{
a2053b27 57 if (!button->m_hasVMT) return;
43a18898
RR
58 if (g_blockEventsOnDrag) return;
59
f6bcfd97 60 button->HasFocus();
43a18898 61}
865bb325 62}
43a18898
RR
63
64//-----------------------------------------------------------------------------
65// "leave"
66//-----------------------------------------------------------------------------
67
865bb325 68extern "C" {
43a18898
RR
69static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
70{
a2053b27 71 if (!button->m_hasVMT) return;
43a18898
RR
72 if (g_blockEventsOnDrag) return;
73
f6bcfd97 74 button->NotFocus();
43a18898 75}
865bb325 76}
43a18898
RR
77
78//-----------------------------------------------------------------------------
79// "pressed"
80//-----------------------------------------------------------------------------
81
865bb325 82extern "C" {
43a18898
RR
83static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
84{
a2053b27 85 if (!button->m_hasVMT) return;
43a18898
RR
86 if (g_blockEventsOnDrag) return;
87
f6bcfd97 88 button->StartSelect();
43a18898 89}
865bb325 90}
43a18898
RR
91
92//-----------------------------------------------------------------------------
93// "released"
94//-----------------------------------------------------------------------------
95
865bb325 96extern "C" {
43a18898
RR
97static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
98{
a2053b27 99 if (!button->m_hasVMT) return;
43a18898
RR
100 if (g_blockEventsOnDrag) return;
101
f6bcfd97 102 button->EndSelect();
6de97a3b 103}
865bb325 104}
151ccd11
RR
105
106//-----------------------------------------------------------------------------
e1e955e1
RR
107// wxBitmapButton
108//-----------------------------------------------------------------------------
109
42b4e99e 110IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
151ccd11 111
c3dfaa10 112void wxBitmapButton::Init()
151ccd11 113{
c3dfaa10 114 m_hasFocus =
902725ee 115 m_isSelected = false;
6de97a3b 116}
151ccd11 117
c3dfaa10
VZ
118bool wxBitmapButton::Create( wxWindow *parent,
119 wxWindowID id,
120 const wxBitmap& bitmap,
121 const wxPoint& pos,
122 const wxSize& size,
123 long style,
124 const wxValidator& validator,
125 const wxString &name )
151ccd11 126{
902725ee
WS
127 m_needParent = true;
128 m_acceptsFocus = true;
f6bcfd97 129
4dcaf11a
RR
130 if (!PreCreation( parent, pos, size ) ||
131 !CreateBase( parent, id, pos, size, style, validator, name ))
132 {
223d09f6 133 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
902725ee 134 return false;
4dcaf11a 135 }
6de97a3b 136
189f58fa 137 m_bmpNormal = bitmap;
f6bcfd97 138
43a18898 139 m_widget = gtk_button_new();
de1c750f 140
de1c750f
RR
141 if (style & wxNO_BORDER)
142 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 143
1e6feb95 144 if (m_bmpNormal.Ok())
43a18898 145 {
29149a64 146 OnSetBitmap();
43a18898 147 }
f6bcfd97 148
9fa72bd2
MR
149 g_signal_connect_after (m_widget, "clicked",
150 G_CALLBACK (gtk_bmpbutton_clicked_callback),
151 this);
152
153 g_signal_connect (m_widget, "enter",
154 G_CALLBACK (gtk_bmpbutton_enter_callback), this);
155 g_signal_connect (m_widget, "leave",
156 G_CALLBACK (gtk_bmpbutton_leave_callback), this);
157 g_signal_connect (m_widget, "pressed",
158 G_CALLBACK (gtk_bmpbutton_press_callback), this);
159 g_signal_connect (m_widget, "released",
160 G_CALLBACK (gtk_bmpbutton_release_callback), this);
f6bcfd97 161
eb082a08 162 m_parent->DoAddChild( this );
f6bcfd97 163
abdeb9e7 164 PostCreation(size);
f6bcfd97 165
902725ee 166 return true;
6de97a3b 167}
f6bcfd97 168
43a18898 169void wxBitmapButton::SetDefault()
151ccd11 170{
43a18898
RR
171 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
172 gtk_widget_grab_default( m_widget );
f6bcfd97 173
3502e687 174 SetSize( m_x, m_y, m_width, m_height );
6de97a3b 175}
151ccd11
RR
176
177void wxBitmapButton::SetLabel( const wxString &label )
178{
223d09f6 179 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
f96aa4d9 180
43a18898 181 wxControl::SetLabel( label );
6de97a3b 182}
151ccd11 183
f40fdaa3 184void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
903f689b 185{
afa7bd1e 186 if (!GTK_BIN(m_widget)->child)
9e691f46 187 return;
29149a64 188
f40fdaa3 189 wxButton::DoApplyWidgetStyle(style);
43a18898
RR
190}
191
29149a64 192void wxBitmapButton::OnSetBitmap()
43a18898 193{
1e6feb95 194 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
f96aa4d9 195
9f884528 196 InvalidateBestSize();
902725ee 197
43a18898 198 wxBitmap the_one;
f6bcfd97 199 if (!m_isEnabled)
29149a64 200 the_one = m_bmpDisabled;
67a6726a 201 else if (m_isSelected)
29149a64 202 the_one = m_bmpSelected;
67a6726a 203 else if (m_hasFocus)
29149a64 204 the_one = m_bmpFocus;
f6bcfd97 205 else
902725ee 206 the_one = m_bmpNormal;
43a18898 207
1e6feb95 208 if (!the_one.Ok()) the_one = m_bmpNormal;
de1c750f 209 if (!the_one.Ok()) return;
f6bcfd97 210
43a18898
RR
211 GdkBitmap *mask = (GdkBitmap *) NULL;
212 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
f6bcfd97 213
afa7bd1e 214 GtkWidget *child = GTK_BIN(m_widget)->child;
9e691f46 215 if (child == NULL)
29149a64
VZ
216 {
217 // initial bitmap
4fab7128 218 GtkWidget *pixmap;
68567a96 219
4fab7128
VS
220 if (the_one.HasPixbuf())
221 pixmap = gtk_image_new_from_pixbuf(the_one.GetPixbuf());
222 else
223 pixmap = gtk_image_new_from_pixmap(the_one.GetPixmap(), mask);
68567a96 224
67a6726a
RR
225 gtk_widget_show(pixmap);
226 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
29149a64
VZ
227 }
228 else
67a6726a 229 { // subsequent bitmaps
4fab7128
VS
230 GtkImage *pixmap = GTK_IMAGE(child);
231 if (the_one.HasPixbuf())
232 gtk_image_set_from_pixbuf(pixmap, the_one.GetPixbuf());
233 else
234 gtk_image_set_from_pixmap(pixmap, the_one.GetPixmap(), mask);
67a6726a 235 }
903f689b
RR
236}
237
e0aeebed
VS
238wxSize wxBitmapButton::DoGetBestSize() const
239{
240 return wxControl::DoGetBestSize();
241}
242
f03fc89f 243bool wxBitmapButton::Enable( bool enable )
43a18898 244{
f03fc89f 245 if ( !wxWindow::Enable(enable) )
902725ee 246 return false;
43a18898 247
29149a64 248 OnSetBitmap();
f03fc89f 249
902725ee 250 return true;
43a18898
RR
251}
252
253void wxBitmapButton::HasFocus()
254{
902725ee 255 m_hasFocus = true;
29149a64 256 OnSetBitmap();
43a18898
RR
257}
258
259void wxBitmapButton::NotFocus()
260{
902725ee 261 m_hasFocus = false;
29149a64 262 OnSetBitmap();
43a18898
RR
263}
264
265void wxBitmapButton::StartSelect()
266{
902725ee 267 m_isSelected = true;
29149a64 268 OnSetBitmap();
43a18898
RR
269}
270
271void wxBitmapButton::EndSelect()
272{
902725ee 273 m_isSelected = false;
29149a64 274 OnSetBitmap();
43a18898 275}
f6bcfd97
BP
276
277#endif // wxUSE_BMPBUTTON