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
64 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
68 else // no text, hence we must have the icons or what would we show?
70 *gtkStyle
= GTK_TOOLBAR_ICONS
;
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 class wxToolBarTool
: public wxToolBarToolBase
81 wxToolBarTool(wxToolBar
*tbar
,
83 const wxString
& label
,
84 const wxBitmap
& bitmap1
,
85 const wxBitmap
& bitmap2
,
88 const wxString
& shortHelpString
,
89 const wxString
& longHelpString
)
90 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
91 clientData
, shortHelpString
, longHelpString
)
96 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
97 : wxToolBarToolBase(tbar
, control
)
102 // is this a radio button?
104 // unlike GetKind(), can be called for any kind of tools, not just buttons
105 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
107 // this is only called for the normal buttons, i.e. not separators nor
109 GtkToolbarChildType
GetGtkChildType() const
114 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
117 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
120 wxFAIL_MSG( _T("unknown toolbar child type") );
124 return GTK_TOOLBAR_CHILD_BUTTON
;
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
141 // ============================================================================
143 // ============================================================================
145 //-----------------------------------------------------------------------------
146 // "clicked" (internal from gtk_toolbar)
147 //-----------------------------------------------------------------------------
149 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
150 wxToolBarTool
*tool
)
153 wxapp_install_idle_handler();
155 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
157 if (tbar
->m_blockEvent
) return;
159 if (g_blockEventsOnDrag
) return;
160 if (!tool
->IsEnabled()) return;
162 if (tool
->CanBeToggled())
166 wxBitmap bitmap
= tool
->GetBitmap();
169 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
171 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
174 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
177 if ( tool
->IsRadio() && !tool
->IsToggled() )
179 // radio button went up, don't report this as a wxWin event
184 tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() );
187 //-----------------------------------------------------------------------------
188 // "enter_notify_event" / "leave_notify_event"
189 //-----------------------------------------------------------------------------
191 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
192 GdkEventCrossing
*gdk_event
,
193 wxToolBarTool
*tool
)
195 if (g_isIdle
) wxapp_install_idle_handler();
197 if (g_blockEventsOnDrag
) return TRUE
;
199 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
202 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
203 tb
->OnMouseEnter( tool
->GetId() );
205 tb
->OnMouseEnter( -1 );
210 //-----------------------------------------------------------------------------
211 // InsertChild callback for wxToolBar
212 //-----------------------------------------------------------------------------
214 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
215 wxWindow
* WXUNUSED(child
) )
217 // we don't do anything here
220 // ----------------------------------------------------------------------------
222 // ----------------------------------------------------------------------------
224 void wxToolBarTool::Init()
227 m_pixmap
= (GtkWidget
*)NULL
;
230 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
231 const wxString
& text
,
232 const wxBitmap
& bitmap1
,
233 const wxBitmap
& bitmap2
,
235 wxObject
*clientData
,
236 const wxString
& shortHelpString
,
237 const wxString
& longHelpString
)
239 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
240 clientData
, shortHelpString
, longHelpString
);
243 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
245 return new wxToolBarTool(this, control
);
248 //-----------------------------------------------------------------------------
249 // wxToolBar construction
250 //-----------------------------------------------------------------------------
252 void wxToolBar::Init()
255 m_bg
= (GdkColor
*)NULL
;
256 m_toolbar
= (GtkToolbar
*)NULL
;
257 m_blockEvent
= FALSE
;
260 wxToolBar::~wxToolBar()
266 bool wxToolBar::Create( wxWindow
*parent
,
271 const wxString
& name
)
274 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
276 if ( !PreCreation( parent
, pos
, size
) ||
277 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
279 wxFAIL_MSG( wxT("wxToolBar creation failed") );
285 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
288 // Doesn't work this way.
289 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
290 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
292 GtkOrientation orient
;
293 GtkToolbarStyle gtkStyle
;
294 GetGtkStyle(style
, &orient
, >kStyle
);
296 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new(orient
, gtkStyle
) );
299 SetToolSeparation(7);
301 if (style
& wxTB_DOCKABLE
)
303 m_widget
= gtk_handle_box_new();
304 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
305 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
307 if (style
& wxTB_FLAT
)
308 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
312 m_widget
= gtk_event_box_new();
313 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
314 ConnectWidget( m_widget
);
315 gtk_widget_show(GTK_WIDGET(m_toolbar
));
318 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
320 // FIXME: there is no such function for toolbars in 2.0
322 if (style
& wxTB_FLAT
)
323 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
332 fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
333 m_fg
->pixel
= fg
.GetPixel();
339 wxColour
bg(255,255,196);
340 bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
341 m_bg
->pixel
= bg
.GetPixel();
343 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
347 gtk_widget_get_style(
348 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
350 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
352 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
354 m_parent
->DoAddChild( this );
363 void wxToolBar::GtkSetStyle()
365 GtkOrientation orient
;
366 GtkToolbarStyle style
;
367 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
369 gtk_toolbar_set_orientation(m_toolbar
, orient
);
370 gtk_toolbar_set_style(m_toolbar
, style
);
373 void wxToolBar::SetWindowStyleFlag( long style
)
375 wxToolBarBase::SetWindowStyleFlag(style
);
381 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
383 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
386 // if we have inserted a space before all the tools we must change the GTK
388 size_t posGtk
= m_xMargin
> 1 ? pos
+ 1 : pos
;
393 if ( tool
->IsButton() )
395 if ( !HasFlag(wxTB_NOICONS
) )
397 wxBitmap bitmap
= tool
->GetNormalBitmap();
399 wxCHECK_MSG( bitmap
.Ok(), FALSE
,
400 wxT("invalid bitmap for wxToolBar icon") );
402 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
,
403 wxT("wxToolBar doesn't support GdkBitmap") );
405 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
,
406 wxT("wxToolBar::Add needs a wxBitmap") );
408 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
410 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
412 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
413 if ( bitmap
.GetMask() )
414 mask
= bitmap
.GetMask()->GetBitmap();
416 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
417 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
419 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
421 tool
->m_pixmap
= tool_pixmap
;
425 switch ( tool
->GetStyle() )
427 case wxTOOL_STYLE_BUTTON
:
428 // for a radio button we need the widget which starts the radio
429 // group it belongs to, i.e. the first radio button immediately
430 // preceding this one
432 GtkWidget
*widget
= NULL
;
434 if ( tool
->IsRadio() )
436 wxToolBarToolsList::compatibility_iterator node
437 = wxToolBarToolsList::compatibility_iterator();
438 if ( pos
) node
= m_tools
.Item(pos
- 1);
442 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
443 if ( !tool
->IsRadio() )
446 widget
= tool
->m_item
;
448 node
= node
->GetPrevious();
453 // this is the first button in the radio button group,
454 // it will be toggled automatically by GTK so bring the
455 // internal flag in sync
460 tool
->m_item
= gtk_toolbar_insert_element
463 tool
->GetGtkChildType(),
465 tool
->GetLabel().empty()
467 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
468 tool
->GetShortHelp().empty()
470 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
471 "", // tooltip_private_text (?)
473 (GtkSignalFunc
)gtk_toolbar_callback
,
480 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
485 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
486 "enter_notify_event",
487 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
489 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
490 "leave_notify_event",
491 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
496 case wxTOOL_STYLE_SEPARATOR
:
497 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
502 case wxTOOL_STYLE_CONTROL
:
503 gtk_toolbar_insert_widget(
505 tool
->GetControl()->m_widget
,
514 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
516 m_width
= req
.width
+ m_xMargin
;
517 m_height
= req
.height
+ 2*m_yMargin
;
522 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
524 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
526 switch ( tool
->GetStyle() )
528 case wxTOOL_STYLE_CONTROL
:
529 tool
->GetControl()->Destroy();
532 case wxTOOL_STYLE_BUTTON
:
533 gtk_widget_destroy( tool
->m_item
);
536 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
542 // ----------------------------------------------------------------------------
543 // wxToolBar tools state
544 // ----------------------------------------------------------------------------
546 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
548 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
552 gtk_widget_set_sensitive( tool
->m_item
, enable
);
556 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
558 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
560 GtkWidget
*item
= tool
->m_item
;
561 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
563 wxBitmap bitmap
= tool
->GetBitmap();
566 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
568 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
571 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
576 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
578 m_blockEvent
= FALSE
;
582 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
583 bool WXUNUSED(toggle
))
585 // VZ: absolutely no idea about how to do it
586 wxFAIL_MSG( _T("not implemented") );
589 // ----------------------------------------------------------------------------
590 // wxToolBar geometry
591 // ----------------------------------------------------------------------------
593 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
594 wxCoord
WXUNUSED(y
)) const
596 // VZ: GTK+ doesn't seem to have such thing
597 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
599 return (wxToolBarToolBase
*)NULL
;
602 void wxToolBar::SetMargins( int x
, int y
)
604 wxCHECK_RET( GetToolsCount() == 0,
605 wxT("wxToolBar::SetMargins must be called before adding tools.") );
609 gtk_toolbar_append_space( m_toolbar
); // oh well
616 void wxToolBar::SetToolSeparation( int separation
)
618 // FIXME: this function disappeared
620 gtk_toolbar_set_space_size( m_toolbar
, separation
);
623 m_toolSeparation
= separation
;
626 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
628 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
632 (void)tool
->SetShortHelp(helpString
);
633 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
634 wxGTK_CONV( helpString
), "");
638 // ----------------------------------------------------------------------------
639 // wxToolBar idle handling
640 // ----------------------------------------------------------------------------
642 void wxToolBar::OnInternalIdle()
644 wxCursor cursor
= m_cursor
;
645 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
649 /* I now set the cursor the anew in every OnInternalIdle call
650 as setting the cursor in a parent window also effects the
651 windows above so that checking for the current cursor is
654 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
656 /* if the toolbar is dockable, then m_widget stands for the
657 GtkHandleBox widget, which uses its own window so that we
658 can set the cursor for it. if the toolbar is not dockable,
659 m_widget comes from m_toolbar which uses its parent's
660 window ("windowless windows") and thus we cannot set the
662 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
665 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
668 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
669 node
= node
->GetNext();
671 GtkWidget
*item
= tool
->m_item
;
674 GdkWindow
*window
= item
->window
;
678 gdk_window_set_cursor( window
, cursor
.GetCursor() );
684 if (wxUpdateUIEvent::CanUpdate(this))
685 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
688 #endif // wxUSE_TOOLBAR_NATIVE