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