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