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 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #include "wx/toolbar.h"
24 #if wxUSE_TOOLBAR_NATIVE
28 // FIXME: Use GtkImage instead of GtkPixmap. Use the new toolbar API for when gtk runtime is new enough?
29 // Beware that the new and old toolbar API may not be mixed in usage.
30 #include <gtk/gtkversion.h>
31 #ifdef GTK_DISABLE_DEPRECATED
32 #undef GTK_DISABLE_DEPRECATED
35 #include "wx/gtk/private.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
42 extern bool g_blockEventsOnDrag
;
43 extern wxCursor g_globalCursor
;
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // translate wxWidgets toolbar style flags to GTK orientation and style
50 static void GetGtkStyle(long style
,
51 GtkOrientation
*orient
, GtkToolbarStyle
*gtkStyle
)
53 *orient
= style
& wxTB_VERTICAL
? GTK_ORIENTATION_VERTICAL
54 : GTK_ORIENTATION_HORIZONTAL
;
57 if ( style
& wxTB_TEXT
)
59 *gtkStyle
= style
& wxTB_NOICONS
62 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
65 else // no text, hence we must have the icons or what would we show?
67 *gtkStyle
= GTK_TOOLBAR_ICONS
;
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 class wxToolBarTool
: public wxToolBarToolBase
78 wxToolBarTool(wxToolBar
*tbar
,
80 const wxString
& label
,
81 const wxBitmap
& bitmap1
,
82 const wxBitmap
& bitmap2
,
85 const wxString
& shortHelpString
,
86 const wxString
& longHelpString
)
87 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
88 clientData
, shortHelpString
, longHelpString
)
93 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
94 : wxToolBarToolBase(tbar
, control
)
99 // is this a radio button?
101 // unlike GetKind(), can be called for any kind of tools, not just buttons
102 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
104 // this is only called for the normal buttons, i.e. not separators nor
106 GtkToolbarChildType
GetGtkChildType() const
111 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
114 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
117 wxFAIL_MSG( _T("unknown toolbar child type") );
121 return GTK_TOOLBAR_CHILD_BUTTON
;
125 void SetPixmap(const wxBitmap
& bitmap
)
129 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
131 if (bitmap
.HasPixbuf())
132 gtk_image_set_from_pixbuf( GTK_IMAGE(m_pixmap
), bitmap
.GetPixbuf() );
134 gtk_pixmap_set( GTK_PIXMAP(m_pixmap
), bitmap
.GetPixmap(), mask
);
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
151 // ============================================================================
153 // ============================================================================
155 //-----------------------------------------------------------------------------
156 // "clicked" (internal from gtk_toolbar)
157 //-----------------------------------------------------------------------------
160 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
161 wxToolBarTool
*tool
)
164 wxapp_install_idle_handler();
166 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
168 if (tbar
->m_blockEvent
) return;
170 if (g_blockEventsOnDrag
) return;
171 if (!tool
->IsEnabled()) return;
173 if (tool
->CanBeToggled())
177 tool
->SetPixmap(tool
->GetBitmap());
179 if ( tool
->IsRadio() && !tool
->IsToggled() )
181 // radio button went up, don't report this as a wxWin event
186 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
191 tool
->SetPixmap(tool
->GetBitmap());
196 //-----------------------------------------------------------------------------
197 // "enter_notify_event" / "leave_notify_event"
198 //-----------------------------------------------------------------------------
201 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
202 GdkEventCrossing
*gdk_event
,
203 wxToolBarTool
*tool
)
205 if (g_isIdle
) wxapp_install_idle_handler();
207 if (g_blockEventsOnDrag
) return TRUE
;
209 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
212 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
213 tb
->OnMouseEnter( tool
->GetId() );
215 tb
->OnMouseEnter( -1 );
221 //-----------------------------------------------------------------------------
222 // InsertChild callback for wxToolBar
223 //-----------------------------------------------------------------------------
225 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
226 wxWindow
* WXUNUSED(child
) )
228 // we don't do anything here
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 void wxToolBarTool::Init()
238 m_pixmap
= (GtkWidget
*)NULL
;
241 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
242 const wxString
& text
,
243 const wxBitmap
& bitmap1
,
244 const wxBitmap
& bitmap2
,
246 wxObject
*clientData
,
247 const wxString
& shortHelpString
,
248 const wxString
& longHelpString
)
250 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
251 clientData
, shortHelpString
, longHelpString
);
254 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
256 return new wxToolBarTool(this, control
);
259 //-----------------------------------------------------------------------------
260 // wxToolBar construction
261 //-----------------------------------------------------------------------------
263 void wxToolBar::Init()
265 m_toolbar
= (GtkToolbar
*)NULL
;
266 m_blockEvent
= false;
268 m_defaultHeight
= 32;
271 wxToolBar::~wxToolBar()
275 bool wxToolBar::Create( wxWindow
*parent
,
280 const wxString
& name
)
283 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
285 if ( !PreCreation( parent
, pos
, size
) ||
286 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
288 wxFAIL_MSG( wxT("wxToolBar creation failed") );
293 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
296 // Doesn't work this way.
297 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
298 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
300 SetToolSeparation(7);
302 if (style
& wxTB_DOCKABLE
)
304 m_widget
= gtk_handle_box_new();
305 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
306 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
308 if (style
& wxTB_FLAT
)
309 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
313 m_widget
= gtk_event_box_new();
314 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
315 ConnectWidget( m_widget
);
316 gtk_widget_show(GTK_WIDGET(m_toolbar
));
319 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
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 void wxToolBar::GtkSetStyle()
336 GtkOrientation orient
;
337 GtkToolbarStyle style
;
338 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
340 gtk_toolbar_set_orientation(m_toolbar
, orient
);
341 gtk_toolbar_set_style(m_toolbar
, style
);
344 void wxToolBar::SetWindowStyleFlag( long style
)
346 wxToolBarBase::SetWindowStyleFlag(style
);
352 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
354 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
358 if ( tool
->IsButton() )
360 if ( !HasFlag(wxTB_NOICONS
) )
362 wxBitmap bitmap
= tool
->GetNormalBitmap();
364 wxCHECK_MSG( bitmap
.Ok(), false,
365 wxT("invalid bitmap for wxToolBar icon") );
367 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, false,
368 wxT("wxToolBar doesn't support GdkBitmap") );
370 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, false,
371 wxT("wxToolBar::Add needs a wxBitmap") );
373 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
376 if (bitmap
.HasPixbuf())
378 tool_pixmap
= gtk_image_new();
379 tool
->m_pixmap
= tool_pixmap
;
380 tool
->SetPixmap(bitmap
);
384 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
386 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
387 if ( bitmap
.GetMask() )
388 mask
= bitmap
.GetMask()->GetBitmap();
390 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
391 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
394 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
396 tool
->m_pixmap
= tool_pixmap
;
400 switch ( tool
->GetStyle() )
402 case wxTOOL_STYLE_BUTTON
:
403 // for a radio button we need the widget which starts the radio
404 // group it belongs to, i.e. the first radio button immediately
405 // preceding this one
407 GtkWidget
*widget
= NULL
;
409 if ( tool
->IsRadio() )
411 wxToolBarToolsList::compatibility_iterator node
412 = wxToolBarToolsList::compatibility_iterator();
414 node
= m_tools
.Item(pos
- 1);
418 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
419 if ( !toolNext
->IsRadio() )
422 widget
= toolNext
->m_item
;
424 node
= node
->GetPrevious();
429 // this is the first button in the radio button group,
430 // it will be toggled automatically by GTK so bring the
431 // internal flag in sync
436 tool
->m_item
= gtk_toolbar_insert_element
439 tool
->GetGtkChildType(),
441 tool
->GetLabel().empty()
443 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
444 tool
->GetShortHelp().empty()
446 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
447 "", // tooltip_private_text (?)
449 (GtkSignalFunc
)gtk_toolbar_callback
,
456 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
461 g_signal_connect (tool
->m_item
, "enter_notify_event",
462 G_CALLBACK (gtk_toolbar_tool_callback
),
464 g_signal_connect (tool
->m_item
, "leave_notify_event",
465 G_CALLBACK (gtk_toolbar_tool_callback
),
470 case wxTOOL_STYLE_SEPARATOR
:
471 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
476 case wxTOOL_STYLE_CONTROL
:
477 gtk_toolbar_insert_widget(
479 tool
->GetControl()->m_widget
,
488 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
490 m_width
= req
.width
+ m_xMargin
;
491 m_height
= req
.height
+ 2*m_yMargin
;
492 InvalidateBestSize();
497 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
499 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
501 switch ( tool
->GetStyle() )
503 case wxTOOL_STYLE_CONTROL
:
504 tool
->GetControl()->Destroy();
507 case wxTOOL_STYLE_BUTTON
:
508 gtk_widget_destroy( tool
->m_item
);
511 case wxTOOL_STYLE_SEPARATOR
:
512 gtk_toolbar_remove_space( m_toolbar
, pos
);
516 InvalidateBestSize();
520 // ----------------------------------------------------------------------------
521 // wxToolBar tools state
522 // ----------------------------------------------------------------------------
524 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
526 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
530 gtk_widget_set_sensitive( tool
->m_item
, enable
);
534 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
536 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
538 GtkWidget
*item
= tool
->m_item
;
539 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
541 tool
->SetPixmap(tool
->GetBitmap());
545 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item
), toggle
);
547 m_blockEvent
= false;
551 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
552 bool WXUNUSED(toggle
))
554 // VZ: absolutely no idea about how to do it
555 wxFAIL_MSG( _T("not implemented") );
558 // ----------------------------------------------------------------------------
559 // wxToolBar geometry
560 // ----------------------------------------------------------------------------
562 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
563 wxCoord
WXUNUSED(y
)) const
565 // VZ: GTK+ doesn't seem to have such thing
566 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
568 return (wxToolBarToolBase
*)NULL
;
571 void wxToolBar::SetMargins( int x
, int y
)
573 wxCHECK_RET( GetToolsCount() == 0,
574 wxT("wxToolBar::SetMargins must be called before adding tools.") );
580 void wxToolBar::SetToolSeparation( int separation
)
582 // FIXME: this function disappeared
584 gtk_toolbar_set_space_size( m_toolbar
, separation
);
587 m_toolSeparation
= separation
;
590 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
592 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
596 (void)tool
->SetShortHelp(helpString
);
597 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
598 wxGTK_CONV( helpString
), "");
602 // ----------------------------------------------------------------------------
603 // wxToolBar idle handling
604 // ----------------------------------------------------------------------------
606 void wxToolBar::OnInternalIdle()
608 wxCursor cursor
= m_cursor
;
609 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
613 /* I now set the cursor the anew in every OnInternalIdle call
614 as setting the cursor in a parent window also effects the
615 windows above so that checking for the current cursor is
618 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
620 /* if the toolbar is dockable, then m_widget stands for the
621 GtkHandleBox widget, which uses its own window so that we
622 can set the cursor for it. if the toolbar is not dockable,
623 m_widget comes from m_toolbar which uses its parent's
624 window ("windowless windows") and thus we cannot set the
626 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
629 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
632 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
633 node
= node
->GetNext();
635 GtkWidget
*item
= tool
->m_item
;
638 GdkWindow
*window
= item
->window
;
642 gdk_window_set_cursor( window
, cursor
.GetCursor() );
648 if (wxUpdateUIEvent::CanUpdate(this))
649 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
653 // ----------------------------------------------------------------------------
657 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
659 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
662 #endif // wxUSE_TOOLBAR_NATIVE