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