1 /////////////////////////////////////////////////////////////////////////////
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma implementation "tbargtk.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
26 #include "wx/toolbar.h"
28 #if wxUSE_TOOLBAR_NATIVE
33 #include "wx/gtk/private.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
40 extern void wxapp_install_idle_handler();
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
65 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
69 else // no text, hence we must have the icons or what would we show?
71 *gtkStyle
= GTK_TOOLBAR_ICONS
;
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 class wxToolBarTool
: public wxToolBarToolBase
82 wxToolBarTool(wxToolBar
*tbar
,
84 const wxString
& label
,
85 const wxBitmap
& bitmap1
,
86 const wxBitmap
& bitmap2
,
89 const wxString
& shortHelpString
,
90 const wxString
& longHelpString
)
91 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
92 clientData
, shortHelpString
, longHelpString
)
97 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
98 : wxToolBarToolBase(tbar
, control
)
103 // is this a radio button?
105 // unlike GetKind(), can be called for any kind of tools, not just buttons
106 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
108 // this is only called for the normal buttons, i.e. not separators nor
110 GtkToolbarChildType
GetGtkChildType() const
115 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
118 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
121 wxFAIL_MSG( _T("unknown toolbar child type") );
125 return GTK_TOOLBAR_CHILD_BUTTON
;
129 void SetPixmap(const wxBitmap
& bitmap
)
133 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
136 if (bitmap
.HasPixbuf())
137 gtk_image_set_from_pixbuf( GTK_IMAGE(m_pixmap
), bitmap
.GetPixbuf() );
139 #endif // !__WXGTK20__
140 gtk_pixmap_set( GTK_PIXMAP(m_pixmap
), bitmap
.GetPixmap(), mask
);
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
157 // ============================================================================
159 // ============================================================================
161 //-----------------------------------------------------------------------------
162 // "clicked" (internal from gtk_toolbar)
163 //-----------------------------------------------------------------------------
166 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
167 wxToolBarTool
*tool
)
170 wxapp_install_idle_handler();
172 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
174 if (tbar
->m_blockEvent
) return;
176 if (g_blockEventsOnDrag
) return;
177 if (!tool
->IsEnabled()) return;
179 if (tool
->CanBeToggled())
183 tool
->SetPixmap(tool
->GetBitmap());
185 if ( tool
->IsRadio() && !tool
->IsToggled() )
187 // radio button went up, don't report this as a wxWin event
192 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
197 tool
->SetPixmap(tool
->GetBitmap());
202 //-----------------------------------------------------------------------------
203 // "enter_notify_event" / "leave_notify_event"
204 //-----------------------------------------------------------------------------
207 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
208 GdkEventCrossing
*gdk_event
,
209 wxToolBarTool
*tool
)
211 if (g_isIdle
) wxapp_install_idle_handler();
213 if (g_blockEventsOnDrag
) return TRUE
;
215 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
218 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
219 tb
->OnMouseEnter( tool
->GetId() );
221 tb
->OnMouseEnter( -1 );
227 //-----------------------------------------------------------------------------
228 // InsertChild callback for wxToolBar
229 //-----------------------------------------------------------------------------
231 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
232 wxWindow
* WXUNUSED(child
) )
234 // we don't do anything here
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 void wxToolBarTool::Init()
244 m_pixmap
= (GtkWidget
*)NULL
;
247 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
248 const wxString
& text
,
249 const wxBitmap
& bitmap1
,
250 const wxBitmap
& bitmap2
,
252 wxObject
*clientData
,
253 const wxString
& shortHelpString
,
254 const wxString
& longHelpString
)
256 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
257 clientData
, shortHelpString
, longHelpString
);
260 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
262 return new wxToolBarTool(this, control
);
265 //-----------------------------------------------------------------------------
266 // wxToolBar construction
267 //-----------------------------------------------------------------------------
269 void wxToolBar::Init()
271 m_toolbar
= (GtkToolbar
*)NULL
;
272 m_blockEvent
= false;
274 m_defaultHeight
= 32;
277 wxToolBar::~wxToolBar()
281 bool wxToolBar::Create( wxWindow
*parent
,
286 const wxString
& name
)
289 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
291 if ( !PreCreation( parent
, pos
, size
) ||
292 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
294 wxFAIL_MSG( wxT("wxToolBar creation failed") );
300 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
303 // Doesn't work this way.
304 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
305 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
307 GtkOrientation orient
;
308 GtkToolbarStyle gtkStyle
;
309 GetGtkStyle(style
, &orient
, >kStyle
);
311 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new(orient
, gtkStyle
) );
314 SetToolSeparation(7);
316 if (style
& wxTB_DOCKABLE
)
318 m_widget
= gtk_handle_box_new();
319 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
320 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
322 if (style
& wxTB_FLAT
)
323 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
327 m_widget
= gtk_event_box_new();
328 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
329 ConnectWidget( m_widget
);
330 gtk_widget_show(GTK_WIDGET(m_toolbar
));
333 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
335 // FIXME: there is no such function for toolbars in 2.0
337 if (style
& wxTB_FLAT
)
338 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
341 m_parent
->DoAddChild( this );
348 void wxToolBar::GtkSetStyle()
350 GtkOrientation orient
;
351 GtkToolbarStyle style
;
352 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
354 gtk_toolbar_set_orientation(m_toolbar
, orient
);
355 gtk_toolbar_set_style(m_toolbar
, style
);
358 void wxToolBar::SetWindowStyleFlag( long style
)
360 wxToolBarBase::SetWindowStyleFlag(style
);
366 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
368 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
371 // if we have inserted a space before all the tools we must change the GTK
373 size_t posGtk
= m_xMargin
> 1 ? pos
+ 1 : pos
;
378 if ( tool
->IsButton() )
380 if ( !HasFlag(wxTB_NOICONS
) )
382 wxBitmap bitmap
= tool
->GetNormalBitmap();
384 wxCHECK_MSG( bitmap
.Ok(), false,
385 wxT("invalid bitmap for wxToolBar icon") );
387 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, false,
388 wxT("wxToolBar doesn't support GdkBitmap") );
390 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, false,
391 wxT("wxToolBar::Add needs a wxBitmap") );
393 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
397 if (bitmap
.HasPixbuf())
399 tool_pixmap
= gtk_image_new();
400 tool
->m_pixmap
= tool_pixmap
;
401 tool
->SetPixmap(bitmap
);
406 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
408 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
409 if ( bitmap
.GetMask() )
410 mask
= bitmap
.GetMask()->GetBitmap();
412 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
413 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
416 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
418 tool
->m_pixmap
= tool_pixmap
;
422 switch ( tool
->GetStyle() )
424 case wxTOOL_STYLE_BUTTON
:
425 // for a radio button we need the widget which starts the radio
426 // group it belongs to, i.e. the first radio button immediately
427 // preceding this one
429 GtkWidget
*widget
= NULL
;
431 if ( tool
->IsRadio() )
433 wxToolBarToolsList::compatibility_iterator node
434 = wxToolBarToolsList::compatibility_iterator();
435 if ( pos
) node
= m_tools
.Item(pos
- 1);
439 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
440 if ( !tool
->IsRadio() )
443 widget
= tool
->m_item
;
445 node
= node
->GetPrevious();
450 // this is the first button in the radio button group,
451 // it will be toggled automatically by GTK so bring the
452 // internal flag in sync
457 tool
->m_item
= gtk_toolbar_insert_element
460 tool
->GetGtkChildType(),
462 tool
->GetLabel().empty()
464 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
465 tool
->GetShortHelp().empty()
467 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
468 "", // tooltip_private_text (?)
470 (GtkSignalFunc
)gtk_toolbar_callback
,
477 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
482 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
483 "enter_notify_event",
484 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
486 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
487 "leave_notify_event",
488 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
493 case wxTOOL_STYLE_SEPARATOR
:
494 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
499 case wxTOOL_STYLE_CONTROL
:
500 gtk_toolbar_insert_widget(
502 tool
->GetControl()->m_widget
,
511 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
513 m_width
= req
.width
+ m_xMargin
;
514 m_height
= req
.height
+ 2*m_yMargin
;
515 InvalidateBestSize();
520 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
522 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
524 switch ( tool
->GetStyle() )
526 case wxTOOL_STYLE_CONTROL
:
527 tool
->GetControl()->Destroy();
530 case wxTOOL_STYLE_BUTTON
:
531 gtk_widget_destroy( tool
->m_item
);
535 case wxTOOL_STYLE_SEPARATOR
:
536 gtk_toolbar_remove_space( m_toolbar
, pos
);
541 InvalidateBestSize();
545 // ----------------------------------------------------------------------------
546 // wxToolBar tools state
547 // ----------------------------------------------------------------------------
549 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
551 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
555 gtk_widget_set_sensitive( tool
->m_item
, enable
);
559 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
561 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
563 GtkWidget
*item
= tool
->m_item
;
564 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
566 tool
->SetPixmap(tool
->GetBitmap());
570 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
572 m_blockEvent
= false;
576 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
577 bool WXUNUSED(toggle
))
579 // VZ: absolutely no idea about how to do it
580 wxFAIL_MSG( _T("not implemented") );
583 // ----------------------------------------------------------------------------
584 // wxToolBar geometry
585 // ----------------------------------------------------------------------------
587 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
588 wxCoord
WXUNUSED(y
)) const
590 // VZ: GTK+ doesn't seem to have such thing
591 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
593 return (wxToolBarToolBase
*)NULL
;
596 void wxToolBar::SetMargins( int x
, int y
)
598 wxCHECK_RET( GetToolsCount() == 0,
599 wxT("wxToolBar::SetMargins must be called before adding tools.") );
603 gtk_toolbar_append_space( m_toolbar
); // oh well
610 void wxToolBar::SetToolSeparation( int separation
)
612 // FIXME: this function disappeared
614 gtk_toolbar_set_space_size( m_toolbar
, separation
);
617 m_toolSeparation
= separation
;
620 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
622 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
626 (void)tool
->SetShortHelp(helpString
);
627 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
628 wxGTK_CONV( helpString
), "");
632 // ----------------------------------------------------------------------------
633 // wxToolBar idle handling
634 // ----------------------------------------------------------------------------
636 void wxToolBar::OnInternalIdle()
638 wxCursor cursor
= m_cursor
;
639 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
643 /* I now set the cursor the anew in every OnInternalIdle call
644 as setting the cursor in a parent window also effects the
645 windows above so that checking for the current cursor is
648 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
650 /* if the toolbar is dockable, then m_widget stands for the
651 GtkHandleBox widget, which uses its own window so that we
652 can set the cursor for it. if the toolbar is not dockable,
653 m_widget comes from m_toolbar which uses its parent's
654 window ("windowless windows") and thus we cannot set the
656 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
659 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
662 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
663 node
= node
->GetNext();
665 GtkWidget
*item
= tool
->m_item
;
668 GdkWindow
*window
= item
->window
;
672 gdk_window_set_cursor( window
, cursor
.GetCursor() );
678 if (wxUpdateUIEvent::CanUpdate(this))
679 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
683 // ----------------------------------------------------------------------------
687 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
690 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
692 wxVisualAttributes attr
;
693 GtkWidget
* widget
= gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL
, GTK_TOOLBAR_BOTH
);
694 attr
= GetDefaultAttributesFromGTKWidget(widget
);
695 gtk_widget_destroy(widget
);
700 #endif // wxUSE_TOOLBAR_NATIVE