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