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 void wxapp_install_idle_handler();
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern bool g_blockEventsOnDrag
;
34 //-----------------------------------------------------------------------------
35 // "clicked" (internal from gtk_toolbar)
36 //-----------------------------------------------------------------------------
38 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
40 if (g_isIdle
) wxapp_install_idle_handler();
42 if (g_blockEventsOnDrag
) return;
43 if (!tool
->m_enabled
) return;
45 if (tool
->m_isToggle
) tool
->m_toggleState
= !tool
->m_toggleState
;
47 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
50 //-----------------------------------------------------------------------------
51 // "enter_notify_event"
52 //-----------------------------------------------------------------------------
54 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
55 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
57 if (g_isIdle
) wxapp_install_idle_handler();
59 if (g_blockEventsOnDrag
) return TRUE
;
61 /* we grey-out the tip text of disabled tool */
63 wxToolBar
*tb
= tool
->m_owner
;
67 if (tb
->m_fg
->red
!= 0)
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
);
78 if (tb
->m_fg
->red
== 0)
80 tb
->m_fg
->red
= 33000;
81 tb
->m_fg
->green
= 33000;
82 tb
->m_fg
->blue
= 33000;
83 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
84 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
90 tb
->OnMouseEnter( tool
->m_index
);
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
99 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
101 BEGIN_EVENT_TABLE(wxToolBar
, wxControl
)
102 EVT_IDLE(wxToolBar::OnIdle
)
105 wxToolBar::wxToolBar()
109 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
110 const wxPoint
& pos
, const wxSize
& size
,
111 long style
, const wxString
& name
)
113 Create( parent
, id
, pos
, size
, style
, name
);
116 wxToolBar::~wxToolBar()
122 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
123 const wxPoint
& pos
, const wxSize
& size
,
124 long style
, const wxString
& name
)
128 PreCreation( parent
, id
, pos
, size
, style
, name
);
130 m_tools
.DeleteContents( TRUE
);
132 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
133 GTK_TOOLBAR_ICONS
) );
136 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
137 m_hasToolAlready
= FALSE
;
139 if (style
& wxTB_DOCKABLE
)
141 m_widget
= gtk_handle_box_new();
142 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
143 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
147 m_widget
= GTK_WIDGET(m_toolbar
);
150 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
156 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
162 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
164 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
169 m_parent
->AddChild( this );
171 (m_parent
->m_insertCallback
)( m_parent
, this );
180 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
182 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
183 event
.SetEventObject(this);
184 event
.SetInt( toolIndex
);
185 event
.SetExtraLong((long) toggleDown
);
187 GetEventHandler()->ProcessEvent(event
);
192 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
194 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
195 event
.SetEventObject( this );
196 event
.SetInt( toolIndex
);
198 GetEventHandler()->ProcessEvent(event
);
201 void wxToolBar::OnMouseEnter( int toolIndex
)
203 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
204 event
.SetEventObject(this);
205 event
.SetInt( toolIndex
);
207 GetEventHandler()->ProcessEvent(event
);
210 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
211 const wxBitmap
& pushedBitmap
, bool toggle
,
212 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
213 const wxString
& helpString1
, const wxString
& helpString2
)
215 m_hasToolAlready
= TRUE
;
217 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
218 _T("invalid bitmap for wxToolBar icon") );
220 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
221 _T("wxToolBar doesn't support GdkBitmap") );
223 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
224 _T("wxToolBar::Add needs a wxBitmap") );
226 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
228 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
230 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
231 if ( bitmap
.GetMask() )
232 mask
= bitmap
.GetMask()->GetBitmap();
234 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
236 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
238 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
240 helpString1
, helpString2
,
243 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
244 : GTK_TOOLBAR_CHILD_BUTTON
;
246 GtkWidget
*item
= gtk_toolbar_append_element
248 GTK_TOOLBAR(m_toolbar
),
252 helpString1
.mbc_str(),
255 (GtkSignalFunc
)gtk_toolbar_callback
,
261 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
262 "enter_notify_event",
263 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
266 m_tools
.Append( tool
);
271 void wxToolBar::AddSeparator()
273 gtk_toolbar_append_space( m_toolbar
);
276 void wxToolBar::ClearTools()
278 wxFAIL_MSG( _T("wxToolBar::ClearTools not implemented") );
281 bool wxToolBar::Realize()
288 wxNode
*node
= m_tools
.First();
291 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
292 if (tool
->m_bitmap1
.Ok())
294 int tool_height
= tool
->m_bitmap1
.GetHeight();
295 if (tool_height
> m_height
) m_height
= tool_height
;
301 m_height
+= 5 + 2*m_yMargin
;
306 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
308 wxNode
*node
= m_tools
.First();
311 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
312 if (tool
->m_index
== toolIndex
)
314 tool
->m_enabled
= enable
;
316 /* we don't disable the tools for now as the bitmaps don't get
317 greyed anyway and this also disables tooltips
320 gtk_widget_set_sensitive( tool->m_item, enable );
328 wxFAIL_MSG( _T("wrong toolbar index") );
331 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
333 wxNode
*node
= m_tools
.First();
336 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
337 if (tool
->m_index
== toolIndex
)
339 tool
->m_toggleState
= toggle
;
340 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
341 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
347 wxFAIL_MSG( _T("wrong toolbar index") );
350 wxObject
*wxToolBar::GetToolClientData( int index
) const
352 wxNode
*node
= m_tools
.First();
355 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
356 if (tool
->m_index
== index
) return tool
->m_clientData
;;
360 wxFAIL_MSG( _T("wrong toolbar index") );
362 return (wxObject
*)NULL
;
365 bool wxToolBar::GetToolState(int toolIndex
) const
367 wxNode
*node
= m_tools
.First();
370 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
371 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
375 wxFAIL_MSG( _T("wrong toolbar index") );
380 bool wxToolBar::GetToolEnabled(int toolIndex
) const
382 wxNode
*node
= m_tools
.First();
385 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
386 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
390 wxFAIL_MSG( _T("wrong toolbar index") );
395 void wxToolBar::SetMargins( int x
, int y
)
397 wxCHECK_RET( !m_hasToolAlready
, _T("wxToolBar::SetMargins must be called before adding tool.") );
399 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
405 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
407 wxFAIL_MSG( _T("wxToolBar::SetToolPacking not implemented") );
410 void wxToolBar::SetToolSeparation( int separation
)
412 gtk_toolbar_set_space_size( m_toolbar
, separation
);
413 m_separation
= separation
;
416 int wxToolBar::GetToolPacking()
421 int wxToolBar::GetToolSeparation()
426 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
428 wxNode
*node
= m_tools
.First();
431 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
432 if (tool
->m_index
== toolIndex
)
434 return tool
->m_longHelpString
;
439 wxFAIL_MSG( _T("wrong toolbar index") );
444 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
446 wxNode
*node
= m_tools
.First();
449 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
450 if (tool
->m_index
== toolIndex
)
452 return tool
->m_shortHelpString
;
457 wxFAIL_MSG( _T("wrong toolbar index") );
462 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
464 wxNode
*node
= m_tools
.First();
467 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
468 if (tool
->m_index
== toolIndex
)
470 tool
->m_longHelpString
= helpString
;
476 wxFAIL_MSG( _T("wrong toolbar index") );
481 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
483 wxNode
*node
= m_tools
.First();
486 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
487 if (tool
->m_index
== toolIndex
)
489 tool
->m_shortHelpString
= helpString
;
495 wxFAIL_MSG( _T("wrong toolbar index") );
500 void wxToolBar::OnIdle( wxIdleEvent
&WXUNUSED(ievent
) )
502 wxEvtHandler
* evtHandler
= GetEventHandler();
504 wxNode
* node
= m_tools
.First();
507 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->Data();
509 wxUpdateUIEvent
event( tool
->m_index
);
510 event
.SetEventObject(this);
512 if (evtHandler
->ProcessEvent( event
))
514 if (event
.GetSetEnabled())
515 EnableTool(tool
->m_index
, event
.GetEnabled());
516 if (event
.GetSetChecked())
517 ToggleTool(tool
->m_index
, event
.GetChecked());
519 if (event.GetSetText())