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