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