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