]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
0b83552a | 2 | // Name: src/gtk/toolbar.cpp |
c801d85f KB |
3 | // Purpose: GTK toolbar |
4 | // Author: Robert Roebling | |
8a0681f9 | 5 | // Modified: 13.12.99 by VZ to derive from wxToolBarBase |
c801d85f | 6 | // Copyright: (c) 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 | ||
8a0681f9 | 13 | #if wxUSE_TOOLBAR_NATIVE |
dcf924a3 | 14 | |
4e3e485b WS |
15 | #include "wx/toolbar.h" |
16 | ||
9dc44eff | 17 | #include <gtk/gtk.h> |
9e691f46 | 18 | #include "wx/gtk/private.h" |
9dc44eff | 19 | #include "wx/gtk/private/gtk2-compat.h" |
fc6557a6 | 20 | |
8a0681f9 VZ |
21 | // ---------------------------------------------------------------------------- |
22 | // globals | |
23 | // ---------------------------------------------------------------------------- | |
acfd422a | 24 | |
314055fa | 25 | // data |
9b7e522a RR |
26 | extern bool g_blockEventsOnDrag; |
27 | extern wxCursor g_globalCursor; | |
314055fa | 28 | |
8a0681f9 VZ |
29 | // ---------------------------------------------------------------------------- |
30 | // wxToolBarTool | |
31 | // ---------------------------------------------------------------------------- | |
32 | ||
33 | class wxToolBarTool : public wxToolBarToolBase | |
34 | { | |
35 | public: | |
36 | wxToolBarTool(wxToolBar *tbar, | |
37 | int id, | |
e76c0b5f | 38 | const wxString& label, |
8a0681f9 VZ |
39 | const wxBitmap& bitmap1, |
40 | const wxBitmap& bitmap2, | |
e76c0b5f | 41 | wxItemKind kind, |
8a0681f9 VZ |
42 | wxObject *clientData, |
43 | const wxString& shortHelpString, | |
44 | const wxString& longHelpString) | |
e76c0b5f | 45 | : wxToolBarToolBase(tbar, id, label, bitmap1, bitmap2, kind, |
8a0681f9 VZ |
46 | clientData, shortHelpString, longHelpString) |
47 | { | |
a1cb0b11 | 48 | m_item = NULL; |
8a0681f9 VZ |
49 | } |
50 | ||
07d02e9e VZ |
51 | wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label) |
52 | : wxToolBarToolBase(tbar, control, label) | |
8a0681f9 | 53 | { |
a1cb0b11 | 54 | m_item = NULL; |
1be45608 VZ |
55 | } |
56 | ||
a1cb0b11 PC |
57 | void SetImage(); |
58 | void CreateDropDown(); | |
59 | void ShowDropdown(GtkToggleButton* button); | |
8a0681f9 | 60 | |
a1cb0b11 | 61 | GtkToolItem* m_item; |
8a0681f9 VZ |
62 | }; |
63 | ||
64 | // ---------------------------------------------------------------------------- | |
65 | // wxWin macros | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
2eb10e2a | 68 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) |
8a0681f9 VZ |
69 | |
70 | // ============================================================================ | |
71 | // implementation | |
72 | // ============================================================================ | |
73 | ||
c801d85f | 74 | //----------------------------------------------------------------------------- |
a1cb0b11 | 75 | // "clicked" from m_item |
c801d85f KB |
76 | //----------------------------------------------------------------------------- |
77 | ||
865bb325 | 78 | extern "C" { |
a1cb0b11 | 79 | static void item_clicked(GtkToolButton*, wxToolBarTool* tool) |
c801d85f | 80 | { |
1144d24d | 81 | if (g_blockEventsOnDrag) return; |
a3622daa | 82 | |
a1cb0b11 PC |
83 | tool->GetToolBar()->OnLeftClick(tool->GetId(), false); |
84 | } | |
85 | } | |
7062497f | 86 | |
a1cb0b11 PC |
87 | //----------------------------------------------------------------------------- |
88 | // "toggled" from m_item | |
89 | //----------------------------------------------------------------------------- | |
8a0681f9 | 90 | |
a1cb0b11 PC |
91 | extern "C" { |
92 | static void item_toggled(GtkToggleToolButton* button, wxToolBarTool* tool) | |
93 | { | |
94 | if (g_blockEventsOnDrag) return; | |
38762f09 | 95 | |
a1cb0b11 PC |
96 | const bool active = gtk_toggle_tool_button_get_active(button) != 0; |
97 | tool->Toggle(active); | |
98 | if (!active && tool->GetKind() == wxITEM_RADIO) | |
99 | return; | |
a3622daa | 100 | |
a1cb0b11 | 101 | if (!tool->GetToolBar()->OnLeftClick(tool->GetId(), active)) |
6bb7cee4 VZ |
102 | { |
103 | // revert back | |
104 | tool->Toggle(); | |
6bb7cee4 | 105 | } |
fc008f25 | 106 | } |
865bb325 | 107 | } |
c801d85f | 108 | |
729b4756 | 109 | //----------------------------------------------------------------------------- |
a1cb0b11 | 110 | // "button_press_event" from m_item child |
729b4756 | 111 | //----------------------------------------------------------------------------- |
a1cb0b11 | 112 | |
729b4756 | 113 | extern "C" { |
a1cb0b11 PC |
114 | static gboolean |
115 | button_press_event(GtkWidget*, GdkEventButton* event, wxToolBarTool* tool) | |
729b4756 RR |
116 | { |
117 | if (event->button != 3) | |
118 | return FALSE; | |
119 | ||
729b4756 | 120 | if (g_blockEventsOnDrag) return TRUE; |
729b4756 | 121 | |
a1cb0b11 PC |
122 | tool->GetToolBar()->OnRightClick( |
123 | tool->GetId(), int(event->x), int(event->y)); | |
729b4756 RR |
124 | |
125 | return TRUE; | |
126 | } | |
127 | } | |
128 | ||
fc6557a6 | 129 | //----------------------------------------------------------------------------- |
a1cb0b11 | 130 | // "child_detached" from m_widget |
fc6557a6 RR |
131 | //----------------------------------------------------------------------------- |
132 | ||
133 | extern "C" { | |
a1cb0b11 | 134 | static void child_detached(GtkWidget*, GtkToolbar* toolbar, void*) |
fc6557a6 | 135 | { |
a1cb0b11 PC |
136 | // disable showing overflow arrow when toolbar is detached, |
137 | // otherwise toolbar collapses to just an arrow | |
138 | gtk_toolbar_set_show_arrow(toolbar, false); | |
fc6557a6 RR |
139 | } |
140 | } | |
141 | ||
142 | //----------------------------------------------------------------------------- | |
a1cb0b11 | 143 | // "child_attached" from m_widget |
fc6557a6 RR |
144 | //----------------------------------------------------------------------------- |
145 | ||
a1cb0b11 PC |
146 | extern "C" { |
147 | static void child_attached(GtkWidget*, GtkToolbar* toolbar, void*) | |
fc6557a6 | 148 | { |
a1cb0b11 | 149 | gtk_toolbar_set_show_arrow(toolbar, true); |
fc6557a6 RR |
150 | } |
151 | } | |
152 | ||
2f2aa628 | 153 | //----------------------------------------------------------------------------- |
a1cb0b11 | 154 | // "enter_notify_event" / "leave_notify_event" from m_item |
2f2aa628 RR |
155 | //----------------------------------------------------------------------------- |
156 | ||
865bb325 | 157 | extern "C" { |
a1cb0b11 PC |
158 | static gboolean |
159 | enter_notify_event(GtkWidget*, GdkEventCrossing* event, wxToolBarTool* tool) | |
314055fa | 160 | { |
1144d24d | 161 | if (g_blockEventsOnDrag) return TRUE; |
248bcf0a | 162 | |
a1cb0b11 PC |
163 | int id = -1; |
164 | if (event->type == GDK_ENTER_NOTIFY) | |
165 | id = tool->GetId(); | |
166 | tool->GetToolBar()->OnMouseEnter(id); | |
248bcf0a | 167 | |
1144d24d | 168 | return FALSE; |
314055fa | 169 | } |
865bb325 | 170 | } |
314055fa | 171 | |
a1cb0b11 PC |
172 | //----------------------------------------------------------------------------- |
173 | // "expose_event" from GtkImage inside m_item | |
174 | //----------------------------------------------------------------------------- | |
175 | ||
176 | extern "C" { | |
177 | static gboolean | |
9dc44eff PC |
178 | #ifdef __WXGTK3__ |
179 | image_draw(GtkWidget* widget, cairo_t* cr, wxToolBarTool* tool) | |
180 | #else | |
a1cb0b11 | 181 | image_expose_event(GtkWidget* widget, GdkEventExpose*, wxToolBarTool* tool) |
9dc44eff | 182 | #endif |
a1cb0b11 PC |
183 | { |
184 | const wxBitmap& bitmap = tool->GetDisabledBitmap(); | |
185 | if (tool->IsEnabled() || !bitmap.IsOk()) | |
186 | return false; | |
187 | ||
188 | // draw disabled bitmap ourselves, GtkImage has no way to specify it | |
385e8575 PC |
189 | GtkAllocation alloc; |
190 | gtk_widget_get_allocation(widget, &alloc); | |
191 | GtkRequisition req; | |
192 | gtk_widget_get_requisition(widget, &req); | |
9dc44eff PC |
193 | const int x = alloc.x + (alloc.width - req.width) / 2; |
194 | const int y = alloc.y + (alloc.height - req.height) / 2; | |
195 | #ifdef __WXGTK3__ | |
196 | bitmap.Draw(cr, x, y); | |
197 | #else | |
a1cb0b11 | 198 | gdk_draw_pixbuf( |
385e8575 | 199 | gtk_widget_get_window(widget), gtk_widget_get_style(widget)->black_gc, bitmap.GetPixbuf(), |
9dc44eff | 200 | 0, 0, x, y, |
a1cb0b11 | 201 | -1, -1, GDK_RGB_DITHER_NORMAL, 0, 0); |
9dc44eff | 202 | #endif |
a1cb0b11 PC |
203 | return true; |
204 | } | |
205 | } | |
206 | ||
207 | //----------------------------------------------------------------------------- | |
208 | // "toggled" from dropdown menu button | |
209 | //----------------------------------------------------------------------------- | |
210 | ||
211 | extern "C" { | |
212 | static void arrow_toggled(GtkToggleButton* button, wxToolBarTool* tool) | |
213 | { | |
214 | if (gtk_toggle_button_get_active(button)) | |
215 | { | |
216 | tool->ShowDropdown(button); | |
217 | gtk_toggle_button_set_active(button, false); | |
218 | } | |
219 | } | |
220 | } | |
221 | ||
222 | //----------------------------------------------------------------------------- | |
223 | // "button_press_event" from dropdown menu button | |
224 | //----------------------------------------------------------------------------- | |
225 | ||
226 | extern "C" { | |
227 | static gboolean | |
228 | arrow_button_press_event(GtkToggleButton* button, GdkEventButton* event, wxToolBarTool* tool) | |
229 | { | |
230 | if (event->button == 1) | |
231 | { | |
232 | g_signal_handlers_block_by_func(button, (void*)arrow_toggled, tool); | |
233 | gtk_toggle_button_set_active(button, true); | |
234 | tool->ShowDropdown(button); | |
235 | gtk_toggle_button_set_active(button, false); | |
236 | g_signal_handlers_unblock_by_func(button, (void*)arrow_toggled, tool); | |
237 | return true; | |
238 | } | |
239 | return false; | |
240 | } | |
241 | } | |
242 | ||
48200154 | 243 | void wxToolBar::AddChildGTK(wxWindowGTK* child) |
bf9e3e73 | 244 | { |
205177b0 PC |
245 | GtkWidget* align = gtk_alignment_new(0.5, 0.5, 0, 0); |
246 | gtk_widget_show(align); | |
247 | gtk_container_add(GTK_CONTAINER(align), child->m_widget); | |
248 | GtkToolItem* item = gtk_tool_item_new(); | |
249 | gtk_container_add(GTK_CONTAINER(item), align); | |
205177b0 | 250 | // position will be corrected in DoInsertTool if necessary |
385e8575 | 251 | gtk_toolbar_insert(GTK_TOOLBAR(gtk_bin_get_child(GTK_BIN(m_widget))), item, -1); |
bf9e3e73 RR |
252 | } |
253 | ||
8a0681f9 VZ |
254 | // ---------------------------------------------------------------------------- |
255 | // wxToolBarTool | |
256 | // ---------------------------------------------------------------------------- | |
c801d85f | 257 | |
a1cb0b11 | 258 | void wxToolBarTool::SetImage() |
8a0681f9 | 259 | { |
a1cb0b11 PC |
260 | const wxBitmap& bitmap = GetNormalBitmap(); |
261 | wxCHECK_RET(bitmap.IsOk(), "invalid bitmap for wxToolBar icon"); | |
262 | ||
263 | GtkWidget* image = gtk_tool_button_get_icon_widget(GTK_TOOL_BUTTON(m_item)); | |
264 | // always use pixbuf, because pixmap mask does not | |
265 | // work with disabled images in some themes | |
266 | gtk_image_set_from_pixbuf(GTK_IMAGE(image), bitmap.GetPixbuf()); | |
267 | } | |
268 | ||
269 | // helper to create a dropdown menu item | |
270 | void wxToolBarTool::CreateDropDown() | |
271 | { | |
272 | gtk_tool_item_set_homogeneous(m_item, false); | |
1897abe1 PC |
273 | GtkOrientation orient = GTK_ORIENTATION_HORIZONTAL; |
274 | GtkArrowType arrowType = GTK_ARROW_DOWN; | |
a1cb0b11 PC |
275 | if (GetToolBar()->HasFlag(wxTB_LEFT | wxTB_RIGHT)) |
276 | { | |
1897abe1 PC |
277 | orient = GTK_ORIENTATION_VERTICAL; |
278 | arrowType = GTK_ARROW_RIGHT; | |
a1cb0b11 | 279 | } |
1897abe1 PC |
280 | GtkWidget* box = gtk_box_new(orient, 0); |
281 | GtkWidget* arrow = gtk_arrow_new(arrowType, GTK_SHADOW_NONE); | |
385e8575 | 282 | GtkWidget* tool_button = gtk_bin_get_child(GTK_BIN(m_item)); |
a1cb0b11 PC |
283 | gtk_widget_reparent(tool_button, box); |
284 | GtkWidget* arrow_button = gtk_toggle_button_new(); | |
285 | gtk_button_set_relief(GTK_BUTTON(arrow_button), | |
286 | gtk_tool_item_get_relief_style(GTK_TOOL_ITEM(m_item))); | |
287 | gtk_container_add(GTK_CONTAINER(arrow_button), arrow); | |
288 | gtk_container_add(GTK_CONTAINER(box), arrow_button); | |
289 | gtk_widget_show_all(box); | |
290 | gtk_container_add(GTK_CONTAINER(m_item), box); | |
291 | ||
292 | g_signal_connect(arrow_button, "toggled", G_CALLBACK(arrow_toggled), this); | |
293 | g_signal_connect(arrow_button, "button_press_event", | |
294 | G_CALLBACK(arrow_button_press_event), this); | |
295 | } | |
296 | ||
297 | void wxToolBarTool::ShowDropdown(GtkToggleButton* button) | |
298 | { | |
299 | wxToolBarBase* toolbar = GetToolBar(); | |
ce7fe42e | 300 | wxCommandEvent event(wxEVT_TOOL_DROPDOWN, GetId()); |
a1cb0b11 PC |
301 | if (!toolbar->HandleWindowEvent(event)) |
302 | { | |
303 | wxMenu* menu = GetDropdownMenu(); | |
304 | if (menu) | |
305 | { | |
385e8575 | 306 | GtkAllocation alloc; |
628e8d44 | 307 | gtk_widget_get_allocation(GTK_WIDGET(button), &alloc); |
a1cb0b11 PC |
308 | int x = alloc.x; |
309 | int y = alloc.y; | |
310 | if (toolbar->HasFlag(wxTB_LEFT | wxTB_RIGHT)) | |
311 | x += alloc.width; | |
312 | else | |
313 | y += alloc.height; | |
314 | toolbar->PopupMenu(menu, x, y); | |
315 | } | |
316 | } | |
8a0681f9 | 317 | } |
c801d85f | 318 | |
8a0681f9 | 319 | wxToolBarToolBase *wxToolBar::CreateTool(int id, |
e76c0b5f | 320 | const wxString& text, |
8a0681f9 VZ |
321 | const wxBitmap& bitmap1, |
322 | const wxBitmap& bitmap2, | |
e76c0b5f | 323 | wxItemKind kind, |
8a0681f9 VZ |
324 | wxObject *clientData, |
325 | const wxString& shortHelpString, | |
326 | const wxString& longHelpString) | |
327 | { | |
e76c0b5f | 328 | return new wxToolBarTool(this, id, text, bitmap1, bitmap2, kind, |
8a0681f9 VZ |
329 | clientData, shortHelpString, longHelpString); |
330 | } | |
b1da76e1 | 331 | |
07d02e9e VZ |
332 | wxToolBarToolBase * |
333 | wxToolBar::CreateTool(wxControl *control, const wxString& label) | |
c801d85f | 334 | { |
07d02e9e | 335 | return new wxToolBarTool(this, control, label); |
fc008f25 | 336 | } |
c801d85f | 337 | |
8a0681f9 VZ |
338 | //----------------------------------------------------------------------------- |
339 | // wxToolBar construction | |
340 | //----------------------------------------------------------------------------- | |
341 | ||
342 | void wxToolBar::Init() | |
c801d85f | 343 | { |
d3b9f782 | 344 | m_toolbar = NULL; |
a1cb0b11 | 345 | m_tooltips = NULL; |
fc008f25 | 346 | } |
c801d85f | 347 | |
a3622daa | 348 | wxToolBar::~wxToolBar() |
c801d85f | 349 | { |
9dc44eff | 350 | #ifndef __WXGTK3__ |
85314957 | 351 | if (m_tooltips) // always NULL if GTK >= 2.12 |
a1cb0b11 PC |
352 | { |
353 | gtk_object_destroy(GTK_OBJECT(m_tooltips)); | |
354 | g_object_unref(m_tooltips); | |
355 | } | |
9dc44eff | 356 | #endif |
fc008f25 | 357 | } |
c801d85f | 358 | |
8a0681f9 VZ |
359 | bool wxToolBar::Create( wxWindow *parent, |
360 | wxWindowID id, | |
361 | const wxPoint& pos, | |
362 | const wxSize& size, | |
363 | long style, | |
364 | const wxString& name ) | |
c801d85f | 365 | { |
8a0681f9 VZ |
366 | if ( !PreCreation( parent, pos, size ) || |
367 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
4dcaf11a | 368 | { |
223d09f6 | 369 | wxFAIL_MSG( wxT("wxToolBar creation failed") ); |
c801d85f | 370 | |
91af0895 | 371 | return false; |
8a0681f9 | 372 | } |
a3622daa | 373 | |
d408730c VZ |
374 | FixupStyle(); |
375 | ||
9e691f46 | 376 | m_toolbar = GTK_TOOLBAR( gtk_toolbar_new() ); |
9dc44eff | 377 | #ifndef __WXGTK3__ |
85314957 VZ |
378 | if (gtk_check_version(2, 12, 0)) |
379 | { | |
380 | m_tooltips = gtk_tooltips_new(); | |
381 | g_object_ref(m_tooltips); | |
382 | gtk_object_sink(GTK_OBJECT(m_tooltips)); | |
383 | } | |
385e8575 | 384 | #endif |
e76c0b5f | 385 | GtkSetStyle(); |
99e8cb50 | 386 | |
3502e687 RR |
387 | if (style & wxTB_DOCKABLE) |
388 | { | |
389 | m_widget = gtk_handle_box_new(); | |
a1cb0b11 PC |
390 | |
391 | g_signal_connect(m_widget, "child_detached", | |
392 | G_CALLBACK(child_detached), NULL); | |
393 | g_signal_connect(m_widget, "child_attached", | |
394 | G_CALLBACK(child_attached), NULL); | |
8a0681f9 | 395 | |
f03fc89f | 396 | if (style & wxTB_FLAT) |
858b5bdd | 397 | gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget), GTK_SHADOW_NONE ); |
3502e687 RR |
398 | } |
399 | else | |
248bcf0a RD |
400 | { |
401 | m_widget = gtk_event_box_new(); | |
248bcf0a | 402 | ConnectWidget( m_widget ); |
3502e687 | 403 | } |
9ff9d30c | 404 | g_object_ref(m_widget); |
a1cb0b11 PC |
405 | gtk_container_add(GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar)); |
406 | gtk_widget_show(GTK_WIDGET(m_toolbar)); | |
be25e480 | 407 | |
f03fc89f | 408 | m_parent->DoAddChild( this ); |
8a0681f9 | 409 | |
abdeb9e7 | 410 | PostCreation(size); |
a3622daa | 411 | |
91af0895 | 412 | return true; |
fc008f25 | 413 | } |
c801d85f | 414 | |
e4161a2a | 415 | GdkWindow *wxToolBar::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const |
48468900 | 416 | { |
385e8575 | 417 | return gtk_widget_get_window(GTK_WIDGET(m_toolbar)); |
48468900 RR |
418 | } |
419 | ||
e76c0b5f VZ |
420 | void wxToolBar::GtkSetStyle() |
421 | { | |
a1cb0b11 PC |
422 | GtkOrientation orient = GTK_ORIENTATION_HORIZONTAL; |
423 | if (HasFlag(wxTB_LEFT | wxTB_RIGHT)) | |
424 | orient = GTK_ORIENTATION_VERTICAL; | |
425 | ||
426 | GtkToolbarStyle style = GTK_TOOLBAR_ICONS; | |
427 | if (HasFlag(wxTB_NOICONS)) | |
428 | style = GTK_TOOLBAR_TEXT; | |
429 | else if (HasFlag(wxTB_TEXT)) | |
430 | { | |
431 | style = GTK_TOOLBAR_BOTH; | |
432 | if (HasFlag(wxTB_HORZ_LAYOUT)) | |
433 | style = GTK_TOOLBAR_BOTH_HORIZ; | |
434 | } | |
e76c0b5f | 435 | |
9dc44eff | 436 | #ifdef __WXGTK3__ |
385e8575 PC |
437 | gtk_orientable_set_orientation(GTK_ORIENTABLE(m_toolbar), orient); |
438 | #else | |
e76c0b5f | 439 | gtk_toolbar_set_orientation(m_toolbar, orient); |
385e8575 | 440 | #endif |
e76c0b5f VZ |
441 | gtk_toolbar_set_style(m_toolbar, style); |
442 | } | |
443 | ||
444 | void wxToolBar::SetWindowStyleFlag( long style ) | |
445 | { | |
446 | wxToolBarBase::SetWindowStyleFlag(style); | |
8ad31f9d | 447 | |
e76c0b5f VZ |
448 | if ( m_toolbar ) |
449 | GtkSetStyle(); | |
450 | } | |
451 | ||
9067c6c5 VZ |
452 | bool wxToolBar::Realize() |
453 | { | |
454 | if ( !wxToolBarBase::Realize() ) | |
455 | return false; | |
456 | ||
457 | // bring the initial state of all the toolbar items in line with the | |
458 | // internal state if the latter was changed by calling wxToolBarTool:: | |
459 | // Enable(): this works under MSW, where the toolbar items are only created | |
460 | // in Realize() which uses the internal state to determine the initial | |
461 | // button state, so make it work under GTK too | |
462 | for ( wxToolBarToolsList::const_iterator i = m_tools.begin(); | |
463 | i != m_tools.end(); | |
464 | ++i ) | |
465 | { | |
466 | // by default the toolbar items are enabled and not toggled, so we only | |
467 | // have to do something if their internal state doesn't correspond to | |
468 | // this | |
469 | if ( !(*i)->IsEnabled() ) | |
470 | DoEnableTool(*i, false); | |
471 | if ( (*i)->IsToggled() ) | |
472 | DoToggleTool(*i, true); | |
473 | } | |
474 | ||
475 | return true; | |
476 | } | |
477 | ||
8a0681f9 | 478 | bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase) |
c801d85f | 479 | { |
5c33522f | 480 | wxToolBarTool* tool = static_cast<wxToolBarTool*>(toolBase); |
248bcf0a | 481 | |
a1cb0b11 | 482 | GSList* radioGroup; |
fbf49ef2 | 483 | GtkWidget* bin_child; |
8a0681f9 VZ |
484 | switch ( tool->GetStyle() ) |
485 | { | |
486 | case wxTOOL_STYLE_BUTTON: | |
a1cb0b11 | 487 | switch (tool->GetKind()) |
8a0681f9 | 488 | { |
a1cb0b11 PC |
489 | case wxITEM_CHECK: |
490 | tool->m_item = gtk_toggle_tool_button_new(); | |
491 | g_signal_connect(tool->m_item, "toggled", | |
492 | G_CALLBACK(item_toggled), tool); | |
493 | break; | |
494 | case wxITEM_RADIO: | |
495 | radioGroup = GetRadioGroup(pos); | |
d4a762e3 | 496 | if (!radioGroup) |
38762f09 VZ |
497 | { |
498 | // this is the first button in the radio button group, | |
499 | // it will be toggled automatically by GTK so bring the | |
500 | // internal flag in sync | |
91af0895 | 501 | tool->Toggle(true); |
38762f09 | 502 | } |
a1cb0b11 PC |
503 | tool->m_item = gtk_radio_tool_button_new(radioGroup); |
504 | g_signal_connect(tool->m_item, "toggled", | |
505 | G_CALLBACK(item_toggled), tool); | |
506 | break; | |
507 | default: | |
508 | wxFAIL_MSG("unknown toolbar child type"); | |
509 | // fall through | |
510 | case wxITEM_DROPDOWN: | |
511 | case wxITEM_NORMAL: | |
512 | tool->m_item = gtk_tool_button_new(NULL, ""); | |
513 | g_signal_connect(tool->m_item, "clicked", | |
514 | G_CALLBACK(item_clicked), tool); | |
515 | break; | |
516 | } | |
517 | if (!HasFlag(wxTB_NOICONS)) | |
518 | { | |
519 | GtkWidget* image = gtk_image_new(); | |
520 | gtk_tool_button_set_icon_widget( | |
521 | GTK_TOOL_BUTTON(tool->m_item), image); | |
522 | tool->SetImage(); | |
523 | gtk_widget_show(image); | |
9dc44eff PC |
524 | #ifdef __WXGTK3__ |
525 | g_signal_connect(image, "draw", | |
526 | G_CALLBACK(image_draw), tool); | |
527 | #else | |
a1cb0b11 PC |
528 | g_signal_connect(image, "expose_event", |
529 | G_CALLBACK(image_expose_event), tool); | |
9dc44eff | 530 | #endif |
8a0681f9 | 531 | } |
a1cb0b11 PC |
532 | if (!tool->GetLabel().empty()) |
533 | { | |
534 | gtk_tool_button_set_label( | |
535 | GTK_TOOL_BUTTON(tool->m_item), wxGTK_CONV(tool->GetLabel())); | |
536 | // needed for labels in horizontal toolbar with wxTB_HORZ_LAYOUT | |
537 | gtk_tool_item_set_is_important(tool->m_item, true); | |
538 | } | |
539 | if (!HasFlag(wxTB_NO_TOOLTIPS) && !tool->GetShortHelp().empty()) | |
540 | { | |
85314957 | 541 | #if GTK_CHECK_VERSION(2, 12, 0) |
385e8575 | 542 | if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,12,0) == NULL) |
85314957 VZ |
543 | { |
544 | gtk_tool_item_set_tooltip_text(tool->m_item, | |
545 | wxGTK_CONV(tool->GetShortHelp())); | |
546 | } | |
547 | else | |
548 | #endif | |
549 | { | |
9dc44eff | 550 | #ifndef __WXGTK3__ |
85314957 VZ |
551 | gtk_tool_item_set_tooltip(tool->m_item, |
552 | m_tooltips, wxGTK_CONV(tool->GetShortHelp()), ""); | |
385e8575 | 553 | #endif |
85314957 | 554 | } |
a1cb0b11 | 555 | } |
fbf49ef2 PC |
556 | bin_child = gtk_bin_get_child(GTK_BIN(tool->m_item)); |
557 | g_signal_connect(bin_child, "button_press_event", | |
a1cb0b11 | 558 | G_CALLBACK(button_press_event), tool); |
fbf49ef2 | 559 | g_signal_connect(bin_child, "enter_notify_event", |
a1cb0b11 | 560 | G_CALLBACK(enter_notify_event), tool); |
fbf49ef2 | 561 | g_signal_connect(bin_child, "leave_notify_event", |
a1cb0b11 PC |
562 | G_CALLBACK(enter_notify_event), tool); |
563 | ||
564 | if (tool->GetKind() == wxITEM_DROPDOWN) | |
565 | tool->CreateDropDown(); | |
205177b0 | 566 | gtk_toolbar_insert(m_toolbar, tool->m_item, int(pos)); |
8a0681f9 VZ |
567 | break; |
568 | ||
569 | case wxTOOL_STYLE_SEPARATOR: | |
a1cb0b11 | 570 | tool->m_item = gtk_separator_tool_item_new(); |
cc260109 VZ |
571 | if ( tool->IsStretchable() ) |
572 | { | |
573 | gtk_separator_tool_item_set_draw | |
574 | ( | |
575 | GTK_SEPARATOR_TOOL_ITEM(tool->m_item), | |
576 | FALSE | |
577 | ); | |
578 | gtk_tool_item_set_expand(tool->m_item, TRUE); | |
579 | } | |
205177b0 | 580 | gtk_toolbar_insert(m_toolbar, tool->m_item, int(pos)); |
1be45608 | 581 | break; |
bf9e3e73 | 582 | |
8a0681f9 | 583 | case wxTOOL_STYLE_CONTROL: |
a1cb0b11 | 584 | wxWindow* control = tool->GetControl(); |
385e8575 | 585 | if (gtk_widget_get_parent(control->m_widget) == NULL) |
48200154 | 586 | AddChildGTK(control); |
385e8575 | 587 | tool->m_item = GTK_TOOL_ITEM(gtk_widget_get_parent(gtk_widget_get_parent(control->m_widget))); |
205177b0 PC |
588 | if (gtk_toolbar_get_item_index(m_toolbar, tool->m_item) != int(pos)) |
589 | { | |
590 | g_object_ref(tool->m_item); | |
591 | gtk_container_remove( | |
592 | GTK_CONTAINER(m_toolbar), GTK_WIDGET(tool->m_item)); | |
593 | gtk_toolbar_insert(m_toolbar, tool->m_item, int(pos)); | |
594 | g_object_unref(tool->m_item); | |
595 | } | |
8a0681f9 VZ |
596 | break; |
597 | } | |
a1cb0b11 | 598 | gtk_widget_show(GTK_WIDGET(tool->m_item)); |
bf9e3e73 | 599 | |
9f884528 | 600 | InvalidateBestSize(); |
bf9e3e73 | 601 | |
91af0895 | 602 | return true; |
bf9e3e73 RR |
603 | } |
604 | ||
a1cb0b11 | 605 | bool wxToolBar::DoDeleteTool(size_t /* pos */, wxToolBarToolBase* toolBase) |
c801d85f | 606 | { |
5c33522f | 607 | wxToolBarTool* tool = static_cast<wxToolBarTool*>(toolBase); |
c801d85f | 608 | |
a1cb0b11 | 609 | if (tool->GetStyle() == wxTOOL_STYLE_CONTROL) |
97d7bfb8 | 610 | { |
a1cb0b11 PC |
611 | // don't destroy the control here as we can be called from |
612 | // RemoveTool() and then we need to keep the control alive; | |
613 | // while if we're called from DeleteTool() the control will | |
614 | // be destroyed when wxToolBarToolBase itself is deleted | |
615 | GtkWidget* widget = tool->GetControl()->m_widget; | |
385e8575 | 616 | gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(widget)), widget); |
8a0681f9 | 617 | } |
9dc44eff | 618 | gtk_widget_destroy(GTK_WIDGET(tool->m_item)); |
a1cb0b11 | 619 | tool->m_item = NULL; |
c801d85f | 620 | |
9f884528 | 621 | InvalidateBestSize(); |
91af0895 | 622 | return true; |
fc008f25 | 623 | } |
46dc76ba | 624 | |
a1cb0b11 PC |
625 | GSList* wxToolBar::GetRadioGroup(size_t pos) |
626 | { | |
627 | GSList* radioGroup = NULL; | |
628 | GtkToolItem* item = NULL; | |
629 | if (pos > 0) | |
630 | { | |
631 | item = gtk_toolbar_get_nth_item(m_toolbar, int(pos) - 1); | |
632 | if (!GTK_IS_RADIO_TOOL_BUTTON(item)) | |
633 | item = NULL; | |
634 | } | |
635 | if (item == NULL && pos < m_tools.size()) | |
636 | { | |
637 | item = gtk_toolbar_get_nth_item(m_toolbar, int(pos)); | |
638 | if (!GTK_IS_RADIO_TOOL_BUTTON(item)) | |
639 | item = NULL; | |
640 | } | |
641 | if (item) | |
642 | radioGroup = gtk_radio_tool_button_get_group((GtkRadioToolButton*)item); | |
643 | return radioGroup; | |
644 | } | |
645 | ||
8a0681f9 VZ |
646 | // ---------------------------------------------------------------------------- |
647 | // wxToolBar tools state | |
648 | // ---------------------------------------------------------------------------- | |
649 | ||
650 | void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable) | |
c801d85f | 651 | { |
5c33522f | 652 | wxToolBarTool* tool = static_cast<wxToolBarTool*>(toolBase); |
8a0681f9 | 653 | |
8a0681f9 | 654 | if (tool->m_item) |
a1cb0b11 | 655 | gtk_widget_set_sensitive(GTK_WIDGET(tool->m_item), enable); |
fc008f25 | 656 | } |
c801d85f | 657 | |
248bcf0a | 658 | void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle ) |
c801d85f | 659 | { |
5c33522f | 660 | wxToolBarTool* tool = static_cast<wxToolBarTool*>(toolBase); |
8a0681f9 | 661 | |
a1cb0b11 | 662 | if (tool->m_item) |
1144d24d | 663 | { |
a1cb0b11 | 664 | g_signal_handlers_block_by_func(tool->m_item, (void*)item_toggled, tool); |
8a0681f9 | 665 | |
a1cb0b11 PC |
666 | gtk_toggle_tool_button_set_active( |
667 | GTK_TOGGLE_TOOL_BUTTON(tool->m_item), toggle); | |
248bcf0a | 668 | |
a1cb0b11 | 669 | g_signal_handlers_unblock_by_func(tool->m_item, (void*)item_toggled, tool); |
1144d24d | 670 | } |
fc008f25 | 671 | } |
c801d85f | 672 | |
8a0681f9 VZ |
673 | void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool), |
674 | bool WXUNUSED(toggle)) | |
c801d85f | 675 | { |
8a0681f9 | 676 | // VZ: absolutely no idea about how to do it |
9a83f860 | 677 | wxFAIL_MSG( wxT("not implemented") ); |
fc008f25 | 678 | } |
c801d85f | 679 | |
8a0681f9 VZ |
680 | // ---------------------------------------------------------------------------- |
681 | // wxToolBar geometry | |
682 | // ---------------------------------------------------------------------------- | |
683 | ||
a1cb0b11 PC |
684 | wxSize wxToolBar::DoGetBestSize() const |
685 | { | |
686 | // Unfortunately, if overflow arrow is enabled GtkToolbar only reports size | |
687 | // of arrow. To get the real size, the arrow is temporarily disabled here. | |
688 | // This is gross, since it will cause a queue_resize, and could potentially | |
689 | // lead to an infinite loop. But there seems to be no alternative, short of | |
690 | // disabling the arrow entirely. | |
691 | gtk_toolbar_set_show_arrow(m_toolbar, false); | |
692 | const wxSize size = wxToolBarBase::DoGetBestSize(); | |
693 | gtk_toolbar_set_show_arrow(m_toolbar, true); | |
694 | return size; | |
695 | } | |
696 | ||
8a0681f9 VZ |
697 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x), |
698 | wxCoord WXUNUSED(y)) const | |
c801d85f | 699 | { |
8a0681f9 | 700 | // VZ: GTK+ doesn't seem to have such thing |
9a83f860 | 701 | wxFAIL_MSG( wxT("wxToolBar::FindToolForPosition() not implemented") ); |
8a0681f9 | 702 | |
d3b9f782 | 703 | return NULL; |
fc008f25 | 704 | } |
c801d85f | 705 | |
a1f79c1e VZ |
706 | void wxToolBar::SetToolShortHelp( int id, const wxString& helpString ) |
707 | { | |
5c33522f | 708 | wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id)); |
a1f79c1e VZ |
709 | |
710 | if ( tool ) | |
711 | { | |
712 | (void)tool->SetShortHelp(helpString); | |
a1cb0b11 PC |
713 | if (tool->m_item) |
714 | { | |
85314957 | 715 | #if GTK_CHECK_VERSION(2, 12, 0) |
385e8575 | 716 | if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,12,0) == NULL) |
85314957 VZ |
717 | { |
718 | gtk_tool_item_set_tooltip_text(tool->m_item, | |
719 | wxGTK_CONV(helpString)); | |
720 | } | |
721 | else | |
722 | #endif | |
723 | { | |
9dc44eff | 724 | #ifndef __WXGTK3__ |
85314957 VZ |
725 | gtk_tool_item_set_tooltip(tool->m_item, |
726 | m_tooltips, wxGTK_CONV(helpString), ""); | |
385e8575 | 727 | #endif |
85314957 | 728 | } |
a1cb0b11 | 729 | } |
a1f79c1e VZ |
730 | } |
731 | } | |
732 | ||
bbd321ff RD |
733 | void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap ) |
734 | { | |
5c33522f | 735 | wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id)); |
bbd321ff RD |
736 | if ( tool ) |
737 | { | |
738 | wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); | |
739 | ||
740 | tool->SetNormalBitmap(bitmap); | |
a1cb0b11 | 741 | tool->SetImage(); |
f4322df6 | 742 | } |
bbd321ff RD |
743 | } |
744 | ||
745 | void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap ) | |
746 | { | |
5c33522f | 747 | wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id)); |
bbd321ff RD |
748 | if ( tool ) |
749 | { | |
750 | wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); | |
751 | ||
752 | tool->SetDisabledBitmap(bitmap); | |
f4322df6 | 753 | } |
bbd321ff RD |
754 | } |
755 | ||
9d522606 RD |
756 | // ---------------------------------------------------------------------------- |
757 | ||
758 | // static | |
759 | wxVisualAttributes | |
760 | wxToolBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
761 | { | |
7fff16b8 | 762 | return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new()); |
9d522606 RD |
763 | } |
764 | ||
a1f79c1e | 765 | #endif // wxUSE_TOOLBAR_NATIVE |