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"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 extern bool g_blockEventsOnDrag
;
27 //-----------------------------------------------------------------------------
28 // "clicked" (internal from gtk_toolbar)
29 //-----------------------------------------------------------------------------
31 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
33 if (g_blockEventsOnDrag
) return;
34 if (!tool
->m_enabled
) return;
36 if (tool
->m_isToggle
) tool
->m_toggleState
= !tool
->m_toggleState
;
38 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
41 //-----------------------------------------------------------------------------
42 // "enter_notify_event"
43 //-----------------------------------------------------------------------------
45 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
46 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
48 if (g_blockEventsOnDrag
) return TRUE
;
50 /* we grey-out the tip text of disabled tool */
52 wxToolBar
*tb
= tool
->m_owner
;
56 if (tb
->m_fg
->red
!= 0)
61 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
62 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
67 if (tb
->m_fg
->red
== 0)
69 tb
->m_fg
->red
= 33000;
70 tb
->m_fg
->green
= 33000;
71 tb
->m_fg
->blue
= 33000;
72 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
73 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
79 tb
->OnMouseEnter( tool
->m_index
);
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
90 BEGIN_EVENT_TABLE(wxToolBar
, wxControl
)
91 EVT_IDLE(wxToolBar::OnIdle
)
94 wxToolBar::wxToolBar()
98 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
99 const wxPoint
& pos
, const wxSize
& size
,
100 long style
, const wxString
& name
)
102 Create( parent
, id
, pos
, size
, style
, name
);
105 wxToolBar::~wxToolBar()
111 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
112 const wxPoint
& pos
, const wxSize
& size
,
113 long style
, const wxString
& name
)
117 PreCreation( parent
, id
, pos
, size
, style
, name
);
119 m_tools
.DeleteContents( TRUE
);
121 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
122 GTK_TOOLBAR_ICONS
) );
125 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
126 m_hasToolAlready
= FALSE
;
128 if (style
& wxTB_DOCKABLE
)
130 m_widget
= gtk_handle_box_new();
131 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
132 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
136 m_widget
= GTK_WIDGET(m_toolbar
);
139 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
145 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
151 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
153 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
158 m_parent
->AddChild( this );
160 (m_parent
->m_insertCallback
)( m_parent
, this );
169 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
171 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
172 event
.SetEventObject(this);
173 event
.SetInt( toolIndex
);
174 event
.SetExtraLong((long) toggleDown
);
176 GetEventHandler()->ProcessEvent(event
);
181 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
183 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
184 event
.SetEventObject( this );
185 event
.SetInt( toolIndex
);
187 GetEventHandler()->ProcessEvent(event
);
190 void wxToolBar::OnMouseEnter( int toolIndex
)
192 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
193 event
.SetEventObject(this);
194 event
.SetInt( toolIndex
);
196 GetEventHandler()->ProcessEvent(event
);
199 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
200 const wxBitmap
& pushedBitmap
, bool toggle
,
201 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
202 const wxString
& helpString1
, const wxString
& helpString2
)
204 m_hasToolAlready
= TRUE
;
206 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
207 _T("invalid bitmap for wxToolBar icon") );
209 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
210 _T("wxToolBar doesn't support GdkBitmap") );
212 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
213 _T("wxToolBar::Add needs a wxBitmap") );
215 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
217 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
219 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
220 if ( bitmap
.GetMask() )
221 mask
= bitmap
.GetMask()->GetBitmap();
223 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
225 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
227 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
229 helpString1
, helpString2
,
232 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
233 : GTK_TOOLBAR_CHILD_BUTTON
;
235 GtkWidget
*item
= gtk_toolbar_append_element
237 GTK_TOOLBAR(m_toolbar
),
241 helpString1
.mbc_str(),
244 (GtkSignalFunc
)gtk_toolbar_callback
,
250 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
251 "enter_notify_event",
252 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
255 m_tools
.Append( tool
);
260 void wxToolBar::AddSeparator()
262 gtk_toolbar_append_space( m_toolbar
);
265 void wxToolBar::ClearTools()
267 wxFAIL_MSG( _T("wxToolBar::ClearTools not implemented") );
270 bool wxToolBar::Realize()
277 wxNode
*node
= m_tools
.First();
280 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
281 if (tool
->m_bitmap1
.Ok())
283 int tool_height
= tool
->m_bitmap1
.GetHeight();
284 if (tool_height
> m_height
) m_height
= tool_height
;
290 m_height
+= 5 + 2*m_yMargin
;
295 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
297 wxNode
*node
= m_tools
.First();
300 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
301 if (tool
->m_index
== toolIndex
)
303 tool
->m_enabled
= enable
;
305 /* we don't disable the tools for now as the bitmaps don't get
306 greyed anyway and this also disables tooltips
309 gtk_widget_set_sensitive( tool->m_item, enable );
317 wxFAIL_MSG( _T("wrong toolbar index") );
320 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
322 wxNode
*node
= m_tools
.First();
325 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
326 if (tool
->m_index
== toolIndex
)
328 tool
->m_toggleState
= toggle
;
329 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
330 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
336 wxFAIL_MSG( _T("wrong toolbar index") );
339 wxObject
*wxToolBar::GetToolClientData( int index
) const
341 wxNode
*node
= m_tools
.First();
344 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
345 if (tool
->m_index
== index
) return tool
->m_clientData
;;
349 wxFAIL_MSG( _T("wrong toolbar index") );
351 return (wxObject
*)NULL
;
354 bool wxToolBar::GetToolState(int toolIndex
) const
356 wxNode
*node
= m_tools
.First();
359 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
360 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
364 wxFAIL_MSG( _T("wrong toolbar index") );
369 bool wxToolBar::GetToolEnabled(int toolIndex
) const
371 wxNode
*node
= m_tools
.First();
374 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
375 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
379 wxFAIL_MSG( _T("wrong toolbar index") );
384 void wxToolBar::SetMargins( int x
, int y
)
386 wxCHECK_RET( !m_hasToolAlready
, _T("wxToolBar::SetMargins must be called before adding tool.") );
388 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
394 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
396 wxFAIL_MSG( _T("wxToolBar::SetToolPacking not implemented") );
399 void wxToolBar::SetToolSeparation( int separation
)
401 gtk_toolbar_set_space_size( m_toolbar
, separation
);
402 m_separation
= separation
;
405 int wxToolBar::GetToolPacking()
410 int wxToolBar::GetToolSeparation()
415 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
417 wxNode
*node
= m_tools
.First();
420 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
421 if (tool
->m_index
== toolIndex
)
423 return tool
->m_longHelpString
;
428 wxFAIL_MSG( _T("wrong toolbar index") );
433 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
435 wxNode
*node
= m_tools
.First();
438 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
439 if (tool
->m_index
== toolIndex
)
441 return tool
->m_shortHelpString
;
446 wxFAIL_MSG( _T("wrong toolbar index") );
451 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
453 wxNode
*node
= m_tools
.First();
456 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
457 if (tool
->m_index
== toolIndex
)
459 tool
->m_longHelpString
= helpString
;
465 wxFAIL_MSG( _T("wrong toolbar index") );
470 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
472 wxNode
*node
= m_tools
.First();
475 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
476 if (tool
->m_index
== toolIndex
)
478 tool
->m_shortHelpString
= helpString
;
484 wxFAIL_MSG( _T("wrong toolbar index") );
489 void wxToolBar::OnIdle( wxIdleEvent
&WXUNUSED(ievent
) )
491 wxEvtHandler
* evtHandler
= GetEventHandler();
493 wxNode
* node
= m_tools
.First();
496 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->Data();
498 wxUpdateUIEvent
event( tool
->m_index
);
499 event
.SetEventObject(this);
501 if (evtHandler
->ProcessEvent( event
))
503 if (event
.GetSetEnabled())
504 EnableTool(tool
->m_index
, event
.GetEnabled());
505 if (event
.GetSetChecked())
506 ToggleTool(tool
->m_index
, event
.GetChecked());
508 if (event.GetSetText())