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