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