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