]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/tbargtk.cpp
mac support as stubs added
[wxWidgets.git] / src / gtk1 / 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 35{
1144d24d
RR
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;
48 m_item = item;
fc008f25 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{
1144d24d
RR
61 if (g_blockEventsOnDrag) return;
62 if (!tool->m_enabled) return;
a3622daa 63
1144d24d 64 if (tool->m_isToggle) tool->m_toggleState = !tool->m_toggleState;
a3622daa 65
1144d24d 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{
1144d24d 76 if (g_blockEventsOnDrag) return TRUE;
314055fa 77
1144d24d 78 tool->m_owner->OnMouseEnter( tool->m_index );
314055fa 79
1144d24d 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 96{
1144d24d 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 107{
1144d24d 108 m_needParent = TRUE;
a3622daa 109
1144d24d 110 PreCreation( parent, id, pos, size, style, name );
c801d85f 111
1144d24d 112 m_tools.DeleteContents( TRUE );
a3622daa 113
1144d24d
RR
114 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL,
115 GTK_TOOLBAR_ICONS ) );
a3622daa 116
1144d24d
RR
117 m_separation = 5;
118 gtk_toolbar_set_space_size( m_toolbar, m_separation );
119 m_hasToolAlready = FALSE;
120
121 m_widget = GTK_WIDGET(m_toolbar);
32e9da8b 122
1144d24d 123 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
b46e8696 124
1144d24d
RR
125 m_fg.red = 0;
126 m_fg.green = 0;
127 m_fg.blue = 0;
128 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), &m_fg );
b46e8696 129
1144d24d
RR
130 m_bg.red = 65535;
131 m_bg.green = 65535;
132 m_bg.blue = 50000;
133 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), &m_bg );
b46e8696 134
1144d24d 135 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar)->tooltips, &m_bg, &m_fg );
a3622daa 136
1144d24d
RR
137 m_xMargin = 0;
138 m_yMargin = 0;
139
140 m_parent->AddChild( this );
6ca41e57 141
1144d24d 142 (m_parent->m_insertCallback)( m_parent, this );
6ca41e57 143
1144d24d 144 PostCreation();
a3622daa 145
1144d24d 146 Show( TRUE );
a3622daa 147
1144d24d 148 return TRUE;
fc008f25 149}
c801d85f 150
716b7364 151bool wxToolBar::OnLeftClick( int toolIndex, bool toggleDown )
c801d85f 152{
1144d24d
RR
153 wxCommandEvent event( wxEVT_COMMAND_TOOL_CLICKED, toolIndex );
154 event.SetEventObject(this);
155 event.SetInt( toolIndex );
156 event.SetExtraLong((long) toggleDown);
c801d85f 157
1144d24d 158 GetEventHandler()->ProcessEvent(event);
c801d85f 159
1144d24d 160 return TRUE;
fc008f25 161}
c801d85f 162
716b7364 163void wxToolBar::OnRightClick( int toolIndex, float WXUNUSED(x), float WXUNUSED(y) )
c801d85f 164{
1144d24d
RR
165 wxCommandEvent event( wxEVT_COMMAND_TOOL_RCLICKED, toolIndex );
166 event.SetEventObject( this );
167 event.SetInt( toolIndex );
c801d85f 168
1144d24d 169 GetEventHandler()->ProcessEvent(event);
fc008f25 170}
c801d85f 171
716b7364 172void wxToolBar::OnMouseEnter( int toolIndex )
c801d85f 173{
1144d24d
RR
174 wxCommandEvent event( wxEVT_COMMAND_TOOL_ENTER, GetId() );
175 event.SetEventObject(this);
176 event.SetInt( toolIndex );
314055fa 177
1144d24d 178 GetEventHandler()->ProcessEvent(event);
fc008f25 179}
c801d85f 180
a3622daa 181wxToolBarTool *wxToolBar::AddTool( int toolIndex, const wxBitmap& bitmap,
debe6624
JS
182 const wxBitmap& pushedBitmap, bool toggle,
183 float WXUNUSED(xPos), float WXUNUSED(yPos), wxObject *clientData,
c801d85f
KB
184 const wxString& helpString1, const wxString& helpString2 )
185{
1144d24d
RR
186 m_hasToolAlready = TRUE;
187
188 wxCHECK_MSG( bitmap.Ok(), (wxToolBarTool *)NULL,
189 "invalid bitmap for wxToolBar icon" );
a3622daa 190
1144d24d
RR
191 wxToolBarTool *tool = new wxToolBarTool( this, toolIndex, bitmap, pushedBitmap,
192 toggle, clientData,
193 helpString1, helpString2 );
a3622daa 194
1144d24d
RR
195 wxCHECK_MSG( bitmap.GetBitmap() == NULL, (wxToolBarTool *)NULL,
196 "wxToolBar doesn't support GdkBitmap" );
03f38c58 197
1144d24d
RR
198 wxCHECK_MSG( bitmap.GetPixmap() != NULL, (wxToolBarTool *)NULL,
199 "wxToolBar::Add needs a wxBitmap" );
903f689b 200
1144d24d 201 GtkWidget *tool_pixmap = (GtkWidget *)NULL;
903f689b 202
903f689b 203 GdkPixmap *pixmap = bitmap.GetPixmap();
a3622daa 204
68dda785
VZ
205 GdkBitmap *mask = (GdkBitmap *)NULL;
206 if ( bitmap.GetMask() )
207 mask = bitmap.GetMask()->GetBitmap();
903f689b
RR
208
209 tool_pixmap = gtk_pixmap_new( pixmap, mask );
903f689b 210
1144d24d 211 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
a3622daa 212
1144d24d
RR
213 GtkToolbarChildType ctype = toggle ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
214 : GTK_TOOLBAR_CHILD_BUTTON;
03f38c58 215
1144d24d 216 GtkWidget *item = gtk_toolbar_append_element
68dda785
VZ
217 (
218 GTK_TOOLBAR(m_toolbar),
219 ctype,
220 (GtkWidget *)NULL,
221 (const char *)NULL,
222 helpString1,
223 "",
224 tool_pixmap,
225 (GtkSignalFunc)gtk_toolbar_callback,
226 (gpointer)tool
227 );
228
1144d24d 229 tool->m_item = item;
03f38c58 230
1144d24d
RR
231 gtk_signal_connect( GTK_OBJECT(tool->m_item),
232 "enter_notify_event",
233 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback),
234 (gpointer)tool );
314055fa 235
1144d24d 236 m_tools.Append( tool );
a3622daa 237
1144d24d 238 return tool;
fc008f25 239}
c801d85f 240
03f38c58 241void wxToolBar::AddSeparator()
c801d85f 242{
1144d24d 243 gtk_toolbar_append_space( m_toolbar );
fc008f25 244}
c801d85f 245
03f38c58 246void wxToolBar::ClearTools()
c801d85f 247{
1144d24d 248 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
fc008f25 249}
c801d85f 250
1144d24d 251bool wxToolBar::Realize()
46dc76ba 252{
1144d24d
RR
253 m_x = 0;
254 m_y = 0;
255 m_width = 100;
256 m_height = 0;
46dc76ba 257
1144d24d
RR
258 wxNode *node = m_tools.First();
259 while (node)
46dc76ba 260 {
1144d24d
RR
261 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
262 if (tool->m_bitmap1.Ok())
263 {
264 int tool_height = tool->m_bitmap1.GetHeight();
265 if (tool_height > m_height) m_height = tool_height;
266 }
46dc76ba 267
1144d24d
RR
268 node = node->Next();
269 }
46dc76ba 270
1144d24d
RR
271 m_height += 5 + 2*m_yMargin;
272
273 return TRUE;
fc008f25 274}
46dc76ba 275
716b7364 276void wxToolBar::EnableTool(int toolIndex, bool enable)
c801d85f 277{
1144d24d
RR
278 wxNode *node = m_tools.First();
279 while (node)
280 {
281 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
282 if (tool->m_index == toolIndex)
283 {
284 tool->m_enabled = enable;
285 return;
286 }
287 node = node->Next();
cf4219e7 288 }
fc008f25 289
1144d24d 290 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 291}
c801d85f 292
fc008f25 293void wxToolBar::ToggleTool( int toolIndex, bool toggle )
c801d85f 294{
1144d24d
RR
295 wxNode *node = m_tools.First();
296 while (node)
297 {
298 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
299 if (tool->m_index == toolIndex)
300 {
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 );
304 return;
305 }
306 node = node->Next();
fc008f25 307 }
fc008f25 308
1144d24d 309 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 310}
c801d85f 311
fc008f25 312wxObject *wxToolBar::GetToolClientData( int index ) const
c801d85f 313{
1144d24d
RR
314 wxNode *node = m_tools.First();
315 while (node)
316 {
317 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
318 if (tool->m_index == index) return tool->m_clientData;;
319 node = node->Next();
320 }
fc008f25 321
1144d24d 322 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 323
1144d24d 324 return (wxObject*)NULL;
fc008f25 325}
c801d85f 326
716b7364 327bool wxToolBar::GetToolState(int toolIndex) const
c801d85f 328{
1144d24d
RR
329 wxNode *node = m_tools.First();
330 while (node)
331 {
332 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
333 if (tool->m_index == toolIndex) return tool->m_toggleState;
334 node = node->Next();
335 }
fc008f25 336
1144d24d 337 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 338
1144d24d 339 return FALSE;
fc008f25 340}
c801d85f 341
716b7364 342bool wxToolBar::GetToolEnabled(int toolIndex) const
c801d85f 343{
1144d24d
RR
344 wxNode *node = m_tools.First();
345 while (node)
346 {
347 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
348 if (tool->m_index == toolIndex) return tool->m_enabled;
349 node = node->Next();
350 }
fc008f25 351
1144d24d 352 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 353
1144d24d 354 return FALSE;
fc008f25 355}
c801d85f 356
1144d24d 357void wxToolBar::SetMargins( int x, int y )
c801d85f 358{
1144d24d
RR
359 wxCHECK_RET( !m_hasToolAlready, "wxToolBar::SetMargins must be called before adding tool." );
360
361 if (x > 2) gtk_toolbar_append_space( m_toolbar ); // oh well
362
363 m_xMargin = x;
364 m_yMargin = y;
fc008f25 365}
c801d85f 366
cf4219e7 367void wxToolBar::SetToolPacking( int WXUNUSED(packing) )
c801d85f 368{
1144d24d 369 wxFAIL_MSG( "wxToolBar::SetToolPacking not implemented" );
fc008f25 370}
c801d85f 371
cf4219e7 372void wxToolBar::SetToolSeparation( int separation )
c801d85f 373{
1144d24d
RR
374 gtk_toolbar_set_space_size( m_toolbar, separation );
375 m_separation = separation;
376}
377
378int wxToolBar::GetToolPacking()
379{
380 return 0;
381}
382
383int wxToolBar::GetToolSeparation()
384{
385 return m_separation;
386}
387
388wxString wxToolBar::GetToolLongHelp(int toolIndex)
389{
390 wxNode *node = m_tools.First();
391 while (node)
392 {
393 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
394 if (tool->m_index == toolIndex)
395 {
396 return tool->m_longHelpString;
397 }
398 node = node->Next();
399 }
400
401 wxFAIL_MSG( "wrong toolbar index" );
402
403 return "";
404}
405
406wxString wxToolBar::GetToolShortHelp(int toolIndex)
407{
408 wxNode *node = m_tools.First();
409 while (node)
410 {
411 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
412 if (tool->m_index == toolIndex)
413 {
414 return tool->m_shortHelpString;
415 }
416 node = node->Next();
417 }
418
419 wxFAIL_MSG( "wrong toolbar index" );
420
421 return "";
fc008f25 422}
c801d85f 423
1144d24d
RR
424void wxToolBar::SetToolLongHelp(int toolIndex, const wxString& helpString)
425{
426 wxNode *node = m_tools.First();
427 while (node)
428 {
429 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
430 if (tool->m_index == toolIndex)
431 {
432 tool->m_longHelpString = helpString;
433 return;
434 }
435 node = node->Next();
436 }
437
438 wxFAIL_MSG( "wrong toolbar index" );
439
440 return;
441}
442
443void wxToolBar::SetToolShortHelp(int toolIndex, const wxString& helpString)
444{
445 wxNode *node = m_tools.First();
446 while (node)
447 {
448 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
449 if (tool->m_index == toolIndex)
450 {
451 tool->m_shortHelpString = helpString;
452 return;
453 }
454 node = node->Next();
455 }
456
457 wxFAIL_MSG( "wrong toolbar index" );
458
459 return;
460}
461
462