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