]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/tbargtk.cpp
renamed IsRefTo() to IsSameAs() (do complain if this is not more clear) and
[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 //-----------------------------------------------------------------------------
219 // InsertChild callback for wxToolBar
220 //-----------------------------------------------------------------------------
221
222 static void wxInsertChildInToolBar( wxToolBar* WXUNUSED(parent),
223 wxWindow* WXUNUSED(child) )
224 {
225 // we don't do anything here
226 }
227
228 // ----------------------------------------------------------------------------
229 // wxToolBarTool
230 // ----------------------------------------------------------------------------
231
232 void wxToolBarTool::Init()
233 {
234 m_item =
235 m_image = NULL;
236 }
237
238 wxToolBarToolBase *wxToolBar::CreateTool(int id,
239 const wxString& text,
240 const wxBitmap& bitmap1,
241 const wxBitmap& bitmap2,
242 wxItemKind kind,
243 wxObject *clientData,
244 const wxString& shortHelpString,
245 const wxString& longHelpString)
246 {
247 return new wxToolBarTool(this, id, text, bitmap1, bitmap2, kind,
248 clientData, shortHelpString, longHelpString);
249 }
250
251 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
252 {
253 return new wxToolBarTool(this, control);
254 }
255
256 //-----------------------------------------------------------------------------
257 // wxToolBar construction
258 //-----------------------------------------------------------------------------
259
260 void wxToolBar::Init()
261 {
262 m_toolbar = (GtkToolbar *)NULL;
263 m_blockEvent = false;
264 m_defaultWidth = 32;
265 m_defaultHeight = 32;
266 }
267
268 wxToolBar::~wxToolBar()
269 {
270 }
271
272 bool wxToolBar::Create( wxWindow *parent,
273 wxWindowID id,
274 const wxPoint& pos,
275 const wxSize& size,
276 long style,
277 const wxString& name )
278 {
279 m_needParent = true;
280 m_insertCallback = (wxInsertChildFunction)wxInsertChildInToolBar;
281
282 if ( !PreCreation( parent, pos, size ) ||
283 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
284 {
285 wxFAIL_MSG( wxT("wxToolBar creation failed") );
286
287 return false;
288 }
289
290 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new() );
291 GtkSetStyle();
292
293 // Doesn't work this way.
294 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
295 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
296
297 SetToolSeparation(7);
298
299 if (style & wxTB_DOCKABLE)
300 {
301 m_widget = gtk_handle_box_new();
302 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
303 gtk_widget_show( GTK_WIDGET(m_toolbar) );
304
305 if (style & wxTB_FLAT)
306 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget), GTK_SHADOW_NONE );
307 }
308 else
309 {
310 m_widget = gtk_event_box_new();
311 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
312 ConnectWidget( m_widget );
313 gtk_widget_show(GTK_WIDGET(m_toolbar));
314 }
315
316 // FIXME: there is no such function for toolbars in 2.0
317 #if 0
318 if (style & wxTB_FLAT)
319 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar), GTK_RELIEF_NONE );
320 #endif
321
322 m_parent->DoAddChild( this );
323
324 PostCreation(size);
325
326 return true;
327 }
328
329 GdkWindow *wxToolBar::GTKGetWindow(wxArrayGdkWindows& windows) const
330 {
331 return GTK_WIDGET(m_toolbar)->window;
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(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 = wx_static_cast(wxToolBarTool*, toolBase);
356
357 if ( tool->IsButton() )
358 {
359 if ( !HasFlag(wxTB_NOICONS) )
360 {
361 wxBitmap bitmap = tool->GetNormalBitmap();
362
363 wxCHECK_MSG( bitmap.Ok(), false,
364 wxT("invalid bitmap for wxToolBar icon") );
365
366 tool->m_image = gtk_image_new();
367 tool->SetImage(bitmap);
368
369 gtk_misc_set_alignment((GtkMisc*)tool->m_image, 0.5, 0.5);
370 }
371 }
372
373 const int posGtk = int(pos);
374
375 switch ( tool->GetStyle() )
376 {
377 case wxTOOL_STYLE_BUTTON:
378 // for a radio button we need the widget which starts the radio
379 // group it belongs to, i.e. the first radio button immediately
380 // preceding this one
381 {
382 GtkWidget *widget = NULL;
383
384 if ( tool->IsRadio() )
385 {
386 wxToolBarToolsList::compatibility_iterator node
387 = wxToolBarToolsList::compatibility_iterator();
388 if ( pos )
389 node = m_tools.Item(pos - 1);
390
391 while ( node )
392 {
393 wxToolBarTool *toolNext = (wxToolBarTool *)node->GetData();
394 if ( !toolNext->IsRadio() )
395 break;
396
397 widget = toolNext->m_item;
398
399 node = node->GetPrevious();
400 }
401
402 if ( !widget )
403 {
404 // this is the first button in the radio button group,
405 // it will be toggled automatically by GTK so bring the
406 // internal flag in sync
407 tool->Toggle(true);
408 }
409 }
410
411 tool->m_item = gtk_toolbar_insert_element
412 (
413 m_toolbar,
414 tool->GetGtkChildType(),
415 widget,
416 tool->GetLabel().empty()
417 ? NULL
418 : (const char*) wxGTK_CONV( tool->GetLabel() ),
419 tool->GetShortHelp().empty()
420 ? NULL
421 : (const char*) wxGTK_CONV( tool->GetShortHelp() ),
422 "", // tooltip_private_text (?)
423 tool->m_image,
424 (GtkSignalFunc)gtk_toolbar_callback,
425 (gpointer)tool,
426 posGtk
427 );
428
429 wxCHECK_MSG(tool->m_item != NULL, false, _T("gtk_toolbar_insert_element() failed"));
430
431 g_signal_connect (tool->m_item, "enter_notify_event",
432 G_CALLBACK (gtk_toolbar_tool_callback),
433 tool);
434 g_signal_connect (tool->m_item, "leave_notify_event",
435 G_CALLBACK (gtk_toolbar_tool_callback),
436 tool);
437 }
438 break;
439
440 case wxTOOL_STYLE_SEPARATOR:
441 gtk_toolbar_insert_space( m_toolbar, posGtk );
442
443 // skip the rest
444 return true;
445
446 case wxTOOL_STYLE_CONTROL:
447 gtk_toolbar_insert_widget(
448 m_toolbar,
449 tool->GetControl()->m_widget,
450 (const char *) NULL,
451 (const char *) NULL,
452 posGtk
453 );
454 break;
455 }
456
457 GtkRequisition req;
458 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
459 (m_widget, &req );
460 m_width = req.width + m_xMargin;
461 m_height = req.height + 2*m_yMargin;
462 InvalidateBestSize();
463
464 return true;
465 }
466
467 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *toolBase)
468 {
469 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase);
470
471 switch ( tool->GetStyle() )
472 {
473 case wxTOOL_STYLE_CONTROL:
474 tool->GetControl()->Destroy();
475 break;
476
477 case wxTOOL_STYLE_BUTTON:
478 gtk_widget_destroy( tool->m_item );
479 break;
480
481 case wxTOOL_STYLE_SEPARATOR:
482 gtk_toolbar_remove_space( m_toolbar, pos );
483 break;
484 }
485
486 InvalidateBestSize();
487 return true;
488 }
489
490 // ----------------------------------------------------------------------------
491 // wxToolBar tools state
492 // ----------------------------------------------------------------------------
493
494 void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable)
495 {
496 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase);
497
498 if (tool->m_item)
499 {
500 gtk_widget_set_sensitive( tool->m_item, enable );
501 }
502 }
503
504 void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle )
505 {
506 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase);
507
508 GtkWidget *item = tool->m_item;
509 if ( item && GTK_IS_TOGGLE_BUTTON(item) )
510 {
511 tool->SetImage(tool->GetBitmap());
512
513 m_blockEvent = true;
514
515 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item), toggle );
516
517 m_blockEvent = false;
518 }
519 }
520
521 void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
522 bool WXUNUSED(toggle))
523 {
524 // VZ: absolutely no idea about how to do it
525 wxFAIL_MSG( _T("not implemented") );
526 }
527
528 // ----------------------------------------------------------------------------
529 // wxToolBar geometry
530 // ----------------------------------------------------------------------------
531
532 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x),
533 wxCoord WXUNUSED(y)) const
534 {
535 // VZ: GTK+ doesn't seem to have such thing
536 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
537
538 return (wxToolBarToolBase *)NULL;
539 }
540
541 void wxToolBar::SetMargins( int x, int y )
542 {
543 wxCHECK_RET( GetToolsCount() == 0,
544 wxT("wxToolBar::SetMargins must be called before adding tools.") );
545
546 m_xMargin = x;
547 m_yMargin = y;
548 }
549
550 void wxToolBar::SetToolSeparation( int separation )
551 {
552 // FIXME: this function disappeared
553 #if 0
554 gtk_toolbar_set_space_size( m_toolbar, separation );
555 #endif
556
557 m_toolSeparation = separation;
558 }
559
560 void wxToolBar::SetToolShortHelp( int id, const wxString& helpString )
561 {
562 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
563
564 if ( tool )
565 {
566 (void)tool->SetShortHelp(helpString);
567 gtk_tooltips_set_tip(m_toolbar->tooltips, tool->m_item,
568 wxGTK_CONV( helpString ), "");
569 }
570 }
571
572 // ----------------------------------------------------------------------------
573 // wxToolBar idle handling
574 // ----------------------------------------------------------------------------
575
576 void wxToolBar::OnInternalIdle()
577 {
578 // Check if we have to show window now
579 if (GtkShowFromOnIdle()) return;
580
581 wxCursor cursor = m_cursor;
582 if (g_globalCursor.Ok()) cursor = g_globalCursor;
583
584 if (cursor.Ok())
585 {
586 /* I now set the cursor the anew in every OnInternalIdle call
587 as setting the cursor in a parent window also effects the
588 windows above so that checking for the current cursor is
589 not possible. */
590
591 if (HasFlag(wxTB_DOCKABLE) && (m_widget->window))
592 {
593 /* if the toolbar is dockable, then m_widget stands for the
594 GtkHandleBox widget, which uses its own window so that we
595 can set the cursor for it. if the toolbar is not dockable,
596 m_widget comes from m_toolbar which uses its parent's
597 window ("windowless windows") and thus we cannot set the
598 cursor. */
599 gdk_window_set_cursor( m_widget->window, cursor.GetCursor() );
600 }
601
602 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
603 while ( node )
604 {
605 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
606 node = node->GetNext();
607
608 GtkWidget *item = tool->m_item;
609 if ( item )
610 {
611 GdkWindow *window = item->window;
612
613 if ( window )
614 {
615 gdk_window_set_cursor( window, cursor.GetCursor() );
616 }
617 }
618 }
619 }
620
621 if (wxUpdateUIEvent::CanUpdate(this))
622 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
623 }
624
625
626 // ----------------------------------------------------------------------------
627
628 // static
629 wxVisualAttributes
630 wxToolBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
631 {
632 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new);
633 }
634
635 #endif // wxUSE_TOOLBAR_NATIVE