]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/bmpbuttn.cpp
Added missing includes
[wxWidgets.git] / src / gtk1 / bmpbuttn.cpp
CommitLineData
151ccd11 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/gtk1/bmpbuttn.cpp
151ccd11
RR
3// Purpose:
4// Author: Robert Roebling
f96aa4d9 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
151ccd11
RR
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
dcf924a3
RR
13#if wxUSE_BMPBUTTON
14
1e6feb95
VZ
15#include "wx/bmpbuttn.h"
16
3cbab641 17#include "wx/gtk1/private.h"
83624f79 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
865bb325 42extern "C" {
66bd6b93 43static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
151ccd11 44{
f6bcfd97 45 if (g_isIdle)
bf9e3e73 46 wxapp_install_idle_handler();
acfd422a 47
a2053b27 48 if (!button->m_hasVMT) return;
43a18898 49 if (g_blockEventsOnDrag) return;
f6bcfd97 50
43a18898
RR
51 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
52 event.SetEventObject(button);
937013e0 53 button->HandleWindowEvent(event);
43a18898 54}
865bb325 55}
43a18898
RR
56
57//-----------------------------------------------------------------------------
58// "enter"
59//-----------------------------------------------------------------------------
60
865bb325 61extern "C" {
43a18898
RR
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 68}
865bb325 69}
43a18898
RR
70
71//-----------------------------------------------------------------------------
72// "leave"
73//-----------------------------------------------------------------------------
74
865bb325 75extern "C" {
43a18898
RR
76static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
77{
a2053b27 78 if (!button->m_hasVMT) return;
43a18898
RR
79 if (g_blockEventsOnDrag) return;
80
f6bcfd97 81 button->NotFocus();
43a18898 82}
865bb325 83}
43a18898
RR
84
85//-----------------------------------------------------------------------------
86// "pressed"
87//-----------------------------------------------------------------------------
88
865bb325 89extern "C" {
43a18898
RR
90static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
91{
a2053b27 92 if (!button->m_hasVMT) return;
43a18898
RR
93 if (g_blockEventsOnDrag) return;
94
f6bcfd97 95 button->StartSelect();
43a18898 96}
865bb325 97}
43a18898
RR
98
99//-----------------------------------------------------------------------------
100// "released"
101//-----------------------------------------------------------------------------
102
865bb325 103extern "C" {
43a18898
RR
104static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
105{
a2053b27 106 if (!button->m_hasVMT) return;
43a18898
RR
107 if (g_blockEventsOnDrag) return;
108
f6bcfd97 109 button->EndSelect();
6de97a3b 110}
865bb325 111}
151ccd11
RR
112
113//-----------------------------------------------------------------------------
e1e955e1
RR
114// wxBitmapButton
115//-----------------------------------------------------------------------------
116
42b4e99e 117IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
151ccd11 118
c3dfaa10 119void wxBitmapButton::Init()
151ccd11 120{
c3dfaa10 121 m_hasFocus =
902725ee 122 m_isSelected = false;
6de97a3b 123}
151ccd11 124
c3dfaa10
VZ
125bool wxBitmapButton::Create( wxWindow *parent,
126 wxWindowID id,
127 const wxBitmap& bitmap,
128 const wxPoint& pos,
129 const wxSize& size,
130 long style,
131 const wxValidator& validator,
132 const wxString &name )
151ccd11 133{
902725ee
WS
134 m_needParent = true;
135 m_acceptsFocus = true;
f6bcfd97 136
4dcaf11a
RR
137 if (!PreCreation( parent, pos, size ) ||
138 !CreateBase( parent, id, pos, size, style, validator, name ))
139 {
223d09f6 140 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
902725ee 141 return false;
4dcaf11a 142 }
6de97a3b 143
189f58fa 144 m_bmpNormal = bitmap;
f6bcfd97 145
43a18898 146 m_widget = gtk_button_new();
de1c750f 147
de1c750f
RR
148 if (style & wxNO_BORDER)
149 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 150
1e6feb95 151 if (m_bmpNormal.Ok())
43a18898 152 {
29149a64 153 OnSetBitmap();
43a18898 154 }
f6bcfd97 155
58b907f6 156 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
43a18898 157 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
151ccd11 158
f6bcfd97 159 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
43a18898 160 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
f6bcfd97 161 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
43a18898 162 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
f6bcfd97 163 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
43a18898 164 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
f6bcfd97 165 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
43a18898 166 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
f6bcfd97 167
eb082a08 168 m_parent->DoAddChild( this );
f6bcfd97 169
abdeb9e7 170 PostCreation(size);
f6bcfd97 171
902725ee 172 return true;
6de97a3b 173}
f6bcfd97 174
151ccd11
RR
175void wxBitmapButton::SetLabel( const wxString &label )
176{
223d09f6 177 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
f96aa4d9 178
43a18898 179 wxControl::SetLabel( label );
6de97a3b 180}
151ccd11 181
f40fdaa3 182void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
903f689b 183{
9e691f46
VZ
184 if ( !BUTTON_CHILD(m_widget) )
185 return;
29149a64 186
f40fdaa3 187 wxButton::DoApplyWidgetStyle(style);
43a18898
RR
188}
189
29149a64 190void wxBitmapButton::OnSetBitmap()
43a18898 191{
1e6feb95 192 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
f96aa4d9 193
9f884528 194 InvalidateBestSize();
902725ee 195
43a18898 196 wxBitmap the_one;
47a8a4d5 197 if (!IsThisEnabled())
29149a64 198 the_one = m_bmpDisabled;
67a6726a 199 else if (m_isSelected)
29149a64 200 the_one = m_bmpSelected;
67a6726a 201 else if (m_hasFocus)
29149a64 202 the_one = m_bmpFocus;
f6bcfd97 203 else
902725ee 204 the_one = m_bmpNormal;
43a18898 205
1e6feb95 206 if (!the_one.Ok()) the_one = m_bmpNormal;
de1c750f 207 if (!the_one.Ok()) return;
f6bcfd97 208
43a18898
RR
209 GdkBitmap *mask = (GdkBitmap *) NULL;
210 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
f6bcfd97 211
9e691f46
VZ
212 GtkWidget *child = BUTTON_CHILD(m_widget);
213 if (child == NULL)
29149a64
VZ
214 {
215 // initial bitmap
4fab7128 216 GtkWidget *pixmap;
4fab7128 217 pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
67a6726a
RR
218 gtk_widget_show(pixmap);
219 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
29149a64
VZ
220 }
221 else
67a6726a 222 { // subsequent bitmaps
4fab7128
VS
223 GtkPixmap *pixmap = GTK_PIXMAP(child);
224 gtk_pixmap_set(pixmap, the_one.GetPixmap(), mask);
67a6726a 225 }
903f689b
RR
226}
227
e0aeebed
VS
228wxSize wxBitmapButton::DoGetBestSize() const
229{
230 return wxControl::DoGetBestSize();
231}
232
f03fc89f 233bool wxBitmapButton::Enable( bool enable )
43a18898 234{
f03fc89f 235 if ( !wxWindow::Enable(enable) )
902725ee 236 return false;
43a18898 237
29149a64 238 OnSetBitmap();
f03fc89f 239
902725ee 240 return true;
43a18898
RR
241}
242
243void wxBitmapButton::HasFocus()
244{
902725ee 245 m_hasFocus = true;
29149a64 246 OnSetBitmap();
43a18898
RR
247}
248
249void wxBitmapButton::NotFocus()
250{
902725ee 251 m_hasFocus = false;
29149a64 252 OnSetBitmap();
43a18898
RR
253}
254
255void wxBitmapButton::StartSelect()
256{
902725ee 257 m_isSelected = true;
29149a64 258 OnSetBitmap();
43a18898
RR
259}
260
261void wxBitmapButton::EndSelect()
262{
902725ee 263 m_isSelected = false;
29149a64 264 OnSetBitmap();
43a18898 265}
f6bcfd97
BP
266
267#endif // wxUSE_BMPBUTTON