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