1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: GTK toolbar
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "tbargtk.h"
14 #include "wx/toolbar.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
36 extern wxCursor g_globalCursor
;
38 //-----------------------------------------------------------------------------
39 // "clicked" (internal from gtk_toolbar)
40 //-----------------------------------------------------------------------------
42 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
45 wxapp_install_idle_handler();
47 if (tool
->m_owner
->m_blockNextEvent
)
49 tool
->m_owner
->m_blockNextEvent
= FALSE
;
53 if (g_blockEventsOnDrag
) return;
54 if (!tool
->m_enabled
) return;
58 tool
->m_toggleState
= !tool
->m_toggleState
;
60 if (tool
->m_bitmap2
.Ok())
62 wxBitmap bitmap
= tool
->m_bitmap1
;
63 if (tool
->m_toggleState
) bitmap
= tool
->m_bitmap2
;
65 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
67 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
68 if (bitmap
.GetMask()) mask
= bitmap
.GetMask()->GetBitmap();
70 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
74 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
77 //-----------------------------------------------------------------------------
78 // "enter_notify_event"
79 //-----------------------------------------------------------------------------
81 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
82 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
84 if (g_isIdle
) wxapp_install_idle_handler();
86 if (g_blockEventsOnDrag
) return TRUE
;
89 wxToolBar
*tb
= tool
->m_owner
;
91 #if (GTK_MINOR_VERSION == 0)
92 /* we grey-out the tip text of disabled tool in GTK 1.0 */
95 if (tb
->m_fg
->red
!= 0)
100 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
102 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
107 if (tb
->m_fg
->red
== 0)
109 tb
->m_fg
->red
= 33000;
110 tb
->m_fg
->green
= 33000;
111 tb
->m_fg
->blue
= 33000;
112 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
113 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
120 tb
->OnMouseEnter( tool
->m_index
);
125 //-----------------------------------------------------------------------------
126 // InsertChild callback for wxToolBar
127 //-----------------------------------------------------------------------------
129 static void wxInsertChildInToolBar( wxToolBar
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
131 /* we don't do anything here but pray */
134 //-----------------------------------------------------------------------------
136 //-----------------------------------------------------------------------------
138 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
140 BEGIN_EVENT_TABLE(wxToolBar
, wxControl
)
141 EVT_IDLE(wxToolBar::OnIdle
)
144 wxToolBar::wxToolBar()
148 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
149 const wxPoint
& pos
, const wxSize
& size
,
150 long style
, const wxString
& name
)
152 Create( parent
, id
, pos
, size
, style
, name
);
155 wxToolBar::~wxToolBar()
161 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
162 const wxPoint
& pos
, const wxSize
& size
,
163 long style
, const wxString
& name
)
166 m_blockNextEvent
= FALSE
;
167 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInToolBar
;
169 if (!PreCreation( parent
, pos
, size
) ||
170 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
172 wxFAIL_MSG( wxT("wxToolBar creation failed") );
176 m_tools
.DeleteContents( TRUE
);
178 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
179 GTK_TOOLBAR_ICONS
) );
181 // gtk_toolbar_set_space_style( m_toolbar, GTK_TOOLBAR_SPACE_LINE );
183 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
184 m_hasToolAlready
= FALSE
;
186 if (style
& wxTB_DOCKABLE
)
188 m_widget
= gtk_handle_box_new();
189 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
190 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
192 #if (GTK_MINOR_VERSION > 0)
193 if (style
& wxTB_FLAT
)
194 gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget
), GTK_SHADOW_NONE
);
199 m_widget
= GTK_WIDGET(m_toolbar
);
202 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
204 #if (GTK_MINOR_VERSION > 0)
205 if (style
& wxTB_FLAT
)
206 gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar
), GTK_RELIEF_NONE
);
213 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
219 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
221 #if (GTK_MINOR_VERSION > 0)
222 gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar
)->tooltips
);
226 gtk_widget_get_style(
227 GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
) );
229 g_style
->bg
[GTK_STATE_NORMAL
] = *m_bg
;
230 gtk_widget_set_style( GTK_TOOLBAR(m_toolbar
)->tooltips
->tip_window
, g_style
);
232 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
238 m_parent
->DoAddChild( this );
247 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
249 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
250 event
.SetEventObject(this);
251 event
.SetInt( toolIndex
);
252 event
.SetExtraLong((long) toggleDown
);
254 GetEventHandler()->ProcessEvent(event
);
259 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
261 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
262 event
.SetEventObject( this );
263 event
.SetInt( toolIndex
);
265 GetEventHandler()->ProcessEvent(event
);
268 void wxToolBar::OnMouseEnter( int toolIndex
)
270 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
271 event
.SetEventObject(this);
272 event
.SetInt( toolIndex
);
274 GetEventHandler()->ProcessEvent(event
);
277 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
278 const wxBitmap
& pushedBitmap
, bool toggle
,
279 wxCoord
WXUNUSED(xPos
), wxCoord
WXUNUSED(yPos
), wxObject
*clientData
,
280 const wxString
& helpString1
, const wxString
& helpString2
)
282 m_hasToolAlready
= TRUE
;
284 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
285 wxT("invalid bitmap for wxToolBar icon") );
287 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
288 wxT("wxToolBar doesn't support GdkBitmap") );
290 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
291 wxT("wxToolBar::Add needs a wxBitmap") );
293 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
295 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
297 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
298 if ( bitmap
.GetMask() )
299 mask
= bitmap
.GetMask()->GetBitmap();
301 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
302 #if (GTK_MINOR_VERSION > 0)
303 gtk_pixmap_set_build_insensitive( GTK_PIXMAP(tool_pixmap
), TRUE
);
306 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
308 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
310 helpString1
, helpString2
,
313 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
314 : GTK_TOOLBAR_CHILD_BUTTON
;
316 GtkWidget
*item
= gtk_toolbar_append_element
322 helpString1
.mbc_str(),
325 (GtkSignalFunc
)gtk_toolbar_callback
,
332 (* GTK_WIDGET_CLASS( GTK_OBJECT(m_widget
)->klass
)->size_request
) (m_widget
, &req
);
334 m_height
= req
.height
;
336 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
337 "enter_notify_event",
338 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
341 m_tools
.Append( tool
);
346 bool wxToolBar::AddControl(wxControl
*control
)
348 wxCHECK_MSG( control
, FALSE
, wxT("toolbar: can't insert NULL control") );
350 wxCHECK_MSG( control
->GetParent() == this, FALSE
,
351 wxT("control must have toolbar as parent") );
353 m_hasToolAlready
= TRUE
;
355 wxToolBarTool
*tool
= new wxToolBarTool(control
);
357 tool
-> m_item
= NULL
;
358 gtk_toolbar_append_widget( m_toolbar
, control
->m_widget
, (const char *) NULL
, (const char *) NULL
);
361 (* GTK_WIDGET_CLASS( GTK_OBJECT(m_widget
)->klass
)->size_request
) (m_widget
, &req
);
363 m_height
= req
.height
;
365 m_tools
.Append( tool
);
370 void wxToolBar::AddSeparator()
372 gtk_toolbar_append_space( m_toolbar
);
375 bool wxToolBar::DeleteTool(int toolIndex
)
377 wxNode
*node
= m_tools
.First();
380 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
381 if (tool
->m_index
== toolIndex
)
384 tool
->m_control
->Destroy();
386 gtk_widget_destroy( tool
->m_item
);
387 m_tools
.DeleteNode( node
);
397 void wxToolBar::ClearTools()
399 wxFAIL_MSG( wxT("wxToolBar::ClearTools not implemented") );
402 bool wxToolBar::Realize()
409 wxNode
*node
= m_tools
.First();
412 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
413 if (tool
->m_bitmap1
.Ok())
415 int tool_height
= tool
->m_bitmap1
.GetHeight();
416 if (tool_height
> m_height
) m_height
= tool_height
;
422 m_height
+= 5 + 2*m_yMargin
;
427 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
429 wxNode
*node
= m_tools
.First();
432 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
433 if (tool
->m_index
== toolIndex
)
435 tool
->m_enabled
= enable
;
437 #if (GTK_MINOR_VERSION > 0)
438 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
439 greyed anyway and this also disables tooltips */
441 gtk_widget_set_sensitive( tool
->m_item
, enable
);
449 wxFAIL_MSG( wxT("wrong toolbar index") );
452 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
454 wxNode
*node
= m_tools
.First();
457 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
458 if (tool
->m_index
== toolIndex
)
460 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
462 tool
->m_toggleState
= toggle
;
464 if (tool
->m_bitmap2
.Ok())
466 wxBitmap bitmap
= tool
->m_bitmap1
;
467 if (tool
->m_toggleState
) bitmap
= tool
->m_bitmap2
;
469 GtkPixmap
*pixmap
= GTK_PIXMAP( tool
->m_pixmap
);
471 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
472 if (bitmap
.GetMask()) mask
= bitmap
.GetMask()->GetBitmap();
474 gtk_pixmap_set( pixmap
, bitmap
.GetPixmap(), mask
);
477 m_blockNextEvent
= TRUE
; // we cannot use gtk_signal_disconnect here
479 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
487 wxFAIL_MSG( wxT("wrong toolbar index") );
490 wxObject
*wxToolBar::GetToolClientData( int index
) const
492 wxNode
*node
= m_tools
.First();
495 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
496 if (tool
->m_index
== index
) return tool
->m_clientData
;;
500 wxFAIL_MSG( wxT("wrong toolbar index") );
502 return (wxObject
*)NULL
;
505 bool wxToolBar::GetToolState(int toolIndex
) const
507 wxNode
*node
= m_tools
.First();
510 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
511 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
515 wxFAIL_MSG( wxT("wrong toolbar index") );
520 bool wxToolBar::GetToolEnabled(int toolIndex
) const
522 wxNode
*node
= m_tools
.First();
525 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
526 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
530 wxFAIL_MSG( wxT("wrong toolbar index") );
535 void wxToolBar::SetMargins( int x
, int y
)
537 wxCHECK_RET( !m_hasToolAlready
, wxT("wxToolBar::SetMargins must be called before adding tool.") );
539 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
545 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
547 wxFAIL_MSG( wxT("wxToolBar::SetToolPacking not implemented") );
550 void wxToolBar::SetToolSeparation( int separation
)
552 gtk_toolbar_set_space_size( m_toolbar
, separation
);
553 m_separation
= separation
;
556 int wxToolBar::GetToolPacking()
561 int wxToolBar::GetToolSeparation()
566 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
568 wxNode
*node
= m_tools
.First();
571 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
572 if (tool
->m_index
== toolIndex
)
574 return tool
->m_longHelpString
;
579 wxFAIL_MSG( wxT("wrong toolbar index") );
584 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
586 wxNode
*node
= m_tools
.First();
589 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
590 if (tool
->m_index
== toolIndex
)
592 return tool
->m_shortHelpString
;
597 wxFAIL_MSG( wxT("wrong toolbar index") );
602 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
604 wxNode
*node
= m_tools
.First();
607 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
608 if (tool
->m_index
== toolIndex
)
610 tool
->m_longHelpString
= helpString
;
616 wxFAIL_MSG( wxT("wrong toolbar index") );
621 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
623 wxNode
*node
= m_tools
.First();
626 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
627 if (tool
->m_index
== toolIndex
)
629 tool
->m_shortHelpString
= helpString
;
635 wxFAIL_MSG( wxT("wrong toolbar index") );
640 void wxToolBar::OnIdle( wxIdleEvent
&WXUNUSED(ievent
) )
642 wxEvtHandler
* evtHandler
= GetEventHandler();
644 wxNode
* node
= m_tools
.First();
647 wxToolBarTool
* tool
= (wxToolBarTool
*) node
->Data();
649 wxUpdateUIEvent
event( tool
->m_index
);
650 event
.SetEventObject(this);
652 if (evtHandler
->ProcessEvent( event
))
654 if (event
.GetSetEnabled())
655 EnableTool(tool
->m_index
, event
.GetEnabled());
656 if (event
.GetSetChecked())
657 ToggleTool(tool
->m_index
, event
.GetChecked());
659 if (event.GetSetText())
668 void wxToolBar::OnInternalIdle()
670 wxCursor cursor
= m_cursor
;
671 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
675 /* I now set the cursor the anew in every OnInternalIdle call
676 as setting the cursor in a parent window also effects the
677 windows above so that checking for the current cursor is
680 if (HasFlag(wxTB_DOCKABLE
))
682 /* if the toolbar is dockable, then m_widget stands for the
683 GtkHandleBox widget, which uses its own window so that we
684 can set the cursor for it. if the toolbar is not dockable,
685 m_widget comes from m_toolbar which uses its parent's
686 window ("windowless windows") and thus we cannot set the
688 gdk_window_set_cursor( m_widget
->window
, cursor
.GetCursor() );
691 wxNode
*node
= m_tools
.First();
694 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
697 if (!tool
->m_item
|| !tool
->m_item
->window
)
700 gdk_window_set_cursor( tool
->m_item
->window
, cursor
.GetCursor() );