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") );
140 case wxITEM_DROPDOWN
:
142 return GTK_TOOLBAR_CHILD_BUTTON
;
146 void SetImage(const wxBitmap
& bitmap
)
150 // setting from pixmap doesn't seem to work right, but pixbuf works well
151 gtk_image_set_from_pixbuf((GtkImage
*)m_image
, bitmap
.GetPixbuf());
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
168 // ============================================================================
170 // ============================================================================
172 //-----------------------------------------------------------------------------
173 // "clicked" (internal from gtk_toolbar)
174 //-----------------------------------------------------------------------------
177 static void gtk_toolbar_callback( GtkWidget
*widget
,
178 wxToolBarTool
*tool
)
180 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
182 if (tbar
->m_blockEvent
) return;
184 if (g_blockEventsOnDrag
) return;
185 if (!tool
->IsEnabled()) return;
187 if (tool
->CanBeToggled())
189 if (tool
->IsRadio() &&
190 gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(widget
)) &&
193 // pressed an already pressed radio button
199 tool
->SetImage(tool
->GetBitmap());
201 if ( tool
->IsRadio() && !tool
->IsToggled() )
203 // radio button went up, don't report this as a wxWin event
208 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
213 tool
->SetImage(tool
->GetBitmap());
218 //-----------------------------------------------------------------------------
220 //-----------------------------------------------------------------------------
222 static gboolean
gtk_toolbar_tool_rclick_callback(GtkWidget
*WXUNUSED(widget
),
223 GdkEventButton
*event
,
224 wxToolBarToolBase
*tool
)
226 if (event
->button
!= 3)
229 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
231 if (tbar
->m_blockEvent
) return TRUE
;
233 if (g_blockEventsOnDrag
) return TRUE
;
234 if (!tool
->IsEnabled()) return TRUE
;
236 tbar
->OnRightClick( tool
->GetId(), (int)event
->x
, (int)event
->y
);
242 //-----------------------------------------------------------------------------
243 // "enter_notify_event" / "leave_notify_event" from dropdown
244 //-----------------------------------------------------------------------------
247 static gint
gtk_toolbar_buddy_enter_callback( GtkWidget
*WXUNUSED(widget
),
248 GdkEventCrossing
*WXUNUSED(gdk_event
),
251 guint8 state
= GTK_WIDGET_STATE( tool
);
252 state
|= GTK_STATE_PRELIGHT
;
253 gtk_widget_set_state( tool
, (GtkStateType
) state
);
257 static gint
gtk_toolbar_buddy_leave_callback( GtkWidget
*WXUNUSED(widget
),
258 GdkEventCrossing
*WXUNUSED(gdk_event
),
261 guint8 state
= GTK_WIDGET_STATE( tool
);
262 state
&= ~GTK_STATE_PRELIGHT
;
263 gtk_widget_set_state( tool
, (GtkStateType
) state
);
268 //-----------------------------------------------------------------------------
269 // "left-click" on dropdown
270 //-----------------------------------------------------------------------------
274 static void gtk_pop_tb_hide_callback( GtkWidget
*WXUNUSED(menu
), GtkToggleButton
*button
)
276 gtk_toggle_button_set_active( button
, FALSE
);
279 static gboolean
gtk_toolbar_dropdown_lclick_callback(GtkWidget
*widget
,
280 GdkEventButton
*event
,
281 wxToolBarToolBase
*tool
)
283 if (event
->button
!= 1)
286 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
288 if (tbar
->m_blockEvent
) return FALSE
;
290 if (g_blockEventsOnDrag
) return FALSE
;
291 if (!tool
->IsEnabled()) return FALSE
;
293 wxCommandEvent
evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, tool
->GetId() );
294 if ( tbar
->GetEventHandler()->ProcessEvent(evt
) )
299 wxMenu
* const menu
= tool
->GetDropdownMenu();
304 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(widget
), TRUE
);
306 g_signal_connect (menu
->m_menu
, "hide",
307 G_CALLBACK (gtk_pop_tb_hide_callback
),
310 tbar
->PopupMenu( menu
, widget
->allocation
.x
,
311 widget
->allocation
.y
+ widget
->allocation
.height
);
318 //-----------------------------------------------------------------------------
319 // "enter_notify_event" / "leave_notify_event"
320 //-----------------------------------------------------------------------------
323 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
324 GdkEventCrossing
*gdk_event
,
325 wxToolBarTool
*tool
)
327 if (g_blockEventsOnDrag
) return TRUE
;
329 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
332 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
333 tb
->OnMouseEnter( tool
->GetId() );
335 tb
->OnMouseEnter( -1 );
343 void gtktoolwidget_size_callback( GtkWidget
*widget
,
344 GtkAllocation
*alloc
,
347 // this shouldn't happen...
348 if (win
->GetParent()->m_wxwindow
) return;
350 wxSize size
= win
->GetEffectiveMinSize();
351 if (size
.y
!= alloc
->height
)
353 GtkAllocation alloc2
;
355 alloc2
.y
= (alloc
->height
- size
.y
+ 3) / 2;
356 alloc2
.width
= alloc
->width
;
357 alloc2
.height
= size
.y
;
358 gtk_widget_size_allocate( widget
, &alloc2
);
362 //-----------------------------------------------------------------------------
363 // InsertChild callback for wxToolBar
364 //-----------------------------------------------------------------------------
366 static void wxInsertChildInToolBar( wxWindow
* WXUNUSED(parent
),
369 // Child widget will be inserted into GtkToolbar by DoInsertTool. Ref it
370 // here so reparenting into wxToolBar doesn't delete it.
371 g_object_ref(child
->m_widget
);
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 void wxToolBarTool::Init()
384 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
385 const wxString
& text
,
386 const wxBitmap
& bitmap1
,
387 const wxBitmap
& bitmap2
,
389 wxObject
*clientData
,
390 const wxString
& shortHelpString
,
391 const wxString
& longHelpString
)
393 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
394 clientData
, shortHelpString
, longHelpString
);
398 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
400 return new wxToolBarTool(this, control
, label
);
403 //-----------------------------------------------------------------------------
404 // wxToolBar construction
405 //-----------------------------------------------------------------------------
407 void wxToolBar::Init()
409 m_toolbar
= (GtkToolbar
*)NULL
;
410 m_blockEvent
= false;
412 m_defaultHeight
= 32;
415 wxToolBar::~wxToolBar()
419 bool wxToolBar::Create( wxWindow
*parent
,
424 const wxString
& name
)
426 m_insertCallback
= wxInsertChildInToolBar
;
428 if ( !PreCreation( parent
, pos
, size
) ||
429 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
431 wxFAIL_MSG( wxT("wxToolBar creation failed") );
438 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
441 // Doesn't work this way.
442 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
443 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
445 SetToolSeparation(7);
447 if (style
& wxTB_DOCKABLE
)
449 m_widget
= gtk_handle_box_new();
450 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
451 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
453 if (style
& wxTB_FLAT
)
454 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
458 m_widget
= gtk_event_box_new();
459 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
460 ConnectWidget( m_widget
);
461 gtk_widget_show(GTK_WIDGET(m_toolbar
));
464 // FIXME: there is no such function for toolbars in 2.0
466 if (style
& wxTB_FLAT
)
467 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
470 m_parent
->DoAddChild( this );
477 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& windows
) const
479 return GTK_WIDGET(m_toolbar
)->window
;
482 void wxToolBar::GtkSetStyle()
484 GtkOrientation orient
;
485 GtkToolbarStyle style
;
486 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
488 gtk_toolbar_set_orientation(m_toolbar
, orient
);
489 gtk_toolbar_set_style(m_toolbar
, style
);
490 gtk_toolbar_set_tooltips(m_toolbar
, !(style
& wxTB_NO_TOOLTIPS
));
493 void wxToolBar::SetWindowStyleFlag( long style
)
495 wxToolBarBase::SetWindowStyleFlag(style
);
501 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
503 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
505 if ( tool
->IsButton() )
507 if ( !HasFlag(wxTB_NOICONS
) )
509 wxBitmap bitmap
= tool
->GetNormalBitmap();
511 wxCHECK_MSG( bitmap
.Ok(), false,
512 wxT("invalid bitmap for wxToolBar icon") );
514 tool
->m_image
= gtk_image_new();
515 tool
->SetImage(bitmap
);
517 gtk_misc_set_alignment((GtkMisc
*)tool
->m_image
, 0.5, 0.5);
524 for ( size_t i
= 0; i
< pos
; i
++ )
528 // if we have a dropdown menu, we use 2 GTK tools internally
529 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item( i
);
530 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
531 if ( tool
->IsButton() && (tool
->GetKind() == wxITEM_DROPDOWN
) )
537 switch ( tool
->GetStyle() )
539 case wxTOOL_STYLE_BUTTON
:
540 // for a radio button we need the widget which starts the radio
541 // group it belongs to, i.e. the first radio button immediately
542 // preceding this one
544 GtkWidget
*widget
= NULL
;
546 if ( tool
->IsRadio() )
548 wxToolBarToolsList::compatibility_iterator node
549 = wxToolBarToolsList::compatibility_iterator();
551 node
= m_tools
.Item(pos
- 1);
555 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
556 if ( !toolNext
->IsRadio() )
559 widget
= toolNext
->m_item
;
561 node
= node
->GetPrevious();
566 // this is the first button in the radio button group,
567 // it will be toggled automatically by GTK so bring the
568 // internal flag in sync
573 tool
->m_item
= gtk_toolbar_insert_element
576 tool
->GetGtkChildType(),
578 tool
->GetLabel().empty()
580 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
581 tool
->GetShortHelp().empty()
583 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
584 "", // tooltip_private_text (?)
586 (GtkSignalFunc
)gtk_toolbar_callback
,
591 wxCHECK_MSG(tool
->m_item
!= NULL
, false, _T("gtk_toolbar_insert_element() failed"));
593 g_signal_connect (tool
->m_item
, "enter_notify_event",
594 G_CALLBACK (gtk_toolbar_tool_callback
),
596 g_signal_connect (tool
->m_item
, "leave_notify_event",
597 G_CALLBACK (gtk_toolbar_tool_callback
),
599 g_signal_connect(tool
->m_item
, "button-press-event",
600 G_CALLBACK (gtk_toolbar_tool_rclick_callback
),
603 if (tool
->GetKind() == wxITEM_DROPDOWN
)
605 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_xpm_data( arrow_down_xpm
);
606 GtkWidget
*dropdown
= gtk_toggle_button_new();
607 GtkWidget
*image
= gtk_image_new_from_pixbuf( pixbuf
);
608 gtk_widget_show( image
);
609 gtk_container_add( GTK_CONTAINER(dropdown
), image
);
611 if (GetWindowStyle() & wxTB_FLAT
)
612 gtk_button_set_relief( GTK_BUTTON(dropdown
), GTK_RELIEF_NONE
);
613 GTK_WIDGET_UNSET_FLAGS (dropdown
, GTK_CAN_FOCUS
);
614 gtk_widget_show( dropdown
);
616 g_signal_connect (dropdown
, "enter_notify_event",
617 G_CALLBACK (gtk_toolbar_buddy_enter_callback
),
619 g_signal_connect (dropdown
, "leave_notify_event",
620 G_CALLBACK (gtk_toolbar_buddy_leave_callback
),
622 g_signal_connect(dropdown
, "button-press-event",
623 G_CALLBACK (gtk_toolbar_dropdown_lclick_callback
),
627 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(tool
->m_item
) )->size_request
)
628 (tool
->m_item
, &req
);
629 gtk_widget_set_size_request( dropdown
, -1, req
.height
);
631 gtk_toolbar_insert_widget(
642 case wxTOOL_STYLE_SEPARATOR
:
643 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
648 case wxTOOL_STYLE_CONTROL
:
649 gtk_toolbar_insert_widget(
651 tool
->GetControl()->m_widget
,
656 // release reference obtained by wxInsertChildInToolBar
657 g_object_unref(tool
->GetControl()->m_widget
);
659 // connect after in order to correct size_allocate events
660 g_signal_connect_after (tool
->GetControl()->m_widget
, "size_allocate",
661 G_CALLBACK (gtktoolwidget_size_callback
), tool
->GetControl());
667 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
669 m_width
= req
.width
+ m_xMargin
;
670 m_height
= req
.height
+ 2*m_yMargin
;
671 InvalidateBestSize();
676 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
678 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
680 switch ( tool
->GetStyle() )
682 case wxTOOL_STYLE_CONTROL
:
683 tool
->GetControl()->Destroy();
686 case wxTOOL_STYLE_BUTTON
:
687 gtk_widget_destroy( tool
->m_item
);
690 case wxTOOL_STYLE_SEPARATOR
:
691 gtk_toolbar_remove_space( m_toolbar
, pos
);
695 InvalidateBestSize();
699 // ----------------------------------------------------------------------------
700 // wxToolBar tools state
701 // ----------------------------------------------------------------------------
703 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
705 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
709 gtk_widget_set_sensitive( tool
->m_item
, enable
);
713 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
715 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
717 GtkWidget
*item
= tool
->m_item
;
718 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
720 tool
->SetImage(tool
->GetBitmap());
724 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item
), toggle
);
726 m_blockEvent
= false;
730 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
731 bool WXUNUSED(toggle
))
733 // VZ: absolutely no idea about how to do it
734 wxFAIL_MSG( _T("not implemented") );
737 // ----------------------------------------------------------------------------
738 // wxToolBar geometry
739 // ----------------------------------------------------------------------------
741 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
742 wxCoord
WXUNUSED(y
)) const
744 // VZ: GTK+ doesn't seem to have such thing
745 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
747 return (wxToolBarToolBase
*)NULL
;
750 void wxToolBar::SetMargins( int x
, int y
)
752 wxCHECK_RET( GetToolsCount() == 0,
753 wxT("wxToolBar::SetMargins must be called before adding tools.") );
759 void wxToolBar::SetToolSeparation( int separation
)
761 // FIXME: this function disappeared
763 gtk_toolbar_set_space_size( m_toolbar
, separation
);
766 m_toolSeparation
= separation
;
769 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
771 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
775 (void)tool
->SetShortHelp(helpString
);
776 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
777 wxGTK_CONV( helpString
), "");
781 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
783 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
786 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
788 tool
->SetNormalBitmap(bitmap
);
789 tool
->SetImage(tool
->GetBitmap());
793 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
795 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
798 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
800 tool
->SetDisabledBitmap(bitmap
);
801 tool
->SetImage(tool
->GetBitmap());
805 // ----------------------------------------------------------------------------
806 // wxToolBar idle handling
807 // ----------------------------------------------------------------------------
809 void wxToolBar::OnInternalIdle()
811 // Check if we have to show window now
812 if (GtkShowFromOnIdle()) return;
814 wxCursor cursor
= m_cursor
;
815 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
819 /* I now set the cursor the anew in every OnInternalIdle call
820 as setting the cursor in a parent window also effects the
821 windows above so that checking for the current cursor is
824 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
826 /* if the toolbar is dockable, then m_widget stands for the
827 GtkHandleBox widget, which uses its own window so that we
828 can set the cursor for it. if the toolbar is not dockable,
829 m_widget comes from m_toolbar which uses its parent's
830 window ("windowless windows") and thus we cannot set the
832 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
835 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
838 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
839 node
= node
->GetNext();
841 GtkWidget
*item
= tool
->m_item
;
844 GdkWindow
*window
= item
->window
;
848 gdk_window_set_cursor( window
, cursor
.GetCursor() );
854 if (wxUpdateUIEvent::CanUpdate(this))
855 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
859 // ----------------------------------------------------------------------------
863 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
865 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
868 #endif // wxUSE_TOOLBAR_NATIVE