1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/toolbar.cpp
3 // Purpose: GTK toolbar
4 // Author: Robert Roebling
5 // Modified: 13.12.99 by VZ to derive from wxToolBarBase
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
21 #if wxUSE_TOOLBAR_NATIVE
23 #include "wx/toolbar.h"
30 #include "wx/gtk1/private.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
37 extern void wxapp_install_idle_handler();
41 extern bool g_blockEventsOnDrag
;
42 extern wxCursor g_globalCursor
;
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // translate wxWidgets toolbar style flags to GTK orientation and style
49 static void GetGtkStyle(long style
,
50 GtkOrientation
*orient
, GtkToolbarStyle
*gtkStyle
)
52 *orient
= style
& wxTB_VERTICAL
? GTK_ORIENTATION_VERTICAL
53 : GTK_ORIENTATION_HORIZONTAL
;
56 if ( style
& wxTB_TEXT
)
58 *gtkStyle
= style
& wxTB_NOICONS
62 else // no text, hence we must have the icons or what would we show?
64 *gtkStyle
= GTK_TOOLBAR_ICONS
;
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 class wxToolBarTool
: public wxToolBarToolBase
75 wxToolBarTool(wxToolBar
*tbar
,
77 const wxString
& label
,
78 const wxBitmap
& bitmap1
,
79 const wxBitmap
& bitmap2
,
82 const wxString
& shortHelpString
,
83 const wxString
& longHelpString
)
84 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
85 clientData
, shortHelpString
, longHelpString
)
90 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
91 : wxToolBarToolBase(tbar
, control
, label
)
96 // is this a radio button?
98 // unlike GetKind(), can be called for any kind of tools, not just buttons
99 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
101 // this is only called for the normal buttons, i.e. not separators nor
103 GtkToolbarChildType
GetGtkChildType() const
108 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
111 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
114 wxFAIL_MSG( wxT("unknown toolbar child type") );
118 return GTK_TOOLBAR_CHILD_BUTTON
;
122 void SetPixmap(const wxBitmap
& bitmap
)
126 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
128 gtk_pixmap_set( GTK_PIXMAP(m_pixmap
), bitmap
.GetPixmap(), mask
);
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
145 // ============================================================================
147 // ============================================================================
149 //-----------------------------------------------------------------------------
150 // "clicked" (internal from gtk_toolbar)
151 //-----------------------------------------------------------------------------
154 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
155 wxToolBarTool
*tool
)
158 wxapp_install_idle_handler();
160 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
162 if (tbar
->m_blockEvent
) return;
164 if (g_blockEventsOnDrag
) return;
165 if (!tool
->IsEnabled()) return;
167 if (tool
->CanBeToggled())
171 tool
->SetPixmap(tool
->GetBitmap());
173 if ( tool
->IsRadio() && !tool
->IsToggled() )
175 // radio button went up, don't report this as a wxWin event
180 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
185 tool
->SetPixmap(tool
->GetBitmap());
190 //-----------------------------------------------------------------------------
191 // "enter_notify_event" / "leave_notify_event"
192 //-----------------------------------------------------------------------------
195 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
196 GdkEventCrossing
*gdk_event
,
197 wxToolBarTool
*tool
)
199 if (g_isIdle
) wxapp_install_idle_handler();
201 if (g_blockEventsOnDrag
) return TRUE
;
203 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
206 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
207 tb
->OnMouseEnter( tool
->GetId() );
209 tb
->OnMouseEnter( -1 );
215 //-----------------------------------------------------------------------------
216 // InsertChild callback for wxToolBar
217 //-----------------------------------------------------------------------------
219 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
220 wxWindow
* WXUNUSED(child
) )
222 // we don't do anything here
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 void wxToolBarTool::Init()
235 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
236 const wxString
& text
,
237 const wxBitmap
& bitmap1
,
238 const wxBitmap
& bitmap2
,
240 wxObject
*clientData
,
241 const wxString
& shortHelpString
,
242 const wxString
& longHelpString
)
244 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
245 clientData
, shortHelpString
, longHelpString
);
249 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
251 return new wxToolBarTool(this, control
, label
);
254 //-----------------------------------------------------------------------------
255 // wxToolBar construction
256 //-----------------------------------------------------------------------------
258 void wxToolBar::Init()
261 m_blockEvent
= false;
263 m_defaultHeight
= 32;
266 wxToolBar::~wxToolBar()
270 bool wxToolBar::Create( wxWindow
*parent
,
275 const wxString
& name
)
278 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
280 if ( !PreCreation( parent
, pos
, size
) ||
281 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
283 wxFAIL_MSG( wxT("wxToolBar creation failed") );
290 GtkOrientation orient
;
291 GtkToolbarStyle gtkStyle
;
292 GetGtkStyle(style
, &orient
, >kStyle
);
294 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new(orient
, gtkStyle
) );
296 SetToolSeparation(7);
298 if (style
& wxTB_DOCKABLE
)
300 m_widget
= gtk_handle_box_new();
301 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
302 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
304 if (style
& wxTB_FLAT
)
305 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
309 m_widget
= gtk_event_box_new();
310 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
311 ConnectWidget( m_widget
);
312 gtk_widget_show(GTK_WIDGET(m_toolbar
));
315 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
317 if (style
& wxTB_FLAT
)
318 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
320 m_parent
->DoAddChild( this );
327 void wxToolBar::GtkSetStyle()
329 GtkOrientation orient
;
330 GtkToolbarStyle style
;
331 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
333 gtk_toolbar_set_orientation(m_toolbar
, orient
);
334 gtk_toolbar_set_style(m_toolbar
, style
);
337 void wxToolBar::SetWindowStyleFlag( long style
)
339 wxToolBarBase::SetWindowStyleFlag(style
);
345 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
347 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
349 // if we have inserted a space before all the tools we must change the GTK
351 size_t posGtk
= m_xMargin
> 1 ? pos
+ 1 : pos
;
353 if ( tool
->IsButton() )
355 if ( !HasFlag(wxTB_NOICONS
) )
357 wxBitmap bitmap
= tool
->GetNormalBitmap();
359 wxCHECK_MSG( bitmap
.IsOk(), false,
360 wxT("invalid bitmap for wxToolBar icon") );
362 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, false,
363 wxT("wxToolBar doesn't support GdkBitmap") );
365 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, false,
366 wxT("wxToolBar::Add needs a wxBitmap") );
368 GtkWidget
*tool_pixmap
= NULL
;
370 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
372 GdkBitmap
*mask
= NULL
;
373 if ( bitmap
.GetMask() )
374 mask
= bitmap
.GetMask()->GetBitmap();
376 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
377 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
379 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
381 tool
->m_pixmap
= tool_pixmap
;
385 switch ( tool
->GetStyle() )
387 case wxTOOL_STYLE_BUTTON
:
388 // for a radio button we need the widget which starts the radio
389 // group it belongs to, i.e. the first radio button immediately
390 // preceding this one
392 GtkWidget
*widget
= NULL
;
394 if ( tool
->IsRadio() )
396 wxToolBarToolsList::compatibility_iterator node
397 = wxToolBarToolsList::compatibility_iterator();
399 node
= m_tools
.Item(pos
- 1);
403 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
404 if ( !toolNext
->IsRadio() )
407 widget
= toolNext
->m_item
;
409 node
= node
->GetPrevious();
414 // this is the first button in the radio button group,
415 // it will be toggled automatically by GTK so bring the
416 // internal flag in sync
421 tool
->m_item
= gtk_toolbar_insert_element
424 tool
->GetGtkChildType(),
426 tool
->GetLabel().empty()
428 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
429 tool
->GetShortHelp().empty()
431 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
432 "", // tooltip_private_text (?)
434 (GtkSignalFunc
)gtk_toolbar_callback
,
441 wxFAIL_MSG( wxT("gtk_toolbar_insert_element() failed") );
446 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
447 "enter_notify_event",
448 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
450 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
451 "leave_notify_event",
452 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
457 case wxTOOL_STYLE_SEPARATOR
:
458 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
463 case wxTOOL_STYLE_CONTROL
:
464 gtk_toolbar_insert_widget(
466 tool
->GetControl()->m_widget
,
475 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
477 m_width
= req
.width
+ m_xMargin
;
478 m_height
= req
.height
+ 2*m_yMargin
;
479 InvalidateBestSize();
484 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
486 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
488 switch ( tool
->GetStyle() )
490 case wxTOOL_STYLE_CONTROL
:
491 // don't destroy the control here as we can be called from
492 // RemoveTool() and then we need to keep the control alive;
493 // while if we're called from DeleteTool() the control will
494 // be destroyed when wxToolBarToolBase itself is deleted
497 case wxTOOL_STYLE_BUTTON
:
498 gtk_widget_destroy( tool
->m_item
);
502 wxFAIL_MSG( "unknown tool style" );
506 InvalidateBestSize();
510 // ----------------------------------------------------------------------------
511 // wxToolBar tools state
512 // ----------------------------------------------------------------------------
514 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
516 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
520 gtk_widget_set_sensitive( tool
->m_item
, enable
);
524 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
526 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
528 GtkWidget
*item
= tool
->m_item
;
529 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
531 tool
->SetPixmap(tool
->GetBitmap());
535 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
537 m_blockEvent
= false;
541 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
542 bool WXUNUSED(toggle
))
544 // VZ: absolutely no idea about how to do it
545 wxFAIL_MSG( wxT("not implemented") );
548 // ----------------------------------------------------------------------------
549 // wxToolBar geometry
550 // ----------------------------------------------------------------------------
552 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
553 wxCoord
WXUNUSED(y
)) const
555 // VZ: GTK+ doesn't seem to have such thing
556 wxFAIL_MSG( wxT("wxToolBar::FindToolForPosition() not implemented") );
561 void wxToolBar::SetMargins( int x
, int y
)
563 wxCHECK_RET( GetToolsCount() == 0,
564 wxT("wxToolBar::SetMargins must be called before adding tools.") );
567 gtk_toolbar_append_space( m_toolbar
); // oh well
573 void wxToolBar::SetToolSeparation( int separation
)
575 gtk_toolbar_set_space_size( m_toolbar
, separation
);
577 m_toolSeparation
= separation
;
580 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
582 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
586 (void)tool
->SetShortHelp(helpString
);
587 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
588 wxGTK_CONV( helpString
), "");
592 // ----------------------------------------------------------------------------
593 // wxToolBar idle handling
594 // ----------------------------------------------------------------------------
596 void wxToolBar::OnInternalIdle()
598 wxCursor cursor
= m_cursor
;
599 if (g_globalCursor
.IsOk()) cursor
= g_globalCursor
;
603 /* I now set the cursor the anew in every OnInternalIdle call
604 as setting the cursor in a parent window also effects the
605 windows above so that checking for the current cursor is
608 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
610 /* if the toolbar is dockable, then m_widget stands for the
611 GtkHandleBox widget, which uses its own window so that we
612 can set the cursor for it. if the toolbar is not dockable,
613 m_widget comes from m_toolbar which uses its parent's
614 window ("windowless windows") and thus we cannot set the
616 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
619 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
622 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
623 node
= node
->GetNext();
625 GtkWidget
*item
= tool
->m_item
;
628 GdkWindow
*window
= item
->window
;
632 gdk_window_set_cursor( window
, cursor
.GetCursor() );
638 if (wxUpdateUIEvent::CanUpdate(this))
639 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
643 // ----------------------------------------------------------------------------
647 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
649 wxVisualAttributes attr
;
650 GtkWidget
* widget
= gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL
, GTK_TOOLBAR_BOTH
);
651 attr
= GetDefaultAttributesFromGTKWidget(widget
);
652 gtk_widget_destroy(widget
);
656 #endif // wxUSE_TOOLBAR_NATIVE