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