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