]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
76b49cf4 | 2 | // Name: src/gtk/tbargtk.cpp |
c801d85f KB |
3 | // Purpose: GTK toolbar |
4 | // Author: Robert Roebling | |
8a0681f9 | 5 | // Modified: 13.12.99 by VZ to derive from wxToolBarBase |
32e9da8b | 6 | // RCS-ID: $Id$ |
c801d85f | 7 | // Copyright: (c) Robert Roebling |
65571936 | 8 | // Licence: wxWindows licence |
c801d85f KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
8a0681f9 VZ |
11 | // ============================================================================ |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
14f355c2 VS |
19 | // For compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
8a0681f9 | 22 | #if wxUSE_TOOLBAR_NATIVE |
dcf924a3 | 23 | |
4e3e485b WS |
24 | #include "wx/toolbar.h" |
25 | ||
76b49cf4 WS |
26 | #ifndef WX_PRECOMP |
27 | #include "wx/frame.h" | |
28 | #endif | |
c801d85f | 29 | |
1efb5db8 MR |
30 | // FIXME: Use GtkImage instead of GtkPixmap. Use the new toolbar API for when gtk runtime is new enough? |
31 | // Beware that the new and old toolbar API may not be mixed in usage. | |
32 | #include <gtk/gtkversion.h> | |
33 | #ifdef GTK_DISABLE_DEPRECATED | |
34 | #undef GTK_DISABLE_DEPRECATED | |
35 | #endif | |
36 | ||
9e691f46 | 37 | #include "wx/gtk/private.h" |
4a32fcf1 | 38 | #include "wx/wxcrt.h" |
fc6557a6 RR |
39 | #include "wx/menu.h" |
40 | ||
41 | ||
42 | /* XPM */ | |
43 | static const char *arrow_down_xpm[] = { | |
44 | /* columns rows colors chars-per-pixel */ | |
45 | "7 7 2 1", | |
46 | " c None", | |
47 | ". c Black", | |
48 | /* pixels */ | |
49 | " ", | |
50 | " ", | |
51 | " ", | |
52 | ".......", | |
53 | " ..... ", | |
54 | " ... ", | |
55 | " . " | |
56 | }; | |
57 | ||
83624f79 | 58 | |
8a0681f9 VZ |
59 | // ---------------------------------------------------------------------------- |
60 | // globals | |
61 | // ---------------------------------------------------------------------------- | |
acfd422a | 62 | |
314055fa | 63 | // data |
9b7e522a RR |
64 | extern bool g_blockEventsOnDrag; |
65 | extern wxCursor g_globalCursor; | |
314055fa | 66 | |
e76c0b5f VZ |
67 | // ---------------------------------------------------------------------------- |
68 | // private functions | |
69 | // ---------------------------------------------------------------------------- | |
70 | ||
77ffb593 | 71 | // translate wxWidgets toolbar style flags to GTK orientation and style |
e76c0b5f VZ |
72 | static void GetGtkStyle(long style, |
73 | GtkOrientation *orient, GtkToolbarStyle *gtkStyle) | |
74 | { | |
7a976304 | 75 | *orient = ( style & wxTB_LEFT || style & wxTB_RIGHT ) ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL; |
e76c0b5f VZ |
76 | |
77 | ||
78 | if ( style & wxTB_TEXT ) | |
79 | { | |
99e8cb50 VZ |
80 | *gtkStyle = style & wxTB_NOICONS |
81 | ? GTK_TOOLBAR_TEXT | |
82 | : ( | |
99e8cb50 | 83 | style & wxTB_HORZ_LAYOUT ? GTK_TOOLBAR_BOTH_HORIZ : |
99e8cb50 | 84 | GTK_TOOLBAR_BOTH); |
e76c0b5f VZ |
85 | } |
86 | else // no text, hence we must have the icons or what would we show? | |
87 | { | |
88 | *gtkStyle = GTK_TOOLBAR_ICONS; | |
89 | } | |
90 | } | |
91 | ||
8a0681f9 VZ |
92 | // ---------------------------------------------------------------------------- |
93 | // wxToolBarTool | |
94 | // ---------------------------------------------------------------------------- | |
95 | ||
96 | class wxToolBarTool : public wxToolBarToolBase | |
97 | { | |
98 | public: | |
99 | wxToolBarTool(wxToolBar *tbar, | |
100 | int id, | |
e76c0b5f | 101 | const wxString& label, |
8a0681f9 VZ |
102 | const wxBitmap& bitmap1, |
103 | const wxBitmap& bitmap2, | |
e76c0b5f | 104 | wxItemKind kind, |
8a0681f9 VZ |
105 | wxObject *clientData, |
106 | const wxString& shortHelpString, | |
107 | const wxString& longHelpString) | |
e76c0b5f | 108 | : wxToolBarToolBase(tbar, id, label, bitmap1, bitmap2, kind, |
8a0681f9 VZ |
109 | clientData, shortHelpString, longHelpString) |
110 | { | |
111 | Init(); | |
112 | } | |
113 | ||
07d02e9e VZ |
114 | wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label) |
115 | : wxToolBarToolBase(tbar, control, label) | |
8a0681f9 VZ |
116 | { |
117 | Init(); | |
118 | } | |
119 | ||
38762f09 VZ |
120 | // is this a radio button? |
121 | // | |
122 | // unlike GetKind(), can be called for any kind of tools, not just buttons | |
123 | bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO; } | |
124 | ||
e76c0b5f VZ |
125 | // this is only called for the normal buttons, i.e. not separators nor |
126 | // controls | |
127 | GtkToolbarChildType GetGtkChildType() const | |
128 | { | |
129 | switch ( GetKind() ) | |
130 | { | |
131 | case wxITEM_CHECK: | |
132 | return GTK_TOOLBAR_CHILD_TOGGLEBUTTON; | |
133 | ||
134 | case wxITEM_RADIO: | |
135 | return GTK_TOOLBAR_CHILD_RADIOBUTTON; | |
136 | ||
137 | default: | |
138 | wxFAIL_MSG( _T("unknown toolbar child type") ); | |
139 | // fall through | |
140 | ||
141 | case wxITEM_NORMAL: | |
142 | return GTK_TOOLBAR_CHILD_BUTTON; | |
143 | } | |
144 | } | |
145 | ||
eba91e51 | 146 | void SetImage(const wxBitmap& bitmap) |
ab86c659 VS |
147 | { |
148 | if (bitmap.Ok()) | |
149 | { | |
eba91e51 PC |
150 | // setting from pixmap doesn't seem to work right, but pixbuf works well |
151 | gtk_image_set_from_pixbuf((GtkImage*)m_image, bitmap.GetPixbuf()); | |
ab86c659 VS |
152 | } |
153 | } | |
154 | ||
8a0681f9 | 155 | GtkWidget *m_item; |
eba91e51 | 156 | GtkWidget *m_image; |
8a0681f9 VZ |
157 | |
158 | protected: | |
159 | void Init(); | |
160 | }; | |
161 | ||
162 | // ---------------------------------------------------------------------------- | |
163 | // wxWin macros | |
164 | // ---------------------------------------------------------------------------- | |
165 | ||
2eb10e2a | 166 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) |
8a0681f9 VZ |
167 | |
168 | // ============================================================================ | |
169 | // implementation | |
170 | // ============================================================================ | |
171 | ||
c801d85f | 172 | //----------------------------------------------------------------------------- |
2f2aa628 | 173 | // "clicked" (internal from gtk_toolbar) |
c801d85f KB |
174 | //----------------------------------------------------------------------------- |
175 | ||
865bb325 | 176 | extern "C" { |
8a0681f9 VZ |
177 | static void gtk_toolbar_callback( GtkWidget *WXUNUSED(widget), |
178 | wxToolBarTool *tool ) | |
c801d85f | 179 | { |
8a0681f9 | 180 | wxToolBar *tbar = (wxToolBar *)tool->GetToolBar(); |
248bcf0a | 181 | |
9864c56d | 182 | if (tbar->m_blockEvent) return; |
acfd422a | 183 | |
1144d24d | 184 | if (g_blockEventsOnDrag) return; |
8a0681f9 | 185 | if (!tool->IsEnabled()) return; |
a3622daa | 186 | |
8a0681f9 | 187 | if (tool->CanBeToggled()) |
248bcf0a | 188 | { |
8a0681f9 VZ |
189 | tool->Toggle(); |
190 | ||
eba91e51 | 191 | tool->SetImage(tool->GetBitmap()); |
38762f09 VZ |
192 | |
193 | if ( tool->IsRadio() && !tool->IsToggled() ) | |
194 | { | |
195 | // radio button went up, don't report this as a wxWin event | |
196 | return; | |
197 | } | |
85eb36c2 | 198 | } |
a3622daa | 199 | |
6bb7cee4 VZ |
200 | if( !tbar->OnLeftClick( tool->GetId(), tool->IsToggled() ) && tool->CanBeToggled() ) |
201 | { | |
202 | // revert back | |
203 | tool->Toggle(); | |
204 | ||
eba91e51 | 205 | tool->SetImage(tool->GetBitmap()); |
6bb7cee4 | 206 | } |
fc008f25 | 207 | } |
865bb325 | 208 | } |
c801d85f | 209 | |
729b4756 RR |
210 | //----------------------------------------------------------------------------- |
211 | // "right-click" | |
212 | //----------------------------------------------------------------------------- | |
213 | extern "C" { | |
214 | static gboolean gtk_toolbar_tool_rclick_callback(GtkWidget *WXUNUSED(widget), | |
215 | GdkEventButton *event, | |
216 | wxToolBarToolBase *tool) | |
217 | { | |
218 | if (event->button != 3) | |
219 | return FALSE; | |
220 | ||
221 | wxToolBar *tbar = (wxToolBar *)tool->GetToolBar(); | |
222 | ||
223 | if (tbar->m_blockEvent) return TRUE; | |
224 | ||
225 | if (g_blockEventsOnDrag) return TRUE; | |
226 | if (!tool->IsEnabled()) return TRUE; | |
227 | ||
228 | tbar->OnRightClick( tool->GetId(), (int)event->x, (int)event->y ); | |
229 | ||
230 | return TRUE; | |
231 | } | |
232 | } | |
233 | ||
fc6557a6 RR |
234 | //----------------------------------------------------------------------------- |
235 | // "enter_notify_event" / "leave_notify_event" from dropdown | |
236 | //----------------------------------------------------------------------------- | |
237 | ||
238 | extern "C" { | |
239 | static gint gtk_toolbar_buddy_enter_callback( GtkWidget *WXUNUSED(widget), | |
240 | GdkEventCrossing *WXUNUSED(gdk_event), | |
241 | GtkWidget *tool ) | |
242 | { | |
243 | guint8 state = GTK_WIDGET_STATE( tool ); | |
244 | state |= GTK_STATE_PRELIGHT; | |
245 | gtk_widget_set_state( tool, (GtkStateType) state ); | |
246 | return FALSE; | |
247 | } | |
248 | ||
249 | static gint gtk_toolbar_buddy_leave_callback( GtkWidget *WXUNUSED(widget), | |
250 | GdkEventCrossing *WXUNUSED(gdk_event), | |
251 | GtkWidget *tool ) | |
252 | { | |
253 | guint8 state = GTK_WIDGET_STATE( tool ); | |
254 | state &= ~GTK_STATE_PRELIGHT; | |
255 | gtk_widget_set_state( tool, (GtkStateType) state ); | |
256 | return FALSE; | |
257 | } | |
258 | } | |
259 | ||
260 | //----------------------------------------------------------------------------- | |
261 | // "left-click" on dropdown | |
262 | //----------------------------------------------------------------------------- | |
263 | ||
264 | extern "C" | |
265 | { | |
266 | static void gtk_pop_tb_hide_callback( GtkWidget *WXUNUSED(menu), GtkToggleButton *button ) | |
267 | { | |
268 | gtk_toggle_button_set_active( button, FALSE ); | |
269 | } | |
270 | ||
271 | static gboolean gtk_toolbar_dropdown_lclick_callback(GtkWidget *widget, | |
272 | GdkEventButton *event, | |
273 | wxToolBarToolBase *tool) | |
274 | { | |
275 | if (event->button != 1) | |
276 | return FALSE; | |
277 | ||
278 | wxToolBar *tbar = (wxToolBar *)tool->GetToolBar(); | |
279 | ||
280 | if (tbar->m_blockEvent) return FALSE; | |
281 | ||
282 | if (g_blockEventsOnDrag) return FALSE; | |
283 | if (!tool->IsEnabled()) return FALSE; | |
284 | ||
285 | wxCommandEvent evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED, tool->GetId() ); | |
286 | if ( tbar->GetEventHandler()->ProcessEvent(evt) ) | |
287 | { | |
288 | return TRUE; | |
289 | } | |
290 | ||
291 | wxMenu * const menu = tool->GetDropdownMenu(); | |
292 | if (!menu) | |
293 | return TRUE; | |
294 | ||
295 | // simulate press | |
296 | gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(widget), TRUE ); | |
297 | ||
298 | g_signal_connect (menu->m_menu, "hide", | |
299 | G_CALLBACK (gtk_pop_tb_hide_callback), | |
300 | widget); | |
301 | ||
302 | tbar->PopupMenu( menu, widget->allocation.x, | |
303 | widget->allocation.y + widget->allocation.height ); | |
304 | ||
305 | ||
306 | return TRUE; | |
307 | } | |
308 | } | |
309 | ||
2f2aa628 | 310 | //----------------------------------------------------------------------------- |
a8945eef | 311 | // "enter_notify_event" / "leave_notify_event" |
2f2aa628 RR |
312 | //----------------------------------------------------------------------------- |
313 | ||
865bb325 | 314 | extern "C" { |
248bcf0a | 315 | static gint gtk_toolbar_tool_callback( GtkWidget *WXUNUSED(widget), |
a8945eef MB |
316 | GdkEventCrossing *gdk_event, |
317 | wxToolBarTool *tool ) | |
314055fa | 318 | { |
1144d24d | 319 | if (g_blockEventsOnDrag) return TRUE; |
248bcf0a | 320 | |
8a0681f9 | 321 | wxToolBar *tb = (wxToolBar *)tool->GetToolBar(); |
248bcf0a | 322 | |
47c93b63 | 323 | // emit the event |
a8945eef MB |
324 | if( gdk_event->type == GDK_ENTER_NOTIFY ) |
325 | tb->OnMouseEnter( tool->GetId() ); | |
326 | else | |
327 | tb->OnMouseEnter( -1 ); | |
248bcf0a | 328 | |
1144d24d | 329 | return FALSE; |
314055fa | 330 | } |
865bb325 | 331 | } |
314055fa | 332 | |
86b65467 RR |
333 | extern "C" { |
334 | static | |
335 | void gtktoolwidget_size_callback( GtkWidget *widget, | |
336 | GtkAllocation *alloc, | |
337 | wxWindow *win ) | |
338 | { | |
339 | // this shouldn't happen... | |
340 | if (win->GetParent()->m_wxwindow) return; | |
f4322df6 | 341 | |
86b65467 RR |
342 | wxSize size = win->GetEffectiveMinSize(); |
343 | if (size.y != alloc->height) | |
344 | { | |
345 | GtkAllocation alloc2; | |
346 | alloc2.x = alloc->x; | |
347 | alloc2.y = (alloc->height - size.y + 3) / 2; | |
348 | alloc2.width = alloc->width; | |
349 | alloc2.height = size.y; | |
350 | gtk_widget_size_allocate( widget, &alloc2 ); | |
351 | } | |
352 | } | |
353 | } | |
bf9e3e73 RR |
354 | //----------------------------------------------------------------------------- |
355 | // InsertChild callback for wxToolBar | |
356 | //----------------------------------------------------------------------------- | |
357 | ||
c821db16 | 358 | static void wxInsertChildInToolBar( wxWindow* WXUNUSED(parent), |
8a0681f9 | 359 | wxWindow* WXUNUSED(child) ) |
bf9e3e73 | 360 | { |
47c93b63 | 361 | // we don't do anything here |
bf9e3e73 RR |
362 | } |
363 | ||
8a0681f9 VZ |
364 | // ---------------------------------------------------------------------------- |
365 | // wxToolBarTool | |
366 | // ---------------------------------------------------------------------------- | |
c801d85f | 367 | |
8a0681f9 VZ |
368 | void wxToolBarTool::Init() |
369 | { | |
370 | m_item = | |
eba91e51 | 371 | m_image = NULL; |
8a0681f9 | 372 | } |
c801d85f | 373 | |
8a0681f9 | 374 | wxToolBarToolBase *wxToolBar::CreateTool(int id, |
e76c0b5f | 375 | const wxString& text, |
8a0681f9 VZ |
376 | const wxBitmap& bitmap1, |
377 | const wxBitmap& bitmap2, | |
e76c0b5f | 378 | wxItemKind kind, |
8a0681f9 VZ |
379 | wxObject *clientData, |
380 | const wxString& shortHelpString, | |
381 | const wxString& longHelpString) | |
382 | { | |
e76c0b5f | 383 | return new wxToolBarTool(this, id, text, bitmap1, bitmap2, kind, |
8a0681f9 VZ |
384 | clientData, shortHelpString, longHelpString); |
385 | } | |
b1da76e1 | 386 | |
07d02e9e VZ |
387 | wxToolBarToolBase * |
388 | wxToolBar::CreateTool(wxControl *control, const wxString& label) | |
c801d85f | 389 | { |
07d02e9e | 390 | return new wxToolBarTool(this, control, label); |
fc008f25 | 391 | } |
c801d85f | 392 | |
8a0681f9 VZ |
393 | //----------------------------------------------------------------------------- |
394 | // wxToolBar construction | |
395 | //----------------------------------------------------------------------------- | |
396 | ||
397 | void wxToolBar::Init() | |
c801d85f | 398 | { |
8a0681f9 | 399 | m_toolbar = (GtkToolbar *)NULL; |
91af0895 | 400 | m_blockEvent = false; |
d2c0a964 RD |
401 | m_defaultWidth = 32; |
402 | m_defaultHeight = 32; | |
fc008f25 | 403 | } |
c801d85f | 404 | |
a3622daa | 405 | wxToolBar::~wxToolBar() |
c801d85f | 406 | { |
fc008f25 | 407 | } |
c801d85f | 408 | |
8a0681f9 VZ |
409 | bool wxToolBar::Create( wxWindow *parent, |
410 | wxWindowID id, | |
411 | const wxPoint& pos, | |
412 | const wxSize& size, | |
413 | long style, | |
414 | const wxString& name ) | |
c801d85f | 415 | { |
c821db16 | 416 | m_insertCallback = wxInsertChildInToolBar; |
a3622daa | 417 | |
8a0681f9 VZ |
418 | if ( !PreCreation( parent, pos, size ) || |
419 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
4dcaf11a | 420 | { |
223d09f6 | 421 | wxFAIL_MSG( wxT("wxToolBar creation failed") ); |
c801d85f | 422 | |
91af0895 | 423 | return false; |
8a0681f9 | 424 | } |
a3622daa | 425 | |
d408730c VZ |
426 | FixupStyle(); |
427 | ||
9e691f46 | 428 | m_toolbar = GTK_TOOLBAR( gtk_toolbar_new() ); |
e76c0b5f | 429 | GtkSetStyle(); |
99e8cb50 | 430 | |
2b5f62a0 VZ |
431 | // Doesn't work this way. |
432 | // GtkToolbarSpaceStyle space_style = GTK_TOOLBAR_SPACE_EMPTY; | |
433 | // gtk_widget_style_set (GTK_WIDGET (m_toolbar), "space_style", &space_style, NULL); | |
a3622daa | 434 | |
8a0681f9 | 435 | SetToolSeparation(7); |
3502e687 RR |
436 | |
437 | if (style & wxTB_DOCKABLE) | |
438 | { | |
439 | m_widget = gtk_handle_box_new(); | |
f03fc89f VZ |
440 | gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) ); |
441 | gtk_widget_show( GTK_WIDGET(m_toolbar) ); | |
8a0681f9 | 442 | |
f03fc89f | 443 | if (style & wxTB_FLAT) |
858b5bdd | 444 | gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget), GTK_SHADOW_NONE ); |
3502e687 RR |
445 | } |
446 | else | |
248bcf0a RD |
447 | { |
448 | m_widget = gtk_event_box_new(); | |
449 | gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar) ); | |
450 | ConnectWidget( m_widget ); | |
451 | gtk_widget_show(GTK_WIDGET(m_toolbar)); | |
3502e687 | 452 | } |
8a0681f9 | 453 | |
9e691f46 | 454 | // FIXME: there is no such function for toolbars in 2.0 |
68567a96 | 455 | #if 0 |
858b5bdd RR |
456 | if (style & wxTB_FLAT) |
457 | gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar), GTK_RELIEF_NONE ); | |
9e691f46 | 458 | #endif |
be25e480 | 459 | |
f03fc89f | 460 | m_parent->DoAddChild( this ); |
8a0681f9 | 461 | |
abdeb9e7 | 462 | PostCreation(size); |
a3622daa | 463 | |
91af0895 | 464 | return true; |
fc008f25 | 465 | } |
c801d85f | 466 | |
48468900 RR |
467 | GdkWindow *wxToolBar::GTKGetWindow(wxArrayGdkWindows& windows) const |
468 | { | |
469 | return GTK_WIDGET(m_toolbar)->window; | |
470 | } | |
471 | ||
e76c0b5f VZ |
472 | void wxToolBar::GtkSetStyle() |
473 | { | |
474 | GtkOrientation orient; | |
475 | GtkToolbarStyle style; | |
476 | GetGtkStyle(GetWindowStyle(), &orient, &style); | |
477 | ||
478 | gtk_toolbar_set_orientation(m_toolbar, orient); | |
479 | gtk_toolbar_set_style(m_toolbar, style); | |
8c4e2405 | 480 | gtk_toolbar_set_tooltips(m_toolbar, !(style & wxTB_NO_TOOLTIPS)); |
e76c0b5f VZ |
481 | } |
482 | ||
483 | void wxToolBar::SetWindowStyleFlag( long style ) | |
484 | { | |
485 | wxToolBarBase::SetWindowStyleFlag(style); | |
8ad31f9d | 486 | |
e76c0b5f VZ |
487 | if ( m_toolbar ) |
488 | GtkSetStyle(); | |
489 | } | |
490 | ||
8a0681f9 | 491 | bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase) |
c801d85f | 492 | { |
8c4e2405 | 493 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase); |
248bcf0a | 494 | |
8a0681f9 VZ |
495 | if ( tool->IsButton() ) |
496 | { | |
2b5f62a0 VZ |
497 | if ( !HasFlag(wxTB_NOICONS) ) |
498 | { | |
499 | wxBitmap bitmap = tool->GetNormalBitmap(); | |
c801d85f | 500 | |
91af0895 | 501 | wxCHECK_MSG( bitmap.Ok(), false, |
2b5f62a0 | 502 | wxT("invalid bitmap for wxToolBar icon") ); |
a3622daa | 503 | |
eba91e51 PC |
504 | tool->m_image = gtk_image_new(); |
505 | tool->SetImage(bitmap); | |
248bcf0a | 506 | |
eba91e51 | 507 | gtk_misc_set_alignment((GtkMisc*)tool->m_image, 0.5, 0.5); |
2b5f62a0 | 508 | } |
8a0681f9 | 509 | } |
c801d85f | 510 | |
fc6557a6 RR |
511 | int posGtk = 0; |
512 | if (pos > 0) | |
513 | { | |
514 | size_t i; | |
515 | for (i = 0; i < pos; i++) | |
516 | { | |
517 | posGtk++; | |
518 | ||
519 | ||
520 | // if we have a dropdown menu, we use 2 GTK tools | |
521 | // internally | |
522 | wxToolBarToolsList::compatibility_iterator node = m_tools.Item( i ); | |
523 | wxToolBarTool *tool = (wxToolBarTool*) node->GetData(); | |
524 | if (tool->IsButton() && (tool->GetKind() == wxITEM_DROPDOWN)) | |
525 | posGtk++; | |
526 | } | |
527 | } | |
528 | ||
529 | ||
8a0681f9 VZ |
530 | switch ( tool->GetStyle() ) |
531 | { | |
532 | case wxTOOL_STYLE_BUTTON: | |
38762f09 VZ |
533 | // for a radio button we need the widget which starts the radio |
534 | // group it belongs to, i.e. the first radio button immediately | |
535 | // preceding this one | |
8a0681f9 | 536 | { |
38762f09 VZ |
537 | GtkWidget *widget = NULL; |
538 | ||
539 | if ( tool->IsRadio() ) | |
540 | { | |
98fc1d65 MB |
541 | wxToolBarToolsList::compatibility_iterator node |
542 | = wxToolBarToolsList::compatibility_iterator(); | |
17a1ebd1 VZ |
543 | if ( pos ) |
544 | node = m_tools.Item(pos - 1); | |
222ed1d6 | 545 | |
38762f09 VZ |
546 | while ( node ) |
547 | { | |
17a1ebd1 VZ |
548 | wxToolBarTool *toolNext = (wxToolBarTool *)node->GetData(); |
549 | if ( !toolNext->IsRadio() ) | |
38762f09 VZ |
550 | break; |
551 | ||
17a1ebd1 | 552 | widget = toolNext->m_item; |
38762f09 VZ |
553 | |
554 | node = node->GetPrevious(); | |
555 | } | |
556 | ||
557 | if ( !widget ) | |
558 | { | |
559 | // this is the first button in the radio button group, | |
560 | // it will be toggled automatically by GTK so bring the | |
561 | // internal flag in sync | |
91af0895 | 562 | tool->Toggle(true); |
38762f09 VZ |
563 | } |
564 | } | |
565 | ||
566 | tool->m_item = gtk_toolbar_insert_element | |
567 | ( | |
568 | m_toolbar, | |
569 | tool->GetGtkChildType(), | |
570 | widget, | |
571 | tool->GetLabel().empty() | |
572 | ? NULL | |
fab591c5 | 573 | : (const char*) wxGTK_CONV( tool->GetLabel() ), |
38762f09 VZ |
574 | tool->GetShortHelp().empty() |
575 | ? NULL | |
fab591c5 | 576 | : (const char*) wxGTK_CONV( tool->GetShortHelp() ), |
38762f09 | 577 | "", // tooltip_private_text (?) |
eba91e51 | 578 | tool->m_image, |
38762f09 VZ |
579 | (GtkSignalFunc)gtk_toolbar_callback, |
580 | (gpointer)tool, | |
6a1359c0 | 581 | posGtk |
38762f09 VZ |
582 | ); |
583 | ||
eba91e51 | 584 | wxCHECK_MSG(tool->m_item != NULL, false, _T("gtk_toolbar_insert_element() failed")); |
99e8cb50 | 585 | |
9fa72bd2 MR |
586 | g_signal_connect (tool->m_item, "enter_notify_event", |
587 | G_CALLBACK (gtk_toolbar_tool_callback), | |
588 | tool); | |
589 | g_signal_connect (tool->m_item, "leave_notify_event", | |
590 | G_CALLBACK (gtk_toolbar_tool_callback), | |
591 | tool); | |
729b4756 RR |
592 | g_signal_connect(tool->m_item, "button-press-event", |
593 | G_CALLBACK (gtk_toolbar_tool_rclick_callback), | |
594 | tool); | |
fc6557a6 RR |
595 | |
596 | if (tool->GetKind() == wxITEM_DROPDOWN) | |
597 | { | |
598 | GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data( arrow_down_xpm ); | |
599 | GtkWidget *dropdown = gtk_toggle_button_new(); | |
600 | GtkWidget *image = gtk_image_new_from_pixbuf( pixbuf ); | |
601 | gtk_widget_show( image ); | |
602 | gtk_container_add( GTK_CONTAINER(dropdown), image ); | |
603 | ||
604 | if (GetWindowStyle() & wxTB_FLAT) | |
605 | gtk_button_set_relief( GTK_BUTTON(dropdown), GTK_RELIEF_NONE ); | |
606 | GTK_WIDGET_UNSET_FLAGS (dropdown, GTK_CAN_FOCUS); | |
607 | gtk_widget_show( dropdown ); | |
608 | ||
609 | g_signal_connect (dropdown, "enter_notify_event", | |
610 | G_CALLBACK (gtk_toolbar_buddy_enter_callback), | |
611 | tool->m_item); | |
612 | g_signal_connect (dropdown, "leave_notify_event", | |
613 | G_CALLBACK (gtk_toolbar_buddy_leave_callback), | |
614 | tool->m_item); | |
615 | g_signal_connect(dropdown, "button-press-event", | |
616 | G_CALLBACK (gtk_toolbar_dropdown_lclick_callback), | |
617 | tool); | |
618 | ||
619 | GtkRequisition req; | |
620 | (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(tool->m_item) )->size_request ) | |
621 | (tool->m_item, &req ); | |
622 | gtk_widget_set_size_request( dropdown, -1, req.height ); | |
623 | ||
624 | gtk_toolbar_insert_widget( | |
625 | m_toolbar, | |
626 | dropdown, | |
627 | (const char *) NULL, | |
628 | (const char *) NULL, | |
629 | posGtk+1 | |
630 | ); | |
631 | } | |
8a0681f9 | 632 | } |
8a0681f9 VZ |
633 | break; |
634 | ||
635 | case wxTOOL_STYLE_SEPARATOR: | |
6a1359c0 | 636 | gtk_toolbar_insert_space( m_toolbar, posGtk ); |
8a0681f9 VZ |
637 | |
638 | // skip the rest | |
91af0895 | 639 | return true; |
bf9e3e73 | 640 | |
8a0681f9 VZ |
641 | case wxTOOL_STYLE_CONTROL: |
642 | gtk_toolbar_insert_widget( | |
643 | m_toolbar, | |
644 | tool->GetControl()->m_widget, | |
645 | (const char *) NULL, | |
646 | (const char *) NULL, | |
6a1359c0 | 647 | posGtk |
8a0681f9 | 648 | ); |
f4322df6 | 649 | |
86b65467 RR |
650 | // connect after in order to correct size_allocate events |
651 | g_signal_connect_after (tool->GetControl()->m_widget, "size_allocate", | |
652 | G_CALLBACK (gtktoolwidget_size_callback), tool->GetControl()); | |
f4322df6 | 653 | |
8a0681f9 VZ |
654 | break; |
655 | } | |
bf9e3e73 | 656 | |
bf9e3e73 | 657 | GtkRequisition req; |
2afa14f2 OK |
658 | (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request ) |
659 | (m_widget, &req ); | |
00655497 | 660 | m_width = req.width + m_xMargin; |
6f67eafe | 661 | m_height = req.height + 2*m_yMargin; |
9f884528 | 662 | InvalidateBestSize(); |
bf9e3e73 | 663 | |
91af0895 | 664 | return true; |
bf9e3e73 RR |
665 | } |
666 | ||
4a64a89c | 667 | bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *toolBase) |
c801d85f | 668 | { |
8c4e2405 | 669 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase); |
c801d85f | 670 | |
8a0681f9 | 671 | switch ( tool->GetStyle() ) |
97d7bfb8 | 672 | { |
8a0681f9 VZ |
673 | case wxTOOL_STYLE_CONTROL: |
674 | tool->GetControl()->Destroy(); | |
675 | break; | |
97d7bfb8 | 676 | |
8a0681f9 VZ |
677 | case wxTOOL_STYLE_BUTTON: |
678 | gtk_widget_destroy( tool->m_item ); | |
679 | break; | |
97d7bfb8 | 680 | |
4a64a89c RD |
681 | case wxTOOL_STYLE_SEPARATOR: |
682 | gtk_toolbar_remove_space( m_toolbar, pos ); | |
683 | break; | |
8a0681f9 | 684 | } |
c801d85f | 685 | |
9f884528 | 686 | InvalidateBestSize(); |
91af0895 | 687 | return true; |
fc008f25 | 688 | } |
46dc76ba | 689 | |
8a0681f9 VZ |
690 | // ---------------------------------------------------------------------------- |
691 | // wxToolBar tools state | |
692 | // ---------------------------------------------------------------------------- | |
693 | ||
694 | void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable) | |
c801d85f | 695 | { |
8c4e2405 | 696 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase); |
8a0681f9 | 697 | |
8a0681f9 | 698 | if (tool->m_item) |
fab591c5 | 699 | { |
8a0681f9 | 700 | gtk_widget_set_sensitive( tool->m_item, enable ); |
fab591c5 | 701 | } |
fc008f25 | 702 | } |
c801d85f | 703 | |
248bcf0a | 704 | void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle ) |
c801d85f | 705 | { |
8c4e2405 | 706 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase); |
8a0681f9 VZ |
707 | |
708 | GtkWidget *item = tool->m_item; | |
709 | if ( item && GTK_IS_TOGGLE_BUTTON(item) ) | |
1144d24d | 710 | { |
eba91e51 | 711 | tool->SetImage(tool->GetBitmap()); |
c801d85f | 712 | |
91af0895 | 713 | m_blockEvent = true; |
8a0681f9 | 714 | |
e343da37 | 715 | gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(item), toggle ); |
248bcf0a | 716 | |
91af0895 | 717 | m_blockEvent = false; |
1144d24d | 718 | } |
fc008f25 | 719 | } |
c801d85f | 720 | |
8a0681f9 VZ |
721 | void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool), |
722 | bool WXUNUSED(toggle)) | |
c801d85f | 723 | { |
8a0681f9 VZ |
724 | // VZ: absolutely no idea about how to do it |
725 | wxFAIL_MSG( _T("not implemented") ); | |
fc008f25 | 726 | } |
c801d85f | 727 | |
8a0681f9 VZ |
728 | // ---------------------------------------------------------------------------- |
729 | // wxToolBar geometry | |
730 | // ---------------------------------------------------------------------------- | |
731 | ||
732 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x), | |
733 | wxCoord WXUNUSED(y)) const | |
c801d85f | 734 | { |
8a0681f9 VZ |
735 | // VZ: GTK+ doesn't seem to have such thing |
736 | wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") ); | |
737 | ||
738 | return (wxToolBarToolBase *)NULL; | |
fc008f25 | 739 | } |
c801d85f | 740 | |
1144d24d | 741 | void wxToolBar::SetMargins( int x, int y ) |
c801d85f | 742 | { |
8a0681f9 VZ |
743 | wxCHECK_RET( GetToolsCount() == 0, |
744 | wxT("wxToolBar::SetMargins must be called before adding tools.") ); | |
248bcf0a | 745 | |
1144d24d RR |
746 | m_xMargin = x; |
747 | m_yMargin = y; | |
fc008f25 | 748 | } |
c801d85f | 749 | |
cf4219e7 | 750 | void wxToolBar::SetToolSeparation( int separation ) |
c801d85f | 751 | { |
9e691f46 | 752 | // FIXME: this function disappeared |
68567a96 | 753 | #if 0 |
1144d24d | 754 | gtk_toolbar_set_space_size( m_toolbar, separation ); |
9e691f46 VZ |
755 | #endif |
756 | ||
8a0681f9 | 757 | m_toolSeparation = separation; |
1144d24d RR |
758 | } |
759 | ||
a1f79c1e VZ |
760 | void wxToolBar::SetToolShortHelp( int id, const wxString& helpString ) |
761 | { | |
8c4e2405 | 762 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id)); |
a1f79c1e VZ |
763 | |
764 | if ( tool ) | |
765 | { | |
766 | (void)tool->SetShortHelp(helpString); | |
767 | gtk_tooltips_set_tip(m_toolbar->tooltips, tool->m_item, | |
fab591c5 | 768 | wxGTK_CONV( helpString ), ""); |
a1f79c1e VZ |
769 | } |
770 | } | |
771 | ||
bbd321ff RD |
772 | void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap ) |
773 | { | |
774 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id)); | |
775 | if ( tool ) | |
776 | { | |
777 | wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); | |
778 | ||
779 | tool->SetNormalBitmap(bitmap); | |
780 | tool->SetImage(tool->GetBitmap()); | |
f4322df6 | 781 | } |
bbd321ff RD |
782 | } |
783 | ||
784 | void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap ) | |
785 | { | |
786 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id)); | |
787 | if ( tool ) | |
788 | { | |
789 | wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); | |
790 | ||
791 | tool->SetDisabledBitmap(bitmap); | |
792 | tool->SetImage(tool->GetBitmap()); | |
f4322df6 | 793 | } |
bbd321ff RD |
794 | } |
795 | ||
8a0681f9 VZ |
796 | // ---------------------------------------------------------------------------- |
797 | // wxToolBar idle handling | |
798 | // ---------------------------------------------------------------------------- | |
1144d24d | 799 | |
9b7e522a RR |
800 | void wxToolBar::OnInternalIdle() |
801 | { | |
1417c811 RR |
802 | // Check if we have to show window now |
803 | if (GtkShowFromOnIdle()) return; | |
f4322df6 | 804 | |
9b7e522a RR |
805 | wxCursor cursor = m_cursor; |
806 | if (g_globalCursor.Ok()) cursor = g_globalCursor; | |
807 | ||
f7a11f8c | 808 | if (cursor.Ok()) |
9b7e522a | 809 | { |
f7a11f8c | 810 | /* I now set the cursor the anew in every OnInternalIdle call |
8a0681f9 VZ |
811 | as setting the cursor in a parent window also effects the |
812 | windows above so that checking for the current cursor is | |
813 | not possible. */ | |
85ec2f26 RR |
814 | |
815 | if (HasFlag(wxTB_DOCKABLE) && (m_widget->window)) | |
9b7e522a | 816 | { |
8a0681f9 VZ |
817 | /* if the toolbar is dockable, then m_widget stands for the |
818 | GtkHandleBox widget, which uses its own window so that we | |
819 | can set the cursor for it. if the toolbar is not dockable, | |
820 | m_widget comes from m_toolbar which uses its parent's | |
821 | window ("windowless windows") and thus we cannot set the | |
822 | cursor. */ | |
823 | gdk_window_set_cursor( m_widget->window, cursor.GetCursor() ); | |
824 | } | |
825 | ||
222ed1d6 | 826 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
8a0681f9 VZ |
827 | while ( node ) |
828 | { | |
829 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData(); | |
830 | node = node->GetNext(); | |
831 | ||
832 | GtkWidget *item = tool->m_item; | |
833 | if ( item ) | |
834 | { | |
835 | GdkWindow *window = item->window; | |
836 | ||
837 | if ( window ) | |
838 | { | |
839 | gdk_window_set_cursor( window, cursor.GetCursor() ); | |
840 | } | |
841 | } | |
9b7e522a RR |
842 | } |
843 | } | |
844 | ||
e39af974 JS |
845 | if (wxUpdateUIEvent::CanUpdate(this)) |
846 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); | |
9b7e522a RR |
847 | } |
848 | ||
9d522606 RD |
849 | |
850 | // ---------------------------------------------------------------------------- | |
851 | ||
852 | // static | |
853 | wxVisualAttributes | |
854 | wxToolBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
855 | { | |
9d522606 | 856 | return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new); |
9d522606 RD |
857 | } |
858 | ||
a1f79c1e | 859 | #endif // wxUSE_TOOLBAR_NATIVE |