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
;
262 m_defaultHeight
= 32;
265 wxToolBar::~wxToolBar()
271 bool wxToolBar::Create( wxWindow
*parent
,
276 const wxString
& name
)
279 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
281 if ( !PreCreation( parent
, pos
, size
) ||
282 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
284 wxFAIL_MSG( wxT("wxToolBar creation failed") );
290 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
293 // Doesn't work this way.
294 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
295 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
297 GtkOrientation orient
;
298 GtkToolbarStyle gtkStyle
;
299 GetGtkStyle(style
, &orient
, >kStyle
);
301 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new(orient
, gtkStyle
) );
304 SetToolSeparation(7);
306 if (style
& wxTB_DOCKABLE
)
308 m_widget
= gtk_handle_box_new();
309 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
310 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
312 if (style
& wxTB_FLAT
)
313 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
317 m_widget
= gtk_event_box_new();
318 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
319 ConnectWidget( m_widget
);
320 gtk_widget_show(GTK_WIDGET(m_toolbar
));
323 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
325 // FIXME: there is no such function for toolbars in 2.0
327 if (style
& wxTB_FLAT
)
328 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
337 fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
338 m_fg
->pixel
= fg
.GetPixel();
344 wxColour
bg(255,255,196);
345 bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
346 m_bg
->pixel
= bg
.GetPixel();
348 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
352 gtk_widget_get_style(
353 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
355 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
357 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
359 m_parent
->DoAddChild( this );
368 void wxToolBar::GtkSetStyle()
370 GtkOrientation orient
;
371 GtkToolbarStyle style
;
372 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
374 gtk_toolbar_set_orientation(m_toolbar
, orient
);
375 gtk_toolbar_set_style(m_toolbar
, style
);
378 void wxToolBar::SetWindowStyleFlag( long style
)
380 wxToolBarBase::SetWindowStyleFlag(style
);
386 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
388 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
391 // if we have inserted a space before all the tools we must change the GTK
393 size_t posGtk
= m_xMargin
> 1 ? pos
+ 1 : pos
;
398 if ( tool
->IsButton() )
400 if ( !HasFlag(wxTB_NOICONS
) )
402 wxBitmap bitmap
= tool
->GetNormalBitmap();
404 wxCHECK_MSG( bitmap
.Ok(), FALSE
,
405 wxT("invalid bitmap for wxToolBar icon") );
407 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
,
408 wxT("wxToolBar doesn't support GdkBitmap") );
410 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
,
411 wxT("wxToolBar::Add needs a wxBitmap") );
413 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
415 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
417 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
418 if ( bitmap
.GetMask() )
419 mask
= bitmap
.GetMask()->GetBitmap();
421 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
422 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
424 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
426 tool
->m_pixmap
= tool_pixmap
;
430 switch ( tool
->GetStyle() )
432 case wxTOOL_STYLE_BUTTON
:
433 // for a radio button we need the widget which starts the radio
434 // group it belongs to, i.e. the first radio button immediately
435 // preceding this one
437 GtkWidget
*widget
= NULL
;
439 if ( tool
->IsRadio() )
441 wxToolBarToolsList::compatibility_iterator node
442 = wxToolBarToolsList::compatibility_iterator();
443 if ( pos
) node
= m_tools
.Item(pos
- 1);
447 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
448 if ( !tool
->IsRadio() )
451 widget
= tool
->m_item
;
453 node
= node
->GetPrevious();
458 // this is the first button in the radio button group,
459 // it will be toggled automatically by GTK so bring the
460 // internal flag in sync
465 tool
->m_item
= gtk_toolbar_insert_element
468 tool
->GetGtkChildType(),
470 tool
->GetLabel().empty()
472 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
473 tool
->GetShortHelp().empty()
475 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
476 "", // tooltip_private_text (?)
478 (GtkSignalFunc
)gtk_toolbar_callback
,
485 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
490 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
491 "enter_notify_event",
492 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
494 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
495 "leave_notify_event",
496 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
501 case wxTOOL_STYLE_SEPARATOR
:
502 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
507 case wxTOOL_STYLE_CONTROL
:
508 gtk_toolbar_insert_widget(
510 tool
->GetControl()->m_widget
,
519 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
521 m_width
= req
.width
+ m_xMargin
;
522 m_height
= req
.height
+ 2*m_yMargin
;
527 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
529 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
531 switch ( tool
->GetStyle() )
533 case wxTOOL_STYLE_CONTROL
:
534 tool
->GetControl()->Destroy();
537 case wxTOOL_STYLE_BUTTON
:
538 gtk_widget_destroy( tool
->m_item
);
541 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
547 // ----------------------------------------------------------------------------
548 // wxToolBar tools state
549 // ----------------------------------------------------------------------------
551 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
553 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
557 gtk_widget_set_sensitive( tool
->m_item
, enable
);
561 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
563 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
565 GtkWidget
*item
= tool
->m_item
;
566 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
568 wxBitmap bitmap
= tool
->GetBitmap();
571 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
573 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
576 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
581 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
583 m_blockEvent
= FALSE
;
587 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
588 bool WXUNUSED(toggle
))
590 // VZ: absolutely no idea about how to do it
591 wxFAIL_MSG( _T("not implemented") );
594 // ----------------------------------------------------------------------------
595 // wxToolBar geometry
596 // ----------------------------------------------------------------------------
598 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
599 wxCoord
WXUNUSED(y
)) const
601 // VZ: GTK+ doesn't seem to have such thing
602 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
604 return (wxToolBarToolBase
*)NULL
;
607 void wxToolBar::SetMargins( int x
, int y
)
609 wxCHECK_RET( GetToolsCount() == 0,
610 wxT("wxToolBar::SetMargins must be called before adding tools.") );
614 gtk_toolbar_append_space( m_toolbar
); // oh well
621 void wxToolBar::SetToolSeparation( int separation
)
623 // FIXME: this function disappeared
625 gtk_toolbar_set_space_size( m_toolbar
, separation
);
628 m_toolSeparation
= separation
;
631 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
633 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
637 (void)tool
->SetShortHelp(helpString
);
638 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
639 wxGTK_CONV( helpString
), "");
643 // ----------------------------------------------------------------------------
644 // wxToolBar idle handling
645 // ----------------------------------------------------------------------------
647 void wxToolBar::OnInternalIdle()
649 wxCursor cursor
= m_cursor
;
650 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
654 /* I now set the cursor the anew in every OnInternalIdle call
655 as setting the cursor in a parent window also effects the
656 windows above so that checking for the current cursor is
659 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
661 /* if the toolbar is dockable, then m_widget stands for the
662 GtkHandleBox widget, which uses its own window so that we
663 can set the cursor for it. if the toolbar is not dockable,
664 m_widget comes from m_toolbar which uses its parent's
665 window ("windowless windows") and thus we cannot set the
667 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
670 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
673 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
674 node
= node
->GetNext();
676 GtkWidget
*item
= tool
->m_item
;
679 GdkWindow
*window
= item
->window
;
683 gdk_window_set_cursor( window
, cursor
.GetCursor() );
689 if (wxUpdateUIEvent::CanUpdate(this))
690 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
693 #endif // wxUSE_TOOLBAR_NATIVE