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