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