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 // this is only called for the normal buttons, i.e. not separators nor
98 GtkToolbarChildType
GetGtkChildType() const
103 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
106 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
109 wxFAIL_MSG( _T("unknown toolbar child type") );
113 return GTK_TOOLBAR_CHILD_BUTTON
;
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
130 // ============================================================================
132 // ============================================================================
134 //-----------------------------------------------------------------------------
135 // "clicked" (internal from gtk_toolbar)
136 //-----------------------------------------------------------------------------
138 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
139 wxToolBarTool
*tool
)
142 wxapp_install_idle_handler();
144 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
146 if (tbar
->m_blockEvent
) return;
148 if (g_blockEventsOnDrag
) return;
149 if (!tool
->IsEnabled()) return;
151 if (tool
->CanBeToggled())
155 wxBitmap bitmap
= tool
->GetBitmap();
158 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
160 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
163 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
167 tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() );
170 //-----------------------------------------------------------------------------
171 // "enter_notify_event" / "leave_notify_event"
172 //-----------------------------------------------------------------------------
174 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
175 GdkEventCrossing
*gdk_event
,
176 wxToolBarTool
*tool
)
178 if (g_isIdle
) wxapp_install_idle_handler();
180 if (g_blockEventsOnDrag
) return TRUE
;
182 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
185 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
186 tb
->OnMouseEnter( tool
->GetId() );
188 tb
->OnMouseEnter( -1 );
193 //-----------------------------------------------------------------------------
194 // InsertChild callback for wxToolBar
195 //-----------------------------------------------------------------------------
197 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
198 wxWindow
* WXUNUSED(child
) )
200 // we don't do anything here
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 void wxToolBarTool::Init()
210 m_pixmap
= (GtkWidget
*)NULL
;
213 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
214 const wxString
& text
,
215 const wxBitmap
& bitmap1
,
216 const wxBitmap
& bitmap2
,
218 wxObject
*clientData
,
219 const wxString
& shortHelpString
,
220 const wxString
& longHelpString
)
222 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
223 clientData
, shortHelpString
, longHelpString
);
226 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
228 return new wxToolBarTool(this, control
);
231 //-----------------------------------------------------------------------------
232 // wxToolBar construction
233 //-----------------------------------------------------------------------------
235 void wxToolBar::Init()
238 m_bg
= (GdkColor
*)NULL
;
239 m_toolbar
= (GtkToolbar
*)NULL
;
240 m_blockEvent
= FALSE
;
243 wxToolBar::~wxToolBar()
249 bool wxToolBar::Create( wxWindow
*parent
,
254 const wxString
& name
)
257 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
259 if ( !PreCreation( parent
, pos
, size
) ||
260 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
262 wxFAIL_MSG( wxT("wxToolBar creation failed") );
268 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
271 GtkOrientation orient
;
272 GtkToolbarStyle gtkStyle
;
273 GetGtkStyle(style
, &orient
, >kStyle
);
275 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new(orient
, gtkStyle
) );
278 SetToolSeparation(7);
280 if (style
& wxTB_DOCKABLE
)
282 m_widget
= gtk_handle_box_new();
283 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
284 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
286 if (style
& wxTB_FLAT
)
287 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
291 m_widget
= GTK_WIDGET(m_toolbar
);
294 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
296 // FIXME: there is no such function for toolbars in 2.0
298 if (style
& wxTB_FLAT
)
299 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
308 fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
309 m_fg
->pixel
= fg
.GetPixel();
315 wxColour
bg(255,255,196);
316 bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
317 m_bg
->pixel
= bg
.GetPixel();
319 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
323 gtk_widget_get_style(
324 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
326 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
328 SET_STYLE_FONT(g_style
, GtkGetDefaultGuiFont());
330 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
332 m_parent
->DoAddChild( this );
341 void wxToolBar::GtkSetStyle()
343 GtkOrientation orient
;
344 GtkToolbarStyle style
;
345 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
347 gtk_toolbar_set_orientation(m_toolbar
, orient
);
348 gtk_toolbar_set_style(m_toolbar
, style
);
351 void wxToolBar::SetWindowStyleFlag( long style
)
353 wxToolBarBase::SetWindowStyleFlag(style
);
359 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
361 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
363 // we have inserted a space before all the tools
364 if (m_xMargin
> 1) pos
++;
366 if ( tool
->IsButton() )
368 wxBitmap bitmap
= tool
->GetNormalBitmap();
370 wxCHECK_MSG( bitmap
.Ok(), FALSE
,
371 wxT("invalid bitmap for wxToolBar icon") );
373 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
,
374 wxT("wxToolBar doesn't support GdkBitmap") );
376 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
,
377 wxT("wxToolBar::Add needs a wxBitmap") );
379 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
381 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
383 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
384 if ( bitmap
.GetMask() )
385 mask
= bitmap
.GetMask()->GetBitmap();
387 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
388 #if (GTK_MINOR_VERSION > 0)
389 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
392 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
394 tool
->m_pixmap
= tool_pixmap
;
397 switch ( tool
->GetStyle() )
399 case wxTOOL_STYLE_BUTTON
:
400 tool
->m_item
= gtk_toolbar_insert_element
403 tool
->GetGtkChildType(),
405 tool
->GetLabel().mbc_str(),
406 tool
->GetShortHelp().mbc_str(),
407 "", // tooltip_private_text (?)
409 (GtkSignalFunc
)gtk_toolbar_callback
,
416 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
421 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
422 "enter_notify_event",
423 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
425 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
426 "leave_notify_event",
427 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
431 case wxTOOL_STYLE_SEPARATOR
:
432 gtk_toolbar_insert_space( m_toolbar
, pos
);
437 case wxTOOL_STYLE_CONTROL
:
438 gtk_toolbar_insert_widget(
440 tool
->GetControl()->m_widget
,
449 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
451 m_width
= req
.width
+ m_xMargin
;
452 m_height
= req
.height
+ 2*m_yMargin
;
457 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
459 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
461 switch ( tool
->GetStyle() )
463 case wxTOOL_STYLE_CONTROL
:
464 tool
->GetControl()->Destroy();
467 case wxTOOL_STYLE_BUTTON
:
468 gtk_widget_destroy( tool
->m_item
);
471 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
477 // ----------------------------------------------------------------------------
478 // wxToolBar tools state
479 // ----------------------------------------------------------------------------
481 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
483 #if (GTK_MINOR_VERSION > 0)
484 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
486 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
487 greyed anyway and this also disables tooltips */
489 gtk_widget_set_sensitive( tool
->m_item
, enable
);
493 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
495 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
497 GtkWidget
*item
= tool
->m_item
;
498 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
500 wxBitmap bitmap
= tool
->GetBitmap();
503 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
505 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
508 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
513 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
515 m_blockEvent
= FALSE
;
519 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
520 bool WXUNUSED(toggle
))
522 // VZ: absolutely no idea about how to do it
523 wxFAIL_MSG( _T("not implemented") );
526 // ----------------------------------------------------------------------------
527 // wxToolBar geometry
528 // ----------------------------------------------------------------------------
530 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
531 wxCoord
WXUNUSED(y
)) const
533 // VZ: GTK+ doesn't seem to have such thing
534 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
536 return (wxToolBarToolBase
*)NULL
;
539 void wxToolBar::SetMargins( int x
, int y
)
541 wxCHECK_RET( GetToolsCount() == 0,
542 wxT("wxToolBar::SetMargins must be called before adding tools.") );
544 if (x
> 1) gtk_toolbar_append_space( m_toolbar
); // oh well
550 void wxToolBar::SetToolSeparation( int separation
)
552 // FIXME: this function disappeared
554 gtk_toolbar_set_space_size( m_toolbar
, separation
);
557 m_toolSeparation
= separation
;
560 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
562 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
566 (void)tool
->SetShortHelp(helpString
);
567 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
568 helpString
.mbc_str(), "");
572 // ----------------------------------------------------------------------------
573 // wxToolBar idle handling
574 // ----------------------------------------------------------------------------
576 void wxToolBar::OnInternalIdle()
578 wxCursor cursor
= m_cursor
;
579 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
583 /* I now set the cursor the anew in every OnInternalIdle call
584 as setting the cursor in a parent window also effects the
585 windows above so that checking for the current cursor is
588 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
590 /* if the toolbar is dockable, then m_widget stands for the
591 GtkHandleBox widget, which uses its own window so that we
592 can set the cursor for it. if the toolbar is not dockable,
593 m_widget comes from m_toolbar which uses its parent's
594 window ("windowless windows") and thus we cannot set the
596 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
599 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
602 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
603 node
= node
->GetNext();
605 GtkWidget
*item
= tool
->m_item
;
608 GdkWindow
*window
= item
->window
;
612 gdk_window_set_cursor( window
, cursor
.GetCursor() );
621 #endif // wxUSE_TOOLBAR_NATIVE