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