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 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool
,wxObject
)
32 wxToolBarTool::wxToolBarTool( wxToolBar
*owner
, int theIndex
,
33 const wxBitmap
& bitmap1
, const wxBitmap
& bitmap2
,
36 const wxString
& shortHelpString
,
37 const wxString
& longHelpString
,
46 m_toggleState
= FALSE
;
47 m_shortHelpString
= shortHelpString
;
48 m_longHelpString
= longHelpString
;
49 m_isMenuCommand
= TRUE
;
50 m_clientData
= clientData
;
51 m_deleteSecondBitmap
= FALSE
;
55 wxToolBarTool::~wxToolBarTool()
59 //-----------------------------------------------------------------------------
60 // "clicked" (internal from gtk_toolbar)
61 //-----------------------------------------------------------------------------
63 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
65 if (g_blockEventsOnDrag
) return;
66 if (!tool
->m_enabled
) return;
68 if (tool
->m_isToggle
) tool
->m_toggleState
= !tool
->m_toggleState
;
70 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
73 //-----------------------------------------------------------------------------
74 // "enter_notify_event"
75 //-----------------------------------------------------------------------------
77 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
78 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
80 if (g_blockEventsOnDrag
) return TRUE
;
82 tool
->m_owner
->OnMouseEnter( tool
->m_index
);
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
91 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
93 wxToolBar::wxToolBar()
97 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
98 const wxPoint
& pos
, const wxSize
& size
,
99 long style
, const wxString
& name
)
101 Create( parent
, id
, pos
, size
, style
, name
);
104 wxToolBar::~wxToolBar()
110 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
111 const wxPoint
& pos
, const wxSize
& size
,
112 long style
, const wxString
& name
)
116 PreCreation( parent
, id
, pos
, size
, style
, name
);
118 m_tools
.DeleteContents( TRUE
);
120 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
,
121 GTK_TOOLBAR_ICONS
) );
124 gtk_toolbar_set_space_size( m_toolbar
, m_separation
);
125 m_hasToolAlready
= FALSE
;
127 if (style
& wxTB_DOCKABLE
)
129 m_widget
= gtk_handle_box_new();
130 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
131 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
135 m_widget
= GTK_WIDGET(m_toolbar
);
138 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar
), TRUE
);
144 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_fg
);
150 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar
) ), m_bg
);
152 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar
)->tooltips
, m_bg
, m_fg
);
157 m_parent
->AddChild( this );
159 (m_parent
->m_insertCallback
)( m_parent
, this );
168 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
170 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
171 event
.SetEventObject(this);
172 event
.SetInt( toolIndex
);
173 event
.SetExtraLong((long) toggleDown
);
175 GetEventHandler()->ProcessEvent(event
);
180 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
182 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
183 event
.SetEventObject( this );
184 event
.SetInt( toolIndex
);
186 GetEventHandler()->ProcessEvent(event
);
189 void wxToolBar::OnMouseEnter( int toolIndex
)
191 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
192 event
.SetEventObject(this);
193 event
.SetInt( toolIndex
);
195 GetEventHandler()->ProcessEvent(event
);
198 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
199 const wxBitmap
& pushedBitmap
, bool toggle
,
200 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
201 const wxString
& helpString1
, const wxString
& helpString2
)
203 m_hasToolAlready
= TRUE
;
205 wxCHECK_MSG( bitmap
.Ok(), (wxToolBarTool
*)NULL
,
206 "invalid bitmap for wxToolBar icon" );
208 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
,
209 "wxToolBar doesn't support GdkBitmap" );
211 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
,
212 "wxToolBar::Add needs a wxBitmap" );
214 GtkWidget
*tool_pixmap
= (GtkWidget
*)NULL
;
216 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
218 GdkBitmap
*mask
= (GdkBitmap
*)NULL
;
219 if ( bitmap
.GetMask() )
220 mask
= bitmap
.GetMask()->GetBitmap();
222 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
224 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
226 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
,
228 helpString1
, helpString2
,
231 GtkToolbarChildType ctype
= toggle
? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
232 : GTK_TOOLBAR_CHILD_BUTTON
;
234 GtkWidget
*item
= gtk_toolbar_append_element
236 GTK_TOOLBAR(m_toolbar
),
243 (GtkSignalFunc
)gtk_toolbar_callback
,
249 gtk_signal_connect( GTK_OBJECT(tool
->m_item
),
250 "enter_notify_event",
251 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
),
254 m_tools
.Append( tool
);
259 void wxToolBar::AddSeparator()
261 gtk_toolbar_append_space( m_toolbar
);
264 void wxToolBar::ClearTools()
266 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
269 bool wxToolBar::Realize()
276 wxNode
*node
= m_tools
.First();
279 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
280 if (tool
->m_bitmap1
.Ok())
282 int tool_height
= tool
->m_bitmap1
.GetHeight();
283 if (tool_height
> m_height
) m_height
= tool_height
;
289 m_height
+= 5 + 2*m_yMargin
;
294 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
296 wxNode
*node
= m_tools
.First();
299 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
300 if (tool
->m_index
== toolIndex
)
302 tool
->m_enabled
= enable
;
305 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" );