]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/tbargtk.cpp
some feeble attempts at making wxBeginBusyCursor work
[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
83624f79
RR
16#include "glib.h"
17#include "gdk/gdk.h"
18#include "gtk/gtk.h"
19
314055fa
RR
20//-----------------------------------------------------------------------------
21// data
22//-----------------------------------------------------------------------------
23
24extern bool g_blockEventsOnDrag;
25
c801d85f 26//-----------------------------------------------------------------------------
2f2aa628 27// "clicked" (internal from gtk_toolbar)
c801d85f
KB
28//-----------------------------------------------------------------------------
29
30static void gtk_toolbar_callback( GtkWidget *WXUNUSED(widget), wxToolBarTool *tool )
31{
1144d24d
RR
32 if (g_blockEventsOnDrag) return;
33 if (!tool->m_enabled) return;
a3622daa 34
1144d24d 35 if (tool->m_isToggle) tool->m_toggleState = !tool->m_toggleState;
a3622daa 36
1144d24d 37 tool->m_owner->OnLeftClick( tool->m_index, tool->m_toggleState );
fc008f25 38}
c801d85f 39
2f2aa628
RR
40//-----------------------------------------------------------------------------
41// "enter_notify_event"
42//-----------------------------------------------------------------------------
43
314055fa
RR
44static gint gtk_toolbar_enter_callback( GtkWidget *WXUNUSED(widget),
45 GdkEventCrossing *WXUNUSED(gdk_event), wxToolBarTool *tool )
46{
1144d24d 47 if (g_blockEventsOnDrag) return TRUE;
b98d804b
RR
48
49 /* we grey-out the tip text of disabled tool */
50
51 wxToolBar *tb = tool->m_owner;
52
53 if (tool->m_enabled)
54 {
55 if (tb->m_fg->red != 0)
56 {
57 tb->m_fg->red = 0;
58 tb->m_fg->green = 0;
59 tb->m_fg->blue = 0;
60 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb->m_toolbar) ), tb->m_fg );
61 gtk_tooltips_set_colors( GTK_TOOLBAR(tb->m_toolbar)->tooltips, tb->m_bg, tb->m_fg );
62 }
63 }
64 else
65 {
66 if (tb->m_fg->red == 0)
67 {
68 tb->m_fg->red = 33000;
69 tb->m_fg->green = 33000;
70 tb->m_fg->blue = 33000;
71 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb->m_toolbar) ), tb->m_fg );
72 gtk_tooltips_set_colors( GTK_TOOLBAR(tb->m_toolbar)->tooltips, tb->m_bg, tb->m_fg );
73 }
74 }
75
76 /* emit the event */
314055fa 77
b98d804b 78 tb->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{
83624f79
RR
102 delete m_fg;
103 delete m_bg;
fc008f25 104}
c801d85f 105
a3622daa 106bool wxToolBar::Create( wxWindow *parent, wxWindowID id,
c801d85f 107 const wxPoint& pos, const wxSize& size,
debe6624 108 long style, const wxString& name )
c801d85f 109{
1144d24d 110 m_needParent = TRUE;
a3622daa 111
1144d24d 112 PreCreation( parent, id, pos, size, style, name );
c801d85f 113
1144d24d 114 m_tools.DeleteContents( TRUE );
a3622daa 115
1144d24d
RR
116 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL,
117 GTK_TOOLBAR_ICONS ) );
a3622daa 118
1144d24d
RR
119 m_separation = 5;
120 gtk_toolbar_set_space_size( m_toolbar, m_separation );
121 m_hasToolAlready = FALSE;
3502e687
RR
122
123 if (style & wxTB_DOCKABLE)
124 {
125 m_widget = gtk_handle_box_new();
126 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
127 gtk_widget_show( GTK_WIDGET(m_toolbar) );
128 }
129 else
130 {
131 m_widget = GTK_WIDGET(m_toolbar);
132 }
32e9da8b 133
1144d24d 134 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
83624f79
RR
135
136 m_fg = new GdkColor;
137 m_fg->red = 0;
138 m_fg->green = 0;
139 m_fg->blue = 0;
140 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_fg );
b46e8696 141
83624f79
RR
142 m_bg = new GdkColor;
143 m_bg->red = 65535;
144 m_bg->green = 65535;
145 m_bg->blue = 50000;
146 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_bg );
b46e8696 147
83624f79 148 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar)->tooltips, m_bg, m_fg );
a3622daa 149
1144d24d
RR
150 m_xMargin = 0;
151 m_yMargin = 0;
152
153 m_parent->AddChild( this );
6ca41e57 154
1144d24d 155 (m_parent->m_insertCallback)( m_parent, this );
6ca41e57 156
1144d24d 157 PostCreation();
a3622daa 158
1144d24d 159 Show( TRUE );
a3622daa 160
1144d24d 161 return TRUE;
fc008f25 162}
c801d85f 163
716b7364 164bool wxToolBar::OnLeftClick( int toolIndex, bool toggleDown )
c801d85f 165{
1144d24d
RR
166 wxCommandEvent event( wxEVT_COMMAND_TOOL_CLICKED, toolIndex );
167 event.SetEventObject(this);
168 event.SetInt( toolIndex );
169 event.SetExtraLong((long) toggleDown);
c801d85f 170
1144d24d 171 GetEventHandler()->ProcessEvent(event);
c801d85f 172
1144d24d 173 return TRUE;
fc008f25 174}
c801d85f 175
716b7364 176void wxToolBar::OnRightClick( int toolIndex, float WXUNUSED(x), float WXUNUSED(y) )
c801d85f 177{
1144d24d
RR
178 wxCommandEvent event( wxEVT_COMMAND_TOOL_RCLICKED, toolIndex );
179 event.SetEventObject( this );
180 event.SetInt( toolIndex );
c801d85f 181
1144d24d 182 GetEventHandler()->ProcessEvent(event);
fc008f25 183}
c801d85f 184
716b7364 185void wxToolBar::OnMouseEnter( int toolIndex )
c801d85f 186{
1144d24d
RR
187 wxCommandEvent event( wxEVT_COMMAND_TOOL_ENTER, GetId() );
188 event.SetEventObject(this);
189 event.SetInt( toolIndex );
314055fa 190
1144d24d 191 GetEventHandler()->ProcessEvent(event);
fc008f25 192}
c801d85f 193
a3622daa 194wxToolBarTool *wxToolBar::AddTool( int toolIndex, const wxBitmap& bitmap,
debe6624
JS
195 const wxBitmap& pushedBitmap, bool toggle,
196 float WXUNUSED(xPos), float WXUNUSED(yPos), wxObject *clientData,
c801d85f
KB
197 const wxString& helpString1, const wxString& helpString2 )
198{
1144d24d
RR
199 m_hasToolAlready = TRUE;
200
201 wxCHECK_MSG( bitmap.Ok(), (wxToolBarTool *)NULL,
202 "invalid bitmap for wxToolBar icon" );
a3622daa 203
1144d24d
RR
204 wxCHECK_MSG( bitmap.GetBitmap() == NULL, (wxToolBarTool *)NULL,
205 "wxToolBar doesn't support GdkBitmap" );
03f38c58 206
1144d24d
RR
207 wxCHECK_MSG( bitmap.GetPixmap() != NULL, (wxToolBarTool *)NULL,
208 "wxToolBar::Add needs a wxBitmap" );
903f689b 209
1144d24d 210 GtkWidget *tool_pixmap = (GtkWidget *)NULL;
903f689b 211
903f689b 212 GdkPixmap *pixmap = bitmap.GetPixmap();
a3622daa 213
68dda785
VZ
214 GdkBitmap *mask = (GdkBitmap *)NULL;
215 if ( bitmap.GetMask() )
216 mask = bitmap.GetMask()->GetBitmap();
903f689b
RR
217
218 tool_pixmap = gtk_pixmap_new( pixmap, mask );
903f689b 219
1144d24d 220 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
a3622daa 221
2b1c162e
RR
222 wxToolBarTool *tool = new wxToolBarTool( this, toolIndex, bitmap, pushedBitmap,
223 toggle, clientData,
224 helpString1, helpString2,
225 tool_pixmap );
226
1144d24d
RR
227 GtkToolbarChildType ctype = toggle ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
228 : GTK_TOOLBAR_CHILD_BUTTON;
03f38c58 229
1144d24d 230 GtkWidget *item = gtk_toolbar_append_element
68dda785
VZ
231 (
232 GTK_TOOLBAR(m_toolbar),
233 ctype,
234 (GtkWidget *)NULL,
235 (const char *)NULL,
236 helpString1,
237 "",
238 tool_pixmap,
239 (GtkSignalFunc)gtk_toolbar_callback,
240 (gpointer)tool
241 );
242
1144d24d 243 tool->m_item = item;
03f38c58 244
1144d24d
RR
245 gtk_signal_connect( GTK_OBJECT(tool->m_item),
246 "enter_notify_event",
247 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback),
248 (gpointer)tool );
314055fa 249
1144d24d 250 m_tools.Append( tool );
a3622daa 251
1144d24d 252 return tool;
fc008f25 253}
c801d85f 254
03f38c58 255void wxToolBar::AddSeparator()
c801d85f 256{
1144d24d 257 gtk_toolbar_append_space( m_toolbar );
fc008f25 258}
c801d85f 259
03f38c58 260void wxToolBar::ClearTools()
c801d85f 261{
1144d24d 262 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
fc008f25 263}
c801d85f 264
1144d24d 265bool wxToolBar::Realize()
46dc76ba 266{
1144d24d
RR
267 m_x = 0;
268 m_y = 0;
269 m_width = 100;
270 m_height = 0;
46dc76ba 271
1144d24d
RR
272 wxNode *node = m_tools.First();
273 while (node)
46dc76ba 274 {
1144d24d
RR
275 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
276 if (tool->m_bitmap1.Ok())
277 {
278 int tool_height = tool->m_bitmap1.GetHeight();
279 if (tool_height > m_height) m_height = tool_height;
280 }
46dc76ba 281
1144d24d
RR
282 node = node->Next();
283 }
46dc76ba 284
1144d24d
RR
285 m_height += 5 + 2*m_yMargin;
286
287 return TRUE;
fc008f25 288}
46dc76ba 289
716b7364 290void wxToolBar::EnableTool(int toolIndex, bool enable)
c801d85f 291{
1144d24d
RR
292 wxNode *node = m_tools.First();
293 while (node)
294 {
295 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
296 if (tool->m_index == toolIndex)
2b1c162e 297 {
1144d24d 298 tool->m_enabled = enable;
2b1c162e 299
b98d804b
RR
300/* we don't disable the tools for now as the bitmaps don't get
301 greyed anyway and this also disables tooltips
302
2b1c162e
RR
303 if (tool->m_item)
304 gtk_widget_set_sensitive( tool->m_item, enable );
b98d804b 305*/
2b1c162e 306
1144d24d
RR
307 return;
308 }
309 node = node->Next();
cf4219e7 310 }
fc008f25 311
1144d24d 312 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 313}
c801d85f 314
fc008f25 315void wxToolBar::ToggleTool( int toolIndex, bool toggle )
c801d85f 316{
1144d24d
RR
317 wxNode *node = m_tools.First();
318 while (node)
319 {
320 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
321 if (tool->m_index == toolIndex)
322 {
323 tool->m_toggleState = toggle;
324 if ((tool->m_item) && (GTK_IS_TOGGLE_BUTTON(tool->m_item)))
325 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool->m_item), toggle );
326 return;
327 }
328 node = node->Next();
fc008f25 329 }
fc008f25 330
1144d24d 331 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 332}
c801d85f 333
fc008f25 334wxObject *wxToolBar::GetToolClientData( int index ) const
c801d85f 335{
1144d24d
RR
336 wxNode *node = m_tools.First();
337 while (node)
338 {
339 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
340 if (tool->m_index == index) return tool->m_clientData;;
341 node = node->Next();
342 }
fc008f25 343
1144d24d 344 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 345
1144d24d 346 return (wxObject*)NULL;
fc008f25 347}
c801d85f 348
716b7364 349bool wxToolBar::GetToolState(int toolIndex) const
c801d85f 350{
1144d24d
RR
351 wxNode *node = m_tools.First();
352 while (node)
353 {
354 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
355 if (tool->m_index == toolIndex) return tool->m_toggleState;
356 node = node->Next();
357 }
fc008f25 358
1144d24d 359 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 360
1144d24d 361 return FALSE;
fc008f25 362}
c801d85f 363
716b7364 364bool wxToolBar::GetToolEnabled(int toolIndex) const
c801d85f 365{
1144d24d
RR
366 wxNode *node = m_tools.First();
367 while (node)
368 {
369 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
370 if (tool->m_index == toolIndex) return tool->m_enabled;
371 node = node->Next();
372 }
fc008f25 373
1144d24d 374 wxFAIL_MSG( "wrong toolbar index" );
fc008f25 375
1144d24d 376 return FALSE;
fc008f25 377}
c801d85f 378
1144d24d 379void wxToolBar::SetMargins( int x, int y )
c801d85f 380{
1144d24d
RR
381 wxCHECK_RET( !m_hasToolAlready, "wxToolBar::SetMargins must be called before adding tool." );
382
383 if (x > 2) gtk_toolbar_append_space( m_toolbar ); // oh well
384
385 m_xMargin = x;
386 m_yMargin = y;
fc008f25 387}
c801d85f 388
cf4219e7 389void wxToolBar::SetToolPacking( int WXUNUSED(packing) )
c801d85f 390{
1144d24d 391 wxFAIL_MSG( "wxToolBar::SetToolPacking not implemented" );
fc008f25 392}
c801d85f 393
cf4219e7 394void wxToolBar::SetToolSeparation( int separation )
c801d85f 395{
1144d24d
RR
396 gtk_toolbar_set_space_size( m_toolbar, separation );
397 m_separation = separation;
398}
399
400int wxToolBar::GetToolPacking()
401{
402 return 0;
403}
404
405int wxToolBar::GetToolSeparation()
406{
407 return m_separation;
408}
409
410wxString wxToolBar::GetToolLongHelp(int toolIndex)
411{
412 wxNode *node = m_tools.First();
413 while (node)
414 {
415 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
416 if (tool->m_index == toolIndex)
417 {
418 return tool->m_longHelpString;
419 }
420 node = node->Next();
421 }
422
423 wxFAIL_MSG( "wrong toolbar index" );
424
425 return "";
426}
427
428wxString wxToolBar::GetToolShortHelp(int toolIndex)
429{
430 wxNode *node = m_tools.First();
431 while (node)
432 {
433 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
434 if (tool->m_index == toolIndex)
435 {
436 return tool->m_shortHelpString;
437 }
438 node = node->Next();
439 }
440
441 wxFAIL_MSG( "wrong toolbar index" );
442
443 return "";
fc008f25 444}
c801d85f 445
1144d24d
RR
446void wxToolBar::SetToolLongHelp(int toolIndex, const wxString& helpString)
447{
448 wxNode *node = m_tools.First();
449 while (node)
450 {
451 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
452 if (tool->m_index == toolIndex)
453 {
454 tool->m_longHelpString = helpString;
455 return;
456 }
457 node = node->Next();
458 }
459
460 wxFAIL_MSG( "wrong toolbar index" );
461
462 return;
463}
464
465void wxToolBar::SetToolShortHelp(int toolIndex, const wxString& helpString)
466{
467 wxNode *node = m_tools.First();
468 while (node)
469 {
470 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
471 if (tool->m_index == toolIndex)
472 {
473 tool->m_shortHelpString = helpString;
474 return;
475 }
476 node = node->Next();
477 }
478
479 wxFAIL_MSG( "wrong toolbar index" );
480
481 return;
482}
483
484