]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
93763ad5 | 2 | // Name: src/gtk1/button.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
dbf858b5 | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
1e6feb95 VZ |
13 | #if wxUSE_BUTTON |
14 | ||
c801d85f | 15 | #include "wx/button.h" |
5f7bcb48 | 16 | #include "wx/stockitem.h" |
c801d85f | 17 | |
3cbab641 MR |
18 | #include "wx/gtk1/private.h" |
19 | #include "wx/gtk1/win_gtk.h" | |
83624f79 | 20 | |
c801d85f KB |
21 | //----------------------------------------------------------------------------- |
22 | // classes | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | class wxButton; | |
26 | ||
acfd422a RR |
27 | //----------------------------------------------------------------------------- |
28 | // idle system | |
29 | //----------------------------------------------------------------------------- | |
30 | ||
31 | extern void wxapp_install_idle_handler(); | |
32 | extern bool g_isIdle; | |
33 | ||
66bd6b93 RR |
34 | //----------------------------------------------------------------------------- |
35 | // data | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | extern bool g_blockEventsOnDrag; | |
39 | ||
c801d85f | 40 | //----------------------------------------------------------------------------- |
e1e955e1 | 41 | // "clicked" |
c801d85f KB |
42 | //----------------------------------------------------------------------------- |
43 | ||
865bb325 | 44 | extern "C" { |
66bd6b93 | 45 | static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button ) |
c801d85f | 46 | { |
9e691f46 | 47 | if (g_isIdle) |
32ac755d | 48 | wxapp_install_idle_handler(); |
acfd422a | 49 | |
a2053b27 | 50 | if (!button->m_hasVMT) return; |
acfd422a | 51 | if (g_blockEventsOnDrag) return; |
9e691f46 | 52 | |
acfd422a RR |
53 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId()); |
54 | event.SetEventObject(button); | |
55 | button->GetEventHandler()->ProcessEvent(event); | |
6de97a3b | 56 | } |
865bb325 | 57 | } |
c801d85f | 58 | |
a90c0600 RR |
59 | //----------------------------------------------------------------------------- |
60 | // "style_set" from m_widget | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
63 | static gint | |
64 | gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win ) | |
65 | { | |
66 | if (g_isIdle) | |
67 | wxapp_install_idle_handler(); | |
b2ff89d6 | 68 | |
f893066b RR |
69 | int left_border = 0; |
70 | int right_border = 0; | |
71 | int top_border = 0; | |
72 | int bottom_border = 0; | |
b2ff89d6 | 73 | |
f893066b RR |
74 | /* the default button has a border around it */ |
75 | if (GTK_WIDGET_CAN_DEFAULT(m_widget)) | |
76 | { | |
f893066b RR |
77 | left_border = 6; |
78 | right_border = 6; | |
79 | top_border = 6; | |
80 | bottom_border = 5; | |
f893066b RR |
81 | win->DoMoveWindow( win->m_x-top_border, |
82 | win->m_y-left_border, | |
83 | win->m_width+left_border+right_border, | |
84 | win->m_height+top_border+bottom_border ); | |
b2ff89d6 | 85 | } |
a90c0600 RR |
86 | |
87 | return FALSE; | |
88 | } | |
89 | ||
c801d85f | 90 | //----------------------------------------------------------------------------- |
e1e955e1 RR |
91 | // wxButton |
92 | //----------------------------------------------------------------------------- | |
93 | ||
94 | IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl) | |
c801d85f | 95 | |
fd0eed64 | 96 | wxButton::wxButton() |
c801d85f | 97 | { |
6de97a3b | 98 | } |
c801d85f | 99 | |
fd0eed64 RR |
100 | wxButton::~wxButton() |
101 | { | |
fd0eed64 RR |
102 | } |
103 | ||
c801d85f | 104 | bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label, |
32c77a71 | 105 | const wxPoint &pos, const wxSize &size, |
6de97a3b | 106 | long style, const wxValidator& validator, const wxString &name ) |
c801d85f | 107 | { |
93763ad5 WS |
108 | m_needParent = true; |
109 | m_acceptsFocus = true; | |
32c77a71 | 110 | |
4dcaf11a RR |
111 | if (!PreCreation( parent, pos, size ) || |
112 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
113 | { | |
223d09f6 | 114 | wxFAIL_MSG( wxT("wxButton creation failed") ); |
93763ad5 | 115 | return false; |
4dcaf11a | 116 | } |
c801d85f | 117 | |
354aa1e3 RR |
118 | m_widget = gtk_button_new_with_label(""); |
119 | ||
2e8613b7 RR |
120 | float x_alignment = 0.5; |
121 | if (HasFlag(wxBU_LEFT)) | |
122 | x_alignment = 0.0; | |
123 | else if (HasFlag(wxBU_RIGHT)) | |
124 | x_alignment = 1.0; | |
125 | ||
126 | float y_alignment = 0.5; | |
127 | if (HasFlag(wxBU_TOP)) | |
128 | y_alignment = 0.0; | |
129 | else if (HasFlag(wxBU_BOTTOM)) | |
130 | y_alignment = 1.0; | |
131 | ||
3cbab641 MR |
132 | if (GTK_IS_MISC(BUTTON_CHILD(m_widget))) |
133 | gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)), | |
77f70672 | 134 | x_alignment, y_alignment); |
a696db45 | 135 | |
5f7bcb48 | 136 | SetLabel(label); |
354aa1e3 | 137 | |
de1c750f RR |
138 | if (style & wxNO_BORDER) |
139 | gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE ); | |
de1c750f | 140 | |
58b907f6 | 141 | gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked", |
b292e2f5 | 142 | GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this ); |
c801d85f | 143 | |
a90c0600 RR |
144 | gtk_signal_connect_after( GTK_OBJECT(m_widget), "style_set", |
145 | GTK_SIGNAL_FUNC(gtk_button_style_set_callback), (gpointer*) this ); | |
b2ff89d6 | 146 | |
f03fc89f | 147 | m_parent->DoAddChild( this ); |
9e691f46 | 148 | |
abdeb9e7 | 149 | PostCreation(size); |
db434467 | 150 | |
4fa87bd9 VS |
151 | return true; |
152 | } | |
b2ff89d6 | 153 | |
32c77a71 | 154 | |
da048e3d | 155 | void wxButton::SetDefault() |
c801d85f | 156 | { |
6c20e8f8 VZ |
157 | wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); |
158 | wxCHECK_RET( tlw, _T("button without top level window?") ); | |
97149f18 | 159 | |
6c20e8f8 | 160 | tlw->SetDefaultItem(this); |
b2ff89d6 | 161 | |
3502e687 RR |
162 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); |
163 | gtk_widget_grab_default( m_widget ); | |
b2ff89d6 | 164 | |
f893066b RR |
165 | // resize for default border |
166 | gtk_button_style_set_callback( m_widget, NULL, this ); | |
6de97a3b | 167 | } |
c801d85f | 168 | |
ebea0891 | 169 | /* static */ |
4fa87bd9 | 170 | wxSize wxButtonBase::GetDefaultSize() |
8dbf4589 RR |
171 | { |
172 | return wxSize(80,26); | |
173 | } | |
174 | ||
5f7bcb48 | 175 | void wxButton::SetLabel( const wxString &lbl ) |
c801d85f | 176 | { |
223d09f6 | 177 | wxCHECK_RET( m_widget != NULL, wxT("invalid button") ); |
9e691f46 | 178 | |
5f7bcb48 VS |
179 | wxString label(lbl); |
180 | ||
5f7bcb48 VS |
181 | if (label.empty() && wxIsStockID(m_windowId)) |
182 | label = wxGetStockLabel(m_windowId); | |
5f7bcb48 VS |
183 | |
184 | wxControl::SetLabel(label); | |
9e691f46 | 185 | |
ee6dd41a | 186 | const wxString labelGTK = GTKRemoveMnemonics(label); |
b2ff89d6 | 187 | |
b2ff89d6 | 188 | gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(labelGTK)); |
6de97a3b | 189 | } |
c801d85f | 190 | |
f03fc89f | 191 | bool wxButton::Enable( bool enable ) |
a9c96bcc | 192 | { |
f03fc89f | 193 | if ( !wxControl::Enable( enable ) ) |
93763ad5 | 194 | return false; |
9e691f46 VZ |
195 | |
196 | gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable ); | |
f03fc89f | 197 | |
93763ad5 | 198 | return true; |
a9c96bcc RR |
199 | } |
200 | ||
2b5f62a0 VZ |
201 | bool wxButton::IsOwnGtkWindow( GdkWindow *window ) |
202 | { | |
2b5f62a0 | 203 | return (window == m_widget->window); |
2b5f62a0 VZ |
204 | } |
205 | ||
f40fdaa3 | 206 | void wxButton::DoApplyWidgetStyle(GtkRcStyle *style) |
868a2826 | 207 | { |
f40fdaa3 VS |
208 | gtk_widget_modify_style(m_widget, style); |
209 | gtk_widget_modify_style(BUTTON_CHILD(m_widget), style); | |
a81258be | 210 | } |
db434467 RR |
211 | |
212 | wxSize wxButton::DoGetBestSize() const | |
213 | { | |
4f819fe4 VZ |
214 | // the default button in wxGTK is bigger than the other ones because of an |
215 | // extra border around it, but we don't want to take it into account in | |
216 | // our size calculations (otherwsie the result is visually ugly), so | |
217 | // always return the size of non default button from here | |
218 | const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget); | |
219 | if ( isDefault ) | |
220 | { | |
221 | // temporarily unset default flag | |
222 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
223 | } | |
224 | ||
db434467 | 225 | wxSize ret( wxControl::DoGetBestSize() ); |
9e691f46 | 226 | |
4f819fe4 VZ |
227 | if ( isDefault ) |
228 | { | |
229 | // set it back again | |
230 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
231 | } | |
232 | ||
391ddf40 | 233 | ret.x += 10; // add a few pixels for sloppy (but common) themes |
b2ff89d6 | 234 | |
8ab696e0 RR |
235 | if (!HasFlag(wxBU_EXACTFIT)) |
236 | { | |
4fa87bd9 VS |
237 | wxSize defaultSize = GetDefaultSize(); |
238 | if (ret.x < defaultSize.x) ret.x = defaultSize.x; | |
239 | if (ret.y < defaultSize.y) ret.y = defaultSize.y; | |
8ab696e0 | 240 | } |
9e691f46 | 241 | |
9f884528 | 242 | CacheBestSize(ret); |
db434467 RR |
243 | return ret; |
244 | } | |
245 | ||
9d522606 RD |
246 | // static |
247 | wxVisualAttributes | |
248 | wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
249 | { | |
250 | return GetDefaultAttributesFromGTKWidget(gtk_button_new); | |
251 | } | |
252 | ||
1e6feb95 | 253 | #endif // wxUSE_BUTTON |