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