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 class wxToolBarTool
: public wxToolBarToolBase
53 wxToolBarTool(wxToolBar
*tbar
,
55 const wxBitmap
& bitmap1
,
56 const wxBitmap
& bitmap2
,
59 const wxString
& shortHelpString
,
60 const wxString
& longHelpString
)
61 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
62 clientData
, shortHelpString
, longHelpString
)
67 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
68 : wxToolBarToolBase(tbar
, control
)
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
86 // ============================================================================
88 // ============================================================================
90 //-----------------------------------------------------------------------------
91 // "clicked" (internal from gtk_toolbar)
92 //-----------------------------------------------------------------------------
94 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
98 wxapp_install_idle_handler();
100 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
102 if (tbar
->m_blockEvent
) return;
104 if (g_blockEventsOnDrag
) return;
105 if (!tool
->IsEnabled()) return;
107 if (tool
->CanBeToggled())
111 wxBitmap bitmap
= tool
->GetBitmap();
114 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
116 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
119 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
123 tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() );
126 //-----------------------------------------------------------------------------
127 // "enter_notify_event" / "leave_notify_event"
128 //-----------------------------------------------------------------------------
130 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
131 GdkEventCrossing
*gdk_event
,
132 wxToolBarTool
*tool
)
134 if (g_isIdle
) wxapp_install_idle_handler();
136 if (g_blockEventsOnDrag
) return TRUE
;
138 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
141 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
142 tb
->OnMouseEnter( tool
->GetId() );
144 tb
->OnMouseEnter( -1 );
149 //-----------------------------------------------------------------------------
150 // InsertChild callback for wxToolBar
151 //-----------------------------------------------------------------------------
153 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
154 wxWindow
* WXUNUSED(child
) )
156 // we don't do anything here
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 void wxToolBarTool::Init()
166 m_pixmap
= (GtkWidget
*)NULL
;
169 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
170 const wxBitmap
& bitmap1
,
171 const wxBitmap
& bitmap2
,
173 wxObject
*clientData
,
174 const wxString
& shortHelpString
,
175 const wxString
& longHelpString
)
177 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
178 clientData
, shortHelpString
, longHelpString
);
181 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
183 return new wxToolBarTool(this, control
);
186 //-----------------------------------------------------------------------------
187 // wxToolBar construction
188 //-----------------------------------------------------------------------------
190 void wxToolBar::Init()
193 m_bg
= (GdkColor
*)NULL
;
194 m_toolbar
= (GtkToolbar
*)NULL
;
195 m_blockEvent
= FALSE
;
198 wxToolBar::~wxToolBar()
204 bool wxToolBar::Create( wxWindow
*parent
,
209 const wxString
& name
)
212 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
214 if ( !PreCreation( parent
, pos
, size
) ||
215 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
217 wxFAIL_MSG( wxT("wxToolBar creation failed") );
222 GtkOrientation orient
= style
& wxTB_VERTICAL
? GTK_ORIENTATION_VERTICAL
223 : GTK_ORIENTATION_HORIZONTAL
;
226 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
227 gtk_toolbar_set_orientation(m_toolbar
, orient
);
228 gtk_toolbar_set_style(m_toolbar
, GTK_TOOLBAR_ICONS
);
230 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( orient
, GTK_TOOLBAR_ICONS
) );
233 SetToolSeparation(7);
235 if (style
& wxTB_DOCKABLE
)
237 m_widget
= gtk_handle_box_new();
238 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
239 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
241 if (style
& wxTB_FLAT
)
242 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
246 m_widget
= GTK_WIDGET(m_toolbar
);
249 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
251 // FIXME: there is no such function for toolbars in 2.0
253 if (style
& wxTB_FLAT
)
254 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
263 fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
264 m_fg
->pixel
= fg
.GetPixel();
270 wxColour
bg(255,255,196);
271 bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
272 m_bg
->pixel
= bg
.GetPixel();
274 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
278 gtk_widget_get_style(
279 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
281 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
283 SET_STYLE_FONT(g_style
, GtkGetDefaultGuiFont());
285 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
287 m_parent
->DoAddChild( this );
296 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
298 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
300 // we have inserted a space before all the tools
301 if (m_xMargin
> 1) pos
++;
303 if ( tool
->IsButton() )
305 wxBitmap bitmap
= tool
->GetNormalBitmap();
307 wxCHECK_MSG( bitmap
.Ok(), FALSE
,
308 wxT("invalid bitmap for wxToolBar icon") );
310 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
,
311 wxT("wxToolBar doesn't support GdkBitmap") );
313 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
,
314 wxT("wxToolBar::Add needs a wxBitmap") );
316 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
318 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
320 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
321 if ( bitmap
.GetMask() )
322 mask
= bitmap
.GetMask()->GetBitmap();
324 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
325 #if (GTK_MINOR_VERSION > 0)
326 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
329 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
331 tool
->m_pixmap
= tool_pixmap
;
334 switch ( tool
->GetStyle() )
336 case wxTOOL_STYLE_BUTTON
:
337 tool
->m_item
= gtk_toolbar_insert_element
341 ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
342 : GTK_TOOLBAR_CHILD_BUTTON
,
345 tool
->GetShortHelp().mbc_str(),
346 "", // tooltip_private_text (?)
348 (GtkSignalFunc
)gtk_toolbar_callback
,
355 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
360 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
361 "enter_notify_event",
362 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
364 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
365 "leave_notify_event",
366 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback
),
370 case wxTOOL_STYLE_SEPARATOR
:
371 gtk_toolbar_insert_space( m_toolbar
, pos
);
376 case wxTOOL_STYLE_CONTROL
:
377 gtk_toolbar_insert_widget(
379 tool
->GetControl()->m_widget
,
388 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
390 m_width
= req
.width
+ m_xMargin
;
391 m_height
= req
.height
+ 2*m_yMargin
;
396 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
398 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
400 switch ( tool
->GetStyle() )
402 case wxTOOL_STYLE_CONTROL
:
403 tool
->GetControl()->Destroy();
406 case wxTOOL_STYLE_BUTTON
:
407 gtk_widget_destroy( tool
->m_item
);
410 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
416 // ----------------------------------------------------------------------------
417 // wxToolBar tools state
418 // ----------------------------------------------------------------------------
420 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
422 #if (GTK_MINOR_VERSION > 0)
423 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
425 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
426 greyed anyway and this also disables tooltips */
428 gtk_widget_set_sensitive( tool
->m_item
, enable
);
432 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
434 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
436 GtkWidget
*item
= tool
->m_item
;
437 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
439 wxBitmap bitmap
= tool
->GetBitmap();
442 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
444 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
447 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
452 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
454 m_blockEvent
= FALSE
;
458 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
459 bool WXUNUSED(toggle
))
461 // VZ: absolutely no idea about how to do it
462 wxFAIL_MSG( _T("not implemented") );
465 // ----------------------------------------------------------------------------
466 // wxToolBar geometry
467 // ----------------------------------------------------------------------------
469 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
470 wxCoord
WXUNUSED(y
)) const
472 // VZ: GTK+ doesn't seem to have such thing
473 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
475 return (wxToolBarToolBase
*)NULL
;
478 void wxToolBar::SetMargins( int x
, int y
)
480 wxCHECK_RET( GetToolsCount() == 0,
481 wxT("wxToolBar::SetMargins must be called before adding tools.") );
483 if (x
> 1) gtk_toolbar_append_space( m_toolbar
); // oh well
489 void wxToolBar::SetToolSeparation( int separation
)
491 // FIXME: this function disappeared
493 gtk_toolbar_set_space_size( m_toolbar
, separation
);
496 m_toolSeparation
= separation
;
499 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
501 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
505 (void)tool
->SetShortHelp(helpString
);
506 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
507 helpString
.mbc_str(), "");
511 // ----------------------------------------------------------------------------
512 // wxToolBar idle handling
513 // ----------------------------------------------------------------------------
515 void wxToolBar::OnInternalIdle()
517 wxCursor cursor
= m_cursor
;
518 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
522 /* I now set the cursor the anew in every OnInternalIdle call
523 as setting the cursor in a parent window also effects the
524 windows above so that checking for the current cursor is
527 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
529 /* if the toolbar is dockable, then m_widget stands for the
530 GtkHandleBox widget, which uses its own window so that we
531 can set the cursor for it. if the toolbar is not dockable,
532 m_widget comes from m_toolbar which uses its parent's
533 window ("windowless windows") and thus we cannot set the
535 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
538 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
541 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
542 node
= node
->GetNext();
544 GtkWidget
*item
= tool
->m_item
;
547 GdkWindow
*window
= item
->window
;
551 gdk_window_set_cursor( window
, cursor
.GetCursor() );
560 #endif // wxUSE_TOOLBAR_NATIVE