1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: GTK toolbar
4 // Author: Robert Roebling
5 // Modified: 13.12.99 by VZ to derive from wxToolBarBase
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #include "wx/toolbar.h"
24 #if wxUSE_TOOLBAR_NATIVE
29 #include "wx/gtk/private.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
36 extern void wxapp_install_idle_handler();
40 extern bool g_blockEventsOnDrag
;
41 extern wxCursor g_globalCursor
;
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // translate wxWidgets toolbar style flags to GTK orientation and style
48 static void GetGtkStyle(long style
,
49 GtkOrientation
*orient
, GtkToolbarStyle
*gtkStyle
)
51 *orient
= style
& wxTB_VERTICAL
? GTK_ORIENTATION_VERTICAL
52 : GTK_ORIENTATION_HORIZONTAL
;
55 if ( style
& wxTB_TEXT
)
57 *gtkStyle
= style
& wxTB_NOICONS
60 style
& wxTB_HORZ_LAYOUT
? GTK_TOOLBAR_BOTH_HORIZ
:
63 else // no text, hence we must have the icons or what would we show?
65 *gtkStyle
= GTK_TOOLBAR_ICONS
;
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 class wxToolBarTool
: public wxToolBarToolBase
76 wxToolBarTool(wxToolBar
*tbar
,
78 const wxString
& label
,
79 const wxBitmap
& bitmap1
,
80 const wxBitmap
& bitmap2
,
83 const wxString
& shortHelpString
,
84 const wxString
& longHelpString
)
85 : wxToolBarToolBase(tbar
, id
, label
, bitmap1
, bitmap2
, kind
,
86 clientData
, shortHelpString
, longHelpString
)
91 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
92 : wxToolBarToolBase(tbar
, control
)
97 // is this a radio button?
99 // unlike GetKind(), can be called for any kind of tools, not just buttons
100 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO
; }
102 // this is only called for the normal buttons, i.e. not separators nor
104 GtkToolbarChildType
GetGtkChildType() const
109 return GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
112 return GTK_TOOLBAR_CHILD_RADIOBUTTON
;
115 wxFAIL_MSG( _T("unknown toolbar child type") );
119 return GTK_TOOLBAR_CHILD_BUTTON
;
123 void SetPixmap(const wxBitmap
& bitmap
)
127 GdkBitmap
*mask
= bitmap
.GetMask() ? bitmap
.GetMask()->GetBitmap()
129 if (bitmap
.HasPixbuf())
130 gtk_image_set_from_pixbuf( GTK_IMAGE(m_pixmap
), bitmap
.GetPixbuf() );
132 gtk_pixmap_set( GTK_PIXMAP(m_pixmap
), bitmap
.GetPixmap(), mask
);
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
149 // ============================================================================
151 // ============================================================================
153 //-----------------------------------------------------------------------------
154 // "clicked" (internal from gtk_toolbar)
155 //-----------------------------------------------------------------------------
158 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
),
159 wxToolBarTool
*tool
)
162 wxapp_install_idle_handler();
164 wxToolBar
*tbar
= (wxToolBar
*)tool
->GetToolBar();
166 if (tbar
->m_blockEvent
) return;
168 if (g_blockEventsOnDrag
) return;
169 if (!tool
->IsEnabled()) return;
171 if (tool
->CanBeToggled())
175 tool
->SetPixmap(tool
->GetBitmap());
177 if ( tool
->IsRadio() && !tool
->IsToggled() )
179 // radio button went up, don't report this as a wxWin event
184 if( !tbar
->OnLeftClick( tool
->GetId(), tool
->IsToggled() ) && tool
->CanBeToggled() )
189 tool
->SetPixmap(tool
->GetBitmap());
194 //-----------------------------------------------------------------------------
195 // "enter_notify_event" / "leave_notify_event"
196 //-----------------------------------------------------------------------------
199 static gint
gtk_toolbar_tool_callback( GtkWidget
*WXUNUSED(widget
),
200 GdkEventCrossing
*gdk_event
,
201 wxToolBarTool
*tool
)
203 if (g_isIdle
) wxapp_install_idle_handler();
205 if (g_blockEventsOnDrag
) return TRUE
;
207 wxToolBar
*tb
= (wxToolBar
*)tool
->GetToolBar();
210 if( gdk_event
->type
== GDK_ENTER_NOTIFY
)
211 tb
->OnMouseEnter( tool
->GetId() );
213 tb
->OnMouseEnter( -1 );
219 //-----------------------------------------------------------------------------
220 // InsertChild callback for wxToolBar
221 //-----------------------------------------------------------------------------
223 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
),
224 wxWindow
* WXUNUSED(child
) )
226 // we don't do anything here
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 void wxToolBarTool::Init()
236 m_pixmap
= (GtkWidget
*)NULL
;
239 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
240 const wxString
& text
,
241 const wxBitmap
& bitmap1
,
242 const wxBitmap
& bitmap2
,
244 wxObject
*clientData
,
245 const wxString
& shortHelpString
,
246 const wxString
& longHelpString
)
248 return new wxToolBarTool(this, id
, text
, bitmap1
, bitmap2
, kind
,
249 clientData
, shortHelpString
, longHelpString
);
252 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
254 return new wxToolBarTool(this, control
);
257 //-----------------------------------------------------------------------------
258 // wxToolBar construction
259 //-----------------------------------------------------------------------------
261 void wxToolBar::Init()
263 m_toolbar
= (GtkToolbar
*)NULL
;
264 m_blockEvent
= false;
266 m_defaultHeight
= 32;
269 wxToolBar::~wxToolBar()
273 bool wxToolBar::Create( wxWindow
*parent
,
278 const wxString
& name
)
281 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
283 if ( !PreCreation( parent
, pos
, size
) ||
284 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
286 wxFAIL_MSG( wxT("wxToolBar creation failed") );
291 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new() );
294 // Doesn't work this way.
295 // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY;
296 // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL);
298 SetToolSeparation(7);
300 if (style
& wxTB_DOCKABLE
)
302 m_widget
= gtk_handle_box_new();
303 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
304 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
306 if (style
& wxTB_FLAT
)
307 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
311 m_widget
= gtk_event_box_new();
312 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
313 ConnectWidget( m_widget
);
314 gtk_widget_show(GTK_WIDGET(m_toolbar
));
317 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
319 // FIXME: there is no such function for toolbars in 2.0
321 if (style
& wxTB_FLAT
)
322 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
325 m_parent
->DoAddChild( this );
332 void wxToolBar::GtkSetStyle()
334 GtkOrientation orient
;
335 GtkToolbarStyle style
;
336 GetGtkStyle(GetWindowStyle(), &orient
, &style
);
338 gtk_toolbar_set_orientation(m_toolbar
, orient
);
339 gtk_toolbar_set_style(m_toolbar
, style
);
342 void wxToolBar::SetWindowStyleFlag( long style
)
344 wxToolBarBase::SetWindowStyleFlag(style
);
350 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*toolBase
)
352 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
356 if ( tool
->IsButton() )
358 if ( !HasFlag(wxTB_NOICONS
) )
360 wxBitmap bitmap
= tool
->GetNormalBitmap();
362 wxCHECK_MSG( bitmap
.Ok(), false,
363 wxT("invalid bitmap for wxToolBar icon") );
365 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, false,
366 wxT("wxToolBar doesn't support GdkBitmap") );
368 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, false,
369 wxT("wxToolBar::Add needs a wxBitmap") );
371 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
374 if (bitmap
.HasPixbuf())
376 tool_pixmap
= gtk_image_new();
377 tool
->m_pixmap
= tool_pixmap
;
378 tool
->SetPixmap(bitmap
);
382 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
384 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
385 if ( bitmap
.GetMask() )
386 mask
= bitmap
.GetMask()->GetBitmap();
388 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
389 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
392 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
394 tool
->m_pixmap
= tool_pixmap
;
398 switch ( tool
->GetStyle() )
400 case wxTOOL_STYLE_BUTTON
:
401 // for a radio button we need the widget which starts the radio
402 // group it belongs to, i.e. the first radio button immediately
403 // preceding this one
405 GtkWidget
*widget
= NULL
;
407 if ( tool
->IsRadio() )
409 wxToolBarToolsList::compatibility_iterator node
410 = wxToolBarToolsList::compatibility_iterator();
412 node
= m_tools
.Item(pos
- 1);
416 wxToolBarTool
*toolNext
= (wxToolBarTool
*)node
->GetData();
417 if ( !toolNext
->IsRadio() )
420 widget
= toolNext
->m_item
;
422 node
= node
->GetPrevious();
427 // this is the first button in the radio button group,
428 // it will be toggled automatically by GTK so bring the
429 // internal flag in sync
434 tool
->m_item
= gtk_toolbar_insert_element
437 tool
->GetGtkChildType(),
439 tool
->GetLabel().empty()
441 : (const char*) wxGTK_CONV( tool
->GetLabel() ),
442 tool
->GetShortHelp().empty()
444 : (const char*) wxGTK_CONV( tool
->GetShortHelp() ),
445 "", // tooltip_private_text (?)
447 (GtkSignalFunc
)gtk_toolbar_callback
,
454 wxFAIL_MSG( _T("gtk_toolbar_insert_element() failed") );
459 g_signal_connect (tool
->m_item
, "enter_notify_event",
460 G_CALLBACK (gtk_toolbar_tool_callback
),
462 g_signal_connect (tool
->m_item
, "leave_notify_event",
463 G_CALLBACK (gtk_toolbar_tool_callback
),
468 case wxTOOL_STYLE_SEPARATOR
:
469 gtk_toolbar_insert_space( m_toolbar
, posGtk
);
474 case wxTOOL_STYLE_CONTROL
:
475 gtk_toolbar_insert_widget(
477 tool
->GetControl()->m_widget
,
486 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
488 m_width
= req
.width
+ m_xMargin
;
489 m_height
= req
.height
+ 2*m_yMargin
;
490 InvalidateBestSize();
495 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*toolBase
)
497 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
499 switch ( tool
->GetStyle() )
501 case wxTOOL_STYLE_CONTROL
:
502 tool
->GetControl()->Destroy();
505 case wxTOOL_STYLE_BUTTON
:
506 gtk_widget_destroy( tool
->m_item
);
509 case wxTOOL_STYLE_SEPARATOR
:
510 gtk_toolbar_remove_space( m_toolbar
, pos
);
514 InvalidateBestSize();
518 // ----------------------------------------------------------------------------
519 // wxToolBar tools state
520 // ----------------------------------------------------------------------------
522 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
524 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
528 gtk_widget_set_sensitive( tool
->m_item
, enable
);
532 void wxToolBar::DoToggleTool( wxToolBarToolBase
*toolBase
, bool toggle
)
534 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
536 GtkWidget
*item
= tool
->m_item
;
537 if ( item
&& GTK_IS_TOGGLE_BUTTON(item
) )
539 tool
->SetPixmap(tool
->GetBitmap());
543 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(item
), toggle
);
545 m_blockEvent
= false;
549 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
550 bool WXUNUSED(toggle
))
552 // VZ: absolutely no idea about how to do it
553 wxFAIL_MSG( _T("not implemented") );
556 // ----------------------------------------------------------------------------
557 // wxToolBar geometry
558 // ----------------------------------------------------------------------------
560 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
561 wxCoord
WXUNUSED(y
)) const
563 // VZ: GTK+ doesn't seem to have such thing
564 wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") );
566 return (wxToolBarToolBase
*)NULL
;
569 void wxToolBar::SetMargins( int x
, int y
)
571 wxCHECK_RET( GetToolsCount() == 0,
572 wxT("wxToolBar::SetMargins must be called before adding tools.") );
578 void wxToolBar::SetToolSeparation( int separation
)
580 // FIXME: this function disappeared
582 gtk_toolbar_set_space_size( m_toolbar
, separation
);
585 m_toolSeparation
= separation
;
588 void wxToolBar::SetToolShortHelp( int id
, const wxString
& helpString
)
590 wxToolBarTool
*tool
= (wxToolBarTool
*)FindById(id
);
594 (void)tool
->SetShortHelp(helpString
);
595 gtk_tooltips_set_tip(m_toolbar
->tooltips
, tool
->m_item
,
596 wxGTK_CONV( helpString
), "");
600 // ----------------------------------------------------------------------------
601 // wxToolBar idle handling
602 // ----------------------------------------------------------------------------
604 void wxToolBar::OnInternalIdle()
606 wxCursor cursor
= m_cursor
;
607 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
611 /* I now set the cursor the anew in every OnInternalIdle call
612 as setting the cursor in a parent window also effects the
613 windows above so that checking for the current cursor is
616 if (HasFlag(wxTB_DOCKABLE
) && (m_widget
->window
))
618 /* if the toolbar is dockable, then m_widget stands for the
619 GtkHandleBox widget, which uses its own window so that we
620 can set the cursor for it. if the toolbar is not dockable,
621 m_widget comes from m_toolbar which uses its parent's
622 window ("windowless windows") and thus we cannot set the
624 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
627 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
630 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
631 node
= node
->GetNext();
633 GtkWidget
*item
= tool
->m_item
;
636 GdkWindow
*window
= item
->window
;
640 gdk_window_set_cursor( window
, cursor
.GetCursor() );
646 if (wxUpdateUIEvent::CanUpdate(this))
647 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
651 // ----------------------------------------------------------------------------
655 wxToolBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
657 return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new
);
660 #endif // wxUSE_TOOLBAR_NATIVE