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
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
38 extern void wxapp_install_idle_handler();
42 extern bool g_blockEventsOnDrag
;
43 extern wxCursor g_globalCursor
;
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 class wxToolBarTool
: public wxToolBarToolBase
52 wxToolBarTool(wxToolBar
*tbar
,
54 const wxBitmap
& bitmap1
,
55 const wxBitmap
& bitmap2
,
58 const wxString
& shortHelpString
,
59 const wxString
& longHelpString
)
60 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
61 clientData
, shortHelpString
, longHelpString
)
66 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
67 : wxToolBarToolBase(tbar
, control
)
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
85 // ============================================================================
87 // ============================================================================
89 //-----------------------------------------------------------------------------
90 // "clicked" (internal from gtk_toolbar)
91 //-----------------------------------------------------------------------------
93 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
97 wxapp_install_idle_handler();
99 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
100 if ( tbar
->m_blockNextEvent
)
102 tbar
->m_blockNextEvent
= FALSE
;
106 if (g_blockEventsOnDrag
) return;
107 if (!tool
->IsEnabled()) return;
109 if (tool
->CanBeToggled())
113 wxBitmap bitmap
= tool
->GetBitmap();
116 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
118 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
121 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
125 tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() );
128 //-----------------------------------------------------------------------------
129 // "enter_notify_event"
130 //-----------------------------------------------------------------------------
132 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
133 GdkEventCrossing
*WXUNUSED(gdk_event
),
134 wxToolBarTool
*tool
)
136 if (g_isIdle
) wxapp_install_idle_handler();
138 if (g_blockEventsOnDrag
) return TRUE
;
140 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
142 #if (GTK_MINOR_VERSION == 0)
143 /* we grey-out the tip text of disabled tool in GTK 1.0 */
144 if (tool
->IsEnabled())
146 if (tb
->m_fg
->red
!= 0)
151 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
153 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
158 if (tb
->m_fg
->red
== 0)
160 tb
->m_fg
->red
= 33000;
161 tb
->m_fg
->green
= 33000;
162 tb
->m_fg
->blue
= 33000;
163 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
164 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
171 tb
->OnMouseEnter( tool
->GetId() );
176 //-----------------------------------------------------------------------------
177 // InsertChild callback for wxToolBar
178 //-----------------------------------------------------------------------------
180 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
181 wxWindow
* WXUNUSED(child
) )
183 /* we don't do anything here but pray */
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 void wxToolBarTool::Init()
193 m_pixmap
= (GtkWidget
*)NULL
;
196 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
197 const wxBitmap
& bitmap1
,
198 const wxBitmap
& bitmap2
,
200 wxObject
*clientData
,
201 const wxString
& shortHelpString
,
202 const wxString
& longHelpString
)
204 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
205 clientData
, shortHelpString
, longHelpString
);
208 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
210 return new wxToolBarTool(this, control
);
213 //-----------------------------------------------------------------------------
214 // wxToolBar construction
215 //-----------------------------------------------------------------------------
217 void wxToolBar::Init()
220 m_bg
= (GdkColor
*)NULL
;
222 m_toolbar
= (GtkToolbar
*)NULL
;
224 m_blockNextEvent
= FALSE
;
227 wxToolBar::~wxToolBar()
233 bool wxToolBar::Create( wxWindow
*parent
,
238 const wxString
& name
)
241 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
243 if ( !PreCreation( parent
, pos
, size
) ||
244 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
246 wxFAIL_MSG( wxT("wxToolBar creation failed") );
251 GtkOrientation orient
= style
& wxTB_VERTICAL
? GTK_ORIENTATION_VERTICAL
252 : GTK_ORIENTATION_HORIZONTAL
;
253 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( orient
, GTK_TOOLBAR_ICONS
) );
255 SetToolSeparation(7);
257 if (style
& wxTB_DOCKABLE
)
259 m_widget
= gtk_handle_box_new();
260 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
261 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
263 #if (GTK_MINOR_VERSION > 0)
264 if (style
& wxTB_FLAT
)
265 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
270 m_widget
= GTK_WIDGET(m_toolbar
);
273 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
275 if (style
& wxTB_FLAT
)
276 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
284 fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
285 m_fg
->pixel
= fg
.GetPixel();
291 wxColour
bg(255,255,196);
292 bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
293 m_bg
->pixel
= bg
.GetPixel();
295 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
299 gtk_widget_get_style(
300 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
302 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
303 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
306 m_parent
->DoAddChild( this );
315 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
317 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
319 // we have inserted a space before all the tools
320 if (m_xMargin
> 1) pos
++;
322 if ( tool
->IsButton() )
324 wxBitmap bitmap
= tool
->GetBitmap1();
326 wxCHECK_MSG( bitmap
.Ok(), FALSE
,
327 wxT("invalid bitmap for wxToolBar icon") );
329 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
,
330 wxT("wxToolBar doesn't support GdkBitmap") );
332 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
,
333 wxT("wxToolBar::Add needs a wxBitmap") );
335 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
337 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
339 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
340 if ( bitmap
.GetMask() )
341 mask
= bitmap
.GetMask()->GetBitmap();
343 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
344 #if (GTK_MINOR_VERSION > 0)
345 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
348 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
350 tool
->m_pixmap
= tool_pixmap
;
353 switch ( tool
->GetStyle() )
355 case wxTOOL_STYLE_BUTTON
:
356 tool
->m_item
= gtk_toolbar_insert_element
360 ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
361 : GTK_TOOLBAR_CHILD_BUTTON
,
364 tool
->GetShortHelp().mbc_str(),
365 "", // tooltip_private_text (?)
367 (GtkSignalFunc
)gtk_toolbar_callback
,
374 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
379 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
380 "enter_notify_event",
381 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
385 case wxTOOL_STYLE_SEPARATOR
:
386 gtk_toolbar_append_space( m_toolbar
);
391 case wxTOOL_STYLE_CONTROL
:
392 gtk_toolbar_insert_widget(
394 tool
->GetControl()->m_widget
,
403 (* GTK_WIDGET_CLASS( GTK_OBJECT(m_widget
)->klass
)->size_request
) (m_widget
, &req
);
404 m_width
= req
.width
+ m_xMargin
;
405 m_height
= req
.height
+ 2*m_yMargin
;
410 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
412 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
414 switch ( tool
->GetStyle() )
416 case wxTOOL_STYLE_CONTROL
:
417 tool
->GetControl()->Destroy();
420 case wxTOOL_STYLE_BUTTON
:
421 gtk_widget_destroy( tool
->m_item
);
424 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
430 // ----------------------------------------------------------------------------
431 // wxToolBar tools state
432 // ----------------------------------------------------------------------------
434 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
436 #if (GTK_MINOR_VERSION > 0)
437 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
439 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
440 greyed anyway and this also disables tooltips */
442 gtk_widget_set_sensitive( tool
->m_item
, enable
);
446 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
448 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
450 GtkWidget
*item
= tool
->m_item
;
451 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
453 wxBitmap bitmap
= tool
->GetBitmap();
456 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
458 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
461 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
464 m_blockNextEvent
= TRUE
; // we cannot use gtk_signal_disconnect here
466 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
470 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
471 bool WXUNUSED(toggle
))
473 // VZ: absolutely no idea about how to do it
474 wxFAIL_MSG( _T("not implemented") );
477 // ----------------------------------------------------------------------------
478 // wxToolBar geometry
479 // ----------------------------------------------------------------------------
481 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
482 wxCoord
WXUNUSED(y
)) const
484 // VZ: GTK+ doesn't seem to have such thing
485 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
487 return (wxToolBarToolBase
*)NULL
;
490 void wxToolBar::SetMargins( int x
, int y
)
492 wxCHECK_RET( GetToolsCount() == 0,
493 wxT("wxToolBar::SetMargins must be called before adding tools.") );
495 if (x
> 1) gtk_toolbar_append_space( m_toolbar
); // oh well
501 void wxToolBar::SetToolSeparation( int separation
)
503 gtk_toolbar_set_space_size( m_toolbar
, separation
);
504 m_toolSeparation
= separation
;
507 // ----------------------------------------------------------------------------
508 // wxToolBar idle handling
509 // ----------------------------------------------------------------------------
511 void wxToolBar::OnInternalIdle()
513 wxCursor cursor
= m_cursor
;
514 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
518 /* I now set the cursor the anew in every OnInternalIdle call
519 as setting the cursor in a parent window also effects the
520 windows above so that checking for the current cursor is
523 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
525 /* if the toolbar is dockable, then m_widget stands for the
526 GtkHandleBox widget, which uses its own window so that we
527 can set the cursor for it. if the toolbar is not dockable,
528 m_widget comes from m_toolbar which uses its parent's
529 window ("windowless windows") and thus we cannot set the
531 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
534 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
537 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
538 node
= node
->GetNext();
540 GtkWidget
*item
= tool
->m_item
;
543 GdkWindow
*window
= item
->window
;
547 gdk_window_set_cursor( window
, cursor
.GetCursor() );