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
)
45 wxapp_install_idle_handler();
47 if (tool
->m_owner
->m_blockNextEvent
)
49 tool
->m_owner
->m_blockNextEvent
= FALSE
;
53 if (g_blockEventsOnDrag
) return;
54 if (!tool
->m_enabled
) return;
58 tool
->m_toggleState
= !tool
->m_toggleState
;
60 if (tool
->m_bitmap2
.Ok())
62 wxBitmap bitmap
= tool
->m_bitmap1
;
63 if (tool
->m_toggleState
) bitmap
= tool
->m_bitmap2
;
65 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
67 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
68 if (bitmap
.GetMask()) mask
= bitmap
.GetMask()->GetBitmap();
70 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
74 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
77 //-----------------------------------------------------------------------------
78 // "enter_notify_event"
79 //-----------------------------------------------------------------------------
81 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
82 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
84 if (g_isIdle
) wxapp_install_idle_handler();
86 if (g_blockEventsOnDrag
) return TRUE
;
89 wxToolBar
*tb
= tool
->m_owner
;
91 #if (GTK_MINOR_VERSION == 0)
92 /* we grey-out the tip text of disabled tool in GTK 1.0 */
95 if (tb
->m_fg
->red
!= 0)
100 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
102 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
107 if (tb
->m_fg
->red
== 0)
109 tb
->m_fg
->red
= 33000;
110 tb
->m_fg
->green
= 33000;
111 tb
->m_fg
->blue
= 33000;
112 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
113 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
120 tb
->OnMouseEnter( tool
->m_index
);
125 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
129 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
131 BEGIN_EVENT_TABLE(wxToolBar
, wxControl
)
132 EVT_IDLE(wxToolBar::OnIdle
)
135 wxToolBar::wxToolBar()
139 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
140 const wxPoint
& pos
, const wxSize
& size
,
141 long style
, const wxString
& name
)
143 Create( parent
, id
, pos
, size
, style
, name
);
146 wxToolBar::~wxToolBar()
152 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
153 const wxPoint
& pos
, const wxSize
& size
,
154 long style
, const wxString
& name
)
157 m_blockNextEvent
= FALSE
;
159 if (!PreCreation( parent
, pos
, size
) ||
160 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
162 wxFAIL_MSG( wxT("wxToolBar creation failed") );
166 m_tools
.DeleteContents( TRUE
);
168 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
169 GTK_TOOLBAR_ICONS
) );
172 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
173 m_hasToolAlready
= FALSE
;
175 if (style
& wxTB_DOCKABLE
)
177 m_widget
= gtk_handle_box_new();
178 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
179 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
181 #if (GTK_MINOR_VERSION > 0)
182 if (style
& wxTB_FLAT
)
183 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
188 m_widget
= GTK_WIDGET(m_toolbar
);
191 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
193 #if (GTK_MINOR_VERSION > 0)
194 if (style
& wxTB_FLAT
)
195 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
202 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
208 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
210 #if (GTK_MINOR_VERSION > 0)
211 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
215 gtk_widget_get_style(
216 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
218 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
219 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
221 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
227 m_parent
->DoAddChild( this );
236 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
238 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
239 event
.SetEventObject(this);
240 event
.SetInt( toolIndex
);
241 event
.SetExtraLong((long) toggleDown
);
243 GetEventHandler()->ProcessEvent(event
);
248 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
250 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
251 event
.SetEventObject( this );
252 event
.SetInt( toolIndex
);
254 GetEventHandler()->ProcessEvent(event
);
257 void wxToolBar::OnMouseEnter( int toolIndex
)
259 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
260 event
.SetEventObject(this);
261 event
.SetInt( toolIndex
);
263 GetEventHandler()->ProcessEvent(event
);
266 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
267 const wxBitmap
& pushedBitmap
, bool toggle
,
268 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
269 const wxString
& helpString1
, const wxString
& helpString2
)
271 m_hasToolAlready
= TRUE
;
273 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
274 wxT("invalid bitmap for wxToolBar icon") );
276 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
277 wxT("wxToolBar doesn't support GdkBitmap") );
279 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
280 wxT("wxToolBar::Add needs a wxBitmap") );
282 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
284 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
286 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
287 if ( bitmap
.GetMask() )
288 mask
= bitmap
.GetMask()->GetBitmap();
290 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
291 #if (GTK_MINOR_VERSION > 0)
292 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
295 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
297 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
299 helpString1
, helpString2
,
302 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
303 : GTK_TOOLBAR_CHILD_BUTTON
;
305 GtkWidget
*item
= gtk_toolbar_append_element
307 GTK_TOOLBAR(m_toolbar
),
311 helpString1
.mbc_str(),
314 (GtkSignalFunc
)gtk_toolbar_callback
,
321 (* GTK_WIDGET_CLASS( GTK_OBJECT(m_widget
)->klass
)->size_request
) (m_widget
, &req
);
323 m_height
= req
.height
;
325 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
326 "enter_notify_event",
327 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
330 m_tools
.Append( tool
);
335 void wxToolBar::AddSeparator()
337 gtk_toolbar_append_space( m_toolbar
);
340 void wxToolBar::ClearTools()
342 wxFAIL_MSG( wxT("wxToolBar::ClearTools not implemented") );
345 bool wxToolBar::Realize()
352 wxNode
*node
= m_tools
.First();
355 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
356 if (tool
->m_bitmap1
.Ok())
358 int tool_height
= tool
->m_bitmap1
.GetHeight();
359 if (tool_height
> m_height
) m_height
= tool_height
;
365 m_height
+= 5 + 2*m_yMargin
;
370 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
372 wxNode
*node
= m_tools
.First();
375 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
376 if (tool
->m_index
== toolIndex
)
378 tool
->m_enabled
= enable
;
380 #if (GTK_MINOR_VERSION > 0)
381 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
382 greyed anyway and this also disables tooltips */
384 gtk_widget_set_sensitive( tool
->m_item
, enable
);
392 wxFAIL_MSG( wxT("wrong toolbar index") );
395 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
397 wxNode
*node
= m_tools
.First();
400 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
401 if (tool
->m_index
== toolIndex
)
403 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
405 tool
->m_toggleState
= toggle
;
407 if (tool
->m_bitmap2
.Ok())
409 wxBitmap bitmap
= tool
->m_bitmap1
;
410 if (tool
->m_toggleState
) bitmap
= tool
->m_bitmap2
;
412 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
414 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
415 if (bitmap
.GetMask()) mask
= bitmap
.GetMask()->GetBitmap();
417 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
420 m_blockNextEvent
= TRUE
; // we cannot use gtk_signal_disconnect here
422 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
430 wxFAIL_MSG( wxT("wrong toolbar index") );
433 wxObject
*wxToolBar::GetToolClientData( int index
) const
435 wxNode
*node
= m_tools
.First();
438 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
439 if (tool
->m_index
== index
) return tool
->m_clientData
;;
443 wxFAIL_MSG( wxT("wrong toolbar index") );
445 return (wxObject
*)NULL
;
448 bool wxToolBar::GetToolState(int toolIndex
) const
450 wxNode
*node
= m_tools
.First();
453 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
454 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
458 wxFAIL_MSG( wxT("wrong toolbar index") );
463 bool wxToolBar::GetToolEnabled(int toolIndex
) const
465 wxNode
*node
= m_tools
.First();
468 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
469 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
473 wxFAIL_MSG( wxT("wrong toolbar index") );
478 void wxToolBar::SetMargins( int x
, int y
)
480 wxCHECK_RET( !m_hasToolAlready
, wxT("wxToolBar::SetMargins must be called before adding tool.") );
482 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
488 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
490 wxFAIL_MSG( wxT("wxToolBar::SetToolPacking not implemented") );
493 void wxToolBar::SetToolSeparation( int separation
)
495 gtk_toolbar_set_space_size( m_toolbar
, separation
);
496 m_separation
= separation
;
499 int wxToolBar::GetToolPacking()
504 int wxToolBar::GetToolSeparation()
509 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
511 wxNode
*node
= m_tools
.First();
514 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
515 if (tool
->m_index
== toolIndex
)
517 return tool
->m_longHelpString
;
522 wxFAIL_MSG( wxT("wrong toolbar index") );
527 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
529 wxNode
*node
= m_tools
.First();
532 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
533 if (tool
->m_index
== toolIndex
)
535 return tool
->m_shortHelpString
;
540 wxFAIL_MSG( wxT("wrong toolbar index") );
545 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
547 wxNode
*node
= m_tools
.First();
550 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
551 if (tool
->m_index
== toolIndex
)
553 tool
->m_longHelpString
= helpString
;
559 wxFAIL_MSG( wxT("wrong toolbar index") );
564 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
566 wxNode
*node
= m_tools
.First();
569 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
570 if (tool
->m_index
== toolIndex
)
572 tool
->m_shortHelpString
= helpString
;
578 wxFAIL_MSG( wxT("wrong toolbar index") );
583 void wxToolBar::OnIdle( wxIdleEvent
&WXUNUSED(ievent
) )
585 wxEvtHandler
* evtHandler
= GetEventHandler();
587 wxNode
* node
= m_tools
.First();
590 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->Data();
592 wxUpdateUIEvent
event( tool
->m_index
);
593 event
.SetEventObject(this);
595 if (evtHandler
->ProcessEvent( event
))
597 if (event
.GetSetEnabled())
598 EnableTool(tool
->m_index
, event
.GetEnabled());
599 if (event
.GetSetChecked())
600 ToggleTool(tool
->m_index
, event
.GetChecked());
602 if (event.GetSetText())
611 void wxToolBar::OnInternalIdle()
613 wxCursor cursor
= m_cursor
;
614 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
618 /* I now set the cursor the anew in every OnInternalIdle call
619 as setting the cursor in a parent window also effects the
620 windows above so that checking for the current cursor is
623 if (HasFlag(wxTB_DOCKABLE
))
625 /* if the toolbar is dockable, then m_widget stands for the
626 GtkHandleBox widget, which uses its own window so that we
627 can set the cursor for it. if the toolbar is not dockable,
628 m_widget comes from m_toolbar which uses its parent's
629 window ("windowless windows") and thus we cannot set the
631 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
634 wxNode
*node
= m_tools
.First();
637 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
638 if (!tool
->m_item
->window
)
641 gdk_window_set_cursor( tool
->m_item
->window
, cursor
.GetCursor() );