]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/tbargtk.cpp
1. wxMotif toolbar works; the toggle buttons have the same width as the other
[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 #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 "gdk/gdk.h"
31 #include "gtk/gtk.h"
32
33 // ----------------------------------------------------------------------------
34 // globals
35 // ----------------------------------------------------------------------------
36
37 // idle system
38 extern void wxapp_install_idle_handler();
39 extern bool g_isIdle;
40
41 // data
42 extern bool g_blockEventsOnDrag;
43 extern wxCursor g_globalCursor;
44
45 // ----------------------------------------------------------------------------
46 // wxToolBarTool
47 // ----------------------------------------------------------------------------
48
49 class wxToolBarTool : public wxToolBarToolBase
50 {
51 public:
52 wxToolBarTool(wxToolBar *tbar,
53 int id,
54 const wxBitmap& bitmap1,
55 const wxBitmap& bitmap2,
56 bool toggle,
57 wxObject *clientData,
58 const wxString& shortHelpString,
59 const wxString& longHelpString)
60 : wxToolBarToolBase(tbar, id, bitmap1, bitmap2, toggle,
61 clientData, shortHelpString, longHelpString)
62 {
63 Init();
64 }
65
66 wxToolBarTool(wxToolBar *tbar, wxControl *control)
67 : wxToolBarToolBase(tbar, control)
68 {
69 Init();
70 }
71
72 GtkWidget *m_item;
73 GtkWidget *m_pixmap;
74
75 protected:
76 void Init();
77 };
78
79 // ----------------------------------------------------------------------------
80 // wxWin macros
81 // ----------------------------------------------------------------------------
82
83 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
84
85 // ============================================================================
86 // implementation
87 // ============================================================================
88
89 //-----------------------------------------------------------------------------
90 // "clicked" (internal from gtk_toolbar)
91 //-----------------------------------------------------------------------------
92
93 static void gtk_toolbar_callback( GtkWidget *WXUNUSED(widget),
94 wxToolBarTool *tool )
95 {
96 if (g_isIdle)
97 wxapp_install_idle_handler();
98
99 wxToolBar *tbar = (wxToolBar *)tool->GetToolBar();
100 if ( tbar->m_blockNextEvent )
101 {
102 tbar->m_blockNextEvent = FALSE;
103 return;
104 }
105
106 if (g_blockEventsOnDrag) return;
107 if (!tool->IsEnabled()) return;
108
109 if (tool->CanBeToggled())
110 {
111 tool->Toggle();
112
113 wxBitmap bitmap = tool->GetBitmap();
114 if ( bitmap.Ok() )
115 {
116 GtkPixmap *pixmap = GTK_PIXMAP( tool->m_pixmap );
117
118 GdkBitmap *mask = bitmap.GetMask() ? bitmap.GetMask()->GetBitmap()
119 : (GdkBitmap *)NULL;
120
121 gtk_pixmap_set( pixmap, bitmap.GetPixmap(), mask );
122 }
123 }
124
125 tbar->OnLeftClick( tool->GetId(), tool->IsToggled() );
126 }
127
128 //-----------------------------------------------------------------------------
129 // "enter_notify_event"
130 //-----------------------------------------------------------------------------
131
132 static gint gtk_toolbar_enter_callback( GtkWidget *WXUNUSED(widget),
133 GdkEventCrossing *WXUNUSED(gdk_event),
134 wxToolBarTool *tool )
135 {
136 if (g_isIdle) wxapp_install_idle_handler();
137
138 if (g_blockEventsOnDrag) return TRUE;
139
140 wxToolBar *tb = (wxToolBar *)tool->GetToolBar();
141
142 #if (GTK_MINOR_VERSION == 0)
143 /* we grey-out the tip text of disabled tool in GTK 1.0 */
144 if (tool->IsEnabled())
145 {
146 if (tb->m_fg->red != 0)
147 {
148 tb->m_fg->red = 0;
149 tb->m_fg->green = 0;
150 tb->m_fg->blue = 0;
151 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb->m_toolbar) ), tb->m_fg );
152
153 gtk_tooltips_set_colors( GTK_TOOLBAR(tb->m_toolbar)->tooltips, tb->m_bg, tb->m_fg );
154 }
155 }
156 else
157 {
158 if (tb->m_fg->red == 0)
159 {
160 tb->m_fg->red = 33000;
161 tb->m_fg->green = 33000;
162 tb->m_fg->blue = 33000;
163 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb->m_toolbar) ), tb->m_fg );
164 gtk_tooltips_set_colors( GTK_TOOLBAR(tb->m_toolbar)->tooltips, tb->m_bg, tb->m_fg );
165 }
166 }
167 #endif
168
169 /* emit the event */
170
171 tb->OnMouseEnter( tool->GetId() );
172
173 return FALSE;
174 }
175
176 //-----------------------------------------------------------------------------
177 // InsertChild callback for wxToolBar
178 //-----------------------------------------------------------------------------
179
180 static void wxInsertChildInToolBar( wxToolBar* WXUNUSED(parent),
181 wxWindow* WXUNUSED(child) )
182 {
183 /* we don't do anything here but pray */
184 }
185
186 // ----------------------------------------------------------------------------
187 // wxToolBarTool
188 // ----------------------------------------------------------------------------
189
190 void wxToolBarTool::Init()
191 {
192 m_item =
193 m_pixmap = (GtkWidget *)NULL;
194 }
195
196 wxToolBarToolBase *wxToolBar::CreateTool(int id,
197 const wxBitmap& bitmap1,
198 const wxBitmap& bitmap2,
199 bool toggle,
200 wxObject *clientData,
201 const wxString& shortHelpString,
202 const wxString& longHelpString)
203 {
204 return new wxToolBarTool(this, id, bitmap1, bitmap2, toggle,
205 clientData, shortHelpString, longHelpString);
206 }
207
208 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
209 {
210 return new wxToolBarTool(this, control);
211 }
212
213 //-----------------------------------------------------------------------------
214 // wxToolBar construction
215 //-----------------------------------------------------------------------------
216
217 void wxToolBar::Init()
218 {
219 m_fg =
220 m_bg = (GdkColor *)NULL;
221
222 m_toolbar = (GtkToolbar *)NULL;
223
224 m_blockNextEvent = FALSE;
225 }
226
227 wxToolBar::~wxToolBar()
228 {
229 delete m_fg;
230 delete m_bg;
231 }
232
233 bool wxToolBar::Create( wxWindow *parent,
234 wxWindowID id,
235 const wxPoint& pos,
236 const wxSize& size,
237 long style,
238 const wxString& name )
239 {
240 m_needParent = TRUE;
241 m_insertCallback = (wxInsertChildFunction)wxInsertChildInToolBar;
242
243 if ( !PreCreation( parent, pos, size ) ||
244 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
245 {
246 wxFAIL_MSG( wxT("wxToolBar creation failed") );
247
248 return FALSE;
249 }
250
251 GtkOrientation orient = style & wxTB_VERTICAL ? GTK_ORIENTATION_VERTICAL
252 : GTK_ORIENTATION_HORIZONTAL;
253 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new( orient, GTK_TOOLBAR_ICONS ) );
254
255 SetToolSeparation(7);
256
257 if (style & wxTB_DOCKABLE)
258 {
259 m_widget = gtk_handle_box_new();
260 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
261 gtk_widget_show( GTK_WIDGET(m_toolbar) );
262
263 #if (GTK_MINOR_VERSION > 0)
264 if (style & wxTB_FLAT)
265 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget), GTK_SHADOW_NONE );
266 #endif
267 }
268 else
269 {
270 m_widget = GTK_WIDGET(m_toolbar);
271 }
272
273 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
274
275 #if (GTK_MINOR_VERSION > 0)
276 if (style & wxTB_FLAT)
277 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar), GTK_RELIEF_NONE );
278 #endif
279
280 m_fg = new GdkColor;
281 m_fg->red = 0;
282 m_fg->green = 0;
283 m_fg->blue = 0;
284 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_fg );
285
286 m_bg = new GdkColor;
287 m_bg->red = 65535;
288 m_bg->green = 65535;
289 m_bg->blue = 50000;
290 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_bg );
291
292 #if (GTK_MINOR_VERSION > 0)
293 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar)->tooltips );
294
295 GtkStyle *g_style =
296 gtk_style_copy(
297 gtk_widget_get_style(
298 GTK_TOOLBAR(m_toolbar)->tooltips->tip_window ) );
299
300 g_style->bg[GTK_STATE_NORMAL] = *m_bg;
301 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar)->tooltips->tip_window, g_style );
302 #else
303 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar)->tooltips, m_bg, m_fg );
304 #endif
305
306 m_parent->DoAddChild( this );
307
308 PostCreation();
309
310 Show( TRUE );
311
312 return TRUE;
313 }
314
315 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
316 {
317 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
318
319 if ( tool->IsButton() )
320 {
321 wxBitmap bitmap = tool->GetBitmap1();
322
323 wxCHECK_MSG( bitmap.Ok(), FALSE,
324 wxT("invalid bitmap for wxToolBar icon") );
325
326 wxCHECK_MSG( bitmap.GetBitmap() == NULL, FALSE,
327 wxT("wxToolBar doesn't support GdkBitmap") );
328
329 wxCHECK_MSG( bitmap.GetPixmap() != NULL, FALSE,
330 wxT("wxToolBar::Add needs a wxBitmap") );
331
332 GtkWidget *tool_pixmap = (GtkWidget *)NULL;
333
334 GdkPixmap *pixmap = bitmap.GetPixmap();
335
336 GdkBitmap *mask = (GdkBitmap *)NULL;
337 if ( bitmap.GetMask() )
338 mask = bitmap.GetMask()->GetBitmap();
339
340 tool_pixmap = gtk_pixmap_new( pixmap, mask );
341 #if (GTK_MINOR_VERSION > 0)
342 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap), TRUE );
343 #endif
344
345 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
346
347 tool->m_pixmap = tool_pixmap;
348 }
349
350 switch ( tool->GetStyle() )
351 {
352 case wxTOOL_STYLE_BUTTON:
353 tool->m_item = gtk_toolbar_insert_element
354 (
355 m_toolbar,
356 tool->CanBeToggled()
357 ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
358 : GTK_TOOLBAR_CHILD_BUTTON,
359 (GtkWidget *)NULL,
360 (const char *)NULL,
361 tool->GetShortHelp().mbc_str(),
362 "", // tooltip_private_text (?)
363 tool->m_pixmap,
364 (GtkSignalFunc)gtk_toolbar_callback,
365 (gpointer)tool,
366 pos
367 );
368
369 if ( !tool->m_item )
370 {
371 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
372
373 return FALSE;
374 }
375
376 gtk_signal_connect( GTK_OBJECT(tool->m_item),
377 "enter_notify_event",
378 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback),
379 (gpointer)tool );
380 break;
381
382 case wxTOOL_STYLE_SEPARATOR:
383 gtk_toolbar_append_space( m_toolbar );
384
385 // skip the rest
386 return TRUE;
387
388 case wxTOOL_STYLE_CONTROL:
389 gtk_toolbar_insert_widget(
390 m_toolbar,
391 tool->GetControl()->m_widget,
392 (const char *) NULL,
393 (const char *) NULL,
394 pos
395 );
396 break;
397 }
398
399 GtkRequisition req;
400 (* GTK_WIDGET_CLASS( GTK_OBJECT(m_widget)->klass )->size_request ) (m_widget, &req );
401 m_width = req.width + m_xMargin;
402 m_height = req.height + 2*m_yMargin + 4;
403
404 return TRUE;
405 }
406
407 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *toolBase)
408 {
409 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
410
411 switch ( tool->GetStyle() )
412 {
413 case wxTOOL_STYLE_CONTROL:
414 tool->GetControl()->Destroy();
415 break;
416
417 case wxTOOL_STYLE_BUTTON:
418 gtk_widget_destroy( tool->m_item );
419 break;
420
421 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
422 }
423
424 return TRUE;
425 }
426
427 // ----------------------------------------------------------------------------
428 // wxToolBar tools state
429 // ----------------------------------------------------------------------------
430
431 void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable)
432 {
433 #if (GTK_MINOR_VERSION > 0)
434 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
435
436 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
437 greyed anyway and this also disables tooltips */
438 if (tool->m_item)
439 gtk_widget_set_sensitive( tool->m_item, enable );
440 #endif
441 }
442
443 void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle )
444 {
445 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
446
447 GtkWidget *item = tool->m_item;
448 if ( item && GTK_IS_TOGGLE_BUTTON(item) )
449 {
450 wxBitmap bitmap = tool->GetBitmap();
451 if ( bitmap.Ok() )
452 {
453 GtkPixmap *pixmap = GTK_PIXMAP( tool->m_pixmap );
454
455 GdkBitmap *mask = bitmap.GetMask() ? bitmap.GetMask()->GetBitmap()
456 : (GdkBitmap *)NULL;
457
458 gtk_pixmap_set( pixmap, bitmap.GetPixmap(), mask );
459 }
460
461 m_blockNextEvent = TRUE; // we cannot use gtk_signal_disconnect here
462
463 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item), toggle );
464 }
465 }
466
467 void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
468 bool WXUNUSED(toggle))
469 {
470 // VZ: absolutely no idea about how to do it
471 wxFAIL_MSG( _T("not implemented") );
472 }
473
474 // ----------------------------------------------------------------------------
475 // wxToolBar geometry
476 // ----------------------------------------------------------------------------
477
478 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x),
479 wxCoord WXUNUSED(y)) const
480 {
481 // VZ: GTK+ doesn't seem to have such thing
482 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
483
484 return (wxToolBarToolBase *)NULL;
485 }
486
487 void wxToolBar::SetMargins( int x, int y )
488 {
489 wxCHECK_RET( GetToolsCount() == 0,
490 wxT("wxToolBar::SetMargins must be called before adding tools.") );
491
492 if (x > 1) gtk_toolbar_append_space( m_toolbar ); // oh well
493
494 m_xMargin = x;
495 m_yMargin = y;
496 }
497
498 void wxToolBar::SetToolSeparation( int separation )
499 {
500 gtk_toolbar_set_space_size( m_toolbar, separation );
501 m_toolSeparation = separation;
502 }
503
504 // ----------------------------------------------------------------------------
505 // wxToolBar idle handling
506 // ----------------------------------------------------------------------------
507
508 void wxToolBar::OnInternalIdle()
509 {
510 wxCursor cursor = m_cursor;
511 if (g_globalCursor.Ok()) cursor = g_globalCursor;
512
513 if (cursor.Ok())
514 {
515 /* I now set the cursor the anew in every OnInternalIdle call
516 as setting the cursor in a parent window also effects the
517 windows above so that checking for the current cursor is
518 not possible. */
519
520 if (HasFlag(wxTB_DOCKABLE) && (m_widget->window))
521 {
522 /* if the toolbar is dockable, then m_widget stands for the
523 GtkHandleBox widget, which uses its own window so that we
524 can set the cursor for it. if the toolbar is not dockable,
525 m_widget comes from m_toolbar which uses its parent's
526 window ("windowless windows") and thus we cannot set the
527 cursor. */
528 gdk_window_set_cursor( m_widget->window, cursor.GetCursor() );
529 }
530
531 wxToolBarToolsList::Node *node = m_tools.GetFirst();
532 while ( node )
533 {
534 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
535 node = node->GetNext();
536
537 GtkWidget *item = tool->m_item;
538 if ( item )
539 {
540 GdkWindow *window = item->window;
541
542 if ( window )
543 {
544 gdk_window_set_cursor( window, cursor.GetCursor() );
545 }
546 }
547 }
548 }
549
550 UpdateWindowUI();
551 }
552
553 #endif