]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/tbargtk.cpp
wxTextFile::Type changed to ::wxTextFileType (portability)
[wxWidgets.git] / src / gtk1 / tbargtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tbargtk.cpp
3 // Purpose: GTK toolbar
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "tbargtk.h"
12 #endif
13
14 #include "wx/toolbar.h"
15
16 //-----------------------------------------------------------------------------
17 // data
18 //-----------------------------------------------------------------------------
19
20 extern bool g_blockEventsOnDrag;
21
22 //-----------------------------------------------------------------------------
23 // wxToolBarTool
24 //-----------------------------------------------------------------------------
25
26 IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool,wxObject)
27
28 wxToolBarTool::wxToolBarTool( wxToolBar *owner, int theIndex,
29 const wxBitmap& bitmap1, const wxBitmap& bitmap2,
30 bool toggle,
31 wxObject *clientData,
32 const wxString& shortHelpString,
33 const wxString& longHelpString,
34 GtkWidget *item )
35 {
36 m_owner = owner;
37 m_index = theIndex;
38 m_bitmap1 = bitmap1;
39 m_bitmap2 = bitmap2;
40 m_isToggle = toggle;
41 m_enabled = TRUE;
42 m_toggleState = FALSE;
43 m_shortHelpString = shortHelpString;
44 m_longHelpString = longHelpString;
45 m_isMenuCommand = TRUE;
46 m_clientData = clientData;
47 m_deleteSecondBitmap = FALSE;
48 m_item = item;
49 }
50
51 wxToolBarTool::~wxToolBarTool()
52 {
53 }
54
55 //-----------------------------------------------------------------------------
56 // "clicked" (internal from gtk_toolbar)
57 //-----------------------------------------------------------------------------
58
59 static void gtk_toolbar_callback( GtkWidget *WXUNUSED(widget), wxToolBarTool *tool )
60 {
61 if (g_blockEventsOnDrag) return;
62 if (!tool->m_enabled) return;
63
64 if (tool->m_isToggle) tool->m_toggleState = !tool->m_toggleState;
65
66 tool->m_owner->OnLeftClick( tool->m_index, tool->m_toggleState );
67 }
68
69 //-----------------------------------------------------------------------------
70 // "enter_notify_event"
71 //-----------------------------------------------------------------------------
72
73 static gint gtk_toolbar_enter_callback( GtkWidget *WXUNUSED(widget),
74 GdkEventCrossing *WXUNUSED(gdk_event), wxToolBarTool *tool )
75 {
76 if (g_blockEventsOnDrag) return TRUE;
77
78 tool->m_owner->OnMouseEnter( tool->m_index );
79
80 return FALSE;
81 }
82
83 //-----------------------------------------------------------------------------
84 // wxToolBar
85 //-----------------------------------------------------------------------------
86
87 IMPLEMENT_DYNAMIC_CLASS(wxToolBar,wxControl)
88
89 wxToolBar::wxToolBar()
90 {
91 }
92
93 wxToolBar::wxToolBar( wxWindow *parent, wxWindowID id,
94 const wxPoint& pos, const wxSize& size,
95 long style, const wxString& name )
96 {
97 Create( parent, id, pos, size, style, name );
98 }
99
100 wxToolBar::~wxToolBar()
101 {
102 }
103
104 bool wxToolBar::Create( wxWindow *parent, wxWindowID id,
105 const wxPoint& pos, const wxSize& size,
106 long style, const wxString& name )
107 {
108 m_needParent = TRUE;
109
110 PreCreation( parent, id, pos, size, style, name );
111
112 m_tools.DeleteContents( TRUE );
113
114 m_toolbar = GTK_TOOLBAR( gtk_toolbar_new( GTK_ORIENTATION_HORIZONTAL,
115 GTK_TOOLBAR_ICONS ) );
116
117 m_separation = 5;
118 gtk_toolbar_set_space_size( m_toolbar, m_separation );
119 m_hasToolAlready = FALSE;
120
121 m_widget = GTK_WIDGET(m_toolbar);
122
123 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
124
125 m_fg.red = 0;
126 m_fg.green = 0;
127 m_fg.blue = 0;
128 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), &m_fg );
129
130 m_bg.red = 65535;
131 m_bg.green = 65535;
132 m_bg.blue = 50000;
133 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), &m_bg );
134
135 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar)->tooltips, &m_bg, &m_fg );
136
137 m_xMargin = 0;
138 m_yMargin = 0;
139
140 m_parent->AddChild( this );
141
142 (m_parent->m_insertCallback)( m_parent, this );
143
144 PostCreation();
145
146 Show( TRUE );
147
148 return TRUE;
149 }
150
151 bool wxToolBar::OnLeftClick( int toolIndex, bool toggleDown )
152 {
153 wxCommandEvent event( wxEVT_COMMAND_TOOL_CLICKED, toolIndex );
154 event.SetEventObject(this);
155 event.SetInt( toolIndex );
156 event.SetExtraLong((long) toggleDown);
157
158 GetEventHandler()->ProcessEvent(event);
159
160 return TRUE;
161 }
162
163 void wxToolBar::OnRightClick( int toolIndex, float WXUNUSED(x), float WXUNUSED(y) )
164 {
165 wxCommandEvent event( wxEVT_COMMAND_TOOL_RCLICKED, toolIndex );
166 event.SetEventObject( this );
167 event.SetInt( toolIndex );
168
169 GetEventHandler()->ProcessEvent(event);
170 }
171
172 void wxToolBar::OnMouseEnter( int toolIndex )
173 {
174 wxCommandEvent event( wxEVT_COMMAND_TOOL_ENTER, GetId() );
175 event.SetEventObject(this);
176 event.SetInt( toolIndex );
177
178 GetEventHandler()->ProcessEvent(event);
179 }
180
181 wxToolBarTool *wxToolBar::AddTool( int toolIndex, const wxBitmap& bitmap,
182 const wxBitmap& pushedBitmap, bool toggle,
183 float WXUNUSED(xPos), float WXUNUSED(yPos), wxObject *clientData,
184 const wxString& helpString1, const wxString& helpString2 )
185 {
186 m_hasToolAlready = TRUE;
187
188 wxCHECK_MSG( bitmap.Ok(), (wxToolBarTool *)NULL,
189 "invalid bitmap for wxToolBar icon" );
190
191 wxToolBarTool *tool = new wxToolBarTool( this, toolIndex, bitmap, pushedBitmap,
192 toggle, clientData,
193 helpString1, helpString2 );
194
195 wxCHECK_MSG( bitmap.GetBitmap() == NULL, (wxToolBarTool *)NULL,
196 "wxToolBar doesn't support GdkBitmap" );
197
198 wxCHECK_MSG( bitmap.GetPixmap() != NULL, (wxToolBarTool *)NULL,
199 "wxToolBar::Add needs a wxBitmap" );
200
201 GtkWidget *tool_pixmap = (GtkWidget *)NULL;
202
203 GdkPixmap *pixmap = bitmap.GetPixmap();
204
205 GdkBitmap *mask = (GdkBitmap *)NULL;
206 if ( bitmap.GetMask() )
207 mask = bitmap.GetMask()->GetBitmap();
208
209 tool_pixmap = gtk_pixmap_new( pixmap, mask );
210
211 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
212
213 GtkToolbarChildType ctype = toggle ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
214 : GTK_TOOLBAR_CHILD_BUTTON;
215
216 GtkWidget *item = gtk_toolbar_append_element
217 (
218 GTK_TOOLBAR(m_toolbar),
219 ctype,
220 (GtkWidget *)NULL,
221 (const char *)NULL,
222 helpString1,
223 "",
224 tool_pixmap,
225 (GtkSignalFunc)gtk_toolbar_callback,
226 (gpointer)tool
227 );
228
229 tool->m_item = item;
230
231 gtk_signal_connect( GTK_OBJECT(tool->m_item),
232 "enter_notify_event",
233 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback),
234 (gpointer)tool );
235
236 m_tools.Append( tool );
237
238 return tool;
239 }
240
241 void wxToolBar::AddSeparator()
242 {
243 gtk_toolbar_append_space( m_toolbar );
244 }
245
246 void wxToolBar::ClearTools()
247 {
248 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
249 }
250
251 bool wxToolBar::Realize()
252 {
253 m_x = 0;
254 m_y = 0;
255 m_width = 100;
256 m_height = 0;
257
258 wxNode *node = m_tools.First();
259 while (node)
260 {
261 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
262 if (tool->m_bitmap1.Ok())
263 {
264 int tool_height = tool->m_bitmap1.GetHeight();
265 if (tool_height > m_height) m_height = tool_height;
266 }
267
268 node = node->Next();
269 }
270
271 m_height += 5 + 2*m_yMargin;
272
273 return TRUE;
274 }
275
276 void wxToolBar::EnableTool(int toolIndex, bool enable)
277 {
278 wxNode *node = m_tools.First();
279 while (node)
280 {
281 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
282 if (tool->m_index == toolIndex)
283 {
284 tool->m_enabled = enable;
285 return;
286 }
287 node = node->Next();
288 }
289
290 wxFAIL_MSG( "wrong toolbar index" );
291 }
292
293 void wxToolBar::ToggleTool( int toolIndex, bool toggle )
294 {
295 wxNode *node = m_tools.First();
296 while (node)
297 {
298 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
299 if (tool->m_index == toolIndex)
300 {
301 tool->m_toggleState = toggle;
302 if ((tool->m_item) && (GTK_IS_TOGGLE_BUTTON(tool->m_item)))
303 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool->m_item), toggle );
304 return;
305 }
306 node = node->Next();
307 }
308
309 wxFAIL_MSG( "wrong toolbar index" );
310 }
311
312 wxObject *wxToolBar::GetToolClientData( int index ) const
313 {
314 wxNode *node = m_tools.First();
315 while (node)
316 {
317 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
318 if (tool->m_index == index) return tool->m_clientData;;
319 node = node->Next();
320 }
321
322 wxFAIL_MSG( "wrong toolbar index" );
323
324 return (wxObject*)NULL;
325 }
326
327 bool wxToolBar::GetToolState(int toolIndex) const
328 {
329 wxNode *node = m_tools.First();
330 while (node)
331 {
332 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
333 if (tool->m_index == toolIndex) return tool->m_toggleState;
334 node = node->Next();
335 }
336
337 wxFAIL_MSG( "wrong toolbar index" );
338
339 return FALSE;
340 }
341
342 bool wxToolBar::GetToolEnabled(int toolIndex) const
343 {
344 wxNode *node = m_tools.First();
345 while (node)
346 {
347 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
348 if (tool->m_index == toolIndex) return tool->m_enabled;
349 node = node->Next();
350 }
351
352 wxFAIL_MSG( "wrong toolbar index" );
353
354 return FALSE;
355 }
356
357 void wxToolBar::SetMargins( int x, int y )
358 {
359 wxCHECK_RET( !m_hasToolAlready, "wxToolBar::SetMargins must be called before adding tool." );
360
361 if (x > 2) gtk_toolbar_append_space( m_toolbar ); // oh well
362
363 m_xMargin = x;
364 m_yMargin = y;
365 }
366
367 void wxToolBar::SetToolPacking( int WXUNUSED(packing) )
368 {
369 wxFAIL_MSG( "wxToolBar::SetToolPacking not implemented" );
370 }
371
372 void wxToolBar::SetToolSeparation( int separation )
373 {
374 gtk_toolbar_set_space_size( m_toolbar, separation );
375 m_separation = separation;
376 }
377
378 int wxToolBar::GetToolPacking()
379 {
380 return 0;
381 }
382
383 int wxToolBar::GetToolSeparation()
384 {
385 return m_separation;
386 }
387
388 wxString wxToolBar::GetToolLongHelp(int toolIndex)
389 {
390 wxNode *node = m_tools.First();
391 while (node)
392 {
393 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
394 if (tool->m_index == toolIndex)
395 {
396 return tool->m_longHelpString;
397 }
398 node = node->Next();
399 }
400
401 wxFAIL_MSG( "wrong toolbar index" );
402
403 return "";
404 }
405
406 wxString wxToolBar::GetToolShortHelp(int toolIndex)
407 {
408 wxNode *node = m_tools.First();
409 while (node)
410 {
411 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
412 if (tool->m_index == toolIndex)
413 {
414 return tool->m_shortHelpString;
415 }
416 node = node->Next();
417 }
418
419 wxFAIL_MSG( "wrong toolbar index" );
420
421 return "";
422 }
423
424 void wxToolBar::SetToolLongHelp(int toolIndex, const wxString& helpString)
425 {
426 wxNode *node = m_tools.First();
427 while (node)
428 {
429 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
430 if (tool->m_index == toolIndex)
431 {
432 tool->m_longHelpString = helpString;
433 return;
434 }
435 node = node->Next();
436 }
437
438 wxFAIL_MSG( "wrong toolbar index" );
439
440 return;
441 }
442
443 void wxToolBar::SetToolShortHelp(int toolIndex, const wxString& helpString)
444 {
445 wxNode *node = m_tools.First();
446 while (node)
447 {
448 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
449 if (tool->m_index == toolIndex)
450 {
451 tool->m_shortHelpString = helpString;
452 return;
453 }
454 node = node->Next();
455 }
456
457 wxFAIL_MSG( "wrong toolbar index" );
458
459 return;
460 }
461
462