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