]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/tbargtk.cpp
added refcounting to wxCharBuffer to fix passing of wxCharBuffer to printf-like functions
[wxWidgets.git] / src / gtk / tbargtk.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
76b49cf4 2// Name: src/gtk/tbargtk.cpp
c801d85f
KB
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
65571936 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
14f355c2
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
8a0681f9 14#if wxUSE_TOOLBAR_NATIVE
dcf924a3 15
4e3e485b
WS
16#include "wx/toolbar.h"
17
9e691f46 18#include "wx/gtk/private.h"
fc6557a6 19
8a0681f9
VZ
20// ----------------------------------------------------------------------------
21// globals
22// ----------------------------------------------------------------------------
acfd422a 23
314055fa 24// data
9b7e522a
RR
25extern bool g_blockEventsOnDrag;
26extern wxCursor g_globalCursor;
314055fa 27
8a0681f9
VZ
28// ----------------------------------------------------------------------------
29// wxToolBarTool
30// ----------------------------------------------------------------------------
31
32class wxToolBarTool : public wxToolBarToolBase
33{
34public:
35 wxToolBarTool(wxToolBar *tbar,
36 int id,
e76c0b5f 37 const wxString& label,
8a0681f9
VZ
38 const wxBitmap& bitmap1,
39 const wxBitmap& bitmap2,
e76c0b5f 40 wxItemKind kind,
8a0681f9
VZ
41 wxObject *clientData,
42 const wxString& shortHelpString,
43 const wxString& longHelpString)
e76c0b5f 44 : wxToolBarToolBase(tbar, id, label, bitmap1, bitmap2, kind,
8a0681f9
VZ
45 clientData, shortHelpString, longHelpString)
46 {
a1cb0b11 47 m_item = NULL;
8a0681f9
VZ
48 }
49
07d02e9e
VZ
50 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
51 : wxToolBarToolBase(tbar, control, label)
8a0681f9 52 {
a1cb0b11 53 m_item = NULL;
ddb57e45
PC
54 // Hold a reference to keep control alive until DoInsertTool() is
55 // called, or if RemoveTool() is called (see DoDeleteTool)
56 g_object_ref(control->m_widget);
57 // release reference when gtk_widget_destroy() is called on control
58 g_signal_connect(
59 control->m_widget, "destroy", G_CALLBACK(g_object_unref), NULL);
1be45608
VZ
60 }
61
a1cb0b11
PC
62 void SetImage();
63 void CreateDropDown();
64 void ShowDropdown(GtkToggleButton* button);
8a0681f9 65
a1cb0b11 66 GtkToolItem* m_item;
8a0681f9
VZ
67};
68
69// ----------------------------------------------------------------------------
70// wxWin macros
71// ----------------------------------------------------------------------------
72
2eb10e2a 73IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
8a0681f9
VZ
74
75// ============================================================================
76// implementation
77// ============================================================================
78
c801d85f 79//-----------------------------------------------------------------------------
a1cb0b11 80// "clicked" from m_item
c801d85f
KB
81//-----------------------------------------------------------------------------
82
865bb325 83extern "C" {
a1cb0b11 84static void item_clicked(GtkToolButton*, wxToolBarTool* tool)
c801d85f 85{
1144d24d 86 if (g_blockEventsOnDrag) return;
a3622daa 87
a1cb0b11
PC
88 tool->GetToolBar()->OnLeftClick(tool->GetId(), false);
89}
90}
7062497f 91
a1cb0b11
PC
92//-----------------------------------------------------------------------------
93// "toggled" from m_item
94//-----------------------------------------------------------------------------
8a0681f9 95
a1cb0b11
PC
96extern "C" {
97static void item_toggled(GtkToggleToolButton* button, wxToolBarTool* tool)
98{
99 if (g_blockEventsOnDrag) return;
38762f09 100
a1cb0b11
PC
101 const bool active = gtk_toggle_tool_button_get_active(button) != 0;
102 tool->Toggle(active);
103 if (!active && tool->GetKind() == wxITEM_RADIO)
104 return;
a3622daa 105
a1cb0b11 106 if (!tool->GetToolBar()->OnLeftClick(tool->GetId(), active))
6bb7cee4
VZ
107 {
108 // revert back
109 tool->Toggle();
6bb7cee4 110 }
fc008f25 111}
865bb325 112}
c801d85f 113
729b4756 114//-----------------------------------------------------------------------------
a1cb0b11 115// "button_press_event" from m_item child
729b4756 116//-----------------------------------------------------------------------------
a1cb0b11 117
729b4756 118extern "C" {
a1cb0b11
PC
119static gboolean
120button_press_event(GtkWidget*, GdkEventButton* event, wxToolBarTool* tool)
729b4756
RR
121{
122 if (event->button != 3)
123 return FALSE;
124
729b4756 125 if (g_blockEventsOnDrag) return TRUE;
729b4756 126
a1cb0b11
PC
127 tool->GetToolBar()->OnRightClick(
128 tool->GetId(), int(event->x), int(event->y));
729b4756
RR
129
130 return TRUE;
131}
132}
133
fc6557a6 134//-----------------------------------------------------------------------------
a1cb0b11 135// "child_detached" from m_widget
fc6557a6
RR
136//-----------------------------------------------------------------------------
137
138extern "C" {
a1cb0b11 139static void child_detached(GtkWidget*, GtkToolbar* toolbar, void*)
fc6557a6 140{
a1cb0b11
PC
141 // disable showing overflow arrow when toolbar is detached,
142 // otherwise toolbar collapses to just an arrow
143 gtk_toolbar_set_show_arrow(toolbar, false);
fc6557a6
RR
144}
145}
146
147//-----------------------------------------------------------------------------
a1cb0b11 148// "child_attached" from m_widget
fc6557a6
RR
149//-----------------------------------------------------------------------------
150
a1cb0b11
PC
151extern "C" {
152static void child_attached(GtkWidget*, GtkToolbar* toolbar, void*)
fc6557a6 153{
a1cb0b11 154 gtk_toolbar_set_show_arrow(toolbar, true);
fc6557a6
RR
155}
156}
157
2f2aa628 158//-----------------------------------------------------------------------------
a1cb0b11 159// "enter_notify_event" / "leave_notify_event" from m_item
2f2aa628
RR
160//-----------------------------------------------------------------------------
161
865bb325 162extern "C" {
a1cb0b11
PC
163static gboolean
164enter_notify_event(GtkWidget*, GdkEventCrossing* event, wxToolBarTool* tool)
314055fa 165{
1144d24d 166 if (g_blockEventsOnDrag) return TRUE;
248bcf0a 167
a1cb0b11
PC
168 int id = -1;
169 if (event->type == GDK_ENTER_NOTIFY)
170 id = tool->GetId();
171 tool->GetToolBar()->OnMouseEnter(id);
248bcf0a 172
1144d24d 173 return FALSE;
314055fa 174}
865bb325 175}
314055fa 176
cca410b3
PC
177//-----------------------------------------------------------------------------
178// "size_request" from m_toolbar
179//-----------------------------------------------------------------------------
180
181extern "C" {
182static void
183size_request(GtkWidget*, GtkRequisition* req, wxToolBar* win)
184{
185 const wxSize margins = win->GetMargins();
186 req->width += margins.x;
187 req->height += 2 * margins.y;
188}
189}
190
a1cb0b11
PC
191//-----------------------------------------------------------------------------
192// "expose_event" from GtkImage inside m_item
193//-----------------------------------------------------------------------------
194
195extern "C" {
196static gboolean
197image_expose_event(GtkWidget* widget, GdkEventExpose*, wxToolBarTool* tool)
198{
199 const wxBitmap& bitmap = tool->GetDisabledBitmap();
200 if (tool->IsEnabled() || !bitmap.IsOk())
201 return false;
202
203 // draw disabled bitmap ourselves, GtkImage has no way to specify it
204 const GtkAllocation& alloc = widget->allocation;
205 gdk_draw_pixbuf(
206 widget->window, widget->style->black_gc, bitmap.GetPixbuf(),
207 0, 0,
208 alloc.x + (alloc.width - widget->requisition.width) / 2,
209 alloc.y + (alloc.height - widget->requisition.height) / 2,
210 -1, -1, GDK_RGB_DITHER_NORMAL, 0, 0);
211 return true;
212}
213}
214
215//-----------------------------------------------------------------------------
216// "toggled" from dropdown menu button
217//-----------------------------------------------------------------------------
218
219extern "C" {
220static void arrow_toggled(GtkToggleButton* button, wxToolBarTool* tool)
221{
222 if (gtk_toggle_button_get_active(button))
223 {
224 tool->ShowDropdown(button);
225 gtk_toggle_button_set_active(button, false);
226 }
227}
228}
229
230//-----------------------------------------------------------------------------
231// "button_press_event" from dropdown menu button
232//-----------------------------------------------------------------------------
233
234extern "C" {
235static gboolean
236arrow_button_press_event(GtkToggleButton* button, GdkEventButton* event, wxToolBarTool* tool)
237{
238 if (event->button == 1)
239 {
240 g_signal_handlers_block_by_func(button, (void*)arrow_toggled, tool);
241 gtk_toggle_button_set_active(button, true);
242 tool->ShowDropdown(button);
243 gtk_toggle_button_set_active(button, false);
244 g_signal_handlers_unblock_by_func(button, (void*)arrow_toggled, tool);
245 return true;
246 }
247 return false;
248}
249}
250
bf9e3e73
RR
251//-----------------------------------------------------------------------------
252// InsertChild callback for wxToolBar
253//-----------------------------------------------------------------------------
254
c821db16 255static void wxInsertChildInToolBar( wxWindow* WXUNUSED(parent),
ddb57e45 256 wxWindow* /* child */)
bf9e3e73 257{
ddb57e45 258 // Child widget will be inserted into GtkToolbar by DoInsertTool()
bf9e3e73
RR
259}
260
8a0681f9
VZ
261// ----------------------------------------------------------------------------
262// wxToolBarTool
263// ----------------------------------------------------------------------------
c801d85f 264
a1cb0b11 265void wxToolBarTool::SetImage()
8a0681f9 266{
a1cb0b11
PC
267 const wxBitmap& bitmap = GetNormalBitmap();
268 wxCHECK_RET(bitmap.IsOk(), "invalid bitmap for wxToolBar icon");
269
270 GtkWidget* image = gtk_tool_button_get_icon_widget(GTK_TOOL_BUTTON(m_item));
271 // always use pixbuf, because pixmap mask does not
272 // work with disabled images in some themes
273 gtk_image_set_from_pixbuf(GTK_IMAGE(image), bitmap.GetPixbuf());
274}
275
276// helper to create a dropdown menu item
277void wxToolBarTool::CreateDropDown()
278{
279 gtk_tool_item_set_homogeneous(m_item, false);
280 GtkWidget* box;
281 GtkWidget* arrow;
282 if (GetToolBar()->HasFlag(wxTB_LEFT | wxTB_RIGHT))
283 {
284 box = gtk_vbox_new(false, 0);
285 arrow = gtk_arrow_new(GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
286 }
287 else
288 {
289 box = gtk_hbox_new(false, 0);
290 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
291 }
292 GtkWidget* tool_button = GTK_BIN(m_item)->child;
293 gtk_widget_reparent(tool_button, box);
294 GtkWidget* arrow_button = gtk_toggle_button_new();
295 gtk_button_set_relief(GTK_BUTTON(arrow_button),
296 gtk_tool_item_get_relief_style(GTK_TOOL_ITEM(m_item)));
297 gtk_container_add(GTK_CONTAINER(arrow_button), arrow);
298 gtk_container_add(GTK_CONTAINER(box), arrow_button);
299 gtk_widget_show_all(box);
300 gtk_container_add(GTK_CONTAINER(m_item), box);
301
302 g_signal_connect(arrow_button, "toggled", G_CALLBACK(arrow_toggled), this);
303 g_signal_connect(arrow_button, "button_press_event",
304 G_CALLBACK(arrow_button_press_event), this);
305}
306
307void wxToolBarTool::ShowDropdown(GtkToggleButton* button)
308{
309 wxToolBarBase* toolbar = GetToolBar();
310 wxCommandEvent event(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED, GetId());
311 if (!toolbar->HandleWindowEvent(event))
312 {
313 wxMenu* menu = GetDropdownMenu();
314 if (menu)
315 {
316 const GtkAllocation& alloc = GTK_WIDGET(button)->allocation;
317 int x = alloc.x;
318 int y = alloc.y;
319 if (toolbar->HasFlag(wxTB_LEFT | wxTB_RIGHT))
320 x += alloc.width;
321 else
322 y += alloc.height;
323 toolbar->PopupMenu(menu, x, y);
324 }
325 }
8a0681f9 326}
c801d85f 327
8a0681f9 328wxToolBarToolBase *wxToolBar::CreateTool(int id,
e76c0b5f 329 const wxString& text,
8a0681f9
VZ
330 const wxBitmap& bitmap1,
331 const wxBitmap& bitmap2,
e76c0b5f 332 wxItemKind kind,
8a0681f9
VZ
333 wxObject *clientData,
334 const wxString& shortHelpString,
335 const wxString& longHelpString)
336{
e76c0b5f 337 return new wxToolBarTool(this, id, text, bitmap1, bitmap2, kind,
8a0681f9
VZ
338 clientData, shortHelpString, longHelpString);
339}
b1da76e1 340
07d02e9e
VZ
341wxToolBarToolBase *
342wxToolBar::CreateTool(wxControl *control, const wxString& label)
c801d85f 343{
07d02e9e 344 return new wxToolBarTool(this, control, label);
fc008f25 345}
c801d85f 346
8a0681f9
VZ
347//-----------------------------------------------------------------------------
348// wxToolBar construction
349//-----------------------------------------------------------------------------
350
351void wxToolBar::Init()
c801d85f 352{
8a0681f9 353 m_toolbar = (GtkToolbar *)NULL;
a1cb0b11 354 m_tooltips = NULL;
fc008f25 355}
c801d85f 356
a3622daa 357wxToolBar::~wxToolBar()
c801d85f 358{
a1cb0b11
PC
359 if (m_tooltips)
360 {
361 gtk_object_destroy(GTK_OBJECT(m_tooltips));
362 g_object_unref(m_tooltips);
363 }
fc008f25 364}
c801d85f 365
8a0681f9
VZ
366bool wxToolBar::Create( wxWindow *parent,
367 wxWindowID id,
368 const wxPoint& pos,
369 const wxSize& size,
370 long style,
371 const wxString& name )
c801d85f 372{
c821db16 373 m_insertCallback = wxInsertChildInToolBar;
a3622daa 374
8a0681f9
VZ
375 if ( !PreCreation( parent, pos, size ) ||
376 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
4dcaf11a 377 {
223d09f6 378 wxFAIL_MSG( wxT("wxToolBar creation failed") );
c801d85f 379
91af0895 380 return false;
8a0681f9 381 }
a3622daa 382
d408730c
VZ
383 FixupStyle();
384
9e691f46 385 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new() );
a1cb0b11
PC
386 m_tooltips = gtk_tooltips_new();
387 g_object_ref(m_tooltips);
388 gtk_object_sink(GTK_OBJECT(m_tooltips));
e76c0b5f 389 GtkSetStyle();
99e8cb50 390
3502e687
RR
391 if (style & wxTB_DOCKABLE)
392 {
393 m_widget = gtk_handle_box_new();
a1cb0b11
PC
394
395 g_signal_connect(m_widget, "child_detached",
396 G_CALLBACK(child_detached), NULL);
397 g_signal_connect(m_widget, "child_attached",
398 G_CALLBACK(child_attached), NULL);
8a0681f9 399
f03fc89f 400 if (style & wxTB_FLAT)
858b5bdd 401 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget), GTK_SHADOW_NONE );
3502e687
RR
402 }
403 else
248bcf0a
RD
404 {
405 m_widget = gtk_event_box_new();
248bcf0a 406 ConnectWidget( m_widget );
3502e687 407 }
a1cb0b11
PC
408 gtk_container_add(GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar));
409 gtk_widget_show(GTK_WIDGET(m_toolbar));
be25e480 410
f03fc89f 411 m_parent->DoAddChild( this );
8a0681f9 412
abdeb9e7 413 PostCreation(size);
a3622daa 414
cca410b3
PC
415 g_signal_connect_after(m_toolbar, "size_request",
416 G_CALLBACK(size_request), this);
417
91af0895 418 return true;
fc008f25 419}
c801d85f 420
e4161a2a 421GdkWindow *wxToolBar::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
48468900
RR
422{
423 return GTK_WIDGET(m_toolbar)->window;
424}
425
e76c0b5f
VZ
426void wxToolBar::GtkSetStyle()
427{
a1cb0b11
PC
428 GtkOrientation orient = GTK_ORIENTATION_HORIZONTAL;
429 if (HasFlag(wxTB_LEFT | wxTB_RIGHT))
430 orient = GTK_ORIENTATION_VERTICAL;
431
432 GtkToolbarStyle style = GTK_TOOLBAR_ICONS;
433 if (HasFlag(wxTB_NOICONS))
434 style = GTK_TOOLBAR_TEXT;
435 else if (HasFlag(wxTB_TEXT))
436 {
437 style = GTK_TOOLBAR_BOTH;
438 if (HasFlag(wxTB_HORZ_LAYOUT))
439 style = GTK_TOOLBAR_BOTH_HORIZ;
440 }
e76c0b5f
VZ
441
442 gtk_toolbar_set_orientation(m_toolbar, orient);
443 gtk_toolbar_set_style(m_toolbar, style);
444}
445
446void wxToolBar::SetWindowStyleFlag( long style )
447{
448 wxToolBarBase::SetWindowStyleFlag(style);
8ad31f9d 449
e76c0b5f
VZ
450 if ( m_toolbar )
451 GtkSetStyle();
452}
453
8a0681f9 454bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
c801d85f 455{
8c4e2405 456 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase);
248bcf0a 457
a1cb0b11 458 GSList* radioGroup;
8a0681f9
VZ
459 switch ( tool->GetStyle() )
460 {
461 case wxTOOL_STYLE_BUTTON:
a1cb0b11 462 switch (tool->GetKind())
8a0681f9 463 {
a1cb0b11
PC
464 case wxITEM_CHECK:
465 tool->m_item = gtk_toggle_tool_button_new();
466 g_signal_connect(tool->m_item, "toggled",
467 G_CALLBACK(item_toggled), tool);
468 break;
469 case wxITEM_RADIO:
470 radioGroup = GetRadioGroup(pos);
471 if (radioGroup)
38762f09
VZ
472 {
473 // this is the first button in the radio button group,
474 // it will be toggled automatically by GTK so bring the
475 // internal flag in sync
91af0895 476 tool->Toggle(true);
38762f09 477 }
a1cb0b11
PC
478 tool->m_item = gtk_radio_tool_button_new(radioGroup);
479 g_signal_connect(tool->m_item, "toggled",
480 G_CALLBACK(item_toggled), tool);
481 break;
482 default:
483 wxFAIL_MSG("unknown toolbar child type");
484 // fall through
485 case wxITEM_DROPDOWN:
486 case wxITEM_NORMAL:
487 tool->m_item = gtk_tool_button_new(NULL, "");
488 g_signal_connect(tool->m_item, "clicked",
489 G_CALLBACK(item_clicked), tool);
490 break;
491 }
492 if (!HasFlag(wxTB_NOICONS))
493 {
494 GtkWidget* image = gtk_image_new();
495 gtk_tool_button_set_icon_widget(
496 GTK_TOOL_BUTTON(tool->m_item), image);
497 tool->SetImage();
498 gtk_widget_show(image);
499 g_signal_connect(image, "expose_event",
500 G_CALLBACK(image_expose_event), tool);
8a0681f9 501 }
a1cb0b11
PC
502 if (!tool->GetLabel().empty())
503 {
504 gtk_tool_button_set_label(
505 GTK_TOOL_BUTTON(tool->m_item), wxGTK_CONV(tool->GetLabel()));
506 // needed for labels in horizontal toolbar with wxTB_HORZ_LAYOUT
507 gtk_tool_item_set_is_important(tool->m_item, true);
508 }
509 if (!HasFlag(wxTB_NO_TOOLTIPS) && !tool->GetShortHelp().empty())
510 {
511 gtk_tool_item_set_tooltip(tool->m_item,
512 m_tooltips, wxGTK_CONV(tool->GetShortHelp()), "");
513 }
514 g_signal_connect(GTK_BIN(tool->m_item)->child, "button_press_event",
515 G_CALLBACK(button_press_event), tool);
516 g_signal_connect(tool->m_item, "enter_notify_event",
517 G_CALLBACK(enter_notify_event), tool);
518 g_signal_connect(tool->m_item, "leave_notify_event",
519 G_CALLBACK(enter_notify_event), tool);
520
521 if (tool->GetKind() == wxITEM_DROPDOWN)
522 tool->CreateDropDown();
8a0681f9
VZ
523 break;
524
525 case wxTOOL_STYLE_SEPARATOR:
a1cb0b11 526 tool->m_item = gtk_separator_tool_item_new();
1be45608 527 break;
bf9e3e73 528
8a0681f9 529 case wxTOOL_STYLE_CONTROL:
1be45608 530 GtkWidget * const align = gtk_alignment_new(0.5, 0.5, 0, 0);
abdf096a 531 gtk_widget_show(align);
a1cb0b11
PC
532 wxWindow* control = tool->GetControl();
533 gtk_container_add(GTK_CONTAINER(align), control->m_widget);
534 tool->m_item = gtk_tool_item_new();
535 gtk_container_add(GTK_CONTAINER(tool->m_item), align);
536 // Inserted items "slide" into place using an animated effect that
537 // causes multiple size events on the item. Must set size request
538 // to keep item size from getting permanently set too small by the
539 // first of these size events.
540 const wxSize size = control->GetSize();
541 gtk_widget_set_size_request(control->m_widget, size.x, size.y);
8a0681f9
VZ
542 break;
543 }
a1cb0b11
PC
544 gtk_widget_show(GTK_WIDGET(tool->m_item));
545 gtk_toolbar_insert(m_toolbar, tool->m_item, int(pos));
bf9e3e73 546
9f884528 547 InvalidateBestSize();
bf9e3e73 548
91af0895 549 return true;
bf9e3e73
RR
550}
551
a1cb0b11 552bool wxToolBar::DoDeleteTool(size_t /* pos */, wxToolBarToolBase* toolBase)
c801d85f 553{
8c4e2405 554 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase);
c801d85f 555
a1cb0b11 556 if (tool->GetStyle() == wxTOOL_STYLE_CONTROL)
97d7bfb8 557 {
a1cb0b11
PC
558 // don't destroy the control here as we can be called from
559 // RemoveTool() and then we need to keep the control alive;
560 // while if we're called from DeleteTool() the control will
561 // be destroyed when wxToolBarToolBase itself is deleted
562 GtkWidget* widget = tool->GetControl()->m_widget;
563 gtk_container_remove(GTK_CONTAINER(widget->parent), widget);
8a0681f9 564 }
a1cb0b11
PC
565 gtk_object_destroy(GTK_OBJECT(tool->m_item));
566 tool->m_item = NULL;
c801d85f 567
9f884528 568 InvalidateBestSize();
91af0895 569 return true;
fc008f25 570}
46dc76ba 571
a1cb0b11
PC
572GSList* wxToolBar::GetRadioGroup(size_t pos)
573{
574 GSList* radioGroup = NULL;
575 GtkToolItem* item = NULL;
576 if (pos > 0)
577 {
578 item = gtk_toolbar_get_nth_item(m_toolbar, int(pos) - 1);
579 if (!GTK_IS_RADIO_TOOL_BUTTON(item))
580 item = NULL;
581 }
582 if (item == NULL && pos < m_tools.size())
583 {
584 item = gtk_toolbar_get_nth_item(m_toolbar, int(pos));
585 if (!GTK_IS_RADIO_TOOL_BUTTON(item))
586 item = NULL;
587 }
588 if (item)
589 radioGroup = gtk_radio_tool_button_get_group((GtkRadioToolButton*)item);
590 return radioGroup;
591}
592
8a0681f9
VZ
593// ----------------------------------------------------------------------------
594// wxToolBar tools state
595// ----------------------------------------------------------------------------
596
597void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable)
c801d85f 598{
8c4e2405 599 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase);
8a0681f9 600
8a0681f9 601 if (tool->m_item)
a1cb0b11 602 gtk_widget_set_sensitive(GTK_WIDGET(tool->m_item), enable);
fc008f25 603}
c801d85f 604
248bcf0a 605void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle )
c801d85f 606{
8c4e2405 607 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase);
8a0681f9 608
a1cb0b11 609 if (tool->m_item)
1144d24d 610 {
a1cb0b11 611 g_signal_handlers_block_by_func(tool->m_item, (void*)item_toggled, tool);
8a0681f9 612
a1cb0b11
PC
613 gtk_toggle_tool_button_set_active(
614 GTK_TOGGLE_TOOL_BUTTON(tool->m_item), toggle);
248bcf0a 615
a1cb0b11 616 g_signal_handlers_unblock_by_func(tool->m_item, (void*)item_toggled, tool);
1144d24d 617 }
fc008f25 618}
c801d85f 619
8a0681f9
VZ
620void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
621 bool WXUNUSED(toggle))
c801d85f 622{
8a0681f9
VZ
623 // VZ: absolutely no idea about how to do it
624 wxFAIL_MSG( _T("not implemented") );
fc008f25 625}
c801d85f 626
8a0681f9
VZ
627// ----------------------------------------------------------------------------
628// wxToolBar geometry
629// ----------------------------------------------------------------------------
630
a1cb0b11
PC
631wxSize wxToolBar::DoGetBestSize() const
632{
633 // Unfortunately, if overflow arrow is enabled GtkToolbar only reports size
634 // of arrow. To get the real size, the arrow is temporarily disabled here.
635 // This is gross, since it will cause a queue_resize, and could potentially
636 // lead to an infinite loop. But there seems to be no alternative, short of
637 // disabling the arrow entirely.
638 gtk_toolbar_set_show_arrow(m_toolbar, false);
639 const wxSize size = wxToolBarBase::DoGetBestSize();
640 gtk_toolbar_set_show_arrow(m_toolbar, true);
641 return size;
642}
643
8a0681f9
VZ
644wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x),
645 wxCoord WXUNUSED(y)) const
c801d85f 646{
8a0681f9
VZ
647 // VZ: GTK+ doesn't seem to have such thing
648 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
649
650 return (wxToolBarToolBase *)NULL;
fc008f25 651}
c801d85f 652
a1f79c1e
VZ
653void wxToolBar::SetToolShortHelp( int id, const wxString& helpString )
654{
8c4e2405 655 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
a1f79c1e
VZ
656
657 if ( tool )
658 {
659 (void)tool->SetShortHelp(helpString);
a1cb0b11
PC
660 if (tool->m_item)
661 {
662 gtk_tool_item_set_tooltip(tool->m_item,
663 m_tooltips, wxGTK_CONV(helpString), "");
664 }
a1f79c1e
VZ
665 }
666}
667
bbd321ff
RD
668void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
669{
670 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
671 if ( tool )
672 {
673 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
674
675 tool->SetNormalBitmap(bitmap);
a1cb0b11 676 tool->SetImage();
f4322df6 677 }
bbd321ff
RD
678}
679
680void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
681{
682 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
683 if ( tool )
684 {
685 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
686
687 tool->SetDisabledBitmap(bitmap);
f4322df6 688 }
bbd321ff
RD
689}
690
8a0681f9
VZ
691// ----------------------------------------------------------------------------
692// wxToolBar idle handling
693// ----------------------------------------------------------------------------
1144d24d 694
9b7e522a
RR
695void wxToolBar::OnInternalIdle()
696{
1417c811
RR
697 // Check if we have to show window now
698 if (GtkShowFromOnIdle()) return;
f4322df6 699
9b7e522a
RR
700 wxCursor cursor = m_cursor;
701 if (g_globalCursor.Ok()) cursor = g_globalCursor;
702
f7a11f8c 703 if (cursor.Ok())
9b7e522a 704 {
f7a11f8c 705 /* I now set the cursor the anew in every OnInternalIdle call
8a0681f9
VZ
706 as setting the cursor in a parent window also effects the
707 windows above so that checking for the current cursor is
708 not possible. */
85ec2f26
RR
709
710 if (HasFlag(wxTB_DOCKABLE) && (m_widget->window))
9b7e522a 711 {
8a0681f9
VZ
712 /* if the toolbar is dockable, then m_widget stands for the
713 GtkHandleBox widget, which uses its own window so that we
714 can set the cursor for it. if the toolbar is not dockable,
715 m_widget comes from m_toolbar which uses its parent's
716 window ("windowless windows") and thus we cannot set the
717 cursor. */
718 gdk_window_set_cursor( m_widget->window, cursor.GetCursor() );
719 }
720
222ed1d6 721 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
8a0681f9
VZ
722 while ( node )
723 {
724 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
725 node = node->GetNext();
726
a1cb0b11 727 if (tool->m_item)
8a0681f9 728 {
a1cb0b11 729 GdkWindow* window = GTK_WIDGET(tool->m_item)->window;
8a0681f9
VZ
730
731 if ( window )
732 {
733 gdk_window_set_cursor( window, cursor.GetCursor() );
734 }
735 }
9b7e522a
RR
736 }
737 }
738
ffa51479 739 if (wxUpdateUIEvent::CanUpdate(this) && IsShownOnScreen())
e39af974 740 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
9b7e522a
RR
741}
742
9d522606
RD
743
744// ----------------------------------------------------------------------------
745
746// static
747wxVisualAttributes
748wxToolBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
749{
9d522606 750 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new);
9d522606
RD
751}
752
a1f79c1e 753#endif // wxUSE_TOOLBAR_NATIVE