]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/tbargtk.cpp
* wxCreateDynamicObject() uses an hashtable now
[wxWidgets.git] / src / gtk1 / tbargtk.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: tbargtk.cpp
3// Purpose: GTK toolbar
4// Author: Robert Roebling
5// Modified by:
6// Created: 01/02/97
a3622daa 7// RCS-ID:
c801d85f 8// Copyright: (c) Robert Roebling
a3622daa 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "tbargtk.h"
14#endif
15
16#include "wx/toolbar.h"
1a5a8367 17#include <wx/intl.h>
c801d85f 18
314055fa
RR
19//-----------------------------------------------------------------------------
20// data
21//-----------------------------------------------------------------------------
22
23extern bool g_blockEventsOnDrag;
24
c801d85f
KB
25//-----------------------------------------------------------------------------
26// wxToolBarTool
27//-----------------------------------------------------------------------------
28
29IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool,wxObject)
a3622daa
VZ
30
31wxToolBarTool::wxToolBarTool( wxToolBar *owner, int theIndex,
debe6624
JS
32 const wxBitmap& bitmap1, const wxBitmap& bitmap2,
33 bool toggle, wxObject *clientData,
fc008f25
RR
34 const wxString& shortHelpString, const wxString& longHelpString,
35 GtkWidget *item )
c801d85f
KB
36{
37 m_owner = owner;
38 m_index = theIndex;
39 m_bitmap1 = bitmap1;
40 m_bitmap2 = bitmap2;
41 m_isToggle = toggle;
42 m_enabled = TRUE;
43 m_toggleState = FALSE;
44 m_shortHelpString = shortHelpString;
45 m_longHelpString = longHelpString;
46 m_isMenuCommand = TRUE;
47 m_clientData = clientData;
48 m_deleteSecondBitmap = FALSE;
fc008f25
RR
49 m_item = item;
50}
c801d85f 51
a3622daa 52wxToolBarTool::~wxToolBarTool()
c801d85f 53{
fc008f25 54}
c801d85f
KB
55
56//-----------------------------------------------------------------------------
716b7364 57// wxToolBar
c801d85f
KB
58//-----------------------------------------------------------------------------
59
60static void gtk_toolbar_callback( GtkWidget *WXUNUSED(widget), wxToolBarTool *tool )
61{
314055fa 62 if (g_blockEventsOnDrag) return;
c801d85f 63 if (!tool->m_enabled) return;
a3622daa 64
c801d85f 65 if (tool->m_isToggle) tool->m_toggleState = !tool->m_toggleState;
a3622daa 66
c801d85f 67 tool->m_owner->OnLeftClick( tool->m_index, tool->m_toggleState );
fc008f25 68}
c801d85f 69
314055fa
RR
70static gint gtk_toolbar_enter_callback( GtkWidget *WXUNUSED(widget),
71 GdkEventCrossing *WXUNUSED(gdk_event), wxToolBarTool *tool )
72{
73 if (g_blockEventsOnDrag) return TRUE;
74
75 tool->m_owner->OnMouseEnter( tool->m_index );
76
77 return TRUE;
78}
79
c801d85f
KB
80//-----------------------------------------------------------------------------
81
716b7364 82IMPLEMENT_DYNAMIC_CLASS(wxToolBar,wxControl)
c801d85f 83
716b7364 84BEGIN_EVENT_TABLE(wxToolBar, wxControl)
c801d85f
KB
85END_EVENT_TABLE()
86
a3622daa 87wxToolBar::wxToolBar()
c801d85f 88{
fc008f25 89}
c801d85f 90
a3622daa 91wxToolBar::wxToolBar( wxWindow *parent, wxWindowID id,
c801d85f 92 const wxPoint& pos, const wxSize& size,
debe6624 93 long style, const wxString& name )
c801d85f
KB
94{
95 Create( parent, id, pos, size, style, name );
fc008f25 96}
c801d85f 97
a3622daa 98wxToolBar::~wxToolBar()
c801d85f 99{
fc008f25 100}
c801d85f 101
a3622daa 102bool wxToolBar::Create( wxWindow *parent, wxWindowID id,
c801d85f 103 const wxPoint& pos, const wxSize& size,
debe6624 104 long style, const wxString& name )
c801d85f
KB
105{
106 m_needParent = TRUE;
a3622daa 107
c801d85f
KB
108 PreCreation( parent, id, pos, size, style, name );
109
110 m_tools.DeleteContents( TRUE );
a3622daa 111
c801d85f 112 m_widget = gtk_handle_box_new();
a3622daa 113
c801d85f 114 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL, GTK_TOOLBAR_ICONS ) );
a3622daa 115
c801d85f 116 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
a3622daa 117
c801d85f 118 gtk_widget_show( GTK_WIDGET(m_toolbar) );
a3622daa 119
c801d85f 120 PostCreation();
a3622daa 121
c801d85f 122 Show( TRUE );
a3622daa 123
c801d85f 124 return TRUE;
fc008f25 125}
c801d85f 126
716b7364 127bool wxToolBar::OnLeftClick( int toolIndex, bool toggleDown )
c801d85f 128{
cf4219e7 129 wxCommandEvent event( wxEVT_COMMAND_TOOL_CLICKED, toolIndex );
c801d85f 130 event.SetEventObject(this);
46dc76ba 131 event.SetInt( toolIndex );
c801d85f
KB
132 event.SetExtraLong((long) toggleDown);
133
134 GetEventHandler()->ProcessEvent(event);
135
136 return TRUE;
fc008f25 137}
c801d85f 138
716b7364 139void wxToolBar::OnRightClick( int toolIndex, float WXUNUSED(x), float WXUNUSED(y) )
c801d85f 140{
cf4219e7 141 wxCommandEvent event( wxEVT_COMMAND_TOOL_RCLICKED, toolIndex );
46dc76ba
RR
142 event.SetEventObject( this );
143 event.SetInt( toolIndex );
c801d85f
KB
144
145 GetEventHandler()->ProcessEvent(event);
fc008f25 146}
c801d85f 147
716b7364 148void wxToolBar::OnMouseEnter( int toolIndex )
c801d85f 149{
314055fa 150 wxCommandEvent event( wxEVT_COMMAND_TOOL_ENTER, GetId() );
c801d85f 151 event.SetEventObject(this);
46dc76ba 152 event.SetInt( toolIndex );
314055fa 153
c801d85f 154 GetEventHandler()->ProcessEvent(event);
fc008f25 155}
c801d85f 156
a3622daa 157wxToolBarTool *wxToolBar::AddTool( int toolIndex, const wxBitmap& bitmap,
debe6624
JS
158 const wxBitmap& pushedBitmap, bool toggle,
159 float WXUNUSED(xPos), float WXUNUSED(yPos), wxObject *clientData,
c801d85f
KB
160 const wxString& helpString1, const wxString& helpString2 )
161{
c67daf87 162 if (!bitmap.Ok()) return (wxToolBarTool *) NULL;
a3622daa
VZ
163
164 wxToolBarTool *tool = new wxToolBarTool( this, toolIndex, bitmap, pushedBitmap, toggle,
fc008f25 165 clientData, helpString1, helpString2 );
a3622daa 166
c67daf87 167 GtkWidget *tool_pixmap = (GtkWidget *) NULL;
903f689b
RR
168
169 wxCHECK_MSG( bitmap.GetBitmap() == NULL, (wxToolBarTool *)NULL, "wxToolBar doesn't support GdkBitmap" )
170
171 wxCHECK_MSG( bitmap.GetPixmap() != NULL, (wxToolBarTool *)NULL, "wxToolBar::Add needs a wxBitmap" )
172
173 if (TRUE)
174 {
175 GdkPixmap *pixmap = bitmap.GetPixmap();
a3622daa 176
c67daf87 177 GdkBitmap *mask = (GdkBitmap *) NULL;
903f689b
RR
178 if (bitmap.GetMask()) mask = bitmap.GetMask()->GetBitmap();
179
180 tool_pixmap = gtk_pixmap_new( pixmap, mask );
181 }
182
c801d85f 183 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
a3622daa 184
c801d85f
KB
185 GtkToolbarChildType ctype = GTK_TOOLBAR_CHILD_BUTTON;
186 if (toggle) ctype = GTK_TOOLBAR_CHILD_TOGGLEBUTTON;
a3622daa 187
c67daf87 188 tool->m_item = gtk_toolbar_append_element( m_toolbar, ctype, (GtkWidget *) NULL, (const char *) NULL, helpString1, "", tool_pixmap,
fc008f25 189 (GtkSignalFunc)gtk_toolbar_callback, (gpointer)tool );
a3622daa 190
314055fa
RR
191 gtk_signal_connect( GTK_OBJECT(tool->m_item), "enter_notify_event",
192 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback), (gpointer)tool );
193
a3622daa
VZ
194 m_tools.Append( tool );
195
c801d85f 196 return tool;
fc008f25 197}
c801d85f 198
716b7364 199void wxToolBar::AddSeparator(void)
c801d85f
KB
200{
201 gtk_toolbar_append_space( m_toolbar );
fc008f25 202}
c801d85f 203
716b7364 204void wxToolBar::ClearTools(void)
c801d85f 205{
fc008f25
RR
206 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
207}
c801d85f 208
e3e65dac 209void wxToolBar::Realize(void)
46dc76ba
RR
210{
211 m_x = 0;
212 m_y = 0;
213 m_width = 100;
214 m_height = 0;
215
216 wxNode *node = m_tools.First();
217 while (node)
218 {
219 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
220 if (tool->m_bitmap1.Ok())
221 {
222 int tool_height = tool->m_bitmap1.GetHeight();
223 if (tool_height > m_height) m_height = tool_height;
fc008f25 224 }
46dc76ba
RR
225
226 node = node->Next();
fc008f25 227 }
46dc76ba
RR
228
229 m_height += 10;
fc008f25 230}
46dc76ba 231
716b7364 232void wxToolBar::EnableTool(int toolIndex, bool enable)
c801d85f 233{
cf4219e7
RR
234 wxNode *node = m_tools.First();
235 while (node)
236 {
237 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
238 if (tool->m_index == toolIndex)
239 {
240 tool->m_enabled = enable;
241 return;
242 }
243 node = node->Next();
fc008f25
RR
244 }
245
246 wxFAIL_MSG( "wrong toolbar index" );
247}
c801d85f 248
fc008f25 249void wxToolBar::ToggleTool( int toolIndex, bool toggle )
c801d85f 250{
fc008f25
RR
251 wxNode *node = m_tools.First();
252 while (node)
253 {
254 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
255 if (tool->m_index == toolIndex)
256 {
257 tool->m_toggleState = toggle;
258 if ((tool->m_item) && (GTK_IS_TOGGLE_BUTTON(tool->m_item)))
259 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool->m_item), toggle );
260 return;
261 }
262 node = node->Next();
263 }
264
265 wxFAIL_MSG( "wrong toolbar index" );
266}
c801d85f 267
fc008f25 268wxObject *wxToolBar::GetToolClientData( int index ) const
c801d85f 269{
cf4219e7
RR
270 wxNode *node = m_tools.First();
271 while (node)
272 {
273 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
274 if (tool->m_index == index) return tool->m_clientData;;
275 node = node->Next();
fc008f25
RR
276 }
277
278 wxFAIL_MSG( "wrong toolbar index" );
279
cf4219e7 280 return (wxObject*)NULL;
fc008f25 281}
c801d85f 282
716b7364 283bool wxToolBar::GetToolState(int toolIndex) const
c801d85f 284{
cf4219e7
RR
285 wxNode *node = m_tools.First();
286 while (node)
287 {
288 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
289 if (tool->m_index == toolIndex) return tool->m_toggleState;
290 node = node->Next();
fc008f25
RR
291 }
292
293 wxFAIL_MSG( "wrong toolbar index" );
294
cf4219e7 295 return FALSE;
fc008f25 296}
c801d85f 297
716b7364 298bool wxToolBar::GetToolEnabled(int toolIndex) const
c801d85f 299{
cf4219e7
RR
300 wxNode *node = m_tools.First();
301 while (node)
302 {
303 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
304 if (tool->m_index == toolIndex) return tool->m_enabled;
305 node = node->Next();
fc008f25
RR
306 }
307
308 wxFAIL_MSG( "wrong toolbar index" );
309
cf4219e7 310 return FALSE;
fc008f25 311}
c801d85f 312
cf4219e7 313void wxToolBar::SetMargins( int WXUNUSED(x), int WXUNUSED(y) )
c801d85f 314{
fc008f25 315}
c801d85f 316
cf4219e7 317void wxToolBar::SetToolPacking( int WXUNUSED(packing) )
c801d85f 318{
fc008f25 319}
c801d85f 320
cf4219e7 321void wxToolBar::SetToolSeparation( int separation )
c801d85f 322{
cf4219e7 323 gtk_toolbar_set_space_size( m_toolbar, separation );
fc008f25 324}
c801d85f 325