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