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