]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/tbargtk.cpp
added virtual wxFrame::FindItemInMenuBar(): overriding it in wxMDIParentFrame allows...
[wxWidgets.git] / src / gtk / tbargtk.cpp
... / ...
CommitLineData
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
44extern bool g_blockEventsOnDrag;
45extern wxCursor g_globalCursor;
46
47// ----------------------------------------------------------------------------
48// private functions
49// ----------------------------------------------------------------------------
50
51// translate wxWidgets toolbar style flags to GTK orientation and style
52static 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
76class wxToolBarTool : public wxToolBarToolBase
77{
78public:
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
138protected:
139 void Init();
140};
141
142// ----------------------------------------------------------------------------
143// wxWin macros
144// ----------------------------------------------------------------------------
145
146IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
147
148// ============================================================================
149// implementation
150// ============================================================================
151
152//-----------------------------------------------------------------------------
153// "clicked" (internal from gtk_toolbar)
154//-----------------------------------------------------------------------------
155
156extern "C" {
157static 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// "right-click"
192//-----------------------------------------------------------------------------
193extern "C" {
194static gboolean gtk_toolbar_tool_rclick_callback(GtkWidget *WXUNUSED(widget),
195 GdkEventButton *event,
196 wxToolBarToolBase *tool)
197{
198 if (event->button != 3)
199 return FALSE;
200
201 wxToolBar *tbar = (wxToolBar *)tool->GetToolBar();
202
203 if (tbar->m_blockEvent) return TRUE;
204
205 if (g_blockEventsOnDrag) return TRUE;
206 if (!tool->IsEnabled()) return TRUE;
207
208 tbar->OnRightClick( tool->GetId(), (int)event->x, (int)event->y );
209
210 return TRUE;
211}
212}
213
214//-----------------------------------------------------------------------------
215// "enter_notify_event" / "leave_notify_event"
216//-----------------------------------------------------------------------------
217
218extern "C" {
219static gint gtk_toolbar_tool_callback( GtkWidget *WXUNUSED(widget),
220 GdkEventCrossing *gdk_event,
221 wxToolBarTool *tool )
222{
223 if (g_blockEventsOnDrag) return TRUE;
224
225 wxToolBar *tb = (wxToolBar *)tool->GetToolBar();
226
227 // emit the event
228 if( gdk_event->type == GDK_ENTER_NOTIFY )
229 tb->OnMouseEnter( tool->GetId() );
230 else
231 tb->OnMouseEnter( -1 );
232
233 return FALSE;
234}
235}
236
237extern "C" {
238static
239void gtktoolwidget_size_callback( GtkWidget *widget,
240 GtkAllocation *alloc,
241 wxWindow *win )
242{
243 // this shouldn't happen...
244 if (win->GetParent()->m_wxwindow) return;
245
246 wxSize size = win->GetEffectiveMinSize();
247 if (size.y != alloc->height)
248 {
249 GtkAllocation alloc2;
250 alloc2.x = alloc->x;
251 alloc2.y = (alloc->height - size.y + 3) / 2;
252 alloc2.width = alloc->width;
253 alloc2.height = size.y;
254 gtk_widget_size_allocate( widget, &alloc2 );
255 }
256}
257}
258//-----------------------------------------------------------------------------
259// InsertChild callback for wxToolBar
260//-----------------------------------------------------------------------------
261
262static void wxInsertChildInToolBar( wxToolBar* WXUNUSED(parent),
263 wxWindow* WXUNUSED(child) )
264{
265 // we don't do anything here
266}
267
268// ----------------------------------------------------------------------------
269// wxToolBarTool
270// ----------------------------------------------------------------------------
271
272void wxToolBarTool::Init()
273{
274 m_item =
275 m_image = NULL;
276}
277
278wxToolBarToolBase *wxToolBar::CreateTool(int id,
279 const wxString& text,
280 const wxBitmap& bitmap1,
281 const wxBitmap& bitmap2,
282 wxItemKind kind,
283 wxObject *clientData,
284 const wxString& shortHelpString,
285 const wxString& longHelpString)
286{
287 return new wxToolBarTool(this, id, text, bitmap1, bitmap2, kind,
288 clientData, shortHelpString, longHelpString);
289}
290
291wxToolBarToolBase *
292wxToolBar::CreateTool(wxControl *control, const wxString& label)
293{
294 return new wxToolBarTool(this, control, label);
295}
296
297//-----------------------------------------------------------------------------
298// wxToolBar construction
299//-----------------------------------------------------------------------------
300
301void wxToolBar::Init()
302{
303 m_toolbar = (GtkToolbar *)NULL;
304 m_blockEvent = false;
305 m_defaultWidth = 32;
306 m_defaultHeight = 32;
307}
308
309wxToolBar::~wxToolBar()
310{
311}
312
313bool wxToolBar::Create( wxWindow *parent,
314 wxWindowID id,
315 const wxPoint& pos,
316 const wxSize& size,
317 long style,
318 const wxString& name )
319{
320 m_insertCallback = (wxInsertChildFunction)wxInsertChildInToolBar;
321
322 if ( !PreCreation( parent, pos, size ) ||
323 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
324 {
325 wxFAIL_MSG( wxT("wxToolBar creation failed") );
326
327 return false;
328 }
329
330 FixupStyle();
331
332 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new() );
333 GtkSetStyle();
334
335 // Doesn't work this way.
336 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
337 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
338
339 SetToolSeparation(7);
340
341 if (style & wxTB_DOCKABLE)
342 {
343 m_widget = gtk_handle_box_new();
344 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
345 gtk_widget_show( GTK_WIDGET(m_toolbar) );
346
347 if (style & wxTB_FLAT)
348 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget), GTK_SHADOW_NONE );
349 }
350 else
351 {
352 m_widget = gtk_event_box_new();
353 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
354 ConnectWidget( m_widget );
355 gtk_widget_show(GTK_WIDGET(m_toolbar));
356 }
357
358 // FIXME: there is no such function for toolbars in 2.0
359#if 0
360 if (style & wxTB_FLAT)
361 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar), GTK_RELIEF_NONE );
362#endif
363
364 m_parent->DoAddChild( this );
365
366 PostCreation(size);
367
368 return true;
369}
370
371GdkWindow *wxToolBar::GTKGetWindow(wxArrayGdkWindows& windows) const
372{
373 return GTK_WIDGET(m_toolbar)->window;
374}
375
376void wxToolBar::GtkSetStyle()
377{
378 GtkOrientation orient;
379 GtkToolbarStyle style;
380 GetGtkStyle(GetWindowStyle(), &orient, &style);
381
382 gtk_toolbar_set_orientation(m_toolbar, orient);
383 gtk_toolbar_set_style(m_toolbar, style);
384 gtk_toolbar_set_tooltips(m_toolbar, !(style & wxTB_NO_TOOLTIPS));
385}
386
387void wxToolBar::SetWindowStyleFlag( long style )
388{
389 wxToolBarBase::SetWindowStyleFlag(style);
390
391 if ( m_toolbar )
392 GtkSetStyle();
393}
394
395bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
396{
397 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase);
398
399 if ( tool->IsButton() )
400 {
401 if ( !HasFlag(wxTB_NOICONS) )
402 {
403 wxBitmap bitmap = tool->GetNormalBitmap();
404
405 wxCHECK_MSG( bitmap.Ok(), false,
406 wxT("invalid bitmap for wxToolBar icon") );
407
408 tool->m_image = gtk_image_new();
409 tool->SetImage(bitmap);
410
411 gtk_misc_set_alignment((GtkMisc*)tool->m_image, 0.5, 0.5);
412 }
413 }
414
415 const int posGtk = int(pos);
416
417 switch ( tool->GetStyle() )
418 {
419 case wxTOOL_STYLE_BUTTON:
420 // for a radio button we need the widget which starts the radio
421 // group it belongs to, i.e. the first radio button immediately
422 // preceding this one
423 {
424 GtkWidget *widget = NULL;
425
426 if ( tool->IsRadio() )
427 {
428 wxToolBarToolsList::compatibility_iterator node
429 = wxToolBarToolsList::compatibility_iterator();
430 if ( pos )
431 node = m_tools.Item(pos - 1);
432
433 while ( node )
434 {
435 wxToolBarTool *toolNext = (wxToolBarTool *)node->GetData();
436 if ( !toolNext->IsRadio() )
437 break;
438
439 widget = toolNext->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_image,
466 (GtkSignalFunc)gtk_toolbar_callback,
467 (gpointer)tool,
468 posGtk
469 );
470
471 wxCHECK_MSG(tool->m_item != NULL, false, _T("gtk_toolbar_insert_element() failed"));
472
473 g_signal_connect (tool->m_item, "enter_notify_event",
474 G_CALLBACK (gtk_toolbar_tool_callback),
475 tool);
476 g_signal_connect (tool->m_item, "leave_notify_event",
477 G_CALLBACK (gtk_toolbar_tool_callback),
478 tool);
479 g_signal_connect(tool->m_item, "button-press-event",
480 G_CALLBACK (gtk_toolbar_tool_rclick_callback),
481 tool);
482 }
483 break;
484
485 case wxTOOL_STYLE_SEPARATOR:
486 gtk_toolbar_insert_space( m_toolbar, posGtk );
487
488 // skip the rest
489 return true;
490
491 case wxTOOL_STYLE_CONTROL:
492 gtk_toolbar_insert_widget(
493 m_toolbar,
494 tool->GetControl()->m_widget,
495 (const char *) NULL,
496 (const char *) NULL,
497 posGtk
498 );
499
500 // connect after in order to correct size_allocate events
501 g_signal_connect_after (tool->GetControl()->m_widget, "size_allocate",
502 G_CALLBACK (gtktoolwidget_size_callback), tool->GetControl());
503
504 break;
505 }
506
507 GtkRequisition req;
508 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
509 (m_widget, &req );
510 m_width = req.width + m_xMargin;
511 m_height = req.height + 2*m_yMargin;
512 InvalidateBestSize();
513
514 return true;
515}
516
517bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *toolBase)
518{
519 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase);
520
521 switch ( tool->GetStyle() )
522 {
523 case wxTOOL_STYLE_CONTROL:
524 tool->GetControl()->Destroy();
525 break;
526
527 case wxTOOL_STYLE_BUTTON:
528 gtk_widget_destroy( tool->m_item );
529 break;
530
531 case wxTOOL_STYLE_SEPARATOR:
532 gtk_toolbar_remove_space( m_toolbar, pos );
533 break;
534 }
535
536 InvalidateBestSize();
537 return true;
538}
539
540// ----------------------------------------------------------------------------
541// wxToolBar tools state
542// ----------------------------------------------------------------------------
543
544void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable)
545{
546 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase);
547
548 if (tool->m_item)
549 {
550 gtk_widget_set_sensitive( tool->m_item, enable );
551 }
552}
553
554void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle )
555{
556 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase);
557
558 GtkWidget *item = tool->m_item;
559 if ( item && GTK_IS_TOGGLE_BUTTON(item) )
560 {
561 tool->SetImage(tool->GetBitmap());
562
563 m_blockEvent = true;
564
565 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item), toggle );
566
567 m_blockEvent = false;
568 }
569}
570
571void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
572 bool WXUNUSED(toggle))
573{
574 // VZ: absolutely no idea about how to do it
575 wxFAIL_MSG( _T("not implemented") );
576}
577
578// ----------------------------------------------------------------------------
579// wxToolBar geometry
580// ----------------------------------------------------------------------------
581
582wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x),
583 wxCoord WXUNUSED(y)) const
584{
585 // VZ: GTK+ doesn't seem to have such thing
586 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
587
588 return (wxToolBarToolBase *)NULL;
589}
590
591void wxToolBar::SetMargins( int x, int y )
592{
593 wxCHECK_RET( GetToolsCount() == 0,
594 wxT("wxToolBar::SetMargins must be called before adding tools.") );
595
596 m_xMargin = x;
597 m_yMargin = y;
598}
599
600void wxToolBar::SetToolSeparation( int separation )
601{
602 // FIXME: this function disappeared
603#if 0
604 gtk_toolbar_set_space_size( m_toolbar, separation );
605#endif
606
607 m_toolSeparation = separation;
608}
609
610void wxToolBar::SetToolShortHelp( int id, const wxString& helpString )
611{
612 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
613
614 if ( tool )
615 {
616 (void)tool->SetShortHelp(helpString);
617 gtk_tooltips_set_tip(m_toolbar->tooltips, tool->m_item,
618 wxGTK_CONV( helpString ), "");
619 }
620}
621
622void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
623{
624 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
625 if ( tool )
626 {
627 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
628
629 tool->SetNormalBitmap(bitmap);
630 tool->SetImage(tool->GetBitmap());
631 }
632}
633
634void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
635{
636 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
637 if ( tool )
638 {
639 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
640
641 tool->SetDisabledBitmap(bitmap);
642 tool->SetImage(tool->GetBitmap());
643 }
644}
645
646// ----------------------------------------------------------------------------
647// wxToolBar idle handling
648// ----------------------------------------------------------------------------
649
650void wxToolBar::OnInternalIdle()
651{
652 // Check if we have to show window now
653 if (GtkShowFromOnIdle()) return;
654
655 wxCursor cursor = m_cursor;
656 if (g_globalCursor.Ok()) cursor = g_globalCursor;
657
658 if (cursor.Ok())
659 {
660 /* I now set the cursor the anew in every OnInternalIdle call
661 as setting the cursor in a parent window also effects the
662 windows above so that checking for the current cursor is
663 not possible. */
664
665 if (HasFlag(wxTB_DOCKABLE) && (m_widget->window))
666 {
667 /* if the toolbar is dockable, then m_widget stands for the
668 GtkHandleBox widget, which uses its own window so that we
669 can set the cursor for it. if the toolbar is not dockable,
670 m_widget comes from m_toolbar which uses its parent's
671 window ("windowless windows") and thus we cannot set the
672 cursor. */
673 gdk_window_set_cursor( m_widget->window, cursor.GetCursor() );
674 }
675
676 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
677 while ( node )
678 {
679 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
680 node = node->GetNext();
681
682 GtkWidget *item = tool->m_item;
683 if ( item )
684 {
685 GdkWindow *window = item->window;
686
687 if ( window )
688 {
689 gdk_window_set_cursor( window, cursor.GetCursor() );
690 }
691 }
692 }
693 }
694
695 if (wxUpdateUIEvent::CanUpdate(this))
696 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
697}
698
699
700// ----------------------------------------------------------------------------
701
702// static
703wxVisualAttributes
704wxToolBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
705{
706 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new);
707}
708
709#endif // wxUSE_TOOLBAR_NATIVE