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