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"
43 static const char *arrow_down_xpm
[] = {
44 /* columns rows colors chars-per-pixel */
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
64 extern bool g_blockEventsOnDrag
;
65 extern wxCursor g_globalCursor
;
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // translate wxWidgets toolbar style flags to GTK orientation and style
72 static void GetGtkStyle(long style
,
73 GtkOrientation
*orient
, GtkToolbarStyle
*gtkStyle
)
75 *orient
= ( style
& wxTB_LEFT
|| style
& wxTB_RIGHT
) ? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
;
78 if ( style
& wxTB_TEXT
)
80 *gtkStyle
= style
& wxTB_NOICONS
83 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
86 else // no text, hence we must have the icons or what would we show?
88 *gtkStyle
= GTK_TOOLBAR_ICONS
;
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 class wxToolBarTool
: public wxToolBarToolBase
99 wxToolBarTool(wxToolBar
*tbar
,
101 const wxString
& label
,
102 const wxBitmap
& bitmap1
,
103 const wxBitmap
& bitmap2
,
105 wxObject
*clientData
,
106 const wxString
& shortHelpString
,
107 const wxString
& longHelpString
)
108 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
109 clientData
, shortHelpString
, longHelpString
)
114 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
115 : wxToolBarToolBase(tbar
, control
, label
)
120 // is this a radio button?
122 // unlike GetKind(), can be called for any kind of tools, not just buttons
123 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
125 // this is only called for the normal buttons, i.e. not separators nor
127 GtkToolbarChildType
GetGtkChildType() const
132 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
135 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
138 wxFAIL_MSG( _T("unknown toolbar child type") );
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
*WXUNUSED(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())
191 tool
->SetImage(tool
->GetBitmap());
193 if ( tool
->IsRadio() && !tool
->IsToggled() )
195 // radio button went up, don't report this as a wxWin event
200 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
205 tool
->SetImage(tool
->GetBitmap());
210 //-----------------------------------------------------------------------------
212 //-----------------------------------------------------------------------------
214 static gboolean
gtk_toolbar_tool_rclick_callback(GtkWidget
*WXUNUSED(widget
),
215 GdkEventButton
*event
,
216 wxToolBarToolBase
*tool
)
218 if (event
->button
!= 3)
221 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
223 if (tbar
->m_blockEvent
) return TRUE
;
225 if (g_blockEventsOnDrag
) return TRUE
;
226 if (!tool
->IsEnabled()) return TRUE
;
228 tbar
->OnRightClick( tool
->GetId(), (int)event
->x
, (int)event
->y
);
234 //-----------------------------------------------------------------------------
235 // "enter_notify_event" / "leave_notify_event" from dropdown
236 //-----------------------------------------------------------------------------
239 static gint
gtk_toolbar_buddy_enter_callback( GtkWidget
*WXUNUSED(widget
),
240 GdkEventCrossing
*WXUNUSED(gdk_event
),
243 guint8 state
= GTK_WIDGET_STATE( tool
);
244 state
|= GTK_STATE_PRELIGHT
;
245 gtk_widget_set_state( tool
, (GtkStateType
) state
);
249 static gint
gtk_toolbar_buddy_leave_callback( GtkWidget
*WXUNUSED(widget
),
250 GdkEventCrossing
*WXUNUSED(gdk_event
),
253 guint8 state
= GTK_WIDGET_STATE( tool
);
254 state
&= ~GTK_STATE_PRELIGHT
;
255 gtk_widget_set_state( tool
, (GtkStateType
) state
);
260 //-----------------------------------------------------------------------------
261 // "left-click" on dropdown
262 //-----------------------------------------------------------------------------
266 static void gtk_pop_tb_hide_callback( GtkWidget
*WXUNUSED(menu
), GtkToggleButton
*button
)
268 gtk_toggle_button_set_active( button
, FALSE
);
271 static gboolean
gtk_toolbar_dropdown_lclick_callback(GtkWidget
*widget
,
272 GdkEventButton
*event
,
273 wxToolBarToolBase
*tool
)
275 if (event
->button
!= 1)
278 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
280 if (tbar
->m_blockEvent
) return FALSE
;
282 if (g_blockEventsOnDrag
) return FALSE
;
283 if (!tool
->IsEnabled()) return FALSE
;
285 wxCommandEvent
evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, tool
->GetId() );
286 if ( tbar
->GetEventHandler()->ProcessEvent(evt
) )
291 wxMenu
* const menu
= tool
->GetDropdownMenu();
296 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(widget
), TRUE
);
298 g_signal_connect (menu
->m_menu
, "hide",
299 G_CALLBACK (gtk_pop_tb_hide_callback
),
302 tbar
->PopupMenu( menu
, widget
->allocation
.x
,
303 widget
->allocation
.y
+ widget
->allocation
.height
);
310 //-----------------------------------------------------------------------------
311 // "enter_notify_event" / "leave_notify_event"
312 //-----------------------------------------------------------------------------
315 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
316 GdkEventCrossing
*gdk_event
,
317 wxToolBarTool
*tool
)
319 if (g_blockEventsOnDrag
) return TRUE
;
321 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
324 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
325 tb
->OnMouseEnter( tool
->GetId() );
327 tb
->OnMouseEnter( -1 );
335 void gtktoolwidget_size_callback( GtkWidget
*widget
,
336 GtkAllocation
*alloc
,
339 // this shouldn't happen...
340 if (win
->GetParent()->m_wxwindow
) return;
342 wxSize size
= win
->GetEffectiveMinSize();
343 if (size
.y
!= alloc
->height
)
345 GtkAllocation alloc2
;
347 alloc2
.y
= (alloc
->height
- size
.y
+ 3) / 2;
348 alloc2
.width
= alloc
->width
;
349 alloc2
.height
= size
.y
;
350 gtk_widget_size_allocate( widget
, &alloc2
);
354 //-----------------------------------------------------------------------------
355 // InsertChild callback for wxToolBar
356 //-----------------------------------------------------------------------------
358 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
359 wxWindow
* WXUNUSED(child
) )
361 // we don't do anything here
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 void wxToolBarTool::Init()
374 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
375 const wxString
& text
,
376 const wxBitmap
& bitmap1
,
377 const wxBitmap
& bitmap2
,
379 wxObject
*clientData
,
380 const wxString
& shortHelpString
,
381 const wxString
& longHelpString
)
383 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
384 clientData
, shortHelpString
, longHelpString
);
388 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
390 return new wxToolBarTool(this, control
, label
);
393 //-----------------------------------------------------------------------------
394 // wxToolBar construction
395 //-----------------------------------------------------------------------------
397 void wxToolBar::Init()
399 m_toolbar
= (GtkToolbar
*)NULL
;
400 m_blockEvent
= false;
402 m_defaultHeight
= 32;
405 wxToolBar::~wxToolBar()
409 bool wxToolBar::Create( wxWindow
*parent
,
414 const wxString
& name
)
416 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
418 if ( !PreCreation( parent
, pos
, size
) ||
419 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
421 wxFAIL_MSG( wxT("wxToolBar creation failed") );
428 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
431 // Doesn't work this way.
432 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
433 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
435 SetToolSeparation(7);
437 if (style
& wxTB_DOCKABLE
)
439 m_widget
= gtk_handle_box_new();
440 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
441 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
443 if (style
& wxTB_FLAT
)
444 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
448 m_widget
= gtk_event_box_new();
449 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
450 ConnectWidget( m_widget
);
451 gtk_widget_show(GTK_WIDGET(m_toolbar
));
454 // FIXME: there is no such function for toolbars in 2.0
456 if (style
& wxTB_FLAT
)
457 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
460 m_parent
->DoAddChild( this );
467 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& windows
) const
469 return GTK_WIDGET(m_toolbar
)->window
;
472 void wxToolBar::GtkSetStyle()
474 GtkOrientation orient
;
475 GtkToolbarStyle style
;
476 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
478 gtk_toolbar_set_orientation(m_toolbar
, orient
);
479 gtk_toolbar_set_style(m_toolbar
, style
);
480 gtk_toolbar_set_tooltips(m_toolbar
, !(style
& wxTB_NO_TOOLTIPS
));
483 void wxToolBar::SetWindowStyleFlag( long style
)
485 wxToolBarBase::SetWindowStyleFlag(style
);
491 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
493 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
495 if ( tool
->IsButton() )
497 if ( !HasFlag(wxTB_NOICONS
) )
499 wxBitmap bitmap
= tool
->GetNormalBitmap();
501 wxCHECK_MSG( bitmap
.Ok(), false,
502 wxT("invalid bitmap for wxToolBar icon") );
504 tool
->m_image
= gtk_image_new();
505 tool
->SetImage(bitmap
);
507 gtk_misc_set_alignment((GtkMisc
*)tool
->m_image
, 0.5, 0.5);
515 for (i
= 0; i
< pos
; i
++)
520 // if we have a dropdown menu, we use 2 GTK tools
522 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item( i
);
523 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->GetData();
524 if (tool
->IsButton() && (tool
->GetKind() == wxITEM_DROPDOWN
))
530 switch ( tool
->GetStyle() )
532 case wxTOOL_STYLE_BUTTON
:
533 // for a radio button we need the widget which starts the radio
534 // group it belongs to, i.e. the first radio button immediately
535 // preceding this one
537 GtkWidget
*widget
= NULL
;
539 if ( tool
->IsRadio() )
541 wxToolBarToolsList::compatibility_iterator node
542 = wxToolBarToolsList::compatibility_iterator();
544 node
= m_tools
.Item(pos
- 1);
548 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
549 if ( !toolNext
->IsRadio() )
552 widget
= toolNext
->m_item
;
554 node
= node
->GetPrevious();
559 // this is the first button in the radio button group,
560 // it will be toggled automatically by GTK so bring the
561 // internal flag in sync
566 tool
->m_item
= gtk_toolbar_insert_element
569 tool
->GetGtkChildType(),
571 tool
->GetLabel().empty()
573 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
574 tool
->GetShortHelp().empty()
576 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
577 "", // tooltip_private_text (?)
579 (GtkSignalFunc
)gtk_toolbar_callback
,
584 wxCHECK_MSG(tool
->m_item
!= NULL
, false, _T("gtk_toolbar_insert_element() failed"));
586 g_signal_connect (tool
->m_item
, "enter_notify_event",
587 G_CALLBACK (gtk_toolbar_tool_callback
),
589 g_signal_connect (tool
->m_item
, "leave_notify_event",
590 G_CALLBACK (gtk_toolbar_tool_callback
),
592 g_signal_connect(tool
->m_item
, "button-press-event",
593 G_CALLBACK (gtk_toolbar_tool_rclick_callback
),
596 if (tool
->GetKind() == wxITEM_DROPDOWN
)
598 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_xpm_data( arrow_down_xpm
);
599 GtkWidget
*dropdown
= gtk_toggle_button_new();
600 GtkWidget
*image
= gtk_image_new_from_pixbuf( pixbuf
);
601 gtk_widget_show( image
);
602 gtk_container_add( GTK_CONTAINER(dropdown
), image
);
604 if (GetWindowStyle() & wxTB_FLAT
)
605 gtk_button_set_relief( GTK_BUTTON(dropdown
), GTK_RELIEF_NONE
);
606 GTK_WIDGET_UNSET_FLAGS (dropdown
, GTK_CAN_FOCUS
);
607 gtk_widget_show( dropdown
);
609 g_signal_connect (dropdown
, "enter_notify_event",
610 G_CALLBACK (gtk_toolbar_buddy_enter_callback
),
612 g_signal_connect (dropdown
, "leave_notify_event",
613 G_CALLBACK (gtk_toolbar_buddy_leave_callback
),
615 g_signal_connect(dropdown
, "button-press-event",
616 G_CALLBACK (gtk_toolbar_dropdown_lclick_callback
),
620 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(tool
->m_item
) )->size_request
)
621 (tool
->m_item
, &req
);
622 gtk_widget_set_size_request( dropdown
, -1, req
.height
);
624 gtk_toolbar_insert_widget(
635 case wxTOOL_STYLE_SEPARATOR
:
636 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
641 case wxTOOL_STYLE_CONTROL
:
642 gtk_toolbar_insert_widget(
644 tool
->GetControl()->m_widget
,
650 // connect after in order to correct size_allocate events
651 g_signal_connect_after (tool
->GetControl()->m_widget
, "size_allocate",
652 G_CALLBACK (gtktoolwidget_size_callback
), tool
->GetControl());
658 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
660 m_width
= req
.width
+ m_xMargin
;
661 m_height
= req
.height
+ 2*m_yMargin
;
662 InvalidateBestSize();
667 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
669 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
671 switch ( tool
->GetStyle() )
673 case wxTOOL_STYLE_CONTROL
:
674 tool
->GetControl()->Destroy();
677 case wxTOOL_STYLE_BUTTON
:
678 gtk_widget_destroy( tool
->m_item
);
681 case wxTOOL_STYLE_SEPARATOR
:
682 gtk_toolbar_remove_space( m_toolbar
, pos
);
686 InvalidateBestSize();
690 // ----------------------------------------------------------------------------
691 // wxToolBar tools state
692 // ----------------------------------------------------------------------------
694 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
696 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
700 gtk_widget_set_sensitive( tool
->m_item
, enable
);
704 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
706 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
708 GtkWidget
*item
= tool
->m_item
;
709 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
711 tool
->SetImage(tool
->GetBitmap());
715 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item
), toggle
);
717 m_blockEvent
= false;
721 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
722 bool WXUNUSED(toggle
))
724 // VZ: absolutely no idea about how to do it
725 wxFAIL_MSG( _T("not implemented") );
728 // ----------------------------------------------------------------------------
729 // wxToolBar geometry
730 // ----------------------------------------------------------------------------
732 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
733 wxCoord
WXUNUSED(y
)) const
735 // VZ: GTK+ doesn't seem to have such thing
736 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
738 return (wxToolBarToolBase
*)NULL
;
741 void wxToolBar::SetMargins( int x
, int y
)
743 wxCHECK_RET( GetToolsCount() == 0,
744 wxT("wxToolBar::SetMargins must be called before adding tools.") );
750 void wxToolBar::SetToolSeparation( int separation
)
752 // FIXME: this function disappeared
754 gtk_toolbar_set_space_size( m_toolbar
, separation
);
757 m_toolSeparation
= separation
;
760 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
762 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
766 (void)tool
->SetShortHelp(helpString
);
767 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
768 wxGTK_CONV( helpString
), "");
772 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
774 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
777 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
779 tool
->SetNormalBitmap(bitmap
);
780 tool
->SetImage(tool
->GetBitmap());
784 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
786 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
789 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
791 tool
->SetDisabledBitmap(bitmap
);
792 tool
->SetImage(tool
->GetBitmap());
796 // ----------------------------------------------------------------------------
797 // wxToolBar idle handling
798 // ----------------------------------------------------------------------------
800 void wxToolBar::OnInternalIdle()
802 // Check if we have to show window now
803 if (GtkShowFromOnIdle()) return;
805 wxCursor cursor
= m_cursor
;
806 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
810 /* I now set the cursor the anew in every OnInternalIdle call
811 as setting the cursor in a parent window also effects the
812 windows above so that checking for the current cursor is
815 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
817 /* if the toolbar is dockable, then m_widget stands for the
818 GtkHandleBox widget, which uses its own window so that we
819 can set the cursor for it. if the toolbar is not dockable,
820 m_widget comes from m_toolbar which uses its parent's
821 window ("windowless windows") and thus we cannot set the
823 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
826 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
829 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
830 node
= node
->GetNext();
832 GtkWidget
*item
= tool
->m_item
;
835 GdkWindow
*window
= item
->window
;
839 gdk_window_set_cursor( window
, cursor
.GetCursor() );
845 if (wxUpdateUIEvent::CanUpdate(this))
846 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
850 // ----------------------------------------------------------------------------
854 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
856 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
859 #endif // wxUSE_TOOLBAR_NATIVE