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 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #if wxUSE_TOOLBAR_NATIVE
24 #include "wx/toolbar.h"
30 // FIXME: Use GtkImage instead of GtkPixmap. Use the new toolbar API for when gtk runtime is new enough?
31 // Beware that the new and old toolbar API may not be mixed in usage.
32 #include <gtk/gtkversion.h>
33 #ifdef GTK_DISABLE_DEPRECATED
34 #undef GTK_DISABLE_DEPRECATED
37 #include "wx/gtk/private.h"
42 static const char *arrow_down_xpm
[] = {
43 /* columns rows colors chars-per-pixel */
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
63 extern bool g_blockEventsOnDrag
;
64 extern wxCursor g_globalCursor
;
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // translate wxWidgets toolbar style flags to GTK orientation and style
71 static void GetGtkStyle(long style
,
72 GtkOrientation
*orient
, GtkToolbarStyle
*gtkStyle
)
74 *orient
= ( style
& wxTB_LEFT
|| style
& wxTB_RIGHT
) ? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
;
77 if ( style
& wxTB_TEXT
)
79 *gtkStyle
= style
& wxTB_NOICONS
82 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
85 else // no text, hence we must have the icons or what would we show?
87 *gtkStyle
= GTK_TOOLBAR_ICONS
;
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 class wxToolBarTool
: public wxToolBarToolBase
98 wxToolBarTool(wxToolBar
*tbar
,
100 const wxString
& label
,
101 const wxBitmap
& bitmap1
,
102 const wxBitmap
& bitmap2
,
104 wxObject
*clientData
,
105 const wxString
& shortHelpString
,
106 const wxString
& longHelpString
)
107 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
108 clientData
, shortHelpString
, longHelpString
)
113 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
114 : wxToolBarToolBase(tbar
, control
, label
)
119 // is this a radio button?
121 // unlike GetKind(), can be called for any kind of tools, not just buttons
122 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
124 // this is only called for the normal buttons, i.e. not separators nor
126 GtkToolbarChildType
GetGtkChildType() const
131 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
134 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
137 wxFAIL_MSG( _T("unknown toolbar child type") );
141 return GTK_TOOLBAR_CHILD_BUTTON
;
145 void SetImage(const wxBitmap
& bitmap
)
149 // setting from pixmap doesn't seem to work right, but pixbuf works well
150 gtk_image_set_from_pixbuf((GtkImage
*)m_image
, bitmap
.GetPixbuf());
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
167 // ============================================================================
169 // ============================================================================
171 //-----------------------------------------------------------------------------
172 // "clicked" (internal from gtk_toolbar)
173 //-----------------------------------------------------------------------------
176 static void gtk_toolbar_callback( GtkWidget
*widget
,
177 wxToolBarTool
*tool
)
179 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
181 if (tbar
->m_blockEvent
) return;
183 if (g_blockEventsOnDrag
) return;
184 if (!tool
->IsEnabled()) return;
186 if (tool
->CanBeToggled())
188 if (tool
->IsRadio() &&
189 gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(widget
)) &&
192 // pressed an already pressed radio button
198 tool
->SetImage(tool
->GetBitmap());
200 if ( tool
->IsRadio() && !tool
->IsToggled() )
202 // radio button went up, don't report this as a wxWin event
207 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
212 tool
->SetImage(tool
->GetBitmap());
217 //-----------------------------------------------------------------------------
219 //-----------------------------------------------------------------------------
221 static gboolean
gtk_toolbar_tool_rclick_callback(GtkWidget
*WXUNUSED(widget
),
222 GdkEventButton
*event
,
223 wxToolBarToolBase
*tool
)
225 if (event
->button
!= 3)
228 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
230 if (tbar
->m_blockEvent
) return TRUE
;
232 if (g_blockEventsOnDrag
) return TRUE
;
233 if (!tool
->IsEnabled()) return TRUE
;
235 tbar
->OnRightClick( tool
->GetId(), (int)event
->x
, (int)event
->y
);
241 //-----------------------------------------------------------------------------
242 // "enter_notify_event" / "leave_notify_event" from dropdown
243 //-----------------------------------------------------------------------------
246 static gint
gtk_toolbar_buddy_enter_callback( GtkWidget
*WXUNUSED(widget
),
247 GdkEventCrossing
*WXUNUSED(gdk_event
),
250 guint8 state
= GTK_WIDGET_STATE( tool
);
251 state
|= GTK_STATE_PRELIGHT
;
252 gtk_widget_set_state( tool
, (GtkStateType
) state
);
256 static gint
gtk_toolbar_buddy_leave_callback( GtkWidget
*WXUNUSED(widget
),
257 GdkEventCrossing
*WXUNUSED(gdk_event
),
260 guint8 state
= GTK_WIDGET_STATE( tool
);
261 state
&= ~GTK_STATE_PRELIGHT
;
262 gtk_widget_set_state( tool
, (GtkStateType
) state
);
267 //-----------------------------------------------------------------------------
268 // "left-click" on dropdown
269 //-----------------------------------------------------------------------------
273 static void gtk_pop_tb_hide_callback( GtkWidget
*WXUNUSED(menu
), GtkToggleButton
*button
)
275 gtk_toggle_button_set_active( button
, FALSE
);
278 static gboolean
gtk_toolbar_dropdown_lclick_callback(GtkWidget
*widget
,
279 GdkEventButton
*event
,
280 wxToolBarToolBase
*tool
)
282 if (event
->button
!= 1)
285 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
287 if (tbar
->m_blockEvent
) return FALSE
;
289 if (g_blockEventsOnDrag
) return FALSE
;
290 if (!tool
->IsEnabled()) return FALSE
;
292 wxCommandEvent
evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, tool
->GetId() );
293 if ( tbar
->GetEventHandler()->ProcessEvent(evt
) )
298 wxMenu
* const menu
= tool
->GetDropdownMenu();
303 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(widget
), TRUE
);
305 g_signal_connect (menu
->m_menu
, "hide",
306 G_CALLBACK (gtk_pop_tb_hide_callback
),
309 tbar
->PopupMenu( menu
, widget
->allocation
.x
,
310 widget
->allocation
.y
+ widget
->allocation
.height
);
317 //-----------------------------------------------------------------------------
318 // "enter_notify_event" / "leave_notify_event"
319 //-----------------------------------------------------------------------------
322 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
323 GdkEventCrossing
*gdk_event
,
324 wxToolBarTool
*tool
)
326 if (g_blockEventsOnDrag
) return TRUE
;
328 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
331 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
332 tb
->OnMouseEnter( tool
->GetId() );
334 tb
->OnMouseEnter( -1 );
342 void gtktoolwidget_size_callback( GtkWidget
*widget
,
343 GtkAllocation
*alloc
,
346 // this shouldn't happen...
347 if (win
->GetParent()->m_wxwindow
) return;
349 wxSize size
= win
->GetEffectiveMinSize();
350 if (size
.y
!= alloc
->height
)
352 GtkAllocation alloc2
;
354 alloc2
.y
= (alloc
->height
- size
.y
+ 3) / 2;
355 alloc2
.width
= alloc
->width
;
356 alloc2
.height
= size
.y
;
357 gtk_widget_size_allocate( widget
, &alloc2
);
361 //-----------------------------------------------------------------------------
362 // InsertChild callback for wxToolBar
363 //-----------------------------------------------------------------------------
365 static void wxInsertChildInToolBar( wxWindow
* WXUNUSED(parent
),
368 // Child widget will be inserted into GtkToolbar by DoInsertTool. Ref it
369 // here so reparenting into wxToolBar doesn't delete it.
370 g_object_ref(child
->m_widget
);
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
377 void wxToolBarTool::Init()
383 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
384 const wxString
& text
,
385 const wxBitmap
& bitmap1
,
386 const wxBitmap
& bitmap2
,
388 wxObject
*clientData
,
389 const wxString
& shortHelpString
,
390 const wxString
& longHelpString
)
392 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
393 clientData
, shortHelpString
, longHelpString
);
397 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
399 return new wxToolBarTool(this, control
, label
);
402 //-----------------------------------------------------------------------------
403 // wxToolBar construction
404 //-----------------------------------------------------------------------------
406 void wxToolBar::Init()
408 m_toolbar
= (GtkToolbar
*)NULL
;
409 m_blockEvent
= false;
411 m_defaultHeight
= 32;
414 wxToolBar::~wxToolBar()
418 bool wxToolBar::Create( wxWindow
*parent
,
423 const wxString
& name
)
425 m_insertCallback
= wxInsertChildInToolBar
;
427 if ( !PreCreation( parent
, pos
, size
) ||
428 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
430 wxFAIL_MSG( wxT("wxToolBar creation failed") );
437 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
440 // Doesn't work this way.
441 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
442 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
444 SetToolSeparation(7);
446 if (style
& wxTB_DOCKABLE
)
448 m_widget
= gtk_handle_box_new();
449 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
450 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
452 if (style
& wxTB_FLAT
)
453 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
457 m_widget
= gtk_event_box_new();
458 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
459 ConnectWidget( m_widget
);
460 gtk_widget_show(GTK_WIDGET(m_toolbar
));
463 // FIXME: there is no such function for toolbars in 2.0
465 if (style
& wxTB_FLAT
)
466 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
469 m_parent
->DoAddChild( this );
476 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& windows
) const
478 return GTK_WIDGET(m_toolbar
)->window
;
481 void wxToolBar::GtkSetStyle()
483 GtkOrientation orient
;
484 GtkToolbarStyle style
;
485 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
487 gtk_toolbar_set_orientation(m_toolbar
, orient
);
488 gtk_toolbar_set_style(m_toolbar
, style
);
489 gtk_toolbar_set_tooltips(m_toolbar
, !(style
& wxTB_NO_TOOLTIPS
));
492 void wxToolBar::SetWindowStyleFlag( long style
)
494 wxToolBarBase::SetWindowStyleFlag(style
);
500 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
502 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
504 if ( tool
->IsButton() )
506 if ( !HasFlag(wxTB_NOICONS
) )
508 wxBitmap bitmap
= tool
->GetNormalBitmap();
510 wxCHECK_MSG( bitmap
.Ok(), false,
511 wxT("invalid bitmap for wxToolBar icon") );
513 tool
->m_image
= gtk_image_new();
514 tool
->SetImage(bitmap
);
516 gtk_misc_set_alignment((GtkMisc
*)tool
->m_image
, 0.5, 0.5);
524 for (i
= 0; i
< pos
; i
++)
529 // if we have a dropdown menu, we use 2 GTK tools
531 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item( i
);
532 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
533 if (tool
->IsButton() && (tool
->GetKind() == wxITEM_DROPDOWN
))
539 switch ( tool
->GetStyle() )
541 case wxTOOL_STYLE_BUTTON
:
542 // for a radio button we need the widget which starts the radio
543 // group it belongs to, i.e. the first radio button immediately
544 // preceding this one
546 GtkWidget
*widget
= NULL
;
548 if ( tool
->IsRadio() )
550 wxToolBarToolsList::compatibility_iterator node
551 = wxToolBarToolsList::compatibility_iterator();
553 node
= m_tools
.Item(pos
- 1);
557 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
558 if ( !toolNext
->IsRadio() )
561 widget
= toolNext
->m_item
;
563 node
= node
->GetPrevious();
568 // this is the first button in the radio button group,
569 // it will be toggled automatically by GTK so bring the
570 // internal flag in sync
575 tool
->m_item
= gtk_toolbar_insert_element
578 tool
->GetGtkChildType(),
580 tool
->GetLabel().empty()
582 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
583 tool
->GetShortHelp().empty()
585 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
586 "", // tooltip_private_text (?)
588 (GtkSignalFunc
)gtk_toolbar_callback
,
593 wxCHECK_MSG(tool
->m_item
!= NULL
, false, _T("gtk_toolbar_insert_element() failed"));
595 g_signal_connect (tool
->m_item
, "enter_notify_event",
596 G_CALLBACK (gtk_toolbar_tool_callback
),
598 g_signal_connect (tool
->m_item
, "leave_notify_event",
599 G_CALLBACK (gtk_toolbar_tool_callback
),
601 g_signal_connect(tool
->m_item
, "button-press-event",
602 G_CALLBACK (gtk_toolbar_tool_rclick_callback
),
605 if (tool
->GetKind() == wxITEM_DROPDOWN
)
607 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_xpm_data( arrow_down_xpm
);
608 GtkWidget
*dropdown
= gtk_toggle_button_new();
609 GtkWidget
*image
= gtk_image_new_from_pixbuf( pixbuf
);
610 gtk_widget_show( image
);
611 gtk_container_add( GTK_CONTAINER(dropdown
), image
);
613 if (GetWindowStyle() & wxTB_FLAT
)
614 gtk_button_set_relief( GTK_BUTTON(dropdown
), GTK_RELIEF_NONE
);
615 GTK_WIDGET_UNSET_FLAGS (dropdown
, GTK_CAN_FOCUS
);
616 gtk_widget_show( dropdown
);
618 g_signal_connect (dropdown
, "enter_notify_event",
619 G_CALLBACK (gtk_toolbar_buddy_enter_callback
),
621 g_signal_connect (dropdown
, "leave_notify_event",
622 G_CALLBACK (gtk_toolbar_buddy_leave_callback
),
624 g_signal_connect(dropdown
, "button-press-event",
625 G_CALLBACK (gtk_toolbar_dropdown_lclick_callback
),
629 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(tool
->m_item
) )->size_request
)
630 (tool
->m_item
, &req
);
631 gtk_widget_set_size_request( dropdown
, -1, req
.height
);
633 gtk_toolbar_insert_widget(
644 case wxTOOL_STYLE_SEPARATOR
:
645 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
650 case wxTOOL_STYLE_CONTROL
:
651 gtk_toolbar_insert_widget(
653 tool
->GetControl()->m_widget
,
658 // release reference obtained by wxInsertChildInToolBar
659 g_object_unref(tool
->GetControl()->m_widget
);
661 // connect after in order to correct size_allocate events
662 g_signal_connect_after (tool
->GetControl()->m_widget
, "size_allocate",
663 G_CALLBACK (gtktoolwidget_size_callback
), tool
->GetControl());
669 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
671 m_width
= req
.width
+ m_xMargin
;
672 m_height
= req
.height
+ 2*m_yMargin
;
673 InvalidateBestSize();
678 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
680 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
682 switch ( tool
->GetStyle() )
684 case wxTOOL_STYLE_CONTROL
:
685 tool
->GetControl()->Destroy();
688 case wxTOOL_STYLE_BUTTON
:
689 gtk_widget_destroy( tool
->m_item
);
692 case wxTOOL_STYLE_SEPARATOR
:
693 gtk_toolbar_remove_space( m_toolbar
, pos
);
697 InvalidateBestSize();
701 // ----------------------------------------------------------------------------
702 // wxToolBar tools state
703 // ----------------------------------------------------------------------------
705 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
707 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
711 gtk_widget_set_sensitive( tool
->m_item
, enable
);
715 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
717 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
719 GtkWidget
*item
= tool
->m_item
;
720 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
722 tool
->SetImage(tool
->GetBitmap());
726 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item
), toggle
);
728 m_blockEvent
= false;
732 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
733 bool WXUNUSED(toggle
))
735 // VZ: absolutely no idea about how to do it
736 wxFAIL_MSG( _T("not implemented") );
739 // ----------------------------------------------------------------------------
740 // wxToolBar geometry
741 // ----------------------------------------------------------------------------
743 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
744 wxCoord
WXUNUSED(y
)) const
746 // VZ: GTK+ doesn't seem to have such thing
747 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
749 return (wxToolBarToolBase
*)NULL
;
752 void wxToolBar::SetMargins( int x
, int y
)
754 wxCHECK_RET( GetToolsCount() == 0,
755 wxT("wxToolBar::SetMargins must be called before adding tools.") );
761 void wxToolBar::SetToolSeparation( int separation
)
763 // FIXME: this function disappeared
765 gtk_toolbar_set_space_size( m_toolbar
, separation
);
768 m_toolSeparation
= separation
;
771 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
773 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
777 (void)tool
->SetShortHelp(helpString
);
778 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
779 wxGTK_CONV( helpString
), "");
783 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
785 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
788 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
790 tool
->SetNormalBitmap(bitmap
);
791 tool
->SetImage(tool
->GetBitmap());
795 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
797 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
800 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
802 tool
->SetDisabledBitmap(bitmap
);
803 tool
->SetImage(tool
->GetBitmap());
807 // ----------------------------------------------------------------------------
808 // wxToolBar idle handling
809 // ----------------------------------------------------------------------------
811 void wxToolBar::OnInternalIdle()
813 // Check if we have to show window now
814 if (GtkShowFromOnIdle()) return;
816 wxCursor cursor
= m_cursor
;
817 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
821 /* I now set the cursor the anew in every OnInternalIdle call
822 as setting the cursor in a parent window also effects the
823 windows above so that checking for the current cursor is
826 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
828 /* if the toolbar is dockable, then m_widget stands for the
829 GtkHandleBox widget, which uses its own window so that we
830 can set the cursor for it. if the toolbar is not dockable,
831 m_widget comes from m_toolbar which uses its parent's
832 window ("windowless windows") and thus we cannot set the
834 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
837 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
840 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
841 node
= node
->GetNext();
843 GtkWidget
*item
= tool
->m_item
;
846 GdkWindow
*window
= item
->window
;
850 gdk_window_set_cursor( window
, cursor
.GetCursor() );
856 if (wxUpdateUIEvent::CanUpdate(this))
857 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
861 // ----------------------------------------------------------------------------
865 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
867 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
870 #endif // wxUSE_TOOLBAR_NATIVE