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 bool g_blockEventsOnDrag
;
27 //-----------------------------------------------------------------------------
28 // "clicked" (internal from gtk_toolbar)
29 //-----------------------------------------------------------------------------
31 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
33 if (g_blockEventsOnDrag
) return;
34 if (!tool
->m_enabled
) return;
36 if (tool
->m_isToggle
) tool
->m_toggleState
= !tool
->m_toggleState
;
38 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
41 //-----------------------------------------------------------------------------
42 // "enter_notify_event"
43 //-----------------------------------------------------------------------------
45 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
46 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
48 if (g_blockEventsOnDrag
) return TRUE
;
50 /* we grey-out the tip text of disabled tool */
52 wxToolBar
*tb
= tool
->m_owner
;
56 if (tb
->m_fg
->red
!= 0)
61 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
62 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
67 if (tb
->m_fg
->red
== 0)
69 tb
->m_fg
->red
= 33000;
70 tb
->m_fg
->green
= 33000;
71 tb
->m_fg
->blue
= 33000;
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
);
79 tb
->OnMouseEnter( tool
->m_index
);
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
90 wxToolBar::wxToolBar()
94 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
95 const wxPoint
& pos
, const wxSize
& size
,
96 long style
, const wxString
& name
)
98 Create( parent
, id
, pos
, size
, style
, name
);
101 wxToolBar::~wxToolBar()
107 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
108 const wxPoint
& pos
, const wxSize
& size
,
109 long style
, const wxString
& name
)
113 PreCreation( parent
, id
, pos
, size
, style
, name
);
115 m_tools
.DeleteContents( TRUE
);
117 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
118 GTK_TOOLBAR_ICONS
) );
121 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
122 m_hasToolAlready
= FALSE
;
124 if (style
& wxTB_DOCKABLE
)
126 m_widget
= gtk_handle_box_new();
127 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
128 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
132 m_widget
= GTK_WIDGET(m_toolbar
);
135 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
141 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
147 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
149 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
154 m_parent
->AddChild( this );
156 (m_parent
->m_insertCallback
)( m_parent
, this );
165 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
167 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
168 event
.SetEventObject(this);
169 event
.SetInt( toolIndex
);
170 event
.SetExtraLong((long) toggleDown
);
172 GetEventHandler()->ProcessEvent(event
);
177 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
179 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
180 event
.SetEventObject( this );
181 event
.SetInt( toolIndex
);
183 GetEventHandler()->ProcessEvent(event
);
186 void wxToolBar::OnMouseEnter( int toolIndex
)
188 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
189 event
.SetEventObject(this);
190 event
.SetInt( toolIndex
);
192 GetEventHandler()->ProcessEvent(event
);
195 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
196 const wxBitmap
& pushedBitmap
, bool toggle
,
197 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
198 const wxString
& helpString1
, const wxString
& helpString2
)
200 m_hasToolAlready
= TRUE
;
202 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
203 "invalid bitmap for wxToolBar icon" );
205 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
206 "wxToolBar doesn't support GdkBitmap" );
208 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
209 "wxToolBar::Add needs a wxBitmap" );
211 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
213 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
215 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
216 if ( bitmap
.GetMask() )
217 mask
= bitmap
.GetMask()->GetBitmap();
219 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
221 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
223 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
225 helpString1
, helpString2
,
228 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
229 : GTK_TOOLBAR_CHILD_BUTTON
;
231 GtkWidget
*item
= gtk_toolbar_append_element
233 GTK_TOOLBAR(m_toolbar
),
240 (GtkSignalFunc
)gtk_toolbar_callback
,
246 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
247 "enter_notify_event",
248 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
251 m_tools
.Append( tool
);
256 void wxToolBar::AddSeparator()
258 gtk_toolbar_append_space( m_toolbar
);
261 void wxToolBar::ClearTools()
263 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
266 bool wxToolBar::Realize()
273 wxNode
*node
= m_tools
.First();
276 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
277 if (tool
->m_bitmap1
.Ok())
279 int tool_height
= tool
->m_bitmap1
.GetHeight();
280 if (tool_height
> m_height
) m_height
= tool_height
;
286 m_height
+= 5 + 2*m_yMargin
;
291 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
293 wxNode
*node
= m_tools
.First();
296 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
297 if (tool
->m_index
== toolIndex
)
299 tool
->m_enabled
= enable
;
301 /* we don't disable the tools for now as the bitmaps don't get
302 greyed anyway and this also disables tooltips
305 gtk_widget_set_sensitive( tool->m_item, enable );
313 wxFAIL_MSG( "wrong toolbar index" );
316 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
318 wxNode
*node
= m_tools
.First();
321 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
322 if (tool
->m_index
== toolIndex
)
324 tool
->m_toggleState
= toggle
;
325 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
326 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
332 wxFAIL_MSG( "wrong toolbar index" );
335 wxObject
*wxToolBar::GetToolClientData( int index
) const
337 wxNode
*node
= m_tools
.First();
340 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
341 if (tool
->m_index
== index
) return tool
->m_clientData
;;
345 wxFAIL_MSG( "wrong toolbar index" );
347 return (wxObject
*)NULL
;
350 bool wxToolBar::GetToolState(int toolIndex
) const
352 wxNode
*node
= m_tools
.First();
355 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
356 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
360 wxFAIL_MSG( "wrong toolbar index" );
365 bool wxToolBar::GetToolEnabled(int toolIndex
) const
367 wxNode
*node
= m_tools
.First();
370 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
371 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
375 wxFAIL_MSG( "wrong toolbar index" );
380 void wxToolBar::SetMargins( int x
, int y
)
382 wxCHECK_RET( !m_hasToolAlready
, "wxToolBar::SetMargins must be called before adding tool." );
384 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
390 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
392 wxFAIL_MSG( "wxToolBar::SetToolPacking not implemented" );
395 void wxToolBar::SetToolSeparation( int separation
)
397 gtk_toolbar_set_space_size( m_toolbar
, separation
);
398 m_separation
= separation
;
401 int wxToolBar::GetToolPacking()
406 int wxToolBar::GetToolSeparation()
411 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
413 wxNode
*node
= m_tools
.First();
416 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
417 if (tool
->m_index
== toolIndex
)
419 return tool
->m_longHelpString
;
424 wxFAIL_MSG( "wrong toolbar index" );
429 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
431 wxNode
*node
= m_tools
.First();
434 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
435 if (tool
->m_index
== toolIndex
)
437 return tool
->m_shortHelpString
;
442 wxFAIL_MSG( "wrong toolbar index" );
447 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
449 wxNode
*node
= m_tools
.First();
452 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
453 if (tool
->m_index
== toolIndex
)
455 tool
->m_longHelpString
= helpString
;
461 wxFAIL_MSG( "wrong toolbar index" );
466 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
468 wxNode
*node
= m_tools
.First();
471 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
472 if (tool
->m_index
== toolIndex
)
474 tool
->m_shortHelpString
= helpString
;
480 wxFAIL_MSG( "wrong toolbar index" );