1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: GTK toolbar
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "tbargtk.h"
14 #include "wx/toolbar.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
36 extern wxCursor g_globalCursor
;
38 //-----------------------------------------------------------------------------
39 // "clicked" (internal from gtk_toolbar)
40 //-----------------------------------------------------------------------------
42 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
44 if (g_isIdle
) wxapp_install_idle_handler();
46 if (g_blockEventsOnDrag
) return;
47 if (!tool
->m_enabled
) return;
51 tool
->m_toggleState
= !tool
->m_toggleState
;
53 if (tool
->m_bitmap2
.Ok())
55 wxBitmap bitmap
= tool
->m_bitmap1
;
56 if (tool
->m_toggleState
) bitmap
= tool
->m_bitmap2
;
58 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
60 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
61 if (bitmap
.GetMask()) mask
= bitmap
.GetMask()->GetBitmap();
63 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
67 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
70 //-----------------------------------------------------------------------------
71 // "enter_notify_event"
72 //-----------------------------------------------------------------------------
74 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
75 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
77 if (g_isIdle
) wxapp_install_idle_handler();
79 if (g_blockEventsOnDrag
) return TRUE
;
82 wxToolBar
*tb
= tool
->m_owner
;
84 #if (GTK_MINOR_VERSION == 0)
85 /* we grey-out the tip text of disabled tool in GTK 1.0 */
88 if (tb
->m_fg
->red
!= 0)
93 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
95 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
100 if (tb
->m_fg
->red
== 0)
102 tb
->m_fg
->red
= 33000;
103 tb
->m_fg
->green
= 33000;
104 tb
->m_fg
->blue
= 33000;
105 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
106 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
113 tb
->OnMouseEnter( tool
->m_index
);
118 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
122 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
124 BEGIN_EVENT_TABLE(wxToolBar
, wxControl
)
125 EVT_IDLE(wxToolBar::OnIdle
)
128 wxToolBar::wxToolBar()
132 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
133 const wxPoint
& pos
, const wxSize
& size
,
134 long style
, const wxString
& name
)
136 Create( parent
, id
, pos
, size
, style
, name
);
139 wxToolBar::~wxToolBar()
145 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
146 const wxPoint
& pos
, const wxSize
& size
,
147 long style
, const wxString
& name
)
151 if (!PreCreation( parent
, pos
, size
) ||
152 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
154 wxFAIL_MSG( wxT("wxToolBar creation failed") );
158 m_tools
.DeleteContents( TRUE
);
160 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
161 GTK_TOOLBAR_ICONS
) );
164 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
165 m_hasToolAlready
= FALSE
;
167 if (style
& wxTB_DOCKABLE
)
169 m_widget
= gtk_handle_box_new();
170 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
171 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
173 #if (GTK_MINOR_VERSION > 0)
174 if (style
& wxTB_FLAT
)
175 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
180 m_widget
= GTK_WIDGET(m_toolbar
);
183 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
185 #if (GTK_MINOR_VERSION > 0)
186 if (style
& wxTB_FLAT
)
187 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
194 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
200 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
202 #if (GTK_MINOR_VERSION > 0)
203 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
207 gtk_widget_get_style(
208 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
210 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
211 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
213 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
219 m_parent
->DoAddChild( this );
228 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
230 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
231 event
.SetEventObject(this);
232 event
.SetInt( toolIndex
);
233 event
.SetExtraLong((long) toggleDown
);
235 GetEventHandler()->ProcessEvent(event
);
240 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
242 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
243 event
.SetEventObject( this );
244 event
.SetInt( toolIndex
);
246 GetEventHandler()->ProcessEvent(event
);
249 void wxToolBar::OnMouseEnter( int toolIndex
)
251 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
252 event
.SetEventObject(this);
253 event
.SetInt( toolIndex
);
255 GetEventHandler()->ProcessEvent(event
);
258 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
259 const wxBitmap
& pushedBitmap
, bool toggle
,
260 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
261 const wxString
& helpString1
, const wxString
& helpString2
)
263 m_hasToolAlready
= TRUE
;
265 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
266 wxT("invalid bitmap for wxToolBar icon") );
268 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
269 wxT("wxToolBar doesn't support GdkBitmap") );
271 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
272 wxT("wxToolBar::Add needs a wxBitmap") );
274 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
276 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
278 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
279 if ( bitmap
.GetMask() )
280 mask
= bitmap
.GetMask()->GetBitmap();
282 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
283 #if (GTK_MINOR_VERSION > 0)
284 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
287 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
289 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
291 helpString1
, helpString2
,
294 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
295 : GTK_TOOLBAR_CHILD_BUTTON
;
297 GtkWidget
*item
= gtk_toolbar_append_element
299 GTK_TOOLBAR(m_toolbar
),
303 helpString1
.mbc_str(),
306 (GtkSignalFunc
)gtk_toolbar_callback
,
313 (* GTK_WIDGET_CLASS( GTK_OBJECT(m_widget
)->klass
)->size_request
) (m_widget
, &req
);
315 m_height
= req
.height
;
317 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
318 "enter_notify_event",
319 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
322 m_tools
.Append( tool
);
327 void wxToolBar::AddSeparator()
329 gtk_toolbar_append_space( m_toolbar
);
332 void wxToolBar::ClearTools()
334 wxFAIL_MSG( wxT("wxToolBar::ClearTools not implemented") );
337 bool wxToolBar::Realize()
344 wxNode
*node
= m_tools
.First();
347 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
348 if (tool
->m_bitmap1
.Ok())
350 int tool_height
= tool
->m_bitmap1
.GetHeight();
351 if (tool_height
> m_height
) m_height
= tool_height
;
357 m_height
+= 5 + 2*m_yMargin
;
362 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
364 wxNode
*node
= m_tools
.First();
367 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
368 if (tool
->m_index
== toolIndex
)
370 tool
->m_enabled
= enable
;
372 #if (GTK_MINOR_VERSION > 0)
373 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
374 greyed anyway and this also disables tooltips */
376 gtk_widget_set_sensitive( tool
->m_item
, enable
);
384 wxFAIL_MSG( wxT("wrong toolbar index") );
387 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
389 wxNode
*node
= m_tools
.First();
392 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
393 if (tool
->m_index
== toolIndex
)
395 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
397 tool
->m_toggleState
= toggle
;
399 if (tool
->m_bitmap2
.Ok())
401 wxBitmap bitmap
= tool
->m_bitmap1
;
402 if (tool
->m_toggleState
) bitmap
= tool
->m_bitmap2
;
404 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
406 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
407 if (bitmap
.GetMask()) mask
= bitmap
.GetMask()->GetBitmap();
409 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
412 gtk_signal_disconnect_by_func( GTK_OBJECT(tool
->m_item
),
413 GTK_SIGNAL_FUNC(gtk_toolbar_callback
), (gpointer
*)tool
);
415 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
417 gtk_signal_connect( GTK_OBJECT(tool
->m_item
), "clicked",
418 GTK_SIGNAL_FUNC(gtk_toolbar_callback
), (gpointer
*)tool
);
426 wxFAIL_MSG( wxT("wrong toolbar index") );
429 wxObject
*wxToolBar::GetToolClientData( int index
) const
431 wxNode
*node
= m_tools
.First();
434 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
435 if (tool
->m_index
== index
) return tool
->m_clientData
;;
439 wxFAIL_MSG( wxT("wrong toolbar index") );
441 return (wxObject
*)NULL
;
444 bool wxToolBar::GetToolState(int toolIndex
) const
446 wxNode
*node
= m_tools
.First();
449 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
450 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
454 wxFAIL_MSG( wxT("wrong toolbar index") );
459 bool wxToolBar::GetToolEnabled(int toolIndex
) const
461 wxNode
*node
= m_tools
.First();
464 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
465 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
469 wxFAIL_MSG( wxT("wrong toolbar index") );
474 void wxToolBar::SetMargins( int x
, int y
)
476 wxCHECK_RET( !m_hasToolAlready
, wxT("wxToolBar::SetMargins must be called before adding tool.") );
478 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
484 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
486 wxFAIL_MSG( wxT("wxToolBar::SetToolPacking not implemented") );
489 void wxToolBar::SetToolSeparation( int separation
)
491 gtk_toolbar_set_space_size( m_toolbar
, separation
);
492 m_separation
= separation
;
495 int wxToolBar::GetToolPacking()
500 int wxToolBar::GetToolSeparation()
505 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
507 wxNode
*node
= m_tools
.First();
510 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
511 if (tool
->m_index
== toolIndex
)
513 return tool
->m_longHelpString
;
518 wxFAIL_MSG( wxT("wrong toolbar index") );
523 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
525 wxNode
*node
= m_tools
.First();
528 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
529 if (tool
->m_index
== toolIndex
)
531 return tool
->m_shortHelpString
;
536 wxFAIL_MSG( wxT("wrong toolbar index") );
541 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
543 wxNode
*node
= m_tools
.First();
546 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
547 if (tool
->m_index
== toolIndex
)
549 tool
->m_longHelpString
= helpString
;
555 wxFAIL_MSG( wxT("wrong toolbar index") );
560 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
562 wxNode
*node
= m_tools
.First();
565 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
566 if (tool
->m_index
== toolIndex
)
568 tool
->m_shortHelpString
= helpString
;
574 wxFAIL_MSG( wxT("wrong toolbar index") );
579 void wxToolBar::OnIdle( wxIdleEvent
&WXUNUSED(ievent
) )
581 wxEvtHandler
* evtHandler
= GetEventHandler();
583 wxNode
* node
= m_tools
.First();
586 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->Data();
588 wxUpdateUIEvent
event( tool
->m_index
);
589 event
.SetEventObject(this);
591 if (evtHandler
->ProcessEvent( event
))
593 if (event
.GetSetEnabled())
594 EnableTool(tool
->m_index
, event
.GetEnabled());
595 if (event
.GetSetChecked())
596 ToggleTool(tool
->m_index
, event
.GetChecked());
598 if (event.GetSetText())
607 void wxToolBar::OnInternalIdle()
609 wxCursor cursor
= m_cursor
;
610 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
614 /* I now set the cursor the anew in every OnInternalIdle call
615 as setting the cursor in a parent window also effects the
616 windows above so that checking for the current cursor is
619 if (HasFlag(wxTB_DOCKABLE
))
621 /* if the toolbar is dockable, then m_widget stands for the
622 GtkHandleBox widget, which uses its own window so that we
623 can set the cursor for it. if the toolbar is not dockable,
624 m_widget comes from m_toolbar which uses its parent's
625 window ("windowless windows") and thus we cannot set the
627 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
630 wxNode
*node
= m_tools
.First();
633 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
634 if (!tool
->m_item
->window
)
637 gdk_window_set_cursor( tool
->m_item
->window
, cursor
.GetCursor() );