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