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