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_VERTICAL
? GTK_ORIENTATION_VERTICAL
56 : GTK_ORIENTATION_HORIZONTAL
;
59 if ( style
& wxTB_TEXT
)
61 *gtkStyle
= style
& wxTB_NOICONS
64 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
67 else // no text, hence we must have the icons or what would we show?
69 *gtkStyle
= GTK_TOOLBAR_ICONS
;
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 class wxToolBarTool
: public wxToolBarToolBase
80 wxToolBarTool(wxToolBar
*tbar
,
82 const wxString
& label
,
83 const wxBitmap
& bitmap1
,
84 const wxBitmap
& bitmap2
,
87 const wxString
& shortHelpString
,
88 const wxString
& longHelpString
)
89 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
90 clientData
, shortHelpString
, longHelpString
)
95 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
96 : wxToolBarToolBase(tbar
, control
)
101 // is this a radio button?
103 // unlike GetKind(), can be called for any kind of tools, not just buttons
104 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
106 // this is only called for the normal buttons, i.e. not separators nor
108 GtkToolbarChildType
GetGtkChildType() const
113 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
116 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
119 wxFAIL_MSG( _T("unknown toolbar child type") );
123 return GTK_TOOLBAR_CHILD_BUTTON
;
127 void SetPixmap(const wxBitmap
& bitmap
)
131 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
133 if (bitmap
.HasPixbuf())
134 gtk_image_set_from_pixbuf( GTK_IMAGE(m_pixmap
), bitmap
.GetPixbuf() );
136 gtk_pixmap_set( GTK_PIXMAP(m_pixmap
), bitmap
.GetPixmap(), mask
);
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
153 // ============================================================================
155 // ============================================================================
157 //-----------------------------------------------------------------------------
158 // "clicked" (internal from gtk_toolbar)
159 //-----------------------------------------------------------------------------
162 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
163 wxToolBarTool
*tool
)
166 wxapp_install_idle_handler();
168 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
170 if (tbar
->m_blockEvent
) return;
172 if (g_blockEventsOnDrag
) return;
173 if (!tool
->IsEnabled()) return;
175 if (tool
->CanBeToggled())
179 tool
->SetPixmap(tool
->GetBitmap());
181 if ( tool
->IsRadio() && !tool
->IsToggled() )
183 // radio button went up, don't report this as a wxWin event
188 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
193 tool
->SetPixmap(tool
->GetBitmap());
198 //-----------------------------------------------------------------------------
199 // "enter_notify_event" / "leave_notify_event"
200 //-----------------------------------------------------------------------------
203 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
204 GdkEventCrossing
*gdk_event
,
205 wxToolBarTool
*tool
)
207 // don't need to install idle handler, its done from "event" signal
209 if (g_blockEventsOnDrag
) return TRUE
;
211 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
214 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
215 tb
->OnMouseEnter( tool
->GetId() );
217 tb
->OnMouseEnter( -1 );
223 //-----------------------------------------------------------------------------
224 // InsertChild callback for wxToolBar
225 //-----------------------------------------------------------------------------
227 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
228 wxWindow
* WXUNUSED(child
) )
230 // we don't do anything here
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 void wxToolBarTool::Init()
240 m_pixmap
= (GtkWidget
*)NULL
;
243 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
244 const wxString
& text
,
245 const wxBitmap
& bitmap1
,
246 const wxBitmap
& bitmap2
,
248 wxObject
*clientData
,
249 const wxString
& shortHelpString
,
250 const wxString
& longHelpString
)
252 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
253 clientData
, shortHelpString
, longHelpString
);
256 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
258 return new wxToolBarTool(this, control
);
261 //-----------------------------------------------------------------------------
262 // wxToolBar construction
263 //-----------------------------------------------------------------------------
265 void wxToolBar::Init()
267 m_toolbar
= (GtkToolbar
*)NULL
;
268 m_blockEvent
= false;
270 m_defaultHeight
= 32;
273 wxToolBar::~wxToolBar()
277 bool wxToolBar::Create( wxWindow
*parent
,
282 const wxString
& name
)
285 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
287 if ( !PreCreation( parent
, pos
, size
) ||
288 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
290 wxFAIL_MSG( wxT("wxToolBar creation failed") );
295 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
298 // Doesn't work this way.
299 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
300 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
302 SetToolSeparation(7);
304 if (style
& wxTB_DOCKABLE
)
306 m_widget
= gtk_handle_box_new();
307 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
308 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
310 if (style
& wxTB_FLAT
)
311 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
315 m_widget
= gtk_event_box_new();
316 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
317 ConnectWidget( m_widget
);
318 gtk_widget_show(GTK_WIDGET(m_toolbar
));
321 // FIXME: there is no such function for toolbars in 2.0
323 if (style
& wxTB_FLAT
)
324 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
327 m_parent
->DoAddChild( this );
334 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& windows
) const
336 return GTK_WIDGET(m_toolbar
)->window
;
339 void wxToolBar::GtkSetStyle()
341 GtkOrientation orient
;
342 GtkToolbarStyle style
;
343 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
345 gtk_toolbar_set_orientation(m_toolbar
, orient
);
346 gtk_toolbar_set_style(m_toolbar
, style
);
347 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), !(style
& wxTB_NO_TOOLTIPS
) );
350 void wxToolBar::SetWindowStyleFlag( long style
)
352 wxToolBarBase::SetWindowStyleFlag(style
);
358 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
360 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
364 if ( tool
->IsButton() )
366 if ( !HasFlag(wxTB_NOICONS
) )
368 wxBitmap bitmap
= tool
->GetNormalBitmap();
370 wxCHECK_MSG( bitmap
.Ok(), false,
371 wxT("invalid bitmap for wxToolBar icon") );
373 wxCHECK_MSG( bitmap
.GetDepth() != 1, false,
374 wxT("wxToolBar doesn't support GdkBitmap") );
376 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, false,
377 wxT("wxToolBar::Add needs a wxBitmap") );
379 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
382 if (bitmap
.HasPixbuf())
384 tool_pixmap
= gtk_image_new();
385 tool
->m_pixmap
= tool_pixmap
;
386 tool
->SetPixmap(bitmap
);
390 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
392 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
393 if ( bitmap
.GetMask() )
394 mask
= bitmap
.GetMask()->GetBitmap();
396 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
397 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
400 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
402 tool
->m_pixmap
= tool_pixmap
;
406 switch ( tool
->GetStyle() )
408 case wxTOOL_STYLE_BUTTON
:
409 // for a radio button we need the widget which starts the radio
410 // group it belongs to, i.e. the first radio button immediately
411 // preceding this one
413 GtkWidget
*widget
= NULL
;
415 if ( tool
->IsRadio() )
417 wxToolBarToolsList::compatibility_iterator node
418 = wxToolBarToolsList::compatibility_iterator();
420 node
= m_tools
.Item(pos
- 1);
424 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
425 if ( !toolNext
->IsRadio() )
428 widget
= toolNext
->m_item
;
430 node
= node
->GetPrevious();
435 // this is the first button in the radio button group,
436 // it will be toggled automatically by GTK so bring the
437 // internal flag in sync
442 tool
->m_item
= gtk_toolbar_insert_element
445 tool
->GetGtkChildType(),
447 tool
->GetLabel().empty()
449 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
450 tool
->GetShortHelp().empty()
452 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
453 "", // tooltip_private_text (?)
455 (GtkSignalFunc
)gtk_toolbar_callback
,
462 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
467 g_signal_connect (tool
->m_item
, "enter_notify_event",
468 G_CALLBACK (gtk_toolbar_tool_callback
),
470 g_signal_connect (tool
->m_item
, "leave_notify_event",
471 G_CALLBACK (gtk_toolbar_tool_callback
),
476 case wxTOOL_STYLE_SEPARATOR
:
477 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
482 case wxTOOL_STYLE_CONTROL
:
483 gtk_toolbar_insert_widget(
485 tool
->GetControl()->m_widget
,
494 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
496 m_width
= req
.width
+ m_xMargin
;
497 m_height
= req
.height
+ 2*m_yMargin
;
498 InvalidateBestSize();
503 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
505 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
507 switch ( tool
->GetStyle() )
509 case wxTOOL_STYLE_CONTROL
:
510 tool
->GetControl()->Destroy();
513 case wxTOOL_STYLE_BUTTON
:
514 gtk_widget_destroy( tool
->m_item
);
517 case wxTOOL_STYLE_SEPARATOR
:
518 gtk_toolbar_remove_space( m_toolbar
, pos
);
522 InvalidateBestSize();
526 // ----------------------------------------------------------------------------
527 // wxToolBar tools state
528 // ----------------------------------------------------------------------------
530 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
532 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
536 gtk_widget_set_sensitive( tool
->m_item
, enable
);
540 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
542 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
544 GtkWidget
*item
= tool
->m_item
;
545 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
547 tool
->SetPixmap(tool
->GetBitmap());
551 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item
), toggle
);
553 m_blockEvent
= false;
557 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
558 bool WXUNUSED(toggle
))
560 // VZ: absolutely no idea about how to do it
561 wxFAIL_MSG( _T("not implemented") );
564 // ----------------------------------------------------------------------------
565 // wxToolBar geometry
566 // ----------------------------------------------------------------------------
568 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
569 wxCoord
WXUNUSED(y
)) const
571 // VZ: GTK+ doesn't seem to have such thing
572 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
574 return (wxToolBarToolBase
*)NULL
;
577 void wxToolBar::SetMargins( int x
, int y
)
579 wxCHECK_RET( GetToolsCount() == 0,
580 wxT("wxToolBar::SetMargins must be called before adding tools.") );
586 void wxToolBar::SetToolSeparation( int separation
)
588 // FIXME: this function disappeared
590 gtk_toolbar_set_space_size( m_toolbar
, separation
);
593 m_toolSeparation
= separation
;
596 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
598 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
602 (void)tool
->SetShortHelp(helpString
);
603 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
604 wxGTK_CONV( helpString
), "");
608 // ----------------------------------------------------------------------------
609 // wxToolBar idle handling
610 // ----------------------------------------------------------------------------
612 void wxToolBar::OnInternalIdle()
614 // Check if we have to show window now
615 if (GtkShowFromOnIdle()) return;
617 wxCursor cursor
= m_cursor
;
618 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
622 /* I now set the cursor the anew in every OnInternalIdle call
623 as setting the cursor in a parent window also effects the
624 windows above so that checking for the current cursor is
627 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
629 /* if the toolbar is dockable, then m_widget stands for the
630 GtkHandleBox widget, which uses its own window so that we
631 can set the cursor for it. if the toolbar is not dockable,
632 m_widget comes from m_toolbar which uses its parent's
633 window ("windowless windows") and thus we cannot set the
635 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
638 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
641 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
642 node
= node
->GetNext();
644 GtkWidget
*item
= tool
->m_item
;
647 GdkWindow
*window
= item
->window
;
651 gdk_window_set_cursor( window
, cursor
.GetCursor() );
657 if (wxUpdateUIEvent::CanUpdate(this))
658 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
662 // ----------------------------------------------------------------------------
666 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
668 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
671 #endif // wxUSE_TOOLBAR_NATIVE