]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/tbargtk.cpp
Make this compile with gtk+ 1.3.1... not sure what the "casting"
[wxWidgets.git] / src / gtk1 / tbargtk.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: tbargtk.cpp
3// Purpose: GTK toolbar
4// Author: Robert Roebling
8a0681f9 5// Modified: 13.12.99 by VZ to derive from wxToolBarBase
32e9da8b 6// RCS-ID: $Id$
c801d85f 7// Copyright: (c) Robert Roebling
a3622daa 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
8a0681f9
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
c801d85f 19#ifdef __GNUG__
8a0681f9 20 #pragma implementation "tbargtk.h"
c801d85f
KB
21#endif
22
23#include "wx/toolbar.h"
dcf924a3 24
8a0681f9 25#if wxUSE_TOOLBAR_NATIVE
dcf924a3 26
e702ff0f 27#include "wx/frame.h"
c801d85f 28
3fa056ab
JJ
29#ifdef __VMS__
30#define gtk_pixmap_set_build_insensitive gtk_pixmap_set_build_insensitiv
31#endif
83624f79
RR
32#include "glib.h"
33#include "gdk/gdk.h"
34#include "gtk/gtk.h"
35
f6bcfd97
BP
36extern GdkFont *GtkGetDefaultGuiFont();
37
8a0681f9
VZ
38// ----------------------------------------------------------------------------
39// globals
40// ----------------------------------------------------------------------------
acfd422a 41
8a0681f9 42// idle system
acfd422a
RR
43extern void wxapp_install_idle_handler();
44extern bool g_isIdle;
45
314055fa 46// data
9b7e522a
RR
47extern bool g_blockEventsOnDrag;
48extern wxCursor g_globalCursor;
314055fa 49
8a0681f9
VZ
50// ----------------------------------------------------------------------------
51// wxToolBarTool
52// ----------------------------------------------------------------------------
53
54class wxToolBarTool : public wxToolBarToolBase
55{
56public:
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
80protected:
81 void Init();
82};
83
84// ----------------------------------------------------------------------------
85// wxWin macros
86// ----------------------------------------------------------------------------
87
88IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
89
90// ============================================================================
91// implementation
92// ============================================================================
93
c801d85f 94//-----------------------------------------------------------------------------
2f2aa628 95// "clicked" (internal from gtk_toolbar)
c801d85f
KB
96//-----------------------------------------------------------------------------
97
8a0681f9
VZ
98static void gtk_toolbar_callback( GtkWidget *WXUNUSED(widget),
99 wxToolBarTool *tool )
c801d85f 100{
59fe1666
RR
101 if (g_isIdle)
102 wxapp_install_idle_handler();
103
8a0681f9
VZ
104 wxToolBar *tbar = (wxToolBar *)tool->GetToolBar();
105 if ( tbar->m_blockNextEvent )
59fe1666 106 {
8a0681f9 107 tbar->m_blockNextEvent = FALSE;
59fe1666
RR
108 return;
109 }
acfd422a 110
1144d24d 111 if (g_blockEventsOnDrag) return;
8a0681f9 112 if (!tool->IsEnabled()) return;
a3622daa 113
8a0681f9 114 if (tool->CanBeToggled())
85eb36c2 115 {
8a0681f9
VZ
116 tool->Toggle();
117
118 wxBitmap bitmap = tool->GetBitmap();
119 if ( bitmap.Ok() )
120 {
85eb36c2 121 GtkPixmap *pixmap = GTK_PIXMAP( tool->m_pixmap );
8a0681f9
VZ
122
123 GdkBitmap *mask = bitmap.GetMask() ? bitmap.GetMask()->GetBitmap()
124 : (GdkBitmap *)NULL;
125
85eb36c2 126 gtk_pixmap_set( pixmap, bitmap.GetPixmap(), mask );
8a0681f9 127 }
85eb36c2 128 }
a3622daa 129
8a0681f9 130 tbar->OnLeftClick( tool->GetId(), tool->IsToggled() );
fc008f25 131}
c801d85f 132
2f2aa628
RR
133//-----------------------------------------------------------------------------
134// "enter_notify_event"
135//-----------------------------------------------------------------------------
136
314055fa 137static gint gtk_toolbar_enter_callback( GtkWidget *WXUNUSED(widget),
8a0681f9
VZ
138 GdkEventCrossing *WXUNUSED(gdk_event),
139 wxToolBarTool *tool )
314055fa 140{
acfd422a
RR
141 if (g_isIdle) wxapp_install_idle_handler();
142
1144d24d 143 if (g_blockEventsOnDrag) return TRUE;
b98d804b 144
8a0681f9 145 wxToolBar *tb = (wxToolBar *)tool->GetToolBar();
b98d804b 146
c693edf3
RR
147#if (GTK_MINOR_VERSION == 0)
148 /* we grey-out the tip text of disabled tool in GTK 1.0 */
8a0681f9 149 if (tool->IsEnabled())
b98d804b
RR
150 {
151 if (tb->m_fg->red != 0)
f03fc89f 152 {
b98d804b
RR
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 );
f03fc89f 157
b98d804b 158 gtk_tooltips_set_colors( GTK_TOOLBAR(tb->m_toolbar)->tooltips, tb->m_bg, tb->m_fg );
f03fc89f 159 }
b98d804b
RR
160 }
161 else
162 {
163 if (tb->m_fg->red == 0)
f03fc89f 164 {
b98d804b
RR
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 );
f03fc89f 170 }
b98d804b 171 }
f7ac40d1 172#endif
b98d804b
RR
173
174 /* emit the event */
314055fa 175
8a0681f9 176 tb->OnMouseEnter( tool->GetId() );
314055fa 177
1144d24d 178 return FALSE;
314055fa
RR
179}
180
bf9e3e73
RR
181//-----------------------------------------------------------------------------
182// InsertChild callback for wxToolBar
183//-----------------------------------------------------------------------------
184
8a0681f9
VZ
185static void wxInsertChildInToolBar( wxToolBar* WXUNUSED(parent),
186 wxWindow* WXUNUSED(child) )
bf9e3e73
RR
187{
188 /* we don't do anything here but pray */
189}
190
8a0681f9
VZ
191// ----------------------------------------------------------------------------
192// wxToolBarTool
193// ----------------------------------------------------------------------------
c801d85f 194
8a0681f9
VZ
195void wxToolBarTool::Init()
196{
197 m_item =
198 m_pixmap = (GtkWidget *)NULL;
199}
c801d85f 200
8a0681f9
VZ
201wxToolBarToolBase *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}
b1da76e1 212
8a0681f9 213wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
c801d85f 214{
8a0681f9 215 return new wxToolBarTool(this, control);
fc008f25 216}
c801d85f 217
8a0681f9
VZ
218//-----------------------------------------------------------------------------
219// wxToolBar construction
220//-----------------------------------------------------------------------------
221
222void wxToolBar::Init()
c801d85f 223{
8a0681f9
VZ
224 m_fg =
225 m_bg = (GdkColor *)NULL;
226
227 m_toolbar = (GtkToolbar *)NULL;
228
229 m_blockNextEvent = FALSE;
fc008f25 230}
c801d85f 231
a3622daa 232wxToolBar::~wxToolBar()
c801d85f 233{
83624f79
RR
234 delete m_fg;
235 delete m_bg;
fc008f25 236}
c801d85f 237
8a0681f9
VZ
238bool wxToolBar::Create( wxWindow *parent,
239 wxWindowID id,
240 const wxPoint& pos,
241 const wxSize& size,
242 long style,
243 const wxString& name )
c801d85f 244{
1144d24d 245 m_needParent = TRUE;
bf9e3e73 246 m_insertCallback = (wxInsertChildFunction)wxInsertChildInToolBar;
a3622daa 247
8a0681f9
VZ
248 if ( !PreCreation( parent, pos, size ) ||
249 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
4dcaf11a 250 {
223d09f6 251 wxFAIL_MSG( wxT("wxToolBar creation failed") );
c801d85f 252
8a0681f9
VZ
253 return FALSE;
254 }
a3622daa 255
8a0681f9
VZ
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 ) );
a3622daa 259
8a0681f9 260 SetToolSeparation(7);
3502e687
RR
261
262 if (style & wxTB_DOCKABLE)
263 {
264 m_widget = gtk_handle_box_new();
f03fc89f
VZ
265 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
266 gtk_widget_show( GTK_WIDGET(m_toolbar) );
8a0681f9 267
b0795d45 268#if (GTK_MINOR_VERSION > 0)
f03fc89f 269 if (style & wxTB_FLAT)
858b5bdd 270 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget), GTK_SHADOW_NONE );
b0795d45 271#endif
3502e687
RR
272 }
273 else
274 {
275 m_widget = GTK_WIDGET(m_toolbar);
276 }
8a0681f9 277
1144d24d 278 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
8a0681f9 279
858b5bdd
RR
280 if (style & wxTB_FLAT)
281 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar), GTK_RELIEF_NONE );
be25e480 282
83624f79
RR
283
284 m_fg = new GdkColor;
be25e480
RR
285 m_fg->red = 0;
286 m_fg->green = 0;
83624f79 287 m_fg->blue = 0;
be25e480
RR
288 wxColour fg(0,0,0);
289 fg.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ) );
290 m_fg->pixel = fg.GetPixel();
291
83624f79
RR
292 m_bg = new GdkColor;
293 m_bg->red = 65535;
294 m_bg->green = 65535;
be25e480
RR
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
fac4253c
RR
300 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar)->tooltips );
301
302 GtkStyle *g_style =
8a0681f9
VZ
303 gtk_style_copy(
304 gtk_widget_get_style(
305 GTK_TOOLBAR(m_toolbar)->tooltips->tip_window ) );
306
fac4253c 307 g_style->bg[GTK_STATE_NORMAL] = *m_bg;
f6bcfd97
BP
308 gdk_font_unref( g_style->font );
309 g_style->font = gdk_font_ref( GtkGetDefaultGuiFont() );
fac4253c 310 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar)->tooltips->tip_window, g_style );
a3622daa 311
f03fc89f 312 m_parent->DoAddChild( this );
8a0681f9 313
1144d24d 314 PostCreation();
a3622daa 315
1144d24d 316 Show( TRUE );
a3622daa 317
1144d24d 318 return TRUE;
fc008f25 319}
c801d85f 320
8a0681f9 321bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
c801d85f 322{
8a0681f9 323 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
c801d85f 324
6f67eafe
RR
325 // we have inserted a space before all the tools
326 if (m_xMargin > 1) pos++;
327
8a0681f9
VZ
328 if ( tool->IsButton() )
329 {
330 wxBitmap bitmap = tool->GetBitmap1();
c801d85f 331
8a0681f9
VZ
332 wxCHECK_MSG( bitmap.Ok(), FALSE,
333 wxT("invalid bitmap for wxToolBar icon") );
a3622daa 334
8a0681f9
VZ
335 wxCHECK_MSG( bitmap.GetBitmap() == NULL, FALSE,
336 wxT("wxToolBar doesn't support GdkBitmap") );
03f38c58 337
8a0681f9
VZ
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();
a3622daa 344
8a0681f9
VZ
345 GdkBitmap *mask = (GdkBitmap *)NULL;
346 if ( bitmap.GetMask() )
347 mask = bitmap.GetMask()->GetBitmap();
348
349 tool_pixmap = gtk_pixmap_new( pixmap, mask );
c693edf3 350#if (GTK_MINOR_VERSION > 0)
8a0681f9 351 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap), TRUE );
c693edf3 352#endif
8a0681f9
VZ
353
354 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
a3622daa 355
8a0681f9
VZ
356 tool->m_pixmap = tool_pixmap;
357 }
c801d85f 358
8a0681f9
VZ
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;
bf9e3e73 396
8a0681f9
VZ
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 }
bf9e3e73 407
bf9e3e73 408 GtkRequisition req;
2afa14f2
OK
409 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
410 (m_widget, &req );
00655497 411 m_width = req.width + m_xMargin;
6f67eafe 412 m_height = req.height + 2*m_yMargin;
bf9e3e73 413
bf9e3e73
RR
414 return TRUE;
415}
416
9c2882d9 417bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
c801d85f 418{
8a0681f9 419 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
c801d85f 420
8a0681f9 421 switch ( tool->GetStyle() )
97d7bfb8 422 {
8a0681f9
VZ
423 case wxTOOL_STYLE_CONTROL:
424 tool->GetControl()->Destroy();
425 break;
97d7bfb8 426
8a0681f9
VZ
427 case wxTOOL_STYLE_BUTTON:
428 gtk_widget_destroy( tool->m_item );
429 break;
97d7bfb8 430
8a0681f9
VZ
431 //case wxTOOL_STYLE_SEPARATOR: -- nothing to do
432 }
c801d85f 433
1144d24d 434 return TRUE;
fc008f25 435}
46dc76ba 436
8a0681f9
VZ
437// ----------------------------------------------------------------------------
438// wxToolBar tools state
439// ----------------------------------------------------------------------------
440
441void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable)
c801d85f 442{
c693edf3 443#if (GTK_MINOR_VERSION > 0)
8a0681f9
VZ
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 );
c693edf3 450#endif
fc008f25 451}
c801d85f 452
8a0681f9 453void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle )
c801d85f 454{
8a0681f9
VZ
455 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
456
457 GtkWidget *item = tool->m_item;
458 if ( item && GTK_IS_TOGGLE_BUTTON(item) )
1144d24d 459 {
8a0681f9
VZ
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 );
1144d24d 469 }
c801d85f 470
8a0681f9
VZ
471 m_blockNextEvent = TRUE; // we cannot use gtk_signal_disconnect here
472
473 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item), toggle );
1144d24d 474 }
fc008f25 475}
c801d85f 476
8a0681f9
VZ
477void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
478 bool WXUNUSED(toggle))
c801d85f 479{
8a0681f9
VZ
480 // VZ: absolutely no idea about how to do it
481 wxFAIL_MSG( _T("not implemented") );
fc008f25 482}
c801d85f 483
8a0681f9
VZ
484// ----------------------------------------------------------------------------
485// wxToolBar geometry
486// ----------------------------------------------------------------------------
487
488wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x),
489 wxCoord WXUNUSED(y)) const
c801d85f 490{
8a0681f9
VZ
491 // VZ: GTK+ doesn't seem to have such thing
492 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
493
494 return (wxToolBarToolBase *)NULL;
fc008f25 495}
c801d85f 496
1144d24d 497void wxToolBar::SetMargins( int x, int y )
c801d85f 498{
8a0681f9
VZ
499 wxCHECK_RET( GetToolsCount() == 0,
500 wxT("wxToolBar::SetMargins must be called before adding tools.") );
1144d24d 501
00655497 502 if (x > 1) gtk_toolbar_append_space( m_toolbar ); // oh well
1144d24d
RR
503
504 m_xMargin = x;
505 m_yMargin = y;
fc008f25 506}
c801d85f 507
cf4219e7 508void wxToolBar::SetToolSeparation( int separation )
c801d85f 509{
1144d24d 510 gtk_toolbar_set_space_size( m_toolbar, separation );
8a0681f9 511 m_toolSeparation = separation;
1144d24d
RR
512}
513
8a0681f9
VZ
514// ----------------------------------------------------------------------------
515// wxToolBar idle handling
516// ----------------------------------------------------------------------------
1144d24d 517
9b7e522a
RR
518void wxToolBar::OnInternalIdle()
519{
520 wxCursor cursor = m_cursor;
521 if (g_globalCursor.Ok()) cursor = g_globalCursor;
522
f7a11f8c 523 if (cursor.Ok())
9b7e522a 524 {
f7a11f8c 525 /* I now set the cursor the anew in every OnInternalIdle call
8a0681f9
VZ
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. */
85ec2f26
RR
529
530 if (HasFlag(wxTB_DOCKABLE) && (m_widget->window))
9b7e522a 531 {
8a0681f9
VZ
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 }
9b7e522a
RR
557 }
558 }
559
560 UpdateWindowUI();
561}
562
dcf924a3 563#endif