]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/tbargtk.cpp
wxDefaultSize.* and wxDefaultPosition.* to wxDefaultCoord.
[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 #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_toolbar = (GtkToolbar *)NULL;
273 m_blockEvent = FALSE;
274 m_defaultWidth = 32;
275 m_defaultHeight = 32;
276 }
277
278 wxToolBar::~wxToolBar()
279 {
280 }
281
282 bool wxToolBar::Create( wxWindow *parent,
283 wxWindowID id,
284 const wxPoint& pos,
285 const wxSize& size,
286 long style,
287 const wxString& name )
288 {
289 m_needParent = TRUE;
290 m_insertCallback = (wxInsertChildFunction)wxInsertChildInToolBar;
291
292 if ( !PreCreation( parent, pos, size ) ||
293 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
294 {
295 wxFAIL_MSG( wxT("wxToolBar creation failed") );
296
297 return FALSE;
298 }
299
300 #ifdef __WXGTK20__
301 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new() );
302 GtkSetStyle();
303
304 // Doesn't work this way.
305 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
306 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
307 #else
308 GtkOrientation orient;
309 GtkToolbarStyle gtkStyle;
310 GetGtkStyle(style, &orient, &gtkStyle);
311
312 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new(orient, gtkStyle) );
313 #endif
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 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
335
336 // FIXME: there is no such function for toolbars in 2.0
337 #ifndef __WXGTK20__
338 if (style & wxTB_FLAT)
339 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar), GTK_RELIEF_NONE );
340 #endif
341
342 m_parent->DoAddChild( this );
343
344 PostCreation(size);
345
346 return TRUE;
347 }
348
349 void wxToolBar::GtkSetStyle()
350 {
351 GtkOrientation orient;
352 GtkToolbarStyle style;
353 GetGtkStyle(GetWindowStyle(), &orient, &style);
354
355 gtk_toolbar_set_orientation(m_toolbar, orient);
356 gtk_toolbar_set_style(m_toolbar, style);
357 }
358
359 void wxToolBar::SetWindowStyleFlag( long style )
360 {
361 wxToolBarBase::SetWindowStyleFlag(style);
362
363 if ( m_toolbar )
364 GtkSetStyle();
365 }
366
367 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
368 {
369 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
370
371 #ifndef __WXGTK20__
372 // if we have inserted a space before all the tools we must change the GTK
373 // index by 1
374 size_t posGtk = m_xMargin > 1 ? pos + 1 : pos;
375 #else
376 size_t posGtk = pos;
377 #endif
378
379 if ( tool->IsButton() )
380 {
381 if ( !HasFlag(wxTB_NOICONS) )
382 {
383 wxBitmap bitmap = tool->GetNormalBitmap();
384
385 wxCHECK_MSG( bitmap.Ok(), FALSE,
386 wxT("invalid bitmap for wxToolBar icon") );
387
388 wxCHECK_MSG( bitmap.GetBitmap() == NULL, FALSE,
389 wxT("wxToolBar doesn't support GdkBitmap") );
390
391 wxCHECK_MSG( bitmap.GetPixmap() != NULL, FALSE,
392 wxT("wxToolBar::Add needs a wxBitmap") );
393
394 GtkWidget *tool_pixmap = (GtkWidget *)NULL;
395
396 GdkPixmap *pixmap = bitmap.GetPixmap();
397
398 GdkBitmap *mask = (GdkBitmap *)NULL;
399 if ( bitmap.GetMask() )
400 mask = bitmap.GetMask()->GetBitmap();
401
402 tool_pixmap = gtk_pixmap_new( pixmap, mask );
403 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap), TRUE );
404
405 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
406
407 tool->m_pixmap = tool_pixmap;
408 }
409 }
410
411 switch ( tool->GetStyle() )
412 {
413 case wxTOOL_STYLE_BUTTON:
414 // for a radio button we need the widget which starts the radio
415 // group it belongs to, i.e. the first radio button immediately
416 // preceding this one
417 {
418 GtkWidget *widget = NULL;
419
420 if ( tool->IsRadio() )
421 {
422 wxToolBarToolsList::compatibility_iterator node
423 = wxToolBarToolsList::compatibility_iterator();
424 if ( pos ) node = m_tools.Item(pos - 1);
425
426 while ( node )
427 {
428 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
429 if ( !tool->IsRadio() )
430 break;
431
432 widget = tool->m_item;
433
434 node = node->GetPrevious();
435 }
436
437 if ( !widget )
438 {
439 // this is the first button in the radio button group,
440 // it will be toggled automatically by GTK so bring the
441 // internal flag in sync
442 tool->Toggle(TRUE);
443 }
444 }
445
446 tool->m_item = gtk_toolbar_insert_element
447 (
448 m_toolbar,
449 tool->GetGtkChildType(),
450 widget,
451 tool->GetLabel().empty()
452 ? NULL
453 : (const char*) wxGTK_CONV( tool->GetLabel() ),
454 tool->GetShortHelp().empty()
455 ? NULL
456 : (const char*) wxGTK_CONV( tool->GetShortHelp() ),
457 "", // tooltip_private_text (?)
458 tool->m_pixmap,
459 (GtkSignalFunc)gtk_toolbar_callback,
460 (gpointer)tool,
461 posGtk
462 );
463
464 if ( !tool->m_item )
465 {
466 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
467
468 return FALSE;
469 }
470
471 gtk_signal_connect( GTK_OBJECT(tool->m_item),
472 "enter_notify_event",
473 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback),
474 (gpointer)tool );
475 gtk_signal_connect( GTK_OBJECT(tool->m_item),
476 "leave_notify_event",
477 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback),
478 (gpointer)tool );
479 }
480 break;
481
482 case wxTOOL_STYLE_SEPARATOR:
483 gtk_toolbar_insert_space( m_toolbar, posGtk );
484
485 // skip the rest
486 return TRUE;
487
488 case wxTOOL_STYLE_CONTROL:
489 gtk_toolbar_insert_widget(
490 m_toolbar,
491 tool->GetControl()->m_widget,
492 (const char *) NULL,
493 (const char *) NULL,
494 posGtk
495 );
496 break;
497 }
498
499 GtkRequisition req;
500 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
501 (m_widget, &req );
502 m_width = req.width + m_xMargin;
503 m_height = req.height + 2*m_yMargin;
504 InvalidateBestSize();
505
506 return TRUE;
507 }
508
509 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
510 {
511 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
512
513 switch ( tool->GetStyle() )
514 {
515 case wxTOOL_STYLE_CONTROL:
516 tool->GetControl()->Destroy();
517 break;
518
519 case wxTOOL_STYLE_BUTTON:
520 gtk_widget_destroy( tool->m_item );
521 break;
522
523 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
524 }
525
526 InvalidateBestSize();
527 return TRUE;
528 }
529
530 // ----------------------------------------------------------------------------
531 // wxToolBar tools state
532 // ----------------------------------------------------------------------------
533
534 void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable)
535 {
536 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
537
538 if (tool->m_item)
539 {
540 gtk_widget_set_sensitive( tool->m_item, enable );
541 }
542 }
543
544 void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle )
545 {
546 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
547
548 GtkWidget *item = tool->m_item;
549 if ( item && GTK_IS_TOGGLE_BUTTON(item) )
550 {
551 wxBitmap bitmap = tool->GetBitmap();
552 if ( bitmap.Ok() )
553 {
554 GtkPixmap *pixmap = GTK_PIXMAP( tool->m_pixmap );
555
556 GdkBitmap *mask = bitmap.GetMask() ? bitmap.GetMask()->GetBitmap()
557 : (GdkBitmap *)NULL;
558
559 gtk_pixmap_set( pixmap, bitmap.GetPixmap(), mask );
560 }
561
562 m_blockEvent = TRUE;
563
564 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item), toggle );
565
566 m_blockEvent = FALSE;
567 }
568 }
569
570 void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
571 bool WXUNUSED(toggle))
572 {
573 // VZ: absolutely no idea about how to do it
574 wxFAIL_MSG( _T("not implemented") );
575 }
576
577 // ----------------------------------------------------------------------------
578 // wxToolBar geometry
579 // ----------------------------------------------------------------------------
580
581 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x),
582 wxCoord WXUNUSED(y)) const
583 {
584 // VZ: GTK+ doesn't seem to have such thing
585 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
586
587 return (wxToolBarToolBase *)NULL;
588 }
589
590 void wxToolBar::SetMargins( int x, int y )
591 {
592 wxCHECK_RET( GetToolsCount() == 0,
593 wxT("wxToolBar::SetMargins must be called before adding tools.") );
594
595 #ifndef __WXGTK20__
596 if (x > 1)
597 gtk_toolbar_append_space( m_toolbar ); // oh well
598 #endif
599
600 m_xMargin = x;
601 m_yMargin = y;
602 }
603
604 void wxToolBar::SetToolSeparation( int separation )
605 {
606 // FIXME: this function disappeared
607 #ifndef __WXGTK20__
608 gtk_toolbar_set_space_size( m_toolbar, separation );
609 #endif
610
611 m_toolSeparation = separation;
612 }
613
614 void wxToolBar::SetToolShortHelp( int id, const wxString& helpString )
615 {
616 wxToolBarTool *tool = (wxToolBarTool *)FindById(id);
617
618 if ( tool )
619 {
620 (void)tool->SetShortHelp(helpString);
621 gtk_tooltips_set_tip(m_toolbar->tooltips, tool->m_item,
622 wxGTK_CONV( helpString ), "");
623 }
624 }
625
626 // ----------------------------------------------------------------------------
627 // wxToolBar idle handling
628 // ----------------------------------------------------------------------------
629
630 void wxToolBar::OnInternalIdle()
631 {
632 wxCursor cursor = m_cursor;
633 if (g_globalCursor.Ok()) cursor = g_globalCursor;
634
635 if (cursor.Ok())
636 {
637 /* I now set the cursor the anew in every OnInternalIdle call
638 as setting the cursor in a parent window also effects the
639 windows above so that checking for the current cursor is
640 not possible. */
641
642 if (HasFlag(wxTB_DOCKABLE) && (m_widget->window))
643 {
644 /* if the toolbar is dockable, then m_widget stands for the
645 GtkHandleBox widget, which uses its own window so that we
646 can set the cursor for it. if the toolbar is not dockable,
647 m_widget comes from m_toolbar which uses its parent's
648 window ("windowless windows") and thus we cannot set the
649 cursor. */
650 gdk_window_set_cursor( m_widget->window, cursor.GetCursor() );
651 }
652
653 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
654 while ( node )
655 {
656 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
657 node = node->GetNext();
658
659 GtkWidget *item = tool->m_item;
660 if ( item )
661 {
662 GdkWindow *window = item->window;
663
664 if ( window )
665 {
666 gdk_window_set_cursor( window, cursor.GetCursor() );
667 }
668 }
669 }
670 }
671
672 if (wxUpdateUIEvent::CanUpdate(this))
673 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
674 }
675
676
677 // ----------------------------------------------------------------------------
678
679 // static
680 wxVisualAttributes
681 wxToolBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
682 {
683 #ifdef __WXGTK20__
684 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new);
685 #else
686 wxVisualAttributes attr;
687 GtkWidget* widget = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL, GTK_TOOLBAR_BOTH);
688 attr = GetDefaultAttributesFromGTKWidget(widget);
689 gtk_widget_destroy(widget);
690 return attr;
691 #endif
692 }
693
694 #endif // wxUSE_TOOLBAR_NATIVE