]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bmpbuttn.cpp
Move Objective-C interfaces into separate header files in preparation for being able...
[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);
43 button->GetEventHandler()->ProcessEvent(event);
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
f6bcfd97 57 button->HasFocus();
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
f6bcfd97 71 button->NotFocus();
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
f6bcfd97 85 button->StartSelect();
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
f6bcfd97 99 button->EndSelect();
6de97a3b 100}
865bb325 101}
151ccd11
RR
102
103//-----------------------------------------------------------------------------
e1e955e1
RR
104// wxBitmapButton
105//-----------------------------------------------------------------------------
106
42b4e99e 107IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
151ccd11 108
c3dfaa10 109void wxBitmapButton::Init()
151ccd11 110{
c3dfaa10 111 m_hasFocus =
902725ee 112 m_isSelected = false;
6de97a3b 113}
151ccd11 114
c3dfaa10
VZ
115bool wxBitmapButton::Create( wxWindow *parent,
116 wxWindowID id,
117 const wxBitmap& bitmap,
118 const wxPoint& pos,
119 const wxSize& size,
120 long style,
121 const wxValidator& validator,
122 const wxString &name )
151ccd11 123{
902725ee 124 m_needParent = true;
f6bcfd97 125
4dcaf11a
RR
126 if (!PreCreation( parent, pos, size ) ||
127 !CreateBase( parent, id, pos, size, style, validator, name ))
128 {
223d09f6 129 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
902725ee 130 return false;
4dcaf11a 131 }
6de97a3b 132
189f58fa 133 m_bmpNormal = bitmap;
f6bcfd97 134
43a18898 135 m_widget = gtk_button_new();
de1c750f 136
de1c750f
RR
137 if (style & wxNO_BORDER)
138 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 139
1e6feb95 140 if (m_bmpNormal.Ok())
43a18898 141 {
29149a64 142 OnSetBitmap();
43a18898 143 }
f6bcfd97 144
9fa72bd2
MR
145 g_signal_connect_after (m_widget, "clicked",
146 G_CALLBACK (gtk_bmpbutton_clicked_callback),
147 this);
148
149 g_signal_connect (m_widget, "enter",
150 G_CALLBACK (gtk_bmpbutton_enter_callback), this);
151 g_signal_connect (m_widget, "leave",
152 G_CALLBACK (gtk_bmpbutton_leave_callback), this);
153 g_signal_connect (m_widget, "pressed",
154 G_CALLBACK (gtk_bmpbutton_press_callback), this);
155 g_signal_connect (m_widget, "released",
156 G_CALLBACK (gtk_bmpbutton_release_callback), this);
f6bcfd97 157
eb082a08 158 m_parent->DoAddChild( this );
f6bcfd97 159
abdeb9e7 160 PostCreation(size);
f6bcfd97 161
902725ee 162 return true;
6de97a3b 163}
f6bcfd97 164
151ccd11
RR
165void wxBitmapButton::SetLabel( const wxString &label )
166{
223d09f6 167 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
f96aa4d9 168
43a18898 169 wxControl::SetLabel( label );
6de97a3b 170}
151ccd11 171
f40fdaa3 172void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
903f689b 173{
afa7bd1e 174 if (!GTK_BIN(m_widget)->child)
9e691f46 175 return;
29149a64 176
f40fdaa3 177 wxButton::DoApplyWidgetStyle(style);
43a18898
RR
178}
179
29149a64 180void wxBitmapButton::OnSetBitmap()
43a18898 181{
1e6feb95 182 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
f96aa4d9 183
9f884528 184 InvalidateBestSize();
902725ee 185
43a18898 186 wxBitmap the_one;
47a8a4d5 187 if (!IsThisEnabled())
29149a64 188 the_one = m_bmpDisabled;
67a6726a 189 else if (m_isSelected)
29149a64 190 the_one = m_bmpSelected;
67a6726a 191 else if (m_hasFocus)
29149a64 192 the_one = m_bmpFocus;
f6bcfd97 193 else
902725ee 194 the_one = m_bmpNormal;
43a18898 195
1e6feb95 196 if (!the_one.Ok()) the_one = m_bmpNormal;
de1c750f 197 if (!the_one.Ok()) return;
f6bcfd97 198
43a18898
RR
199 GdkBitmap *mask = (GdkBitmap *) NULL;
200 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
f6bcfd97 201
afa7bd1e 202 GtkWidget *child = GTK_BIN(m_widget)->child;
9e691f46 203 if (child == NULL)
29149a64
VZ
204 {
205 // initial bitmap
4fab7128 206 GtkWidget *pixmap;
68567a96 207
4fab7128
VS
208 if (the_one.HasPixbuf())
209 pixmap = gtk_image_new_from_pixbuf(the_one.GetPixbuf());
210 else
211 pixmap = gtk_image_new_from_pixmap(the_one.GetPixmap(), mask);
68567a96 212
67a6726a
RR
213 gtk_widget_show(pixmap);
214 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
29149a64
VZ
215 }
216 else
67a6726a 217 { // subsequent bitmaps
4fab7128
VS
218 GtkImage *pixmap = GTK_IMAGE(child);
219 if (the_one.HasPixbuf())
220 gtk_image_set_from_pixbuf(pixmap, the_one.GetPixbuf());
221 else
222 gtk_image_set_from_pixmap(pixmap, the_one.GetPixmap(), mask);
67a6726a 223 }
903f689b
RR
224}
225
e0aeebed
VS
226wxSize wxBitmapButton::DoGetBestSize() const
227{
228 return wxControl::DoGetBestSize();
229}
230
f03fc89f 231bool wxBitmapButton::Enable( bool enable )
43a18898 232{
f03fc89f 233 if ( !wxWindow::Enable(enable) )
902725ee 234 return false;
43a18898 235
29149a64 236 OnSetBitmap();
f03fc89f 237
902725ee 238 return true;
43a18898
RR
239}
240
241void wxBitmapButton::HasFocus()
242{
902725ee 243 m_hasFocus = true;
29149a64 244 OnSetBitmap();
43a18898
RR
245}
246
247void wxBitmapButton::NotFocus()
248{
902725ee 249 m_hasFocus = false;
29149a64 250 OnSetBitmap();
43a18898
RR
251}
252
253void wxBitmapButton::StartSelect()
254{
902725ee 255 m_isSelected = true;
29149a64 256 OnSetBitmap();
43a18898
RR
257}
258
259void wxBitmapButton::EndSelect()
260{
902725ee 261 m_isSelected = false;
29149a64 262 OnSetBitmap();
43a18898 263}
f6bcfd97
BP
264
265#endif // wxUSE_BMPBUTTON