]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bmpbuttn.cpp
Changed code to get rid of a compiler warning about statement always being FALSE
[wxWidgets.git] / src / gtk / bmpbuttn.cpp
CommitLineData
151ccd11 1/////////////////////////////////////////////////////////////////////////////
f6bcfd97 2// Name: gtk/bmpbuttn.cpp
151ccd11
RR
3// Purpose:
4// Author: Robert Roebling
f96aa4d9 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
f6bcfd97 7// Licence: wxWindows licence
151ccd11
RR
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{
f6bcfd97 46 if (g_isIdle)
bf9e3e73 47 wxapp_install_idle_handler();
acfd422a 48
a2053b27 49 if (!button->m_hasVMT) return;
43a18898 50 if (g_blockEventsOnDrag) return;
f6bcfd97 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
f6bcfd97 66 button->HasFocus();
43a18898
RR
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
f6bcfd97 78 button->NotFocus();
43a18898
RR
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
f6bcfd97 90 button->StartSelect();
43a18898
RR
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
f6bcfd97 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 115bool wxBitmapButton::Create( wxWindow *parent, wxWindowID id, const wxBitmap &bitmap,
f6bcfd97 116 const wxPoint &pos, const wxSize &size,
43a18898 117 long style, const wxValidator& validator, const wxString &name )
151ccd11 118{
43a18898 119 m_needParent = TRUE;
b292e2f5 120 m_acceptsFocus = TRUE;
f6bcfd97
BP
121
122 m_marginX =
123 m_marginY = 0;
124
4dcaf11a
RR
125 if (!PreCreation( parent, pos, size ) ||
126 !CreateBase( parent, id, pos, size, style, validator, name ))
127 {
223d09f6 128 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
f6bcfd97 129 return FALSE;
4dcaf11a 130 }
6de97a3b 131
43a18898
RR
132 m_bitmap = bitmap;
133 m_disabled = bitmap;
134 m_focus = bitmap;
135 m_selected = bitmap;
f6bcfd97 136
43a18898 137 m_widget = gtk_button_new();
de1c750f 138
f6bcfd97 139#if (GTK_MINOR_VERSION > 0)
de1c750f
RR
140 if (style & wxNO_BORDER)
141 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
142#endif
143
43a18898
RR
144 if (m_bitmap.Ok())
145 {
4dcaf11a
RR
146 wxSize newSize = size;
147
43a18898
RR
148 GdkBitmap *mask = (GdkBitmap *) NULL;
149 if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap();
150 GtkWidget *pixmap = gtk_pixmap_new( m_bitmap.GetPixmap(), mask );
f6bcfd97 151
43a18898
RR
152 gtk_widget_show( pixmap );
153 gtk_container_add( GTK_CONTAINER(m_widget), pixmap );
f6bcfd97 154
d1af991f
RR
155 int border = 10;
156 if (style & wxNO_BORDER) border = 4;
157 if (newSize.x == -1) newSize.x = m_bitmap.GetWidth()+border;
158 if (newSize.y == -1) newSize.y = m_bitmap.GetHeight()+border;
159 SetSize( newSize.x, newSize.y );
43a18898 160 }
f6bcfd97
BP
161
162 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
43a18898 163 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
151ccd11 164
f6bcfd97 165 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
43a18898 166 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
f6bcfd97 167 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
43a18898 168 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
f6bcfd97 169 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
43a18898 170 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
f6bcfd97 171 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
43a18898 172 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
f6bcfd97 173
eb082a08 174 m_parent->DoAddChild( this );
f6bcfd97 175
43a18898 176 PostCreation();
f6bcfd97 177
43a18898 178 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 179
43a18898 180 Show( TRUE );
f6bcfd97 181
43a18898 182 return TRUE;
6de97a3b 183}
f6bcfd97 184
43a18898 185void wxBitmapButton::SetDefault()
151ccd11 186{
43a18898
RR
187 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
188 gtk_widget_grab_default( m_widget );
f6bcfd97 189
3502e687 190 SetSize( m_x, m_y, m_width, m_height );
6de97a3b 191}
151ccd11
RR
192
193void wxBitmapButton::SetLabel( const wxString &label )
194{
223d09f6 195 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
f96aa4d9 196
43a18898 197 wxControl::SetLabel( label );
6de97a3b 198}
151ccd11 199
43a18898 200wxString wxBitmapButton::GetLabel() const
151ccd11 201{
223d09f6 202 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid button") );
f96aa4d9 203
43a18898 204 return wxControl::GetLabel();
6de97a3b 205}
903f689b 206
43a18898 207void wxBitmapButton::ApplyWidgetStyle()
903f689b 208{
43a18898
RR
209}
210
211void wxBitmapButton::SetBitmap()
212{
223d09f6 213 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
f96aa4d9 214
43a18898 215 wxBitmap the_one;
f6bcfd97
BP
216
217 if (!m_isEnabled)
43a18898 218 the_one = m_disabled;
f6bcfd97 219 else
43a18898 220 {
f6bcfd97
BP
221 if (m_isSelected)
222 {
223 the_one = m_selected;
224 }
225 else
226 {
227 if (m_hasFocus)
228 the_one = m_focus;
229 else
230 the_one = m_bitmap;
43a18898
RR
231 }
232 }
233
de1c750f
RR
234 if (!the_one.Ok()) the_one = m_bitmap;
235 if (!the_one.Ok()) return;
f6bcfd97 236
43a18898
RR
237 GtkButton *bin = GTK_BUTTON( m_widget );
238 GtkPixmap *g_pixmap = GTK_PIXMAP( bin->child );
f6bcfd97 239
43a18898
RR
240 GdkBitmap *mask = (GdkBitmap *) NULL;
241 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
f6bcfd97 242
43a18898 243 gtk_pixmap_set( g_pixmap, the_one.GetPixmap(), mask );
903f689b
RR
244}
245
f6bcfd97 246void wxBitmapButton::SetBitmapDisabled( const wxBitmap& bitmap )
58614078 247{
223d09f6 248 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
43a18898
RR
249
250 if ( ! m_disabled.Ok() ) return;
251 m_disabled = bitmap;
252
253 SetBitmap();
58614078
RR
254}
255
f6bcfd97 256void wxBitmapButton::SetBitmapFocus( const wxBitmap& bitmap )
43a18898 257{
223d09f6 258 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
43a18898
RR
259
260 if ( ! m_focus.Ok() ) return;
261 m_focus = bitmap;
262
263 SetBitmap();
264}
265
266void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap )
267{
223d09f6 268 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
43a18898
RR
269
270 if (!m_bitmap.Ok()) return;
271 m_bitmap = bitmap;
f6bcfd97 272
43a18898
RR
273 SetBitmap();
274}
275
276void wxBitmapButton::SetBitmapSelected( const wxBitmap& bitmap )
277{
223d09f6 278 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
43a18898
RR
279
280 if ( ! m_selected.Ok() ) return;
281 m_selected = bitmap;
282
283 SetBitmap();
284}
285
f03fc89f 286bool wxBitmapButton::Enable( bool enable )
43a18898 287{
f03fc89f
VZ
288 if ( !wxWindow::Enable(enable) )
289 return FALSE;
43a18898 290
f03fc89f
VZ
291 SetBitmap();
292
293 return TRUE;
43a18898
RR
294}
295
296void wxBitmapButton::HasFocus()
297{
298 m_hasFocus = TRUE;
299 SetBitmap();
300}
301
302void wxBitmapButton::NotFocus()
303{
304 m_hasFocus = FALSE;
305 SetBitmap();
306}
307
308void wxBitmapButton::StartSelect()
309{
310 m_isSelected = TRUE;
311 SetBitmap();
312}
313
314void wxBitmapButton::EndSelect()
315{
316 m_isSelected = FALSE;
317 SetBitmap();
318}
f6bcfd97
BP
319
320#endif // wxUSE_BMPBUTTON
dcf924a3 321