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"
16 //-----------------------------------------------------------------------------
18 //-----------------------------------------------------------------------------
20 extern bool g_blockEventsOnDrag
;
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool
,wxObject
)
28 wxToolBarTool::wxToolBarTool( wxToolBar
*owner
, int theIndex
,
29 const wxBitmap
& bitmap1
, const wxBitmap
& bitmap2
,
32 const wxString
& shortHelpString
,
33 const wxString
& longHelpString
,
42 m_toggleState
= FALSE
;
43 m_shortHelpString
= shortHelpString
;
44 m_longHelpString
= longHelpString
;
45 m_isMenuCommand
= TRUE
;
46 m_clientData
= clientData
;
47 m_deleteSecondBitmap
= FALSE
;
51 wxToolBarTool::~wxToolBarTool()
55 //-----------------------------------------------------------------------------
56 // "clicked" (internal from gtk_toolbar)
57 //-----------------------------------------------------------------------------
59 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
61 if (g_blockEventsOnDrag
) return;
62 if (!tool
->m_enabled
) return;
64 if (tool
->m_isToggle
) tool
->m_toggleState
= !tool
->m_toggleState
;
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_blockEventsOnDrag
) return TRUE
;
78 tool
->m_owner
->OnMouseEnter( tool
->m_index
);
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
87 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
89 wxToolBar::wxToolBar()
93 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
94 const wxPoint
& pos
, const wxSize
& size
,
95 long style
, const wxString
& name
)
97 Create( parent
, id
, pos
, size
, style
, name
);
100 wxToolBar::~wxToolBar()
104 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
105 const wxPoint
& pos
, const wxSize
& size
,
106 long style
, const wxString
& name
)
110 PreCreation( parent
, id
, pos
, size
, style
, name
);
112 m_tools
.DeleteContents( TRUE
);
114 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
115 GTK_TOOLBAR_ICONS
) );
118 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
119 m_hasToolAlready
= FALSE
;
121 m_widget
= GTK_WIDGET(m_toolbar
);
123 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
128 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), &m_fg
);
133 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), &m_bg
);
135 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, &m_bg
, &m_fg
);
140 m_parent
->AddChild( this );
142 (m_parent
->m_insertCallback
)( m_parent
, this );
151 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
153 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
154 event
.SetEventObject(this);
155 event
.SetInt( toolIndex
);
156 event
.SetExtraLong((long) toggleDown
);
158 GetEventHandler()->ProcessEvent(event
);
163 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
165 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
166 event
.SetEventObject( this );
167 event
.SetInt( toolIndex
);
169 GetEventHandler()->ProcessEvent(event
);
172 void wxToolBar::OnMouseEnter( int toolIndex
)
174 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
175 event
.SetEventObject(this);
176 event
.SetInt( toolIndex
);
178 GetEventHandler()->ProcessEvent(event
);
181 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
182 const wxBitmap
& pushedBitmap
, bool toggle
,
183 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
184 const wxString
& helpString1
, const wxString
& helpString2
)
186 m_hasToolAlready
= TRUE
;
188 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
189 "invalid bitmap for wxToolBar icon" );
191 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
193 helpString1
, helpString2
);
195 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
196 "wxToolBar doesn't support GdkBitmap" );
198 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
199 "wxToolBar::Add needs a wxBitmap" );
201 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
203 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
205 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
206 if ( bitmap
.GetMask() )
207 mask
= bitmap
.GetMask()->GetBitmap();
209 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
211 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
213 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
214 : GTK_TOOLBAR_CHILD_BUTTON
;
216 GtkWidget
*item
= gtk_toolbar_append_element
218 GTK_TOOLBAR(m_toolbar
),
225 (GtkSignalFunc
)gtk_toolbar_callback
,
231 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
232 "enter_notify_event",
233 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
236 m_tools
.Append( tool
);
241 void wxToolBar::AddSeparator()
243 gtk_toolbar_append_space( m_toolbar
);
246 void wxToolBar::ClearTools()
248 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
251 bool wxToolBar::Realize()
258 wxNode
*node
= m_tools
.First();
261 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
262 if (tool
->m_bitmap1
.Ok())
264 int tool_height
= tool
->m_bitmap1
.GetHeight();
265 if (tool_height
> m_height
) m_height
= tool_height
;
271 m_height
+= 5 + 2*m_yMargin
;
276 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
278 wxNode
*node
= m_tools
.First();
281 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
282 if (tool
->m_index
== toolIndex
)
284 tool
->m_enabled
= enable
;
290 wxFAIL_MSG( "wrong toolbar index" );
293 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
295 wxNode
*node
= m_tools
.First();
298 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
299 if (tool
->m_index
== toolIndex
)
301 tool
->m_toggleState
= toggle
;
302 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
303 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
309 wxFAIL_MSG( "wrong toolbar index" );
312 wxObject
*wxToolBar::GetToolClientData( int index
) const
314 wxNode
*node
= m_tools
.First();
317 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
318 if (tool
->m_index
== index
) return tool
->m_clientData
;;
322 wxFAIL_MSG( "wrong toolbar index" );
324 return (wxObject
*)NULL
;
327 bool wxToolBar::GetToolState(int toolIndex
) const
329 wxNode
*node
= m_tools
.First();
332 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
333 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
337 wxFAIL_MSG( "wrong toolbar index" );
342 bool wxToolBar::GetToolEnabled(int toolIndex
) const
344 wxNode
*node
= m_tools
.First();
347 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
348 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
352 wxFAIL_MSG( "wrong toolbar index" );
357 void wxToolBar::SetMargins( int x
, int y
)
359 wxCHECK_RET( !m_hasToolAlready
, "wxToolBar::SetMargins must be called before adding tool." );
361 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
367 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
369 wxFAIL_MSG( "wxToolBar::SetToolPacking not implemented" );
372 void wxToolBar::SetToolSeparation( int separation
)
374 gtk_toolbar_set_space_size( m_toolbar
, separation
);
375 m_separation
= separation
;
378 int wxToolBar::GetToolPacking()
383 int wxToolBar::GetToolSeparation()
388 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
390 wxNode
*node
= m_tools
.First();
393 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
394 if (tool
->m_index
== toolIndex
)
396 return tool
->m_longHelpString
;
401 wxFAIL_MSG( "wrong toolbar index" );
406 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
408 wxNode
*node
= m_tools
.First();
411 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
412 if (tool
->m_index
== toolIndex
)
414 return tool
->m_shortHelpString
;
419 wxFAIL_MSG( "wrong toolbar index" );
424 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
426 wxNode
*node
= m_tools
.First();
429 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
430 if (tool
->m_index
== toolIndex
)
432 tool
->m_longHelpString
= helpString
;
438 wxFAIL_MSG( "wrong toolbar index" );
443 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
445 wxNode
*node
= m_tools
.First();
448 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
449 if (tool
->m_index
== toolIndex
)
451 tool
->m_shortHelpString
= helpString
;
457 wxFAIL_MSG( "wrong toolbar index" );