1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/tbargtk.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
)
54 // Hold a reference to keep control alive until DoInsertTool() is
55 // called, or if RemoveTool() is called (see DoDeleteTool)
56 g_object_ref(control
->m_widget
);
57 // release reference when gtk_widget_destroy() is called on control
59 control
->m_widget
, "destroy", G_CALLBACK(g_object_unref
), NULL
);
63 void CreateDropDown();
64 void ShowDropdown(GtkToggleButton
* button
);
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
75 // ============================================================================
77 // ============================================================================
79 //-----------------------------------------------------------------------------
80 // "clicked" from m_item
81 //-----------------------------------------------------------------------------
84 static void item_clicked(GtkToolButton
*, wxToolBarTool
* tool
)
86 if (g_blockEventsOnDrag
) return;
88 tool
->GetToolBar()->OnLeftClick(tool
->GetId(), false);
92 //-----------------------------------------------------------------------------
93 // "toggled" from m_item
94 //-----------------------------------------------------------------------------
97 static void item_toggled(GtkToggleToolButton
* button
, wxToolBarTool
* tool
)
99 if (g_blockEventsOnDrag
) return;
101 const bool active
= gtk_toggle_tool_button_get_active(button
) != 0;
102 tool
->Toggle(active
);
103 if (!active
&& tool
->GetKind() == wxITEM_RADIO
)
106 if (!tool
->GetToolBar()->OnLeftClick(tool
->GetId(), active
))
114 //-----------------------------------------------------------------------------
115 // "button_press_event" from m_item child
116 //-----------------------------------------------------------------------------
120 button_press_event(GtkWidget
*, GdkEventButton
* event
, wxToolBarTool
* tool
)
122 if (event
->button
!= 3)
125 if (g_blockEventsOnDrag
) return TRUE
;
127 tool
->GetToolBar()->OnRightClick(
128 tool
->GetId(), int(event
->x
), int(event
->y
));
134 //-----------------------------------------------------------------------------
135 // "child_detached" from m_widget
136 //-----------------------------------------------------------------------------
139 static void child_detached(GtkWidget
*, GtkToolbar
* toolbar
, void*)
141 // disable showing overflow arrow when toolbar is detached,
142 // otherwise toolbar collapses to just an arrow
143 gtk_toolbar_set_show_arrow(toolbar
, false);
147 //-----------------------------------------------------------------------------
148 // "child_attached" from m_widget
149 //-----------------------------------------------------------------------------
152 static void child_attached(GtkWidget
*, GtkToolbar
* toolbar
, void*)
154 gtk_toolbar_set_show_arrow(toolbar
, true);
158 //-----------------------------------------------------------------------------
159 // "enter_notify_event" / "leave_notify_event" from m_item
160 //-----------------------------------------------------------------------------
164 enter_notify_event(GtkWidget
*, GdkEventCrossing
* event
, wxToolBarTool
* tool
)
166 if (g_blockEventsOnDrag
) return TRUE
;
169 if (event
->type
== GDK_ENTER_NOTIFY
)
171 tool
->GetToolBar()->OnMouseEnter(id
);
177 //-----------------------------------------------------------------------------
178 // "size_request" from m_toolbar
179 //-----------------------------------------------------------------------------
183 size_request(GtkWidget
*, GtkRequisition
* req
, wxToolBar
* win
)
185 const wxSize margins
= win
->GetMargins();
186 req
->width
+= margins
.x
;
187 req
->height
+= 2 * margins
.y
;
191 //-----------------------------------------------------------------------------
192 // "expose_event" from GtkImage inside m_item
193 //-----------------------------------------------------------------------------
197 image_expose_event(GtkWidget
* widget
, GdkEventExpose
*, wxToolBarTool
* tool
)
199 const wxBitmap
& bitmap
= tool
->GetDisabledBitmap();
200 if (tool
->IsEnabled() || !bitmap
.IsOk())
203 // draw disabled bitmap ourselves, GtkImage has no way to specify it
204 const GtkAllocation
& alloc
= widget
->allocation
;
206 widget
->window
, widget
->style
->black_gc
, bitmap
.GetPixbuf(),
208 alloc
.x
+ (alloc
.width
- widget
->requisition
.width
) / 2,
209 alloc
.y
+ (alloc
.height
- widget
->requisition
.height
) / 2,
210 -1, -1, GDK_RGB_DITHER_NORMAL
, 0, 0);
215 //-----------------------------------------------------------------------------
216 // "toggled" from dropdown menu button
217 //-----------------------------------------------------------------------------
220 static void arrow_toggled(GtkToggleButton
* button
, wxToolBarTool
* tool
)
222 if (gtk_toggle_button_get_active(button
))
224 tool
->ShowDropdown(button
);
225 gtk_toggle_button_set_active(button
, false);
230 //-----------------------------------------------------------------------------
231 // "button_press_event" from dropdown menu button
232 //-----------------------------------------------------------------------------
236 arrow_button_press_event(GtkToggleButton
* button
, GdkEventButton
* event
, wxToolBarTool
* tool
)
238 if (event
->button
== 1)
240 g_signal_handlers_block_by_func(button
, (void*)arrow_toggled
, tool
);
241 gtk_toggle_button_set_active(button
, true);
242 tool
->ShowDropdown(button
);
243 gtk_toggle_button_set_active(button
, false);
244 g_signal_handlers_unblock_by_func(button
, (void*)arrow_toggled
, tool
);
251 //-----------------------------------------------------------------------------
252 // InsertChild callback for wxToolBar
253 //-----------------------------------------------------------------------------
255 static void wxInsertChildInToolBar( wxWindow
* WXUNUSED(parent
),
256 wxWindow
* /* child */)
258 // Child widget will be inserted into GtkToolbar by DoInsertTool()
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 void wxToolBarTool::SetImage()
267 const wxBitmap
& bitmap
= GetNormalBitmap();
268 wxCHECK_RET(bitmap
.IsOk(), "invalid bitmap for wxToolBar icon");
270 GtkWidget
* image
= gtk_tool_button_get_icon_widget(GTK_TOOL_BUTTON(m_item
));
271 // always use pixbuf, because pixmap mask does not
272 // work with disabled images in some themes
273 gtk_image_set_from_pixbuf(GTK_IMAGE(image
), bitmap
.GetPixbuf());
276 // helper to create a dropdown menu item
277 void wxToolBarTool::CreateDropDown()
279 gtk_tool_item_set_homogeneous(m_item
, false);
282 if (GetToolBar()->HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
284 box
= gtk_vbox_new(false, 0);
285 arrow
= gtk_arrow_new(GTK_ARROW_RIGHT
, GTK_SHADOW_NONE
);
289 box
= gtk_hbox_new(false, 0);
290 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
292 GtkWidget
* tool_button
= GTK_BIN(m_item
)->child
;
293 gtk_widget_reparent(tool_button
, box
);
294 GtkWidget
* arrow_button
= gtk_toggle_button_new();
295 gtk_button_set_relief(GTK_BUTTON(arrow_button
),
296 gtk_tool_item_get_relief_style(GTK_TOOL_ITEM(m_item
)));
297 gtk_container_add(GTK_CONTAINER(arrow_button
), arrow
);
298 gtk_container_add(GTK_CONTAINER(box
), arrow_button
);
299 gtk_widget_show_all(box
);
300 gtk_container_add(GTK_CONTAINER(m_item
), box
);
302 g_signal_connect(arrow_button
, "toggled", G_CALLBACK(arrow_toggled
), this);
303 g_signal_connect(arrow_button
, "button_press_event",
304 G_CALLBACK(arrow_button_press_event
), this);
307 void wxToolBarTool::ShowDropdown(GtkToggleButton
* button
)
309 wxToolBarBase
* toolbar
= GetToolBar();
310 wxCommandEvent
event(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, GetId());
311 if (!toolbar
->HandleWindowEvent(event
))
313 wxMenu
* menu
= GetDropdownMenu();
316 const GtkAllocation
& alloc
= GTK_WIDGET(button
)->allocation
;
319 if (toolbar
->HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
323 toolbar
->PopupMenu(menu
, x
, y
);
328 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
329 const wxString
& text
,
330 const wxBitmap
& bitmap1
,
331 const wxBitmap
& bitmap2
,
333 wxObject
*clientData
,
334 const wxString
& shortHelpString
,
335 const wxString
& longHelpString
)
337 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
338 clientData
, shortHelpString
, longHelpString
);
342 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
344 return new wxToolBarTool(this, control
, label
);
347 //-----------------------------------------------------------------------------
348 // wxToolBar construction
349 //-----------------------------------------------------------------------------
351 void wxToolBar::Init()
353 m_toolbar
= (GtkToolbar
*)NULL
;
357 wxToolBar::~wxToolBar()
361 gtk_object_destroy(GTK_OBJECT(m_tooltips
));
362 g_object_unref(m_tooltips
);
366 bool wxToolBar::Create( wxWindow
*parent
,
371 const wxString
& name
)
373 m_insertCallback
= wxInsertChildInToolBar
;
375 if ( !PreCreation( parent
, pos
, size
) ||
376 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
378 wxFAIL_MSG( wxT("wxToolBar creation failed") );
385 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
386 m_tooltips
= gtk_tooltips_new();
387 g_object_ref(m_tooltips
);
388 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 gtk_container_add(GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
));
409 gtk_widget_show(GTK_WIDGET(m_toolbar
));
411 m_parent
->DoAddChild( this );
415 g_signal_connect_after(m_toolbar
, "size_request",
416 G_CALLBACK(size_request
), this);
421 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
423 return GTK_WIDGET(m_toolbar
)->window
;
426 void wxToolBar::GtkSetStyle()
428 GtkOrientation orient
= GTK_ORIENTATION_HORIZONTAL
;
429 if (HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
430 orient
= GTK_ORIENTATION_VERTICAL
;
432 GtkToolbarStyle style
= GTK_TOOLBAR_ICONS
;
433 if (HasFlag(wxTB_NOICONS
))
434 style
= GTK_TOOLBAR_TEXT
;
435 else if (HasFlag(wxTB_TEXT
))
437 style
= GTK_TOOLBAR_BOTH
;
438 if (HasFlag(wxTB_HORZ_LAYOUT
))
439 style
= GTK_TOOLBAR_BOTH_HORIZ
;
442 gtk_toolbar_set_orientation(m_toolbar
, orient
);
443 gtk_toolbar_set_style(m_toolbar
, style
);
446 void wxToolBar::SetWindowStyleFlag( long style
)
448 wxToolBarBase::SetWindowStyleFlag(style
);
454 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
456 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
459 switch ( tool
->GetStyle() )
461 case wxTOOL_STYLE_BUTTON
:
462 switch (tool
->GetKind())
465 tool
->m_item
= gtk_toggle_tool_button_new();
466 g_signal_connect(tool
->m_item
, "toggled",
467 G_CALLBACK(item_toggled
), tool
);
470 radioGroup
= GetRadioGroup(pos
);
473 // this is the first button in the radio button group,
474 // it will be toggled automatically by GTK so bring the
475 // internal flag in sync
478 tool
->m_item
= gtk_radio_tool_button_new(radioGroup
);
479 g_signal_connect(tool
->m_item
, "toggled",
480 G_CALLBACK(item_toggled
), tool
);
483 wxFAIL_MSG("unknown toolbar child type");
485 case wxITEM_DROPDOWN
:
487 tool
->m_item
= gtk_tool_button_new(NULL
, "");
488 g_signal_connect(tool
->m_item
, "clicked",
489 G_CALLBACK(item_clicked
), tool
);
492 if (!HasFlag(wxTB_NOICONS
))
494 GtkWidget
* image
= gtk_image_new();
495 gtk_tool_button_set_icon_widget(
496 GTK_TOOL_BUTTON(tool
->m_item
), image
);
498 gtk_widget_show(image
);
499 g_signal_connect(image
, "expose_event",
500 G_CALLBACK(image_expose_event
), tool
);
502 if (!tool
->GetLabel().empty())
504 gtk_tool_button_set_label(
505 GTK_TOOL_BUTTON(tool
->m_item
), wxGTK_CONV(tool
->GetLabel()));
506 // needed for labels in horizontal toolbar with wxTB_HORZ_LAYOUT
507 gtk_tool_item_set_is_important(tool
->m_item
, true);
509 if (!HasFlag(wxTB_NO_TOOLTIPS
) && !tool
->GetShortHelp().empty())
511 gtk_tool_item_set_tooltip(tool
->m_item
,
512 m_tooltips
, wxGTK_CONV(tool
->GetShortHelp()), "");
514 g_signal_connect(GTK_BIN(tool
->m_item
)->child
, "button_press_event",
515 G_CALLBACK(button_press_event
), tool
);
516 g_signal_connect(tool
->m_item
, "enter_notify_event",
517 G_CALLBACK(enter_notify_event
), tool
);
518 g_signal_connect(tool
->m_item
, "leave_notify_event",
519 G_CALLBACK(enter_notify_event
), tool
);
521 if (tool
->GetKind() == wxITEM_DROPDOWN
)
522 tool
->CreateDropDown();
525 case wxTOOL_STYLE_SEPARATOR
:
526 tool
->m_item
= gtk_separator_tool_item_new();
529 case wxTOOL_STYLE_CONTROL
:
530 GtkWidget
* const align
= gtk_alignment_new(0.5, 0.5, 0, 0);
531 gtk_widget_show(align
);
532 wxWindow
* control
= tool
->GetControl();
533 gtk_container_add(GTK_CONTAINER(align
), control
->m_widget
);
534 tool
->m_item
= gtk_tool_item_new();
535 gtk_container_add(GTK_CONTAINER(tool
->m_item
), align
);
536 // Inserted items "slide" into place using an animated effect that
537 // causes multiple size events on the item. Must set size request
538 // to keep item size from getting permanently set too small by the
539 // first of these size events.
540 const wxSize size
= control
->GetSize();
541 gtk_widget_set_size_request(control
->m_widget
, size
.x
, size
.y
);
544 gtk_widget_show(GTK_WIDGET(tool
->m_item
));
545 gtk_toolbar_insert(m_toolbar
, tool
->m_item
, int(pos
));
547 InvalidateBestSize();
552 bool wxToolBar::DoDeleteTool(size_t /* pos */, wxToolBarToolBase
* toolBase
)
554 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
556 if (tool
->GetStyle() == wxTOOL_STYLE_CONTROL
)
558 // don't destroy the control here as we can be called from
559 // RemoveTool() and then we need to keep the control alive;
560 // while if we're called from DeleteTool() the control will
561 // be destroyed when wxToolBarToolBase itself is deleted
562 GtkWidget
* widget
= tool
->GetControl()->m_widget
;
563 gtk_container_remove(GTK_CONTAINER(widget
->parent
), widget
);
565 gtk_object_destroy(GTK_OBJECT(tool
->m_item
));
568 InvalidateBestSize();
572 GSList
* wxToolBar::GetRadioGroup(size_t pos
)
574 GSList
* radioGroup
= NULL
;
575 GtkToolItem
* item
= NULL
;
578 item
= gtk_toolbar_get_nth_item(m_toolbar
, int(pos
) - 1);
579 if (!GTK_IS_RADIO_TOOL_BUTTON(item
))
582 if (item
== NULL
&& pos
< m_tools
.size())
584 item
= gtk_toolbar_get_nth_item(m_toolbar
, int(pos
));
585 if (!GTK_IS_RADIO_TOOL_BUTTON(item
))
589 radioGroup
= gtk_radio_tool_button_get_group((GtkRadioToolButton
*)item
);
593 // ----------------------------------------------------------------------------
594 // wxToolBar tools state
595 // ----------------------------------------------------------------------------
597 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
599 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
602 gtk_widget_set_sensitive(GTK_WIDGET(tool
->m_item
), enable
);
605 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
607 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
611 g_signal_handlers_block_by_func(tool
->m_item
, (void*)item_toggled
, tool
);
613 gtk_toggle_tool_button_set_active(
614 GTK_TOGGLE_TOOL_BUTTON(tool
->m_item
), toggle
);
616 g_signal_handlers_unblock_by_func(tool
->m_item
, (void*)item_toggled
, tool
);
620 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
621 bool WXUNUSED(toggle
))
623 // VZ: absolutely no idea about how to do it
624 wxFAIL_MSG( _T("not implemented") );
627 // ----------------------------------------------------------------------------
628 // wxToolBar geometry
629 // ----------------------------------------------------------------------------
631 wxSize
wxToolBar::DoGetBestSize() const
633 // Unfortunately, if overflow arrow is enabled GtkToolbar only reports size
634 // of arrow. To get the real size, the arrow is temporarily disabled here.
635 // This is gross, since it will cause a queue_resize, and could potentially
636 // lead to an infinite loop. But there seems to be no alternative, short of
637 // disabling the arrow entirely.
638 gtk_toolbar_set_show_arrow(m_toolbar
, false);
639 const wxSize size
= wxToolBarBase::DoGetBestSize();
640 gtk_toolbar_set_show_arrow(m_toolbar
, true);
644 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
645 wxCoord
WXUNUSED(y
)) const
647 // VZ: GTK+ doesn't seem to have such thing
648 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
650 return (wxToolBarToolBase
*)NULL
;
653 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
655 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
659 (void)tool
->SetShortHelp(helpString
);
662 gtk_tool_item_set_tooltip(tool
->m_item
,
663 m_tooltips
, wxGTK_CONV(helpString
), "");
668 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
670 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
673 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
675 tool
->SetNormalBitmap(bitmap
);
680 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
682 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
685 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
687 tool
->SetDisabledBitmap(bitmap
);
691 // ----------------------------------------------------------------------------
692 // wxToolBar idle handling
693 // ----------------------------------------------------------------------------
695 void wxToolBar::OnInternalIdle()
697 // Check if we have to show window now
698 if (GtkShowFromOnIdle()) return;
700 wxCursor cursor
= m_cursor
;
701 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
705 /* I now set the cursor the anew in every OnInternalIdle call
706 as setting the cursor in a parent window also effects the
707 windows above so that checking for the current cursor is
710 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
712 /* if the toolbar is dockable, then m_widget stands for the
713 GtkHandleBox widget, which uses its own window so that we
714 can set the cursor for it. if the toolbar is not dockable,
715 m_widget comes from m_toolbar which uses its parent's
716 window ("windowless windows") and thus we cannot set the
718 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
721 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
724 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
725 node
= node
->GetNext();
729 GdkWindow
* window
= GTK_WIDGET(tool
->m_item
)->window
;
733 gdk_window_set_cursor( window
, cursor
.GetCursor() );
739 if (wxUpdateUIEvent::CanUpdate(this) && IsShownOnScreen())
740 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
744 // ----------------------------------------------------------------------------
748 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
750 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
753 #endif // wxUSE_TOOLBAR_NATIVE