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