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