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