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