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 #undef GTK_DISABLE_DEPRECATED
34 #include "wx/gtk/private.h"
37 static const char *arrow_down_xpm
[] = {
38 /* columns rows colors chars-per-pixel */
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
58 extern bool g_blockEventsOnDrag
;
59 extern wxCursor g_globalCursor
;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // translate wxWidgets toolbar style flags to GTK orientation and style
66 static void GetGtkStyle(long style
,
67 GtkOrientation
*orient
, GtkToolbarStyle
*gtkStyle
)
69 *orient
= ( style
& wxTB_LEFT
|| style
& wxTB_RIGHT
) ? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
;
72 if ( style
& wxTB_TEXT
)
74 *gtkStyle
= style
& wxTB_NOICONS
77 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
80 else // no text, hence we must have the icons or what would we show?
82 *gtkStyle
= GTK_TOOLBAR_ICONS
;
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 class wxToolBarTool
: public wxToolBarToolBase
93 wxToolBarTool(wxToolBar
*tbar
,
95 const wxString
& label
,
96 const wxBitmap
& bitmap1
,
97 const wxBitmap
& bitmap2
,
100 const wxString
& shortHelpString
,
101 const wxString
& longHelpString
)
102 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
103 clientData
, shortHelpString
, longHelpString
)
108 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
109 : wxToolBarToolBase(tbar
, control
, label
)
114 // is this a radio button?
116 // unlike GetKind(), can be called for any kind of tools, not just buttons
117 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
119 // this is only called for the normal buttons, i.e. not separators nor
121 GtkToolbarChildType
GetGtkChildType() const
126 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
129 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
132 wxFAIL_MSG( _T("unknown toolbar child type") );
135 case wxITEM_DROPDOWN
:
137 return GTK_TOOLBAR_CHILD_BUTTON
;
141 void SetImage(const wxBitmap
& bitmap
)
145 // setting from pixmap doesn't seem to work right, but pixbuf works well
146 gtk_image_set_from_pixbuf((GtkImage
*)m_image
, bitmap
.GetPixbuf());
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
163 // ============================================================================
165 // ============================================================================
167 //-----------------------------------------------------------------------------
168 // "clicked" (internal from gtk_toolbar)
169 //-----------------------------------------------------------------------------
172 static void gtk_toolbar_callback( GtkWidget
*widget
,
173 wxToolBarTool
*tool
)
175 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
177 if (tbar
->m_blockEvent
) return;
179 if (g_blockEventsOnDrag
) return;
180 if (!tool
->IsEnabled()) return;
182 if (tool
->CanBeToggled())
184 if (tool
->IsRadio() &&
185 gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(widget
)) &&
188 // pressed an already pressed radio button
194 tool
->SetImage(tool
->GetBitmap());
196 if ( tool
->IsRadio() && !tool
->IsToggled() )
198 // radio button went up, don't report this as a wxWin event
203 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
208 tool
->SetImage(tool
->GetBitmap());
213 //-----------------------------------------------------------------------------
215 //-----------------------------------------------------------------------------
217 static gboolean
gtk_toolbar_tool_rclick_callback(GtkWidget
*WXUNUSED(widget
),
218 GdkEventButton
*event
,
219 wxToolBarToolBase
*tool
)
221 if (event
->button
!= 3)
224 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
226 if (tbar
->m_blockEvent
) return TRUE
;
228 if (g_blockEventsOnDrag
) return TRUE
;
229 if (!tool
->IsEnabled()) return TRUE
;
231 tbar
->OnRightClick( tool
->GetId(), (int)event
->x
, (int)event
->y
);
237 //-----------------------------------------------------------------------------
238 // "enter_notify_event" / "leave_notify_event" from dropdown
239 //-----------------------------------------------------------------------------
242 static gint
gtk_toolbar_buddy_enter_callback( GtkWidget
*WXUNUSED(widget
),
243 GdkEventCrossing
*WXUNUSED(gdk_event
),
246 guint8 state
= GTK_WIDGET_STATE( tool
);
247 state
|= GTK_STATE_PRELIGHT
;
248 gtk_widget_set_state( tool
, (GtkStateType
) state
);
252 static gint
gtk_toolbar_buddy_leave_callback( GtkWidget
*WXUNUSED(widget
),
253 GdkEventCrossing
*WXUNUSED(gdk_event
),
256 guint8 state
= GTK_WIDGET_STATE( tool
);
257 state
&= ~GTK_STATE_PRELIGHT
;
258 gtk_widget_set_state( tool
, (GtkStateType
) state
);
263 //-----------------------------------------------------------------------------
264 // "left-click" on dropdown
265 //-----------------------------------------------------------------------------
269 static void gtk_pop_tb_hide_callback( GtkWidget
*WXUNUSED(menu
), GtkToggleButton
*button
)
271 gtk_toggle_button_set_active( button
, FALSE
);
274 static gboolean
gtk_toolbar_dropdown_lclick_callback(GtkWidget
*widget
,
275 GdkEventButton
*event
,
276 wxToolBarToolBase
*tool
)
278 if (event
->button
!= 1)
281 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
283 if (tbar
->m_blockEvent
) return FALSE
;
285 if (g_blockEventsOnDrag
) return FALSE
;
286 if (!tool
->IsEnabled()) return FALSE
;
288 wxCommandEvent
evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, tool
->GetId() );
289 if ( tbar
->GetEventHandler()->ProcessEvent(evt
) )
294 wxMenu
* const menu
= tool
->GetDropdownMenu();
299 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(widget
), TRUE
);
301 g_signal_connect (menu
->m_menu
, "hide",
302 G_CALLBACK (gtk_pop_tb_hide_callback
),
305 tbar
->PopupMenu( menu
, widget
->allocation
.x
,
306 widget
->allocation
.y
+ widget
->allocation
.height
);
313 //-----------------------------------------------------------------------------
314 // "enter_notify_event" / "leave_notify_event"
315 //-----------------------------------------------------------------------------
318 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
319 GdkEventCrossing
*gdk_event
,
320 wxToolBarTool
*tool
)
322 if (g_blockEventsOnDrag
) return TRUE
;
324 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
327 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
328 tb
->OnMouseEnter( tool
->GetId() );
330 tb
->OnMouseEnter( -1 );
336 //-----------------------------------------------------------------------------
337 // InsertChild callback for wxToolBar
338 //-----------------------------------------------------------------------------
340 static void wxInsertChildInToolBar( wxWindow
* WXUNUSED(parent
),
343 // Child widget will be inserted into GtkToolbar by DoInsertTool. Ref it
344 // here so reparenting into wxToolBar doesn't delete it.
345 g_object_ref(child
->m_widget
);
348 // ----------------------------------------------------------------------------
350 // ----------------------------------------------------------------------------
352 void wxToolBarTool::Init()
358 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
359 const wxString
& text
,
360 const wxBitmap
& bitmap1
,
361 const wxBitmap
& bitmap2
,
363 wxObject
*clientData
,
364 const wxString
& shortHelpString
,
365 const wxString
& longHelpString
)
367 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
368 clientData
, shortHelpString
, longHelpString
);
372 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
374 return new wxToolBarTool(this, control
, label
);
377 //-----------------------------------------------------------------------------
378 // wxToolBar construction
379 //-----------------------------------------------------------------------------
381 void wxToolBar::Init()
383 m_toolbar
= (GtkToolbar
*)NULL
;
384 m_blockEvent
= false;
386 m_defaultHeight
= 32;
389 wxToolBar::~wxToolBar()
393 bool wxToolBar::Create( wxWindow
*parent
,
398 const wxString
& name
)
400 m_insertCallback
= wxInsertChildInToolBar
;
402 if ( !PreCreation( parent
, pos
, size
) ||
403 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
405 wxFAIL_MSG( wxT("wxToolBar creation failed") );
412 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
415 // Doesn't work this way.
416 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
417 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
419 SetToolSeparation(7);
421 if (style
& wxTB_DOCKABLE
)
423 m_widget
= gtk_handle_box_new();
424 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
425 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
427 if (style
& wxTB_FLAT
)
428 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
432 m_widget
= gtk_event_box_new();
433 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
434 ConnectWidget( m_widget
);
435 gtk_widget_show(GTK_WIDGET(m_toolbar
));
438 // FIXME: there is no such function for toolbars in 2.0
440 if (style
& wxTB_FLAT
)
441 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
444 m_parent
->DoAddChild( this );
451 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
453 return GTK_WIDGET(m_toolbar
)->window
;
456 void wxToolBar::GtkSetStyle()
458 GtkOrientation orient
;
459 GtkToolbarStyle style
;
460 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
462 gtk_toolbar_set_orientation(m_toolbar
, orient
);
463 gtk_toolbar_set_style(m_toolbar
, style
);
464 gtk_toolbar_set_tooltips(m_toolbar
, !(style
& wxTB_NO_TOOLTIPS
));
467 void wxToolBar::SetWindowStyleFlag( long style
)
469 wxToolBarBase::SetWindowStyleFlag(style
);
475 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
477 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
479 if ( tool
->IsButton() )
481 if ( !HasFlag(wxTB_NOICONS
) )
483 wxBitmap bitmap
= tool
->GetNormalBitmap();
485 wxCHECK_MSG( bitmap
.Ok(), false,
486 wxT("invalid bitmap for wxToolBar icon") );
488 tool
->m_image
= gtk_image_new();
489 tool
->SetImage(bitmap
);
491 gtk_misc_set_alignment((GtkMisc
*)tool
->m_image
, 0.5, 0.5);
498 for ( size_t i
= 0; i
< pos
; i
++ )
502 // if we have a dropdown menu, we use 2 GTK tools internally
503 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item( i
);
504 wxToolBarTool
* const tool2
= (wxToolBarTool
*) node
->GetData();
505 if ( tool2
->IsButton() && tool2
->GetKind() == wxITEM_DROPDOWN
)
511 switch ( tool
->GetStyle() )
513 case wxTOOL_STYLE_BUTTON
:
514 // for a radio button we need the widget which starts the radio
515 // group it belongs to, i.e. the first radio button immediately
516 // preceding this one
518 GtkWidget
*widget
= NULL
;
520 if ( tool
->IsRadio() )
522 wxToolBarToolsList::compatibility_iterator node
523 = wxToolBarToolsList::compatibility_iterator();
525 node
= m_tools
.Item(pos
- 1);
529 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
530 if ( !toolNext
->IsRadio() )
533 widget
= toolNext
->m_item
;
535 node
= node
->GetPrevious();
540 // this is the first button in the radio button group,
541 // it will be toggled automatically by GTK so bring the
542 // internal flag in sync
547 tool
->m_item
= gtk_toolbar_insert_element
550 tool
->GetGtkChildType(),
552 tool
->GetLabel().empty()
554 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
555 tool
->GetShortHelp().empty()
557 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
558 "", // tooltip_private_text (?)
560 (GtkSignalFunc
)gtk_toolbar_callback
,
565 wxCHECK_MSG(tool
->m_item
!= NULL
, false, _T("gtk_toolbar_insert_element() failed"));
567 g_signal_connect (tool
->m_item
, "enter_notify_event",
568 G_CALLBACK (gtk_toolbar_tool_callback
),
570 g_signal_connect (tool
->m_item
, "leave_notify_event",
571 G_CALLBACK (gtk_toolbar_tool_callback
),
573 g_signal_connect(tool
->m_item
, "button-press-event",
574 G_CALLBACK (gtk_toolbar_tool_rclick_callback
),
577 if (tool
->GetKind() == wxITEM_DROPDOWN
)
579 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_xpm_data( arrow_down_xpm
);
580 GtkWidget
*dropdown
= gtk_toggle_button_new();
581 GtkWidget
*image
= gtk_image_new_from_pixbuf( pixbuf
);
582 gtk_widget_show( image
);
583 gtk_container_add( GTK_CONTAINER(dropdown
), image
);
585 if (GetWindowStyle() & wxTB_FLAT
)
586 gtk_button_set_relief( GTK_BUTTON(dropdown
), GTK_RELIEF_NONE
);
587 GTK_WIDGET_UNSET_FLAGS (dropdown
, GTK_CAN_FOCUS
);
588 gtk_widget_show( dropdown
);
590 g_signal_connect (dropdown
, "enter_notify_event",
591 G_CALLBACK (gtk_toolbar_buddy_enter_callback
),
593 g_signal_connect (dropdown
, "leave_notify_event",
594 G_CALLBACK (gtk_toolbar_buddy_leave_callback
),
596 g_signal_connect(dropdown
, "button-press-event",
597 G_CALLBACK (gtk_toolbar_dropdown_lclick_callback
),
601 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(tool
->m_item
) )->size_request
)
602 (tool
->m_item
, &req
);
603 gtk_widget_set_size_request( dropdown
, -1, req
.height
);
605 gtk_toolbar_insert_widget(
616 case wxTOOL_STYLE_SEPARATOR
:
617 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
622 case wxTOOL_STYLE_CONTROL
:
623 GtkWidget
* align
= gtk_alignment_new(0.5, 0.5, 0, 0);
624 gtk_widget_show(align
);
625 gtk_container_add((GtkContainer
*)align
, tool
->GetControl()->m_widget
);
626 gtk_toolbar_insert_widget(
633 // release reference obtained by wxInsertChildInToolBar
634 g_object_unref(tool
->GetControl()->m_widget
);
639 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
641 m_width
= req
.width
+ m_xMargin
;
642 m_height
= req
.height
+ 2*m_yMargin
;
643 InvalidateBestSize();
648 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
650 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
652 switch ( tool
->GetStyle() )
654 case wxTOOL_STYLE_CONTROL
:
655 tool
->GetControl()->Destroy();
658 case wxTOOL_STYLE_BUTTON
:
659 gtk_widget_destroy( tool
->m_item
);
662 case wxTOOL_STYLE_SEPARATOR
:
663 gtk_toolbar_remove_space( m_toolbar
, pos
);
667 InvalidateBestSize();
671 // ----------------------------------------------------------------------------
672 // wxToolBar tools state
673 // ----------------------------------------------------------------------------
675 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
677 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
681 gtk_widget_set_sensitive( tool
->m_item
, enable
);
685 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
687 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
689 GtkWidget
*item
= tool
->m_item
;
690 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
692 tool
->SetImage(tool
->GetBitmap());
696 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item
), toggle
);
698 m_blockEvent
= false;
702 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
703 bool WXUNUSED(toggle
))
705 // VZ: absolutely no idea about how to do it
706 wxFAIL_MSG( _T("not implemented") );
709 // ----------------------------------------------------------------------------
710 // wxToolBar geometry
711 // ----------------------------------------------------------------------------
713 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
714 wxCoord
WXUNUSED(y
)) const
716 // VZ: GTK+ doesn't seem to have such thing
717 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
719 return (wxToolBarToolBase
*)NULL
;
722 void wxToolBar::SetMargins( int x
, int y
)
724 wxCHECK_RET( GetToolsCount() == 0,
725 wxT("wxToolBar::SetMargins must be called before adding tools.") );
731 void wxToolBar::SetToolSeparation( int separation
)
733 // FIXME: this function disappeared
735 gtk_toolbar_set_space_size( m_toolbar
, separation
);
738 m_toolSeparation
= separation
;
741 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
743 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
747 (void)tool
->SetShortHelp(helpString
);
748 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
749 wxGTK_CONV( helpString
), "");
753 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
755 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
758 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
760 tool
->SetNormalBitmap(bitmap
);
761 tool
->SetImage(tool
->GetBitmap());
765 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
767 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
770 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
772 tool
->SetDisabledBitmap(bitmap
);
773 tool
->SetImage(tool
->GetBitmap());
777 // ----------------------------------------------------------------------------
778 // wxToolBar idle handling
779 // ----------------------------------------------------------------------------
781 void wxToolBar::OnInternalIdle()
783 // Check if we have to show window now
784 if (GtkShowFromOnIdle()) return;
786 wxCursor cursor
= m_cursor
;
787 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
791 /* I now set the cursor the anew in every OnInternalIdle call
792 as setting the cursor in a parent window also effects the
793 windows above so that checking for the current cursor is
796 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
798 /* if the toolbar is dockable, then m_widget stands for the
799 GtkHandleBox widget, which uses its own window so that we
800 can set the cursor for it. if the toolbar is not dockable,
801 m_widget comes from m_toolbar which uses its parent's
802 window ("windowless windows") and thus we cannot set the
804 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
807 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
810 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
811 node
= node
->GetNext();
813 GtkWidget
*item
= tool
->m_item
;
816 GdkWindow
*window
= item
->window
;
820 gdk_window_set_cursor( window
, cursor
.GetCursor() );
826 if (wxUpdateUIEvent::CanUpdate(this))
827 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
831 // ----------------------------------------------------------------------------
835 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
837 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
840 #endif // wxUSE_TOOLBAR_NATIVE