]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bmpbuttn.cpp
Useful little fix that automatically hides the busy cursor in modal dialogs.
[wxWidgets.git] / src / gtk / bmpbuttn.cpp
CommitLineData
151ccd11
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: bmpbuttn.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
151ccd11
RR
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "bmpbuttn.h"
12#endif
13
14#include "wx/bmpbuttn.h"
15
dcf924a3
RR
16#if wxUSE_BMPBUTTON
17
83624f79
RR
18#include "gdk/gdk.h"
19#include "gtk/gtk.h"
20
151ccd11
RR
21//-----------------------------------------------------------------------------
22// classes
23//-----------------------------------------------------------------------------
24
25class wxBitmapButton;
26
acfd422a
RR
27//-----------------------------------------------------------------------------
28// idle system
29//-----------------------------------------------------------------------------
30
31extern void wxapp_install_idle_handler();
32extern bool g_isIdle;
33
66bd6b93
RR
34//-----------------------------------------------------------------------------
35// data
36//-----------------------------------------------------------------------------
37
38extern bool g_blockEventsOnDrag;
39
151ccd11 40//-----------------------------------------------------------------------------
e1e955e1 41// "clicked"
151ccd11
RR
42//-----------------------------------------------------------------------------
43
66bd6b93 44static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
151ccd11 45{
acfd422a
RR
46 if (g_isIdle) wxapp_install_idle_handler();
47
a2053b27 48 if (!button->m_hasVMT) return;
43a18898 49 if (g_blockEventsOnDrag) return;
66bd6b93 50
43a18898
RR
51 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
52 event.SetEventObject(button);
53 button->GetEventHandler()->ProcessEvent(event);
54}
55
56//-----------------------------------------------------------------------------
57// "enter"
58//-----------------------------------------------------------------------------
59
60static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
61{
a2053b27 62 if (!button->m_hasVMT) return;
43a18898
RR
63 if (g_blockEventsOnDrag) return;
64
65 button->HasFocus();
66}
67
68//-----------------------------------------------------------------------------
69// "leave"
70//-----------------------------------------------------------------------------
71
72static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
73{
a2053b27 74 if (!button->m_hasVMT) return;
43a18898
RR
75 if (g_blockEventsOnDrag) return;
76
77 button->NotFocus();
78}
79
80//-----------------------------------------------------------------------------
81// "pressed"
82//-----------------------------------------------------------------------------
83
84static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
85{
a2053b27 86 if (!button->m_hasVMT) return;
43a18898
RR
87 if (g_blockEventsOnDrag) return;
88
89 button->StartSelect();
90}
91
92//-----------------------------------------------------------------------------
93// "released"
94//-----------------------------------------------------------------------------
95
96static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
97{
a2053b27 98 if (!button->m_hasVMT) return;
43a18898
RR
99 if (g_blockEventsOnDrag) return;
100
101 button->EndSelect();
6de97a3b 102}
151ccd11
RR
103
104//-----------------------------------------------------------------------------
e1e955e1
RR
105// wxBitmapButton
106//-----------------------------------------------------------------------------
107
108IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxControl)
151ccd11 109
43a18898 110wxBitmapButton::wxBitmapButton()
151ccd11 111{
6de97a3b 112}
151ccd11 113
43a18898
RR
114bool wxBitmapButton::Create( wxWindow *parent, wxWindowID id, const wxBitmap &bitmap,
115 const wxPoint &pos, const wxSize &size,
116 long style, const wxValidator& validator, const wxString &name )
151ccd11 117{
43a18898 118 m_needParent = TRUE;
b292e2f5 119 m_acceptsFocus = TRUE;
151ccd11 120
4dcaf11a
RR
121 if (!PreCreation( parent, pos, size ) ||
122 !CreateBase( parent, id, pos, size, style, validator, name ))
123 {
124 wxFAIL_MSG( _T("wxBitmapButton creation failed") );
125 return FALSE;
126 }
6de97a3b 127
43a18898
RR
128 m_bitmap = bitmap;
129 m_disabled = bitmap;
130 m_focus = bitmap;
131 m_selected = bitmap;
151ccd11 132
43a18898 133 m_label = "";
151ccd11 134
43a18898 135 m_widget = gtk_button_new();
de1c750f
RR
136
137#if (GTK_MINOR_VERSION > 0)
138 if (style & wxNO_BORDER)
139 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
140#endif
141
43a18898
RR
142 if (m_bitmap.Ok())
143 {
4dcaf11a
RR
144 wxSize newSize = size;
145
43a18898
RR
146 GdkBitmap *mask = (GdkBitmap *) NULL;
147 if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap();
148 GtkWidget *pixmap = gtk_pixmap_new( m_bitmap.GetPixmap(), mask );
151ccd11 149
43a18898
RR
150 gtk_widget_show( pixmap );
151 gtk_container_add( GTK_CONTAINER(m_widget), pixmap );
d1af991f
RR
152
153 int border = 10;
154 if (style & wxNO_BORDER) border = 4;
155 if (newSize.x == -1) newSize.x = m_bitmap.GetWidth()+border;
156 if (newSize.y == -1) newSize.y = m_bitmap.GetHeight()+border;
157 SetSize( newSize.x, newSize.y );
43a18898 158 }
151ccd11 159
43a18898
RR
160 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
161 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
151ccd11 162
43a18898
RR
163 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
164 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
165 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
166 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
167 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
168 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
169 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
170 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
171
eb082a08 172 m_parent->DoAddChild( this );
6ca41e57 173
43a18898 174 PostCreation();
151ccd11 175
43a18898 176 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 177
43a18898 178 Show( TRUE );
151ccd11 179
43a18898 180 return TRUE;
6de97a3b 181}
151ccd11 182
43a18898 183void wxBitmapButton::SetDefault()
151ccd11 184{
43a18898
RR
185 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
186 gtk_widget_grab_default( m_widget );
3502e687
RR
187
188 SetSize( m_x, m_y, m_width, m_height );
6de97a3b 189}
151ccd11
RR
190
191void wxBitmapButton::SetLabel( const wxString &label )
192{
62bd5cf0 193 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
f96aa4d9 194
43a18898 195 wxControl::SetLabel( label );
6de97a3b 196}
151ccd11 197
43a18898 198wxString wxBitmapButton::GetLabel() const
151ccd11 199{
62bd5cf0 200 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid button") );
f96aa4d9 201
43a18898 202 return wxControl::GetLabel();
6de97a3b 203}
903f689b 204
43a18898 205void wxBitmapButton::ApplyWidgetStyle()
903f689b 206{
43a18898
RR
207}
208
209void wxBitmapButton::SetBitmap()
210{
62bd5cf0 211 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
f96aa4d9 212
43a18898 213 wxBitmap the_one;
903f689b 214
de1c750f 215 if (!m_isEnabled)
43a18898
RR
216 the_one = m_disabled;
217 else
218 {
de1c750f 219 if (m_isSelected)
43a18898
RR
220 {
221 the_one = m_selected;
222 }
223 else
224 {
de1c750f 225 if (m_hasFocus)
43a18898
RR
226 the_one = m_focus;
227 else
228 the_one = m_bitmap;
229 }
230 }
231
de1c750f
RR
232 if (!the_one.Ok()) the_one = m_bitmap;
233 if (!the_one.Ok()) return;
903f689b 234
43a18898
RR
235 GtkButton *bin = GTK_BUTTON( m_widget );
236 GtkPixmap *g_pixmap = GTK_PIXMAP( bin->child );
903f689b 237
43a18898
RR
238 GdkBitmap *mask = (GdkBitmap *) NULL;
239 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
240
241 gtk_pixmap_set( g_pixmap, the_one.GetPixmap(), mask );
903f689b
RR
242}
243
43a18898 244void wxBitmapButton::SetBitmapDisabled( const wxBitmap& bitmap )
58614078 245{
62bd5cf0 246 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
43a18898
RR
247
248 if ( ! m_disabled.Ok() ) return;
249 m_disabled = bitmap;
250
251 SetBitmap();
58614078
RR
252}
253
43a18898
RR
254void wxBitmapButton::SetBitmapFocus( const wxBitmap& bitmap )
255{
62bd5cf0 256 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
43a18898
RR
257
258 if ( ! m_focus.Ok() ) return;
259 m_focus = bitmap;
260
261 SetBitmap();
262}
263
264void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap )
265{
62bd5cf0 266 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
43a18898
RR
267
268 if (!m_bitmap.Ok()) return;
269 m_bitmap = bitmap;
270
271 SetBitmap();
272}
273
274void wxBitmapButton::SetBitmapSelected( const wxBitmap& bitmap )
275{
62bd5cf0 276 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
43a18898
RR
277
278 if ( ! m_selected.Ok() ) return;
279 m_selected = bitmap;
280
281 SetBitmap();
282}
283
f03fc89f 284bool wxBitmapButton::Enable( bool enable )
43a18898 285{
f03fc89f
VZ
286 if ( !wxWindow::Enable(enable) )
287 return FALSE;
43a18898 288
f03fc89f
VZ
289 SetBitmap();
290
291 return TRUE;
43a18898
RR
292}
293
294void wxBitmapButton::HasFocus()
295{
296 m_hasFocus = TRUE;
297 SetBitmap();
298}
299
300void wxBitmapButton::NotFocus()
301{
302 m_hasFocus = FALSE;
303 SetBitmap();
304}
305
306void wxBitmapButton::StartSelect()
307{
308 m_isSelected = TRUE;
309 SetBitmap();
310}
311
312void wxBitmapButton::EndSelect()
313{
314 m_isSelected = FALSE;
315 SetBitmap();
316}
dcf924a3
RR
317
318#endif