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