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 PreCreation( parent
, id
, pos
, size
, style
, name
);
152 m_tools
.DeleteContents( TRUE
);
154 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
155 GTK_TOOLBAR_ICONS
) );
158 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
159 m_hasToolAlready
= FALSE
;
161 if (style
& wxTB_DOCKABLE
)
163 m_widget
= gtk_handle_box_new();
164 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
165 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
167 #if (GTK_MINOR_VERSION > 0)
168 if (style
& wxTB_FLAT
)
169 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
174 m_widget
= GTK_WIDGET(m_toolbar
);
177 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
179 #if (GTK_MINOR_VERSION > 0)
180 if (style
& wxTB_FLAT
)
181 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
188 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
194 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
196 #if (GTK_MINOR_VERSION > 0)
197 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
201 gtk_widget_get_style(
202 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
204 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
205 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
207 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
213 m_parent
->DoAddChild( this );
222 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
224 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
225 event
.SetEventObject(this);
226 event
.SetInt( toolIndex
);
227 event
.SetExtraLong((long) toggleDown
);
229 GetEventHandler()->ProcessEvent(event
);
234 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
236 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
237 event
.SetEventObject( this );
238 event
.SetInt( toolIndex
);
240 GetEventHandler()->ProcessEvent(event
);
243 void wxToolBar::OnMouseEnter( int toolIndex
)
245 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
246 event
.SetEventObject(this);
247 event
.SetInt( toolIndex
);
249 GetEventHandler()->ProcessEvent(event
);
252 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
253 const wxBitmap
& pushedBitmap
, bool toggle
,
254 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
255 const wxString
& helpString1
, const wxString
& helpString2
)
257 m_hasToolAlready
= TRUE
;
259 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
260 _T("invalid bitmap for wxToolBar icon") );
262 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
263 _T("wxToolBar doesn't support GdkBitmap") );
265 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
266 _T("wxToolBar::Add needs a wxBitmap") );
268 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
270 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
272 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
273 if ( bitmap
.GetMask() )
274 mask
= bitmap
.GetMask()->GetBitmap();
276 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
277 #if (GTK_MINOR_VERSION > 0)
278 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
281 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
283 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
285 helpString1
, helpString2
,
288 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
289 : GTK_TOOLBAR_CHILD_BUTTON
;
291 GtkWidget
*item
= gtk_toolbar_append_element
293 GTK_TOOLBAR(m_toolbar
),
297 helpString1
.mbc_str(),
300 (GtkSignalFunc
)gtk_toolbar_callback
,
306 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
307 "enter_notify_event",
308 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
311 m_tools
.Append( tool
);
316 void wxToolBar::AddSeparator()
318 gtk_toolbar_append_space( m_toolbar
);
321 void wxToolBar::ClearTools()
323 wxFAIL_MSG( _T("wxToolBar::ClearTools not implemented") );
326 bool wxToolBar::Realize()
333 wxNode
*node
= m_tools
.First();
336 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
337 if (tool
->m_bitmap1
.Ok())
339 int tool_height
= tool
->m_bitmap1
.GetHeight();
340 if (tool_height
> m_height
) m_height
= tool_height
;
346 m_height
+= 5 + 2*m_yMargin
;
351 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
353 wxNode
*node
= m_tools
.First();
356 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
357 if (tool
->m_index
== toolIndex
)
359 tool
->m_enabled
= enable
;
361 #if (GTK_MINOR_VERSION > 0)
362 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
363 greyed anyway and this also disables tooltips */
365 gtk_widget_set_sensitive( tool
->m_item
, enable
);
373 wxFAIL_MSG( _T("wrong toolbar index") );
376 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
378 wxNode
*node
= m_tools
.First();
381 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
382 if (tool
->m_index
== toolIndex
)
384 tool
->m_toggleState
= toggle
;
385 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
386 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
392 wxFAIL_MSG( _T("wrong toolbar index") );
395 wxObject
*wxToolBar::GetToolClientData( int index
) const
397 wxNode
*node
= m_tools
.First();
400 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
401 if (tool
->m_index
== index
) return tool
->m_clientData
;;
405 wxFAIL_MSG( _T("wrong toolbar index") );
407 return (wxObject
*)NULL
;
410 bool wxToolBar::GetToolState(int toolIndex
) const
412 wxNode
*node
= m_tools
.First();
415 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
416 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
420 wxFAIL_MSG( _T("wrong toolbar index") );
425 bool wxToolBar::GetToolEnabled(int toolIndex
) const
427 wxNode
*node
= m_tools
.First();
430 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
431 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
435 wxFAIL_MSG( _T("wrong toolbar index") );
440 void wxToolBar::SetMargins( int x
, int y
)
442 wxCHECK_RET( !m_hasToolAlready
, _T("wxToolBar::SetMargins must be called before adding tool.") );
444 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
450 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
452 wxFAIL_MSG( _T("wxToolBar::SetToolPacking not implemented") );
455 void wxToolBar::SetToolSeparation( int separation
)
457 gtk_toolbar_set_space_size( m_toolbar
, separation
);
458 m_separation
= separation
;
461 int wxToolBar::GetToolPacking()
466 int wxToolBar::GetToolSeparation()
471 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
473 wxNode
*node
= m_tools
.First();
476 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
477 if (tool
->m_index
== toolIndex
)
479 return tool
->m_longHelpString
;
484 wxFAIL_MSG( _T("wrong toolbar index") );
489 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
491 wxNode
*node
= m_tools
.First();
494 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
495 if (tool
->m_index
== toolIndex
)
497 return tool
->m_shortHelpString
;
502 wxFAIL_MSG( _T("wrong toolbar index") );
507 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
509 wxNode
*node
= m_tools
.First();
512 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
513 if (tool
->m_index
== toolIndex
)
515 tool
->m_longHelpString
= helpString
;
521 wxFAIL_MSG( _T("wrong toolbar index") );
526 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
528 wxNode
*node
= m_tools
.First();
531 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
532 if (tool
->m_index
== toolIndex
)
534 tool
->m_shortHelpString
= helpString
;
540 wxFAIL_MSG( _T("wrong toolbar index") );
545 void wxToolBar::OnIdle( wxIdleEvent
&WXUNUSED(ievent
) )
547 wxEvtHandler
* evtHandler
= GetEventHandler();
549 wxNode
* node
= m_tools
.First();
552 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->Data();
554 wxUpdateUIEvent
event( tool
->m_index
);
555 event
.SetEventObject(this);
557 if (evtHandler
->ProcessEvent( event
))
559 if (event
.GetSetEnabled())
560 EnableTool(tool
->m_index
, event
.GetEnabled());
561 if (event
.GetSetChecked())
562 ToggleTool(tool
->m_index
, event
.GetChecked());
564 if (event.GetSetText())