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;
47 tool
->m_toggleState
= !tool
->m_toggleState
;
49 if (tool
->m_bitmap2
.Ok())
51 wxBitmap bitmap
= tool
->m_bitmap1
;
52 if (tool
->m_toggleState
) bitmap
= tool
->m_bitmap2
;
54 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
56 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
57 if (bitmap
.GetMask()) mask
= bitmap
.GetMask()->GetBitmap();
59 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
63 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
66 //-----------------------------------------------------------------------------
67 // "enter_notify_event"
68 //-----------------------------------------------------------------------------
70 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
71 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
73 if (g_isIdle
) wxapp_install_idle_handler();
75 if (g_blockEventsOnDrag
) return TRUE
;
77 /* we grey-out the tip text of disabled tool */
79 wxToolBar
*tb
= tool
->m_owner
;
83 if (tb
->m_fg
->red
!= 0)
88 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
90 #if (GTK_MINOR_VERSION > 0)
94 GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
->tip_window
) );
96 g_style
->fg
[GTK_STATE_NORMAL
] = *tb
->m_fg
;
97 gtk_widget_set_style( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
->tip_window
, g_style
);
99 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
105 if (tb
->m_fg
->red
== 0)
107 tb
->m_fg
->red
= 33000;
108 tb
->m_fg
->green
= 33000;
109 tb
->m_fg
->blue
= 33000;
110 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
111 #if (GTK_MINOR_VERSION > 0)
114 gtk_widget_get_style(
115 GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
->tip_window
) );
117 g_style
->fg
[GTK_STATE_NORMAL
] = *tb
->m_fg
;
118 gtk_widget_set_style( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
->tip_window
, g_style
);
120 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
127 tb
->OnMouseEnter( tool
->m_index
);
132 //-----------------------------------------------------------------------------
134 //-----------------------------------------------------------------------------
136 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
138 BEGIN_EVENT_TABLE(wxToolBar
, wxControl
)
139 EVT_IDLE(wxToolBar::OnIdle
)
142 wxToolBar::wxToolBar()
146 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
147 const wxPoint
& pos
, const wxSize
& size
,
148 long style
, const wxString
& name
)
150 Create( parent
, id
, pos
, size
, style
, name
);
153 wxToolBar::~wxToolBar()
159 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
160 const wxPoint
& pos
, const wxSize
& size
,
161 long style
, const wxString
& name
)
165 PreCreation( parent
, id
, pos
, size
, style
, name
);
167 m_tools
.DeleteContents( TRUE
);
169 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
170 GTK_TOOLBAR_ICONS
) );
173 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
174 m_hasToolAlready
= FALSE
;
176 if (style
& wxTB_DOCKABLE
)
178 m_widget
= gtk_handle_box_new();
179 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
180 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
182 #if (GTK_MINOR_VERSION > 0)
183 if (style
& wxTB_FLAT
)
184 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
189 m_widget
= GTK_WIDGET(m_toolbar
);
192 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
194 #if (GTK_MINOR_VERSION > 0)
195 if (style
& wxTB_FLAT
)
196 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
203 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
209 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
211 #if (GTK_MINOR_VERSION > 0)
212 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
216 gtk_widget_get_style(
217 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
219 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
220 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
222 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
228 m_parent
->DoAddChild( this );
237 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
239 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
240 event
.SetEventObject(this);
241 event
.SetInt( toolIndex
);
242 event
.SetExtraLong((long) toggleDown
);
244 GetEventHandler()->ProcessEvent(event
);
249 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
251 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
252 event
.SetEventObject( this );
253 event
.SetInt( toolIndex
);
255 GetEventHandler()->ProcessEvent(event
);
258 void wxToolBar::OnMouseEnter( int toolIndex
)
260 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
261 event
.SetEventObject(this);
262 event
.SetInt( toolIndex
);
264 GetEventHandler()->ProcessEvent(event
);
267 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
268 const wxBitmap
& pushedBitmap
, bool toggle
,
269 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
270 const wxString
& helpString1
, const wxString
& helpString2
)
272 m_hasToolAlready
= TRUE
;
274 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
275 _T("invalid bitmap for wxToolBar icon") );
277 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
278 _T("wxToolBar doesn't support GdkBitmap") );
280 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
281 _T("wxToolBar::Add needs a wxBitmap") );
283 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
285 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
287 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
288 if ( bitmap
.GetMask() )
289 mask
= bitmap
.GetMask()->GetBitmap();
291 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
293 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
295 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
297 helpString1
, helpString2
,
300 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
301 : GTK_TOOLBAR_CHILD_BUTTON
;
303 GtkWidget
*item
= gtk_toolbar_append_element
305 GTK_TOOLBAR(m_toolbar
),
309 helpString1
.mbc_str(),
312 (GtkSignalFunc
)gtk_toolbar_callback
,
318 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
319 "enter_notify_event",
320 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
323 m_tools
.Append( tool
);
328 void wxToolBar::AddSeparator()
330 gtk_toolbar_append_space( m_toolbar
);
333 void wxToolBar::ClearTools()
335 wxFAIL_MSG( _T("wxToolBar::ClearTools not implemented") );
338 bool wxToolBar::Realize()
345 wxNode
*node
= m_tools
.First();
348 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
349 if (tool
->m_bitmap1
.Ok())
351 int tool_height
= tool
->m_bitmap1
.GetHeight();
352 if (tool_height
> m_height
) m_height
= tool_height
;
358 m_height
+= 5 + 2*m_yMargin
;
363 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
365 wxNode
*node
= m_tools
.First();
368 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
369 if (tool
->m_index
== toolIndex
)
371 tool
->m_enabled
= enable
;
373 /* we don't disable the tools for now as the bitmaps don't get
374 greyed anyway and this also disables tooltips
377 gtk_widget_set_sensitive( tool->m_item, enable );
385 wxFAIL_MSG( _T("wrong toolbar index") );
388 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
390 wxNode
*node
= m_tools
.First();
393 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
394 if (tool
->m_index
== toolIndex
)
396 tool
->m_toggleState
= toggle
;
397 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
398 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
404 wxFAIL_MSG( _T("wrong toolbar index") );
407 wxObject
*wxToolBar::GetToolClientData( int index
) const
409 wxNode
*node
= m_tools
.First();
412 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
413 if (tool
->m_index
== index
) return tool
->m_clientData
;;
417 wxFAIL_MSG( _T("wrong toolbar index") );
419 return (wxObject
*)NULL
;
422 bool wxToolBar::GetToolState(int toolIndex
) const
424 wxNode
*node
= m_tools
.First();
427 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
428 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
432 wxFAIL_MSG( _T("wrong toolbar index") );
437 bool wxToolBar::GetToolEnabled(int toolIndex
) const
439 wxNode
*node
= m_tools
.First();
442 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
443 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
447 wxFAIL_MSG( _T("wrong toolbar index") );
452 void wxToolBar::SetMargins( int x
, int y
)
454 wxCHECK_RET( !m_hasToolAlready
, _T("wxToolBar::SetMargins must be called before adding tool.") );
456 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
462 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
464 wxFAIL_MSG( _T("wxToolBar::SetToolPacking not implemented") );
467 void wxToolBar::SetToolSeparation( int separation
)
469 gtk_toolbar_set_space_size( m_toolbar
, separation
);
470 m_separation
= separation
;
473 int wxToolBar::GetToolPacking()
478 int wxToolBar::GetToolSeparation()
483 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
485 wxNode
*node
= m_tools
.First();
488 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
489 if (tool
->m_index
== toolIndex
)
491 return tool
->m_longHelpString
;
496 wxFAIL_MSG( _T("wrong toolbar index") );
501 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
503 wxNode
*node
= m_tools
.First();
506 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
507 if (tool
->m_index
== toolIndex
)
509 return tool
->m_shortHelpString
;
514 wxFAIL_MSG( _T("wrong toolbar index") );
519 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
521 wxNode
*node
= m_tools
.First();
524 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
525 if (tool
->m_index
== toolIndex
)
527 tool
->m_longHelpString
= helpString
;
533 wxFAIL_MSG( _T("wrong toolbar index") );
538 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
540 wxNode
*node
= m_tools
.First();
543 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
544 if (tool
->m_index
== toolIndex
)
546 tool
->m_shortHelpString
= helpString
;
552 wxFAIL_MSG( _T("wrong toolbar index") );
557 void wxToolBar::OnIdle( wxIdleEvent
&WXUNUSED(ievent
) )
559 wxEvtHandler
* evtHandler
= GetEventHandler();
561 wxNode
* node
= m_tools
.First();
564 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->Data();
566 wxUpdateUIEvent
event( tool
->m_index
);
567 event
.SetEventObject(this);
569 if (evtHandler
->ProcessEvent( event
))
571 if (event
.GetSetEnabled())
572 EnableTool(tool
->m_index
, event
.GetEnabled());
573 if (event
.GetSetChecked())
574 ToggleTool(tool
->m_index
, event
.GetChecked());
576 if (event.GetSetText())