]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bmpbuttn.cpp
mac paths updated
[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();
de1c750f 140
de1c750f
RR
141 if (style & wxNO_BORDER)
142 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 143
1e6feb95 144 if (m_bmpNormal.Ok())
43a18898 145 {
29149a64 146 OnSetBitmap();
43a18898 147 }
f6bcfd97 148
9fa72bd2
MR
149 g_signal_connect_after (m_widget, "clicked",
150 G_CALLBACK (gtk_bmpbutton_clicked_callback),
151 this);
152
153 g_signal_connect (m_widget, "enter",
154 G_CALLBACK (gtk_bmpbutton_enter_callback), this);
155 g_signal_connect (m_widget, "leave",
156 G_CALLBACK (gtk_bmpbutton_leave_callback), this);
157 g_signal_connect (m_widget, "pressed",
158 G_CALLBACK (gtk_bmpbutton_press_callback), this);
159 g_signal_connect (m_widget, "released",
160 G_CALLBACK (gtk_bmpbutton_release_callback), this);
f6bcfd97 161
eb082a08 162 m_parent->DoAddChild( this );
f6bcfd97 163
abdeb9e7 164 PostCreation(size);
f6bcfd97 165
902725ee 166 return true;
6de97a3b 167}
f6bcfd97 168
151ccd11
RR
169void wxBitmapButton::SetLabel( const wxString &label )
170{
223d09f6 171 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
f96aa4d9 172
43a18898 173 wxControl::SetLabel( label );
6de97a3b 174}
151ccd11 175
f40fdaa3 176void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
903f689b 177{
afa7bd1e 178 if (!GTK_BIN(m_widget)->child)
9e691f46 179 return;
29149a64 180
f40fdaa3 181 wxButton::DoApplyWidgetStyle(style);
43a18898
RR
182}
183
29149a64 184void wxBitmapButton::OnSetBitmap()
43a18898 185{
1e6feb95 186 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
f96aa4d9 187
9f884528 188 InvalidateBestSize();
902725ee 189
43a18898 190 wxBitmap the_one;
47a8a4d5 191 if (!IsThisEnabled())
29149a64 192 the_one = m_bmpDisabled;
6fba6c78 193 else if (m_isPressed)
29149a64 194 the_one = m_bmpSelected;
6fba6c78
VS
195 else if (m_mouseHovers)
196 the_one = m_bmpHover;
197 else if (HasFocus())
29149a64 198 the_one = m_bmpFocus;
f6bcfd97 199 else
902725ee 200 the_one = m_bmpNormal;
43a18898 201
6fba6c78
VS
202 if (!the_one.Ok())
203 the_one = m_bmpNormal;
204 if (!the_one.Ok())
205 return;
f6bcfd97 206
afa7bd1e 207 GtkWidget *child = GTK_BIN(m_widget)->child;
9e691f46 208 if (child == NULL)
29149a64
VZ
209 {
210 // initial bitmap
6fba6c78 211 GtkWidget *pixmap =
e0cfa715 212 gtk_image_new_from_pixbuf(the_one.GetPixbuf());
68567a96 213
67a6726a
RR
214 gtk_widget_show(pixmap);
215 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
29149a64
VZ
216 }
217 else
67a6726a 218 { // subsequent bitmaps
4fab7128 219 GtkImage *pixmap = GTK_IMAGE(child);
e0cfa715 220 gtk_image_set_from_pixbuf(pixmap, the_one.GetPixbuf());
67a6726a 221 }
903f689b
RR
222}
223
e0aeebed
VS
224wxSize wxBitmapButton::DoGetBestSize() const
225{
226 return wxControl::DoGetBestSize();
227}
228
f03fc89f 229bool wxBitmapButton::Enable( bool enable )
43a18898 230{
f03fc89f 231 if ( !wxWindow::Enable(enable) )
902725ee 232 return false;
43a18898 233
29149a64 234 OnSetBitmap();
f03fc89f 235
902725ee 236 return true;
43a18898
RR
237}
238
6fba6c78
VS
239void wxBitmapButton::GTKMouseEnters()
240{
241 m_mouseHovers = true;
242 OnSetBitmap();
243}
244
245void wxBitmapButton::GTKMouseLeaves()
43a18898 246{
6fba6c78 247 m_mouseHovers = false;
29149a64 248 OnSetBitmap();
43a18898
RR
249}
250
6fba6c78 251void wxBitmapButton::GTKPressed()
43a18898 252{
6fba6c78 253 m_isPressed = true;
29149a64 254 OnSetBitmap();
43a18898
RR
255}
256
6fba6c78 257void wxBitmapButton::GTKReleased()
43a18898 258{
6fba6c78 259 m_isPressed = false;
29149a64 260 OnSetBitmap();
43a18898
RR
261}
262
6fba6c78 263void wxBitmapButton::OnFocusChange(wxFocusEvent& event)
43a18898 264{
6fba6c78 265 event.Skip();
29149a64 266 OnSetBitmap();
43a18898 267}
f6bcfd97
BP
268
269#endif // wxUSE_BMPBUTTON