]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/tbargtk.cpp
Added scrolling of widgets to GTK 2.0.
[wxWidgets.git] / src / gtk / tbargtk.cpp
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
32 extern GdkFont *GtkGetDefaultGuiFont();
33
34 // ----------------------------------------------------------------------------
35 // globals
36 // ----------------------------------------------------------------------------
37
38 // idle system
39 extern void wxapp_install_idle_handler();
40 extern bool g_isIdle;
41
42 // data
43 extern bool g_blockEventsOnDrag;
44 extern wxCursor g_globalCursor;
45
46 // ----------------------------------------------------------------------------
47 // private functions
48 // ----------------------------------------------------------------------------
49
50 // translate wxWindows toolbar style flags to GTK orientation and style
51 static 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
72 class wxToolBarTool : public wxToolBarToolBase
73 {
74 public:
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
125 protected:
126 void Init();
127 };
128
129 // ----------------------------------------------------------------------------
130 // wxWin macros
131 // ----------------------------------------------------------------------------
132
133 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
134
135 // ============================================================================
136 // implementation
137 // ============================================================================
138
139 //-----------------------------------------------------------------------------
140 // "clicked" (internal from gtk_toolbar)
141 //-----------------------------------------------------------------------------
142
143 static 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
185 static 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
208 static void wxInsertChildInToolBar( wxToolBar* WXUNUSED(parent),
209 wxWindow* WXUNUSED(child) )
210 {
211 // we don't do anything here
212 }
213
214 // ----------------------------------------------------------------------------
215 // wxToolBarTool
216 // ----------------------------------------------------------------------------
217
218 void wxToolBarTool::Init()
219 {
220 m_item =
221 m_pixmap = (GtkWidget *)NULL;
222 }
223
224 wxToolBarToolBase *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
237 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
238 {
239 return new wxToolBarTool(this, control);
240 }
241
242 //-----------------------------------------------------------------------------
243 // wxToolBar construction
244 //-----------------------------------------------------------------------------
245
246 void wxToolBar::Init()
247 {
248 m_fg =
249 m_bg = (GdkColor *)NULL;
250 m_toolbar = (GtkToolbar *)NULL;
251 m_blockEvent = FALSE;
252 }
253
254 wxToolBar::~wxToolBar()
255 {
256 delete m_fg;
257 delete m_bg;
258 }
259
260 bool 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 #else
282 GtkOrientation orient;
283 GtkToolbarStyle gtkStyle;
284 GetGtkStyle(style, &orient, &gtkStyle);
285
286 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new(orient, gtkStyle) );
287 #endif
288
289 SetToolSeparation(7);
290
291 if (style & wxTB_DOCKABLE)
292 {
293 m_widget = gtk_handle_box_new();
294 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
295 gtk_widget_show( GTK_WIDGET(m_toolbar) );
296
297 if (style & wxTB_FLAT)
298 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget), GTK_SHADOW_NONE );
299 }
300 else
301 {
302 m_widget = GTK_WIDGET(m_toolbar);
303 }
304
305 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
306
307 // FIXME: there is no such function for toolbars in 2.0
308 #ifndef __WXGTK20__
309 if (style & wxTB_FLAT)
310 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar), GTK_RELIEF_NONE );
311 #endif
312
313
314 m_fg = new GdkColor;
315 m_fg->red = 0;
316 m_fg->green = 0;
317 m_fg->blue = 0;
318 wxColour fg(0,0,0);
319 fg.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ) );
320 m_fg->pixel = fg.GetPixel();
321
322 m_bg = new GdkColor;
323 m_bg->red = 65535;
324 m_bg->green = 65535;
325 m_bg->blue = 49980;
326 wxColour bg(255,255,196);
327 bg.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ) );
328 m_bg->pixel = bg.GetPixel();
329
330 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar)->tooltips );
331
332 GtkStyle *g_style =
333 gtk_style_copy(
334 gtk_widget_get_style(
335 GTK_TOOLBAR(m_toolbar)->tooltips->tip_window ) );
336
337 g_style->bg[GTK_STATE_NORMAL] = *m_bg;
338
339 SET_STYLE_FONT(g_style, GtkGetDefaultGuiFont());
340
341 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar)->tooltips->tip_window, g_style );
342
343 m_parent->DoAddChild( this );
344
345 PostCreation();
346
347 Show( TRUE );
348
349 return TRUE;
350 }
351
352 void wxToolBar::GtkSetStyle()
353 {
354 GtkOrientation orient;
355 GtkToolbarStyle style;
356 GetGtkStyle(GetWindowStyle(), &orient, &style);
357
358 gtk_toolbar_set_orientation(m_toolbar, orient);
359 gtk_toolbar_set_style(m_toolbar, style);
360 }
361
362 void wxToolBar::SetWindowStyleFlag( long style )
363 {
364 wxToolBarBase::SetWindowStyleFlag(style);
365
366 if ( m_toolbar )
367 GtkSetStyle();
368 }
369
370 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
371 {
372 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
373
374 // we have inserted a space before all the tools
375 if (m_xMargin > 1) pos++;
376
377 if ( tool->IsButton() )
378 {
379 wxBitmap bitmap = tool->GetNormalBitmap();
380
381 wxCHECK_MSG( bitmap.Ok(), FALSE,
382 wxT("invalid bitmap for wxToolBar icon") );
383
384 wxCHECK_MSG( bitmap.GetBitmap() == NULL, FALSE,
385 wxT("wxToolBar doesn't support GdkBitmap") );
386
387 wxCHECK_MSG( bitmap.GetPixmap() != NULL, FALSE,
388 wxT("wxToolBar::Add needs a wxBitmap") );
389
390 GtkWidget *tool_pixmap = (GtkWidget *)NULL;
391
392 GdkPixmap *pixmap = bitmap.GetPixmap();
393
394 GdkBitmap *mask = (GdkBitmap *)NULL;
395 if ( bitmap.GetMask() )
396 mask = bitmap.GetMask()->GetBitmap();
397
398 tool_pixmap = gtk_pixmap_new( pixmap, mask );
399 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap), TRUE );
400
401 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
402
403 tool->m_pixmap = tool_pixmap;
404 }
405
406 switch ( tool->GetStyle() )
407 {
408 case wxTOOL_STYLE_BUTTON:
409 // for a radio button we need the widget which starts the radio
410 // group it belongs to, i.e. the first radio button immediately
411 // preceding this one
412 {
413 GtkWidget *widget = NULL;
414
415 if ( tool->IsRadio() )
416 {
417 wxToolBarToolsList::Node *node = pos ? m_tools.Item(pos - 1)
418 : NULL;
419 while ( node )
420 {
421 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
422 if ( !tool->IsRadio() )
423 break;
424
425 widget = tool->m_item;
426
427 node = node->GetPrevious();
428 }
429
430 if ( !widget )
431 {
432 // this is the first button in the radio button group,
433 // it will be toggled automatically by GTK so bring the
434 // internal flag in sync
435 tool->Toggle(TRUE);
436 }
437 }
438
439 tool->m_item = gtk_toolbar_insert_element
440 (
441 m_toolbar,
442 tool->GetGtkChildType(),
443 widget,
444 tool->GetLabel().empty()
445 ? NULL
446 : (const char*) wxGTK_CONV( tool->GetLabel() ),
447 tool->GetShortHelp().empty()
448 ? NULL
449 : (const char*) wxGTK_CONV( tool->GetShortHelp() ),
450 "", // tooltip_private_text (?)
451 tool->m_pixmap,
452 (GtkSignalFunc)gtk_toolbar_callback,
453 (gpointer)tool,
454 pos
455 );
456
457 if ( !tool->m_item )
458 {
459 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
460
461 return FALSE;
462 }
463
464 gtk_signal_connect( GTK_OBJECT(tool->m_item),
465 "enter_notify_event",
466 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback),
467 (gpointer)tool );
468 gtk_signal_connect( GTK_OBJECT(tool->m_item),
469 "leave_notify_event",
470 GTK_SIGNAL_FUNC(gtk_toolbar_tool_callback),
471 (gpointer)tool );
472 }
473 break;
474
475 case wxTOOL_STYLE_SEPARATOR:
476 gtk_toolbar_insert_space( m_toolbar, pos );
477
478 // skip the rest
479 return TRUE;
480
481 case wxTOOL_STYLE_CONTROL:
482 gtk_toolbar_insert_widget(
483 m_toolbar,
484 tool->GetControl()->m_widget,
485 (const char *) NULL,
486 (const char *) NULL,
487 pos
488 );
489 break;
490 }
491
492 GtkRequisition req;
493 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
494 (m_widget, &req );
495 m_width = req.width + m_xMargin;
496 m_height = req.height + 2*m_yMargin;
497
498 return TRUE;
499 }
500
501 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
502 {
503 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
504
505 switch ( tool->GetStyle() )
506 {
507 case wxTOOL_STYLE_CONTROL:
508 tool->GetControl()->Destroy();
509 break;
510
511 case wxTOOL_STYLE_BUTTON:
512 gtk_widget_destroy( tool->m_item );
513 break;
514
515 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
516 }
517
518 return TRUE;
519 }
520
521 // ----------------------------------------------------------------------------
522 // wxToolBar tools state
523 // ----------------------------------------------------------------------------
524
525 void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable)
526 {
527 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
528
529 if (tool->m_item)
530 {
531 gtk_widget_set_sensitive( tool->m_item, enable );
532 }
533 }
534
535 void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle )
536 {
537 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
538
539 GtkWidget *item = tool->m_item;
540 if ( item && GTK_IS_TOGGLE_BUTTON(item) )
541 {
542 wxBitmap bitmap = tool->GetBitmap();
543 if ( bitmap.Ok() )
544 {
545 GtkPixmap *pixmap = GTK_PIXMAP( tool->m_pixmap );
546
547 GdkBitmap *mask = bitmap.GetMask() ? bitmap.GetMask()->GetBitmap()
548 : (GdkBitmap *)NULL;
549
550 gtk_pixmap_set( pixmap, bitmap.GetPixmap(), mask );
551 }
552
553 m_blockEvent = TRUE;
554
555 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item), toggle );
556
557 m_blockEvent = FALSE;
558 }
559 }
560
561 void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
562 bool WXUNUSED(toggle))
563 {
564 // VZ: absolutely no idea about how to do it
565 wxFAIL_MSG( _T("not implemented") );
566 }
567
568 // ----------------------------------------------------------------------------
569 // wxToolBar geometry
570 // ----------------------------------------------------------------------------
571
572 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x),
573 wxCoord WXUNUSED(y)) const
574 {
575 // VZ: GTK+ doesn't seem to have such thing
576 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
577
578 return (wxToolBarToolBase *)NULL;
579 }
580
581 void wxToolBar::SetMargins( int x, int y )
582 {
583 wxCHECK_RET( GetToolsCount() == 0,
584 wxT("wxToolBar::SetMargins must be called before adding tools.") );
585
586 if (x > 1) gtk_toolbar_append_space( m_toolbar ); // oh well
587
588 m_xMargin = x;
589 m_yMargin = y;
590 }
591
592 void wxToolBar::SetToolSeparation( int separation )
593 {
594 // FIXME: this function disappeared
595 #ifndef __WXGTK20__
596 gtk_toolbar_set_space_size( m_toolbar, separation );
597 #endif
598
599 m_toolSeparation = separation;
600 }
601
602 void wxToolBar::SetToolShortHelp( int id, const wxString& helpString )
603 {
604 wxToolBarTool *tool = (wxToolBarTool *)FindById(id);
605
606 if ( tool )
607 {
608 (void)tool->SetShortHelp(helpString);
609 gtk_tooltips_set_tip(m_toolbar->tooltips, tool->m_item,
610 wxGTK_CONV( helpString ), "");
611 }
612 }
613
614 // ----------------------------------------------------------------------------
615 // wxToolBar idle handling
616 // ----------------------------------------------------------------------------
617
618 void wxToolBar::OnInternalIdle()
619 {
620 wxCursor cursor = m_cursor;
621 if (g_globalCursor.Ok()) cursor = g_globalCursor;
622
623 if (cursor.Ok())
624 {
625 /* I now set the cursor the anew in every OnInternalIdle call
626 as setting the cursor in a parent window also effects the
627 windows above so that checking for the current cursor is
628 not possible. */
629
630 if (HasFlag(wxTB_DOCKABLE) && (m_widget->window))
631 {
632 /* if the toolbar is dockable, then m_widget stands for the
633 GtkHandleBox widget, which uses its own window so that we
634 can set the cursor for it. if the toolbar is not dockable,
635 m_widget comes from m_toolbar which uses its parent's
636 window ("windowless windows") and thus we cannot set the
637 cursor. */
638 gdk_window_set_cursor( m_widget->window, cursor.GetCursor() );
639 }
640
641 wxToolBarToolsList::Node *node = m_tools.GetFirst();
642 while ( node )
643 {
644 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
645 node = node->GetNext();
646
647 GtkWidget *item = tool->m_item;
648 if ( item )
649 {
650 GdkWindow *window = item->window;
651
652 if ( window )
653 {
654 gdk_window_set_cursor( window, cursor.GetCursor() );
655 }
656 }
657 }
658 }
659
660 UpdateWindowUI();
661 }
662
663 #endif // wxUSE_TOOLBAR_NATIVE