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 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
66 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
69 else // no text, hence we must have the icons or what would we show?
71 *gtkStyle
= GTK_TOOLBAR_ICONS
;
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 class wxToolBarTool
: public wxToolBarToolBase
82 wxToolBarTool(wxToolBar
*tbar
,
84 const wxString
& label
,
85 const wxBitmap
& bitmap1
,
86 const wxBitmap
& bitmap2
,
89 const wxString
& shortHelpString
,
90 const wxString
& longHelpString
)
91 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
92 clientData
, shortHelpString
, longHelpString
)
97 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
98 : wxToolBarToolBase(tbar
, control
)
103 // is this a radio button?
105 // unlike GetKind(), can be called for any kind of tools, not just buttons
106 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
108 // this is only called for the normal buttons, i.e. not separators nor
110 GtkToolbarChildType
GetGtkChildType() const
115 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
118 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
121 wxFAIL_MSG( _T("unknown toolbar child type") );
125 return GTK_TOOLBAR_CHILD_BUTTON
;
129 void SetPixmap(const wxBitmap
& bitmap
)
133 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
135 if (bitmap
.HasPixbuf())
136 gtk_image_set_from_pixbuf( GTK_IMAGE(m_pixmap
), bitmap
.GetPixbuf() );
138 gtk_pixmap_set( GTK_PIXMAP(m_pixmap
), bitmap
.GetPixmap(), mask
);
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
155 // ============================================================================
157 // ============================================================================
159 //-----------------------------------------------------------------------------
160 // "clicked" (internal from gtk_toolbar)
161 //-----------------------------------------------------------------------------
164 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
165 wxToolBarTool
*tool
)
168 wxapp_install_idle_handler();
170 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
172 if (tbar
->m_blockEvent
) return;
174 if (g_blockEventsOnDrag
) return;
175 if (!tool
->IsEnabled()) return;
177 if (tool
->CanBeToggled())
181 tool
->SetPixmap(tool
->GetBitmap());
183 if ( tool
->IsRadio() && !tool
->IsToggled() )
185 // radio button went up, don't report this as a wxWin event
190 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
195 tool
->SetPixmap(tool
->GetBitmap());
200 //-----------------------------------------------------------------------------
201 // "enter_notify_event" / "leave_notify_event"
202 //-----------------------------------------------------------------------------
205 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
206 GdkEventCrossing
*gdk_event
,
207 wxToolBarTool
*tool
)
209 if (g_isIdle
) wxapp_install_idle_handler();
211 if (g_blockEventsOnDrag
) return TRUE
;
213 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
216 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
217 tb
->OnMouseEnter( tool
->GetId() );
219 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") );
297 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
300 // Doesn't work this way.
301 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
302 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
304 SetToolSeparation(7);
306 if (style
& wxTB_DOCKABLE
)
308 m_widget
= gtk_handle_box_new();
309 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
310 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
312 if (style
& wxTB_FLAT
)
313 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
317 m_widget
= gtk_event_box_new();
318 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
319 ConnectWidget( m_widget
);
320 gtk_widget_show(GTK_WIDGET(m_toolbar
));
323 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
325 // FIXME: there is no such function for toolbars in 2.0
327 if (style
& wxTB_FLAT
)
328 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
331 m_parent
->DoAddChild( this );
338 void wxToolBar::GtkSetStyle()
340 GtkOrientation orient
;
341 GtkToolbarStyle style
;
342 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
344 gtk_toolbar_set_orientation(m_toolbar
, orient
);
345 gtk_toolbar_set_style(m_toolbar
, style
);
348 void wxToolBar::SetWindowStyleFlag( long style
)
350 wxToolBarBase::SetWindowStyleFlag(style
);
356 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
358 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
362 if ( tool
->IsButton() )
364 if ( !HasFlag(wxTB_NOICONS
) )
366 wxBitmap bitmap
= tool
->GetNormalBitmap();
368 wxCHECK_MSG( bitmap
.Ok(), false,
369 wxT("invalid bitmap for wxToolBar icon") );
371 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, false,
372 wxT("wxToolBar doesn't support GdkBitmap") );
374 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, false,
375 wxT("wxToolBar::Add needs a wxBitmap") );
377 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
380 if (bitmap
.HasPixbuf())
382 tool_pixmap
= gtk_image_new();
383 tool
->m_pixmap
= tool_pixmap
;
384 tool
->SetPixmap(bitmap
);
388 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
390 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
391 if ( bitmap
.GetMask() )
392 mask
= bitmap
.GetMask()->GetBitmap();
394 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
395 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
398 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
400 tool
->m_pixmap
= tool_pixmap
;
404 switch ( tool
->GetStyle() )
406 case wxTOOL_STYLE_BUTTON
:
407 // for a radio button we need the widget which starts the radio
408 // group it belongs to, i.e. the first radio button immediately
409 // preceding this one
411 GtkWidget
*widget
= NULL
;
413 if ( tool
->IsRadio() )
415 wxToolBarToolsList::compatibility_iterator node
416 = wxToolBarToolsList::compatibility_iterator();
418 node
= m_tools
.Item(pos
- 1);
422 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
423 if ( !toolNext
->IsRadio() )
426 widget
= toolNext
->m_item
;
428 node
= node
->GetPrevious();
433 // this is the first button in the radio button group,
434 // it will be toggled automatically by GTK so bring the
435 // internal flag in sync
440 tool
->m_item
= gtk_toolbar_insert_element
443 tool
->GetGtkChildType(),
445 tool
->GetLabel().empty()
447 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
448 tool
->GetShortHelp().empty()
450 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
451 "", // tooltip_private_text (?)
453 (GtkSignalFunc
)gtk_toolbar_callback
,
460 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
465 g_signal_connect (tool
->m_item
, "enter_notify_event",
466 G_CALLBACK (gtk_toolbar_tool_callback
),
468 g_signal_connect (tool
->m_item
, "leave_notify_event",
469 G_CALLBACK (gtk_toolbar_tool_callback
),
474 case wxTOOL_STYLE_SEPARATOR
:
475 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
480 case wxTOOL_STYLE_CONTROL
:
481 gtk_toolbar_insert_widget(
483 tool
->GetControl()->m_widget
,
492 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
494 m_width
= req
.width
+ m_xMargin
;
495 m_height
= req
.height
+ 2*m_yMargin
;
496 InvalidateBestSize();
501 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
503 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
505 switch ( tool
->GetStyle() )
507 case wxTOOL_STYLE_CONTROL
:
508 tool
->GetControl()->Destroy();
511 case wxTOOL_STYLE_BUTTON
:
512 gtk_widget_destroy( tool
->m_item
);
515 case wxTOOL_STYLE_SEPARATOR
:
516 gtk_toolbar_remove_space( m_toolbar
, pos
);
520 InvalidateBestSize();
524 // ----------------------------------------------------------------------------
525 // wxToolBar tools state
526 // ----------------------------------------------------------------------------
528 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
530 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
534 gtk_widget_set_sensitive( tool
->m_item
, enable
);
538 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
540 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
542 GtkWidget
*item
= tool
->m_item
;
543 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
545 tool
->SetPixmap(tool
->GetBitmap());
549 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item
), toggle
);
551 m_blockEvent
= false;
555 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
556 bool WXUNUSED(toggle
))
558 // VZ: absolutely no idea about how to do it
559 wxFAIL_MSG( _T("not implemented") );
562 // ----------------------------------------------------------------------------
563 // wxToolBar geometry
564 // ----------------------------------------------------------------------------
566 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
567 wxCoord
WXUNUSED(y
)) const
569 // VZ: GTK+ doesn't seem to have such thing
570 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
572 return (wxToolBarToolBase
*)NULL
;
575 void wxToolBar::SetMargins( int x
, int y
)
577 wxCHECK_RET( GetToolsCount() == 0,
578 wxT("wxToolBar::SetMargins must be called before adding tools.") );
584 void wxToolBar::SetToolSeparation( int separation
)
586 // FIXME: this function disappeared
588 gtk_toolbar_set_space_size( m_toolbar
, separation
);
591 m_toolSeparation
= separation
;
594 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
596 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
600 (void)tool
->SetShortHelp(helpString
);
601 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
602 wxGTK_CONV( helpString
), "");
606 // ----------------------------------------------------------------------------
607 // wxToolBar idle handling
608 // ----------------------------------------------------------------------------
610 void wxToolBar::OnInternalIdle()
612 wxCursor cursor
= m_cursor
;
613 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
617 /* I now set the cursor the anew in every OnInternalIdle call
618 as setting the cursor in a parent window also effects the
619 windows above so that checking for the current cursor is
622 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
624 /* if the toolbar is dockable, then m_widget stands for the
625 GtkHandleBox widget, which uses its own window so that we
626 can set the cursor for it. if the toolbar is not dockable,
627 m_widget comes from m_toolbar which uses its parent's
628 window ("windowless windows") and thus we cannot set the
630 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
633 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
636 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
637 node
= node
->GetNext();
639 GtkWidget
*item
= tool
->m_item
;
642 GdkWindow
*window
= item
->window
;
646 gdk_window_set_cursor( window
, cursor
.GetCursor() );
652 if (wxUpdateUIEvent::CanUpdate(this))
653 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
657 // ----------------------------------------------------------------------------
661 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
663 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
666 #endif // wxUSE_TOOLBAR_NATIVE