1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/toolbar.cpp
3 // Purpose: GTK toolbar
4 // Author: Robert Roebling
5 // Modified: 13.12.99 by VZ to derive from wxToolBarBase
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #if wxUSE_TOOLBAR_NATIVE
16 #include "wx/toolbar.h"
18 #include "wx/gtk/private.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
25 extern bool g_blockEventsOnDrag
;
26 extern wxCursor g_globalCursor
;
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 class wxToolBarTool
: public wxToolBarToolBase
35 wxToolBarTool(wxToolBar
*tbar
,
37 const wxString
& label
,
38 const wxBitmap
& bitmap1
,
39 const wxBitmap
& bitmap2
,
42 const wxString
& shortHelpString
,
43 const wxString
& longHelpString
)
44 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
45 clientData
, shortHelpString
, longHelpString
)
50 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
51 : wxToolBarToolBase(tbar
, control
, label
)
57 void CreateDropDown();
58 void ShowDropdown(GtkToggleButton
* button
);
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
69 // ============================================================================
71 // ============================================================================
73 //-----------------------------------------------------------------------------
74 // "clicked" from m_item
75 //-----------------------------------------------------------------------------
78 static void item_clicked(GtkToolButton
*, wxToolBarTool
* tool
)
80 if (g_blockEventsOnDrag
) return;
82 tool
->GetToolBar()->OnLeftClick(tool
->GetId(), false);
86 //-----------------------------------------------------------------------------
87 // "toggled" from m_item
88 //-----------------------------------------------------------------------------
91 static void item_toggled(GtkToggleToolButton
* button
, wxToolBarTool
* tool
)
93 if (g_blockEventsOnDrag
) return;
95 const bool active
= gtk_toggle_tool_button_get_active(button
) != 0;
97 if (!active
&& tool
->GetKind() == wxITEM_RADIO
)
100 if (!tool
->GetToolBar()->OnLeftClick(tool
->GetId(), active
))
108 //-----------------------------------------------------------------------------
109 // "button_press_event" from m_item child
110 //-----------------------------------------------------------------------------
114 button_press_event(GtkWidget
*, GdkEventButton
* event
, wxToolBarTool
* tool
)
116 if (event
->button
!= 3)
119 if (g_blockEventsOnDrag
) return TRUE
;
121 tool
->GetToolBar()->OnRightClick(
122 tool
->GetId(), int(event
->x
), int(event
->y
));
128 //-----------------------------------------------------------------------------
129 // "child_detached" from m_widget
130 //-----------------------------------------------------------------------------
133 static void child_detached(GtkWidget
*, GtkToolbar
* toolbar
, void*)
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);
141 //-----------------------------------------------------------------------------
142 // "child_attached" from m_widget
143 //-----------------------------------------------------------------------------
146 static void child_attached(GtkWidget
*, GtkToolbar
* toolbar
, void*)
148 gtk_toolbar_set_show_arrow(toolbar
, true);
152 //-----------------------------------------------------------------------------
153 // "enter_notify_event" / "leave_notify_event" from m_item
154 //-----------------------------------------------------------------------------
158 enter_notify_event(GtkWidget
*, GdkEventCrossing
* event
, wxToolBarTool
* tool
)
160 if (g_blockEventsOnDrag
) return TRUE
;
163 if (event
->type
== GDK_ENTER_NOTIFY
)
165 tool
->GetToolBar()->OnMouseEnter(id
);
171 //-----------------------------------------------------------------------------
172 // "expose_event" from GtkImage inside m_item
173 //-----------------------------------------------------------------------------
177 image_expose_event(GtkWidget
* widget
, GdkEventExpose
*, wxToolBarTool
* tool
)
179 const wxBitmap
& bitmap
= tool
->GetDisabledBitmap();
180 if (tool
->IsEnabled() || !bitmap
.IsOk())
183 // draw disabled bitmap ourselves, GtkImage has no way to specify it
185 gtk_widget_get_allocation(widget
, &alloc
);
187 gtk_widget_get_requisition(widget
, &req
);
189 gtk_widget_get_window(widget
), gtk_widget_get_style(widget
)->black_gc
, bitmap
.GetPixbuf(),
191 alloc
.x
+ (alloc
.width
- req
.width
) / 2,
192 alloc
.y
+ (alloc
.height
- req
.height
) / 2,
193 -1, -1, GDK_RGB_DITHER_NORMAL
, 0, 0);
198 //-----------------------------------------------------------------------------
199 // "toggled" from dropdown menu button
200 //-----------------------------------------------------------------------------
203 static void arrow_toggled(GtkToggleButton
* button
, wxToolBarTool
* tool
)
205 if (gtk_toggle_button_get_active(button
))
207 tool
->ShowDropdown(button
);
208 gtk_toggle_button_set_active(button
, false);
213 //-----------------------------------------------------------------------------
214 // "button_press_event" from dropdown menu button
215 //-----------------------------------------------------------------------------
219 arrow_button_press_event(GtkToggleButton
* button
, GdkEventButton
* event
, wxToolBarTool
* tool
)
221 if (event
->button
== 1)
223 g_signal_handlers_block_by_func(button
, (void*)arrow_toggled
, tool
);
224 gtk_toggle_button_set_active(button
, true);
225 tool
->ShowDropdown(button
);
226 gtk_toggle_button_set_active(button
, false);
227 g_signal_handlers_unblock_by_func(button
, (void*)arrow_toggled
, tool
);
234 void wxToolBar::AddChildGTK(wxWindowGTK
* child
)
236 GtkWidget
* align
= gtk_alignment_new(0.5, 0.5, 0, 0);
237 gtk_widget_show(align
);
238 gtk_container_add(GTK_CONTAINER(align
), child
->m_widget
);
239 GtkToolItem
* item
= gtk_tool_item_new();
240 gtk_container_add(GTK_CONTAINER(item
), align
);
241 // position will be corrected in DoInsertTool if necessary
242 gtk_toolbar_insert(GTK_TOOLBAR(gtk_bin_get_child(GTK_BIN(m_widget
))), item
, -1);
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 void wxToolBarTool::SetImage()
251 const wxBitmap
& bitmap
= GetNormalBitmap();
252 wxCHECK_RET(bitmap
.IsOk(), "invalid bitmap for wxToolBar icon");
254 GtkWidget
* image
= gtk_tool_button_get_icon_widget(GTK_TOOL_BUTTON(m_item
));
255 // always use pixbuf, because pixmap mask does not
256 // work with disabled images in some themes
257 gtk_image_set_from_pixbuf(GTK_IMAGE(image
), bitmap
.GetPixbuf());
260 // helper to create a dropdown menu item
261 void wxToolBarTool::CreateDropDown()
263 gtk_tool_item_set_homogeneous(m_item
, false);
266 if (GetToolBar()->HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
268 box
= gtk_vbox_new(false, 0);
269 arrow
= gtk_arrow_new(GTK_ARROW_RIGHT
, GTK_SHADOW_NONE
);
273 box
= gtk_hbox_new(false, 0);
274 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
276 GtkWidget
* tool_button
= gtk_bin_get_child(GTK_BIN(m_item
));
277 gtk_widget_reparent(tool_button
, box
);
278 GtkWidget
* arrow_button
= gtk_toggle_button_new();
279 gtk_button_set_relief(GTK_BUTTON(arrow_button
),
280 gtk_tool_item_get_relief_style(GTK_TOOL_ITEM(m_item
)));
281 gtk_container_add(GTK_CONTAINER(arrow_button
), arrow
);
282 gtk_container_add(GTK_CONTAINER(box
), arrow_button
);
283 gtk_widget_show_all(box
);
284 gtk_container_add(GTK_CONTAINER(m_item
), box
);
286 g_signal_connect(arrow_button
, "toggled", G_CALLBACK(arrow_toggled
), this);
287 g_signal_connect(arrow_button
, "button_press_event",
288 G_CALLBACK(arrow_button_press_event
), this);
291 void wxToolBarTool::ShowDropdown(GtkToggleButton
* button
)
293 wxToolBarBase
* toolbar
= GetToolBar();
294 wxCommandEvent
event(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, GetId());
295 if (!toolbar
->HandleWindowEvent(event
))
297 wxMenu
* menu
= GetDropdownMenu();
301 gtk_widget_get_allocation(GTK_WIDGET(button
), &alloc
);
304 if (toolbar
->HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
308 toolbar
->PopupMenu(menu
, x
, y
);
313 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
314 const wxString
& text
,
315 const wxBitmap
& bitmap1
,
316 const wxBitmap
& bitmap2
,
318 wxObject
*clientData
,
319 const wxString
& shortHelpString
,
320 const wxString
& longHelpString
)
322 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
323 clientData
, shortHelpString
, longHelpString
);
327 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
329 return new wxToolBarTool(this, control
, label
);
332 //-----------------------------------------------------------------------------
333 // wxToolBar construction
334 //-----------------------------------------------------------------------------
336 void wxToolBar::Init()
342 wxToolBar::~wxToolBar()
344 if (m_tooltips
) // always NULL if GTK >= 2.12
346 gtk_object_destroy(GTK_OBJECT(m_tooltips
));
347 g_object_unref(m_tooltips
);
351 bool wxToolBar::Create( wxWindow
*parent
,
356 const wxString
& name
)
358 if ( !PreCreation( parent
, pos
, size
) ||
359 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
361 wxFAIL_MSG( wxT("wxToolBar creation failed") );
368 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
369 #if !GTK_CHECK_VERSION(3,0,0) && !defined(GTK_DISABLE_DEPRECATED)
370 if (gtk_check_version(2, 12, 0))
372 m_tooltips
= gtk_tooltips_new();
373 g_object_ref(m_tooltips
);
374 gtk_object_sink(GTK_OBJECT(m_tooltips
));
379 if (style
& wxTB_DOCKABLE
)
381 m_widget
= gtk_handle_box_new();
383 g_signal_connect(m_widget
, "child_detached",
384 G_CALLBACK(child_detached
), NULL
);
385 g_signal_connect(m_widget
, "child_attached",
386 G_CALLBACK(child_attached
), NULL
);
388 if (style
& wxTB_FLAT
)
389 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
393 m_widget
= gtk_event_box_new();
394 ConnectWidget( m_widget
);
396 g_object_ref(m_widget
);
397 gtk_container_add(GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
));
398 gtk_widget_show(GTK_WIDGET(m_toolbar
));
400 m_parent
->DoAddChild( this );
407 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
409 return gtk_widget_get_window(GTK_WIDGET(m_toolbar
));
412 void wxToolBar::GtkSetStyle()
414 GtkOrientation orient
= GTK_ORIENTATION_HORIZONTAL
;
415 if (HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
416 orient
= GTK_ORIENTATION_VERTICAL
;
418 GtkToolbarStyle style
= GTK_TOOLBAR_ICONS
;
419 if (HasFlag(wxTB_NOICONS
))
420 style
= GTK_TOOLBAR_TEXT
;
421 else if (HasFlag(wxTB_TEXT
))
423 style
= GTK_TOOLBAR_BOTH
;
424 if (HasFlag(wxTB_HORZ_LAYOUT
))
425 style
= GTK_TOOLBAR_BOTH_HORIZ
;
428 #if GTK_CHECK_VERSION(3,0,0) || defined(GTK_DISABLE_DEPRECATED)
429 gtk_orientable_set_orientation(GTK_ORIENTABLE(m_toolbar
), orient
);
431 gtk_toolbar_set_orientation(m_toolbar
, orient
);
433 gtk_toolbar_set_style(m_toolbar
, style
);
436 void wxToolBar::SetWindowStyleFlag( long style
)
438 wxToolBarBase::SetWindowStyleFlag(style
);
444 bool wxToolBar::Realize()
446 if ( !wxToolBarBase::Realize() )
449 // bring the initial state of all the toolbar items in line with the
450 // internal state if the latter was changed by calling wxToolBarTool::
451 // Enable(): this works under MSW, where the toolbar items are only created
452 // in Realize() which uses the internal state to determine the initial
453 // button state, so make it work under GTK too
454 for ( wxToolBarToolsList::const_iterator i
= m_tools
.begin();
458 // by default the toolbar items are enabled and not toggled, so we only
459 // have to do something if their internal state doesn't correspond to
461 if ( !(*i
)->IsEnabled() )
462 DoEnableTool(*i
, false);
463 if ( (*i
)->IsToggled() )
464 DoToggleTool(*i
, true);
470 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
472 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
475 GtkWidget
* bin_child
;
476 switch ( tool
->GetStyle() )
478 case wxTOOL_STYLE_BUTTON
:
479 switch (tool
->GetKind())
482 tool
->m_item
= gtk_toggle_tool_button_new();
483 g_signal_connect(tool
->m_item
, "toggled",
484 G_CALLBACK(item_toggled
), tool
);
487 radioGroup
= GetRadioGroup(pos
);
490 // this is the first button in the radio button group,
491 // it will be toggled automatically by GTK so bring the
492 // internal flag in sync
495 tool
->m_item
= gtk_radio_tool_button_new(radioGroup
);
496 g_signal_connect(tool
->m_item
, "toggled",
497 G_CALLBACK(item_toggled
), tool
);
500 wxFAIL_MSG("unknown toolbar child type");
502 case wxITEM_DROPDOWN
:
504 tool
->m_item
= gtk_tool_button_new(NULL
, "");
505 g_signal_connect(tool
->m_item
, "clicked",
506 G_CALLBACK(item_clicked
), tool
);
509 if (!HasFlag(wxTB_NOICONS
))
511 GtkWidget
* image
= gtk_image_new();
512 gtk_tool_button_set_icon_widget(
513 GTK_TOOL_BUTTON(tool
->m_item
), image
);
515 gtk_widget_show(image
);
516 g_signal_connect(image
, "expose_event",
517 G_CALLBACK(image_expose_event
), tool
);
519 if (!tool
->GetLabel().empty())
521 gtk_tool_button_set_label(
522 GTK_TOOL_BUTTON(tool
->m_item
), wxGTK_CONV(tool
->GetLabel()));
523 // needed for labels in horizontal toolbar with wxTB_HORZ_LAYOUT
524 gtk_tool_item_set_is_important(tool
->m_item
, true);
526 if (!HasFlag(wxTB_NO_TOOLTIPS
) && !tool
->GetShortHelp().empty())
528 #if GTK_CHECK_VERSION(2, 12, 0)
529 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,12,0) == NULL
)
531 gtk_tool_item_set_tooltip_text(tool
->m_item
,
532 wxGTK_CONV(tool
->GetShortHelp()));
537 #if !GTK_CHECK_VERSION(3,0,0) && !defined(GTK_DISABLE_DEPRECATED)
538 gtk_tool_item_set_tooltip(tool
->m_item
,
539 m_tooltips
, wxGTK_CONV(tool
->GetShortHelp()), "");
543 bin_child
= gtk_bin_get_child(GTK_BIN(tool
->m_item
));
544 g_signal_connect(bin_child
, "button_press_event",
545 G_CALLBACK(button_press_event
), tool
);
546 g_signal_connect(bin_child
, "enter_notify_event",
547 G_CALLBACK(enter_notify_event
), tool
);
548 g_signal_connect(bin_child
, "leave_notify_event",
549 G_CALLBACK(enter_notify_event
), tool
);
551 if (tool
->GetKind() == wxITEM_DROPDOWN
)
552 tool
->CreateDropDown();
553 gtk_toolbar_insert(m_toolbar
, tool
->m_item
, int(pos
));
556 case wxTOOL_STYLE_SEPARATOR
:
557 tool
->m_item
= gtk_separator_tool_item_new();
558 if ( tool
->IsStretchable() )
560 gtk_separator_tool_item_set_draw
562 GTK_SEPARATOR_TOOL_ITEM(tool
->m_item
),
565 gtk_tool_item_set_expand(tool
->m_item
, TRUE
);
567 gtk_toolbar_insert(m_toolbar
, tool
->m_item
, int(pos
));
570 case wxTOOL_STYLE_CONTROL
:
571 wxWindow
* control
= tool
->GetControl();
572 if (gtk_widget_get_parent(control
->m_widget
) == NULL
)
573 AddChildGTK(control
);
574 tool
->m_item
= GTK_TOOL_ITEM(gtk_widget_get_parent(gtk_widget_get_parent(control
->m_widget
)));
575 if (gtk_toolbar_get_item_index(m_toolbar
, tool
->m_item
) != int(pos
))
577 g_object_ref(tool
->m_item
);
578 gtk_container_remove(
579 GTK_CONTAINER(m_toolbar
), GTK_WIDGET(tool
->m_item
));
580 gtk_toolbar_insert(m_toolbar
, tool
->m_item
, int(pos
));
581 g_object_unref(tool
->m_item
);
585 gtk_widget_show(GTK_WIDGET(tool
->m_item
));
587 InvalidateBestSize();
592 bool wxToolBar::DoDeleteTool(size_t /* pos */, wxToolBarToolBase
* toolBase
)
594 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
596 if (tool
->GetStyle() == wxTOOL_STYLE_CONTROL
)
598 // don't destroy the control here as we can be called from
599 // RemoveTool() and then we need to keep the control alive;
600 // while if we're called from DeleteTool() the control will
601 // be destroyed when wxToolBarToolBase itself is deleted
602 GtkWidget
* widget
= tool
->GetControl()->m_widget
;
603 gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(widget
)), widget
);
605 gtk_object_destroy(GTK_OBJECT(tool
->m_item
));
608 InvalidateBestSize();
612 GSList
* wxToolBar::GetRadioGroup(size_t pos
)
614 GSList
* radioGroup
= NULL
;
615 GtkToolItem
* item
= NULL
;
618 item
= gtk_toolbar_get_nth_item(m_toolbar
, int(pos
) - 1);
619 if (!GTK_IS_RADIO_TOOL_BUTTON(item
))
622 if (item
== NULL
&& pos
< m_tools
.size())
624 item
= gtk_toolbar_get_nth_item(m_toolbar
, int(pos
));
625 if (!GTK_IS_RADIO_TOOL_BUTTON(item
))
629 radioGroup
= gtk_radio_tool_button_get_group((GtkRadioToolButton
*)item
);
633 // ----------------------------------------------------------------------------
634 // wxToolBar tools state
635 // ----------------------------------------------------------------------------
637 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
639 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
642 gtk_widget_set_sensitive(GTK_WIDGET(tool
->m_item
), enable
);
645 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
647 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
651 g_signal_handlers_block_by_func(tool
->m_item
, (void*)item_toggled
, tool
);
653 gtk_toggle_tool_button_set_active(
654 GTK_TOGGLE_TOOL_BUTTON(tool
->m_item
), toggle
);
656 g_signal_handlers_unblock_by_func(tool
->m_item
, (void*)item_toggled
, tool
);
660 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
661 bool WXUNUSED(toggle
))
663 // VZ: absolutely no idea about how to do it
664 wxFAIL_MSG( wxT("not implemented") );
667 // ----------------------------------------------------------------------------
668 // wxToolBar geometry
669 // ----------------------------------------------------------------------------
671 wxSize
wxToolBar::DoGetBestSize() const
673 // Unfortunately, if overflow arrow is enabled GtkToolbar only reports size
674 // of arrow. To get the real size, the arrow is temporarily disabled here.
675 // This is gross, since it will cause a queue_resize, and could potentially
676 // lead to an infinite loop. But there seems to be no alternative, short of
677 // disabling the arrow entirely.
678 gtk_toolbar_set_show_arrow(m_toolbar
, false);
679 const wxSize size
= wxToolBarBase::DoGetBestSize();
680 gtk_toolbar_set_show_arrow(m_toolbar
, true);
684 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
685 wxCoord
WXUNUSED(y
)) const
687 // VZ: GTK+ doesn't seem to have such thing
688 wxFAIL_MSG( wxT("wxToolBar::FindToolForPosition() not implemented") );
693 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
695 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
699 (void)tool
->SetShortHelp(helpString
);
702 #if GTK_CHECK_VERSION(2, 12, 0)
703 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,12,0) == NULL
)
705 gtk_tool_item_set_tooltip_text(tool
->m_item
,
706 wxGTK_CONV(helpString
));
711 #if !GTK_CHECK_VERSION(3,0,0) && !defined(GTK_DISABLE_DEPRECATED)
712 gtk_tool_item_set_tooltip(tool
->m_item
,
713 m_tooltips
, wxGTK_CONV(helpString
), "");
720 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
722 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
725 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
727 tool
->SetNormalBitmap(bitmap
);
732 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
734 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
737 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
739 tool
->SetDisabledBitmap(bitmap
);
743 // ----------------------------------------------------------------------------
747 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
749 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
752 #endif // wxUSE_TOOLBAR_NATIVE