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 extern GdkFont
*GtkGetDefaultGuiFont();
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
43 extern void wxapp_install_idle_handler();
47 extern bool g_blockEventsOnDrag
;
48 extern wxCursor g_globalCursor
;
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 class wxToolBarTool
: public wxToolBarToolBase
57 wxToolBarTool(wxToolBar
*tbar
,
59 const wxBitmap
& bitmap1
,
60 const wxBitmap
& bitmap2
,
63 const wxString
& shortHelpString
,
64 const wxString
& longHelpString
)
65 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
66 clientData
, shortHelpString
, longHelpString
)
71 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
72 : wxToolBarToolBase(tbar
, control
)
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
90 // ============================================================================
92 // ============================================================================
94 //-----------------------------------------------------------------------------
95 // "clicked" (internal from gtk_toolbar)
96 //-----------------------------------------------------------------------------
98 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
102 wxapp_install_idle_handler();
104 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
105 if ( tbar
->m_blockNextEvent
)
107 tbar
->m_blockNextEvent
= FALSE
;
111 if (g_blockEventsOnDrag
) return;
112 if (!tool
->IsEnabled()) return;
114 if (tool
->CanBeToggled())
118 wxBitmap bitmap
= tool
->GetBitmap();
121 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
123 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
126 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
130 tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() );
133 //-----------------------------------------------------------------------------
134 // "enter_notify_event"
135 //-----------------------------------------------------------------------------
137 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
138 GdkEventCrossing
*WXUNUSED(gdk_event
),
139 wxToolBarTool
*tool
)
141 if (g_isIdle
) wxapp_install_idle_handler();
143 if (g_blockEventsOnDrag
) return TRUE
;
145 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
147 #if (GTK_MINOR_VERSION == 0)
148 /* we grey-out the tip text of disabled tool in GTK 1.0 */
149 if (tool
->IsEnabled())
151 if (tb
->m_fg
->red
!= 0)
156 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
158 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
163 if (tb
->m_fg
->red
== 0)
165 tb
->m_fg
->red
= 33000;
166 tb
->m_fg
->green
= 33000;
167 tb
->m_fg
->blue
= 33000;
168 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
169 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
176 tb
->OnMouseEnter( tool
->GetId() );
181 //-----------------------------------------------------------------------------
182 // InsertChild callback for wxToolBar
183 //-----------------------------------------------------------------------------
185 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
186 wxWindow
* WXUNUSED(child
) )
188 /* we don't do anything here but pray */
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 void wxToolBarTool::Init()
198 m_pixmap
= (GtkWidget
*)NULL
;
201 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
202 const wxBitmap
& bitmap1
,
203 const wxBitmap
& bitmap2
,
205 wxObject
*clientData
,
206 const wxString
& shortHelpString
,
207 const wxString
& longHelpString
)
209 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
210 clientData
, shortHelpString
, longHelpString
);
213 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
215 return new wxToolBarTool(this, control
);
218 //-----------------------------------------------------------------------------
219 // wxToolBar construction
220 //-----------------------------------------------------------------------------
222 void wxToolBar::Init()
225 m_bg
= (GdkColor
*)NULL
;
227 m_toolbar
= (GtkToolbar
*)NULL
;
229 m_blockNextEvent
= FALSE
;
232 wxToolBar::~wxToolBar()
238 bool wxToolBar::Create( wxWindow
*parent
,
243 const wxString
& name
)
246 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
248 if ( !PreCreation( parent
, pos
, size
) ||
249 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
251 wxFAIL_MSG( wxT("wxToolBar creation failed") );
256 GtkOrientation orient
= style
& wxTB_VERTICAL
? GTK_ORIENTATION_VERTICAL
257 : GTK_ORIENTATION_HORIZONTAL
;
258 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( orient
, GTK_TOOLBAR_ICONS
) );
260 SetToolSeparation(7);
262 if (style
& wxTB_DOCKABLE
)
264 m_widget
= gtk_handle_box_new();
265 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
266 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
268 #if (GTK_MINOR_VERSION > 0)
269 if (style
& wxTB_FLAT
)
270 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
275 m_widget
= GTK_WIDGET(m_toolbar
);
278 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
280 if (style
& wxTB_FLAT
)
281 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
289 fg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
290 m_fg
->pixel
= fg
.GetPixel();
296 wxColour
bg(255,255,196);
297 bg
.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ) );
298 m_bg
->pixel
= bg
.GetPixel();
300 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
304 gtk_widget_get_style(
305 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
307 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
308 gdk_font_unref( g_style
->font
);
309 g_style
->font
= gdk_font_ref( GtkGetDefaultGuiFont() );
310 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
312 m_parent
->DoAddChild( this );
321 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
323 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
325 // we have inserted a space before all the tools
326 if (m_xMargin
> 1) pos
++;
328 if ( tool
->IsButton() )
330 wxBitmap bitmap
= tool
->GetBitmap1();
332 wxCHECK_MSG( bitmap
.Ok(), FALSE
,
333 wxT("invalid bitmap for wxToolBar icon") );
335 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, FALSE
,
336 wxT("wxToolBar doesn't support GdkBitmap") );
338 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, FALSE
,
339 wxT("wxToolBar::Add needs a wxBitmap") );
341 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
343 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
345 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
346 if ( bitmap
.GetMask() )
347 mask
= bitmap
.GetMask()->GetBitmap();
349 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
350 #if (GTK_MINOR_VERSION > 0)
351 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
354 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
356 tool
->m_pixmap
= tool_pixmap
;
359 switch ( tool
->GetStyle() )
361 case wxTOOL_STYLE_BUTTON
:
362 tool
->m_item
= gtk_toolbar_insert_element
366 ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
367 : GTK_TOOLBAR_CHILD_BUTTON
,
370 tool
->GetShortHelp().mbc_str(),
371 "", // tooltip_private_text (?)
373 (GtkSignalFunc
)gtk_toolbar_callback
,
380 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
385 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
386 "enter_notify_event",
387 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
391 case wxTOOL_STYLE_SEPARATOR
:
392 gtk_toolbar_append_space( m_toolbar
);
397 case wxTOOL_STYLE_CONTROL
:
398 gtk_toolbar_insert_widget(
400 tool
->GetControl()->m_widget
,
409 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
411 m_width
= req
.width
+ m_xMargin
;
412 m_height
= req
.height
+ 2*m_yMargin
;
417 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*toolBase
)
419 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
421 switch ( tool
->GetStyle() )
423 case wxTOOL_STYLE_CONTROL
:
424 tool
->GetControl()->Destroy();
427 case wxTOOL_STYLE_BUTTON
:
428 gtk_widget_destroy( tool
->m_item
);
431 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
437 // ----------------------------------------------------------------------------
438 // wxToolBar tools state
439 // ----------------------------------------------------------------------------
441 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
443 #if (GTK_MINOR_VERSION > 0)
444 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
446 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
447 greyed anyway and this also disables tooltips */
449 gtk_widget_set_sensitive( tool
->m_item
, enable
);
453 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
455 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
457 GtkWidget
*item
= tool
->m_item
;
458 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
460 wxBitmap bitmap
= tool
->GetBitmap();
463 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
465 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
468 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
471 m_blockNextEvent
= TRUE
; // we cannot use gtk_signal_disconnect here
473 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
477 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
478 bool WXUNUSED(toggle
))
480 // VZ: absolutely no idea about how to do it
481 wxFAIL_MSG( _T("not implemented") );
484 // ----------------------------------------------------------------------------
485 // wxToolBar geometry
486 // ----------------------------------------------------------------------------
488 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
489 wxCoord
WXUNUSED(y
)) const
491 // VZ: GTK+ doesn't seem to have such thing
492 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
494 return (wxToolBarToolBase
*)NULL
;
497 void wxToolBar::SetMargins( int x
, int y
)
499 wxCHECK_RET( GetToolsCount() == 0,
500 wxT("wxToolBar::SetMargins must be called before adding tools.") );
502 if (x
> 1) gtk_toolbar_append_space( m_toolbar
); // oh well
508 void wxToolBar::SetToolSeparation( int separation
)
510 gtk_toolbar_set_space_size( m_toolbar
, separation
);
511 m_toolSeparation
= separation
;
514 // ----------------------------------------------------------------------------
515 // wxToolBar idle handling
516 // ----------------------------------------------------------------------------
518 void wxToolBar::OnInternalIdle()
520 wxCursor cursor
= m_cursor
;
521 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
525 /* I now set the cursor the anew in every OnInternalIdle call
526 as setting the cursor in a parent window also effects the
527 windows above so that checking for the current cursor is
530 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
532 /* if the toolbar is dockable, then m_widget stands for the
533 GtkHandleBox widget, which uses its own window so that we
534 can set the cursor for it. if the toolbar is not dockable,
535 m_widget comes from m_toolbar which uses its parent's
536 window ("windowless windows") and thus we cannot set the
538 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
541 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
544 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
545 node
= node
->GetNext();
547 GtkWidget
*item
= tool
->m_item
;
550 GdkWindow
*window
= item
->window
;
554 gdk_window_set_cursor( window
, cursor
.GetCursor() );