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