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 #define gtk_pixmap_set_build_insensitive gtk_pixmap_set_build_insensitiv
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
41 extern void wxapp_install_idle_handler();
45 extern bool g_blockEventsOnDrag
;
46 extern wxCursor g_globalCursor
;
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 class wxToolBarTool
: public wxToolBarToolBase
55 wxToolBarTool(wxToolBar
*tbar
,
57 const wxBitmap
& bitmap1
,
58 const wxBitmap
& bitmap2
,
61 const wxString
& shortHelpString
,
62 const wxString
& longHelpString
)
63 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
64 clientData
, shortHelpString
, longHelpString
)
69 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
70 : wxToolBarToolBase(tbar
, control
)
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
88 // ============================================================================
90 // ============================================================================
92 //-----------------------------------------------------------------------------
93 // "clicked" (internal from gtk_toolbar)
94 //-----------------------------------------------------------------------------
96 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
100 wxapp_install_idle_handler();
102 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
103 if ( tbar
->m_blockNextEvent
)
105 tbar
->m_blockNextEvent
= FALSE
;
109 if (g_blockEventsOnDrag
) return;
110 if (!tool
->IsEnabled()) return;
112 if (tool
->CanBeToggled())
116 wxBitmap bitmap
= tool
->GetBitmap();
119 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
121 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
124 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
128 tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() );
131 //-----------------------------------------------------------------------------
132 // "enter_notify_event"
133 //-----------------------------------------------------------------------------
135 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
136 GdkEventCrossing
*WXUNUSED(gdk_event
),
137 wxToolBarTool
*tool
)
139 if (g_isIdle
) wxapp_install_idle_handler();
141 if (g_blockEventsOnDrag
) return TRUE
;
143 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
145 #if (GTK_MINOR_VERSION == 0)
146 /* we grey-out the tip text of disabled tool in GTK 1.0 */
147 if (tool
->IsEnabled())
149 if (tb
->m_fg
->red
!= 0)
154 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
156 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
161 if (tb
->m_fg
->red
== 0)
163 tb
->m_fg
->red
= 33000;
164 tb
->m_fg
->green
= 33000;
165 tb
->m_fg
->blue
= 33000;
166 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
167 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
174 tb
->OnMouseEnter( tool
->GetId() );
179 //-----------------------------------------------------------------------------
180 // InsertChild callback for wxToolBar
181 //-----------------------------------------------------------------------------
183 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
184 wxWindow
* WXUNUSED(child
) )
186 /* we don't do anything here but pray */
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 void wxToolBarTool::Init()
196 m_pixmap
= (GtkWidget
*)NULL
;
199 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
200 const wxBitmap
& bitmap1
,
201 const wxBitmap
& bitmap2
,
203 wxObject
*clientData
,
204 const wxString
& shortHelpString
,
205 const wxString
& longHelpString
)
207 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
208 clientData
, shortHelpString
, longHelpString
);
211 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
213 return new wxToolBarTool(this, control
);
216 //-----------------------------------------------------------------------------
217 // wxToolBar construction
218 //-----------------------------------------------------------------------------
220 void wxToolBar::Init()
223 m_bg
= (GdkColor
*)NULL
;
225 m_toolbar
= (GtkToolbar
*)NULL
;
227 m_blockNextEvent
= FALSE
;
230 wxToolBar::~wxToolBar()
236 bool wxToolBar::Create( wxWindow
*parent
,
241 const wxString
& name
)
244 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
246 if ( !PreCreation( parent
, pos
, size
) ||
247 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
249 wxFAIL_MSG( wxT("wxToolBar creation failed") );
254 GtkOrientation orient
= style
& wxTB_VERTICAL
? GTK_ORIENTATION_VERTICAL
255 : GTK_ORIENTATION_HORIZONTAL
;
256 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( orient
, GTK_TOOLBAR_ICONS
) );
258 SetToolSeparation(7);
260 if (style
& wxTB_DOCKABLE
)
262 m_widget
= gtk_handle_box_new();
263 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
264 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
266 #if (GTK_MINOR_VERSION > 0)
267 if (style
& wxTB_FLAT
)
268 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
273 m_widget
= GTK_WIDGET(m_toolbar
);
276 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
278 if (style
& wxTB_FLAT
)
279 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
287 fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
288 m_fg
->pixel
= fg
.GetPixel();
294 wxColour
bg(255,255,196);
295 bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
296 m_bg
->pixel
= bg
.GetPixel();
298 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
302 gtk_widget_get_style(
303 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
305 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
306 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
309 m_parent
->DoAddChild( this );
318 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
320 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
322 // we have inserted a space before all the tools
323 if (m_xMargin
> 1) pos
++;
325 if ( tool
->IsButton() )
327 wxBitmap bitmap
= tool
->GetBitmap1();
329 wxCHECK_MSG( bitmap
.Ok(), FALSE
,
330 wxT("invalid bitmap for wxToolBar icon") );
332 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
,
333 wxT("wxToolBar doesn't support GdkBitmap") );
335 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
,
336 wxT("wxToolBar::Add needs a wxBitmap") );
338 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
340 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
342 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
343 if ( bitmap
.GetMask() )
344 mask
= bitmap
.GetMask()->GetBitmap();
346 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
347 #if (GTK_MINOR_VERSION > 0)
348 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
351 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
353 tool
->m_pixmap
= tool_pixmap
;
356 switch ( tool
->GetStyle() )
358 case wxTOOL_STYLE_BUTTON
:
359 tool
->m_item
= gtk_toolbar_insert_element
363 ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
364 : GTK_TOOLBAR_CHILD_BUTTON
,
367 tool
->GetShortHelp().mbc_str(),
368 "", // tooltip_private_text (?)
370 (GtkSignalFunc
)gtk_toolbar_callback
,
377 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
382 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
383 "enter_notify_event",
384 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
388 case wxTOOL_STYLE_SEPARATOR
:
389 gtk_toolbar_append_space( m_toolbar
);
394 case wxTOOL_STYLE_CONTROL
:
395 gtk_toolbar_insert_widget(
397 tool
->GetControl()->m_widget
,
406 (* GTK_WIDGET_CLASS( GTK_OBJECT(m_widget
)->klass
)->size_request
) (m_widget
, &req
);
407 m_width
= req
.width
+ m_xMargin
;
408 m_height
= req
.height
+ 2*m_yMargin
;
413 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
415 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
417 switch ( tool
->GetStyle() )
419 case wxTOOL_STYLE_CONTROL
:
420 tool
->GetControl()->Destroy();
423 case wxTOOL_STYLE_BUTTON
:
424 gtk_widget_destroy( tool
->m_item
);
427 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
433 // ----------------------------------------------------------------------------
434 // wxToolBar tools state
435 // ----------------------------------------------------------------------------
437 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
439 #if (GTK_MINOR_VERSION > 0)
440 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
442 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
443 greyed anyway and this also disables tooltips */
445 gtk_widget_set_sensitive( tool
->m_item
, enable
);
449 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
451 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
453 GtkWidget
*item
= tool
->m_item
;
454 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
456 wxBitmap bitmap
= tool
->GetBitmap();
459 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
461 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
464 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
467 m_blockNextEvent
= TRUE
; // we cannot use gtk_signal_disconnect here
469 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
473 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
474 bool WXUNUSED(toggle
))
476 // VZ: absolutely no idea about how to do it
477 wxFAIL_MSG( _T("not implemented") );
480 // ----------------------------------------------------------------------------
481 // wxToolBar geometry
482 // ----------------------------------------------------------------------------
484 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
485 wxCoord
WXUNUSED(y
)) const
487 // VZ: GTK+ doesn't seem to have such thing
488 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
490 return (wxToolBarToolBase
*)NULL
;
493 void wxToolBar::SetMargins( int x
, int y
)
495 wxCHECK_RET( GetToolsCount() == 0,
496 wxT("wxToolBar::SetMargins must be called before adding tools.") );
498 if (x
> 1) gtk_toolbar_append_space( m_toolbar
); // oh well
504 void wxToolBar::SetToolSeparation( int separation
)
506 gtk_toolbar_set_space_size( m_toolbar
, separation
);
507 m_toolSeparation
= separation
;
510 // ----------------------------------------------------------------------------
511 // wxToolBar idle handling
512 // ----------------------------------------------------------------------------
514 void wxToolBar::OnInternalIdle()
516 wxCursor cursor
= m_cursor
;
517 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
521 /* I now set the cursor the anew in every OnInternalIdle call
522 as setting the cursor in a parent window also effects the
523 windows above so that checking for the current cursor is
526 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
528 /* if the toolbar is dockable, then m_widget stands for the
529 GtkHandleBox widget, which uses its own window so that we
530 can set the cursor for it. if the toolbar is not dockable,
531 m_widget comes from m_toolbar which uses its parent's
532 window ("windowless windows") and thus we cannot set the
534 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
537 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
540 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
541 node
= node
->GetNext();
543 GtkWidget
*item
= tool
->m_item
;
546 GdkWindow
*window
= item
->window
;
550 gdk_window_set_cursor( window
, cursor
.GetCursor() );