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