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);
276 if (GetToolBar()->HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
278 box
= gtk_vbox_new(false, 0);
279 arrow
= gtk_arrow_new(GTK_ARROW_RIGHT
, GTK_SHADOW_NONE
);
283 box
= gtk_hbox_new(false, 0);
284 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
286 GtkWidget
* tool_button
= gtk_bin_get_child(GTK_BIN(m_item
));
287 gtk_widget_reparent(tool_button
, box
);
288 GtkWidget
* arrow_button
= gtk_toggle_button_new();
289 gtk_button_set_relief(GTK_BUTTON(arrow_button
),
290 gtk_tool_item_get_relief_style(GTK_TOOL_ITEM(m_item
)));
291 gtk_container_add(GTK_CONTAINER(arrow_button
), arrow
);
292 gtk_container_add(GTK_CONTAINER(box
), arrow_button
);
293 gtk_widget_show_all(box
);
294 gtk_container_add(GTK_CONTAINER(m_item
), box
);
296 g_signal_connect(arrow_button
, "toggled", G_CALLBACK(arrow_toggled
), this);
297 g_signal_connect(arrow_button
, "button_press_event",
298 G_CALLBACK(arrow_button_press_event
), this);
301 void wxToolBarTool::ShowDropdown(GtkToggleButton
* button
)
303 wxToolBarBase
* toolbar
= GetToolBar();
304 wxCommandEvent
event(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, GetId());
305 if (!toolbar
->HandleWindowEvent(event
))
307 wxMenu
* menu
= GetDropdownMenu();
311 gtk_widget_get_allocation(GTK_WIDGET(button
), &alloc
);
314 if (toolbar
->HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
318 toolbar
->PopupMenu(menu
, x
, y
);
323 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
324 const wxString
& text
,
325 const wxBitmap
& bitmap1
,
326 const wxBitmap
& bitmap2
,
328 wxObject
*clientData
,
329 const wxString
& shortHelpString
,
330 const wxString
& longHelpString
)
332 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
333 clientData
, shortHelpString
, longHelpString
);
337 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
339 return new wxToolBarTool(this, control
, label
);
342 //-----------------------------------------------------------------------------
343 // wxToolBar construction
344 //-----------------------------------------------------------------------------
346 void wxToolBar::Init()
352 wxToolBar::~wxToolBar()
355 if (m_tooltips
) // always NULL if GTK >= 2.12
357 gtk_object_destroy(GTK_OBJECT(m_tooltips
));
358 g_object_unref(m_tooltips
);
363 bool wxToolBar::Create( wxWindow
*parent
,
368 const wxString
& name
)
370 if ( !PreCreation( parent
, pos
, size
) ||
371 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
373 wxFAIL_MSG( wxT("wxToolBar creation failed") );
380 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
382 if (gtk_check_version(2, 12, 0))
384 m_tooltips
= gtk_tooltips_new();
385 g_object_ref(m_tooltips
);
386 gtk_object_sink(GTK_OBJECT(m_tooltips
));
391 if (style
& wxTB_DOCKABLE
)
393 m_widget
= gtk_handle_box_new();
395 g_signal_connect(m_widget
, "child_detached",
396 G_CALLBACK(child_detached
), NULL
);
397 g_signal_connect(m_widget
, "child_attached",
398 G_CALLBACK(child_attached
), NULL
);
400 if (style
& wxTB_FLAT
)
401 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
405 m_widget
= gtk_event_box_new();
406 ConnectWidget( m_widget
);
408 g_object_ref(m_widget
);
409 gtk_container_add(GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
));
410 gtk_widget_show(GTK_WIDGET(m_toolbar
));
412 m_parent
->DoAddChild( this );
419 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
421 return gtk_widget_get_window(GTK_WIDGET(m_toolbar
));
424 void wxToolBar::GtkSetStyle()
426 GtkOrientation orient
= GTK_ORIENTATION_HORIZONTAL
;
427 if (HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
428 orient
= GTK_ORIENTATION_VERTICAL
;
430 GtkToolbarStyle style
= GTK_TOOLBAR_ICONS
;
431 if (HasFlag(wxTB_NOICONS
))
432 style
= GTK_TOOLBAR_TEXT
;
433 else if (HasFlag(wxTB_TEXT
))
435 style
= GTK_TOOLBAR_BOTH
;
436 if (HasFlag(wxTB_HORZ_LAYOUT
))
437 style
= GTK_TOOLBAR_BOTH_HORIZ
;
441 gtk_orientable_set_orientation(GTK_ORIENTABLE(m_toolbar
), orient
);
443 gtk_toolbar_set_orientation(m_toolbar
, orient
);
445 gtk_toolbar_set_style(m_toolbar
, style
);
448 void wxToolBar::SetWindowStyleFlag( long style
)
450 wxToolBarBase::SetWindowStyleFlag(style
);
456 bool wxToolBar::Realize()
458 if ( !wxToolBarBase::Realize() )
461 // bring the initial state of all the toolbar items in line with the
462 // internal state if the latter was changed by calling wxToolBarTool::
463 // Enable(): this works under MSW, where the toolbar items are only created
464 // in Realize() which uses the internal state to determine the initial
465 // button state, so make it work under GTK too
466 for ( wxToolBarToolsList::const_iterator i
= m_tools
.begin();
470 // by default the toolbar items are enabled and not toggled, so we only
471 // have to do something if their internal state doesn't correspond to
473 if ( !(*i
)->IsEnabled() )
474 DoEnableTool(*i
, false);
475 if ( (*i
)->IsToggled() )
476 DoToggleTool(*i
, true);
482 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
484 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
487 GtkWidget
* bin_child
;
488 switch ( tool
->GetStyle() )
490 case wxTOOL_STYLE_BUTTON
:
491 switch (tool
->GetKind())
494 tool
->m_item
= gtk_toggle_tool_button_new();
495 g_signal_connect(tool
->m_item
, "toggled",
496 G_CALLBACK(item_toggled
), tool
);
499 radioGroup
= GetRadioGroup(pos
);
502 // this is the first button in the radio button group,
503 // it will be toggled automatically by GTK so bring the
504 // internal flag in sync
507 tool
->m_item
= gtk_radio_tool_button_new(radioGroup
);
508 g_signal_connect(tool
->m_item
, "toggled",
509 G_CALLBACK(item_toggled
), tool
);
512 wxFAIL_MSG("unknown toolbar child type");
514 case wxITEM_DROPDOWN
:
516 tool
->m_item
= gtk_tool_button_new(NULL
, "");
517 g_signal_connect(tool
->m_item
, "clicked",
518 G_CALLBACK(item_clicked
), tool
);
521 if (!HasFlag(wxTB_NOICONS
))
523 GtkWidget
* image
= gtk_image_new();
524 gtk_tool_button_set_icon_widget(
525 GTK_TOOL_BUTTON(tool
->m_item
), image
);
527 gtk_widget_show(image
);
529 g_signal_connect(image
, "draw",
530 G_CALLBACK(image_draw
), tool
);
532 g_signal_connect(image
, "expose_event",
533 G_CALLBACK(image_expose_event
), tool
);
536 if (!tool
->GetLabel().empty())
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);
543 if (!HasFlag(wxTB_NO_TOOLTIPS
) && !tool
->GetShortHelp().empty())
545 #if GTK_CHECK_VERSION(2, 12, 0)
546 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,12,0) == NULL
)
548 gtk_tool_item_set_tooltip_text(tool
->m_item
,
549 wxGTK_CONV(tool
->GetShortHelp()));
555 gtk_tool_item_set_tooltip(tool
->m_item
,
556 m_tooltips
, wxGTK_CONV(tool
->GetShortHelp()), "");
560 bin_child
= gtk_bin_get_child(GTK_BIN(tool
->m_item
));
561 g_signal_connect(bin_child
, "button_press_event",
562 G_CALLBACK(button_press_event
), tool
);
563 g_signal_connect(bin_child
, "enter_notify_event",
564 G_CALLBACK(enter_notify_event
), tool
);
565 g_signal_connect(bin_child
, "leave_notify_event",
566 G_CALLBACK(enter_notify_event
), tool
);
568 if (tool
->GetKind() == wxITEM_DROPDOWN
)
569 tool
->CreateDropDown();
570 gtk_toolbar_insert(m_toolbar
, tool
->m_item
, int(pos
));
573 case wxTOOL_STYLE_SEPARATOR
:
574 tool
->m_item
= gtk_separator_tool_item_new();
575 if ( tool
->IsStretchable() )
577 gtk_separator_tool_item_set_draw
579 GTK_SEPARATOR_TOOL_ITEM(tool
->m_item
),
582 gtk_tool_item_set_expand(tool
->m_item
, TRUE
);
584 gtk_toolbar_insert(m_toolbar
, tool
->m_item
, int(pos
));
587 case wxTOOL_STYLE_CONTROL
:
588 wxWindow
* control
= tool
->GetControl();
589 if (gtk_widget_get_parent(control
->m_widget
) == NULL
)
590 AddChildGTK(control
);
591 tool
->m_item
= GTK_TOOL_ITEM(gtk_widget_get_parent(gtk_widget_get_parent(control
->m_widget
)));
592 if (gtk_toolbar_get_item_index(m_toolbar
, tool
->m_item
) != int(pos
))
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
);
602 gtk_widget_show(GTK_WIDGET(tool
->m_item
));
604 InvalidateBestSize();
609 bool wxToolBar::DoDeleteTool(size_t /* pos */, wxToolBarToolBase
* toolBase
)
611 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
613 if (tool
->GetStyle() == wxTOOL_STYLE_CONTROL
)
615 // don't destroy the control here as we can be called from
616 // RemoveTool() and then we need to keep the control alive;
617 // while if we're called from DeleteTool() the control will
618 // be destroyed when wxToolBarToolBase itself is deleted
619 GtkWidget
* widget
= tool
->GetControl()->m_widget
;
620 gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(widget
)), widget
);
622 gtk_widget_destroy(GTK_WIDGET(tool
->m_item
));
625 InvalidateBestSize();
629 GSList
* wxToolBar::GetRadioGroup(size_t pos
)
631 GSList
* radioGroup
= NULL
;
632 GtkToolItem
* item
= NULL
;
635 item
= gtk_toolbar_get_nth_item(m_toolbar
, int(pos
) - 1);
636 if (!GTK_IS_RADIO_TOOL_BUTTON(item
))
639 if (item
== NULL
&& pos
< m_tools
.size())
641 item
= gtk_toolbar_get_nth_item(m_toolbar
, int(pos
));
642 if (!GTK_IS_RADIO_TOOL_BUTTON(item
))
646 radioGroup
= gtk_radio_tool_button_get_group((GtkRadioToolButton
*)item
);
650 // ----------------------------------------------------------------------------
651 // wxToolBar tools state
652 // ----------------------------------------------------------------------------
654 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
656 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
659 gtk_widget_set_sensitive(GTK_WIDGET(tool
->m_item
), enable
);
662 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
664 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
668 g_signal_handlers_block_by_func(tool
->m_item
, (void*)item_toggled
, tool
);
670 gtk_toggle_tool_button_set_active(
671 GTK_TOGGLE_TOOL_BUTTON(tool
->m_item
), toggle
);
673 g_signal_handlers_unblock_by_func(tool
->m_item
, (void*)item_toggled
, tool
);
677 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
678 bool WXUNUSED(toggle
))
680 // VZ: absolutely no idea about how to do it
681 wxFAIL_MSG( wxT("not implemented") );
684 // ----------------------------------------------------------------------------
685 // wxToolBar geometry
686 // ----------------------------------------------------------------------------
688 wxSize
wxToolBar::DoGetBestSize() const
690 // Unfortunately, if overflow arrow is enabled GtkToolbar only reports size
691 // of arrow. To get the real size, the arrow is temporarily disabled here.
692 // This is gross, since it will cause a queue_resize, and could potentially
693 // lead to an infinite loop. But there seems to be no alternative, short of
694 // disabling the arrow entirely.
695 gtk_toolbar_set_show_arrow(m_toolbar
, false);
696 const wxSize size
= wxToolBarBase::DoGetBestSize();
697 gtk_toolbar_set_show_arrow(m_toolbar
, true);
701 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
702 wxCoord
WXUNUSED(y
)) const
704 // VZ: GTK+ doesn't seem to have such thing
705 wxFAIL_MSG( wxT("wxToolBar::FindToolForPosition() not implemented") );
710 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
712 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
716 (void)tool
->SetShortHelp(helpString
);
719 #if GTK_CHECK_VERSION(2, 12, 0)
720 if (GTK_CHECK_VERSION(3,0,0) || gtk_check_version(2,12,0) == NULL
)
722 gtk_tool_item_set_tooltip_text(tool
->m_item
,
723 wxGTK_CONV(helpString
));
729 gtk_tool_item_set_tooltip(tool
->m_item
,
730 m_tooltips
, wxGTK_CONV(helpString
), "");
737 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
739 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
742 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
744 tool
->SetNormalBitmap(bitmap
);
749 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
751 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
754 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
756 tool
->SetDisabledBitmap(bitmap
);
760 // ----------------------------------------------------------------------------
764 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
766 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
769 #endif // wxUSE_TOOLBAR_NATIVE