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 SetImage(const wxBitmap
& bitmap
)
131 // setting from pixmap doesn't seem to work right, but pixbuf works well
132 gtk_image_set_from_pixbuf((GtkImage
*)m_image
, bitmap
.GetPixbuf());
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
149 // ============================================================================
151 // ============================================================================
153 //-----------------------------------------------------------------------------
154 // "clicked" (internal from gtk_toolbar)
155 //-----------------------------------------------------------------------------
158 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
159 wxToolBarTool
*tool
)
162 wxapp_install_idle_handler();
164 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
166 if (tbar
->m_blockEvent
) return;
168 if (g_blockEventsOnDrag
) return;
169 if (!tool
->IsEnabled()) return;
171 if (tool
->CanBeToggled())
175 tool
->SetImage(tool
->GetBitmap());
177 if ( tool
->IsRadio() && !tool
->IsToggled() )
179 // radio button went up, don't report this as a wxWin event
184 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
189 tool
->SetImage(tool
->GetBitmap());
194 //-----------------------------------------------------------------------------
195 // "enter_notify_event" / "leave_notify_event"
196 //-----------------------------------------------------------------------------
199 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
200 GdkEventCrossing
*gdk_event
,
201 wxToolBarTool
*tool
)
203 // don't need to install idle handler, its done from "event" signal
205 if (g_blockEventsOnDrag
) return TRUE
;
207 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
210 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
211 tb
->OnMouseEnter( tool
->GetId() );
213 tb
->OnMouseEnter( -1 );
219 //-----------------------------------------------------------------------------
220 // InsertChild callback for wxToolBar
221 //-----------------------------------------------------------------------------
223 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
224 wxWindow
* WXUNUSED(child
) )
226 // we don't do anything here
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 void wxToolBarTool::Init()
239 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
240 const wxString
& text
,
241 const wxBitmap
& bitmap1
,
242 const wxBitmap
& bitmap2
,
244 wxObject
*clientData
,
245 const wxString
& shortHelpString
,
246 const wxString
& longHelpString
)
248 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
249 clientData
, shortHelpString
, longHelpString
);
252 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
254 return new wxToolBarTool(this, control
);
257 //-----------------------------------------------------------------------------
258 // wxToolBar construction
259 //-----------------------------------------------------------------------------
261 void wxToolBar::Init()
263 m_toolbar
= (GtkToolbar
*)NULL
;
264 m_blockEvent
= false;
266 m_defaultHeight
= 32;
269 wxToolBar::~wxToolBar()
273 bool wxToolBar::Create( wxWindow
*parent
,
278 const wxString
& name
)
281 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
283 if ( !PreCreation( parent
, pos
, size
) ||
284 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
286 wxFAIL_MSG( wxT("wxToolBar creation failed") );
291 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
294 // Doesn't work this way.
295 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
296 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
298 SetToolSeparation(7);
300 if (style
& wxTB_DOCKABLE
)
302 m_widget
= gtk_handle_box_new();
303 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
304 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
306 if (style
& wxTB_FLAT
)
307 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
311 m_widget
= gtk_event_box_new();
312 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
313 ConnectWidget( m_widget
);
314 gtk_widget_show(GTK_WIDGET(m_toolbar
));
317 // FIXME: there is no such function for toolbars in 2.0
319 if (style
& wxTB_FLAT
)
320 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
323 m_parent
->DoAddChild( this );
330 GdkWindow
*wxToolBar::GTKGetWindow(wxArrayGdkWindows
& windows
) const
332 return GTK_WIDGET(m_toolbar
)->window
;
335 void wxToolBar::GtkSetStyle()
337 GtkOrientation orient
;
338 GtkToolbarStyle style
;
339 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
341 gtk_toolbar_set_orientation(m_toolbar
, orient
);
342 gtk_toolbar_set_style(m_toolbar
, style
);
343 gtk_toolbar_set_tooltips(m_toolbar
, !(style
& wxTB_NO_TOOLTIPS
));
346 void wxToolBar::SetWindowStyleFlag( long style
)
348 wxToolBarBase::SetWindowStyleFlag(style
);
354 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
356 wxToolBarTool
* tool
= wx_static_cast(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 tool
->m_image
= gtk_image_new();
368 tool
->SetImage(bitmap
);
370 gtk_misc_set_alignment((GtkMisc
*)tool
->m_image
, 0.5, 0.5);
374 const int posGtk
= int(pos
);
376 switch ( tool
->GetStyle() )
378 case wxTOOL_STYLE_BUTTON
:
379 // for a radio button we need the widget which starts the radio
380 // group it belongs to, i.e. the first radio button immediately
381 // preceding this one
383 GtkWidget
*widget
= NULL
;
385 if ( tool
->IsRadio() )
387 wxToolBarToolsList::compatibility_iterator node
388 = wxToolBarToolsList::compatibility_iterator();
390 node
= m_tools
.Item(pos
- 1);
394 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
395 if ( !toolNext
->IsRadio() )
398 widget
= toolNext
->m_item
;
400 node
= node
->GetPrevious();
405 // this is the first button in the radio button group,
406 // it will be toggled automatically by GTK so bring the
407 // internal flag in sync
412 tool
->m_item
= gtk_toolbar_insert_element
415 tool
->GetGtkChildType(),
417 tool
->GetLabel().empty()
419 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
420 tool
->GetShortHelp().empty()
422 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
423 "", // tooltip_private_text (?)
425 (GtkSignalFunc
)gtk_toolbar_callback
,
430 wxCHECK_MSG(tool
->m_item
!= NULL
, false, _T("gtk_toolbar_insert_element() failed"));
432 g_signal_connect (tool
->m_item
, "enter_notify_event",
433 G_CALLBACK (gtk_toolbar_tool_callback
),
435 g_signal_connect (tool
->m_item
, "leave_notify_event",
436 G_CALLBACK (gtk_toolbar_tool_callback
),
441 case wxTOOL_STYLE_SEPARATOR
:
442 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
447 case wxTOOL_STYLE_CONTROL
:
448 gtk_toolbar_insert_widget(
450 tool
->GetControl()->m_widget
,
459 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
461 m_width
= req
.width
+ m_xMargin
;
462 m_height
= req
.height
+ 2*m_yMargin
;
463 InvalidateBestSize();
468 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
470 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
472 switch ( tool
->GetStyle() )
474 case wxTOOL_STYLE_CONTROL
:
475 tool
->GetControl()->Destroy();
478 case wxTOOL_STYLE_BUTTON
:
479 gtk_widget_destroy( tool
->m_item
);
482 case wxTOOL_STYLE_SEPARATOR
:
483 gtk_toolbar_remove_space( m_toolbar
, pos
);
487 InvalidateBestSize();
491 // ----------------------------------------------------------------------------
492 // wxToolBar tools state
493 // ----------------------------------------------------------------------------
495 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
497 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
501 gtk_widget_set_sensitive( tool
->m_item
, enable
);
505 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
507 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, toolBase
);
509 GtkWidget
*item
= tool
->m_item
;
510 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
512 tool
->SetImage(tool
->GetBitmap());
516 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item
), toggle
);
518 m_blockEvent
= false;
522 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
523 bool WXUNUSED(toggle
))
525 // VZ: absolutely no idea about how to do it
526 wxFAIL_MSG( _T("not implemented") );
529 // ----------------------------------------------------------------------------
530 // wxToolBar geometry
531 // ----------------------------------------------------------------------------
533 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
534 wxCoord
WXUNUSED(y
)) const
536 // VZ: GTK+ doesn't seem to have such thing
537 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
539 return (wxToolBarToolBase
*)NULL
;
542 void wxToolBar::SetMargins( int x
, int y
)
544 wxCHECK_RET( GetToolsCount() == 0,
545 wxT("wxToolBar::SetMargins must be called before adding tools.") );
551 void wxToolBar::SetToolSeparation( int separation
)
553 // FIXME: this function disappeared
555 gtk_toolbar_set_space_size( m_toolbar
, separation
);
558 m_toolSeparation
= separation
;
561 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
563 wxToolBarTool
* tool
= wx_static_cast(wxToolBarTool
*, FindById(id
));
567 (void)tool
->SetShortHelp(helpString
);
568 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
569 wxGTK_CONV( helpString
), "");
573 // ----------------------------------------------------------------------------
574 // wxToolBar idle handling
575 // ----------------------------------------------------------------------------
577 void wxToolBar::OnInternalIdle()
579 // Check if we have to show window now
580 if (GtkShowFromOnIdle()) return;
582 wxCursor cursor
= m_cursor
;
583 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
587 /* I now set the cursor the anew in every OnInternalIdle call
588 as setting the cursor in a parent window also effects the
589 windows above so that checking for the current cursor is
592 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
594 /* if the toolbar is dockable, then m_widget stands for the
595 GtkHandleBox widget, which uses its own window so that we
596 can set the cursor for it. if the toolbar is not dockable,
597 m_widget comes from m_toolbar which uses its parent's
598 window ("windowless windows") and thus we cannot set the
600 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
603 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
606 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
607 node
= node
->GetNext();
609 GtkWidget
*item
= tool
->m_item
;
612 GdkWindow
*window
= item
->window
;
616 gdk_window_set_cursor( window
, cursor
.GetCursor() );
622 if (wxUpdateUIEvent::CanUpdate(this))
623 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
627 // ----------------------------------------------------------------------------
631 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
633 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
636 #endif // wxUSE_TOOLBAR_NATIVE