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 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #if wxUSE_TOOLBAR_NATIVE
16 #include "wx/toolbar.h"
22 // FIXME: Use GtkImage instead of GtkPixmap. Use the new toolbar API for when gtk runtime is new enough?
23 // Beware that the new and old toolbar API may not be mixed in usage.
24 #undef GTK_DISABLE_DEPRECATED
26 #include "wx/gtk/private.h"
29 static const char *arrow_down_xpm
[] = {
30 /* columns rows colors chars-per-pixel */
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
50 extern bool g_blockEventsOnDrag
;
51 extern wxCursor g_globalCursor
;
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // translate wxWidgets toolbar style flags to GTK orientation and style
58 static void GetGtkStyle(long style
,
59 GtkOrientation
*orient
, GtkToolbarStyle
*gtkStyle
)
61 *orient
= ( style
& wxTB_LEFT
|| style
& wxTB_RIGHT
) ? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
;
64 if ( style
& wxTB_TEXT
)
66 *gtkStyle
= style
& wxTB_NOICONS
69 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
72 else // no text, hence we must have the icons or what would we show?
74 *gtkStyle
= GTK_TOOLBAR_ICONS
;
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 class wxToolBarTool
: public wxToolBarToolBase
85 wxToolBarTool(wxToolBar
*tbar
,
87 const wxString
& label
,
88 const wxBitmap
& bitmap1
,
89 const wxBitmap
& bitmap2
,
92 const wxString
& shortHelpString
,
93 const wxString
& longHelpString
)
94 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
95 clientData
, shortHelpString
, longHelpString
)
100 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
101 : wxToolBarToolBase(tbar
, control
, label
)
106 // is this a radio button?
108 // unlike GetKind(), can be called for any kind of tools, not just buttons
109 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
111 // this is only called for the normal buttons, i.e. not separators nor
113 GtkToolbarChildType
GetGtkChildType() const
118 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
121 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
124 wxFAIL_MSG( _T("unknown toolbar child type") );
127 case wxITEM_DROPDOWN
:
129 return GTK_TOOLBAR_CHILD_BUTTON
;
133 void SetImage(const wxBitmap
& bitmap
)
137 // setting from pixmap doesn't seem to work right, but pixbuf works well
138 gtk_image_set_from_pixbuf((GtkImage
*)m_image
, bitmap
.GetPixbuf());
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
155 // ============================================================================
157 // ============================================================================
159 //-----------------------------------------------------------------------------
160 // "clicked" (internal from gtk_toolbar)
161 //-----------------------------------------------------------------------------
164 static void gtk_toolbar_callback( GtkWidget
*widget
,
165 wxToolBarTool
*tool
)
167 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
169 if (tbar
->m_blockEvent
) return;
171 if (g_blockEventsOnDrag
) return;
172 if (!tool
->IsEnabled()) return;
174 if (tool
->CanBeToggled())
176 if (tool
->IsRadio() &&
177 gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(widget
)) &&
180 // pressed an already pressed radio button
186 tool
->SetImage(tool
->GetBitmap());
188 if ( tool
->IsRadio() && !tool
->IsToggled() )
190 // radio button went up, don't report this as a wxWin event
195 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
200 tool
->SetImage(tool
->GetBitmap());
205 //-----------------------------------------------------------------------------
207 //-----------------------------------------------------------------------------
209 static gboolean
gtk_toolbar_tool_rclick_callback(GtkWidget
*WXUNUSED(widget
),
210 GdkEventButton
*event
,
211 wxToolBarToolBase
*tool
)
213 if (event
->button
!= 3)
216 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
218 if (tbar
->m_blockEvent
) return TRUE
;
220 if (g_blockEventsOnDrag
) return TRUE
;
221 if (!tool
->IsEnabled()) return TRUE
;
223 tbar
->OnRightClick( tool
->GetId(), (int)event
->x
, (int)event
->y
);
229 //-----------------------------------------------------------------------------
230 // "enter_notify_event" / "leave_notify_event" from dropdown
231 //-----------------------------------------------------------------------------
234 static gint
gtk_toolbar_buddy_enter_callback( GtkWidget
*WXUNUSED(widget
),
235 GdkEventCrossing
*WXUNUSED(gdk_event
),
238 guint8 state
= GTK_WIDGET_STATE( tool
);
239 state
|= GTK_STATE_PRELIGHT
;
240 gtk_widget_set_state( tool
, (GtkStateType
) state
);
244 static gint
gtk_toolbar_buddy_leave_callback( GtkWidget
*WXUNUSED(widget
),
245 GdkEventCrossing
*WXUNUSED(gdk_event
),
248 guint8 state
= GTK_WIDGET_STATE( tool
);
249 state
&= ~GTK_STATE_PRELIGHT
;
250 gtk_widget_set_state( tool
, (GtkStateType
) state
);
255 //-----------------------------------------------------------------------------
256 // "left-click" on dropdown
257 //-----------------------------------------------------------------------------
261 static void gtk_pop_tb_hide_callback( GtkWidget
*WXUNUSED(menu
), GtkToggleButton
*button
)
263 gtk_toggle_button_set_active( button
, FALSE
);
266 static gboolean
gtk_toolbar_dropdown_lclick_callback(GtkWidget
*widget
,
267 GdkEventButton
*event
,
268 wxToolBarToolBase
*tool
)
270 if (event
->button
!= 1)
273 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
275 if (tbar
->m_blockEvent
) return FALSE
;
277 if (g_blockEventsOnDrag
) return FALSE
;
278 if (!tool
->IsEnabled()) return FALSE
;
280 wxCommandEvent
evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, tool
->GetId() );
281 if ( tbar
->GetEventHandler()->ProcessEvent(evt
) )
286 wxMenu
* const menu
= tool
->GetDropdownMenu();
291 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(widget
), TRUE
);
293 g_signal_connect (menu
->m_menu
, "hide",
294 G_CALLBACK (gtk_pop_tb_hide_callback
),
297 tbar
->PopupMenu( menu
, widget
->allocation
.x
,
298 widget
->allocation
.y
+ widget
->allocation
.height
);
305 //-----------------------------------------------------------------------------
306 // "enter_notify_event" / "leave_notify_event"
307 //-----------------------------------------------------------------------------
310 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
311 GdkEventCrossing
*gdk_event
,
312 wxToolBarTool
*tool
)
314 if (g_blockEventsOnDrag
) return TRUE
;
316 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
319 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
320 tb
->OnMouseEnter( tool
->GetId() );
322 tb
->OnMouseEnter( -1 );
328 //-----------------------------------------------------------------------------
329 // "size_request" from m_toolbar
330 //-----------------------------------------------------------------------------
334 size_request(GtkWidget
*, GtkRequisition
* req
, wxToolBar
* win
)
336 const wxSize margins
= win
->GetMargins();
337 req
->width
+= margins
.x
;
338 req
->height
+= 2 * margins
.y
;
342 //-----------------------------------------------------------------------------
343 // InsertChild callback for wxToolBar
344 //-----------------------------------------------------------------------------
346 static void wxInsertChildInToolBar( wxWindow
* WXUNUSED(parent
),
349 // Child widget will be inserted into GtkToolbar by DoInsertTool. Ref it
350 // here so reparenting into wxToolBar doesn't delete it.
351 g_object_ref(child
->m_widget
);
354 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
358 void wxToolBarTool::Init()
364 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
365 const wxString
& text
,
366 const wxBitmap
& bitmap1
,
367 const wxBitmap
& bitmap2
,
369 wxObject
*clientData
,
370 const wxString
& shortHelpString
,
371 const wxString
& longHelpString
)
373 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
374 clientData
, shortHelpString
, longHelpString
);
378 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
380 return new wxToolBarTool(this, control
, label
);
383 //-----------------------------------------------------------------------------
384 // wxToolBar construction
385 //-----------------------------------------------------------------------------
387 void wxToolBar::Init()
389 m_toolbar
= (GtkToolbar
*)NULL
;
390 m_blockEvent
= false;
392 m_defaultHeight
= 32;
395 wxToolBar::~wxToolBar()
399 bool wxToolBar::Create( wxWindow
*parent
,
404 const wxString
& name
)
406 m_insertCallback
= wxInsertChildInToolBar
;
408 if ( !PreCreation( parent
, pos
, size
) ||
409 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
411 wxFAIL_MSG( wxT("wxToolBar creation failed") );
418 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
421 // Doesn't work this way.
422 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
423 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
425 SetToolSeparation(7);
427 if (style
& wxTB_DOCKABLE
)
429 m_widget
= gtk_handle_box_new();
430 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
431 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
433 if (style
& wxTB_FLAT
)
434 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
438 m_widget
= gtk_event_box_new();
439 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
440 ConnectWidget( m_widget
);
441 gtk_widget_show(GTK_WIDGET(m_toolbar
));
444 // FIXME: there is no such function for toolbars in 2.0
446 if (style
& wxTB_FLAT
)
447 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
450 m_parent
->DoAddChild( this );
454 g_signal_connect_after(m_toolbar
, "size_request",
455 G_CALLBACK(size_request
), this);
460 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
462 return GTK_WIDGET(m_toolbar
)->window
;
465 void wxToolBar::GtkSetStyle()
467 GtkOrientation orient
;
468 GtkToolbarStyle style
;
469 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
471 gtk_toolbar_set_orientation(m_toolbar
, orient
);
472 gtk_toolbar_set_style(m_toolbar
, style
);
473 gtk_toolbar_set_tooltips(m_toolbar
, !(style
& wxTB_NO_TOOLTIPS
));
476 void wxToolBar::SetWindowStyleFlag( long style
)
478 wxToolBarBase::SetWindowStyleFlag(style
);
484 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
486 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
488 if ( tool
->IsButton() )
490 if ( !HasFlag(wxTB_NOICONS
) )
492 wxBitmap bitmap
= tool
->GetNormalBitmap();
494 wxCHECK_MSG( bitmap
.Ok(), false,
495 wxT("invalid bitmap for wxToolBar icon") );
497 tool
->m_image
= gtk_image_new();
498 tool
->SetImage(bitmap
);
500 gtk_misc_set_alignment((GtkMisc
*)tool
->m_image
, 0.5, 0.5);
507 for ( size_t i
= 0; i
< pos
; i
++ )
511 // if we have a dropdown menu, we use 2 GTK tools internally
512 wxToolBarToolsList::compatibility_iterator node
= m_tools
.Item( i
);
513 wxToolBarTool
* const tool2
= (wxToolBarTool
*) node
->GetData();
514 if ( tool2
->IsButton() && tool2
->GetKind() == wxITEM_DROPDOWN
)
520 switch ( tool
->GetStyle() )
522 case wxTOOL_STYLE_BUTTON
:
523 // for a radio button we need the widget which starts the radio
524 // group it belongs to, i.e. the first radio button immediately
525 // preceding this one
527 GtkWidget
*widget
= NULL
;
529 if ( tool
->IsRadio() )
531 wxToolBarToolsList::compatibility_iterator node
532 = wxToolBarToolsList::compatibility_iterator();
534 node
= m_tools
.Item(pos
- 1);
538 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
539 if ( !toolNext
->IsRadio() )
542 widget
= toolNext
->m_item
;
544 node
= node
->GetPrevious();
549 // this is the first button in the radio button group,
550 // it will be toggled automatically by GTK so bring the
551 // internal flag in sync
556 tool
->m_item
= gtk_toolbar_insert_element
559 tool
->GetGtkChildType(),
561 tool
->GetLabel().empty()
563 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
564 tool
->GetShortHelp().empty()
566 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
567 "", // tooltip_private_text (?)
569 (GtkSignalFunc
)gtk_toolbar_callback
,
574 wxCHECK_MSG(tool
->m_item
!= NULL
, false, _T("gtk_toolbar_insert_element() failed"));
576 g_signal_connect (tool
->m_item
, "enter_notify_event",
577 G_CALLBACK (gtk_toolbar_tool_callback
),
579 g_signal_connect (tool
->m_item
, "leave_notify_event",
580 G_CALLBACK (gtk_toolbar_tool_callback
),
582 g_signal_connect(tool
->m_item
, "button-press-event",
583 G_CALLBACK (gtk_toolbar_tool_rclick_callback
),
586 if (tool
->GetKind() == wxITEM_DROPDOWN
)
588 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_xpm_data( arrow_down_xpm
);
589 GtkWidget
*dropdown
= gtk_toggle_button_new();
590 GtkWidget
*image
= gtk_image_new_from_pixbuf( pixbuf
);
591 gtk_widget_show( image
);
592 gtk_container_add( GTK_CONTAINER(dropdown
), image
);
594 if (GetWindowStyle() & wxTB_FLAT
)
595 gtk_button_set_relief( GTK_BUTTON(dropdown
), GTK_RELIEF_NONE
);
596 GTK_WIDGET_UNSET_FLAGS (dropdown
, GTK_CAN_FOCUS
);
597 gtk_widget_show( dropdown
);
599 g_signal_connect (dropdown
, "enter_notify_event",
600 G_CALLBACK (gtk_toolbar_buddy_enter_callback
),
602 g_signal_connect (dropdown
, "leave_notify_event",
603 G_CALLBACK (gtk_toolbar_buddy_leave_callback
),
605 g_signal_connect(dropdown
, "button-press-event",
606 G_CALLBACK (gtk_toolbar_dropdown_lclick_callback
),
610 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(tool
->m_item
) )->size_request
)
611 (tool
->m_item
, &req
);
612 gtk_widget_set_size_request( dropdown
, -1, req
.height
);
614 gtk_toolbar_insert_widget(
625 case wxTOOL_STYLE_SEPARATOR
:
626 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
631 case wxTOOL_STYLE_CONTROL
:
632 GtkWidget
* align
= gtk_alignment_new(0.5, 0.5, 0, 0);
633 gtk_widget_show(align
);
634 gtk_container_add((GtkContainer
*)align
, tool
->GetControl()->m_widget
);
635 gtk_toolbar_insert_widget(
642 // release reference obtained by wxInsertChildInToolBar
643 g_object_unref(tool
->GetControl()->m_widget
);
647 InvalidateBestSize();
652 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
654 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
656 switch ( tool
->GetStyle() )
658 case wxTOOL_STYLE_CONTROL
:
659 tool
->GetControl()->Destroy();
662 case wxTOOL_STYLE_BUTTON
:
663 gtk_widget_destroy( tool
->m_item
);
666 case wxTOOL_STYLE_SEPARATOR
:
667 gtk_toolbar_remove_space( m_toolbar
, pos
);
671 InvalidateBestSize();
675 // ----------------------------------------------------------------------------
676 // wxToolBar tools state
677 // ----------------------------------------------------------------------------
679 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
681 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
685 gtk_widget_set_sensitive( tool
->m_item
, enable
);
689 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
691 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
693 GtkWidget
*item
= tool
->m_item
;
694 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
696 tool
->SetImage(tool
->GetBitmap());
700 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item
), toggle
);
702 m_blockEvent
= false;
706 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
707 bool WXUNUSED(toggle
))
709 // VZ: absolutely no idea about how to do it
710 wxFAIL_MSG( _T("not implemented") );
713 // ----------------------------------------------------------------------------
714 // wxToolBar geometry
715 // ----------------------------------------------------------------------------
717 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
718 wxCoord
WXUNUSED(y
)) const
720 // VZ: GTK+ doesn't seem to have such thing
721 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
723 return (wxToolBarToolBase
*)NULL
;
726 void wxToolBar::SetMargins( int x
, int y
)
728 wxCHECK_RET( GetToolsCount() == 0,
729 wxT("wxToolBar::SetMargins must be called before adding tools.") );
735 void wxToolBar::SetToolSeparation( int separation
)
737 // FIXME: this function disappeared
739 gtk_toolbar_set_space_size( m_toolbar
, separation
);
742 m_toolSeparation
= separation
;
745 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
747 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
751 (void)tool
->SetShortHelp(helpString
);
752 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
753 wxGTK_CONV( helpString
), "");
757 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
759 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
762 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
764 tool
->SetNormalBitmap(bitmap
);
765 tool
->SetImage(tool
->GetBitmap());
769 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
771 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
774 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
776 tool
->SetDisabledBitmap(bitmap
);
777 tool
->SetImage(tool
->GetBitmap());
781 // ----------------------------------------------------------------------------
782 // wxToolBar idle handling
783 // ----------------------------------------------------------------------------
785 void wxToolBar::OnInternalIdle()
787 // Check if we have to show window now
788 if (GtkShowFromOnIdle()) return;
790 wxCursor cursor
= m_cursor
;
791 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
795 /* I now set the cursor the anew in every OnInternalIdle call
796 as setting the cursor in a parent window also effects the
797 windows above so that checking for the current cursor is
800 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
802 /* if the toolbar is dockable, then m_widget stands for the
803 GtkHandleBox widget, which uses its own window so that we
804 can set the cursor for it. if the toolbar is not dockable,
805 m_widget comes from m_toolbar which uses its parent's
806 window ("windowless windows") and thus we cannot set the
808 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
811 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
814 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
815 node
= node
->GetNext();
817 GtkWidget
*item
= tool
->m_item
;
820 GdkWindow
*window
= item
->window
;
824 gdk_window_set_cursor( window
, cursor
.GetCursor() );
830 if (wxUpdateUIEvent::CanUpdate(this))
831 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
835 // ----------------------------------------------------------------------------
839 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
841 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
844 #endif // wxUSE_TOOLBAR_NATIVE