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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma implementation "tbargtk.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
26 #include "wx/toolbar.h"
28 #if wxUSE_TOOLBAR_NATIVE
33 #include "wx/gtk/private.h"
35 extern GdkFont
*GtkGetDefaultGuiFont();
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
42 extern void wxapp_install_idle_handler();
46 extern bool g_blockEventsOnDrag
;
47 extern wxCursor g_globalCursor
;
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // translate wxWindows 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
67 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
71 else // no text, hence we must have the icons or what would we show?
73 *gtkStyle
= GTK_TOOLBAR_ICONS
;
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 class wxToolBarTool
: public wxToolBarToolBase
84 wxToolBarTool(wxToolBar
*tbar
,
86 const wxString
& label
,
87 const wxBitmap
& bitmap1
,
88 const wxBitmap
& bitmap2
,
91 const wxString
& shortHelpString
,
92 const wxString
& longHelpString
)
93 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
94 clientData
, shortHelpString
, longHelpString
)
99 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
100 : wxToolBarToolBase(tbar
, control
)
105 // is this a radio button?
107 // unlike GetKind(), can be called for any kind of tools, not just buttons
108 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
110 // this is only called for the normal buttons, i.e. not separators nor
112 GtkToolbarChildType
GetGtkChildType() const
117 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
120 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
123 wxFAIL_MSG( _T("unknown toolbar child type") );
127 return GTK_TOOLBAR_CHILD_BUTTON
;
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
144 // ============================================================================
146 // ============================================================================
148 //-----------------------------------------------------------------------------
149 // "clicked" (internal from gtk_toolbar)
150 //-----------------------------------------------------------------------------
152 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
153 wxToolBarTool
*tool
)
156 wxapp_install_idle_handler();
158 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
160 if (tbar
->m_blockEvent
) return;
162 if (g_blockEventsOnDrag
) return;
163 if (!tool
->IsEnabled()) return;
165 if (tool
->CanBeToggled())
169 wxBitmap bitmap
= tool
->GetBitmap();
172 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
174 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
177 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
180 if ( tool
->IsRadio() && !tool
->IsToggled() )
182 // radio button went up, don't report this as a wxWin event
187 tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() );
190 //-----------------------------------------------------------------------------
191 // "enter_notify_event" / "leave_notify_event"
192 //-----------------------------------------------------------------------------
194 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
195 GdkEventCrossing
*gdk_event
,
196 wxToolBarTool
*tool
)
198 if (g_isIdle
) wxapp_install_idle_handler();
200 if (g_blockEventsOnDrag
) return TRUE
;
202 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
205 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
206 tb
->OnMouseEnter( tool
->GetId() );
208 tb
->OnMouseEnter( -1 );
213 //-----------------------------------------------------------------------------
214 // InsertChild callback for wxToolBar
215 //-----------------------------------------------------------------------------
217 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
218 wxWindow
* WXUNUSED(child
) )
220 // we don't do anything here
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 void wxToolBarTool::Init()
230 m_pixmap
= (GtkWidget
*)NULL
;
233 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
234 const wxString
& text
,
235 const wxBitmap
& bitmap1
,
236 const wxBitmap
& bitmap2
,
238 wxObject
*clientData
,
239 const wxString
& shortHelpString
,
240 const wxString
& longHelpString
)
242 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
243 clientData
, shortHelpString
, longHelpString
);
246 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
248 return new wxToolBarTool(this, control
);
251 //-----------------------------------------------------------------------------
252 // wxToolBar construction
253 //-----------------------------------------------------------------------------
255 void wxToolBar::Init()
258 m_bg
= (GdkColor
*)NULL
;
259 m_toolbar
= (GtkToolbar
*)NULL
;
260 m_blockEvent
= FALSE
;
263 wxToolBar::~wxToolBar()
269 bool wxToolBar::Create( wxWindow
*parent
,
274 const wxString
& name
)
277 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
279 if ( !PreCreation( parent
, pos
, size
) ||
280 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
282 wxFAIL_MSG( wxT("wxToolBar creation failed") );
288 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
291 // Doesn't work this way.
292 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
293 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
295 GtkOrientation orient
;
296 GtkToolbarStyle gtkStyle
;
297 GetGtkStyle(style
, &orient
, >kStyle
);
299 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new(orient
, gtkStyle
) );
302 SetToolSeparation(7);
304 if (style
& wxTB_DOCKABLE
)
306 m_widget
= gtk_handle_box_new();
307 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
308 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
310 if (style
& wxTB_FLAT
)
311 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
315 m_widget
= gtk_event_box_new();
316 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
317 ConnectWidget( m_widget
);
318 gtk_widget_show(GTK_WIDGET(m_toolbar
));
321 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
323 // FIXME: there is no such function for toolbars in 2.0
325 if (style
& wxTB_FLAT
)
326 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
335 fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
336 m_fg
->pixel
= fg
.GetPixel();
342 wxColour
bg(255,255,196);
343 bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
344 m_bg
->pixel
= bg
.GetPixel();
346 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
350 gtk_widget_get_style(
351 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
353 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
355 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
357 m_parent
->DoAddChild( this );
366 void wxToolBar::GtkSetStyle()
368 GtkOrientation orient
;
369 GtkToolbarStyle style
;
370 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
372 gtk_toolbar_set_orientation(m_toolbar
, orient
);
373 gtk_toolbar_set_style(m_toolbar
, style
);
376 void wxToolBar::SetWindowStyleFlag( long style
)
378 wxToolBarBase::SetWindowStyleFlag(style
);
384 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
386 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
389 // if we have inserted a space before all the tools we must change the GTK
391 size_t posGtk
= m_xMargin
> 1 ? pos
+ 1 : pos
;
396 if ( tool
->IsButton() )
398 if ( !HasFlag(wxTB_NOICONS
) )
400 wxBitmap bitmap
= tool
->GetNormalBitmap();
402 wxCHECK_MSG( bitmap
.Ok(), FALSE
,
403 wxT("invalid bitmap for wxToolBar icon") );
405 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
,
406 wxT("wxToolBar doesn't support GdkBitmap") );
408 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
,
409 wxT("wxToolBar::Add needs a wxBitmap") );
411 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
413 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
415 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
416 if ( bitmap
.GetMask() )
417 mask
= bitmap
.GetMask()->GetBitmap();
419 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
420 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
422 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
424 tool
->m_pixmap
= tool_pixmap
;
428 switch ( tool
->GetStyle() )
430 case wxTOOL_STYLE_BUTTON
:
431 // for a radio button we need the widget which starts the radio
432 // group it belongs to, i.e. the first radio button immediately
433 // preceding this one
435 GtkWidget
*widget
= NULL
;
437 if ( tool
->IsRadio() )
439 wxToolBarToolsList::compatibility_iterator node
440 = wxToolBarToolsList::compatibility_iterator();
441 if ( pos
) node
= m_tools
.Item(pos
- 1);
445 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
446 if ( !tool
->IsRadio() )
449 widget
= tool
->m_item
;
451 node
= node
->GetPrevious();
456 // this is the first button in the radio button group,
457 // it will be toggled automatically by GTK so bring the
458 // internal flag in sync
463 tool
->m_item
= gtk_toolbar_insert_element
466 tool
->GetGtkChildType(),
468 tool
->GetLabel().empty()
470 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
471 tool
->GetShortHelp().empty()
473 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
474 "", // tooltip_private_text (?)
476 (GtkSignalFunc
)gtk_toolbar_callback
,
483 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
488 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
489 "enter_notify_event",
490 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
492 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
493 "leave_notify_event",
494 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
499 case wxTOOL_STYLE_SEPARATOR
:
500 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
505 case wxTOOL_STYLE_CONTROL
:
506 gtk_toolbar_insert_widget(
508 tool
->GetControl()->m_widget
,
517 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
519 m_width
= req
.width
+ m_xMargin
;
520 m_height
= req
.height
+ 2*m_yMargin
;
525 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
527 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
529 switch ( tool
->GetStyle() )
531 case wxTOOL_STYLE_CONTROL
:
532 tool
->GetControl()->Destroy();
535 case wxTOOL_STYLE_BUTTON
:
536 gtk_widget_destroy( tool
->m_item
);
539 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
545 // ----------------------------------------------------------------------------
546 // wxToolBar tools state
547 // ----------------------------------------------------------------------------
549 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
551 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
555 gtk_widget_set_sensitive( tool
->m_item
, enable
);
559 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
561 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
563 GtkWidget
*item
= tool
->m_item
;
564 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
566 wxBitmap bitmap
= tool
->GetBitmap();
569 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
571 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
574 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
579 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
581 m_blockEvent
= FALSE
;
585 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
586 bool WXUNUSED(toggle
))
588 // VZ: absolutely no idea about how to do it
589 wxFAIL_MSG( _T("not implemented") );
592 // ----------------------------------------------------------------------------
593 // wxToolBar geometry
594 // ----------------------------------------------------------------------------
596 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
597 wxCoord
WXUNUSED(y
)) const
599 // VZ: GTK+ doesn't seem to have such thing
600 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
602 return (wxToolBarToolBase
*)NULL
;
605 void wxToolBar::SetMargins( int x
, int y
)
607 wxCHECK_RET( GetToolsCount() == 0,
608 wxT("wxToolBar::SetMargins must be called before adding tools.") );
612 gtk_toolbar_append_space( m_toolbar
); // oh well
619 void wxToolBar::SetToolSeparation( int separation
)
621 // FIXME: this function disappeared
623 gtk_toolbar_set_space_size( m_toolbar
, separation
);
626 m_toolSeparation
= separation
;
629 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
631 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
635 (void)tool
->SetShortHelp(helpString
);
636 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
637 wxGTK_CONV( helpString
), "");
641 // ----------------------------------------------------------------------------
642 // wxToolBar idle handling
643 // ----------------------------------------------------------------------------
645 void wxToolBar::OnInternalIdle()
647 wxCursor cursor
= m_cursor
;
648 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
652 /* I now set the cursor the anew in every OnInternalIdle call
653 as setting the cursor in a parent window also effects the
654 windows above so that checking for the current cursor is
657 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
659 /* if the toolbar is dockable, then m_widget stands for the
660 GtkHandleBox widget, which uses its own window so that we
661 can set the cursor for it. if the toolbar is not dockable,
662 m_widget comes from m_toolbar which uses its parent's
663 window ("windowless windows") and thus we cannot set the
665 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
668 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
671 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
672 node
= node
->GetNext();
674 GtkWidget
*item
= tool
->m_item
;
677 GdkWindow
*window
= item
->window
;
681 gdk_window_set_cursor( window
, cursor
.GetCursor() );
687 if (wxUpdateUIEvent::CanUpdate(this))
688 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
691 #endif // wxUSE_TOOLBAR_NATIVE