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 #if (GTK_MINOR_VERSION == 0)
84 /* we grey-out the tip text of disabled tool in GTK 1.0 */
87 if (tb
->m_fg
->red
!= 0)
92 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
94 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
99 if (tb
->m_fg
->red
== 0)
101 tb
->m_fg
->red
= 33000;
102 tb
->m_fg
->green
= 33000;
103 tb
->m_fg
->blue
= 33000;
104 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
105 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
112 tb
->OnMouseEnter( tool
->m_index
);
117 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
121 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
123 BEGIN_EVENT_TABLE(wxToolBar
, wxControl
)
124 EVT_IDLE(wxToolBar::OnIdle
)
127 wxToolBar::wxToolBar()
131 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
132 const wxPoint
& pos
, const wxSize
& size
,
133 long style
, const wxString
& name
)
135 Create( parent
, id
, pos
, size
, style
, name
);
138 wxToolBar::~wxToolBar()
144 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
145 const wxPoint
& pos
, const wxSize
& size
,
146 long style
, const wxString
& name
)
150 if (!PreCreation( parent
, pos
, size
) ||
151 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
153 wxFAIL_MSG( _T("wxToolBar creation failed") );
157 m_tools
.DeleteContents( TRUE
);
159 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
160 GTK_TOOLBAR_ICONS
) );
163 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
164 m_hasToolAlready
= FALSE
;
166 if (style
& wxTB_DOCKABLE
)
168 m_widget
= gtk_handle_box_new();
169 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
170 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
172 #if (GTK_MINOR_VERSION > 0)
173 if (style
& wxTB_FLAT
)
174 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
179 m_widget
= GTK_WIDGET(m_toolbar
);
182 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
184 #if (GTK_MINOR_VERSION > 0)
185 if (style
& wxTB_FLAT
)
186 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
193 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
199 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
201 #if (GTK_MINOR_VERSION > 0)
202 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
206 gtk_widget_get_style(
207 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
209 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
210 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
212 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
218 m_parent
->DoAddChild( this );
227 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
229 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
230 event
.SetEventObject(this);
231 event
.SetInt( toolIndex
);
232 event
.SetExtraLong((long) toggleDown
);
234 GetEventHandler()->ProcessEvent(event
);
239 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
241 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
242 event
.SetEventObject( this );
243 event
.SetInt( toolIndex
);
245 GetEventHandler()->ProcessEvent(event
);
248 void wxToolBar::OnMouseEnter( int toolIndex
)
250 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
251 event
.SetEventObject(this);
252 event
.SetInt( toolIndex
);
254 GetEventHandler()->ProcessEvent(event
);
257 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
258 const wxBitmap
& pushedBitmap
, bool toggle
,
259 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
260 const wxString
& helpString1
, const wxString
& helpString2
)
262 m_hasToolAlready
= TRUE
;
264 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
265 _T("invalid bitmap for wxToolBar icon") );
267 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
268 _T("wxToolBar doesn't support GdkBitmap") );
270 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
271 _T("wxToolBar::Add needs a wxBitmap") );
273 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
275 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
277 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
278 if ( bitmap
.GetMask() )
279 mask
= bitmap
.GetMask()->GetBitmap();
281 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
282 #if (GTK_MINOR_VERSION > 0)
283 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
286 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
288 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
290 helpString1
, helpString2
,
293 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
294 : GTK_TOOLBAR_CHILD_BUTTON
;
296 GtkWidget
*item
= gtk_toolbar_append_element
298 GTK_TOOLBAR(m_toolbar
),
302 helpString1
.mbc_str(),
305 (GtkSignalFunc
)gtk_toolbar_callback
,
311 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
312 "enter_notify_event",
313 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
316 m_tools
.Append( tool
);
321 void wxToolBar::AddSeparator()
323 gtk_toolbar_append_space( m_toolbar
);
326 void wxToolBar::ClearTools()
328 wxFAIL_MSG( _T("wxToolBar::ClearTools not implemented") );
331 bool wxToolBar::Realize()
338 wxNode
*node
= m_tools
.First();
341 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
342 if (tool
->m_bitmap1
.Ok())
344 int tool_height
= tool
->m_bitmap1
.GetHeight();
345 if (tool_height
> m_height
) m_height
= tool_height
;
351 m_height
+= 5 + 2*m_yMargin
;
356 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
358 wxNode
*node
= m_tools
.First();
361 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
362 if (tool
->m_index
== toolIndex
)
364 tool
->m_enabled
= enable
;
366 #if (GTK_MINOR_VERSION > 0)
367 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
368 greyed anyway and this also disables tooltips */
370 gtk_widget_set_sensitive( tool
->m_item
, enable
);
378 wxFAIL_MSG( _T("wrong toolbar index") );
381 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
383 wxNode
*node
= m_tools
.First();
386 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
387 if (tool
->m_index
== toolIndex
)
389 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
391 tool
->m_toggleState
= toggle
;
393 if (tool
->m_bitmap2
.Ok())
395 wxBitmap bitmap
= tool
->m_bitmap1
;
396 if (tool
->m_toggleState
) bitmap
= tool
->m_bitmap2
;
398 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
400 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
401 if (bitmap
.GetMask()) mask
= bitmap
.GetMask()->GetBitmap();
403 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
406 gtk_signal_disconnect_by_func( GTK_OBJECT(tool
->m_item
),
407 GTK_SIGNAL_FUNC(gtk_toolbar_callback
), (gpointer
*)tool
);
409 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
411 gtk_signal_connect( GTK_OBJECT(tool
->m_item
), "clicked",
412 GTK_SIGNAL_FUNC(gtk_toolbar_callback
), (gpointer
*)tool
);
420 wxFAIL_MSG( _T("wrong toolbar index") );
423 wxObject
*wxToolBar::GetToolClientData( int index
) const
425 wxNode
*node
= m_tools
.First();
428 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
429 if (tool
->m_index
== index
) return tool
->m_clientData
;;
433 wxFAIL_MSG( _T("wrong toolbar index") );
435 return (wxObject
*)NULL
;
438 bool wxToolBar::GetToolState(int toolIndex
) const
440 wxNode
*node
= m_tools
.First();
443 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
444 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
448 wxFAIL_MSG( _T("wrong toolbar index") );
453 bool wxToolBar::GetToolEnabled(int toolIndex
) const
455 wxNode
*node
= m_tools
.First();
458 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
459 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
463 wxFAIL_MSG( _T("wrong toolbar index") );
468 void wxToolBar::SetMargins( int x
, int y
)
470 wxCHECK_RET( !m_hasToolAlready
, _T("wxToolBar::SetMargins must be called before adding tool.") );
472 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
478 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
480 wxFAIL_MSG( _T("wxToolBar::SetToolPacking not implemented") );
483 void wxToolBar::SetToolSeparation( int separation
)
485 gtk_toolbar_set_space_size( m_toolbar
, separation
);
486 m_separation
= separation
;
489 int wxToolBar::GetToolPacking()
494 int wxToolBar::GetToolSeparation()
499 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
501 wxNode
*node
= m_tools
.First();
504 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
505 if (tool
->m_index
== toolIndex
)
507 return tool
->m_longHelpString
;
512 wxFAIL_MSG( _T("wrong toolbar index") );
517 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
519 wxNode
*node
= m_tools
.First();
522 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
523 if (tool
->m_index
== toolIndex
)
525 return tool
->m_shortHelpString
;
530 wxFAIL_MSG( _T("wrong toolbar index") );
535 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
537 wxNode
*node
= m_tools
.First();
540 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
541 if (tool
->m_index
== toolIndex
)
543 tool
->m_longHelpString
= helpString
;
549 wxFAIL_MSG( _T("wrong toolbar index") );
554 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
556 wxNode
*node
= m_tools
.First();
559 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
560 if (tool
->m_index
== toolIndex
)
562 tool
->m_shortHelpString
= helpString
;
568 wxFAIL_MSG( _T("wrong toolbar index") );
573 void wxToolBar::OnIdle( wxIdleEvent
&WXUNUSED(ievent
) )
575 wxEvtHandler
* evtHandler
= GetEventHandler();
577 wxNode
* node
= m_tools
.First();
580 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->Data();
582 wxUpdateUIEvent
event( tool
->m_index
);
583 event
.SetEventObject(this);
585 if (evtHandler
->ProcessEvent( event
))
587 if (event
.GetSetEnabled())
588 EnableTool(tool
->m_index
, event
.GetEnabled());
589 if (event
.GetSetChecked())
590 ToggleTool(tool
->m_index
, event
.GetChecked());
592 if (event.GetSetText())