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
;
78 wxToolBar
*tb
= tool
->m_owner
;
80 /* we grey-out the tip text of disabled tool */
84 if (tb
->m_fg
->red
!= 0)
89 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
91 #if (GTK_MINOR_VERSION > 0)
95 GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
->tip_window
) );
97 g_style
->fg
[GTK_STATE_NORMAL
] = *tb
->m_fg
;
98 gtk_widget_set_style( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
->tip_window
, g_style
);
100 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
106 if (tb
->m_fg
->red
== 0)
108 tb
->m_fg
->red
= 33000;
109 tb
->m_fg
->green
= 33000;
110 tb
->m_fg
->blue
= 33000;
111 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
112 #if (GTK_MINOR_VERSION > 0)
115 gtk_widget_get_style(
116 GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
->tip_window
) );
118 g_style
->fg
[GTK_STATE_NORMAL
] = *tb
->m_fg
;
119 gtk_widget_set_style( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
->tip_window
, g_style
);
121 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
129 tb
->OnMouseEnter( tool
->m_index
);
134 //-----------------------------------------------------------------------------
136 //-----------------------------------------------------------------------------
138 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
140 BEGIN_EVENT_TABLE(wxToolBar
, wxControl
)
141 EVT_IDLE(wxToolBar::OnIdle
)
144 wxToolBar::wxToolBar()
148 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
149 const wxPoint
& pos
, const wxSize
& size
,
150 long style
, const wxString
& name
)
152 Create( parent
, id
, pos
, size
, style
, name
);
155 wxToolBar::~wxToolBar()
161 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
162 const wxPoint
& pos
, const wxSize
& size
,
163 long style
, const wxString
& name
)
167 PreCreation( parent
, id
, pos
, size
, style
, name
);
169 m_tools
.DeleteContents( TRUE
);
171 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
172 GTK_TOOLBAR_ICONS
) );
175 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
176 m_hasToolAlready
= FALSE
;
178 if (style
& wxTB_DOCKABLE
)
180 m_widget
= gtk_handle_box_new();
181 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
182 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
184 #if (GTK_MINOR_VERSION > 0)
185 if (style
& wxTB_FLAT
)
186 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
191 m_widget
= GTK_WIDGET(m_toolbar
);
194 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
196 #if (GTK_MINOR_VERSION > 0)
197 if (style
& wxTB_FLAT
)
198 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
205 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
211 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
213 #if (GTK_MINOR_VERSION > 0)
214 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
218 gtk_widget_get_style(
219 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
221 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
222 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
224 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
230 m_parent
->DoAddChild( this );
239 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
241 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
242 event
.SetEventObject(this);
243 event
.SetInt( toolIndex
);
244 event
.SetExtraLong((long) toggleDown
);
246 GetEventHandler()->ProcessEvent(event
);
251 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
253 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
254 event
.SetEventObject( this );
255 event
.SetInt( toolIndex
);
257 GetEventHandler()->ProcessEvent(event
);
260 void wxToolBar::OnMouseEnter( int toolIndex
)
262 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
263 event
.SetEventObject(this);
264 event
.SetInt( toolIndex
);
266 GetEventHandler()->ProcessEvent(event
);
269 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
270 const wxBitmap
& pushedBitmap
, bool toggle
,
271 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
272 const wxString
& helpString1
, const wxString
& helpString2
)
274 m_hasToolAlready
= TRUE
;
276 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
277 _T("invalid bitmap for wxToolBar icon") );
279 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
280 _T("wxToolBar doesn't support GdkBitmap") );
282 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
283 _T("wxToolBar::Add needs a wxBitmap") );
285 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
287 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
289 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
290 if ( bitmap
.GetMask() )
291 mask
= bitmap
.GetMask()->GetBitmap();
293 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
294 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
296 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
298 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
300 helpString1
, helpString2
,
303 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
304 : GTK_TOOLBAR_CHILD_BUTTON
;
306 GtkWidget
*item
= gtk_toolbar_append_element
308 GTK_TOOLBAR(m_toolbar
),
312 helpString1
.mbc_str(),
315 (GtkSignalFunc
)gtk_toolbar_callback
,
321 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
322 "enter_notify_event",
323 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
326 m_tools
.Append( tool
);
331 void wxToolBar::AddSeparator()
333 gtk_toolbar_append_space( m_toolbar
);
336 void wxToolBar::ClearTools()
338 wxFAIL_MSG( _T("wxToolBar::ClearTools not implemented") );
341 bool wxToolBar::Realize()
348 wxNode
*node
= m_tools
.First();
351 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
352 if (tool
->m_bitmap1
.Ok())
354 int tool_height
= tool
->m_bitmap1
.GetHeight();
355 if (tool_height
> m_height
) m_height
= tool_height
;
361 m_height
+= 5 + 2*m_yMargin
;
366 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
368 wxNode
*node
= m_tools
.First();
371 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
372 if (tool
->m_index
== toolIndex
)
374 tool
->m_enabled
= enable
;
376 /* we don't disable the tools for now as the bitmaps don't get
377 greyed anyway and this also disables tooltips */
380 gtk_widget_set_sensitive( tool
->m_item
, enable
);
387 wxFAIL_MSG( _T("wrong toolbar index") );
390 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
392 wxNode
*node
= m_tools
.First();
395 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
396 if (tool
->m_index
== toolIndex
)
398 tool
->m_toggleState
= toggle
;
399 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
400 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
406 wxFAIL_MSG( _T("wrong toolbar index") );
409 wxObject
*wxToolBar::GetToolClientData( int index
) const
411 wxNode
*node
= m_tools
.First();
414 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
415 if (tool
->m_index
== index
) return tool
->m_clientData
;;
419 wxFAIL_MSG( _T("wrong toolbar index") );
421 return (wxObject
*)NULL
;
424 bool wxToolBar::GetToolState(int toolIndex
) const
426 wxNode
*node
= m_tools
.First();
429 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
430 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
434 wxFAIL_MSG( _T("wrong toolbar index") );
439 bool wxToolBar::GetToolEnabled(int toolIndex
) const
441 wxNode
*node
= m_tools
.First();
444 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
445 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
449 wxFAIL_MSG( _T("wrong toolbar index") );
454 void wxToolBar::SetMargins( int x
, int y
)
456 wxCHECK_RET( !m_hasToolAlready
, _T("wxToolBar::SetMargins must be called before adding tool.") );
458 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
464 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
466 wxFAIL_MSG( _T("wxToolBar::SetToolPacking not implemented") );
469 void wxToolBar::SetToolSeparation( int separation
)
471 gtk_toolbar_set_space_size( m_toolbar
, separation
);
472 m_separation
= separation
;
475 int wxToolBar::GetToolPacking()
480 int wxToolBar::GetToolSeparation()
485 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
487 wxNode
*node
= m_tools
.First();
490 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
491 if (tool
->m_index
== toolIndex
)
493 return tool
->m_longHelpString
;
498 wxFAIL_MSG( _T("wrong toolbar index") );
503 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
505 wxNode
*node
= m_tools
.First();
508 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
509 if (tool
->m_index
== toolIndex
)
511 return tool
->m_shortHelpString
;
516 wxFAIL_MSG( _T("wrong toolbar index") );
521 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
523 wxNode
*node
= m_tools
.First();
526 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
527 if (tool
->m_index
== toolIndex
)
529 tool
->m_longHelpString
= helpString
;
535 wxFAIL_MSG( _T("wrong toolbar index") );
540 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
542 wxNode
*node
= m_tools
.First();
545 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
546 if (tool
->m_index
== toolIndex
)
548 tool
->m_shortHelpString
= helpString
;
554 wxFAIL_MSG( _T("wrong toolbar index") );
559 void wxToolBar::OnIdle( wxIdleEvent
&WXUNUSED(ievent
) )
561 wxEvtHandler
* evtHandler
= GetEventHandler();
563 wxNode
* node
= m_tools
.First();
566 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->Data();
568 wxUpdateUIEvent
event( tool
->m_index
);
569 event
.SetEventObject(this);
571 if (evtHandler
->ProcessEvent( event
))
573 if (event
.GetSetEnabled())
574 EnableTool(tool
->m_index
, event
.GetEnabled());
575 if (event
.GetSetChecked())
576 ToggleTool(tool
->m_index
, event
.GetChecked());
578 if (event.GetSetText())