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
:
61 ( style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
: GTK_TOOLBAR_BOTH
);
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
;
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
136 // ============================================================================
138 // ============================================================================
140 //-----------------------------------------------------------------------------
141 // "clicked" (internal from gtk_toolbar)
142 //-----------------------------------------------------------------------------
144 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
145 wxToolBarTool
*tool
)
148 wxapp_install_idle_handler();
150 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
152 if (tbar
->m_blockEvent
) return;
154 if (g_blockEventsOnDrag
) return;
155 if (!tool
->IsEnabled()) return;
157 if (tool
->CanBeToggled())
161 wxBitmap bitmap
= tool
->GetBitmap();
164 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
166 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
169 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
172 if ( tool
->IsRadio() && !tool
->IsToggled() )
174 // radio button went up, don't report this as a wxWin event
179 tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() );
182 //-----------------------------------------------------------------------------
183 // "enter_notify_event" / "leave_notify_event"
184 //-----------------------------------------------------------------------------
186 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
187 GdkEventCrossing
*gdk_event
,
188 wxToolBarTool
*tool
)
190 if (g_isIdle
) wxapp_install_idle_handler();
192 if (g_blockEventsOnDrag
) return TRUE
;
194 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
197 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
198 tb
->OnMouseEnter( tool
->GetId() );
200 tb
->OnMouseEnter( -1 );
205 //-----------------------------------------------------------------------------
206 // InsertChild callback for wxToolBar
207 //-----------------------------------------------------------------------------
209 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
210 wxWindow
* WXUNUSED(child
) )
212 // we don't do anything here
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 void wxToolBarTool::Init()
222 m_pixmap
= (GtkWidget
*)NULL
;
225 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
226 const wxString
& text
,
227 const wxBitmap
& bitmap1
,
228 const wxBitmap
& bitmap2
,
230 wxObject
*clientData
,
231 const wxString
& shortHelpString
,
232 const wxString
& longHelpString
)
234 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
235 clientData
, shortHelpString
, longHelpString
);
238 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
240 return new wxToolBarTool(this, control
);
243 //-----------------------------------------------------------------------------
244 // wxToolBar construction
245 //-----------------------------------------------------------------------------
247 void wxToolBar::Init()
250 m_bg
= (GdkColor
*)NULL
;
251 m_toolbar
= (GtkToolbar
*)NULL
;
252 m_blockEvent
= FALSE
;
255 wxToolBar::~wxToolBar()
261 bool wxToolBar::Create( wxWindow
*parent
,
266 const wxString
& name
)
269 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
271 if ( !PreCreation( parent
, pos
, size
) ||
272 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
274 wxFAIL_MSG( wxT("wxToolBar creation failed") );
280 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
283 // Doesn't work this way.
284 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
285 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
287 GtkOrientation orient
;
288 GtkToolbarStyle gtkStyle
;
289 GetGtkStyle(style
, &orient
, >kStyle
);
291 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 // FIXME: there is no such function for toolbars in 2.0
317 if (style
& wxTB_FLAT
)
318 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
327 fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
328 m_fg
->pixel
= fg
.GetPixel();
334 wxColour
bg(255,255,196);
335 bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
336 m_bg
->pixel
= bg
.GetPixel();
338 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
342 gtk_widget_get_style(
343 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
345 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
347 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
349 m_parent
->DoAddChild( this );
358 void wxToolBar::GtkSetStyle()
360 GtkOrientation orient
;
361 GtkToolbarStyle style
;
362 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
364 gtk_toolbar_set_orientation(m_toolbar
, orient
);
365 gtk_toolbar_set_style(m_toolbar
, style
);
368 void wxToolBar::SetWindowStyleFlag( long style
)
370 wxToolBarBase::SetWindowStyleFlag(style
);
376 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
378 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
381 // if we have inserted a space before all the tools we must change the GTK
383 size_t posGtk
= m_xMargin
> 1 ? pos
+ 1 : pos
;
388 if ( tool
->IsButton() )
390 if ( !HasFlag(wxTB_NOICONS
) )
392 wxBitmap bitmap
= tool
->GetNormalBitmap();
394 wxCHECK_MSG( bitmap
.Ok(), FALSE
,
395 wxT("invalid bitmap for wxToolBar icon") );
397 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
,
398 wxT("wxToolBar doesn't support GdkBitmap") );
400 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
,
401 wxT("wxToolBar::Add needs a wxBitmap") );
403 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
405 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
407 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
408 if ( bitmap
.GetMask() )
409 mask
= bitmap
.GetMask()->GetBitmap();
411 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
412 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
414 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
416 tool
->m_pixmap
= tool_pixmap
;
420 switch ( tool
->GetStyle() )
422 case wxTOOL_STYLE_BUTTON
:
423 // for a radio button we need the widget which starts the radio
424 // group it belongs to, i.e. the first radio button immediately
425 // preceding this one
427 GtkWidget
*widget
= NULL
;
429 if ( tool
->IsRadio() )
431 wxToolBarToolsList::Node
*node
= pos
? m_tools
.Item(pos
- 1)
435 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
436 if ( !tool
->IsRadio() )
439 widget
= tool
->m_item
;
441 node
= node
->GetPrevious();
446 // this is the first button in the radio button group,
447 // it will be toggled automatically by GTK so bring the
448 // internal flag in sync
453 tool
->m_item
= gtk_toolbar_insert_element
456 tool
->GetGtkChildType(),
458 tool
->GetLabel().empty()
460 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
461 tool
->GetShortHelp().empty()
463 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
464 "", // tooltip_private_text (?)
466 (GtkSignalFunc
)gtk_toolbar_callback
,
473 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
478 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
479 "enter_notify_event",
480 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
482 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
483 "leave_notify_event",
484 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
489 case wxTOOL_STYLE_SEPARATOR
:
490 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
495 case wxTOOL_STYLE_CONTROL
:
496 gtk_toolbar_insert_widget(
498 tool
->GetControl()->m_widget
,
507 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
509 m_width
= req
.width
+ m_xMargin
;
510 m_height
= req
.height
+ 2*m_yMargin
;
515 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
517 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
519 switch ( tool
->GetStyle() )
521 case wxTOOL_STYLE_CONTROL
:
522 tool
->GetControl()->Destroy();
525 case wxTOOL_STYLE_BUTTON
:
526 gtk_widget_destroy( tool
->m_item
);
529 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
535 // ----------------------------------------------------------------------------
536 // wxToolBar tools state
537 // ----------------------------------------------------------------------------
539 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
541 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
545 gtk_widget_set_sensitive( tool
->m_item
, enable
);
549 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
551 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
553 GtkWidget
*item
= tool
->m_item
;
554 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
556 wxBitmap bitmap
= tool
->GetBitmap();
559 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
561 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
564 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
569 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
571 m_blockEvent
= FALSE
;
575 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
576 bool WXUNUSED(toggle
))
578 // VZ: absolutely no idea about how to do it
579 wxFAIL_MSG( _T("not implemented") );
582 // ----------------------------------------------------------------------------
583 // wxToolBar geometry
584 // ----------------------------------------------------------------------------
586 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
587 wxCoord
WXUNUSED(y
)) const
589 // VZ: GTK+ doesn't seem to have such thing
590 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
592 return (wxToolBarToolBase
*)NULL
;
595 void wxToolBar::SetMargins( int x
, int y
)
597 wxCHECK_RET( GetToolsCount() == 0,
598 wxT("wxToolBar::SetMargins must be called before adding tools.") );
602 gtk_toolbar_append_space( m_toolbar
); // oh well
609 void wxToolBar::SetToolSeparation( int separation
)
611 // FIXME: this function disappeared
613 gtk_toolbar_set_space_size( m_toolbar
, separation
);
616 m_toolSeparation
= separation
;
619 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
621 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
625 (void)tool
->SetShortHelp(helpString
);
626 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
627 wxGTK_CONV( helpString
), "");
631 // ----------------------------------------------------------------------------
632 // wxToolBar idle handling
633 // ----------------------------------------------------------------------------
635 void wxToolBar::OnInternalIdle()
637 wxCursor cursor
= m_cursor
;
638 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
642 /* I now set the cursor the anew in every OnInternalIdle call
643 as setting the cursor in a parent window also effects the
644 windows above so that checking for the current cursor is
647 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
649 /* if the toolbar is dockable, then m_widget stands for the
650 GtkHandleBox widget, which uses its own window so that we
651 can set the cursor for it. if the toolbar is not dockable,
652 m_widget comes from m_toolbar which uses its parent's
653 window ("windowless windows") and thus we cannot set the
655 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
658 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
661 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
662 node
= node
->GetNext();
664 GtkWidget
*item
= tool
->m_item
;
667 GdkWindow
*window
= item
->window
;
671 gdk_window_set_cursor( window
, cursor
.GetCursor() );
677 if (wxUpdateUIEvent::CanUpdate(this))
678 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
681 #endif // wxUSE_TOOLBAR_NATIVE