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
,
312 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
313 "enter_notify_event",
314 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
317 m_tools
.Append( tool
);
322 void wxToolBar::AddSeparator()
324 gtk_toolbar_append_space( m_toolbar
);
327 void wxToolBar::ClearTools()
329 wxFAIL_MSG( wxT("wxToolBar::ClearTools not implemented") );
332 bool wxToolBar::Realize()
339 wxNode
*node
= m_tools
.First();
342 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
343 if (tool
->m_bitmap1
.Ok())
345 int tool_height
= tool
->m_bitmap1
.GetHeight();
346 if (tool_height
> m_height
) m_height
= tool_height
;
352 m_height
+= 5 + 2*m_yMargin
;
357 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
359 wxNode
*node
= m_tools
.First();
362 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
363 if (tool
->m_index
== toolIndex
)
365 tool
->m_enabled
= enable
;
367 #if (GTK_MINOR_VERSION > 0)
368 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
369 greyed anyway and this also disables tooltips */
371 gtk_widget_set_sensitive( tool
->m_item
, enable
);
379 wxFAIL_MSG( wxT("wrong toolbar index") );
382 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
384 wxNode
*node
= m_tools
.First();
387 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
388 if (tool
->m_index
== toolIndex
)
390 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
392 tool
->m_toggleState
= toggle
;
394 if (tool
->m_bitmap2
.Ok())
396 wxBitmap bitmap
= tool
->m_bitmap1
;
397 if (tool
->m_toggleState
) bitmap
= tool
->m_bitmap2
;
399 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
401 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
402 if (bitmap
.GetMask()) mask
= bitmap
.GetMask()->GetBitmap();
404 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
407 gtk_signal_disconnect_by_func( GTK_OBJECT(tool
->m_item
),
408 GTK_SIGNAL_FUNC(gtk_toolbar_callback
), (gpointer
*)tool
);
410 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
412 gtk_signal_connect( GTK_OBJECT(tool
->m_item
), "clicked",
413 GTK_SIGNAL_FUNC(gtk_toolbar_callback
), (gpointer
*)tool
);
421 wxFAIL_MSG( wxT("wrong toolbar index") );
424 wxObject
*wxToolBar::GetToolClientData( int index
) const
426 wxNode
*node
= m_tools
.First();
429 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
430 if (tool
->m_index
== index
) return tool
->m_clientData
;;
434 wxFAIL_MSG( wxT("wrong toolbar index") );
436 return (wxObject
*)NULL
;
439 bool wxToolBar::GetToolState(int toolIndex
) const
441 wxNode
*node
= m_tools
.First();
444 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
445 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
449 wxFAIL_MSG( wxT("wrong toolbar index") );
454 bool wxToolBar::GetToolEnabled(int toolIndex
) const
456 wxNode
*node
= m_tools
.First();
459 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
460 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
464 wxFAIL_MSG( wxT("wrong toolbar index") );
469 void wxToolBar::SetMargins( int x
, int y
)
471 wxCHECK_RET( !m_hasToolAlready
, wxT("wxToolBar::SetMargins must be called before adding tool.") );
473 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
479 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
481 wxFAIL_MSG( wxT("wxToolBar::SetToolPacking not implemented") );
484 void wxToolBar::SetToolSeparation( int separation
)
486 gtk_toolbar_set_space_size( m_toolbar
, separation
);
487 m_separation
= separation
;
490 int wxToolBar::GetToolPacking()
495 int wxToolBar::GetToolSeparation()
500 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
502 wxNode
*node
= m_tools
.First();
505 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
506 if (tool
->m_index
== toolIndex
)
508 return tool
->m_longHelpString
;
513 wxFAIL_MSG( wxT("wrong toolbar index") );
518 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
520 wxNode
*node
= m_tools
.First();
523 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
524 if (tool
->m_index
== toolIndex
)
526 return tool
->m_shortHelpString
;
531 wxFAIL_MSG( wxT("wrong toolbar index") );
536 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
538 wxNode
*node
= m_tools
.First();
541 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
542 if (tool
->m_index
== toolIndex
)
544 tool
->m_longHelpString
= helpString
;
550 wxFAIL_MSG( wxT("wrong toolbar index") );
555 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
557 wxNode
*node
= m_tools
.First();
560 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
561 if (tool
->m_index
== toolIndex
)
563 tool
->m_shortHelpString
= helpString
;
569 wxFAIL_MSG( wxT("wrong toolbar index") );
574 void wxToolBar::OnIdle( wxIdleEvent
&WXUNUSED(ievent
) )
576 wxEvtHandler
* evtHandler
= GetEventHandler();
578 wxNode
* node
= m_tools
.First();
581 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->Data();
583 wxUpdateUIEvent
event( tool
->m_index
);
584 event
.SetEventObject(this);
586 if (evtHandler
->ProcessEvent( event
))
588 if (event
.GetSetEnabled())
589 EnableTool(tool
->m_index
, event
.GetEnabled());
590 if (event
.GetSetChecked())
591 ToggleTool(tool
->m_index
, event
.GetChecked());
593 if (event.GetSetText())
602 void wxToolBar::OnInternalIdle()
604 wxCursor cursor
= m_cursor
;
605 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
609 /* I now set the cursor the anew in every OnInternalIdle call
610 as setting the cursor in a parent window also effects the
611 windows above so that checking for the current cursor is
614 if (HasFlag(wxTB_DOCKABLE
))
616 /* if the toolbar is dockable, then m_widget stands for the
617 GtkHandleBox widget, which uses its own window so that we
618 can set the cursor for it. if the toolbar is not dockable,
619 m_widget comes from m_toolbar which uses its parent's
620 window ("windowless windows") and thus we cannot set the
622 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
625 wxNode
*node
= m_tools
.First();
628 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
629 if (!tool
->m_item
->window
)
632 gdk_window_set_cursor( tool
->m_item
->window
, cursor
.GetCursor() );