]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/tbargtk.cpp
CalcBoundingBox() added so that Max() works in wxDC
[wxWidgets.git] / src / gtk / tbargtk.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: tbargtk.cpp
3// Purpose: GTK toolbar
4// Author: Robert Roebling
32e9da8b 5// RCS-ID: $Id$
c801d85f 6// Copyright: (c) Robert Roebling
a3622daa 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "tbargtk.h"
12#endif
13
14#include "wx/toolbar.h"
15
314055fa
RR
16//-----------------------------------------------------------------------------
17// data
18//-----------------------------------------------------------------------------
19
20extern bool g_blockEventsOnDrag;
21
c801d85f
KB
22//-----------------------------------------------------------------------------
23// wxToolBarTool
24//-----------------------------------------------------------------------------
25
26IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool,wxObject)
a3622daa
VZ
27
28wxToolBarTool::wxToolBarTool( wxToolBar *owner, int theIndex,
03f38c58
VZ
29 const wxBitmap& bitmap1, const wxBitmap& bitmap2,
30 bool toggle,
31 wxObject *clientData,
32 const wxString& shortHelpString,
33 const wxString& longHelpString,
34 GtkWidget *item )
c801d85f
KB
35{
36 m_owner = owner;
37 m_index = theIndex;
38 m_bitmap1 = bitmap1;
39 m_bitmap2 = bitmap2;
40 m_isToggle = toggle;
41 m_enabled = TRUE;
42 m_toggleState = FALSE;
43 m_shortHelpString = shortHelpString;
44 m_longHelpString = longHelpString;
45 m_isMenuCommand = TRUE;
46 m_clientData = clientData;
47 m_deleteSecondBitmap = FALSE;
fc008f25
RR
48 m_item = item;
49}
c801d85f 50
a3622daa 51wxToolBarTool::~wxToolBarTool()
c801d85f 52{
fc008f25 53}
c801d85f
KB
54
55//-----------------------------------------------------------------------------
2f2aa628 56// "clicked" (internal from gtk_toolbar)
c801d85f
KB
57//-----------------------------------------------------------------------------
58
59static void gtk_toolbar_callback( GtkWidget *WXUNUSED(widget), wxToolBarTool *tool )
60{
314055fa 61 if (g_blockEventsOnDrag) return;
c801d85f 62 if (!tool->m_enabled) return;
a3622daa 63
c801d85f 64 if (tool->m_isToggle) tool->m_toggleState = !tool->m_toggleState;
a3622daa 65
c801d85f 66 tool->m_owner->OnLeftClick( tool->m_index, tool->m_toggleState );
fc008f25 67}
c801d85f 68
2f2aa628
RR
69//-----------------------------------------------------------------------------
70// "enter_notify_event"
71//-----------------------------------------------------------------------------
72
314055fa
RR
73static gint gtk_toolbar_enter_callback( GtkWidget *WXUNUSED(widget),
74 GdkEventCrossing *WXUNUSED(gdk_event), wxToolBarTool *tool )
75{
76 if (g_blockEventsOnDrag) return TRUE;
77
78 tool->m_owner->OnMouseEnter( tool->m_index );
79
83058c58 80 return FALSE;
314055fa
RR
81}
82
2f2aa628
RR
83//-----------------------------------------------------------------------------
84// wxToolBar
c801d85f
KB
85//-----------------------------------------------------------------------------
86
716b7364 87IMPLEMENT_DYNAMIC_CLASS(wxToolBar,wxControl)
c801d85f 88
a3622daa 89wxToolBar::wxToolBar()
c801d85f 90{
fc008f25 91}
c801d85f 92
a3622daa 93wxToolBar::wxToolBar( wxWindow *parent, wxWindowID id,
c801d85f 94 const wxPoint& pos, const wxSize& size,
debe6624 95 long style, const wxString& name )
c801d85f
KB
96{
97 Create( parent, id, pos, size, style, name );
fc008f25 98}
c801d85f 99
a3622daa 100wxToolBar::~wxToolBar()
c801d85f 101{
fc008f25 102}
c801d85f 103
a3622daa 104bool wxToolBar::Create( wxWindow *parent, wxWindowID id,
c801d85f 105 const wxPoint& pos, const wxSize& size,
debe6624 106 long style, const wxString& name )
c801d85f
KB
107{
108 m_needParent = TRUE;
a3622daa 109
c801d85f
KB
110 PreCreation( parent, id, pos, size, style, name );
111
112 m_tools.DeleteContents( TRUE );
a3622daa 113
68dda785
VZ
114 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL,
115 GTK_TOOLBAR_ICONS ) );
a3622daa 116
32e9da8b
RR
117 m_widget = GTK_WIDGET(m_toolbar);
118
119 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
a3622daa 120
6ca41e57
RR
121 m_parent->AddChild( this );
122
123 (m_parent->m_insertCallback)( m_parent, this );
124
c801d85f 125 PostCreation();
a3622daa 126
c801d85f 127 Show( TRUE );
a3622daa 128
c801d85f 129 return TRUE;
fc008f25 130}
c801d85f 131
716b7364 132bool wxToolBar::OnLeftClick( int toolIndex, bool toggleDown )
c801d85f 133{
cf4219e7 134 wxCommandEvent event( wxEVT_COMMAND_TOOL_CLICKED, toolIndex );
c801d85f 135 event.SetEventObject(this);
46dc76ba 136 event.SetInt( toolIndex );
c801d85f
KB
137 event.SetExtraLong((long) toggleDown);
138
139 GetEventHandler()->ProcessEvent(event);
140
141 return TRUE;
fc008f25 142}
c801d85f 143
716b7364 144void wxToolBar::OnRightClick( int toolIndex, float WXUNUSED(x), float WXUNUSED(y) )
c801d85f 145{
cf4219e7 146 wxCommandEvent event( wxEVT_COMMAND_TOOL_RCLICKED, toolIndex );
46dc76ba
RR
147 event.SetEventObject( this );
148 event.SetInt( toolIndex );
c801d85f
KB
149
150 GetEventHandler()->ProcessEvent(event);
fc008f25 151}
c801d85f 152
716b7364 153void wxToolBar::OnMouseEnter( int toolIndex )
c801d85f 154{
314055fa 155 wxCommandEvent event( wxEVT_COMMAND_TOOL_ENTER, GetId() );
c801d85f 156 event.SetEventObject(this);
46dc76ba 157 event.SetInt( toolIndex );
314055fa 158
c801d85f 159 GetEventHandler()->ProcessEvent(event);
fc008f25 160}
c801d85f 161
a3622daa 162wxToolBarTool *wxToolBar::AddTool( int toolIndex, const wxBitmap& bitmap,
debe6624
JS
163 const wxBitmap& pushedBitmap, bool toggle,
164 float WXUNUSED(xPos), float WXUNUSED(yPos), wxObject *clientData,
c801d85f
KB
165 const wxString& helpString1, const wxString& helpString2 )
166{
03f38c58
VZ
167 wxCHECK_MSG( bitmap.Ok(), (wxToolBarTool *)NULL,
168 "invalid bitmap for wxToolBar icon" );
a3622daa 169
03f38c58
VZ
170 wxToolBarTool *tool = new wxToolBarTool( this, toolIndex, bitmap, pushedBitmap,
171 toggle, clientData,
172 helpString1, helpString2 );
a3622daa 173
03f38c58
VZ
174 wxCHECK_MSG( bitmap.GetBitmap() == NULL, (wxToolBarTool *)NULL,
175 "wxToolBar doesn't support GdkBitmap" );
176
177 wxCHECK_MSG( bitmap.GetPixmap() != NULL, (wxToolBarTool *)NULL,
178 "wxToolBar::Add needs a wxBitmap" );
903f689b 179
68dda785 180 GtkWidget *tool_pixmap = (GtkWidget *)NULL;
903f689b 181
03f38c58 182 if (TRUE) // FIXME huh?
903f689b
RR
183 {
184 GdkPixmap *pixmap = bitmap.GetPixmap();
a3622daa 185
68dda785
VZ
186 GdkBitmap *mask = (GdkBitmap *)NULL;
187 if ( bitmap.GetMask() )
188 mask = bitmap.GetMask()->GetBitmap();
903f689b
RR
189
190 tool_pixmap = gtk_pixmap_new( pixmap, mask );
191 }
192
c801d85f 193 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
a3622daa 194
03f38c58
VZ
195 GtkToolbarChildType ctype = toggle ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
196 : GTK_TOOLBAR_CHILD_BUTTON;
197
68dda785
VZ
198 GtkWidget *item = gtk_toolbar_append_element
199 (
200 GTK_TOOLBAR(m_toolbar),
201 ctype,
202 (GtkWidget *)NULL,
203 (const char *)NULL,
204 helpString1,
205 "",
206 tool_pixmap,
207 (GtkSignalFunc)gtk_toolbar_callback,
208 (gpointer)tool
209 );
210
68dda785 211 tool->m_item = item;
03f38c58
VZ
212
213 gtk_signal_connect( GTK_OBJECT(tool->m_item),
214 "enter_notify_event",
215 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback),
216 (gpointer)tool );
314055fa 217
a3622daa
VZ
218 m_tools.Append( tool );
219
c801d85f 220 return tool;
fc008f25 221}
c801d85f 222
03f38c58 223void wxToolBar::AddSeparator()
c801d85f
KB
224{
225 gtk_toolbar_append_space( m_toolbar );
fc008f25 226}
c801d85f 227
03f38c58 228void wxToolBar::ClearTools()
c801d85f 229{
fc008f25
RR
230 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
231}
c801d85f 232
03f38c58 233void wxToolBar::Realize()
46dc76ba
RR
234{
235 m_x = 0;
236 m_y = 0;
237 m_width = 100;
238 m_height = 0;
239
240 wxNode *node = m_tools.First();
241 while (node)
242 {
243 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
244 if (tool->m_bitmap1.Ok())
245 {
246 int tool_height = tool->m_bitmap1.GetHeight();
247 if (tool_height > m_height) m_height = tool_height;
fc008f25 248 }
46dc76ba
RR
249
250 node = node->Next();
fc008f25 251 }
46dc76ba
RR
252
253 m_height += 10;
fc008f25 254}
46dc76ba 255
716b7364 256void wxToolBar::EnableTool(int toolIndex, bool enable)
c801d85f 257{
cf4219e7
RR
258 wxNode *node = m_tools.First();
259 while (node)
260 {
261 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
262 if (tool->m_index == toolIndex)
263 {
264 tool->m_enabled = enable;
265 return;
266 }
267 node = node->Next();
fc008f25
RR
268 }
269
270 wxFAIL_MSG( "wrong toolbar index" );
271}
c801d85f 272
fc008f25 273void wxToolBar::ToggleTool( int toolIndex, bool toggle )
c801d85f 274{
fc008f25
RR
275 wxNode *node = m_tools.First();
276 while (node)
277 {
278 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
279 if (tool->m_index == toolIndex)
280 {
281 tool->m_toggleState = toggle;
282 if ((tool->m_item) && (GTK_IS_TOGGLE_BUTTON(tool->m_item)))
283 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool->m_item), toggle );
284 return;
285 }
286 node = node->Next();
287 }
288
289 wxFAIL_MSG( "wrong toolbar index" );
290}
c801d85f 291
fc008f25 292wxObject *wxToolBar::GetToolClientData( int index ) const
c801d85f 293{
cf4219e7
RR
294 wxNode *node = m_tools.First();
295 while (node)
296 {
297 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
298 if (tool->m_index == index) return tool->m_clientData;;
299 node = node->Next();
fc008f25
RR
300 }
301
302 wxFAIL_MSG( "wrong toolbar index" );
303
cf4219e7 304 return (wxObject*)NULL;
fc008f25 305}
c801d85f 306
716b7364 307bool wxToolBar::GetToolState(int toolIndex) const
c801d85f 308{
cf4219e7
RR
309 wxNode *node = m_tools.First();
310 while (node)
311 {
312 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
313 if (tool->m_index == toolIndex) return tool->m_toggleState;
314 node = node->Next();
fc008f25
RR
315 }
316
317 wxFAIL_MSG( "wrong toolbar index" );
318
cf4219e7 319 return FALSE;
fc008f25 320}
c801d85f 321
716b7364 322bool wxToolBar::GetToolEnabled(int toolIndex) const
c801d85f 323{
cf4219e7
RR
324 wxNode *node = m_tools.First();
325 while (node)
326 {
327 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
328 if (tool->m_index == toolIndex) return tool->m_enabled;
329 node = node->Next();
fc008f25
RR
330 }
331
332 wxFAIL_MSG( "wrong toolbar index" );
333
cf4219e7 334 return FALSE;
fc008f25 335}
c801d85f 336
cf4219e7 337void wxToolBar::SetMargins( int WXUNUSED(x), int WXUNUSED(y) )
c801d85f 338{
03f38c58 339 wxFAIL_MSG( "wxToolBar::SetMargins not implemented" );
fc008f25 340}
c801d85f 341
cf4219e7 342void wxToolBar::SetToolPacking( int WXUNUSED(packing) )
c801d85f 343{
03f38c58 344 wxFAIL_MSG( "wxToolBar::SetToolPacking not implemented" );
fc008f25 345}
c801d85f 346
cf4219e7 347void wxToolBar::SetToolSeparation( int separation )
c801d85f 348{
cf4219e7 349 gtk_toolbar_set_space_size( m_toolbar, separation );
fc008f25 350}
c801d85f 351