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