1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: GTK toolbar
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "tbargtk.h"
16 #include "wx/toolbar.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 extern bool g_blockEventsOnDrag
;
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool
,wxObject
)
31 wxToolBarTool::wxToolBarTool( wxToolBar
*owner
, int theIndex
,
32 const wxBitmap
& bitmap1
, const wxBitmap
& bitmap2
,
33 bool toggle
, wxObject
*clientData
,
34 const wxString
& shortHelpString
, const wxString
& longHelpString
,
43 m_toggleState
= FALSE
;
44 m_shortHelpString
= shortHelpString
;
45 m_longHelpString
= longHelpString
;
46 m_isMenuCommand
= TRUE
;
47 m_clientData
= clientData
;
48 m_deleteSecondBitmap
= FALSE
;
52 wxToolBarTool::~wxToolBarTool()
56 //-----------------------------------------------------------------------------
57 // "clicked" (internal from gtk_toolbar)
58 //-----------------------------------------------------------------------------
60 static void gtk_toolbar_callback( GtkWidget
*WXUNUSED(widget
), wxToolBarTool
*tool
)
62 if (g_blockEventsOnDrag
) return;
63 if (!tool
->m_enabled
) return;
65 if (tool
->m_isToggle
) tool
->m_toggleState
= !tool
->m_toggleState
;
67 tool
->m_owner
->OnLeftClick( tool
->m_index
, tool
->m_toggleState
);
70 //-----------------------------------------------------------------------------
71 // "enter_notify_event"
72 //-----------------------------------------------------------------------------
74 static gint
gtk_toolbar_enter_callback( GtkWidget
*WXUNUSED(widget
),
75 GdkEventCrossing
*WXUNUSED(gdk_event
), wxToolBarTool
*tool
)
77 if (g_blockEventsOnDrag
) return TRUE
;
79 tool
->m_owner
->OnMouseEnter( tool
->m_index
);
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
,wxControl
)
90 wxToolBar::wxToolBar()
94 wxToolBar::wxToolBar( wxWindow
*parent
, wxWindowID id
,
95 const wxPoint
& pos
, const wxSize
& size
,
96 long style
, const wxString
& name
)
98 Create( parent
, id
, pos
, size
, style
, name
);
101 wxToolBar::~wxToolBar()
105 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
106 const wxPoint
& pos
, const wxSize
& size
,
107 long style
, const wxString
& name
)
111 PreCreation( parent
, id
, pos
, size
, style
, name
);
113 m_tools
.DeleteContents( TRUE
);
115 m_widget
= gtk_handle_box_new();
117 m_toolbar
= GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL
, GTK_TOOLBAR_ICONS
) );
119 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_toolbar
) );
121 gtk_widget_show( GTK_WIDGET(m_toolbar
) );
130 bool wxToolBar::OnLeftClick( int toolIndex
, bool toggleDown
)
132 wxCommandEvent
event( wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
133 event
.SetEventObject(this);
134 event
.SetInt( toolIndex
);
135 event
.SetExtraLong((long) toggleDown
);
137 GetEventHandler()->ProcessEvent(event
);
142 void wxToolBar::OnRightClick( int toolIndex
, float WXUNUSED(x
), float WXUNUSED(y
) )
144 wxCommandEvent
event( wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
145 event
.SetEventObject( this );
146 event
.SetInt( toolIndex
);
148 GetEventHandler()->ProcessEvent(event
);
151 void wxToolBar::OnMouseEnter( int toolIndex
)
153 wxCommandEvent
event( wxEVT_COMMAND_TOOL_ENTER
, GetId() );
154 event
.SetEventObject(this);
155 event
.SetInt( toolIndex
);
157 GetEventHandler()->ProcessEvent(event
);
160 wxToolBarTool
*wxToolBar::AddTool( int toolIndex
, const wxBitmap
& bitmap
,
161 const wxBitmap
& pushedBitmap
, bool toggle
,
162 float WXUNUSED(xPos
), float WXUNUSED(yPos
), wxObject
*clientData
,
163 const wxString
& helpString1
, const wxString
& helpString2
)
165 if (!bitmap
.Ok()) return (wxToolBarTool
*) NULL
;
167 wxToolBarTool
*tool
= new wxToolBarTool( this, toolIndex
, bitmap
, pushedBitmap
, toggle
,
168 clientData
, helpString1
, helpString2
);
170 GtkWidget
*tool_pixmap
= (GtkWidget
*) NULL
;
172 wxCHECK_MSG( bitmap
.GetBitmap() == NULL
, (wxToolBarTool
*)NULL
, "wxToolBar doesn't support GdkBitmap" )
174 wxCHECK_MSG( bitmap
.GetPixmap() != NULL
, (wxToolBarTool
*)NULL
, "wxToolBar::Add needs a wxBitmap" )
178 GdkPixmap
*pixmap
= bitmap
.GetPixmap();
180 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
181 if (bitmap
.GetMask()) mask
= bitmap
.GetMask()->GetBitmap();
183 tool_pixmap
= gtk_pixmap_new( pixmap
, mask
);
186 gtk_misc_set_alignment( GTK_MISC(tool_pixmap
), 0.5, 0.5 );
188 GtkToolbarChildType ctype
= GTK_TOOLBAR_CHILD_BUTTON
;
189 if (toggle
) ctype
= GTK_TOOLBAR_CHILD_TOGGLEBUTTON
;
191 tool
->m_item
= gtk_toolbar_append_element( m_toolbar
, ctype
, (GtkWidget
*) NULL
, (const char *) NULL
, helpString1
, "", tool_pixmap
,
192 (GtkSignalFunc
)gtk_toolbar_callback
, (gpointer
)tool
);
194 gtk_signal_connect( GTK_OBJECT(tool
->m_item
), "enter_notify_event",
195 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback
), (gpointer
)tool
);
197 m_tools
.Append( tool
);
202 void wxToolBar::AddSeparator(void)
204 gtk_toolbar_append_space( m_toolbar
);
207 void wxToolBar::ClearTools(void)
209 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
212 void wxToolBar::Realize(void)
219 wxNode
*node
= m_tools
.First();
222 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
223 if (tool
->m_bitmap1
.Ok())
225 int tool_height
= tool
->m_bitmap1
.GetHeight();
226 if (tool_height
> m_height
) m_height
= tool_height
;
235 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
237 wxNode
*node
= m_tools
.First();
240 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
241 if (tool
->m_index
== toolIndex
)
243 tool
->m_enabled
= enable
;
249 wxFAIL_MSG( "wrong toolbar index" );
252 void wxToolBar::ToggleTool( int toolIndex
, bool toggle
)
254 wxNode
*node
= m_tools
.First();
257 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
258 if (tool
->m_index
== toolIndex
)
260 tool
->m_toggleState
= toggle
;
261 if ((tool
->m_item
) && (GTK_IS_TOGGLE_BUTTON(tool
->m_item
)))
262 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool
->m_item
), toggle
);
268 wxFAIL_MSG( "wrong toolbar index" );
271 wxObject
*wxToolBar::GetToolClientData( int index
) const
273 wxNode
*node
= m_tools
.First();
276 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
277 if (tool
->m_index
== index
) return tool
->m_clientData
;;
281 wxFAIL_MSG( "wrong toolbar index" );
283 return (wxObject
*)NULL
;
286 bool wxToolBar::GetToolState(int toolIndex
) const
288 wxNode
*node
= m_tools
.First();
291 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
292 if (tool
->m_index
== toolIndex
) return tool
->m_toggleState
;
296 wxFAIL_MSG( "wrong toolbar index" );
301 bool wxToolBar::GetToolEnabled(int toolIndex
) const
303 wxNode
*node
= m_tools
.First();
306 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
307 if (tool
->m_index
== toolIndex
) return tool
->m_enabled
;
311 wxFAIL_MSG( "wrong toolbar index" );
316 void wxToolBar::SetMargins( int WXUNUSED(x
), int WXUNUSED(y
) )
320 void wxToolBar::SetToolPacking( int WXUNUSED(packing
) )
324 void wxToolBar::SetToolSeparation( int separation
)
326 gtk_toolbar_set_space_size( m_toolbar
, separation
);