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 extern GdkFont
*GtkGetDefaultGuiFont();
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
42 extern void wxapp_install_idle_handler();
46 extern bool g_blockEventsOnDrag
;
47 extern wxCursor g_globalCursor
;
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // translate wxWidgets toolbar style flags to GTK orientation and style
54 static void GetGtkStyle(long style
,
55 GtkOrientation
*orient
, GtkToolbarStyle
*gtkStyle
)
57 *orient
= style
& wxTB_VERTICAL
? GTK_ORIENTATION_VERTICAL
58 : GTK_ORIENTATION_HORIZONTAL
;
61 if ( style
& wxTB_TEXT
)
63 *gtkStyle
= style
& wxTB_NOICONS
67 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
71 else // no text, hence we must have the icons or what would we show?
73 *gtkStyle
= GTK_TOOLBAR_ICONS
;
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 class wxToolBarTool
: public wxToolBarToolBase
84 wxToolBarTool(wxToolBar
*tbar
,
86 const wxString
& label
,
87 const wxBitmap
& bitmap1
,
88 const wxBitmap
& bitmap2
,
91 const wxString
& shortHelpString
,
92 const wxString
& longHelpString
)
93 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
94 clientData
, shortHelpString
, longHelpString
)
99 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
100 : wxToolBarToolBase(tbar
, control
)
105 // is this a radio button?
107 // unlike GetKind(), can be called for any kind of tools, not just buttons
108 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
110 // this is only called for the normal buttons, i.e. not separators nor
112 GtkToolbarChildType
GetGtkChildType() const
117 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
120 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
123 wxFAIL_MSG( _T("unknown toolbar child type") );
127 return GTK_TOOLBAR_CHILD_BUTTON
;
131 void SetPixmap(const wxBitmap
& bitmap
)
135 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
138 if (bitmap
.HasPixbuf())
139 gtk_image_set_from_pixbuf( GTK_IMAGE(m_pixmap
), bitmap
.GetPixbuf() );
141 #endif // !__WXGTK20__
142 gtk_pixmap_set( GTK_PIXMAP(m_pixmap
), bitmap
.GetPixmap(), mask
);
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
159 // ============================================================================
161 // ============================================================================
163 //-----------------------------------------------------------------------------
164 // "clicked" (internal from gtk_toolbar)
165 //-----------------------------------------------------------------------------
167 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
168 wxToolBarTool
*tool
)
171 wxapp_install_idle_handler();
173 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
175 if (tbar
->m_blockEvent
) return;
177 if (g_blockEventsOnDrag
) return;
178 if (!tool
->IsEnabled()) return;
180 if (tool
->CanBeToggled())
184 tool
->SetPixmap(tool
->GetBitmap());
186 if ( tool
->IsRadio() && !tool
->IsToggled() )
188 // radio button went up, don't report this as a wxWin event
193 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
198 tool
->SetPixmap(tool
->GetBitmap());
202 //-----------------------------------------------------------------------------
203 // "enter_notify_event" / "leave_notify_event"
204 //-----------------------------------------------------------------------------
206 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
207 GdkEventCrossing
*gdk_event
,
208 wxToolBarTool
*tool
)
210 if (g_isIdle
) wxapp_install_idle_handler();
212 if (g_blockEventsOnDrag
) return TRUE
;
214 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
217 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
218 tb
->OnMouseEnter( tool
->GetId() );
220 tb
->OnMouseEnter( -1 );
225 //-----------------------------------------------------------------------------
226 // InsertChild callback for wxToolBar
227 //-----------------------------------------------------------------------------
229 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
230 wxWindow
* WXUNUSED(child
) )
232 // we don't do anything here
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 void wxToolBarTool::Init()
242 m_pixmap
= (GtkWidget
*)NULL
;
245 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
246 const wxString
& text
,
247 const wxBitmap
& bitmap1
,
248 const wxBitmap
& bitmap2
,
250 wxObject
*clientData
,
251 const wxString
& shortHelpString
,
252 const wxString
& longHelpString
)
254 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
255 clientData
, shortHelpString
, longHelpString
);
258 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
260 return new wxToolBarTool(this, control
);
263 //-----------------------------------------------------------------------------
264 // wxToolBar construction
265 //-----------------------------------------------------------------------------
267 void wxToolBar::Init()
269 m_toolbar
= (GtkToolbar
*)NULL
;
270 m_blockEvent
= FALSE
;
272 m_defaultHeight
= 32;
275 wxToolBar::~wxToolBar()
279 bool wxToolBar::Create( wxWindow
*parent
,
284 const wxString
& name
)
287 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
289 if ( !PreCreation( parent
, pos
, size
) ||
290 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
292 wxFAIL_MSG( wxT("wxToolBar creation failed") );
298 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
301 // Doesn't work this way.
302 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
303 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
305 GtkOrientation orient
;
306 GtkToolbarStyle gtkStyle
;
307 GetGtkStyle(style
, &orient
, >kStyle
);
309 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new(orient
, gtkStyle
) );
312 SetToolSeparation(7);
314 if (style
& wxTB_DOCKABLE
)
316 m_widget
= gtk_handle_box_new();
317 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
318 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
320 if (style
& wxTB_FLAT
)
321 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
325 m_widget
= gtk_event_box_new();
326 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
327 ConnectWidget( m_widget
);
328 gtk_widget_show(GTK_WIDGET(m_toolbar
));
331 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
333 // FIXME: there is no such function for toolbars in 2.0
335 if (style
& wxTB_FLAT
)
336 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
339 m_parent
->DoAddChild( this );
346 void wxToolBar::GtkSetStyle()
348 GtkOrientation orient
;
349 GtkToolbarStyle style
;
350 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
352 gtk_toolbar_set_orientation(m_toolbar
, orient
);
353 gtk_toolbar_set_style(m_toolbar
, style
);
356 void wxToolBar::SetWindowStyleFlag( long style
)
358 wxToolBarBase::SetWindowStyleFlag(style
);
364 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
366 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
369 // if we have inserted a space before all the tools we must change the GTK
371 size_t posGtk
= m_xMargin
> 1 ? pos
+ 1 : pos
;
376 if ( tool
->IsButton() )
378 if ( !HasFlag(wxTB_NOICONS
) )
380 wxBitmap bitmap
= tool
->GetNormalBitmap();
382 wxCHECK_MSG( bitmap
.Ok(), FALSE
,
383 wxT("invalid bitmap for wxToolBar icon") );
385 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
,
386 wxT("wxToolBar doesn't support GdkBitmap") );
388 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
,
389 wxT("wxToolBar::Add needs a wxBitmap") );
391 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
395 if (bitmap
.HasPixbuf())
397 tool_pixmap
= gtk_image_new();
398 tool
->m_pixmap
= tool_pixmap
;
399 tool
->SetPixmap(bitmap
);
404 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
406 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
407 if ( bitmap
.GetMask() )
408 mask
= bitmap
.GetMask()->GetBitmap();
410 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
411 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
414 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
416 tool
->m_pixmap
= tool_pixmap
;
420 switch ( tool
->GetStyle() )
422 case wxTOOL_STYLE_BUTTON
:
423 // for a radio button we need the widget which starts the radio
424 // group it belongs to, i.e. the first radio button immediately
425 // preceding this one
427 GtkWidget
*widget
= NULL
;
429 if ( tool
->IsRadio() )
431 wxToolBarToolsList::compatibility_iterator node
432 = wxToolBarToolsList::compatibility_iterator();
433 if ( pos
) node
= m_tools
.Item(pos
- 1);
437 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
438 if ( !tool
->IsRadio() )
441 widget
= tool
->m_item
;
443 node
= node
->GetPrevious();
448 // this is the first button in the radio button group,
449 // it will be toggled automatically by GTK so bring the
450 // internal flag in sync
455 tool
->m_item
= gtk_toolbar_insert_element
458 tool
->GetGtkChildType(),
460 tool
->GetLabel().empty()
462 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
463 tool
->GetShortHelp().empty()
465 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
466 "", // tooltip_private_text (?)
468 (GtkSignalFunc
)gtk_toolbar_callback
,
475 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
480 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
481 "enter_notify_event",
482 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
484 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
485 "leave_notify_event",
486 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
491 case wxTOOL_STYLE_SEPARATOR
:
492 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
497 case wxTOOL_STYLE_CONTROL
:
498 gtk_toolbar_insert_widget(
500 tool
->GetControl()->m_widget
,
509 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
511 m_width
= req
.width
+ m_xMargin
;
512 m_height
= req
.height
+ 2*m_yMargin
;
513 InvalidateBestSize();
518 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
520 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
522 switch ( tool
->GetStyle() )
524 case wxTOOL_STYLE_CONTROL
:
525 tool
->GetControl()->Destroy();
528 case wxTOOL_STYLE_BUTTON
:
529 gtk_widget_destroy( tool
->m_item
);
532 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
535 InvalidateBestSize();
539 // ----------------------------------------------------------------------------
540 // wxToolBar tools state
541 // ----------------------------------------------------------------------------
543 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
545 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
549 gtk_widget_set_sensitive( tool
->m_item
, enable
);
553 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
555 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
557 GtkWidget
*item
= tool
->m_item
;
558 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
560 tool
->SetPixmap(tool
->GetBitmap());
564 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
566 m_blockEvent
= FALSE
;
570 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
571 bool WXUNUSED(toggle
))
573 // VZ: absolutely no idea about how to do it
574 wxFAIL_MSG( _T("not implemented") );
577 // ----------------------------------------------------------------------------
578 // wxToolBar geometry
579 // ----------------------------------------------------------------------------
581 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
582 wxCoord
WXUNUSED(y
)) const
584 // VZ: GTK+ doesn't seem to have such thing
585 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
587 return (wxToolBarToolBase
*)NULL
;
590 void wxToolBar::SetMargins( int x
, int y
)
592 wxCHECK_RET( GetToolsCount() == 0,
593 wxT("wxToolBar::SetMargins must be called before adding tools.") );
597 gtk_toolbar_append_space( m_toolbar
); // oh well
604 void wxToolBar::SetToolSeparation( int separation
)
606 // FIXME: this function disappeared
608 gtk_toolbar_set_space_size( m_toolbar
, separation
);
611 m_toolSeparation
= separation
;
614 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
616 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
620 (void)tool
->SetShortHelp(helpString
);
621 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
622 wxGTK_CONV( helpString
), "");
626 // ----------------------------------------------------------------------------
627 // wxToolBar idle handling
628 // ----------------------------------------------------------------------------
630 void wxToolBar::OnInternalIdle()
632 wxCursor cursor
= m_cursor
;
633 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
637 /* I now set the cursor the anew in every OnInternalIdle call
638 as setting the cursor in a parent window also effects the
639 windows above so that checking for the current cursor is
642 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
644 /* if the toolbar is dockable, then m_widget stands for the
645 GtkHandleBox widget, which uses its own window so that we
646 can set the cursor for it. if the toolbar is not dockable,
647 m_widget comes from m_toolbar which uses its parent's
648 window ("windowless windows") and thus we cannot set the
650 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
653 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
656 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
657 node
= node
->GetNext();
659 GtkWidget
*item
= tool
->m_item
;
662 GdkWindow
*window
= item
->window
;
666 gdk_window_set_cursor( window
, cursor
.GetCursor() );
672 if (wxUpdateUIEvent::CanUpdate(this))
673 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
677 // ----------------------------------------------------------------------------
681 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
684 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
686 wxVisualAttributes attr
;
687 GtkWidget
* widget
= gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL
, GTK_TOOLBAR_BOTH
);
688 attr
= GetDefaultAttributesFromGTKWidget(widget
);
689 gtk_widget_destroy(widget
);
694 #endif // wxUSE_TOOLBAR_NATIVE