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