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