1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: GTK toolbar
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "tbargtk.h"
16 #include "wx/toolbar.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 extern bool g_blockEventsOnDrag
;
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool
,wxObject
)
30 wxToolBarTool::wxToolBarTool( wxToolBar
*owner
, int theIndex
,
31 const wxBitmap
& bitmap1
, const wxBitmap
& bitmap2
,
34 const wxString
& shortHelpString
,
35 const wxString
& longHelpString
,
44 m_toggleState
= FALSE
;
45 m_shortHelpString
= shortHelpString
;
46 m_longHelpString
= longHelpString
;
47 m_isMenuCommand
= TRUE
;
48 m_clientData
= clientData
;
49 m_deleteSecondBitmap
= FALSE
;
53 wxToolBarTool::~wxToolBarTool()
57 //-----------------------------------------------------------------------------
58 // "clicked" (internal from gtk_toolbar)
59 //-----------------------------------------------------------------------------
61 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
63 if (g_blockEventsOnDrag
) return;
64 if (!tool
->m_enabled
) return;
66 if (tool
->m_isToggle
) tool
->m_toggleState
= !tool
->m_toggleState
;
68 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
71 //-----------------------------------------------------------------------------
72 // "enter_notify_event"
73 //-----------------------------------------------------------------------------
75 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
76 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
78 if (g_blockEventsOnDrag
) return TRUE
;
80 tool
->m_owner
->OnMouseEnter( tool
->m_index
);
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
89 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
91 wxToolBar::wxToolBar()
95 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
96 const wxPoint
& pos
, const wxSize
& size
,
97 long style
, const wxString
& name
)
99 Create( parent
, id
, pos
, size
, style
, name
);
102 wxToolBar::~wxToolBar()
106 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
107 const wxPoint
& pos
, const wxSize
& size
,
108 long style
, const wxString
& name
)
112 PreCreation( parent
, id
, pos
, size
, style
, name
);
114 m_tools
.DeleteContents( TRUE
);
116 m_widget
= gtk_handle_box_new();
118 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
119 GTK_TOOLBAR_ICONS
) );
120 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
122 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
124 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
133 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
135 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
136 event
.SetEventObject(this);
137 event
.SetInt( toolIndex
);
138 event
.SetExtraLong((long) toggleDown
);
140 GetEventHandler()->ProcessEvent(event
);
145 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
147 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
148 event
.SetEventObject( this );
149 event
.SetInt( toolIndex
);
151 GetEventHandler()->ProcessEvent(event
);
154 void wxToolBar::OnMouseEnter( int toolIndex
)
156 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
157 event
.SetEventObject(this);
158 event
.SetInt( toolIndex
);
160 GetEventHandler()->ProcessEvent(event
);
163 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
164 const wxBitmap
& pushedBitmap
, bool toggle
,
165 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
166 const wxString
& helpString1
, const wxString
& helpString2
)
168 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
169 "invalid bitmap for wxToolBar icon" );
171 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
173 helpString1
, helpString2
);
175 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
176 "wxToolBar doesn't support GdkBitmap" );
178 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
179 "wxToolBar::Add needs a wxBitmap" );
181 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
183 if (TRUE
) // FIXME huh?
185 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
187 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
188 if ( bitmap
.GetMask() )
189 mask
= bitmap
.GetMask()->GetBitmap();
191 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
194 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
196 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
197 : GTK_TOOLBAR_CHILD_BUTTON
;
199 GtkWidget
*item
= gtk_toolbar_append_element
201 GTK_TOOLBAR(m_toolbar
),
208 (GtkSignalFunc
)gtk_toolbar_callback
,
212 // VZ: we don't want GDK_NO_EXPOSE events because for some reason our
213 // toolbar buttons get them (it doesn't happen in a standalone GTK+ program
214 // for unknown reasons) and it prevents tooltips from appearing.
215 gtk_widget_set_events( GTK_WIDGET(item
),
216 gtk_widget_get_events( GTK_WIDGET(item
) ) &
220 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
221 "enter_notify_event",
222 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
225 m_tools
.Append( tool
);
230 void wxToolBar::AddSeparator()
232 gtk_toolbar_append_space( m_toolbar
);
235 void wxToolBar::ClearTools()
237 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
240 void wxToolBar::Realize()
247 wxNode
*node
= m_tools
.First();
250 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
251 if (tool
->m_bitmap1
.Ok())
253 int tool_height
= tool
->m_bitmap1
.GetHeight();
254 if (tool_height
> m_height
) m_height
= tool_height
;
263 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
265 wxNode
*node
= m_tools
.First();
268 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
269 if (tool
->m_index
== toolIndex
)
271 tool
->m_enabled
= enable
;
277 wxFAIL_MSG( "wrong toolbar index" );
280 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
282 wxNode
*node
= m_tools
.First();
285 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
286 if (tool
->m_index
== toolIndex
)
288 tool
->m_toggleState
= toggle
;
289 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
290 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
296 wxFAIL_MSG( "wrong toolbar index" );
299 wxObject
*wxToolBar::GetToolClientData( int index
) const
301 wxNode
*node
= m_tools
.First();
304 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
305 if (tool
->m_index
== index
) return tool
->m_clientData
;;
309 wxFAIL_MSG( "wrong toolbar index" );
311 return (wxObject
*)NULL
;
314 bool wxToolBar::GetToolState(int toolIndex
) const
316 wxNode
*node
= m_tools
.First();
319 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
320 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
324 wxFAIL_MSG( "wrong toolbar index" );
329 bool wxToolBar::GetToolEnabled(int toolIndex
) const
331 wxNode
*node
= m_tools
.First();
334 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
335 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
339 wxFAIL_MSG( "wrong toolbar index" );
344 void wxToolBar::SetMargins( int WXUNUSED(x
), int WXUNUSED(y
) )
346 wxFAIL_MSG( "wxToolBar::SetMargins not implemented" );
349 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
351 wxFAIL_MSG( "wxToolBar::SetToolPacking not implemented" );
354 void wxToolBar::SetToolSeparation( int separation
)
356 gtk_toolbar_set_space_size( m_toolbar
, separation
);