]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/tbargtk.cpp
BC++ and Watcom C++ makefile and source fixes
[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
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 GtkRequisition req;
313 (* GTK_WIDGET_CLASS( GTK_OBJECT(m_widget)->klass )->size_request ) (m_widget, &req );
314 m_width = req.width;
315 m_height = req.height;
316
317 gtk_signal_connect( GTK_OBJECT(tool->m_item),
318 "enter_notify_event",
319 GTK_SIGNAL_FUNC(gtk_toolbar_enter_callback),
320 (gpointer)tool );
321
322 m_tools.Append( tool );
323
324 return tool;
325 }
326
327 void wxToolBar::AddSeparator()
328 {
329 gtk_toolbar_append_space( m_toolbar );
330 }
331
332 void wxToolBar::ClearTools()
333 {
334 wxFAIL_MSG( wxT("wxToolBar::ClearTools not implemented") );
335 }
336
337 bool wxToolBar::Realize()
338 {
339 m_x = 0;
340 m_y = 0;
341 m_width = 100;
342 m_height = 0;
343
344 wxNode *node = m_tools.First();
345 while (node)
346 {
347 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
348 if (tool->m_bitmap1.Ok())
349 {
350 int tool_height = tool->m_bitmap1.GetHeight();
351 if (tool_height > m_height) m_height = tool_height;
352 }
353
354 node = node->Next();
355 }
356
357 m_height += 5 + 2*m_yMargin;
358
359 return TRUE;
360 }
361
362 void wxToolBar::EnableTool(int toolIndex, bool enable)
363 {
364 wxNode *node = m_tools.First();
365 while (node)
366 {
367 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
368 if (tool->m_index == toolIndex)
369 {
370 tool->m_enabled = enable;
371
372 #if (GTK_MINOR_VERSION > 0)
373 /* we don't disable the tools for GTK 1.0 as the bitmaps don't get
374 greyed anyway and this also disables tooltips */
375 if (tool->m_item)
376 gtk_widget_set_sensitive( tool->m_item, enable );
377 #endif
378
379 return;
380 }
381 node = node->Next();
382 }
383
384 wxFAIL_MSG( wxT("wrong toolbar index") );
385 }
386
387 void wxToolBar::ToggleTool( int toolIndex, bool toggle )
388 {
389 wxNode *node = m_tools.First();
390 while (node)
391 {
392 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
393 if (tool->m_index == toolIndex)
394 {
395 if ((tool->m_item) && (GTK_IS_TOGGLE_BUTTON(tool->m_item)))
396 {
397 tool->m_toggleState = toggle;
398
399 if (tool->m_bitmap2.Ok())
400 {
401 wxBitmap bitmap = tool->m_bitmap1;
402 if (tool->m_toggleState) bitmap = tool->m_bitmap2;
403
404 GtkPixmap *pixmap = GTK_PIXMAP( tool->m_pixmap );
405
406 GdkBitmap *mask = (GdkBitmap *) NULL;
407 if (bitmap.GetMask()) mask = bitmap.GetMask()->GetBitmap();
408
409 gtk_pixmap_set( pixmap, bitmap.GetPixmap(), mask );
410 }
411
412 gtk_signal_disconnect_by_func( GTK_OBJECT(tool->m_item),
413 GTK_SIGNAL_FUNC(gtk_toolbar_callback), (gpointer*)tool );
414
415 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(tool->m_item), toggle );
416
417 gtk_signal_connect( GTK_OBJECT(tool->m_item), "clicked",
418 GTK_SIGNAL_FUNC(gtk_toolbar_callback), (gpointer*)tool );
419 }
420
421 return;
422 }
423 node = node->Next();
424 }
425
426 wxFAIL_MSG( wxT("wrong toolbar index") );
427 }
428
429 wxObject *wxToolBar::GetToolClientData( int index ) const
430 {
431 wxNode *node = m_tools.First();
432 while (node)
433 {
434 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
435 if (tool->m_index == index) return tool->m_clientData;;
436 node = node->Next();
437 }
438
439 wxFAIL_MSG( wxT("wrong toolbar index") );
440
441 return (wxObject*)NULL;
442 }
443
444 bool wxToolBar::GetToolState(int toolIndex) const
445 {
446 wxNode *node = m_tools.First();
447 while (node)
448 {
449 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
450 if (tool->m_index == toolIndex) return tool->m_toggleState;
451 node = node->Next();
452 }
453
454 wxFAIL_MSG( wxT("wrong toolbar index") );
455
456 return FALSE;
457 }
458
459 bool wxToolBar::GetToolEnabled(int toolIndex) const
460 {
461 wxNode *node = m_tools.First();
462 while (node)
463 {
464 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
465 if (tool->m_index == toolIndex) return tool->m_enabled;
466 node = node->Next();
467 }
468
469 wxFAIL_MSG( wxT("wrong toolbar index") );
470
471 return FALSE;
472 }
473
474 void wxToolBar::SetMargins( int x, int y )
475 {
476 wxCHECK_RET( !m_hasToolAlready, wxT("wxToolBar::SetMargins must be called before adding tool.") );
477
478 if (x > 2) gtk_toolbar_append_space( m_toolbar ); // oh well
479
480 m_xMargin = x;
481 m_yMargin = y;
482 }
483
484 void wxToolBar::SetToolPacking( int WXUNUSED(packing) )
485 {
486 wxFAIL_MSG( wxT("wxToolBar::SetToolPacking not implemented") );
487 }
488
489 void wxToolBar::SetToolSeparation( int separation )
490 {
491 gtk_toolbar_set_space_size( m_toolbar, separation );
492 m_separation = separation;
493 }
494
495 int wxToolBar::GetToolPacking()
496 {
497 return 0;
498 }
499
500 int wxToolBar::GetToolSeparation()
501 {
502 return m_separation;
503 }
504
505 wxString wxToolBar::GetToolLongHelp(int toolIndex)
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 return tool->m_longHelpString;
514 }
515 node = node->Next();
516 }
517
518 wxFAIL_MSG( wxT("wrong toolbar index") );
519
520 return wxT("");
521 }
522
523 wxString wxToolBar::GetToolShortHelp(int toolIndex)
524 {
525 wxNode *node = m_tools.First();
526 while (node)
527 {
528 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
529 if (tool->m_index == toolIndex)
530 {
531 return tool->m_shortHelpString;
532 }
533 node = node->Next();
534 }
535
536 wxFAIL_MSG( wxT("wrong toolbar index") );
537
538 return wxT("");
539 }
540
541 void wxToolBar::SetToolLongHelp(int toolIndex, const wxString& helpString)
542 {
543 wxNode *node = m_tools.First();
544 while (node)
545 {
546 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
547 if (tool->m_index == toolIndex)
548 {
549 tool->m_longHelpString = helpString;
550 return;
551 }
552 node = node->Next();
553 }
554
555 wxFAIL_MSG( wxT("wrong toolbar index") );
556
557 return;
558 }
559
560 void wxToolBar::SetToolShortHelp(int toolIndex, const wxString& helpString)
561 {
562 wxNode *node = m_tools.First();
563 while (node)
564 {
565 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
566 if (tool->m_index == toolIndex)
567 {
568 tool->m_shortHelpString = helpString;
569 return;
570 }
571 node = node->Next();
572 }
573
574 wxFAIL_MSG( wxT("wrong toolbar index") );
575
576 return;
577 }
578
579 void wxToolBar::OnIdle( wxIdleEvent &WXUNUSED(ievent) )
580 {
581 wxEvtHandler* evtHandler = GetEventHandler();
582
583 wxNode* node = m_tools.First();
584 while (node)
585 {
586 wxToolBarTool* tool = (wxToolBarTool*) node->Data();
587
588 wxUpdateUIEvent event( tool->m_index );
589 event.SetEventObject(this);
590
591 if (evtHandler->ProcessEvent( event ))
592 {
593 if (event.GetSetEnabled())
594 EnableTool(tool->m_index, event.GetEnabled());
595 if (event.GetSetChecked())
596 ToggleTool(tool->m_index, event.GetChecked());
597 /*
598 if (event.GetSetText())
599 // Set tooltip?
600 */
601 }
602
603 node = node->Next();
604 }
605 }
606
607 void wxToolBar::OnInternalIdle()
608 {
609 wxCursor cursor = m_cursor;
610 if (g_globalCursor.Ok()) cursor = g_globalCursor;
611
612 if (cursor.Ok())
613 {
614 /* I now set the cursor the anew in every OnInternalIdle call
615 as setting the cursor in a parent window also effects the
616 windows above so that checking for the current cursor is
617 not possible. */
618
619 if (HasFlag(wxTB_DOCKABLE))
620 {
621 /* if the toolbar is dockable, then m_widget stands for the
622 GtkHandleBox widget, which uses its own window so that we
623 can set the cursor for it. if the toolbar is not dockable,
624 m_widget comes from m_toolbar which uses its parent's
625 window ("windowless windows") and thus we cannot set the
626 cursor. */
627 gdk_window_set_cursor( m_widget->window, cursor.GetCursor() );
628 }
629
630 wxNode *node = m_tools.First();
631 while (node)
632 {
633 wxToolBarTool *tool = (wxToolBarTool*)node->Data();
634 if (!tool->m_item->window)
635 break;
636 else
637 gdk_window_set_cursor( tool->m_item->window, cursor.GetCursor() );
638
639 node = node->Next();
640 }
641 }
642
643 UpdateWindowUI();
644 }
645
646 #endif