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