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"
19 #include "wx/gtk/private.h"
20 #include "wx/gtk/private/gtk2-compat.h"
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
27 extern bool g_blockEventsOnDrag
;
28 extern wxCursor g_globalCursor
;
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 class wxToolBarTool
: public wxToolBarToolBase
37 wxToolBarTool(wxToolBar
*tbar
,
39 const wxString
& label
,
40 const wxBitmap
& bitmap1
,
41 const wxBitmap
& bitmap2
,
44 const wxString
& shortHelpString
,
45 const wxString
& longHelpString
)
46 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
47 clientData
, shortHelpString
, longHelpString
)
52 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
53 : wxToolBarToolBase(tbar
, control
, label
)
59 void CreateDropDown();
60 void ShowDropdown(GtkToggleButton
* button
);
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
71 // ============================================================================
73 // ============================================================================
75 //-----------------------------------------------------------------------------
76 // "clicked" from m_item
77 //-----------------------------------------------------------------------------
80 static void item_clicked(GtkToolButton
*, wxToolBarTool
* tool
)
82 if (g_blockEventsOnDrag
) return;
84 tool
->GetToolBar()->OnLeftClick(tool
->GetId(), false);
88 //-----------------------------------------------------------------------------
89 // "toggled" from m_item
90 //-----------------------------------------------------------------------------
93 static void item_toggled(GtkToggleToolButton
* button
, wxToolBarTool
* tool
)
95 if (g_blockEventsOnDrag
) return;
97 const bool active
= gtk_toggle_tool_button_get_active(button
) != 0;
99 if (!active
&& tool
->GetKind() == wxITEM_RADIO
)
102 if (!tool
->GetToolBar()->OnLeftClick(tool
->GetId(), active
))
110 //-----------------------------------------------------------------------------
111 // "button_press_event" from m_item child
112 //-----------------------------------------------------------------------------
116 button_press_event(GtkWidget
*, GdkEventButton
* event
, wxToolBarTool
* tool
)
118 if (event
->button
!= 3)
121 if (g_blockEventsOnDrag
) return TRUE
;
123 tool
->GetToolBar()->OnRightClick(
124 tool
->GetId(), int(event
->x
), int(event
->y
));
130 //-----------------------------------------------------------------------------
131 // "child_detached" from m_widget
132 //-----------------------------------------------------------------------------
135 static void child_detached(GtkWidget
*, GtkToolbar
* toolbar
, void*)
137 // disable showing overflow arrow when toolbar is detached,
138 // otherwise toolbar collapses to just an arrow
139 gtk_toolbar_set_show_arrow(toolbar
, false);
143 //-----------------------------------------------------------------------------
144 // "child_attached" from m_widget
145 //-----------------------------------------------------------------------------
148 static void child_attached(GtkWidget
*, GtkToolbar
* toolbar
, void*)
150 gtk_toolbar_set_show_arrow(toolbar
, true);
154 //-----------------------------------------------------------------------------
155 // "enter_notify_event" / "leave_notify_event" from m_item
156 //-----------------------------------------------------------------------------
160 enter_notify_event(GtkWidget
*, GdkEventCrossing
* event
, wxToolBarTool
* tool
)
162 if (g_blockEventsOnDrag
) return TRUE
;
165 if (event
->type
== GDK_ENTER_NOTIFY
)
167 tool
->GetToolBar()->OnMouseEnter(id
);
173 //-----------------------------------------------------------------------------
174 // "expose_event" from GtkImage inside m_item
175 //-----------------------------------------------------------------------------
180 image_draw(GtkWidget
* widget
, cairo_t
* cr
, wxToolBarTool
* tool
)
182 image_expose_event(GtkWidget
* widget
, GdkEventExpose
*, wxToolBarTool
* tool
)
185 const wxBitmap
& bitmap
= tool
->GetDisabledBitmap();
186 if (tool
->IsEnabled() || !bitmap
.IsOk())
189 // draw disabled bitmap ourselves, GtkImage has no way to specify it
191 gtk_widget_get_allocation(widget
, &alloc
);
193 gtk_widget_get_requisition(widget
, &req
);
194 const int x
= alloc
.x
+ (alloc
.width
- req
.width
) / 2;
195 const int y
= alloc
.y
+ (alloc
.height
- req
.height
) / 2;
197 bitmap
.Draw(cr
, x
, y
);
200 gtk_widget_get_window(widget
), gtk_widget_get_style(widget
)->black_gc
, bitmap
.GetPixbuf(),
202 -1, -1, GDK_RGB_DITHER_NORMAL
, 0, 0);
208 //-----------------------------------------------------------------------------
209 // "toggled" from dropdown menu button
210 //-----------------------------------------------------------------------------
213 static void arrow_toggled(GtkToggleButton
* button
, wxToolBarTool
* tool
)
215 if (gtk_toggle_button_get_active(button
))
217 tool
->ShowDropdown(button
);
218 gtk_toggle_button_set_active(button
, false);
223 //-----------------------------------------------------------------------------
224 // "button_press_event" from dropdown menu button
225 //-----------------------------------------------------------------------------
229 arrow_button_press_event(GtkToggleButton
* button
, GdkEventButton
* event
, wxToolBarTool
* tool
)
231 if (event
->button
== 1)
233 g_signal_handlers_block_by_func(button
, (void*)arrow_toggled
, tool
);
234 gtk_toggle_button_set_active(button
, true);
235 tool
->ShowDropdown(button
);
236 gtk_toggle_button_set_active(button
, false);
237 g_signal_handlers_unblock_by_func(button
, (void*)arrow_toggled
, tool
);
244 void wxToolBar::AddChildGTK(wxWindowGTK
* child
)
246 GtkWidget
* align
= gtk_alignment_new(0.5, 0.5, 0, 0);
247 gtk_widget_show(align
);
248 gtk_container_add(GTK_CONTAINER(align
), child
->m_widget
);
249 GtkToolItem
* item
= gtk_tool_item_new();
250 gtk_container_add(GTK_CONTAINER(item
), align
);
251 // position will be corrected in DoInsertTool if necessary
252 gtk_toolbar_insert(GTK_TOOLBAR(gtk_bin_get_child(GTK_BIN(m_widget
))), item
, -1);
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
259 void wxToolBarTool::SetImage()
261 const wxBitmap
& bitmap
= GetNormalBitmap();
262 wxCHECK_RET(bitmap
.IsOk(), "invalid bitmap for wxToolBar icon");
264 GtkWidget
* image
= gtk_tool_button_get_icon_widget(GTK_TOOL_BUTTON(m_item
));
265 // always use pixbuf, because pixmap mask does not
266 // work with disabled images in some themes
267 gtk_image_set_from_pixbuf(GTK_IMAGE(image
), bitmap
.GetPixbuf());
270 // helper to create a dropdown menu item
271 void wxToolBarTool::CreateDropDown()
273 gtk_tool_item_set_homogeneous(m_item
, false);
274 GtkOrientation orient
= GTK_ORIENTATION_HORIZONTAL
;
275 GtkArrowType arrowType
= GTK_ARROW_DOWN
;
276 if (GetToolBar()->HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
278 orient
= GTK_ORIENTATION_VERTICAL
;
279 arrowType
= GTK_ARROW_RIGHT
;
281 GtkWidget
* box
= gtk_box_new(orient
, 0);
282 GtkWidget
* arrow
= gtk_arrow_new(arrowType
, GTK_SHADOW_NONE
);
283 GtkWidget
* tool_button
= gtk_bin_get_child(GTK_BIN(m_item
));
284 gtk_widget_reparent(tool_button
, box
);
285 GtkWidget
* arrow_button
= gtk_toggle_button_new();
286 gtk_button_set_relief(GTK_BUTTON(arrow_button
),
287 gtk_tool_item_get_relief_style(GTK_TOOL_ITEM(m_item
)));
288 gtk_container_add(GTK_CONTAINER(arrow_button
), arrow
);
289 gtk_container_add(GTK_CONTAINER(box
), arrow_button
);
290 gtk_widget_show_all(box
);
291 gtk_container_add(GTK_CONTAINER(m_item
), box
);
293 g_signal_connect(arrow_button
, "toggled", G_CALLBACK(arrow_toggled
), this);
294 g_signal_connect(arrow_button
, "button_press_event",
295 G_CALLBACK(arrow_button_press_event
), this);
298 void wxToolBarTool::ShowDropdown(GtkToggleButton
* button
)
300 wxToolBarBase
* toolbar
= GetToolBar();
301 wxCommandEvent
event(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, GetId());
302 if (!toolbar
->HandleWindowEvent(event
))
304 wxMenu
* menu
= GetDropdownMenu();
308 gtk_widget_get_allocation(GTK_WIDGET(button
), &alloc
);
311 if (toolbar
->HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
315 toolbar
->PopupMenu(menu
, x
, y
);
320 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
321 const wxString
& text
,
322 const wxBitmap
& bitmap1
,
323 const wxBitmap
& bitmap2
,
325 wxObject
*clientData
,
326 const wxString
& shortHelpString
,
327 const wxString
& longHelpString
)
329 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
330 clientData
, shortHelpString
, longHelpString
);
334 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
336 return new wxToolBarTool(this, control
, label
);
339 //-----------------------------------------------------------------------------
340 // wxToolBar construction
341 //-----------------------------------------------------------------------------
343 void wxToolBar::Init()
349 wxToolBar::~wxToolBar()
352 if (m_tooltips
) // always NULL if GTK >= 2.12
354 gtk_object_destroy(GTK_OBJECT(m_tooltips
));
355 g_object_unref(m_tooltips
);
360 bool wxToolBar::Create( wxWindow
*parent
,
365 const wxString
& name
)
367 if ( !PreCreation( parent
, pos
, size
) ||
368 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
370 wxFAIL_MSG( wxT("wxToolBar creation failed") );
377 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
379 if (gtk_check_version(2, 12, 0))
381 m_tooltips
= gtk_tooltips_new();
382 g_object_ref(m_tooltips
);
383 gtk_object_sink(GTK_OBJECT(m_tooltips
));
388 if (style
& wxTB_DOCKABLE
)
390 m_widget
= gtk_handle_box_new();
392 g_signal_connect(m_widget
, "child_detached",
393 G_CALLBACK(child_detached
), NULL
);
394 g_signal_connect(m_widget
, "child_attached",
395 G_CALLBACK(child_attached
), NULL
);
397 if (style
& wxTB_FLAT
)
398 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
402 m_widget
= gtk_event_box_new();
403 ConnectWidget( m_widget
);
405 g_object_ref(m_widget
);
406 gtk_container_add(GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
));
407 gtk_widget_show(GTK_WIDGET(m_toolbar
));
409 m_parent
->DoAddChild( this );
416 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
418 return gtk_widget_get_window(GTK_WIDGET(m_toolbar
));
421 void wxToolBar::GtkSetStyle()
423 GtkOrientation orient
= GTK_ORIENTATION_HORIZONTAL
;
424 if (HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
425 orient
= GTK_ORIENTATION_VERTICAL
;
427 GtkToolbarStyle style
= GTK_TOOLBAR_ICONS
;
428 if (HasFlag(wxTB_NOICONS
))
429 style
= GTK_TOOLBAR_TEXT
;
430 else if (HasFlag(wxTB_TEXT
))
432 style
= GTK_TOOLBAR_BOTH
;
433 if (HasFlag(wxTB_HORZ_LAYOUT
))
434 style
= GTK_TOOLBAR_BOTH_HORIZ
;
438 gtk_orientable_set_orientation(GTK_ORIENTABLE(m_toolbar
), orient
);
440 gtk_toolbar_set_orientation(m_toolbar
, orient
);
442 gtk_toolbar_set_style(m_toolbar
, style
);
445 void wxToolBar::SetWindowStyleFlag( long style
)
447 wxToolBarBase::SetWindowStyleFlag(style
);
453 bool wxToolBar::Realize()
455 if ( !wxToolBarBase::Realize() )
458 // bring the initial state of all the toolbar items in line with the
459 // internal state if the latter was changed by calling wxToolBarTool::
460 // Enable(): this works under MSW, where the toolbar items are only created
461 // in Realize() which uses the internal state to determine the initial
462 // button state, so make it work under GTK too
463 for ( wxToolBarToolsList::const_iterator i
= m_tools
.begin();
467 // by default the toolbar items are enabled and not toggled, so we only
468 // have to do something if their internal state doesn't correspond to
470 if ( !(*i
)->IsEnabled() )
471 DoEnableTool(*i
, false);
472 if ( (*i
)->IsToggled() )
473 DoToggleTool(*i
, true);
479 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
481 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
484 GtkWidget
* bin_child
;
485 switch ( tool
->GetStyle() )
487 case wxTOOL_STYLE_BUTTON
:
488 switch (tool
->GetKind())
491 tool
->m_item
= gtk_toggle_tool_button_new();
492 g_signal_connect(tool
->m_item
, "toggled",
493 G_CALLBACK(item_toggled
), tool
);
496 radioGroup
= GetRadioGroup(pos
);
499 // this is the first button in the radio button group,
500 // it will be toggled automatically by GTK so bring the
501 // internal flag in sync
504 tool
->m_item
= gtk_radio_tool_button_new(radioGroup
);
505 g_signal_connect(tool
->m_item
, "toggled",
506 G_CALLBACK(item_toggled
), tool
);
509 wxFAIL_MSG("unknown toolbar child type");
511 case wxITEM_DROPDOWN
:
513 tool
->m_item
= gtk_tool_button_new(NULL
, "");
514 g_signal_connect(tool
->m_item
, "clicked",
515 G_CALLBACK(item_clicked
), tool
);
518 if (!HasFlag(wxTB_NOICONS
))
520 GtkWidget
* image
= gtk_image_new();
521 gtk_tool_button_set_icon_widget(
522 GTK_TOOL_BUTTON(tool
->m_item
), image
);
524 gtk_widget_show(image
);
526 g_signal_connect(image
, "draw",
527 G_CALLBACK(image_draw
), tool
);
529 g_signal_connect(image
, "expose_event",
530 G_CALLBACK(image_expose_event
), tool
);
533 if (!tool
->GetLabel().empty())
535 gtk_tool_button_set_label(
536 GTK_TOOL_BUTTON(tool
->m_item
), wxGTK_CONV(tool
->GetLabel()));
537 // needed for labels in horizontal toolbar with wxTB_HORZ_LAYOUT
538 gtk_tool_item_set_is_important(tool
->m_item
, true);
540 if (!HasFlag(wxTB_NO_TOOLTIPS
) && !tool
->GetShortHelp().empty())
542 #if GTK_CHECK_VERSION(2, 12, 0)
543 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,12,0) == NULL
)
545 gtk_tool_item_set_tooltip_text(tool
->m_item
,
546 wxGTK_CONV(tool
->GetShortHelp()));
552 gtk_tool_item_set_tooltip(tool
->m_item
,
553 m_tooltips
, wxGTK_CONV(tool
->GetShortHelp()), "");
557 bin_child
= gtk_bin_get_child(GTK_BIN(tool
->m_item
));
558 g_signal_connect(bin_child
, "button_press_event",
559 G_CALLBACK(button_press_event
), tool
);
560 g_signal_connect(bin_child
, "enter_notify_event",
561 G_CALLBACK(enter_notify_event
), tool
);
562 g_signal_connect(bin_child
, "leave_notify_event",
563 G_CALLBACK(enter_notify_event
), tool
);
565 if (tool
->GetKind() == wxITEM_DROPDOWN
)
566 tool
->CreateDropDown();
567 gtk_toolbar_insert(m_toolbar
, tool
->m_item
, int(pos
));
570 case wxTOOL_STYLE_SEPARATOR
:
571 tool
->m_item
= gtk_separator_tool_item_new();
572 if ( tool
->IsStretchable() )
574 gtk_separator_tool_item_set_draw
576 GTK_SEPARATOR_TOOL_ITEM(tool
->m_item
),
579 gtk_tool_item_set_expand(tool
->m_item
, TRUE
);
581 gtk_toolbar_insert(m_toolbar
, tool
->m_item
, int(pos
));
584 case wxTOOL_STYLE_CONTROL
:
585 wxWindow
* control
= tool
->GetControl();
586 if (gtk_widget_get_parent(control
->m_widget
) == NULL
)
587 AddChildGTK(control
);
588 tool
->m_item
= GTK_TOOL_ITEM(gtk_widget_get_parent(gtk_widget_get_parent(control
->m_widget
)));
589 if (gtk_toolbar_get_item_index(m_toolbar
, tool
->m_item
) != int(pos
))
591 g_object_ref(tool
->m_item
);
592 gtk_container_remove(
593 GTK_CONTAINER(m_toolbar
), GTK_WIDGET(tool
->m_item
));
594 gtk_toolbar_insert(m_toolbar
, tool
->m_item
, int(pos
));
595 g_object_unref(tool
->m_item
);
599 gtk_widget_show(GTK_WIDGET(tool
->m_item
));
601 InvalidateBestSize();
606 bool wxToolBar::DoDeleteTool(size_t /* pos */, wxToolBarToolBase
* toolBase
)
608 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
610 if (tool
->GetStyle() == wxTOOL_STYLE_CONTROL
)
612 // don't destroy the control here as we can be called from
613 // RemoveTool() and then we need to keep the control alive;
614 // while if we're called from DeleteTool() the control will
615 // be destroyed when wxToolBarToolBase itself is deleted
616 GtkWidget
* widget
= tool
->GetControl()->m_widget
;
617 gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(widget
)), widget
);
619 gtk_widget_destroy(GTK_WIDGET(tool
->m_item
));
622 InvalidateBestSize();
626 GSList
* wxToolBar::GetRadioGroup(size_t pos
)
628 GSList
* radioGroup
= NULL
;
629 GtkToolItem
* item
= NULL
;
632 item
= gtk_toolbar_get_nth_item(m_toolbar
, int(pos
) - 1);
633 if (!GTK_IS_RADIO_TOOL_BUTTON(item
))
636 if (item
== NULL
&& pos
< m_tools
.size())
638 item
= gtk_toolbar_get_nth_item(m_toolbar
, int(pos
));
639 if (!GTK_IS_RADIO_TOOL_BUTTON(item
))
643 radioGroup
= gtk_radio_tool_button_get_group((GtkRadioToolButton
*)item
);
647 // ----------------------------------------------------------------------------
648 // wxToolBar tools state
649 // ----------------------------------------------------------------------------
651 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
653 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
656 gtk_widget_set_sensitive(GTK_WIDGET(tool
->m_item
), enable
);
659 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
661 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
665 g_signal_handlers_block_by_func(tool
->m_item
, (void*)item_toggled
, tool
);
667 gtk_toggle_tool_button_set_active(
668 GTK_TOGGLE_TOOL_BUTTON(tool
->m_item
), toggle
);
670 g_signal_handlers_unblock_by_func(tool
->m_item
, (void*)item_toggled
, tool
);
674 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
675 bool WXUNUSED(toggle
))
677 // VZ: absolutely no idea about how to do it
678 wxFAIL_MSG( wxT("not implemented") );
681 // ----------------------------------------------------------------------------
682 // wxToolBar geometry
683 // ----------------------------------------------------------------------------
685 wxSize
wxToolBar::DoGetBestSize() const
687 // Unfortunately, if overflow arrow is enabled GtkToolbar only reports size
688 // of arrow. To get the real size, the arrow is temporarily disabled here.
689 // This is gross, since it will cause a queue_resize, and could potentially
690 // lead to an infinite loop. But there seems to be no alternative, short of
691 // disabling the arrow entirely.
692 gtk_toolbar_set_show_arrow(m_toolbar
, false);
693 const wxSize size
= wxToolBarBase::DoGetBestSize();
694 gtk_toolbar_set_show_arrow(m_toolbar
, true);
698 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
699 wxCoord
WXUNUSED(y
)) const
701 // VZ: GTK+ doesn't seem to have such thing
702 wxFAIL_MSG( wxT("wxToolBar::FindToolForPosition() not implemented") );
707 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
709 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
713 (void)tool
->SetShortHelp(helpString
);
716 #if GTK_CHECK_VERSION(2, 12, 0)
717 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,12,0) == NULL
)
719 gtk_tool_item_set_tooltip_text(tool
->m_item
,
720 wxGTK_CONV(helpString
));
726 gtk_tool_item_set_tooltip(tool
->m_item
,
727 m_tooltips
, wxGTK_CONV(helpString
), "");
734 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
736 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
739 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
741 tool
->SetNormalBitmap(bitmap
);
746 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
748 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
751 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
753 tool
->SetDisabledBitmap(bitmap
);
757 // ----------------------------------------------------------------------------
761 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
763 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new());
766 #endif // wxUSE_TOOLBAR_NATIVE