]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bmpbuttn.cpp
fixed wxXmlResource::Load's detection of filenames to be done as early as possible
[wxWidgets.git] / src / gtk / bmpbuttn.cpp
CommitLineData
151ccd11 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/gtk/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
a1abca32 17#include <gtk/gtk.h>
83624f79 18
151ccd11
RR
19//-----------------------------------------------------------------------------
20// classes
21//-----------------------------------------------------------------------------
22
23class wxBitmapButton;
24
66bd6b93
RR
25//-----------------------------------------------------------------------------
26// data
27//-----------------------------------------------------------------------------
28
29extern bool g_blockEventsOnDrag;
30
151ccd11 31//-----------------------------------------------------------------------------
e1e955e1 32// "clicked"
151ccd11
RR
33//-----------------------------------------------------------------------------
34
865bb325 35extern "C" {
66bd6b93 36static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
151ccd11 37{
a2053b27 38 if (!button->m_hasVMT) return;
43a18898 39 if (g_blockEventsOnDrag) return;
f6bcfd97 40
43a18898
RR
41 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
42 event.SetEventObject(button);
937013e0 43 button->HandleWindowEvent(event);
43a18898 44}
865bb325 45}
43a18898
RR
46
47//-----------------------------------------------------------------------------
48// "enter"
49//-----------------------------------------------------------------------------
50
865bb325 51extern "C" {
43a18898
RR
52static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
53{
a2053b27 54 if (!button->m_hasVMT) return;
43a18898
RR
55 if (g_blockEventsOnDrag) return;
56
6fba6c78 57 button->GTKMouseEnters();
43a18898 58}
865bb325 59}
43a18898
RR
60
61//-----------------------------------------------------------------------------
62// "leave"
63//-----------------------------------------------------------------------------
64
865bb325 65extern "C" {
43a18898
RR
66static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
67{
a2053b27 68 if (!button->m_hasVMT) return;
43a18898
RR
69 if (g_blockEventsOnDrag) return;
70
6fba6c78 71 button->GTKMouseLeaves();
43a18898 72}
865bb325 73}
43a18898
RR
74
75//-----------------------------------------------------------------------------
76// "pressed"
77//-----------------------------------------------------------------------------
78
865bb325 79extern "C" {
43a18898
RR
80static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
81{
a2053b27 82 if (!button->m_hasVMT) return;
43a18898
RR
83 if (g_blockEventsOnDrag) return;
84
6fba6c78 85 button->GTKPressed();
43a18898 86}
865bb325 87}
43a18898
RR
88
89//-----------------------------------------------------------------------------
90// "released"
91//-----------------------------------------------------------------------------
92
865bb325 93extern "C" {
43a18898
RR
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
6fba6c78 99 button->GTKReleased();
6de97a3b 100}
865bb325 101}
151ccd11
RR
102
103//-----------------------------------------------------------------------------
e1e955e1
RR
104// wxBitmapButton
105//-----------------------------------------------------------------------------
106
42b4e99e 107IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
151ccd11 108
6fba6c78
VS
109BEGIN_EVENT_TABLE(wxBitmapButton, wxButton)
110 EVT_SET_FOCUS(wxBitmapButton::OnFocusChange)
111 EVT_KILL_FOCUS(wxBitmapButton::OnFocusChange)
112END_EVENT_TABLE()
113
114
c3dfaa10 115void wxBitmapButton::Init()
151ccd11 116{
6fba6c78
VS
117 m_mouseHovers =
118 m_isPressed = false;
6de97a3b 119}
151ccd11 120
c3dfaa10
VZ
121bool wxBitmapButton::Create( wxWindow *parent,
122 wxWindowID id,
123 const wxBitmap& bitmap,
124 const wxPoint& pos,
125 const wxSize& size,
126 long style,
127 const wxValidator& validator,
128 const wxString &name )
151ccd11 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") );
902725ee 134 return false;
4dcaf11a 135 }
6de97a3b 136
189f58fa 137 m_bmpNormal = bitmap;
f6bcfd97 138
43a18898 139 m_widget = gtk_button_new();
9ff9d30c 140 g_object_ref(m_widget);
de1c750f 141
de1c750f
RR
142 if (style & wxNO_BORDER)
143 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 144
1e6feb95 145 if (m_bmpNormal.Ok())
43a18898 146 {
29149a64 147 OnSetBitmap();
43a18898 148 }
f6bcfd97 149
9fa72bd2
MR
150 g_signal_connect_after (m_widget, "clicked",
151 G_CALLBACK (gtk_bmpbutton_clicked_callback),
152 this);
153
154 g_signal_connect (m_widget, "enter",
155 G_CALLBACK (gtk_bmpbutton_enter_callback), this);
156 g_signal_connect (m_widget, "leave",
157 G_CALLBACK (gtk_bmpbutton_leave_callback), this);
158 g_signal_connect (m_widget, "pressed",
159 G_CALLBACK (gtk_bmpbutton_press_callback), this);
160 g_signal_connect (m_widget, "released",
161 G_CALLBACK (gtk_bmpbutton_release_callback), this);
f6bcfd97 162
eb082a08 163 m_parent->DoAddChild( this );
f6bcfd97 164
abdeb9e7 165 PostCreation(size);
f6bcfd97 166
902725ee 167 return true;
6de97a3b 168}
f6bcfd97 169
151ccd11
RR
170void wxBitmapButton::SetLabel( const wxString &label )
171{
223d09f6 172 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
f96aa4d9 173
43a18898 174 wxControl::SetLabel( label );
6de97a3b 175}
151ccd11 176
f40fdaa3 177void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
903f689b 178{
afa7bd1e 179 if (!GTK_BIN(m_widget)->child)
9e691f46 180 return;
29149a64 181
f40fdaa3 182 wxButton::DoApplyWidgetStyle(style);
43a18898
RR
183}
184
29149a64 185void wxBitmapButton::OnSetBitmap()
43a18898 186{
1e6feb95 187 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
f96aa4d9 188
9f884528 189 InvalidateBestSize();
902725ee 190
43a18898 191 wxBitmap the_one;
47a8a4d5 192 if (!IsThisEnabled())
29149a64 193 the_one = m_bmpDisabled;
6fba6c78 194 else if (m_isPressed)
29149a64 195 the_one = m_bmpSelected;
6fba6c78
VS
196 else if (m_mouseHovers)
197 the_one = m_bmpHover;
198 else if (HasFocus())
29149a64 199 the_one = m_bmpFocus;
f6bcfd97 200 else
902725ee 201 the_one = m_bmpNormal;
43a18898 202
6fba6c78
VS
203 if (!the_one.Ok())
204 the_one = m_bmpNormal;
205 if (!the_one.Ok())
206 return;
f6bcfd97 207
4a4a02ac
PC
208 GtkWidget* image = GTK_BIN(m_widget)->child;
209 if (image == NULL)
29149a64 210 {
4a4a02ac
PC
211 image = gtk_image_new();
212 gtk_widget_show(image);
213 gtk_container_add(GTK_CONTAINER(m_widget), image);
67a6726a 214 }
4a4a02ac
PC
215 // always use pixbuf, because pixmap mask does not
216 // work with disabled images in some themes
217 gtk_image_set_from_pixbuf(GTK_IMAGE(image), the_one.GetPixbuf());
903f689b
RR
218}
219
e0aeebed
VS
220wxSize wxBitmapButton::DoGetBestSize() const
221{
222 return wxControl::DoGetBestSize();
223}
224
f03fc89f 225bool wxBitmapButton::Enable( bool enable )
43a18898 226{
f03fc89f 227 if ( !wxWindow::Enable(enable) )
902725ee 228 return false;
43a18898 229
29149a64 230 OnSetBitmap();
f03fc89f 231
902725ee 232 return true;
43a18898
RR
233}
234
6fba6c78
VS
235void wxBitmapButton::GTKMouseEnters()
236{
237 m_mouseHovers = true;
238 OnSetBitmap();
239}
240
241void wxBitmapButton::GTKMouseLeaves()
43a18898 242{
6fba6c78 243 m_mouseHovers = false;
29149a64 244 OnSetBitmap();
43a18898
RR
245}
246
6fba6c78 247void wxBitmapButton::GTKPressed()
43a18898 248{
6fba6c78 249 m_isPressed = true;
29149a64 250 OnSetBitmap();
43a18898
RR
251}
252
6fba6c78 253void wxBitmapButton::GTKReleased()
43a18898 254{
6fba6c78 255 m_isPressed = false;
29149a64 256 OnSetBitmap();
43a18898
RR
257}
258
6fba6c78 259void wxBitmapButton::OnFocusChange(wxFocusEvent& event)
43a18898 260{
6fba6c78 261 event.Skip();
29149a64 262 OnSetBitmap();
43a18898 263}
f6bcfd97
BP
264
265#endif // wxUSE_BMPBUTTON