]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bmpbuttn.cpp
1. fixed wxBase RPM template
[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 146 wxSize newSize = size;
67a6726a 147 int border = (style & wxNO_BORDER) ? 4 : 10;
d1af991f
RR
148 if (newSize.x == -1) newSize.x = m_bitmap.GetWidth()+border;
149 if (newSize.y == -1) newSize.y = m_bitmap.GetHeight()+border;
150 SetSize( newSize.x, newSize.y );
67a6726a 151 SetBitmap();
43a18898 152 }
f6bcfd97
BP
153
154 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
43a18898 155 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
151ccd11 156
f6bcfd97 157 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
43a18898 158 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
f6bcfd97 159 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
43a18898 160 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
f6bcfd97 161 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
43a18898 162 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
f6bcfd97 163 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
43a18898 164 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
f6bcfd97 165
eb082a08 166 m_parent->DoAddChild( this );
f6bcfd97 167
43a18898 168 PostCreation();
f6bcfd97 169
43a18898 170 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 171
43a18898 172 Show( TRUE );
f6bcfd97 173
43a18898 174 return TRUE;
6de97a3b 175}
f6bcfd97 176
43a18898 177void wxBitmapButton::SetDefault()
151ccd11 178{
43a18898
RR
179 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
180 gtk_widget_grab_default( m_widget );
f6bcfd97 181
3502e687 182 SetSize( m_x, m_y, m_width, m_height );
6de97a3b 183}
151ccd11
RR
184
185void wxBitmapButton::SetLabel( const wxString &label )
186{
223d09f6 187 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
f96aa4d9 188
43a18898 189 wxControl::SetLabel( label );
6de97a3b 190}
151ccd11 191
43a18898 192wxString wxBitmapButton::GetLabel() const
151ccd11 193{
223d09f6 194 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid button") );
f96aa4d9 195
43a18898 196 return wxControl::GetLabel();
6de97a3b 197}
903f689b 198
43a18898 199void wxBitmapButton::ApplyWidgetStyle()
903f689b 200{
67a6726a
RR
201 if (GTK_BUTTON(m_widget)->child == NULL) return;
202
203 wxButton::ApplyWidgetStyle();
43a18898
RR
204}
205
206void wxBitmapButton::SetBitmap()
207{
223d09f6 208 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
f96aa4d9 209
43a18898 210 wxBitmap the_one;
f6bcfd97 211 if (!m_isEnabled)
43a18898 212 the_one = m_disabled;
67a6726a
RR
213 else if (m_isSelected)
214 the_one = m_selected;
215 else if (m_hasFocus)
216 the_one = m_focus;
f6bcfd97 217 else
67a6726a 218 the_one = m_bitmap;
43a18898 219
de1c750f
RR
220 if (!the_one.Ok()) the_one = m_bitmap;
221 if (!the_one.Ok()) return;
f6bcfd97 222
43a18898
RR
223 GdkBitmap *mask = (GdkBitmap *) NULL;
224 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
f6bcfd97 225
67a6726a
RR
226 GtkButton *bin = GTK_BUTTON(m_widget);
227 if (bin->child == NULL)
228 { // initial bitmap
229 GtkWidget *pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
230 gtk_widget_show(pixmap);
231 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
232 }
233 else
234 { // subsequent bitmaps
235 GtkPixmap *g_pixmap = GTK_PIXMAP(bin->child);
236 gtk_pixmap_set(g_pixmap, the_one.GetPixmap(), mask);
237 }
903f689b
RR
238}
239
f6bcfd97 240void wxBitmapButton::SetBitmapDisabled( const wxBitmap& bitmap )
58614078 241{
67a6726a
RR
242 if (!bitmap.Ok()) return;
243 m_disabled = bitmap;
43a18898 244
67a6726a 245 SetBitmap();
58614078
RR
246}
247
f6bcfd97 248void wxBitmapButton::SetBitmapFocus( const wxBitmap& bitmap )
43a18898 249{
67a6726a 250 if (!bitmap.Ok()) return;
43a18898
RR
251 m_focus = bitmap;
252
253 SetBitmap();
254}
255
256void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap )
257{
67a6726a 258 if (!bitmap.Ok()) return;
43a18898 259 m_bitmap = bitmap;
f6bcfd97 260
43a18898
RR
261 SetBitmap();
262}
263
264void wxBitmapButton::SetBitmapSelected( const wxBitmap& bitmap )
265{
67a6726a 266 if (!bitmap.Ok()) return;
43a18898
RR
267 m_selected = bitmap;
268
269 SetBitmap();
270}
271
f03fc89f 272bool wxBitmapButton::Enable( bool enable )
43a18898 273{
f03fc89f
VZ
274 if ( !wxWindow::Enable(enable) )
275 return FALSE;
43a18898 276
f03fc89f
VZ
277 SetBitmap();
278
279 return TRUE;
43a18898
RR
280}
281
282void wxBitmapButton::HasFocus()
283{
284 m_hasFocus = TRUE;
285 SetBitmap();
286}
287
288void wxBitmapButton::NotFocus()
289{
290 m_hasFocus = FALSE;
291 SetBitmap();
292}
293
294void wxBitmapButton::StartSelect()
295{
296 m_isSelected = TRUE;
297 SetBitmap();
298}
299
300void wxBitmapButton::EndSelect()
301{
302 m_isSelected = FALSE;
303 SetBitmap();
304}
f6bcfd97
BP
305
306#endif // wxUSE_BMPBUTTON
dcf924a3 307