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