1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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"
31 #include "wx/gtk1/private.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
38 extern void wxapp_install_idle_handler();
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
63 else // no text, hence we must have the icons or what would we show?
65 *gtkStyle
= GTK_TOOLBAR_ICONS
;
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 class wxToolBarTool
: public wxToolBarToolBase
76 wxToolBarTool(wxToolBar
*tbar
,
78 const wxString
& label
,
79 const wxBitmap
& bitmap1
,
80 const wxBitmap
& bitmap2
,
83 const wxString
& shortHelpString
,
84 const wxString
& longHelpString
)
85 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
86 clientData
, shortHelpString
, longHelpString
)
91 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
92 : wxToolBarToolBase(tbar
, control
)
97 // is this a radio button?
99 // unlike GetKind(), can be called for any kind of tools, not just buttons
100 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
102 // this is only called for the normal buttons, i.e. not separators nor
104 GtkToolbarChildType
GetGtkChildType() const
109 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
112 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
115 wxFAIL_MSG( _T("unknown toolbar child type") );
119 return GTK_TOOLBAR_CHILD_BUTTON
;
123 void SetPixmap(const wxBitmap
& bitmap
)
127 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
129 gtk_pixmap_set( GTK_PIXMAP(m_pixmap
), bitmap
.GetPixmap(), mask
);
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
146 // ============================================================================
148 // ============================================================================
150 //-----------------------------------------------------------------------------
151 // "clicked" (internal from gtk_toolbar)
152 //-----------------------------------------------------------------------------
155 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
156 wxToolBarTool
*tool
)
159 wxapp_install_idle_handler();
161 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
163 if (tbar
->m_blockEvent
) return;
165 if (g_blockEventsOnDrag
) return;
166 if (!tool
->IsEnabled()) return;
168 if (tool
->CanBeToggled())
172 tool
->SetPixmap(tool
->GetBitmap());
174 if ( tool
->IsRadio() && !tool
->IsToggled() )
176 // radio button went up, don't report this as a wxWin event
181 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
186 tool
->SetPixmap(tool
->GetBitmap());
191 //-----------------------------------------------------------------------------
192 // "enter_notify_event" / "leave_notify_event"
193 //-----------------------------------------------------------------------------
196 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
197 GdkEventCrossing
*gdk_event
,
198 wxToolBarTool
*tool
)
200 if (g_isIdle
) wxapp_install_idle_handler();
202 if (g_blockEventsOnDrag
) return TRUE
;
204 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
207 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
208 tb
->OnMouseEnter( tool
->GetId() );
210 tb
->OnMouseEnter( -1 );
216 //-----------------------------------------------------------------------------
217 // InsertChild callback for wxToolBar
218 //-----------------------------------------------------------------------------
220 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
221 wxWindow
* WXUNUSED(child
) )
223 // we don't do anything here
226 // ----------------------------------------------------------------------------
228 // ----------------------------------------------------------------------------
230 void wxToolBarTool::Init()
233 m_pixmap
= (GtkWidget
*)NULL
;
236 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
237 const wxString
& text
,
238 const wxBitmap
& bitmap1
,
239 const wxBitmap
& bitmap2
,
241 wxObject
*clientData
,
242 const wxString
& shortHelpString
,
243 const wxString
& longHelpString
)
245 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
246 clientData
, shortHelpString
, longHelpString
);
249 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
251 return new wxToolBarTool(this, control
);
254 //-----------------------------------------------------------------------------
255 // wxToolBar construction
256 //-----------------------------------------------------------------------------
258 void wxToolBar::Init()
260 m_toolbar
= (GtkToolbar
*)NULL
;
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") );
288 GtkOrientation orient
;
289 GtkToolbarStyle gtkStyle
;
290 GetGtkStyle(style
, &orient
, >kStyle
);
292 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new(orient
, gtkStyle
) );
294 SetToolSeparation(7);
296 if (style
& wxTB_DOCKABLE
)
298 m_widget
= gtk_handle_box_new();
299 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
300 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
302 if (style
& wxTB_FLAT
)
303 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
307 m_widget
= gtk_event_box_new();
308 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
309 ConnectWidget( m_widget
);
310 gtk_widget_show(GTK_WIDGET(m_toolbar
));
313 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
315 if (style
& wxTB_FLAT
)
316 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
318 m_parent
->DoAddChild( this );
325 void wxToolBar::GtkSetStyle()
327 GtkOrientation orient
;
328 GtkToolbarStyle style
;
329 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
331 gtk_toolbar_set_orientation(m_toolbar
, orient
);
332 gtk_toolbar_set_style(m_toolbar
, style
);
335 void wxToolBar::SetWindowStyleFlag( long style
)
337 wxToolBarBase::SetWindowStyleFlag(style
);
343 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
345 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
347 // if we have inserted a space before all the tools we must change the GTK
349 size_t posGtk
= m_xMargin
> 1 ? pos
+ 1 : pos
;
351 if ( tool
->IsButton() )
353 if ( !HasFlag(wxTB_NOICONS
) )
355 wxBitmap bitmap
= tool
->GetNormalBitmap();
357 wxCHECK_MSG( bitmap
.Ok(), false,
358 wxT("invalid bitmap for wxToolBar icon") );
360 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, false,
361 wxT("wxToolBar doesn't support GdkBitmap") );
363 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, false,
364 wxT("wxToolBar::Add needs a wxBitmap") );
366 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
368 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
370 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
371 if ( bitmap
.GetMask() )
372 mask
= bitmap
.GetMask()->GetBitmap();
374 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
375 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
377 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
379 tool
->m_pixmap
= tool_pixmap
;
383 switch ( tool
->GetStyle() )
385 case wxTOOL_STYLE_BUTTON
:
386 // for a radio button we need the widget which starts the radio
387 // group it belongs to, i.e. the first radio button immediately
388 // preceding this one
390 GtkWidget
*widget
= NULL
;
392 if ( tool
->IsRadio() )
394 wxToolBarToolsList::compatibility_iterator node
395 = wxToolBarToolsList::compatibility_iterator();
397 node
= m_tools
.Item(pos
- 1);
401 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
402 if ( !toolNext
->IsRadio() )
405 widget
= toolNext
->m_item
;
407 node
= node
->GetPrevious();
412 // this is the first button in the radio button group,
413 // it will be toggled automatically by GTK so bring the
414 // internal flag in sync
419 tool
->m_item
= gtk_toolbar_insert_element
422 tool
->GetGtkChildType(),
424 tool
->GetLabel().empty()
426 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
427 tool
->GetShortHelp().empty()
429 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
430 "", // tooltip_private_text (?)
432 (GtkSignalFunc
)gtk_toolbar_callback
,
439 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
444 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
445 "enter_notify_event",
446 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
448 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
449 "leave_notify_event",
450 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
455 case wxTOOL_STYLE_SEPARATOR
:
456 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
461 case wxTOOL_STYLE_CONTROL
:
462 gtk_toolbar_insert_widget(
464 tool
->GetControl()->m_widget
,
473 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
475 m_width
= req
.width
+ m_xMargin
;
476 m_height
= req
.height
+ 2*m_yMargin
;
477 InvalidateBestSize();
482 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
484 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
486 switch ( tool
->GetStyle() )
488 case wxTOOL_STYLE_CONTROL
:
489 tool
->GetControl()->Destroy();
492 case wxTOOL_STYLE_BUTTON
:
493 gtk_widget_destroy( tool
->m_item
);
497 InvalidateBestSize();
501 // ----------------------------------------------------------------------------
502 // wxToolBar tools state
503 // ----------------------------------------------------------------------------
505 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
507 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
511 gtk_widget_set_sensitive( tool
->m_item
, enable
);
515 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
517 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
519 GtkWidget
*item
= tool
->m_item
;
520 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
522 tool
->SetPixmap(tool
->GetBitmap());
526 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
528 m_blockEvent
= false;
532 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
533 bool WXUNUSED(toggle
))
535 // VZ: absolutely no idea about how to do it
536 wxFAIL_MSG( _T("not implemented") );
539 // ----------------------------------------------------------------------------
540 // wxToolBar geometry
541 // ----------------------------------------------------------------------------
543 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
544 wxCoord
WXUNUSED(y
)) const
546 // VZ: GTK+ doesn't seem to have such thing
547 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
549 return (wxToolBarToolBase
*)NULL
;
552 void wxToolBar::SetMargins( int x
, int y
)
554 wxCHECK_RET( GetToolsCount() == 0,
555 wxT("wxToolBar::SetMargins must be called before adding tools.") );
558 gtk_toolbar_append_space( m_toolbar
); // oh well
564 void wxToolBar::SetToolSeparation( int separation
)
566 gtk_toolbar_set_space_size( m_toolbar
, separation
);
568 m_toolSeparation
= separation
;
571 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
573 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
577 (void)tool
->SetShortHelp(helpString
);
578 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
579 wxGTK_CONV( helpString
), "");
583 // ----------------------------------------------------------------------------
584 // wxToolBar idle handling
585 // ----------------------------------------------------------------------------
587 void wxToolBar::OnInternalIdle()
589 wxCursor cursor
= m_cursor
;
590 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
594 /* I now set the cursor the anew in every OnInternalIdle call
595 as setting the cursor in a parent window also effects the
596 windows above so that checking for the current cursor is
599 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
601 /* if the toolbar is dockable, then m_widget stands for the
602 GtkHandleBox widget, which uses its own window so that we
603 can set the cursor for it. if the toolbar is not dockable,
604 m_widget comes from m_toolbar which uses its parent's
605 window ("windowless windows") and thus we cannot set the
607 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
610 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
613 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
614 node
= node
->GetNext();
616 GtkWidget
*item
= tool
->m_item
;
619 GdkWindow
*window
= item
->window
;
623 gdk_window_set_cursor( window
, cursor
.GetCursor() );
629 if (wxUpdateUIEvent::CanUpdate(this))
630 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
634 // ----------------------------------------------------------------------------
638 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
640 wxVisualAttributes attr
;
641 GtkWidget
* widget
= gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL
, GTK_TOOLBAR_BOTH
);
642 attr
= GetDefaultAttributesFromGTKWidget(widget
);
643 gtk_widget_destroy(widget
);
647 #endif // wxUSE_TOOLBAR_NATIVE