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"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 extern bool g_blockEventsOnDrag
;
26 //-----------------------------------------------------------------------------
27 // "clicked" (internal from gtk_toolbar)
28 //-----------------------------------------------------------------------------
30 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
32 if (g_blockEventsOnDrag
) return;
33 if (!tool
->m_enabled
) return;
35 if (tool
->m_isToggle
) tool
->m_toggleState
= !tool
->m_toggleState
;
37 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
40 //-----------------------------------------------------------------------------
41 // "enter_notify_event"
42 //-----------------------------------------------------------------------------
44 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
45 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
47 if (g_blockEventsOnDrag
) return TRUE
;
49 /* we grey-out the tip text of disabled tool */
51 wxToolBar
*tb
= tool
->m_owner
;
55 if (tb
->m_fg
->red
!= 0)
60 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
61 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
66 if (tb
->m_fg
->red
== 0)
68 tb
->m_fg
->red
= 33000;
69 tb
->m_fg
->green
= 33000;
70 tb
->m_fg
->blue
= 33000;
71 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(tb
->m_toolbar
) ), tb
->m_fg
);
72 gtk_tooltips_set_colors( GTK_TOOLBAR(tb
->m_toolbar
)->tooltips
, tb
->m_bg
, tb
->m_fg
);
78 tb
->OnMouseEnter( tool
->m_index
);
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
87 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
89 wxToolBar::wxToolBar()
93 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
94 const wxPoint
& pos
, const wxSize
& size
,
95 long style
, const wxString
& name
)
97 Create( parent
, id
, pos
, size
, style
, name
);
100 wxToolBar::~wxToolBar()
106 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
107 const wxPoint
& pos
, const wxSize
& size
,
108 long style
, const wxString
& name
)
112 PreCreation( parent
, id
, pos
, size
, style
, name
);
114 m_tools
.DeleteContents( TRUE
);
116 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
117 GTK_TOOLBAR_ICONS
) );
120 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
121 m_hasToolAlready
= FALSE
;
123 if (style
& wxTB_DOCKABLE
)
125 m_widget
= gtk_handle_box_new();
126 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
127 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
131 m_widget
= GTK_WIDGET(m_toolbar
);
134 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
140 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
146 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
148 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
153 m_parent
->AddChild( this );
155 (m_parent
->m_insertCallback
)( m_parent
, this );
164 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
166 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
167 event
.SetEventObject(this);
168 event
.SetInt( toolIndex
);
169 event
.SetExtraLong((long) toggleDown
);
171 GetEventHandler()->ProcessEvent(event
);
176 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
178 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
179 event
.SetEventObject( this );
180 event
.SetInt( toolIndex
);
182 GetEventHandler()->ProcessEvent(event
);
185 void wxToolBar::OnMouseEnter( int toolIndex
)
187 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
188 event
.SetEventObject(this);
189 event
.SetInt( toolIndex
);
191 GetEventHandler()->ProcessEvent(event
);
194 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
195 const wxBitmap
& pushedBitmap
, bool toggle
,
196 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
197 const wxString
& helpString1
, const wxString
& helpString2
)
199 m_hasToolAlready
= TRUE
;
201 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
202 "invalid bitmap for wxToolBar icon" );
204 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
205 "wxToolBar doesn't support GdkBitmap" );
207 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
208 "wxToolBar::Add needs a wxBitmap" );
210 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
212 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
214 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
215 if ( bitmap
.GetMask() )
216 mask
= bitmap
.GetMask()->GetBitmap();
218 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
220 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
222 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
224 helpString1
, helpString2
,
227 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
228 : GTK_TOOLBAR_CHILD_BUTTON
;
230 GtkWidget
*item
= gtk_toolbar_append_element
232 GTK_TOOLBAR(m_toolbar
),
239 (GtkSignalFunc
)gtk_toolbar_callback
,
245 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
246 "enter_notify_event",
247 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
250 m_tools
.Append( tool
);
255 void wxToolBar::AddSeparator()
257 gtk_toolbar_append_space( m_toolbar
);
260 void wxToolBar::ClearTools()
262 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
265 bool wxToolBar::Realize()
272 wxNode
*node
= m_tools
.First();
275 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
276 if (tool
->m_bitmap1
.Ok())
278 int tool_height
= tool
->m_bitmap1
.GetHeight();
279 if (tool_height
> m_height
) m_height
= tool_height
;
285 m_height
+= 5 + 2*m_yMargin
;
290 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
292 wxNode
*node
= m_tools
.First();
295 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
296 if (tool
->m_index
== toolIndex
)
298 tool
->m_enabled
= enable
;
300 /* we don't disable the tools for now as the bitmaps don't get
301 greyed anyway and this also disables tooltips
304 gtk_widget_set_sensitive( tool->m_item, enable );
312 wxFAIL_MSG( "wrong toolbar index" );
315 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
317 wxNode
*node
= m_tools
.First();
320 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
321 if (tool
->m_index
== toolIndex
)
323 tool
->m_toggleState
= toggle
;
324 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
325 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
331 wxFAIL_MSG( "wrong toolbar index" );
334 wxObject
*wxToolBar::GetToolClientData( int index
) const
336 wxNode
*node
= m_tools
.First();
339 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
340 if (tool
->m_index
== index
) return tool
->m_clientData
;;
344 wxFAIL_MSG( "wrong toolbar index" );
346 return (wxObject
*)NULL
;
349 bool wxToolBar::GetToolState(int toolIndex
) const
351 wxNode
*node
= m_tools
.First();
354 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
355 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
359 wxFAIL_MSG( "wrong toolbar index" );
364 bool wxToolBar::GetToolEnabled(int toolIndex
) const
366 wxNode
*node
= m_tools
.First();
369 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
370 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
374 wxFAIL_MSG( "wrong toolbar index" );
379 void wxToolBar::SetMargins( int x
, int y
)
381 wxCHECK_RET( !m_hasToolAlready
, "wxToolBar::SetMargins must be called before adding tool." );
383 if (x
> 2) gtk_toolbar_append_space( m_toolbar
); // oh well
389 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
391 wxFAIL_MSG( "wxToolBar::SetToolPacking not implemented" );
394 void wxToolBar::SetToolSeparation( int separation
)
396 gtk_toolbar_set_space_size( m_toolbar
, separation
);
397 m_separation
= separation
;
400 int wxToolBar::GetToolPacking()
405 int wxToolBar::GetToolSeparation()
410 wxString
wxToolBar::GetToolLongHelp(int toolIndex
)
412 wxNode
*node
= m_tools
.First();
415 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
416 if (tool
->m_index
== toolIndex
)
418 return tool
->m_longHelpString
;
423 wxFAIL_MSG( "wrong toolbar index" );
428 wxString
wxToolBar::GetToolShortHelp(int toolIndex
)
430 wxNode
*node
= m_tools
.First();
433 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
434 if (tool
->m_index
== toolIndex
)
436 return tool
->m_shortHelpString
;
441 wxFAIL_MSG( "wrong toolbar index" );
446 void wxToolBar::SetToolLongHelp(int toolIndex
, const wxString
& helpString
)
448 wxNode
*node
= m_tools
.First();
451 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
452 if (tool
->m_index
== toolIndex
)
454 tool
->m_longHelpString
= helpString
;
460 wxFAIL_MSG( "wrong toolbar index" );
465 void wxToolBar::SetToolShortHelp(int toolIndex
, const wxString
& helpString
)
467 wxNode
*node
= m_tools
.First();
470 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
471 if (tool
->m_index
== toolIndex
)
473 tool
->m_shortHelpString
= helpString
;
479 wxFAIL_MSG( "wrong toolbar index" );