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