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 // ----------------------------------------------------------------------------
20 #pragma implementation "tbargtk.h"
23 #include "wx/toolbar.h"
25 #if wxUSE_TOOLBAR_NATIVE
30 #include "wx/gtk/private.h"
32 extern GdkFont
*GtkGetDefaultGuiFont();
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
39 extern void wxapp_install_idle_handler();
43 extern bool g_blockEventsOnDrag
;
44 extern wxCursor g_globalCursor
;
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // translate wxWindows toolbar style flags to GTK orientation and style
51 static void GetGtkStyle(long style
,
52 GtkOrientation
*orient
, GtkToolbarStyle
*gtkStyle
)
54 *orient
= style
& wxTB_VERTICAL
? GTK_ORIENTATION_VERTICAL
55 : GTK_ORIENTATION_HORIZONTAL
;
58 if ( style
& wxTB_TEXT
)
60 *gtkStyle
= style
& wxTB_NOICONS
? GTK_TOOLBAR_TEXT
: GTK_TOOLBAR_BOTH
;
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
)
91 : wxToolBarToolBase(tbar
, control
)
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( _T("unknown toolbar child type") );
118 return GTK_TOOLBAR_CHILD_BUTTON
;
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
135 // ============================================================================
137 // ============================================================================
139 //-----------------------------------------------------------------------------
140 // "clicked" (internal from gtk_toolbar)
141 //-----------------------------------------------------------------------------
143 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
144 wxToolBarTool
*tool
)
147 wxapp_install_idle_handler();
149 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
151 if (tbar
->m_blockEvent
) return;
153 if (g_blockEventsOnDrag
) return;
154 if (!tool
->IsEnabled()) return;
156 if (tool
->CanBeToggled())
160 wxBitmap bitmap
= tool
->GetBitmap();
163 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
165 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
168 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
171 if ( tool
->IsRadio() && !tool
->IsToggled() )
173 // radio button went up, don't report this as a wxWin event
178 tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() );
181 //-----------------------------------------------------------------------------
182 // "enter_notify_event" / "leave_notify_event"
183 //-----------------------------------------------------------------------------
185 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
186 GdkEventCrossing
*gdk_event
,
187 wxToolBarTool
*tool
)
189 if (g_isIdle
) wxapp_install_idle_handler();
191 if (g_blockEventsOnDrag
) return TRUE
;
193 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
196 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
197 tb
->OnMouseEnter( tool
->GetId() );
199 tb
->OnMouseEnter( -1 );
204 //-----------------------------------------------------------------------------
205 // InsertChild callback for wxToolBar
206 //-----------------------------------------------------------------------------
208 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
209 wxWindow
* WXUNUSED(child
) )
211 // we don't do anything here
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 void wxToolBarTool::Init()
221 m_pixmap
= (GtkWidget
*)NULL
;
224 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
225 const wxString
& text
,
226 const wxBitmap
& bitmap1
,
227 const wxBitmap
& bitmap2
,
229 wxObject
*clientData
,
230 const wxString
& shortHelpString
,
231 const wxString
& longHelpString
)
233 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
234 clientData
, shortHelpString
, longHelpString
);
237 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
239 return new wxToolBarTool(this, control
);
242 //-----------------------------------------------------------------------------
243 // wxToolBar construction
244 //-----------------------------------------------------------------------------
246 void wxToolBar::Init()
249 m_bg
= (GdkColor
*)NULL
;
250 m_toolbar
= (GtkToolbar
*)NULL
;
251 m_blockEvent
= FALSE
;
254 wxToolBar::~wxToolBar()
260 bool wxToolBar::Create( wxWindow
*parent
,
265 const wxString
& name
)
268 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
270 if ( !PreCreation( parent
, pos
, size
) ||
271 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
273 wxFAIL_MSG( wxT("wxToolBar creation failed") );
279 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
282 // Doesn't work this way.
283 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
284 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
286 GtkOrientation orient
;
287 GtkToolbarStyle gtkStyle
;
288 GetGtkStyle(style
, &orient
, >kStyle
);
290 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new(orient
, gtkStyle
) );
293 SetToolSeparation(7);
295 if (style
& wxTB_DOCKABLE
)
297 m_widget
= gtk_handle_box_new();
298 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
299 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
301 if (style
& wxTB_FLAT
)
302 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
306 m_widget
= gtk_event_box_new();
307 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
308 ConnectWidget( m_widget
);
309 gtk_widget_show(GTK_WIDGET(m_toolbar
));
312 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
314 // FIXME: there is no such function for toolbars in 2.0
316 if (style
& wxTB_FLAT
)
317 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
326 fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
327 m_fg
->pixel
= fg
.GetPixel();
333 wxColour
bg(255,255,196);
334 bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
335 m_bg
->pixel
= bg
.GetPixel();
337 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
341 gtk_widget_get_style(
342 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
344 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
346 SET_STYLE_FONT(g_style
, GtkGetDefaultGuiFont());
348 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
350 m_parent
->DoAddChild( this );
359 void wxToolBar::GtkSetStyle()
361 GtkOrientation orient
;
362 GtkToolbarStyle style
;
363 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
365 gtk_toolbar_set_orientation(m_toolbar
, orient
);
366 gtk_toolbar_set_style(m_toolbar
, style
);
369 void wxToolBar::SetWindowStyleFlag( long style
)
371 wxToolBarBase::SetWindowStyleFlag(style
);
377 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
379 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
382 // if we have inserted a space before all the tools we must change the GTK
384 size_t posGtk
= m_xMargin
> 1 ? pos
+ 1 : pos
;
389 if ( tool
->IsButton() )
391 if ( !HasFlag(wxTB_NOICONS
) )
393 wxBitmap bitmap
= tool
->GetNormalBitmap();
395 wxCHECK_MSG( bitmap
.Ok(), FALSE
,
396 wxT("invalid bitmap for wxToolBar icon") );
398 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
,
399 wxT("wxToolBar doesn't support GdkBitmap") );
401 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
,
402 wxT("wxToolBar::Add needs a wxBitmap") );
404 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
406 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
408 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
409 if ( bitmap
.GetMask() )
410 mask
= bitmap
.GetMask()->GetBitmap();
412 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
413 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
415 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
417 tool
->m_pixmap
= tool_pixmap
;
421 switch ( tool
->GetStyle() )
423 case wxTOOL_STYLE_BUTTON
:
424 // for a radio button we need the widget which starts the radio
425 // group it belongs to, i.e. the first radio button immediately
426 // preceding this one
428 GtkWidget
*widget
= NULL
;
430 if ( tool
->IsRadio() )
432 wxToolBarToolsList::Node
*node
= pos
? m_tools
.Item(pos
- 1)
436 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
437 if ( !tool
->IsRadio() )
440 widget
= tool
->m_item
;
442 node
= node
->GetPrevious();
447 // this is the first button in the radio button group,
448 // it will be toggled automatically by GTK so bring the
449 // internal flag in sync
454 tool
->m_item
= gtk_toolbar_insert_element
457 tool
->GetGtkChildType(),
459 tool
->GetLabel().empty()
461 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
462 tool
->GetShortHelp().empty()
464 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
465 "", // tooltip_private_text (?)
467 (GtkSignalFunc
)gtk_toolbar_callback
,
474 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
479 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
480 "enter_notify_event",
481 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
483 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
484 "leave_notify_event",
485 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
490 case wxTOOL_STYLE_SEPARATOR
:
491 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
496 case wxTOOL_STYLE_CONTROL
:
497 gtk_toolbar_insert_widget(
499 tool
->GetControl()->m_widget
,
508 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
510 m_width
= req
.width
+ m_xMargin
;
511 m_height
= req
.height
+ 2*m_yMargin
;
516 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
518 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
520 switch ( tool
->GetStyle() )
522 case wxTOOL_STYLE_CONTROL
:
523 tool
->GetControl()->Destroy();
526 case wxTOOL_STYLE_BUTTON
:
527 gtk_widget_destroy( tool
->m_item
);
530 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
536 // ----------------------------------------------------------------------------
537 // wxToolBar tools state
538 // ----------------------------------------------------------------------------
540 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
542 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
546 gtk_widget_set_sensitive( tool
->m_item
, enable
);
550 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
552 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
554 GtkWidget
*item
= tool
->m_item
;
555 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
557 wxBitmap bitmap
= tool
->GetBitmap();
560 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
562 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
565 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
570 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
572 m_blockEvent
= FALSE
;
576 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
577 bool WXUNUSED(toggle
))
579 // VZ: absolutely no idea about how to do it
580 wxFAIL_MSG( _T("not implemented") );
583 // ----------------------------------------------------------------------------
584 // wxToolBar geometry
585 // ----------------------------------------------------------------------------
587 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
588 wxCoord
WXUNUSED(y
)) const
590 // VZ: GTK+ doesn't seem to have such thing
591 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
593 return (wxToolBarToolBase
*)NULL
;
596 void wxToolBar::SetMargins( int x
, int y
)
598 wxCHECK_RET( GetToolsCount() == 0,
599 wxT("wxToolBar::SetMargins must be called before adding tools.") );
603 gtk_toolbar_append_space( m_toolbar
); // oh well
610 void wxToolBar::SetToolSeparation( int separation
)
612 // FIXME: this function disappeared
614 gtk_toolbar_set_space_size( m_toolbar
, separation
);
617 m_toolSeparation
= separation
;
620 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
622 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
626 (void)tool
->SetShortHelp(helpString
);
627 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
628 wxGTK_CONV( helpString
), "");
632 // ----------------------------------------------------------------------------
633 // wxToolBar idle handling
634 // ----------------------------------------------------------------------------
636 void wxToolBar::OnInternalIdle()
638 wxCursor cursor
= m_cursor
;
639 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
643 /* I now set the cursor the anew in every OnInternalIdle call
644 as setting the cursor in a parent window also effects the
645 windows above so that checking for the current cursor is
648 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
650 /* if the toolbar is dockable, then m_widget stands for the
651 GtkHandleBox widget, which uses its own window so that we
652 can set the cursor for it. if the toolbar is not dockable,
653 m_widget comes from m_toolbar which uses its parent's
654 window ("windowless windows") and thus we cannot set the
656 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
659 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
662 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
663 node
= node
->GetNext();
665 GtkWidget
*item
= tool
->m_item
;
668 GdkWindow
*window
= item
->window
;
672 gdk_window_set_cursor( window
, cursor
.GetCursor() );
681 #endif // wxUSE_TOOLBAR_NATIVE