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 // "size_request" from m_toolbar
173 //-----------------------------------------------------------------------------
177 size_request(GtkWidget
*, GtkRequisition
* req
, wxToolBar
* win
)
179 const wxSize margins
= win
->GetMargins();
180 req
->width
+= margins
.x
;
181 req
->height
+= 2 * margins
.y
;
185 //-----------------------------------------------------------------------------
186 // "expose_event" from GtkImage inside m_item
187 //-----------------------------------------------------------------------------
191 image_expose_event(GtkWidget
* widget
, GdkEventExpose
*, wxToolBarTool
* tool
)
193 const wxBitmap
& bitmap
= tool
->GetDisabledBitmap();
194 if (tool
->IsEnabled() || !bitmap
.IsOk())
197 // draw disabled bitmap ourselves, GtkImage has no way to specify it
198 const GtkAllocation
& alloc
= widget
->allocation
;
200 widget
->window
, widget
->style
->black_gc
, bitmap
.GetPixbuf(),
202 alloc
.x
+ (alloc
.width
- widget
->requisition
.width
) / 2,
203 alloc
.y
+ (alloc
.height
- widget
->requisition
.height
) / 2,
204 -1, -1, GDK_RGB_DITHER_NORMAL
, 0, 0);
209 //-----------------------------------------------------------------------------
210 // "toggled" from dropdown menu button
211 //-----------------------------------------------------------------------------
214 static void arrow_toggled(GtkToggleButton
* button
, wxToolBarTool
* tool
)
216 if (gtk_toggle_button_get_active(button
))
218 tool
->ShowDropdown(button
);
219 gtk_toggle_button_set_active(button
, false);
224 //-----------------------------------------------------------------------------
225 // "button_press_event" from dropdown menu button
226 //-----------------------------------------------------------------------------
230 arrow_button_press_event(GtkToggleButton
* button
, GdkEventButton
* event
, wxToolBarTool
* tool
)
232 if (event
->button
== 1)
234 g_signal_handlers_block_by_func(button
, (void*)arrow_toggled
, tool
);
235 gtk_toggle_button_set_active(button
, true);
236 tool
->ShowDropdown(button
);
237 gtk_toggle_button_set_active(button
, false);
238 g_signal_handlers_unblock_by_func(button
, (void*)arrow_toggled
, tool
);
245 void wxToolBar::AddChildGTK(wxWindowGTK
* child
)
247 GtkWidget
* align
= gtk_alignment_new(0.5, 0.5, 0, 0);
248 gtk_widget_show(align
);
249 gtk_container_add(GTK_CONTAINER(align
), child
->m_widget
);
250 GtkToolItem
* item
= gtk_tool_item_new();
251 gtk_container_add(GTK_CONTAINER(item
), align
);
252 // position will be corrected in DoInsertTool if necessary
253 gtk_toolbar_insert(GTK_TOOLBAR(GTK_BIN(m_widget
)->child
), item
, -1);
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 void wxToolBarTool::SetImage()
262 const wxBitmap
& bitmap
= GetNormalBitmap();
263 wxCHECK_RET(bitmap
.IsOk(), "invalid bitmap for wxToolBar icon");
265 GtkWidget
* image
= gtk_tool_button_get_icon_widget(GTK_TOOL_BUTTON(m_item
));
266 // always use pixbuf, because pixmap mask does not
267 // work with disabled images in some themes
268 gtk_image_set_from_pixbuf(GTK_IMAGE(image
), bitmap
.GetPixbuf());
271 // helper to create a dropdown menu item
272 void wxToolBarTool::CreateDropDown()
274 gtk_tool_item_set_homogeneous(m_item
, false);
277 if (GetToolBar()->HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
279 box
= gtk_vbox_new(false, 0);
280 arrow
= gtk_arrow_new(GTK_ARROW_RIGHT
, GTK_SHADOW_NONE
);
284 box
= gtk_hbox_new(false, 0);
285 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
287 GtkWidget
* tool_button
= GTK_BIN(m_item
)->child
;
288 gtk_widget_reparent(tool_button
, box
);
289 GtkWidget
* arrow_button
= gtk_toggle_button_new();
290 gtk_button_set_relief(GTK_BUTTON(arrow_button
),
291 gtk_tool_item_get_relief_style(GTK_TOOL_ITEM(m_item
)));
292 gtk_container_add(GTK_CONTAINER(arrow_button
), arrow
);
293 gtk_container_add(GTK_CONTAINER(box
), arrow_button
);
294 gtk_widget_show_all(box
);
295 gtk_container_add(GTK_CONTAINER(m_item
), box
);
297 g_signal_connect(arrow_button
, "toggled", G_CALLBACK(arrow_toggled
), this);
298 g_signal_connect(arrow_button
, "button_press_event",
299 G_CALLBACK(arrow_button_press_event
), this);
302 void wxToolBarTool::ShowDropdown(GtkToggleButton
* button
)
304 wxToolBarBase
* toolbar
= GetToolBar();
305 wxCommandEvent
event(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, GetId());
306 if (!toolbar
->HandleWindowEvent(event
))
308 wxMenu
* menu
= GetDropdownMenu();
311 const GtkAllocation
& alloc
= GTK_WIDGET(button
)->allocation
;
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()
356 gtk_object_destroy(GTK_OBJECT(m_tooltips
));
357 g_object_unref(m_tooltips
);
361 bool wxToolBar::Create( wxWindow
*parent
,
366 const wxString
& name
)
368 if ( !PreCreation( parent
, pos
, size
) ||
369 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
371 wxFAIL_MSG( wxT("wxToolBar creation failed") );
378 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
379 m_tooltips
= gtk_tooltips_new();
380 g_object_ref(m_tooltips
);
381 gtk_object_sink(GTK_OBJECT(m_tooltips
));
384 if (style
& wxTB_DOCKABLE
)
386 m_widget
= gtk_handle_box_new();
388 g_signal_connect(m_widget
, "child_detached",
389 G_CALLBACK(child_detached
), NULL
);
390 g_signal_connect(m_widget
, "child_attached",
391 G_CALLBACK(child_attached
), NULL
);
393 if (style
& wxTB_FLAT
)
394 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
398 m_widget
= gtk_event_box_new();
399 ConnectWidget( m_widget
);
401 g_object_ref(m_widget
);
402 gtk_container_add(GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
));
403 gtk_widget_show(GTK_WIDGET(m_toolbar
));
405 m_parent
->DoAddChild( this );
409 g_signal_connect_after(m_toolbar
, "size_request",
410 G_CALLBACK(size_request
), this);
415 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
417 return GTK_WIDGET(m_toolbar
)->window
;
420 void wxToolBar::GtkSetStyle()
422 GtkOrientation orient
= GTK_ORIENTATION_HORIZONTAL
;
423 if (HasFlag(wxTB_LEFT
| wxTB_RIGHT
))
424 orient
= GTK_ORIENTATION_VERTICAL
;
426 GtkToolbarStyle style
= GTK_TOOLBAR_ICONS
;
427 if (HasFlag(wxTB_NOICONS
))
428 style
= GTK_TOOLBAR_TEXT
;
429 else if (HasFlag(wxTB_TEXT
))
431 style
= GTK_TOOLBAR_BOTH
;
432 if (HasFlag(wxTB_HORZ_LAYOUT
))
433 style
= GTK_TOOLBAR_BOTH_HORIZ
;
436 gtk_toolbar_set_orientation(m_toolbar
, orient
);
437 gtk_toolbar_set_style(m_toolbar
, style
);
440 void wxToolBar::SetWindowStyleFlag( long style
)
442 wxToolBarBase::SetWindowStyleFlag(style
);
448 bool wxToolBar::Realize()
450 if ( !wxToolBarBase::Realize() )
453 // bring the initial state of all the toolbar items in line with the
454 // internal state if the latter was changed by calling wxToolBarTool::
455 // Enable(): this works under MSW, where the toolbar items are only created
456 // in Realize() which uses the internal state to determine the initial
457 // button state, so make it work under GTK too
458 for ( wxToolBarToolsList::const_iterator i
= m_tools
.begin();
462 // by default the toolbar items are enabled and not toggled, so we only
463 // have to do something if their internal state doesn't correspond to
465 if ( !(*i
)->IsEnabled() )
466 DoEnableTool(*i
, false);
467 if ( (*i
)->IsToggled() )
468 DoToggleTool(*i
, true);
474 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
476 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
479 switch ( tool
->GetStyle() )
481 case wxTOOL_STYLE_BUTTON
:
482 switch (tool
->GetKind())
485 tool
->m_item
= gtk_toggle_tool_button_new();
486 g_signal_connect(tool
->m_item
, "toggled",
487 G_CALLBACK(item_toggled
), tool
);
490 radioGroup
= GetRadioGroup(pos
);
493 // this is the first button in the radio button group,
494 // it will be toggled automatically by GTK so bring the
495 // internal flag in sync
498 tool
->m_item
= gtk_radio_tool_button_new(radioGroup
);
499 g_signal_connect(tool
->m_item
, "toggled",
500 G_CALLBACK(item_toggled
), tool
);
503 wxFAIL_MSG("unknown toolbar child type");
505 case wxITEM_DROPDOWN
:
507 tool
->m_item
= gtk_tool_button_new(NULL
, "");
508 g_signal_connect(tool
->m_item
, "clicked",
509 G_CALLBACK(item_clicked
), tool
);
512 if (!HasFlag(wxTB_NOICONS
))
514 GtkWidget
* image
= gtk_image_new();
515 gtk_tool_button_set_icon_widget(
516 GTK_TOOL_BUTTON(tool
->m_item
), image
);
518 gtk_widget_show(image
);
519 g_signal_connect(image
, "expose_event",
520 G_CALLBACK(image_expose_event
), tool
);
522 if (!tool
->GetLabel().empty())
524 gtk_tool_button_set_label(
525 GTK_TOOL_BUTTON(tool
->m_item
), wxGTK_CONV(tool
->GetLabel()));
526 // needed for labels in horizontal toolbar with wxTB_HORZ_LAYOUT
527 gtk_tool_item_set_is_important(tool
->m_item
, true);
529 if (!HasFlag(wxTB_NO_TOOLTIPS
) && !tool
->GetShortHelp().empty())
531 gtk_tool_item_set_tooltip(tool
->m_item
,
532 m_tooltips
, wxGTK_CONV(tool
->GetShortHelp()), "");
534 g_signal_connect(GTK_BIN(tool
->m_item
)->child
, "button_press_event",
535 G_CALLBACK(button_press_event
), tool
);
536 g_signal_connect(tool
->m_item
, "enter_notify_event",
537 G_CALLBACK(enter_notify_event
), tool
);
538 g_signal_connect(tool
->m_item
, "leave_notify_event",
539 G_CALLBACK(enter_notify_event
), tool
);
541 if (tool
->GetKind() == wxITEM_DROPDOWN
)
542 tool
->CreateDropDown();
543 gtk_toolbar_insert(m_toolbar
, tool
->m_item
, int(pos
));
546 case wxTOOL_STYLE_SEPARATOR
:
547 tool
->m_item
= gtk_separator_tool_item_new();
548 if ( tool
->IsStretchable() )
550 gtk_separator_tool_item_set_draw
552 GTK_SEPARATOR_TOOL_ITEM(tool
->m_item
),
555 gtk_tool_item_set_expand(tool
->m_item
, TRUE
);
557 gtk_toolbar_insert(m_toolbar
, tool
->m_item
, int(pos
));
560 case wxTOOL_STYLE_CONTROL
:
561 wxWindow
* control
= tool
->GetControl();
562 if (control
->m_widget
->parent
== NULL
)
563 AddChildGTK(control
);
564 tool
->m_item
= GTK_TOOL_ITEM(control
->m_widget
->parent
->parent
);
565 if (gtk_toolbar_get_item_index(m_toolbar
, tool
->m_item
) != int(pos
))
567 g_object_ref(tool
->m_item
);
568 gtk_container_remove(
569 GTK_CONTAINER(m_toolbar
), GTK_WIDGET(tool
->m_item
));
570 gtk_toolbar_insert(m_toolbar
, tool
->m_item
, int(pos
));
571 g_object_unref(tool
->m_item
);
573 // Inserted items "slide" into place using an animated effect that
574 // causes multiple size events on the item. Must set size request
575 // to keep item size from getting permanently set too small by the
576 // first of these size events.
577 const wxSize size
= control
->GetSize();
578 gtk_widget_set_size_request(control
->m_widget
, size
.x
, size
.y
);
581 gtk_widget_show(GTK_WIDGET(tool
->m_item
));
583 InvalidateBestSize();
588 bool wxToolBar::DoDeleteTool(size_t /* pos */, wxToolBarToolBase
* toolBase
)
590 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
592 if (tool
->GetStyle() == wxTOOL_STYLE_CONTROL
)
594 // don't destroy the control here as we can be called from
595 // RemoveTool() and then we need to keep the control alive;
596 // while if we're called from DeleteTool() the control will
597 // be destroyed when wxToolBarToolBase itself is deleted
598 GtkWidget
* widget
= tool
->GetControl()->m_widget
;
599 gtk_container_remove(GTK_CONTAINER(widget
->parent
), widget
);
601 gtk_object_destroy(GTK_OBJECT(tool
->m_item
));
604 InvalidateBestSize();
608 GSList
* wxToolBar::GetRadioGroup(size_t pos
)
610 GSList
* radioGroup
= NULL
;
611 GtkToolItem
* item
= NULL
;
614 item
= gtk_toolbar_get_nth_item(m_toolbar
, int(pos
) - 1);
615 if (!GTK_IS_RADIO_TOOL_BUTTON(item
))
618 if (item
== NULL
&& pos
< m_tools
.size())
620 item
= gtk_toolbar_get_nth_item(m_toolbar
, int(pos
));
621 if (!GTK_IS_RADIO_TOOL_BUTTON(item
))
625 radioGroup
= gtk_radio_tool_button_get_group((GtkRadioToolButton
*)item
);
629 // ----------------------------------------------------------------------------
630 // wxToolBar tools state
631 // ----------------------------------------------------------------------------
633 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
635 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
638 gtk_widget_set_sensitive(GTK_WIDGET(tool
->m_item
), enable
);
641 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
643 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(toolBase
);
647 g_signal_handlers_block_by_func(tool
->m_item
, (void*)item_toggled
, tool
);
649 gtk_toggle_tool_button_set_active(
650 GTK_TOGGLE_TOOL_BUTTON(tool
->m_item
), toggle
);
652 g_signal_handlers_unblock_by_func(tool
->m_item
, (void*)item_toggled
, tool
);
656 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
657 bool WXUNUSED(toggle
))
659 // VZ: absolutely no idea about how to do it
660 wxFAIL_MSG( wxT("not implemented") );
663 // ----------------------------------------------------------------------------
664 // wxToolBar geometry
665 // ----------------------------------------------------------------------------
667 wxSize
wxToolBar::DoGetBestSize() const
669 // Unfortunately, if overflow arrow is enabled GtkToolbar only reports size
670 // of arrow. To get the real size, the arrow is temporarily disabled here.
671 // This is gross, since it will cause a queue_resize, and could potentially
672 // lead to an infinite loop. But there seems to be no alternative, short of
673 // disabling the arrow entirely.
674 gtk_toolbar_set_show_arrow(m_toolbar
, false);
675 const wxSize size
= wxToolBarBase::DoGetBestSize();
676 gtk_toolbar_set_show_arrow(m_toolbar
, true);
680 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
681 wxCoord
WXUNUSED(y
)) const
683 // VZ: GTK+ doesn't seem to have such thing
684 wxFAIL_MSG( wxT("wxToolBar::FindToolForPosition() not implemented") );
689 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
691 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
695 (void)tool
->SetShortHelp(helpString
);
698 gtk_tool_item_set_tooltip(tool
->m_item
,
699 m_tooltips
, wxGTK_CONV(helpString
), "");
704 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
706 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
709 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
711 tool
->SetNormalBitmap(bitmap
);
716 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
718 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
721 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
723 tool
->SetDisabledBitmap(bitmap
);
727 // ----------------------------------------------------------------------------
728 // wxToolBar idle handling
729 // ----------------------------------------------------------------------------
731 void wxToolBar::OnInternalIdle()
733 // Check if we have to show window now
734 if (GTKShowFromOnIdle()) return;
736 if (wxUpdateUIEvent::CanUpdate(this) && IsShownOnScreen())
737 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
741 // ----------------------------------------------------------------------------
745 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
747 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
750 #endif // wxUSE_TOOLBAR_NATIVE