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 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
192 wxBitmap bitmap
= tool
->GetBitmap();
195 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
197 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
200 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
205 //-----------------------------------------------------------------------------
206 // "enter_notify_event" / "leave_notify_event"
207 //-----------------------------------------------------------------------------
209 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
210 GdkEventCrossing
*gdk_event
,
211 wxToolBarTool
*tool
)
213 if (g_isIdle
) wxapp_install_idle_handler();
215 if (g_blockEventsOnDrag
) return TRUE
;
217 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
220 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
221 tb
->OnMouseEnter( tool
->GetId() );
223 tb
->OnMouseEnter( -1 );
228 //-----------------------------------------------------------------------------
229 // InsertChild callback for wxToolBar
230 //-----------------------------------------------------------------------------
232 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
233 wxWindow
* WXUNUSED(child
) )
235 // we don't do anything here
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 void wxToolBarTool::Init()
245 m_pixmap
= (GtkWidget
*)NULL
;
248 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
249 const wxString
& text
,
250 const wxBitmap
& bitmap1
,
251 const wxBitmap
& bitmap2
,
253 wxObject
*clientData
,
254 const wxString
& shortHelpString
,
255 const wxString
& longHelpString
)
257 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
258 clientData
, shortHelpString
, longHelpString
);
261 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
263 return new wxToolBarTool(this, control
);
266 //-----------------------------------------------------------------------------
267 // wxToolBar construction
268 //-----------------------------------------------------------------------------
270 void wxToolBar::Init()
273 m_bg
= (GdkColor
*)NULL
;
274 m_toolbar
= (GtkToolbar
*)NULL
;
275 m_blockEvent
= FALSE
;
277 m_defaultHeight
= 32;
280 wxToolBar::~wxToolBar()
286 bool wxToolBar::Create( wxWindow
*parent
,
291 const wxString
& name
)
294 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
296 if ( !PreCreation( parent
, pos
, size
) ||
297 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
299 wxFAIL_MSG( wxT("wxToolBar creation failed") );
305 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
308 // Doesn't work this way.
309 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
310 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
312 GtkOrientation orient
;
313 GtkToolbarStyle gtkStyle
;
314 GetGtkStyle(style
, &orient
, >kStyle
);
316 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new(orient
, gtkStyle
) );
319 SetToolSeparation(7);
321 if (style
& wxTB_DOCKABLE
)
323 m_widget
= gtk_handle_box_new();
324 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
325 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
327 if (style
& wxTB_FLAT
)
328 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
332 m_widget
= gtk_event_box_new();
333 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
334 ConnectWidget( m_widget
);
335 gtk_widget_show(GTK_WIDGET(m_toolbar
));
338 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
340 // FIXME: there is no such function for toolbars in 2.0
342 if (style
& wxTB_FLAT
)
343 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
352 fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
353 m_fg
->pixel
= fg
.GetPixel();
359 wxColour
bg(255,255,196);
360 bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
361 m_bg
->pixel
= bg
.GetPixel();
363 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
367 gtk_widget_get_style(
368 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
370 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
372 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
374 m_parent
->DoAddChild( this );
383 void wxToolBar::GtkSetStyle()
385 GtkOrientation orient
;
386 GtkToolbarStyle style
;
387 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
389 gtk_toolbar_set_orientation(m_toolbar
, orient
);
390 gtk_toolbar_set_style(m_toolbar
, style
);
393 void wxToolBar::SetWindowStyleFlag( long style
)
395 wxToolBarBase::SetWindowStyleFlag(style
);
401 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
403 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
406 // if we have inserted a space before all the tools we must change the GTK
408 size_t posGtk
= m_xMargin
> 1 ? pos
+ 1 : pos
;
413 if ( tool
->IsButton() )
415 if ( !HasFlag(wxTB_NOICONS
) )
417 wxBitmap bitmap
= tool
->GetNormalBitmap();
419 wxCHECK_MSG( bitmap
.Ok(), FALSE
,
420 wxT("invalid bitmap for wxToolBar icon") );
422 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
,
423 wxT("wxToolBar doesn't support GdkBitmap") );
425 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
,
426 wxT("wxToolBar::Add needs a wxBitmap") );
428 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
430 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
432 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
433 if ( bitmap
.GetMask() )
434 mask
= bitmap
.GetMask()->GetBitmap();
436 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
437 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
439 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
441 tool
->m_pixmap
= tool_pixmap
;
445 switch ( tool
->GetStyle() )
447 case wxTOOL_STYLE_BUTTON
:
448 // for a radio button we need the widget which starts the radio
449 // group it belongs to, i.e. the first radio button immediately
450 // preceding this one
452 GtkWidget
*widget
= NULL
;
454 if ( tool
->IsRadio() )
456 wxToolBarToolsList::compatibility_iterator node
457 = wxToolBarToolsList::compatibility_iterator();
458 if ( pos
) node
= m_tools
.Item(pos
- 1);
462 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
463 if ( !tool
->IsRadio() )
466 widget
= tool
->m_item
;
468 node
= node
->GetPrevious();
473 // this is the first button in the radio button group,
474 // it will be toggled automatically by GTK so bring the
475 // internal flag in sync
480 tool
->m_item
= gtk_toolbar_insert_element
483 tool
->GetGtkChildType(),
485 tool
->GetLabel().empty()
487 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
488 tool
->GetShortHelp().empty()
490 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
491 "", // tooltip_private_text (?)
493 (GtkSignalFunc
)gtk_toolbar_callback
,
500 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
505 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
506 "enter_notify_event",
507 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
509 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
510 "leave_notify_event",
511 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
516 case wxTOOL_STYLE_SEPARATOR
:
517 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
522 case wxTOOL_STYLE_CONTROL
:
523 gtk_toolbar_insert_widget(
525 tool
->GetControl()->m_widget
,
534 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
536 m_width
= req
.width
+ m_xMargin
;
537 m_height
= req
.height
+ 2*m_yMargin
;
542 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
544 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
546 switch ( tool
->GetStyle() )
548 case wxTOOL_STYLE_CONTROL
:
549 tool
->GetControl()->Destroy();
552 case wxTOOL_STYLE_BUTTON
:
553 gtk_widget_destroy( tool
->m_item
);
556 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
562 // ----------------------------------------------------------------------------
563 // wxToolBar tools state
564 // ----------------------------------------------------------------------------
566 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
568 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
572 gtk_widget_set_sensitive( tool
->m_item
, enable
);
576 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
578 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
580 GtkWidget
*item
= tool
->m_item
;
581 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
583 wxBitmap bitmap
= tool
->GetBitmap();
586 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
588 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
591 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
596 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
598 m_blockEvent
= FALSE
;
602 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
603 bool WXUNUSED(toggle
))
605 // VZ: absolutely no idea about how to do it
606 wxFAIL_MSG( _T("not implemented") );
609 // ----------------------------------------------------------------------------
610 // wxToolBar geometry
611 // ----------------------------------------------------------------------------
613 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
614 wxCoord
WXUNUSED(y
)) const
616 // VZ: GTK+ doesn't seem to have such thing
617 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
619 return (wxToolBarToolBase
*)NULL
;
622 void wxToolBar::SetMargins( int x
, int y
)
624 wxCHECK_RET( GetToolsCount() == 0,
625 wxT("wxToolBar::SetMargins must be called before adding tools.") );
629 gtk_toolbar_append_space( m_toolbar
); // oh well
636 void wxToolBar::SetToolSeparation( int separation
)
638 // FIXME: this function disappeared
640 gtk_toolbar_set_space_size( m_toolbar
, separation
);
643 m_toolSeparation
= separation
;
646 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
648 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
652 (void)tool
->SetShortHelp(helpString
);
653 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
654 wxGTK_CONV( helpString
), "");
658 // ----------------------------------------------------------------------------
659 // wxToolBar idle handling
660 // ----------------------------------------------------------------------------
662 void wxToolBar::OnInternalIdle()
664 wxCursor cursor
= m_cursor
;
665 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
669 /* I now set the cursor the anew in every OnInternalIdle call
670 as setting the cursor in a parent window also effects the
671 windows above so that checking for the current cursor is
674 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
676 /* if the toolbar is dockable, then m_widget stands for the
677 GtkHandleBox widget, which uses its own window so that we
678 can set the cursor for it. if the toolbar is not dockable,
679 m_widget comes from m_toolbar which uses its parent's
680 window ("windowless windows") and thus we cannot set the
682 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
685 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
688 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
689 node
= node
->GetNext();
691 GtkWidget
*item
= tool
->m_item
;
694 GdkWindow
*window
= item
->window
;
698 gdk_window_set_cursor( window
, cursor
.GetCursor() );
704 if (wxUpdateUIEvent::CanUpdate(this))
705 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
708 #endif // wxUSE_TOOLBAR_NATIVE