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