]>
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 | ||
14f355c2 VS |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
8a0681f9 | 14 | #if wxUSE_TOOLBAR_NATIVE |
dcf924a3 | 15 | |
4e3e485b WS |
16 | #include "wx/toolbar.h" |
17 | ||
9e691f46 | 18 | #include "wx/gtk/private.h" |
fc6557a6 | 19 | |
8a0681f9 VZ |
20 | // ---------------------------------------------------------------------------- |
21 | // globals | |
22 | // ---------------------------------------------------------------------------- | |
acfd422a | 23 | |
314055fa | 24 | // data |
9b7e522a RR |
25 | extern bool g_blockEventsOnDrag; |
26 | extern wxCursor g_globalCursor; | |
314055fa | 27 | |
8a0681f9 VZ |
28 | // ---------------------------------------------------------------------------- |
29 | // wxToolBarTool | |
30 | // ---------------------------------------------------------------------------- | |
31 | ||
32 | class wxToolBarTool : public wxToolBarToolBase | |
33 | { | |
34 | public: | |
35 | wxToolBarTool(wxToolBar *tbar, | |
36 | int id, | |
e76c0b5f | 37 | const wxString& label, |
8a0681f9 VZ |
38 | const wxBitmap& bitmap1, |
39 | const wxBitmap& bitmap2, | |
e76c0b5f | 40 | wxItemKind kind, |
8a0681f9 VZ |
41 | wxObject *clientData, |
42 | const wxString& shortHelpString, | |
43 | const wxString& longHelpString) | |
e76c0b5f | 44 | : wxToolBarToolBase(tbar, id, label, bitmap1, bitmap2, kind, |
8a0681f9 VZ |
45 | clientData, shortHelpString, longHelpString) |
46 | { | |
a1cb0b11 | 47 | m_item = NULL; |
8a0681f9 VZ |
48 | } |
49 | ||
07d02e9e VZ |
50 | wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label) |
51 | : wxToolBarToolBase(tbar, control, label) | |
8a0681f9 | 52 | { |
a1cb0b11 | 53 | m_item = NULL; |
ddb57e45 PC |
54 | // Hold a reference to keep control alive until DoInsertTool() is |
55 | // called, or if RemoveTool() is called (see DoDeleteTool) | |
56 | g_object_ref(control->m_widget); | |
57 | // release reference when gtk_widget_destroy() is called on control | |
58 | g_signal_connect( | |
59 | control->m_widget, "destroy", G_CALLBACK(g_object_unref), NULL); | |
1be45608 VZ |
60 | } |
61 | ||
a1cb0b11 PC |
62 | void SetImage(); |
63 | void CreateDropDown(); | |
64 | void ShowDropdown(GtkToggleButton* button); | |
8a0681f9 | 65 | |
a1cb0b11 | 66 | GtkToolItem* m_item; |
8a0681f9 VZ |
67 | }; |
68 | ||
69 | // ---------------------------------------------------------------------------- | |
70 | // wxWin macros | |
71 | // ---------------------------------------------------------------------------- | |
72 | ||
2eb10e2a | 73 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) |
8a0681f9 VZ |
74 | |
75 | // ============================================================================ | |
76 | // implementation | |
77 | // ============================================================================ | |
78 | ||
c801d85f | 79 | //----------------------------------------------------------------------------- |
a1cb0b11 | 80 | // "clicked" from m_item |
c801d85f KB |
81 | //----------------------------------------------------------------------------- |
82 | ||
865bb325 | 83 | extern "C" { |
a1cb0b11 | 84 | static void item_clicked(GtkToolButton*, wxToolBarTool* tool) |
c801d85f | 85 | { |
1144d24d | 86 | if (g_blockEventsOnDrag) return; |
a3622daa | 87 | |
a1cb0b11 PC |
88 | tool->GetToolBar()->OnLeftClick(tool->GetId(), false); |
89 | } | |
90 | } | |
7062497f | 91 | |
a1cb0b11 PC |
92 | //----------------------------------------------------------------------------- |
93 | // "toggled" from m_item | |
94 | //----------------------------------------------------------------------------- | |
8a0681f9 | 95 | |
a1cb0b11 PC |
96 | extern "C" { |
97 | static void item_toggled(GtkToggleToolButton* button, wxToolBarTool* tool) | |
98 | { | |
99 | if (g_blockEventsOnDrag) return; | |
38762f09 | 100 | |
a1cb0b11 PC |
101 | const bool active = gtk_toggle_tool_button_get_active(button) != 0; |
102 | tool->Toggle(active); | |
103 | if (!active && tool->GetKind() == wxITEM_RADIO) | |
104 | return; | |
a3622daa | 105 | |
a1cb0b11 | 106 | if (!tool->GetToolBar()->OnLeftClick(tool->GetId(), active)) |
6bb7cee4 VZ |
107 | { |
108 | // revert back | |
109 | tool->Toggle(); | |
6bb7cee4 | 110 | } |
fc008f25 | 111 | } |
865bb325 | 112 | } |
c801d85f | 113 | |
729b4756 | 114 | //----------------------------------------------------------------------------- |
a1cb0b11 | 115 | // "button_press_event" from m_item child |
729b4756 | 116 | //----------------------------------------------------------------------------- |
a1cb0b11 | 117 | |
729b4756 | 118 | extern "C" { |
a1cb0b11 PC |
119 | static gboolean |
120 | button_press_event(GtkWidget*, GdkEventButton* event, wxToolBarTool* tool) | |
729b4756 RR |
121 | { |
122 | if (event->button != 3) | |
123 | return FALSE; | |
124 | ||
729b4756 | 125 | if (g_blockEventsOnDrag) return TRUE; |
729b4756 | 126 | |
a1cb0b11 PC |
127 | tool->GetToolBar()->OnRightClick( |
128 | tool->GetId(), int(event->x), int(event->y)); | |
729b4756 RR |
129 | |
130 | return TRUE; | |
131 | } | |
132 | } | |
133 | ||
fc6557a6 | 134 | //----------------------------------------------------------------------------- |
a1cb0b11 | 135 | // "child_detached" from m_widget |
fc6557a6 RR |
136 | //----------------------------------------------------------------------------- |
137 | ||
138 | extern "C" { | |
a1cb0b11 | 139 | static void child_detached(GtkWidget*, GtkToolbar* toolbar, void*) |
fc6557a6 | 140 | { |
a1cb0b11 PC |
141 | // disable showing overflow arrow when toolbar is detached, |
142 | // otherwise toolbar collapses to just an arrow | |
143 | gtk_toolbar_set_show_arrow(toolbar, false); | |
fc6557a6 RR |
144 | } |
145 | } | |
146 | ||
147 | //----------------------------------------------------------------------------- | |
a1cb0b11 | 148 | // "child_attached" from m_widget |
fc6557a6 RR |
149 | //----------------------------------------------------------------------------- |
150 | ||
a1cb0b11 PC |
151 | extern "C" { |
152 | static void child_attached(GtkWidget*, GtkToolbar* toolbar, void*) | |
fc6557a6 | 153 | { |
a1cb0b11 | 154 | gtk_toolbar_set_show_arrow(toolbar, true); |
fc6557a6 RR |
155 | } |
156 | } | |
157 | ||
2f2aa628 | 158 | //----------------------------------------------------------------------------- |
a1cb0b11 | 159 | // "enter_notify_event" / "leave_notify_event" from m_item |
2f2aa628 RR |
160 | //----------------------------------------------------------------------------- |
161 | ||
865bb325 | 162 | extern "C" { |
a1cb0b11 PC |
163 | static gboolean |
164 | enter_notify_event(GtkWidget*, GdkEventCrossing* event, wxToolBarTool* tool) | |
314055fa | 165 | { |
1144d24d | 166 | if (g_blockEventsOnDrag) return TRUE; |
248bcf0a | 167 | |
a1cb0b11 PC |
168 | int id = -1; |
169 | if (event->type == GDK_ENTER_NOTIFY) | |
170 | id = tool->GetId(); | |
171 | tool->GetToolBar()->OnMouseEnter(id); | |
248bcf0a | 172 | |
1144d24d | 173 | return FALSE; |
314055fa | 174 | } |
865bb325 | 175 | } |
314055fa | 176 | |
cca410b3 PC |
177 | //----------------------------------------------------------------------------- |
178 | // "size_request" from m_toolbar | |
179 | //----------------------------------------------------------------------------- | |
180 | ||
181 | extern "C" { | |
182 | static void | |
183 | size_request(GtkWidget*, GtkRequisition* req, wxToolBar* win) | |
184 | { | |
185 | const wxSize margins = win->GetMargins(); | |
186 | req->width += margins.x; | |
187 | req->height += 2 * margins.y; | |
188 | } | |
189 | } | |
190 | ||
a1cb0b11 PC |
191 | //----------------------------------------------------------------------------- |
192 | // "expose_event" from GtkImage inside m_item | |
193 | //----------------------------------------------------------------------------- | |
194 | ||
195 | extern "C" { | |
196 | static gboolean | |
197 | image_expose_event(GtkWidget* widget, GdkEventExpose*, wxToolBarTool* tool) | |
198 | { | |
199 | const wxBitmap& bitmap = tool->GetDisabledBitmap(); | |
200 | if (tool->IsEnabled() || !bitmap.IsOk()) | |
201 | return false; | |
202 | ||
203 | // draw disabled bitmap ourselves, GtkImage has no way to specify it | |
204 | const GtkAllocation& alloc = widget->allocation; | |
205 | gdk_draw_pixbuf( | |
206 | widget->window, widget->style->black_gc, bitmap.GetPixbuf(), | |
207 | 0, 0, | |
208 | alloc.x + (alloc.width - widget->requisition.width) / 2, | |
209 | alloc.y + (alloc.height - widget->requisition.height) / 2, | |
210 | -1, -1, GDK_RGB_DITHER_NORMAL, 0, 0); | |
211 | return true; | |
212 | } | |
213 | } | |
214 | ||
215 | //----------------------------------------------------------------------------- | |
216 | // "toggled" from dropdown menu button | |
217 | //----------------------------------------------------------------------------- | |
218 | ||
219 | extern "C" { | |
220 | static void arrow_toggled(GtkToggleButton* button, wxToolBarTool* tool) | |
221 | { | |
222 | if (gtk_toggle_button_get_active(button)) | |
223 | { | |
224 | tool->ShowDropdown(button); | |
225 | gtk_toggle_button_set_active(button, false); | |
226 | } | |
227 | } | |
228 | } | |
229 | ||
230 | //----------------------------------------------------------------------------- | |
231 | // "button_press_event" from dropdown menu button | |
232 | //----------------------------------------------------------------------------- | |
233 | ||
234 | extern "C" { | |
235 | static gboolean | |
236 | arrow_button_press_event(GtkToggleButton* button, GdkEventButton* event, wxToolBarTool* tool) | |
237 | { | |
238 | if (event->button == 1) | |
239 | { | |
240 | g_signal_handlers_block_by_func(button, (void*)arrow_toggled, tool); | |
241 | gtk_toggle_button_set_active(button, true); | |
242 | tool->ShowDropdown(button); | |
243 | gtk_toggle_button_set_active(button, false); | |
244 | g_signal_handlers_unblock_by_func(button, (void*)arrow_toggled, tool); | |
245 | return true; | |
246 | } | |
247 | return false; | |
248 | } | |
249 | } | |
250 | ||
bf9e3e73 RR |
251 | //----------------------------------------------------------------------------- |
252 | // InsertChild callback for wxToolBar | |
253 | //----------------------------------------------------------------------------- | |
254 | ||
205177b0 | 255 | static void wxInsertChildInToolBar(wxWindow* parent, wxWindow* child) |
bf9e3e73 | 256 | { |
205177b0 PC |
257 | GtkWidget* align = gtk_alignment_new(0.5, 0.5, 0, 0); |
258 | gtk_widget_show(align); | |
259 | gtk_container_add(GTK_CONTAINER(align), child->m_widget); | |
260 | GtkToolItem* item = gtk_tool_item_new(); | |
261 | gtk_container_add(GTK_CONTAINER(item), align); | |
262 | wxToolBar* tbar = static_cast<wxToolBar*>(parent); | |
263 | // position will be corrected in DoInsertTool if necessary | |
264 | gtk_toolbar_insert(GTK_TOOLBAR(GTK_BIN(tbar->m_widget)->child), item, -1); | |
bf9e3e73 RR |
265 | } |
266 | ||
8a0681f9 VZ |
267 | // ---------------------------------------------------------------------------- |
268 | // wxToolBarTool | |
269 | // ---------------------------------------------------------------------------- | |
c801d85f | 270 | |
a1cb0b11 | 271 | void wxToolBarTool::SetImage() |
8a0681f9 | 272 | { |
a1cb0b11 PC |
273 | const wxBitmap& bitmap = GetNormalBitmap(); |
274 | wxCHECK_RET(bitmap.IsOk(), "invalid bitmap for wxToolBar icon"); | |
275 | ||
276 | GtkWidget* image = gtk_tool_button_get_icon_widget(GTK_TOOL_BUTTON(m_item)); | |
277 | // always use pixbuf, because pixmap mask does not | |
278 | // work with disabled images in some themes | |
279 | gtk_image_set_from_pixbuf(GTK_IMAGE(image), bitmap.GetPixbuf()); | |
280 | } | |
281 | ||
282 | // helper to create a dropdown menu item | |
283 | void wxToolBarTool::CreateDropDown() | |
284 | { | |
285 | gtk_tool_item_set_homogeneous(m_item, false); | |
286 | GtkWidget* box; | |
287 | GtkWidget* arrow; | |
288 | if (GetToolBar()->HasFlag(wxTB_LEFT | wxTB_RIGHT)) | |
289 | { | |
290 | box = gtk_vbox_new(false, 0); | |
291 | arrow = gtk_arrow_new(GTK_ARROW_RIGHT, GTK_SHADOW_NONE); | |
292 | } | |
293 | else | |
294 | { | |
295 | box = gtk_hbox_new(false, 0); | |
296 | arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE); | |
297 | } | |
298 | GtkWidget* tool_button = GTK_BIN(m_item)->child; | |
299 | gtk_widget_reparent(tool_button, box); | |
300 | GtkWidget* arrow_button = gtk_toggle_button_new(); | |
301 | gtk_button_set_relief(GTK_BUTTON(arrow_button), | |
302 | gtk_tool_item_get_relief_style(GTK_TOOL_ITEM(m_item))); | |
303 | gtk_container_add(GTK_CONTAINER(arrow_button), arrow); | |
304 | gtk_container_add(GTK_CONTAINER(box), arrow_button); | |
305 | gtk_widget_show_all(box); | |
306 | gtk_container_add(GTK_CONTAINER(m_item), box); | |
307 | ||
308 | g_signal_connect(arrow_button, "toggled", G_CALLBACK(arrow_toggled), this); | |
309 | g_signal_connect(arrow_button, "button_press_event", | |
310 | G_CALLBACK(arrow_button_press_event), this); | |
311 | } | |
312 | ||
313 | void wxToolBarTool::ShowDropdown(GtkToggleButton* button) | |
314 | { | |
315 | wxToolBarBase* toolbar = GetToolBar(); | |
316 | wxCommandEvent event(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED, GetId()); | |
317 | if (!toolbar->HandleWindowEvent(event)) | |
318 | { | |
319 | wxMenu* menu = GetDropdownMenu(); | |
320 | if (menu) | |
321 | { | |
322 | const GtkAllocation& alloc = GTK_WIDGET(button)->allocation; | |
323 | int x = alloc.x; | |
324 | int y = alloc.y; | |
325 | if (toolbar->HasFlag(wxTB_LEFT | wxTB_RIGHT)) | |
326 | x += alloc.width; | |
327 | else | |
328 | y += alloc.height; | |
329 | toolbar->PopupMenu(menu, x, y); | |
330 | } | |
331 | } | |
8a0681f9 | 332 | } |
c801d85f | 333 | |
8a0681f9 | 334 | wxToolBarToolBase *wxToolBar::CreateTool(int id, |
e76c0b5f | 335 | const wxString& text, |
8a0681f9 VZ |
336 | const wxBitmap& bitmap1, |
337 | const wxBitmap& bitmap2, | |
e76c0b5f | 338 | wxItemKind kind, |
8a0681f9 VZ |
339 | wxObject *clientData, |
340 | const wxString& shortHelpString, | |
341 | const wxString& longHelpString) | |
342 | { | |
e76c0b5f | 343 | return new wxToolBarTool(this, id, text, bitmap1, bitmap2, kind, |
8a0681f9 VZ |
344 | clientData, shortHelpString, longHelpString); |
345 | } | |
b1da76e1 | 346 | |
07d02e9e VZ |
347 | wxToolBarToolBase * |
348 | wxToolBar::CreateTool(wxControl *control, const wxString& label) | |
c801d85f | 349 | { |
07d02e9e | 350 | return new wxToolBarTool(this, control, label); |
fc008f25 | 351 | } |
c801d85f | 352 | |
8a0681f9 VZ |
353 | //----------------------------------------------------------------------------- |
354 | // wxToolBar construction | |
355 | //----------------------------------------------------------------------------- | |
356 | ||
357 | void wxToolBar::Init() | |
c801d85f | 358 | { |
8a0681f9 | 359 | m_toolbar = (GtkToolbar *)NULL; |
a1cb0b11 | 360 | m_tooltips = NULL; |
fc008f25 | 361 | } |
c801d85f | 362 | |
a3622daa | 363 | wxToolBar::~wxToolBar() |
c801d85f | 364 | { |
a1cb0b11 PC |
365 | if (m_tooltips) |
366 | { | |
367 | gtk_object_destroy(GTK_OBJECT(m_tooltips)); | |
368 | g_object_unref(m_tooltips); | |
369 | } | |
fc008f25 | 370 | } |
c801d85f | 371 | |
8a0681f9 VZ |
372 | bool wxToolBar::Create( wxWindow *parent, |
373 | wxWindowID id, | |
374 | const wxPoint& pos, | |
375 | const wxSize& size, | |
376 | long style, | |
377 | const wxString& name ) | |
c801d85f | 378 | { |
c821db16 | 379 | m_insertCallback = wxInsertChildInToolBar; |
a3622daa | 380 | |
8a0681f9 VZ |
381 | if ( !PreCreation( parent, pos, size ) || |
382 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
4dcaf11a | 383 | { |
223d09f6 | 384 | wxFAIL_MSG( wxT("wxToolBar creation failed") ); |
c801d85f | 385 | |
91af0895 | 386 | return false; |
8a0681f9 | 387 | } |
a3622daa | 388 | |
d408730c VZ |
389 | FixupStyle(); |
390 | ||
9e691f46 | 391 | m_toolbar = GTK_TOOLBAR( gtk_toolbar_new() ); |
a1cb0b11 PC |
392 | m_tooltips = gtk_tooltips_new(); |
393 | g_object_ref(m_tooltips); | |
394 | gtk_object_sink(GTK_OBJECT(m_tooltips)); | |
e76c0b5f | 395 | GtkSetStyle(); |
99e8cb50 | 396 | |
3502e687 RR |
397 | if (style & wxTB_DOCKABLE) |
398 | { | |
399 | m_widget = gtk_handle_box_new(); | |
a1cb0b11 PC |
400 | |
401 | g_signal_connect(m_widget, "child_detached", | |
402 | G_CALLBACK(child_detached), NULL); | |
403 | g_signal_connect(m_widget, "child_attached", | |
404 | G_CALLBACK(child_attached), NULL); | |
8a0681f9 | 405 | |
f03fc89f | 406 | if (style & wxTB_FLAT) |
858b5bdd | 407 | gtk_handle_box_set_shadow_type( GTK_HANDLE_BOX(m_widget), GTK_SHADOW_NONE ); |
3502e687 RR |
408 | } |
409 | else | |
248bcf0a RD |
410 | { |
411 | m_widget = gtk_event_box_new(); | |
248bcf0a | 412 | ConnectWidget( m_widget ); |
3502e687 | 413 | } |
a1cb0b11 PC |
414 | gtk_container_add(GTK_CONTAINER(m_widget), GTK_WIDGET(m_toolbar)); |
415 | gtk_widget_show(GTK_WIDGET(m_toolbar)); | |
be25e480 | 416 | |
f03fc89f | 417 | m_parent->DoAddChild( this ); |
8a0681f9 | 418 | |
abdeb9e7 | 419 | PostCreation(size); |
a3622daa | 420 | |
cca410b3 PC |
421 | g_signal_connect_after(m_toolbar, "size_request", |
422 | G_CALLBACK(size_request), this); | |
423 | ||
91af0895 | 424 | return true; |
fc008f25 | 425 | } |
c801d85f | 426 | |
e4161a2a | 427 | GdkWindow *wxToolBar::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const |
48468900 RR |
428 | { |
429 | return GTK_WIDGET(m_toolbar)->window; | |
430 | } | |
431 | ||
e76c0b5f VZ |
432 | void wxToolBar::GtkSetStyle() |
433 | { | |
a1cb0b11 PC |
434 | GtkOrientation orient = GTK_ORIENTATION_HORIZONTAL; |
435 | if (HasFlag(wxTB_LEFT | wxTB_RIGHT)) | |
436 | orient = GTK_ORIENTATION_VERTICAL; | |
437 | ||
438 | GtkToolbarStyle style = GTK_TOOLBAR_ICONS; | |
439 | if (HasFlag(wxTB_NOICONS)) | |
440 | style = GTK_TOOLBAR_TEXT; | |
441 | else if (HasFlag(wxTB_TEXT)) | |
442 | { | |
443 | style = GTK_TOOLBAR_BOTH; | |
444 | if (HasFlag(wxTB_HORZ_LAYOUT)) | |
445 | style = GTK_TOOLBAR_BOTH_HORIZ; | |
446 | } | |
e76c0b5f VZ |
447 | |
448 | gtk_toolbar_set_orientation(m_toolbar, orient); | |
449 | gtk_toolbar_set_style(m_toolbar, style); | |
450 | } | |
451 | ||
452 | void wxToolBar::SetWindowStyleFlag( long style ) | |
453 | { | |
454 | wxToolBarBase::SetWindowStyleFlag(style); | |
8ad31f9d | 455 | |
e76c0b5f VZ |
456 | if ( m_toolbar ) |
457 | GtkSetStyle(); | |
458 | } | |
459 | ||
8a0681f9 | 460 | bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase) |
c801d85f | 461 | { |
8c4e2405 | 462 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase); |
248bcf0a | 463 | |
a1cb0b11 | 464 | GSList* radioGroup; |
8a0681f9 VZ |
465 | switch ( tool->GetStyle() ) |
466 | { | |
467 | case wxTOOL_STYLE_BUTTON: | |
a1cb0b11 | 468 | switch (tool->GetKind()) |
8a0681f9 | 469 | { |
a1cb0b11 PC |
470 | case wxITEM_CHECK: |
471 | tool->m_item = gtk_toggle_tool_button_new(); | |
472 | g_signal_connect(tool->m_item, "toggled", | |
473 | G_CALLBACK(item_toggled), tool); | |
474 | break; | |
475 | case wxITEM_RADIO: | |
476 | radioGroup = GetRadioGroup(pos); | |
477 | if (radioGroup) | |
38762f09 VZ |
478 | { |
479 | // this is the first button in the radio button group, | |
480 | // it will be toggled automatically by GTK so bring the | |
481 | // internal flag in sync | |
91af0895 | 482 | tool->Toggle(true); |
38762f09 | 483 | } |
a1cb0b11 PC |
484 | tool->m_item = gtk_radio_tool_button_new(radioGroup); |
485 | g_signal_connect(tool->m_item, "toggled", | |
486 | G_CALLBACK(item_toggled), tool); | |
487 | break; | |
488 | default: | |
489 | wxFAIL_MSG("unknown toolbar child type"); | |
490 | // fall through | |
491 | case wxITEM_DROPDOWN: | |
492 | case wxITEM_NORMAL: | |
493 | tool->m_item = gtk_tool_button_new(NULL, ""); | |
494 | g_signal_connect(tool->m_item, "clicked", | |
495 | G_CALLBACK(item_clicked), tool); | |
496 | break; | |
497 | } | |
498 | if (!HasFlag(wxTB_NOICONS)) | |
499 | { | |
500 | GtkWidget* image = gtk_image_new(); | |
501 | gtk_tool_button_set_icon_widget( | |
502 | GTK_TOOL_BUTTON(tool->m_item), image); | |
503 | tool->SetImage(); | |
504 | gtk_widget_show(image); | |
505 | g_signal_connect(image, "expose_event", | |
506 | G_CALLBACK(image_expose_event), tool); | |
8a0681f9 | 507 | } |
a1cb0b11 PC |
508 | if (!tool->GetLabel().empty()) |
509 | { | |
510 | gtk_tool_button_set_label( | |
511 | GTK_TOOL_BUTTON(tool->m_item), wxGTK_CONV(tool->GetLabel())); | |
512 | // needed for labels in horizontal toolbar with wxTB_HORZ_LAYOUT | |
513 | gtk_tool_item_set_is_important(tool->m_item, true); | |
514 | } | |
515 | if (!HasFlag(wxTB_NO_TOOLTIPS) && !tool->GetShortHelp().empty()) | |
516 | { | |
517 | gtk_tool_item_set_tooltip(tool->m_item, | |
518 | m_tooltips, wxGTK_CONV(tool->GetShortHelp()), ""); | |
519 | } | |
520 | g_signal_connect(GTK_BIN(tool->m_item)->child, "button_press_event", | |
521 | G_CALLBACK(button_press_event), tool); | |
522 | g_signal_connect(tool->m_item, "enter_notify_event", | |
523 | G_CALLBACK(enter_notify_event), tool); | |
524 | g_signal_connect(tool->m_item, "leave_notify_event", | |
525 | G_CALLBACK(enter_notify_event), tool); | |
526 | ||
527 | if (tool->GetKind() == wxITEM_DROPDOWN) | |
528 | tool->CreateDropDown(); | |
205177b0 | 529 | gtk_toolbar_insert(m_toolbar, tool->m_item, int(pos)); |
8a0681f9 VZ |
530 | break; |
531 | ||
532 | case wxTOOL_STYLE_SEPARATOR: | |
a1cb0b11 | 533 | tool->m_item = gtk_separator_tool_item_new(); |
205177b0 | 534 | gtk_toolbar_insert(m_toolbar, tool->m_item, int(pos)); |
1be45608 | 535 | break; |
bf9e3e73 | 536 | |
8a0681f9 | 537 | case wxTOOL_STYLE_CONTROL: |
a1cb0b11 | 538 | wxWindow* control = tool->GetControl(); |
205177b0 PC |
539 | if (control->m_widget->parent == NULL) |
540 | wxInsertChildInToolBar(this, control); | |
541 | tool->m_item = GTK_TOOL_ITEM(control->m_widget->parent->parent); | |
542 | if (gtk_toolbar_get_item_index(m_toolbar, tool->m_item) != int(pos)) | |
543 | { | |
544 | g_object_ref(tool->m_item); | |
545 | gtk_container_remove( | |
546 | GTK_CONTAINER(m_toolbar), GTK_WIDGET(tool->m_item)); | |
547 | gtk_toolbar_insert(m_toolbar, tool->m_item, int(pos)); | |
548 | g_object_unref(tool->m_item); | |
549 | } | |
a1cb0b11 PC |
550 | // Inserted items "slide" into place using an animated effect that |
551 | // causes multiple size events on the item. Must set size request | |
552 | // to keep item size from getting permanently set too small by the | |
553 | // first of these size events. | |
554 | const wxSize size = control->GetSize(); | |
555 | gtk_widget_set_size_request(control->m_widget, size.x, size.y); | |
8a0681f9 VZ |
556 | break; |
557 | } | |
a1cb0b11 | 558 | gtk_widget_show(GTK_WIDGET(tool->m_item)); |
bf9e3e73 | 559 | |
9f884528 | 560 | InvalidateBestSize(); |
bf9e3e73 | 561 | |
91af0895 | 562 | return true; |
bf9e3e73 RR |
563 | } |
564 | ||
a1cb0b11 | 565 | bool wxToolBar::DoDeleteTool(size_t /* pos */, wxToolBarToolBase* toolBase) |
c801d85f | 566 | { |
8c4e2405 | 567 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase); |
c801d85f | 568 | |
a1cb0b11 | 569 | if (tool->GetStyle() == wxTOOL_STYLE_CONTROL) |
97d7bfb8 | 570 | { |
a1cb0b11 PC |
571 | // don't destroy the control here as we can be called from |
572 | // RemoveTool() and then we need to keep the control alive; | |
573 | // while if we're called from DeleteTool() the control will | |
574 | // be destroyed when wxToolBarToolBase itself is deleted | |
575 | GtkWidget* widget = tool->GetControl()->m_widget; | |
576 | gtk_container_remove(GTK_CONTAINER(widget->parent), widget); | |
8a0681f9 | 577 | } |
a1cb0b11 PC |
578 | gtk_object_destroy(GTK_OBJECT(tool->m_item)); |
579 | tool->m_item = NULL; | |
c801d85f | 580 | |
9f884528 | 581 | InvalidateBestSize(); |
91af0895 | 582 | return true; |
fc008f25 | 583 | } |
46dc76ba | 584 | |
a1cb0b11 PC |
585 | GSList* wxToolBar::GetRadioGroup(size_t pos) |
586 | { | |
587 | GSList* radioGroup = NULL; | |
588 | GtkToolItem* item = NULL; | |
589 | if (pos > 0) | |
590 | { | |
591 | item = gtk_toolbar_get_nth_item(m_toolbar, int(pos) - 1); | |
592 | if (!GTK_IS_RADIO_TOOL_BUTTON(item)) | |
593 | item = NULL; | |
594 | } | |
595 | if (item == NULL && pos < m_tools.size()) | |
596 | { | |
597 | item = gtk_toolbar_get_nth_item(m_toolbar, int(pos)); | |
598 | if (!GTK_IS_RADIO_TOOL_BUTTON(item)) | |
599 | item = NULL; | |
600 | } | |
601 | if (item) | |
602 | radioGroup = gtk_radio_tool_button_get_group((GtkRadioToolButton*)item); | |
603 | return radioGroup; | |
604 | } | |
605 | ||
8a0681f9 VZ |
606 | // ---------------------------------------------------------------------------- |
607 | // wxToolBar tools state | |
608 | // ---------------------------------------------------------------------------- | |
609 | ||
610 | void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable) | |
c801d85f | 611 | { |
8c4e2405 | 612 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase); |
8a0681f9 | 613 | |
8a0681f9 | 614 | if (tool->m_item) |
a1cb0b11 | 615 | gtk_widget_set_sensitive(GTK_WIDGET(tool->m_item), enable); |
fc008f25 | 616 | } |
c801d85f | 617 | |
248bcf0a | 618 | void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle ) |
c801d85f | 619 | { |
8c4e2405 | 620 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, toolBase); |
8a0681f9 | 621 | |
a1cb0b11 | 622 | if (tool->m_item) |
1144d24d | 623 | { |
a1cb0b11 | 624 | g_signal_handlers_block_by_func(tool->m_item, (void*)item_toggled, tool); |
8a0681f9 | 625 | |
a1cb0b11 PC |
626 | gtk_toggle_tool_button_set_active( |
627 | GTK_TOGGLE_TOOL_BUTTON(tool->m_item), toggle); | |
248bcf0a | 628 | |
a1cb0b11 | 629 | g_signal_handlers_unblock_by_func(tool->m_item, (void*)item_toggled, tool); |
1144d24d | 630 | } |
fc008f25 | 631 | } |
c801d85f | 632 | |
8a0681f9 VZ |
633 | void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool), |
634 | bool WXUNUSED(toggle)) | |
c801d85f | 635 | { |
8a0681f9 VZ |
636 | // VZ: absolutely no idea about how to do it |
637 | wxFAIL_MSG( _T("not implemented") ); | |
fc008f25 | 638 | } |
c801d85f | 639 | |
8a0681f9 VZ |
640 | // ---------------------------------------------------------------------------- |
641 | // wxToolBar geometry | |
642 | // ---------------------------------------------------------------------------- | |
643 | ||
a1cb0b11 PC |
644 | wxSize wxToolBar::DoGetBestSize() const |
645 | { | |
646 | // Unfortunately, if overflow arrow is enabled GtkToolbar only reports size | |
647 | // of arrow. To get the real size, the arrow is temporarily disabled here. | |
648 | // This is gross, since it will cause a queue_resize, and could potentially | |
649 | // lead to an infinite loop. But there seems to be no alternative, short of | |
650 | // disabling the arrow entirely. | |
651 | gtk_toolbar_set_show_arrow(m_toolbar, false); | |
652 | const wxSize size = wxToolBarBase::DoGetBestSize(); | |
653 | gtk_toolbar_set_show_arrow(m_toolbar, true); | |
654 | return size; | |
655 | } | |
656 | ||
8a0681f9 VZ |
657 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x), |
658 | wxCoord WXUNUSED(y)) const | |
c801d85f | 659 | { |
8a0681f9 VZ |
660 | // VZ: GTK+ doesn't seem to have such thing |
661 | wxFAIL_MSG( _T("wxToolBar::FindToolForPosition() not implemented") ); | |
662 | ||
663 | return (wxToolBarToolBase *)NULL; | |
fc008f25 | 664 | } |
c801d85f | 665 | |
a1f79c1e VZ |
666 | void wxToolBar::SetToolShortHelp( int id, const wxString& helpString ) |
667 | { | |
8c4e2405 | 668 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id)); |
a1f79c1e VZ |
669 | |
670 | if ( tool ) | |
671 | { | |
672 | (void)tool->SetShortHelp(helpString); | |
a1cb0b11 PC |
673 | if (tool->m_item) |
674 | { | |
675 | gtk_tool_item_set_tooltip(tool->m_item, | |
676 | m_tooltips, wxGTK_CONV(helpString), ""); | |
677 | } | |
a1f79c1e VZ |
678 | } |
679 | } | |
680 | ||
bbd321ff RD |
681 | void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap ) |
682 | { | |
683 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id)); | |
684 | if ( tool ) | |
685 | { | |
686 | wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); | |
687 | ||
688 | tool->SetNormalBitmap(bitmap); | |
a1cb0b11 | 689 | tool->SetImage(); |
f4322df6 | 690 | } |
bbd321ff RD |
691 | } |
692 | ||
693 | void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap ) | |
694 | { | |
695 | wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id)); | |
696 | if ( tool ) | |
697 | { | |
698 | wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); | |
699 | ||
700 | tool->SetDisabledBitmap(bitmap); | |
f4322df6 | 701 | } |
bbd321ff RD |
702 | } |
703 | ||
8a0681f9 VZ |
704 | // ---------------------------------------------------------------------------- |
705 | // wxToolBar idle handling | |
706 | // ---------------------------------------------------------------------------- | |
1144d24d | 707 | |
9b7e522a RR |
708 | void wxToolBar::OnInternalIdle() |
709 | { | |
1417c811 RR |
710 | // Check if we have to show window now |
711 | if (GtkShowFromOnIdle()) return; | |
f4322df6 | 712 | |
9b7e522a RR |
713 | wxCursor cursor = m_cursor; |
714 | if (g_globalCursor.Ok()) cursor = g_globalCursor; | |
715 | ||
f7a11f8c | 716 | if (cursor.Ok()) |
9b7e522a | 717 | { |
f7a11f8c | 718 | /* I now set the cursor the anew in every OnInternalIdle call |
8a0681f9 VZ |
719 | as setting the cursor in a parent window also effects the |
720 | windows above so that checking for the current cursor is | |
721 | not possible. */ | |
85ec2f26 RR |
722 | |
723 | if (HasFlag(wxTB_DOCKABLE) && (m_widget->window)) | |
9b7e522a | 724 | { |
8a0681f9 VZ |
725 | /* if the toolbar is dockable, then m_widget stands for the |
726 | GtkHandleBox widget, which uses its own window so that we | |
727 | can set the cursor for it. if the toolbar is not dockable, | |
728 | m_widget comes from m_toolbar which uses its parent's | |
729 | window ("windowless windows") and thus we cannot set the | |
730 | cursor. */ | |
731 | gdk_window_set_cursor( m_widget->window, cursor.GetCursor() ); | |
732 | } | |
733 | ||
222ed1d6 | 734 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
8a0681f9 VZ |
735 | while ( node ) |
736 | { | |
737 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData(); | |
738 | node = node->GetNext(); | |
739 | ||
a1cb0b11 | 740 | if (tool->m_item) |
8a0681f9 | 741 | { |
a1cb0b11 | 742 | GdkWindow* window = GTK_WIDGET(tool->m_item)->window; |
8a0681f9 VZ |
743 | |
744 | if ( window ) | |
745 | { | |
746 | gdk_window_set_cursor( window, cursor.GetCursor() ); | |
747 | } | |
748 | } | |
9b7e522a RR |
749 | } |
750 | } | |
751 | ||
ffa51479 | 752 | if (wxUpdateUIEvent::CanUpdate(this) && IsShownOnScreen()) |
e39af974 | 753 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); |
9b7e522a RR |
754 | } |
755 | ||
9d522606 RD |
756 | |
757 | // ---------------------------------------------------------------------------- | |
758 | ||
759 | // static | |
760 | wxVisualAttributes | |
761 | wxToolBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
762 | { | |
9d522606 | 763 | return GetDefaultAttributesFromGTKWidget(gtk_toolbar_new); |
9d522606 RD |
764 | } |
765 | ||
a1f79c1e | 766 | #endif // wxUSE_TOOLBAR_NATIVE |