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