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"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 extern bool g_blockEventsOnDrag
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool
,wxObject
)
32 wxToolBarTool::wxToolBarTool( wxToolBar
*owner
, int theIndex
,
33 const wxBitmap
& bitmap1
, const wxBitmap
& bitmap2
,
36 const wxString
& shortHelpString
,
37 const wxString
& longHelpString
,
46 m_toggleState
= FALSE
;
47 m_shortHelpString
= shortHelpString
;
48 m_longHelpString
= longHelpString
;
49 m_isMenuCommand
= TRUE
;
50 m_clientData
= clientData
;
51 m_deleteSecondBitmap
= FALSE
;
55 wxToolBarTool::~wxToolBarTool()
59 //-----------------------------------------------------------------------------
60 // "clicked" (internal from gtk_toolbar)
61 //-----------------------------------------------------------------------------
63 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
65 if (g_blockEventsOnDrag
) return;
66 if (!tool
->m_enabled
) return;
68 if (tool
->m_isToggle
) tool
->m_toggleState
= !tool
->m_toggleState
;
70 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
73 //-----------------------------------------------------------------------------
74 // "enter_notify_event"
75 //-----------------------------------------------------------------------------
77 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
78 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
80 if (g_blockEventsOnDrag
) return TRUE
;
82 tool
->m_owner
->OnMouseEnter( tool
->m_index
);
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
91 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
93 wxToolBar::wxToolBar()
97 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
98 const wxPoint
& pos
, const wxSize
& size
,
99 long style
, const wxString
& name
)
101 Create( parent
, id
, pos
, size
, style
, name
);
104 wxToolBar::~wxToolBar()
110 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
111 const wxPoint
& pos
, const wxSize
& size
,
112 long style
, const wxString
& name
)
116 PreCreation( parent
, id
, pos
, size
, style
, name
);
118 m_tools
.DeleteContents( TRUE
);
120 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
121 GTK_TOOLBAR_ICONS
) );
124 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
125 m_hasToolAlready
= FALSE
;
127 m_widget
= GTK_WIDGET(m_toolbar
);
129 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
135 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
141 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
143 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
148 m_parent
->AddChild( this );
150 (m_parent
->m_insertCallback
)( m_parent
, this );
159 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
161 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
162 event
.SetEventObject(this);
163 event
.SetInt( toolIndex
);
164 event
.SetExtraLong((long) toggleDown
);
166 GetEventHandler()->ProcessEvent(event
);
171 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
173 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
174 event
.SetEventObject( this );
175 event
.SetInt( toolIndex
);
177 GetEventHandler()->ProcessEvent(event
);
180 void wxToolBar::OnMouseEnter( int toolIndex
)
182 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
183 event
.SetEventObject(this);
184 event
.SetInt( toolIndex
);
186 GetEventHandler()->ProcessEvent(event
);
189 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
190 const wxBitmap
& pushedBitmap
, bool toggle
,
191 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
192 const wxString
& helpString1
, const wxString
& helpString2
)
194 m_hasToolAlready
= TRUE
;
196 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
197 "invalid bitmap for wxToolBar icon" );
199 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
200 "wxToolBar doesn't support GdkBitmap" );
202 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
203 "wxToolBar::Add needs a wxBitmap" );
205 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
207 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
209 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
210 if ( bitmap
.GetMask() )
211 mask
= bitmap
.GetMask()->GetBitmap();
213 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
215 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
217 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
219 helpString1
, helpString2
,
222 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
223 : GTK_TOOLBAR_CHILD_BUTTON
;
225 GtkWidget
*item
= gtk_toolbar_append_element
227 GTK_TOOLBAR(m_toolbar
),
234 (GtkSignalFunc
)gtk_toolbar_callback
,
240 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
241 "enter_notify_event",
242 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
245 m_tools
.Append( tool
);
250 void wxToolBar::AddSeparator()
252 gtk_toolbar_append_space( m_toolbar
);
255 void wxToolBar::ClearTools()
257 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
260 bool wxToolBar::Realize()
267 wxNode
*node
= m_tools
.First();
270 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
271 if (tool
->m_bitmap1
.Ok())
273 int tool_height
= tool
->m_bitmap1
.GetHeight();
274 if (tool_height
> m_height
) m_height
= tool_height
;
280 m_height
+= 5 + 2*m_yMargin
;
285 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
287 wxNode
*node
= m_tools
.First();
290 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
291 if (tool
->m_index
== toolIndex
)
293 tool
->m_enabled
= enable
;
296 gtk_widget_set_sensitive( tool
->m_item
, enable
);
303 wxFAIL_MSG( "wrong toolbar index" );
306 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
308 wxNode
*node
= m_tools
.First();
311 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
312 if (tool
->m_index
== toolIndex
)
314 tool
->m_toggleState
= toggle
;
315 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
316 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
322 wxFAIL_MSG( "wrong toolbar index" );
325 wxObject
*wxToolBar::GetToolClientData( int index
) const
327 wxNode
*node
= m_tools
.First();
330 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
331 if (tool
->m_index
== index
) return tool
->m_clientData
;;
335 wxFAIL_MSG( "wrong toolbar index" );
337 return (wxObject
*)NULL
;
340 bool wxToolBar::GetToolState(int toolIndex
) const
342 wxNode
*node
= m_tools
.First();
345 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
346 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
350 wxFAIL_MSG( "wrong toolbar index" );
355 bool wxToolBar::GetToolEnabled(int toolIndex
) const
357 wxNode
*node
= m_tools
.First();
360 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
361 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
365 wxFAIL_MSG( "wrong toolbar index" );
370 void wxToolBar::SetMargins( int x
, int y
)
372 wxCHECK_RET( !m_hasToolAlready
, "wxToolBar::SetMargins must be called before adding tool." );
374 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
380 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
382 wxFAIL_MSG( "wxToolBar::SetToolPacking not implemented" );
385 void wxToolBar::SetToolSeparation( int separation
)
387 gtk_toolbar_set_space_size( m_toolbar
, separation
);
388 m_separation
= separation
;
391 int wxToolBar::GetToolPacking()
396 int wxToolBar::GetToolSeparation()
401 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
403 wxNode
*node
= m_tools
.First();
406 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
407 if (tool
->m_index
== toolIndex
)
409 return tool
->m_longHelpString
;
414 wxFAIL_MSG( "wrong toolbar index" );
419 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
421 wxNode
*node
= m_tools
.First();
424 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
425 if (tool
->m_index
== toolIndex
)
427 return tool
->m_shortHelpString
;
432 wxFAIL_MSG( "wrong toolbar index" );
437 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
439 wxNode
*node
= m_tools
.First();
442 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
443 if (tool
->m_index
== toolIndex
)
445 tool
->m_longHelpString
= helpString
;
451 wxFAIL_MSG( "wrong toolbar index" );
456 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
458 wxNode
*node
= m_tools
.First();
461 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
462 if (tool
->m_index
== toolIndex
)
464 tool
->m_shortHelpString
= helpString
;
470 wxFAIL_MSG( "wrong toolbar index" );