]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/tbargtk.cpp
added missing consts and pass objects by const reference instead of by value (patch...
[wxWidgets.git] / src / gtk1 / tbargtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tbargtk.cpp
3 // Purpose: GTK toolbar
4 // Author: Robert Roebling
5 // Modified: 13.12.99 by VZ to derive from wxToolBarBase
6 // RCS-ID: $Id$
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #include "wx/toolbar.h"
23
24 #if wxUSE_TOOLBAR_NATIVE
25
26 #include "wx/frame.h"
27
28 #include <glib.h>
29 #include "wx/gtk/private.h"
30
31 // ----------------------------------------------------------------------------
32 // globals
33 // ----------------------------------------------------------------------------
34
35 // idle system
36 extern void wxapp_install_idle_handler();
37 extern bool g_isIdle;
38
39 // data
40 extern bool g_blockEventsOnDrag;
41 extern wxCursor g_globalCursor;
42
43 // ----------------------------------------------------------------------------
44 // private functions
45 // ----------------------------------------------------------------------------
46
47 // translate wxWidgets toolbar style flags to GTK orientation and style
48 static void GetGtkStyle(long style,
49 GtkOrientation *orient, GtkToolbarStyle *gtkStyle)
50 {
51 *orient = style & wxTB_VERTICAL ? GTK_ORIENTATION_VERTICAL
52 : GTK_ORIENTATION_HORIZONTAL;
53
54
55 if ( style & wxTB_TEXT )
56 {
57 *gtkStyle = style & wxTB_NOICONS
58 ? GTK_TOOLBAR_TEXT
59 : (
60 #ifdef __WXGTK20__
61 style & wxTB_HORZ_LAYOUT ? GTK_TOOLBAR_BOTH_HORIZ :
62 #endif // __WXGTK20__
63 GTK_TOOLBAR_BOTH);
64 }
65 else // no text, hence we must have the icons or what would we show?
66 {
67 *gtkStyle = GTK_TOOLBAR_ICONS;
68 }
69 }
70
71 // ----------------------------------------------------------------------------
72 // wxToolBarTool
73 // ----------------------------------------------------------------------------
74
75 class wxToolBarTool : public wxToolBarToolBase
76 {
77 public:
78 wxToolBarTool(wxToolBar *tbar,
79 int id,
80 const wxString& label,
81 const wxBitmap& bitmap1,
82 const wxBitmap& bitmap2,
83 wxItemKind kind,
84 wxObject *clientData,
85 const wxString& shortHelpString,
86 const wxString& longHelpString)
87 : wxToolBarToolBase(tbar, id, label, bitmap1, bitmap2, kind,
88 clientData, shortHelpString, longHelpString)
89 {
90 Init();
91 }
92
93 wxToolBarTool(wxToolBar *tbar, wxControl *control)
94 : wxToolBarToolBase(tbar, control)
95 {
96 Init();
97 }
98
99 // is this a radio button?
100 //
101 // unlike GetKind(), can be called for any kind of tools, not just buttons
102 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO; }
103
104 // this is only called for the normal buttons, i.e. not separators nor
105 // controls
106 GtkToolbarChildType GetGtkChildType() const
107 {
108 switch ( GetKind() )
109 {
110 case wxITEM_CHECK:
111 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON;
112
113 case wxITEM_RADIO:
114 return GTK_TOOLBAR_CHILD_RADIOBUTTON;
115
116 default:
117 wxFAIL_MSG( _T("unknown toolbar child type") );
118 // fall through
119
120 case wxITEM_NORMAL:
121 return GTK_TOOLBAR_CHILD_BUTTON;
122 }
123 }
124
125 void SetPixmap(const wxBitmap& bitmap)
126 {
127 if (bitmap.Ok())
128 {
129 GdkBitmap *mask = bitmap.GetMask() ? bitmap.GetMask()->GetBitmap()
130 : (GdkBitmap *)NULL;
131 #ifdef __WXGTK20__
132 if (bitmap.HasPixbuf())
133 gtk_image_set_from_pixbuf( GTK_IMAGE(m_pixmap), bitmap.GetPixbuf() );
134 else
135 #endif // !__WXGTK20__
136 gtk_pixmap_set( GTK_PIXMAP(m_pixmap), bitmap.GetPixmap(), mask );
137 }
138 }
139
140 GtkWidget *m_item;
141 GtkWidget *m_pixmap;
142
143 protected:
144 void Init();
145 };
146
147 // ----------------------------------------------------------------------------
148 // wxWin macros
149 // ----------------------------------------------------------------------------
150
151 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
152
153 // ============================================================================
154 // implementation
155 // ============================================================================
156
157 //-----------------------------------------------------------------------------
158 // "clicked" (internal from gtk_toolbar)
159 //-----------------------------------------------------------------------------
160
161 extern "C" {
162 static void gtk_toolbar_callback( GtkWidget *WXUNUSED(widget),
163 wxToolBarTool *tool )
164 {
165 if (g_isIdle)
166 wxapp_install_idle_handler();
167
168 wxToolBar *tbar = (wxToolBar *)tool->GetToolBar();
169
170 if (tbar->m_blockEvent) return;
171
172 if (g_blockEventsOnDrag) return;
173 if (!tool->IsEnabled()) return;
174
175 if (tool->CanBeToggled())
176 {
177 tool->Toggle();
178
179 tool->SetPixmap(tool->GetBitmap());
180
181 if ( tool->IsRadio() && !tool->IsToggled() )
182 {
183 // radio button went up, don't report this as a wxWin event
184 return;
185 }
186 }
187
188 if( !tbar->OnLeftClick( tool->GetId(), tool->IsToggled() ) && tool->CanBeToggled() )
189 {
190 // revert back
191 tool->Toggle();
192
193 tool->SetPixmap(tool->GetBitmap());
194 }
195 }
196 }
197
198 //-----------------------------------------------------------------------------
199 // "enter_notify_event" / "leave_notify_event"
200 //-----------------------------------------------------------------------------
201
202 extern "C" {
203 static gint gtk_toolbar_tool_callback( GtkWidget *WXUNUSED(widget),
204 GdkEventCrossing *gdk_event,
205 wxToolBarTool *tool )
206 {
207 if (g_isIdle) wxapp_install_idle_handler();
208
209 if (g_blockEventsOnDrag) return TRUE;
210
211 wxToolBar *tb = (wxToolBar *)tool->GetToolBar();
212
213 // emit the event
214 if( gdk_event->type == GDK_ENTER_NOTIFY )
215 tb->OnMouseEnter( tool->GetId() );
216 else
217 tb->OnMouseEnter( -1 );
218
219 return FALSE;
220 }
221 }
222
223 //-----------------------------------------------------------------------------
224 // InsertChild callback for wxToolBar
225 //-----------------------------------------------------------------------------
226
227 static void wxInsertChildInToolBar( wxToolBar* WXUNUSED(parent),
228 wxWindow* WXUNUSED(child) )
229 {
230 // we don't do anything here
231 }
232
233 // ----------------------------------------------------------------------------
234 // wxToolBarTool
235 // ----------------------------------------------------------------------------
236
237 void wxToolBarTool::Init()
238 {
239 m_item =
240 m_pixmap = (GtkWidget *)NULL;
241 }
242
243 wxToolBarToolBase *wxToolBar::CreateTool(int id,
244 const wxString& text,
245 const wxBitmap& bitmap1,
246 const wxBitmap& bitmap2,
247 wxItemKind kind,
248 wxObject *clientData,
249 const wxString& shortHelpString,
250 const wxString& longHelpString)
251 {
252 return new wxToolBarTool(this, id, text, bitmap1, bitmap2, kind,
253 clientData, shortHelpString, longHelpString);
254 }
255
256 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
257 {
258 return new wxToolBarTool(this, control);
259 }
260
261 //-----------------------------------------------------------------------------
262 // wxToolBar construction
263 //-----------------------------------------------------------------------------
264
265 void wxToolBar::Init()
266 {
267 m_toolbar = (GtkToolbar *)NULL;
268 m_blockEvent = false;
269 m_defaultWidth = 32;
270 m_defaultHeight = 32;
271 }
272
273 wxToolBar::~wxToolBar()
274 {
275 }
276
277 bool wxToolBar::Create( wxWindow *parent,
278 wxWindowID id,
279 const wxPoint& pos,
280 const wxSize& size,
281 long style,
282 const wxString& name )
283 {
284 m_needParent = true;
285 m_insertCallback = (wxInsertChildFunction)wxInsertChildInToolBar;
286
287 if ( !PreCreation( parent, pos, size ) ||
288 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
289 {
290 wxFAIL_MSG( wxT("wxToolBar creation failed") );
291
292 return false;
293 }
294
295 #ifdef __WXGTK20__
296 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new() );
297 GtkSetStyle();
298
299 // Doesn't work this way.
300 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
301 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
302 #else
303 GtkOrientation orient;
304 GtkToolbarStyle gtkStyle;
305 GetGtkStyle(style, &orient, &gtkStyle);
306
307 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new(orient, gtkStyle) );
308 #endif
309
310 SetToolSeparation(7);
311
312 if (style & wxTB_DOCKABLE)
313 {
314 m_widget = gtk_handle_box_new();
315 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
316 gtk_widget_show( GTK_WIDGET(m_toolbar) );
317
318 if (style & wxTB_FLAT)
319 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget), GTK_SHADOW_NONE );
320 }
321 else
322 {
323 m_widget = gtk_event_box_new();
324 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
325 ConnectWidget( m_widget );
326 gtk_widget_show(GTK_WIDGET(m_toolbar));
327 }
328
329 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
330
331 // FIXME: there is no such function for toolbars in 2.0
332 #ifndef __WXGTK20__
333 if (style & wxTB_FLAT)
334 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar), GTK_RELIEF_NONE );
335 #endif
336
337 m_parent->DoAddChild( this );
338
339 PostCreation(size);
340
341 return true;
342 }
343
344 void wxToolBar::GtkSetStyle()
345 {
346 GtkOrientation orient;
347 GtkToolbarStyle style;
348 GetGtkStyle(GetWindowStyle(), &orient, &style);
349
350 gtk_toolbar_set_orientation(m_toolbar, orient);
351 gtk_toolbar_set_style(m_toolbar, style);
352 }
353
354 void wxToolBar::SetWindowStyleFlag( long style )
355 {
356 wxToolBarBase::SetWindowStyleFlag(style);
357
358 if ( m_toolbar )
359 GtkSetStyle();
360 }
361
362 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
363 {
364 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
365
366 #ifndef __WXGTK20__
367 // if we have inserted a space before all the tools we must change the GTK
368 // index by 1
369 size_t posGtk = m_xMargin > 1 ? pos + 1 : pos;
370 #else
371 size_t posGtk = pos;
372 #endif
373
374 if ( tool->IsButton() )
375 {
376 if ( !HasFlag(wxTB_NOICONS) )
377 {
378 wxBitmap bitmap = tool->GetNormalBitmap();
379
380 wxCHECK_MSG( bitmap.Ok(), false,
381 wxT("invalid bitmap for wxToolBar icon") );
382
383 wxCHECK_MSG( bitmap.GetBitmap() == NULL, false,
384 wxT("wxToolBar doesn't support GdkBitmap") );
385
386 wxCHECK_MSG( bitmap.GetPixmap() != NULL, false,
387 wxT("wxToolBar::Add needs a wxBitmap") );
388
389 GtkWidget *tool_pixmap = (GtkWidget *)NULL;
390
391
392 #ifdef __WXGTK20__
393 if (bitmap.HasPixbuf())
394 {
395 tool_pixmap = gtk_image_new();
396 tool->m_pixmap = tool_pixmap;
397 tool->SetPixmap(bitmap);
398 }
399 else
400 #endif
401 {
402 GdkPixmap *pixmap = bitmap.GetPixmap();
403
404 GdkBitmap *mask = (GdkBitmap *)NULL;
405 if ( bitmap.GetMask() )
406 mask = bitmap.GetMask()->GetBitmap();
407
408 tool_pixmap = gtk_pixmap_new( pixmap, mask );
409 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap), TRUE );
410 }
411
412 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
413
414 tool->m_pixmap = tool_pixmap;
415 }
416 }
417
418 switch ( tool->GetStyle() )
419 {
420 case wxTOOL_STYLE_BUTTON:
421 // for a radio button we need the widget which starts the radio
422 // group it belongs to, i.e. the first radio button immediately
423 // preceding this one
424 {
425 GtkWidget *widget = NULL;
426
427 if ( tool->IsRadio() )
428 {
429 wxToolBarToolsList::compatibility_iterator node
430 = wxToolBarToolsList::compatibility_iterator();
431 if ( pos ) node = m_tools.Item(pos - 1);
432
433 while ( node )
434 {
435 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
436 if ( !tool->IsRadio() )
437 break;
438
439 widget = tool->m_item;
440
441 node = node->GetPrevious();
442 }
443
444 if ( !widget )
445 {
446 // this is the first button in the radio button group,
447 // it will be toggled automatically by GTK so bring the
448 // internal flag in sync
449 tool->Toggle(true);
450 }
451 }
452
453 tool->m_item = gtk_toolbar_insert_element
454 (
455 m_toolbar,
456 tool->GetGtkChildType(),
457 widget,
458 tool->GetLabel().empty()
459 ? NULL
460 : (const char*) wxGTK_CONV( tool->GetLabel() ),
461 tool->GetShortHelp().empty()
462 ? NULL
463 : (const char*) wxGTK_CONV( tool->GetShortHelp() ),
464 "", // tooltip_private_text (?)
465 tool->m_pixmap,
466 (GtkSignalFunc)gtk_toolbar_callback,
467 (gpointer)tool,
468 posGtk
469 );
470
471 if ( !tool->m_item )
472 {
473 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
474
475 return false;
476 }
477
478 gtk_signal_connect( GTK_OBJECT(tool->m_item),
479 "enter_notify_event",
480 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback),
481 (gpointer)tool );
482 gtk_signal_connect( GTK_OBJECT(tool->m_item),
483 "leave_notify_event",
484 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback),
485 (gpointer)tool );
486 }
487 break;
488
489 case wxTOOL_STYLE_SEPARATOR:
490 gtk_toolbar_insert_space( m_toolbar, posGtk );
491
492 // skip the rest
493 return true;
494
495 case wxTOOL_STYLE_CONTROL:
496 gtk_toolbar_insert_widget(
497 m_toolbar,
498 tool->GetControl()->m_widget,
499 (const char *) NULL,
500 (const char *) NULL,
501 posGtk
502 );
503 break;
504 }
505
506 GtkRequisition req;
507 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
508 (m_widget, &req );
509 m_width = req.width + m_xMargin;
510 m_height = req.height + 2*m_yMargin;
511 InvalidateBestSize();
512
513 return true;
514 }
515
516 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *toolBase)
517 {
518 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
519
520 switch ( tool->GetStyle() )
521 {
522 case wxTOOL_STYLE_CONTROL:
523 tool->GetControl()->Destroy();
524 break;
525
526 case wxTOOL_STYLE_BUTTON:
527 gtk_widget_destroy( tool->m_item );
528 break;
529
530 #ifdef __WXGTK20__
531 case wxTOOL_STYLE_SEPARATOR:
532 gtk_toolbar_remove_space( m_toolbar, pos );
533 break;
534 #endif
535 }
536
537 InvalidateBestSize();
538 return true;
539 }
540
541 // ----------------------------------------------------------------------------
542 // wxToolBar tools state
543 // ----------------------------------------------------------------------------
544
545 void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable)
546 {
547 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
548
549 if (tool->m_item)
550 {
551 gtk_widget_set_sensitive( tool->m_item, enable );
552 }
553 }
554
555 void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle )
556 {
557 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
558
559 GtkWidget *item = tool->m_item;
560 if ( item && GTK_IS_TOGGLE_BUTTON(item) )
561 {
562 tool->SetPixmap(tool->GetBitmap());
563
564 m_blockEvent = true;
565
566 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item), toggle );
567
568 m_blockEvent = false;
569 }
570 }
571
572 void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
573 bool WXUNUSED(toggle))
574 {
575 // VZ: absolutely no idea about how to do it
576 wxFAIL_MSG( _T("not implemented") );
577 }
578
579 // ----------------------------------------------------------------------------
580 // wxToolBar geometry
581 // ----------------------------------------------------------------------------
582
583 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x),
584 wxCoord WXUNUSED(y)) const
585 {
586 // VZ: GTK+ doesn't seem to have such thing
587 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
588
589 return (wxToolBarToolBase *)NULL;
590 }
591
592 void wxToolBar::SetMargins( int x, int y )
593 {
594 wxCHECK_RET( GetToolsCount() == 0,
595 wxT("wxToolBar::SetMargins must be called before adding tools.") );
596
597 #ifndef __WXGTK20__
598 if (x > 1)
599 gtk_toolbar_append_space( m_toolbar ); // oh well
600 #endif
601
602 m_xMargin = x;
603 m_yMargin = y;
604 }
605
606 void wxToolBar::SetToolSeparation( int separation )
607 {
608 // FIXME: this function disappeared
609 #ifndef __WXGTK20__
610 gtk_toolbar_set_space_size( m_toolbar, separation );
611 #endif
612
613 m_toolSeparation = separation;
614 }
615
616 void wxToolBar::SetToolShortHelp( int id, const wxString& helpString )
617 {
618 wxToolBarTool *tool = (wxToolBarTool *)FindById(id);
619
620 if ( tool )
621 {
622 (void)tool->SetShortHelp(helpString);
623 gtk_tooltips_set_tip(m_toolbar->tooltips, tool->m_item,
624 wxGTK_CONV( helpString ), "");
625 }
626 }
627
628 // ----------------------------------------------------------------------------
629 // wxToolBar idle handling
630 // ----------------------------------------------------------------------------
631
632 void wxToolBar::OnInternalIdle()
633 {
634 wxCursor cursor = m_cursor;
635 if (g_globalCursor.Ok()) cursor = g_globalCursor;
636
637 if (cursor.Ok())
638 {
639 /* I now set the cursor the anew in every OnInternalIdle call
640 as setting the cursor in a parent window also effects the
641 windows above so that checking for the current cursor is
642 not possible. */
643
644 if (HasFlag(wxTB_DOCKABLE) && (m_widget->window))
645 {
646 /* if the toolbar is dockable, then m_widget stands for the
647 GtkHandleBox widget, which uses its own window so that we
648 can set the cursor for it. if the toolbar is not dockable,
649 m_widget comes from m_toolbar which uses its parent's
650 window ("windowless windows") and thus we cannot set the
651 cursor. */
652 gdk_window_set_cursor( m_widget->window, cursor.GetCursor() );
653 }
654
655 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
656 while ( node )
657 {
658 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
659 node = node->GetNext();
660
661 GtkWidget *item = tool->m_item;
662 if ( item )
663 {
664 GdkWindow *window = item->window;
665
666 if ( window )
667 {
668 gdk_window_set_cursor( window, cursor.GetCursor() );
669 }
670 }
671 }
672 }
673
674 if (wxUpdateUIEvent::CanUpdate(this))
675 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
676 }
677
678
679 // ----------------------------------------------------------------------------
680
681 // static
682 wxVisualAttributes
683 wxToolBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
684 {
685 #ifdef __WXGTK20__
686 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new);
687 #else
688 wxVisualAttributes attr;
689 GtkWidget* widget = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL, GTK_TOOLBAR_BOTH);
690 attr = GetDefaultAttributesFromGTKWidget(widget);
691 gtk_widget_destroy(widget);
692 return attr;
693 #endif
694 }
695
696 #endif // wxUSE_TOOLBAR_NATIVE