lib/alexandria/ui/iconview_tooltips.rb in alexandria-book-collection-manager-0.6.9 vs lib/alexandria/ui/iconview_tooltips.rb in alexandria-book-collection-manager-0.7.0

- old
+ new

@@ -1,10 +1,10 @@ # -*- ruby -*- # # Copyright (C) 2007 kksou # Copyright (C) 2008,2009 Cathal Mc Ginley -# Copyright (C) 2011, 2014 Matijs van Zuijlen +# Copyright (C) 2011, 2014, 2016 Matijs van Zuijlen # # Alexandria is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. @@ -31,136 +31,49 @@ class IconViewTooltips include Alexandria::Logging def initialize(view) - @tooltip_window = Gtk::Window.new(Gtk::Window::POPUP) - @tooltip_window.name = 'gtk-tooltips' - @tooltip_window.resizable = false - @tooltip_window.border_width = 4 - @tooltip_window.app_paintable = true - - @tooltip_window.signal_connect('expose_event') { |window, event| - on_expose(window, event) - } - - @tooltip_window.signal_connect('leave_notify_event') { |vw, event| - on_leave(vw, event) - } - - @label = Gtk::Label.new('') - @label.wrap = true - @label.set_alignment(0.5, 0.5) - @label.use_markup = true - @label.show - - @tooltip_window.add(@label) set_view(view) end def set_view(view) - view.signal_connect('motion_notify_event') { |vw, event| - on_motion(vw, event) - } - view.signal_connect('leave_notify_event') { |vw, event| - on_leave(vw, event) - } - end + view.has_tooltip = true + view.signal_connect('query-tooltip') do |_widget, x, y, _keyboard_mode, tooltip| + tree_path = view.get_path_at_pos(x, y) + if tree_path + iter = view.model.get_iter(tree_path) - def on_expose(window, _event) - # this paints a nice outline around the label - size = window.size_request - window.style.paint_flat_box(window.window, - Gtk::STATE_NORMAL, - Gtk::SHADOW_OUT, - nil, - @tooltip_window, - 'tooltip', - 0, 0, size[0], size[1]) - # must return nil so the label contents get drawn correctly - nil + title = iter[2] # HACK: hardcoded, should use column names... + authors = iter[4] + publisher = iter[6] + year = iter[7] + tooltip.set_markup label_for_book(title, authors, publisher, year) + end + end end def label_for_book(title, authors, publisher, year) # This is much too complex... but it works for now! html = '' - if title.size > 0 + unless title.empty? html += "<b>#{CGI.escapeHTML(title)}</b>" - if authors.size > 0 - html += "\n" - end + html += "\n" unless authors.empty? end - if authors.size > 0 + unless authors.empty? html += "<i>#{CGI.escapeHTML(authors)}</i>" end - if (title.size > 0) or (authors.size > 0) - html += "\n" - end + html += "\n" if !title.empty? || !authors.empty? html += '<small>' - if publisher and publisher.size > 0 - html += "#{CGI.escapeHTML(publisher)}" + if publisher && !publisher.empty? + html += CGI.escapeHTML(publisher).to_s end - if year and year.size > 0 - if publisher and publisher.size > 0 - html += ' ' - end + if year && !year.empty? + html += ' ' if publisher && !publisher.empty? html += "(#{year})" end html + '</small>' - end - - def on_motion(view, event) - tree_path = view.get_path(event.x, event.y) - # TODO translate path a few times, for sorting & filtering... - # hmmm, actually seems to work. Report a bug if you can spot a failure - if tree_path - iter = view.model.get_iter(tree_path) - if @latest_iter.nil? - @latest_iter = iter - - @tooltip_timeout_id = Gtk.timeout_add(250) do - if @latest_iter == iter - - title = iter[2] # HACK hardcoded, should use column names... - authors = iter[4] - publisher = iter[6] - year = iter[7] - @label.markup = label_for_book(title, authors, publisher, year) - ## "<b>#{title}</b>\n<i>#{authors}</i>\n<small>#{publisher} <i>(#{year})</i></small>" - size = @tooltip_window.size_request - @tooltip_window.move(event.x_root - size[0], - event.y_root + 12) - @tooltip_window.show - # don't run again - false - else - @tooltip_timeout_id = nil - end - end - - elsif @latest_iter != iter - hide_tooltip - end - - else - hide_tooltip - end - end - - def hide_tooltip - unless @tooltip_window.nil? - @tooltip_window.hide - if @tooltip_timeout_id - Gtk.timeout_remove(@tooltip_timeout_id) - @tooltip_timeout_id = nil - end - @latest_iter = nil - end - end - - def on_leave(_view, _event) - @tooltip_window.hide end end