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