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