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
;
37 //-----------------------------------------------------------------------------
38 // "clicked" (internal from gtk_toolbar)
39 //-----------------------------------------------------------------------------
41 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
43 if (g_isIdle
) wxapp_install_idle_handler();
45 if (g_blockEventsOnDrag
) return;
46 if (!tool
->m_enabled
) return;
50 tool
->m_toggleState
= !tool
->m_toggleState
;
52 if (tool
->m_bitmap2
.Ok())
54 wxBitmap bitmap
= tool
->m_bitmap1
;
55 if (tool
->m_toggleState
) bitmap
= tool
->m_bitmap2
;
57 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
59 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
60 if (bitmap
.GetMask()) mask
= bitmap
.GetMask()->GetBitmap();
62 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
66 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
69 //-----------------------------------------------------------------------------
70 // "enter_notify_event"
71 //-----------------------------------------------------------------------------
73 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
74 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
76 if (g_isIdle
) wxapp_install_idle_handler();
78 if (g_blockEventsOnDrag
) return TRUE
;
81 wxToolBar
*tb
= tool
->m_owner
;
83 /* we grey-out the tip text of disabled tool */
87 if (tb
->m_fg
->red
!= 0)
92 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
94 #if (GTK_MINOR_VERSION > 0)
98 GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
->tip_window
) );
100 g_style
->fg
[GTK_STATE_NORMAL
] = *tb
->m_fg
;
101 gtk_widget_set_style( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
->tip_window
, g_style
);
103 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
109 if (tb
->m_fg
->red
== 0)
111 tb
->m_fg
->red
= 33000;
112 tb
->m_fg
->green
= 33000;
113 tb
->m_fg
->blue
= 33000;
114 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
115 #if (GTK_MINOR_VERSION > 0)
118 gtk_widget_get_style(
119 GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
->tip_window
) );
121 g_style
->fg
[GTK_STATE_NORMAL
] = *tb
->m_fg
;
122 gtk_widget_set_style( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
->tip_window
, g_style
);
124 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
132 tb
->OnMouseEnter( tool
->m_index
);
137 //-----------------------------------------------------------------------------
139 //-----------------------------------------------------------------------------
141 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
143 BEGIN_EVENT_TABLE(wxToolBar
, wxControl
)
144 EVT_IDLE(wxToolBar::OnIdle
)
147 wxToolBar::wxToolBar()
151 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
152 const wxPoint
& pos
, const wxSize
& size
,
153 long style
, const wxString
& name
)
155 Create( parent
, id
, pos
, size
, style
, name
);
158 wxToolBar::~wxToolBar()
164 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
165 const wxPoint
& pos
, const wxSize
& size
,
166 long style
, const wxString
& name
)
170 PreCreation( parent
, id
, pos
, size
, style
, name
);
172 m_tools
.DeleteContents( TRUE
);
174 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
175 GTK_TOOLBAR_ICONS
) );
178 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
179 m_hasToolAlready
= FALSE
;
181 if (style
& wxTB_DOCKABLE
)
183 m_widget
= gtk_handle_box_new();
184 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
185 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
187 #if (GTK_MINOR_VERSION > 0)
188 if (style
& wxTB_FLAT
)
189 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
194 m_widget
= GTK_WIDGET(m_toolbar
);
197 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
199 #if (GTK_MINOR_VERSION > 0)
200 if (style
& wxTB_FLAT
)
201 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
208 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
214 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
216 #if (GTK_MINOR_VERSION > 0)
217 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
221 gtk_widget_get_style(
222 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
224 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
225 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
227 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
233 m_parent
->DoAddChild( this );
242 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
244 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
245 event
.SetEventObject(this);
246 event
.SetInt( toolIndex
);
247 event
.SetExtraLong((long) toggleDown
);
249 GetEventHandler()->ProcessEvent(event
);
254 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
256 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
257 event
.SetEventObject( this );
258 event
.SetInt( toolIndex
);
260 GetEventHandler()->ProcessEvent(event
);
263 void wxToolBar::OnMouseEnter( int toolIndex
)
265 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
266 event
.SetEventObject(this);
267 event
.SetInt( toolIndex
);
269 GetEventHandler()->ProcessEvent(event
);
272 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
273 const wxBitmap
& pushedBitmap
, bool toggle
,
274 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
275 const wxString
& helpString1
, const wxString
& helpString2
)
277 m_hasToolAlready
= TRUE
;
279 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
280 _T("invalid bitmap for wxToolBar icon") );
282 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
283 _T("wxToolBar doesn't support GdkBitmap") );
285 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
286 _T("wxToolBar::Add needs a wxBitmap") );
288 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
290 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
292 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
293 if ( bitmap
.GetMask() )
294 mask
= bitmap
.GetMask()->GetBitmap();
296 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
297 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
299 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
301 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
303 helpString1
, helpString2
,
306 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
307 : GTK_TOOLBAR_CHILD_BUTTON
;
309 GtkWidget
*item
= gtk_toolbar_append_element
311 GTK_TOOLBAR(m_toolbar
),
315 helpString1
.mbc_str(),
318 (GtkSignalFunc
)gtk_toolbar_callback
,
324 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
325 "enter_notify_event",
326 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
329 m_tools
.Append( tool
);
334 void wxToolBar::AddSeparator()
336 gtk_toolbar_append_space( m_toolbar
);
339 void wxToolBar::ClearTools()
341 wxFAIL_MSG( _T("wxToolBar::ClearTools not implemented") );
344 bool wxToolBar::Realize()
351 wxNode
*node
= m_tools
.First();
354 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
355 if (tool
->m_bitmap1
.Ok())
357 int tool_height
= tool
->m_bitmap1
.GetHeight();
358 if (tool_height
> m_height
) m_height
= tool_height
;
364 m_height
+= 5 + 2*m_yMargin
;
369 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
371 wxNode
*node
= m_tools
.First();
374 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
375 if (tool
->m_index
== toolIndex
)
377 tool
->m_enabled
= enable
;
379 /* we don't disable the tools for now as the bitmaps don't get
380 greyed anyway and this also disables tooltips */
383 gtk_widget_set_sensitive( tool
->m_item
, enable
);
390 wxFAIL_MSG( _T("wrong toolbar index") );
393 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
395 wxNode
*node
= m_tools
.First();
398 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
399 if (tool
->m_index
== toolIndex
)
401 tool
->m_toggleState
= toggle
;
402 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
403 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
409 wxFAIL_MSG( _T("wrong toolbar index") );
412 wxObject
*wxToolBar::GetToolClientData( int index
) const
414 wxNode
*node
= m_tools
.First();
417 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
418 if (tool
->m_index
== index
) return tool
->m_clientData
;;
422 wxFAIL_MSG( _T("wrong toolbar index") );
424 return (wxObject
*)NULL
;
427 bool wxToolBar::GetToolState(int toolIndex
) const
429 wxNode
*node
= m_tools
.First();
432 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
433 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
437 wxFAIL_MSG( _T("wrong toolbar index") );
442 bool wxToolBar::GetToolEnabled(int toolIndex
) const
444 wxNode
*node
= m_tools
.First();
447 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
448 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
452 wxFAIL_MSG( _T("wrong toolbar index") );
457 void wxToolBar::SetMargins( int x
, int y
)
459 wxCHECK_RET( !m_hasToolAlready
, _T("wxToolBar::SetMargins must be called before adding tool.") );
461 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
467 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
469 wxFAIL_MSG( _T("wxToolBar::SetToolPacking not implemented") );
472 void wxToolBar::SetToolSeparation( int separation
)
474 gtk_toolbar_set_space_size( m_toolbar
, separation
);
475 m_separation
= separation
;
478 int wxToolBar::GetToolPacking()
483 int wxToolBar::GetToolSeparation()
488 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
490 wxNode
*node
= m_tools
.First();
493 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
494 if (tool
->m_index
== toolIndex
)
496 return tool
->m_longHelpString
;
501 wxFAIL_MSG( _T("wrong toolbar index") );
506 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
508 wxNode
*node
= m_tools
.First();
511 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
512 if (tool
->m_index
== toolIndex
)
514 return tool
->m_shortHelpString
;
519 wxFAIL_MSG( _T("wrong toolbar index") );
524 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
526 wxNode
*node
= m_tools
.First();
529 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
530 if (tool
->m_index
== toolIndex
)
532 tool
->m_longHelpString
= helpString
;
538 wxFAIL_MSG( _T("wrong toolbar index") );
543 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
545 wxNode
*node
= m_tools
.First();
548 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
549 if (tool
->m_index
== toolIndex
)
551 tool
->m_shortHelpString
= helpString
;
557 wxFAIL_MSG( _T("wrong toolbar index") );
562 void wxToolBar::OnIdle( wxIdleEvent
&WXUNUSED(ievent
) )
564 wxEvtHandler
* evtHandler
= GetEventHandler();
566 wxNode
* node
= m_tools
.First();
569 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->Data();
571 wxUpdateUIEvent
event( tool
->m_index
);
572 event
.SetEventObject(this);
574 if (evtHandler
->ProcessEvent( event
))
576 if (event
.GetSetEnabled())
577 EnableTool(tool
->m_index
, event
.GetEnabled());
578 if (event
.GetSetChecked())
579 ToggleTool(tool
->m_index
, event
.GetChecked());
581 if (event.GetSetText())