]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/tbargtk.cpp
unused param warning fixed
[wxWidgets.git] / src / gtk / tbargtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tbargtk.cpp
3 // Purpose: GTK toolbar
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "tbargtk.h"
12 #endif
13
14 #include "wx/toolbar.h"
15
16 #if wxUSE_TOOLBAR
17
18 #include "wx/frame.h"
19
20 #include "glib.h"
21 #include "gdk/gdk.h"
22 #include "gtk/gtk.h"
23
24 //-----------------------------------------------------------------------------
25 // idle system
26 //-----------------------------------------------------------------------------
27
28 extern void wxapp_install_idle_handler();
29 extern bool g_isIdle;
30
31 //-----------------------------------------------------------------------------
32 // data
33 //-----------------------------------------------------------------------------
34
35 extern bool g_blockEventsOnDrag;
36 extern wxCursor g_globalCursor;
37
38 //-----------------------------------------------------------------------------
39 // "clicked" (internal from gtk_toolbar)
40 //-----------------------------------------------------------------------------
41
42 static void gtk_toolbar_callback( GtkWidget *WXUNUSED(widget), wxToolBarTool *tool )
43 {
44 if (g_isIdle)
45 wxapp_install_idle_handler();
46
47 if (tool->m_owner->m_blockNextEvent)
48 {
49 tool->m_owner->m_blockNextEvent = FALSE;
50 return;
51 }
52
53 if (g_blockEventsOnDrag) return;
54 if (!tool->m_enabled) return;
55
56 if (tool->m_isToggle)
57 {
58 tool->m_toggleState = !tool->m_toggleState;
59
60 if (tool->m_bitmap2.Ok())
61 {
62 wxBitmap bitmap = tool->m_bitmap1;
63 if (tool->m_toggleState) bitmap = tool->m_bitmap2;
64
65 GtkPixmap *pixmap = GTK_PIXMAP( tool->m_pixmap );
66
67 GdkBitmap *mask = (GdkBitmap *) NULL;
68 if (bitmap.GetMask()) mask = bitmap.GetMask()->GetBitmap();
69
70 gtk_pixmap_set( pixmap, bitmap.GetPixmap(), mask );
71 }
72 }
73
74 tool->m_owner->OnLeftClick( tool->m_index, tool->m_toggleState );
75 }
76
77 //-----------------------------------------------------------------------------
78 // "enter_notify_event"
79 //-----------------------------------------------------------------------------
80
81 static gint gtk_toolbar_enter_callback( GtkWidget *WXUNUSED(widget),
82 GdkEventCrossing *WXUNUSED(gdk_event), wxToolBarTool *tool )
83 {
84 if (g_isIdle) wxapp_install_idle_handler();
85
86 if (g_blockEventsOnDrag) return TRUE;
87
88
89 wxToolBar *tb = tool->m_owner;
90
91 #if (GTK_MINOR_VERSION == 0)
92 /* we grey-out the tip text of disabled tool in GTK 1.0 */
93 if (tool->m_enabled)
94 {
95 if (tb->m_fg->red != 0)
96 {
97 tb->m_fg->red = 0;
98 tb->m_fg->green = 0;
99 tb->m_fg->blue = 0;
100 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb->m_toolbar) ), tb->m_fg );
101
102 gtk_tooltips_set_colors( GTK_TOOLBAR(tb->m_toolbar)->tooltips, tb->m_bg, tb->m_fg );
103 }
104 }
105 else
106 {
107 if (tb->m_fg->red == 0)
108 {
109 tb->m_fg->red = 33000;
110 tb->m_fg->green = 33000;
111 tb->m_fg->blue = 33000;
112 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb->m_toolbar) ), tb->m_fg );
113 gtk_tooltips_set_colors( GTK_TOOLBAR(tb->m_toolbar)->tooltips, tb->m_bg, tb->m_fg );
114 }
115 }
116 #endif
117
118 /* emit the event */
119
120 tb->OnMouseEnter( tool->m_index );
121
122 return FALSE;
123 }
124
125 //-----------------------------------------------------------------------------
126 // wxToolBar
127 //-----------------------------------------------------------------------------
128
129 IMPLEMENT_DYNAMIC_CLASS(wxToolBar,wxControl)
130
131 BEGIN_EVENT_TABLE(wxToolBar, wxControl)
132 EVT_IDLE(wxToolBar::OnIdle)
133 END_EVENT_TABLE()
134
135 wxToolBar::wxToolBar()
136 {
137 }
138
139 wxToolBar::wxToolBar( wxWindow *parent, wxWindowID id,
140 const wxPoint& pos, const wxSize& size,
141 long style, const wxString& name )
142 {
143 Create( parent, id, pos, size, style, name );
144 }
145
146 wxToolBar::~wxToolBar()
147 {
148 delete m_fg;
149 delete m_bg;
150 }
151
152 bool wxToolBar::Create( wxWindow *parent, wxWindowID id,
153 const wxPoint& pos, const wxSize& size,
154 long style, const wxString& name )
155 {
156 m_needParent = TRUE;
157 m_blockNextEvent = FALSE;
158
159 if (!PreCreation( parent, pos, size ) ||
160 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
161 {
162 wxFAIL_MSG( wxT("wxToolBar creation failed") );
163 return FALSE;
164 }
165
166 m_tools.DeleteContents( TRUE );
167
168 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL,
169 GTK_TOOLBAR_ICONS ) );
170
171 m_separation = 5;
172 gtk_toolbar_set_space_size( m_toolbar, m_separation );
173 m_hasToolAlready = FALSE;
174
175 if (style & wxTB_DOCKABLE)
176 {
177 m_widget = gtk_handle_box_new();
178 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
179 gtk_widget_show( GTK_WIDGET(m_toolbar) );
180
181 #if (GTK_MINOR_VERSION > 0)
182 if (style & wxTB_FLAT)
183 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget), GTK_SHADOW_NONE );
184 #endif
185 }
186 else
187 {
188 m_widget = GTK_WIDGET(m_toolbar);
189 }
190
191 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
192
193 #if (GTK_MINOR_VERSION > 0)
194 if (style & wxTB_FLAT)
195 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar), GTK_RELIEF_NONE );
196 #endif
197
198 m_fg = new GdkColor;
199 m_fg->red = 0;
200 m_fg->green = 0;
201 m_fg->blue = 0;
202 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_fg );
203
204 m_bg = new GdkColor;
205 m_bg->red = 65535;
206 m_bg->green = 65535;
207 m_bg->blue = 50000;
208 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_bg );
209
210 #if (GTK_MINOR_VERSION > 0)
211 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar)->tooltips );
212
213 GtkStyle *g_style =
214 gtk_style_copy(
215 gtk_widget_get_style(
216 GTK_TOOLBAR(m_toolbar)->tooltips->tip_window ) );
217
218 g_style->bg[GTK_STATE_NORMAL] = *m_bg;
219 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar)->tooltips->tip_window, g_style );
220 #else
221 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar)->tooltips, m_bg, m_fg );
222 #endif
223
224 m_xMargin = 0;
225 m_yMargin = 0;
226
227 m_parent->DoAddChild( this );
228
229 PostCreation();
230
231 Show( TRUE );
232
233 return TRUE;
234 }
235
236 bool wxToolBar::OnLeftClick( int toolIndex, bool toggleDown )
237 {
238 wxCommandEvent event( wxEVT_COMMAND_TOOL_CLICKED, toolIndex );
239 event.SetEventObject(this);
240 event.SetInt( toolIndex );
241 event.SetExtraLong((long) toggleDown);
242
243 GetEventHandler()->ProcessEvent(event);
244
245 return TRUE;
246 }
247
248 void wxToolBar::OnRightClick( int toolIndex, float WXUNUSED(x), float WXUNUSED(y) )
249 {
250 wxCommandEvent event( wxEVT_COMMAND_TOOL_RCLICKED, toolIndex );
251 event.SetEventObject( this );
252 event.SetInt( toolIndex );
253
254 GetEventHandler()->ProcessEvent(event);
255 }
256
257 void wxToolBar::OnMouseEnter( int toolIndex )
258 {
259 wxCommandEvent event( wxEVT_COMMAND_TOOL_ENTER, GetId() );
260 event.SetEventObject(this);
261 event.SetInt( toolIndex );
262
263 GetEventHandler()->ProcessEvent(event);
264 }
265
266 wxToolBarTool *wxToolBar::AddTool( int toolIndex, const wxBitmap& bitmap,
267 const wxBitmap& pushedBitmap, bool toggle,
268 float WXUNUSED(xPos), float WXUNUSED(yPos), wxObject *clientData,
269 const wxString& helpString1, const wxString& helpString2 )
270 {
271 m_hasToolAlready = TRUE;
272
273 wxCHECK_MSG( bitmap.Ok(), (wxToolBarTool *)NULL,
274 wxT("invalid bitmap for wxToolBar icon") );
275
276 wxCHECK_MSG( bitmap.GetBitmap() == NULL, (wxToolBarTool *)NULL,
277 wxT("wxToolBar doesn't support GdkBitmap") );
278
279 wxCHECK_MSG( bitmap.GetPixmap() != NULL, (wxToolBarTool *)NULL,
280 wxT("wxToolBar::Add needs a wxBitmap") );
281
282 GtkWidget *tool_pixmap = (GtkWidget *)NULL;
283
284 GdkPixmap *pixmap = bitmap.GetPixmap();
285
286 GdkBitmap *mask = (GdkBitmap *)NULL;
287 if ( bitmap.GetMask() )
288 mask = bitmap.GetMask()->GetBitmap();
289
290 tool_pixmap = gtk_pixmap_new( pixmap, mask );
291 #if (GTK_MINOR_VERSION > 0)
292 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap), TRUE );
293 #endif
294
295 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
296
297 wxToolBarTool *tool = new wxToolBarTool( this, toolIndex, bitmap, pushedBitmap,
298 toggle, clientData,
299 helpString1, helpString2,
300 tool_pixmap );
301
302 GtkToolbarChildType ctype = toggle ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
303 : GTK_TOOLBAR_CHILD_BUTTON;
304
305 GtkWidget *item = gtk_toolbar_append_element
306 (
307 GTK_TOOLBAR(m_toolbar),
308 ctype,
309 (GtkWidget *)NULL,
310 (const char *)NULL,
311 helpString1.mbc_str(),
312 "",
313 tool_pixmap,
314 (GtkSignalFunc)gtk_toolbar_callback,
315 (gpointer)tool
316 );
317
318 tool->m_item = item;
319
320 GtkRequisition req;
321 (* GTK_WIDGET_CLASS( GTK_OBJECT(m_widget)->klass )->size_request ) (m_widget, &req );
322 m_width = req.width;
323 m_height = req.height;
324
325 gtk_signal_connect( GTK_OBJECT(tool->m_item),
326 "enter_notify_event",
327 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback),
328 (gpointer)tool );
329
330 m_tools.Append( tool );
331
332 return tool;
333 }
334
335 void wxToolBar::AddSeparator()
336 {
337 gtk_toolbar_append_space( m_toolbar );
338 }
339
340 void wxToolBar::ClearTools()
341 {
342 wxFAIL_MSG( wxT("wxToolBar::ClearTools not implemented") );
343 }
344
345 bool wxToolBar::Realize()
346 {
347 m_x = 0;
348 m_y = 0;
349 m_width = 100;
350 m_height = 0;
351
352 wxNode *node = m_tools.First();
353 while (node)
354 {
355 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
356 if (tool->m_bitmap1.Ok())
357 {
358 int tool_height = tool->m_bitmap1.GetHeight();
359 if (tool_height > m_height) m_height = tool_height;
360 }
361
362 node = node->Next();
363 }
364
365 m_height += 5 + 2*m_yMargin;
366
367 return TRUE;
368 }
369
370 void wxToolBar::EnableTool(int toolIndex, bool enable)
371 {
372 wxNode *node = m_tools.First();
373 while (node)
374 {
375 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
376 if (tool->m_index == toolIndex)
377 {
378 tool->m_enabled = enable;
379
380 #if (GTK_MINOR_VERSION > 0)
381 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
382 greyed anyway and this also disables tooltips */
383 if (tool->m_item)
384 gtk_widget_set_sensitive( tool->m_item, enable );
385 #endif
386
387 return;
388 }
389 node = node->Next();
390 }
391
392 wxFAIL_MSG( wxT("wrong toolbar index") );
393 }
394
395 void wxToolBar::ToggleTool( int toolIndex, bool toggle )
396 {
397 wxNode *node = m_tools.First();
398 while (node)
399 {
400 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
401 if (tool->m_index == toolIndex)
402 {
403 if ((tool->m_item) && (GTK_IS_TOGGLE_BUTTON(tool->m_item)))
404 {
405 tool->m_toggleState = toggle;
406
407 if (tool->m_bitmap2.Ok())
408 {
409 wxBitmap bitmap = tool->m_bitmap1;
410 if (tool->m_toggleState) bitmap = tool->m_bitmap2;
411
412 GtkPixmap *pixmap = GTK_PIXMAP( tool->m_pixmap );
413
414 GdkBitmap *mask = (GdkBitmap *) NULL;
415 if (bitmap.GetMask()) mask = bitmap.GetMask()->GetBitmap();
416
417 gtk_pixmap_set( pixmap, bitmap.GetPixmap(), mask );
418 }
419
420 m_blockNextEvent = TRUE; // we cannot use gtk_signal_disconnect here
421
422 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool->m_item), toggle );
423 }
424
425 return;
426 }
427 node = node->Next();
428 }
429
430 wxFAIL_MSG( wxT("wrong toolbar index") );
431 }
432
433 wxObject *wxToolBar::GetToolClientData( int index ) const
434 {
435 wxNode *node = m_tools.First();
436 while (node)
437 {
438 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
439 if (tool->m_index == index) return tool->m_clientData;;
440 node = node->Next();
441 }
442
443 wxFAIL_MSG( wxT("wrong toolbar index") );
444
445 return (wxObject*)NULL;
446 }
447
448 bool wxToolBar::GetToolState(int toolIndex) const
449 {
450 wxNode *node = m_tools.First();
451 while (node)
452 {
453 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
454 if (tool->m_index == toolIndex) return tool->m_toggleState;
455 node = node->Next();
456 }
457
458 wxFAIL_MSG( wxT("wrong toolbar index") );
459
460 return FALSE;
461 }
462
463 bool wxToolBar::GetToolEnabled(int toolIndex) const
464 {
465 wxNode *node = m_tools.First();
466 while (node)
467 {
468 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
469 if (tool->m_index == toolIndex) return tool->m_enabled;
470 node = node->Next();
471 }
472
473 wxFAIL_MSG( wxT("wrong toolbar index") );
474
475 return FALSE;
476 }
477
478 void wxToolBar::SetMargins( int x, int y )
479 {
480 wxCHECK_RET( !m_hasToolAlready, wxT("wxToolBar::SetMargins must be called before adding tool.") );
481
482 if (x > 2) gtk_toolbar_append_space( m_toolbar ); // oh well
483
484 m_xMargin = x;
485 m_yMargin = y;
486 }
487
488 void wxToolBar::SetToolPacking( int WXUNUSED(packing) )
489 {
490 wxFAIL_MSG( wxT("wxToolBar::SetToolPacking not implemented") );
491 }
492
493 void wxToolBar::SetToolSeparation( int separation )
494 {
495 gtk_toolbar_set_space_size( m_toolbar, separation );
496 m_separation = separation;
497 }
498
499 int wxToolBar::GetToolPacking()
500 {
501 return 0;
502 }
503
504 int wxToolBar::GetToolSeparation()
505 {
506 return m_separation;
507 }
508
509 wxString wxToolBar::GetToolLongHelp(int toolIndex)
510 {
511 wxNode *node = m_tools.First();
512 while (node)
513 {
514 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
515 if (tool->m_index == toolIndex)
516 {
517 return tool->m_longHelpString;
518 }
519 node = node->Next();
520 }
521
522 wxFAIL_MSG( wxT("wrong toolbar index") );
523
524 return wxT("");
525 }
526
527 wxString wxToolBar::GetToolShortHelp(int toolIndex)
528 {
529 wxNode *node = m_tools.First();
530 while (node)
531 {
532 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
533 if (tool->m_index == toolIndex)
534 {
535 return tool->m_shortHelpString;
536 }
537 node = node->Next();
538 }
539
540 wxFAIL_MSG( wxT("wrong toolbar index") );
541
542 return wxT("");
543 }
544
545 void wxToolBar::SetToolLongHelp(int toolIndex, const wxString& helpString)
546 {
547 wxNode *node = m_tools.First();
548 while (node)
549 {
550 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
551 if (tool->m_index == toolIndex)
552 {
553 tool->m_longHelpString = helpString;
554 return;
555 }
556 node = node->Next();
557 }
558
559 wxFAIL_MSG( wxT("wrong toolbar index") );
560
561 return;
562 }
563
564 void wxToolBar::SetToolShortHelp(int toolIndex, const wxString& helpString)
565 {
566 wxNode *node = m_tools.First();
567 while (node)
568 {
569 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
570 if (tool->m_index == toolIndex)
571 {
572 tool->m_shortHelpString = helpString;
573 return;
574 }
575 node = node->Next();
576 }
577
578 wxFAIL_MSG( wxT("wrong toolbar index") );
579
580 return;
581 }
582
583 void wxToolBar::OnIdle( wxIdleEvent &WXUNUSED(ievent) )
584 {
585 wxEvtHandler* evtHandler = GetEventHandler();
586
587 wxNode* node = m_tools.First();
588 while (node)
589 {
590 wxToolBarTool* tool = (wxToolBarTool*) node->Data();
591
592 wxUpdateUIEvent event( tool->m_index );
593 event.SetEventObject(this);
594
595 if (evtHandler->ProcessEvent( event ))
596 {
597 if (event.GetSetEnabled())
598 EnableTool(tool->m_index, event.GetEnabled());
599 if (event.GetSetChecked())
600 ToggleTool(tool->m_index, event.GetChecked());
601 /*
602 if (event.GetSetText())
603 // Set tooltip?
604 */
605 }
606
607 node = node->Next();
608 }
609 }
610
611 void wxToolBar::OnInternalIdle()
612 {
613 wxCursor cursor = m_cursor;
614 if (g_globalCursor.Ok()) cursor = g_globalCursor;
615
616 if (cursor.Ok())
617 {
618 /* I now set the cursor the anew in every OnInternalIdle call
619 as setting the cursor in a parent window also effects the
620 windows above so that checking for the current cursor is
621 not possible. */
622
623 if (HasFlag(wxTB_DOCKABLE))
624 {
625 /* if the toolbar is dockable, then m_widget stands for the
626 GtkHandleBox widget, which uses its own window so that we
627 can set the cursor for it. if the toolbar is not dockable,
628 m_widget comes from m_toolbar which uses its parent's
629 window ("windowless windows") and thus we cannot set the
630 cursor. */
631 gdk_window_set_cursor( m_widget->window, cursor.GetCursor() );
632 }
633
634 wxNode *node = m_tools.First();
635 while (node)
636 {
637 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
638 if (!tool->m_item->window)
639 break;
640 else
641 gdk_window_set_cursor( tool->m_item->window, cursor.GetCursor() );
642
643 node = node->Next();
644 }
645 }
646
647 UpdateWindowUI();
648 }
649
650 #endif