]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/tbargtk.cpp
5712f800d3940a22fc8032104297fe3a8829fd0b
[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 if (style & wxTB_DOCKABLE)
128 {
129 m_widget = gtk_handle_box_new();
130 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) );
131 gtk_widget_show( GTK_WIDGET(m_toolbar) );
132 }
133 else
134 {
135 m_widget = GTK_WIDGET(m_toolbar);
136 }
137
138 gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE );
139
140 m_fg = new GdkColor;
141 m_fg->red = 0;
142 m_fg->green = 0;
143 m_fg->blue = 0;
144 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_fg );
145
146 m_bg = new GdkColor;
147 m_bg->red = 65535;
148 m_bg->green = 65535;
149 m_bg->blue = 50000;
150 gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_bg );
151
152 gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar)->tooltips, m_bg, m_fg );
153
154 m_xMargin = 0;
155 m_yMargin = 0;
156
157 m_parent->AddChild( this );
158
159 (m_parent->m_insertCallback)( m_parent, this );
160
161 PostCreation();
162
163 Show( TRUE );
164
165 return TRUE;
166 }
167
168 bool wxToolBar::OnLeftClick( int toolIndex, bool toggleDown )
169 {
170 wxCommandEvent event( wxEVT_COMMAND_TOOL_CLICKED, toolIndex );
171 event.SetEventObject(this);
172 event.SetInt( toolIndex );
173 event.SetExtraLong((long) toggleDown);
174
175 GetEventHandler()->ProcessEvent(event);
176
177 return TRUE;
178 }
179
180 void wxToolBar::OnRightClick( int toolIndex, float WXUNUSED(x), float WXUNUSED(y) )
181 {
182 wxCommandEvent event( wxEVT_COMMAND_TOOL_RCLICKED, toolIndex );
183 event.SetEventObject( this );
184 event.SetInt( toolIndex );
185
186 GetEventHandler()->ProcessEvent(event);
187 }
188
189 void wxToolBar::OnMouseEnter( int toolIndex )
190 {
191 wxCommandEvent event( wxEVT_COMMAND_TOOL_ENTER, GetId() );
192 event.SetEventObject(this);
193 event.SetInt( toolIndex );
194
195 GetEventHandler()->ProcessEvent(event);
196 }
197
198 wxToolBarTool *wxToolBar::AddTool( int toolIndex, const wxBitmap& bitmap,
199 const wxBitmap& pushedBitmap, bool toggle,
200 float WXUNUSED(xPos), float WXUNUSED(yPos), wxObject *clientData,
201 const wxString& helpString1, const wxString& helpString2 )
202 {
203 m_hasToolAlready = TRUE;
204
205 wxCHECK_MSG( bitmap.Ok(), (wxToolBarTool *)NULL,
206 "invalid bitmap for wxToolBar icon" );
207
208 wxCHECK_MSG( bitmap.GetBitmap() == NULL, (wxToolBarTool *)NULL,
209 "wxToolBar doesn't support GdkBitmap" );
210
211 wxCHECK_MSG( bitmap.GetPixmap() != NULL, (wxToolBarTool *)NULL,
212 "wxToolBar::Add needs a wxBitmap" );
213
214 GtkWidget *tool_pixmap = (GtkWidget *)NULL;
215
216 GdkPixmap *pixmap = bitmap.GetPixmap();
217
218 GdkBitmap *mask = (GdkBitmap *)NULL;
219 if ( bitmap.GetMask() )
220 mask = bitmap.GetMask()->GetBitmap();
221
222 tool_pixmap = gtk_pixmap_new( pixmap, mask );
223
224 gtk_misc_set_alignment( GTK_MISC(tool_pixmap), 0.5, 0.5 );
225
226 wxToolBarTool *tool = new wxToolBarTool( this, toolIndex, bitmap, pushedBitmap,
227 toggle, clientData,
228 helpString1, helpString2,
229 tool_pixmap );
230
231 GtkToolbarChildType ctype = toggle ? GTK_TOOLBAR_CHILD_TOGGLEBUTTON
232 : GTK_TOOLBAR_CHILD_BUTTON;
233
234 GtkWidget *item = gtk_toolbar_append_element
235 (
236 GTK_TOOLBAR(m_toolbar),
237 ctype,
238 (GtkWidget *)NULL,
239 (const char *)NULL,
240 helpString1,
241 "",
242 tool_pixmap,
243 (GtkSignalFunc)gtk_toolbar_callback,
244 (gpointer)tool
245 );
246
247 tool->m_item = item;
248
249 gtk_signal_connect( GTK_OBJECT(tool->m_item),
250 "enter_notify_event",
251 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback),
252 (gpointer)tool );
253
254 m_tools.Append( tool );
255
256 return tool;
257 }
258
259 void wxToolBar::AddSeparator()
260 {
261 gtk_toolbar_append_space( m_toolbar );
262 }
263
264 void wxToolBar::ClearTools()
265 {
266 wxFAIL_MSG( "wxToolBar::ClearTools not implemented" );
267 }
268
269 bool wxToolBar::Realize()
270 {
271 m_x = 0;
272 m_y = 0;
273 m_width = 100;
274 m_height = 0;
275
276 wxNode *node = m_tools.First();
277 while (node)
278 {
279 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
280 if (tool->m_bitmap1.Ok())
281 {
282 int tool_height = tool->m_bitmap1.GetHeight();
283 if (tool_height > m_height) m_height = tool_height;
284 }
285
286 node = node->Next();
287 }
288
289 m_height += 5 + 2*m_yMargin;
290
291 return TRUE;
292 }
293
294 void wxToolBar::EnableTool(int toolIndex, bool enable)
295 {
296 wxNode *node = m_tools.First();
297 while (node)
298 {
299 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
300 if (tool->m_index == toolIndex)
301 {
302 tool->m_enabled = enable;
303
304 if (tool->m_item)
305 gtk_widget_set_sensitive( tool->m_item, enable );
306
307 return;
308 }
309 node = node->Next();
310 }
311
312 wxFAIL_MSG( "wrong toolbar index" );
313 }
314
315 void wxToolBar::ToggleTool( int toolIndex, bool toggle )
316 {
317 wxNode *node = m_tools.First();
318 while (node)
319 {
320 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
321 if (tool->m_index == toolIndex)
322 {
323 tool->m_toggleState = toggle;
324 if ((tool->m_item) && (GTK_IS_TOGGLE_BUTTON(tool->m_item)))
325 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool->m_item), toggle );
326 return;
327 }
328 node = node->Next();
329 }
330
331 wxFAIL_MSG( "wrong toolbar index" );
332 }
333
334 wxObject *wxToolBar::GetToolClientData( int index ) const
335 {
336 wxNode *node = m_tools.First();
337 while (node)
338 {
339 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
340 if (tool->m_index == index) return tool->m_clientData;;
341 node = node->Next();
342 }
343
344 wxFAIL_MSG( "wrong toolbar index" );
345
346 return (wxObject*)NULL;
347 }
348
349 bool wxToolBar::GetToolState(int toolIndex) const
350 {
351 wxNode *node = m_tools.First();
352 while (node)
353 {
354 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
355 if (tool->m_index == toolIndex) return tool->m_toggleState;
356 node = node->Next();
357 }
358
359 wxFAIL_MSG( "wrong toolbar index" );
360
361 return FALSE;
362 }
363
364 bool wxToolBar::GetToolEnabled(int toolIndex) const
365 {
366 wxNode *node = m_tools.First();
367 while (node)
368 {
369 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
370 if (tool->m_index == toolIndex) return tool->m_enabled;
371 node = node->Next();
372 }
373
374 wxFAIL_MSG( "wrong toolbar index" );
375
376 return FALSE;
377 }
378
379 void wxToolBar::SetMargins( int x, int y )
380 {
381 wxCHECK_RET( !m_hasToolAlready, "wxToolBar::SetMargins must be called before adding tool." );
382
383 if (x > 2) gtk_toolbar_append_space( m_toolbar ); // oh well
384
385 m_xMargin = x;
386 m_yMargin = y;
387 }
388
389 void wxToolBar::SetToolPacking( int WXUNUSED(packing) )
390 {
391 wxFAIL_MSG( "wxToolBar::SetToolPacking not implemented" );
392 }
393
394 void wxToolBar::SetToolSeparation( int separation )
395 {
396 gtk_toolbar_set_space_size( m_toolbar, separation );
397 m_separation = separation;
398 }
399
400 int wxToolBar::GetToolPacking()
401 {
402 return 0;
403 }
404
405 int wxToolBar::GetToolSeparation()
406 {
407 return m_separation;
408 }
409
410 wxString wxToolBar::GetToolLongHelp(int toolIndex)
411 {
412 wxNode *node = m_tools.First();
413 while (node)
414 {
415 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
416 if (tool->m_index == toolIndex)
417 {
418 return tool->m_longHelpString;
419 }
420 node = node->Next();
421 }
422
423 wxFAIL_MSG( "wrong toolbar index" );
424
425 return "";
426 }
427
428 wxString wxToolBar::GetToolShortHelp(int toolIndex)
429 {
430 wxNode *node = m_tools.First();
431 while (node)
432 {
433 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
434 if (tool->m_index == toolIndex)
435 {
436 return tool->m_shortHelpString;
437 }
438 node = node->Next();
439 }
440
441 wxFAIL_MSG( "wrong toolbar index" );
442
443 return "";
444 }
445
446 void wxToolBar::SetToolLongHelp(int toolIndex, const wxString& helpString)
447 {
448 wxNode *node = m_tools.First();
449 while (node)
450 {
451 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
452 if (tool->m_index == toolIndex)
453 {
454 tool->m_longHelpString = helpString;
455 return;
456 }
457 node = node->Next();
458 }
459
460 wxFAIL_MSG( "wrong toolbar index" );
461
462 return;
463 }
464
465 void wxToolBar::SetToolShortHelp(int toolIndex, const wxString& helpString)
466 {
467 wxNode *node = m_tools.First();
468 while (node)
469 {
470 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
471 if (tool->m_index == toolIndex)
472 {
473 tool->m_shortHelpString = helpString;
474 return;
475 }
476 node = node->Next();
477 }
478
479 wxFAIL_MSG( "wrong toolbar index" );
480
481 return;
482 }
483
484