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"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
44 extern bool g_blockEventsOnDrag
;
45 extern wxCursor g_globalCursor
;
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // translate wxWidgets toolbar style flags to GTK orientation and style
52 static void GetGtkStyle(long style
,
53 GtkOrientation
*orient
, GtkToolbarStyle
*gtkStyle
)
55 *orient
= ( style
& wxTB_LEFT
|| style
& wxTB_RIGHT
) ? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
;
58 if ( style
& wxTB_TEXT
)
60 *gtkStyle
= style
& wxTB_NOICONS
63 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
66 else // no text, hence we must have the icons or what would we show?
68 *gtkStyle
= GTK_TOOLBAR_ICONS
;
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 class wxToolBarTool
: public wxToolBarToolBase
79 wxToolBarTool(wxToolBar
*tbar
,
81 const wxString
& label
,
82 const wxBitmap
& bitmap1
,
83 const wxBitmap
& bitmap2
,
86 const wxString
& shortHelpString
,
87 const wxString
& longHelpString
)
88 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
89 clientData
, shortHelpString
, longHelpString
)
94 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
95 : wxToolBarToolBase(tbar
, control
, label
)
100 // is this a radio button?
102 // unlike GetKind(), can be called for any kind of tools, not just buttons
103 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
105 // this is only called for the normal buttons, i.e. not separators nor
107 GtkToolbarChildType
GetGtkChildType() const
112 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
115 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
118 wxFAIL_MSG( _T("unknown toolbar child type") );
122 return GTK_TOOLBAR_CHILD_BUTTON
;
126 void SetImage(const wxBitmap
& bitmap
)
130 // setting from pixmap doesn't seem to work right, but pixbuf works well
131 gtk_image_set_from_pixbuf((GtkImage
*)m_image
, bitmap
.GetPixbuf());
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
148 // ============================================================================
150 // ============================================================================
152 //-----------------------------------------------------------------------------
153 // "clicked" (internal from gtk_toolbar)
154 //-----------------------------------------------------------------------------
157 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
158 wxToolBarTool
*tool
)
160 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
162 if (tbar
->m_blockEvent
) return;
164 if (g_blockEventsOnDrag
) return;
165 if (!tool
->IsEnabled()) return;
167 if (tool
->CanBeToggled())
171 tool
->SetImage(tool
->GetBitmap());
173 if ( tool
->IsRadio() && !tool
->IsToggled() )
175 // radio button went up, don't report this as a wxWin event
180 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
185 tool
->SetImage(tool
->GetBitmap());
190 //-----------------------------------------------------------------------------
192 //-----------------------------------------------------------------------------
194 static gboolean
gtk_toolbar_tool_rclick_callback(GtkWidget
*WXUNUSED(widget
),
195 GdkEventButton
*event
,
196 wxToolBarToolBase
*tool
)
198 if (event
->button
!= 3)
201 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
203 if (tbar
->m_blockEvent
) return TRUE
;
205 if (g_blockEventsOnDrag
) return TRUE
;
206 if (!tool
->IsEnabled()) return TRUE
;
208 tbar
->OnRightClick( tool
->GetId(), (int)event
->x
, (int)event
->y
);
214 //-----------------------------------------------------------------------------
215 // "enter_notify_event" / "leave_notify_event"
216 //-----------------------------------------------------------------------------
219 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
220 GdkEventCrossing
*gdk_event
,
221 wxToolBarTool
*tool
)
223 if (g_blockEventsOnDrag
) return TRUE
;
225 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
228 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
229 tb
->OnMouseEnter( tool
->GetId() );
231 tb
->OnMouseEnter( -1 );
239 void gtktoolwidget_size_callback( GtkWidget
*widget
,
240 GtkAllocation
*alloc
,
243 // this shouldn't happen...
244 if (win
->GetParent()->m_wxwindow
) return;
246 wxSize size
= win
->GetEffectiveMinSize();
247 if (size
.y
!= alloc
->height
)
249 GtkAllocation alloc2
;
251 alloc2
.y
= (alloc
->height
- size
.y
+ 3) / 2;
252 alloc2
.width
= alloc
->width
;
253 alloc2
.height
= size
.y
;
254 gtk_widget_size_allocate( widget
, &alloc2
);
258 //-----------------------------------------------------------------------------
259 // InsertChild callback for wxToolBar
260 //-----------------------------------------------------------------------------
262 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
263 wxWindow
* WXUNUSED(child
) )
265 // we don't do anything here
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 void wxToolBarTool::Init()
278 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
279 const wxString
& text
,
280 const wxBitmap
& bitmap1
,
281 const wxBitmap
& bitmap2
,
283 wxObject
*clientData
,
284 const wxString
& shortHelpString
,
285 const wxString
& longHelpString
)
287 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
288 clientData
, shortHelpString
, longHelpString
);
292 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
294 return new wxToolBarTool(this, control
, label
);
297 //-----------------------------------------------------------------------------
298 // wxToolBar construction
299 //-----------------------------------------------------------------------------
301 void wxToolBar::Init()
303 m_toolbar
= (GtkToolbar
*)NULL
;
304 m_blockEvent
= false;
306 m_defaultHeight
= 32;
309 wxToolBar::~wxToolBar()
313 bool wxToolBar::Create( wxWindow
*parent
,
318 const wxString
& name
)
320 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
322 if ( !PreCreation( parent
, pos
, size
) ||
323 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
325 wxFAIL_MSG( wxT("wxToolBar creation failed") );
332 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
335 // Doesn't work this way.
336 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
337 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
339 SetToolSeparation(7);
341 if (style
& wxTB_DOCKABLE
)
343 m_widget
= gtk_handle_box_new();
344 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
345 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
347 if (style
& wxTB_FLAT
)
348 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
352 m_widget
= gtk_event_box_new();
353 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
354 ConnectWidget( m_widget
);
355 gtk_widget_show(GTK_WIDGET(m_toolbar
));
358 // FIXME: there is no such function for toolbars in 2.0
360 if (style
& wxTB_FLAT
)
361 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
364 m_parent
->DoAddChild( this );
371 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& windows
) const
373 return GTK_WIDGET(m_toolbar
)->window
;
376 void wxToolBar::GtkSetStyle()
378 GtkOrientation orient
;
379 GtkToolbarStyle style
;
380 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
382 gtk_toolbar_set_orientation(m_toolbar
, orient
);
383 gtk_toolbar_set_style(m_toolbar
, style
);
384 gtk_toolbar_set_tooltips(m_toolbar
, !(style
& wxTB_NO_TOOLTIPS
));
387 void wxToolBar::SetWindowStyleFlag( long style
)
389 wxToolBarBase::SetWindowStyleFlag(style
);
395 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
397 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
399 if ( tool
->IsButton() )
401 if ( !HasFlag(wxTB_NOICONS
) )
403 wxBitmap bitmap
= tool
->GetNormalBitmap();
405 wxCHECK_MSG( bitmap
.Ok(), false,
406 wxT("invalid bitmap for wxToolBar icon") );
408 tool
->m_image
= gtk_image_new();
409 tool
->SetImage(bitmap
);
411 gtk_misc_set_alignment((GtkMisc
*)tool
->m_image
, 0.5, 0.5);
415 const int posGtk
= int(pos
);
417 switch ( tool
->GetStyle() )
419 case wxTOOL_STYLE_BUTTON
:
420 // for a radio button we need the widget which starts the radio
421 // group it belongs to, i.e. the first radio button immediately
422 // preceding this one
424 GtkWidget
*widget
= NULL
;
426 if ( tool
->IsRadio() )
428 wxToolBarToolsList::compatibility_iterator node
429 = wxToolBarToolsList::compatibility_iterator();
431 node
= m_tools
.Item(pos
- 1);
435 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
436 if ( !toolNext
->IsRadio() )
439 widget
= toolNext
->m_item
;
441 node
= node
->GetPrevious();
446 // this is the first button in the radio button group,
447 // it will be toggled automatically by GTK so bring the
448 // internal flag in sync
453 tool
->m_item
= gtk_toolbar_insert_element
456 tool
->GetGtkChildType(),
458 tool
->GetLabel().empty()
460 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
461 tool
->GetShortHelp().empty()
463 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
464 "", // tooltip_private_text (?)
466 (GtkSignalFunc
)gtk_toolbar_callback
,
471 wxCHECK_MSG(tool
->m_item
!= NULL
, false, _T("gtk_toolbar_insert_element() failed"));
473 g_signal_connect (tool
->m_item
, "enter_notify_event",
474 G_CALLBACK (gtk_toolbar_tool_callback
),
476 g_signal_connect (tool
->m_item
, "leave_notify_event",
477 G_CALLBACK (gtk_toolbar_tool_callback
),
479 g_signal_connect(tool
->m_item
, "button-press-event",
480 G_CALLBACK (gtk_toolbar_tool_rclick_callback
),
485 case wxTOOL_STYLE_SEPARATOR
:
486 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
491 case wxTOOL_STYLE_CONTROL
:
492 gtk_toolbar_insert_widget(
494 tool
->GetControl()->m_widget
,
500 // connect after in order to correct size_allocate events
501 g_signal_connect_after (tool
->GetControl()->m_widget
, "size_allocate",
502 G_CALLBACK (gtktoolwidget_size_callback
), tool
->GetControl());
508 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
510 m_width
= req
.width
+ m_xMargin
;
511 m_height
= req
.height
+ 2*m_yMargin
;
512 InvalidateBestSize();
517 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
519 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
521 switch ( tool
->GetStyle() )
523 case wxTOOL_STYLE_CONTROL
:
524 tool
->GetControl()->Destroy();
527 case wxTOOL_STYLE_BUTTON
:
528 gtk_widget_destroy( tool
->m_item
);
531 case wxTOOL_STYLE_SEPARATOR
:
532 gtk_toolbar_remove_space( m_toolbar
, pos
);
536 InvalidateBestSize();
540 // ----------------------------------------------------------------------------
541 // wxToolBar tools state
542 // ----------------------------------------------------------------------------
544 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
546 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
550 gtk_widget_set_sensitive( tool
->m_item
, enable
);
554 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
556 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
558 GtkWidget
*item
= tool
->m_item
;
559 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
561 tool
->SetImage(tool
->GetBitmap());
565 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item
), toggle
);
567 m_blockEvent
= false;
571 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
572 bool WXUNUSED(toggle
))
574 // VZ: absolutely no idea about how to do it
575 wxFAIL_MSG( _T("not implemented") );
578 // ----------------------------------------------------------------------------
579 // wxToolBar geometry
580 // ----------------------------------------------------------------------------
582 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
583 wxCoord
WXUNUSED(y
)) const
585 // VZ: GTK+ doesn't seem to have such thing
586 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
588 return (wxToolBarToolBase
*)NULL
;
591 void wxToolBar::SetMargins( int x
, int y
)
593 wxCHECK_RET( GetToolsCount() == 0,
594 wxT("wxToolBar::SetMargins must be called before adding tools.") );
600 void wxToolBar::SetToolSeparation( int separation
)
602 // FIXME: this function disappeared
604 gtk_toolbar_set_space_size( m_toolbar
, separation
);
607 m_toolSeparation
= separation
;
610 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
612 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
616 (void)tool
->SetShortHelp(helpString
);
617 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
618 wxGTK_CONV( helpString
), "");
622 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
624 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
627 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
629 tool
->SetNormalBitmap(bitmap
);
630 tool
->SetImage(tool
->GetBitmap());
634 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
636 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
639 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
641 tool
->SetDisabledBitmap(bitmap
);
642 tool
->SetImage(tool
->GetBitmap());
646 // ----------------------------------------------------------------------------
647 // wxToolBar idle handling
648 // ----------------------------------------------------------------------------
650 void wxToolBar::OnInternalIdle()
652 // Check if we have to show window now
653 if (GtkShowFromOnIdle()) return;
655 wxCursor cursor
= m_cursor
;
656 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
660 /* I now set the cursor the anew in every OnInternalIdle call
661 as setting the cursor in a parent window also effects the
662 windows above so that checking for the current cursor is
665 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
667 /* if the toolbar is dockable, then m_widget stands for the
668 GtkHandleBox widget, which uses its own window so that we
669 can set the cursor for it. if the toolbar is not dockable,
670 m_widget comes from m_toolbar which uses its parent's
671 window ("windowless windows") and thus we cannot set the
673 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
676 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
679 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
680 node
= node
->GetNext();
682 GtkWidget
*item
= tool
->m_item
;
685 GdkWindow
*window
= item
->window
;
689 gdk_window_set_cursor( window
, cursor
.GetCursor() );
695 if (wxUpdateUIEvent::CanUpdate(this))
696 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
700 // ----------------------------------------------------------------------------
704 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
706 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
709 #endif // wxUSE_TOOLBAR_NATIVE