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