module Alexandria::UI::DragAndDropable

Constants

BADGE_MARKUP

Public Instance Methods

setup_view_source_dnd(view) click to toggle source
# File lib/alexandria/ui/dndable.rb, line 29
def setup_view_source_dnd(view)
  # better be Loggable!
  log.info { format("setup_view_source_dnd for %s", view) }
  view.signal_connect_after("drag-begin") do |_widget, drag_context|
    n_books = @parent.selected_books.length
    if n_books > 1
      # Render generic book icon.
      pixmap, mask = Icons::BOOK_ICON.render_pixmap_and_mask(255)

      # Create number badge.
      context = Gdk::Pango.context
      layout = Pango::Layout.new(context)
      layout.markup = BADGE_MARKUP % n_books
      width, height = layout.pixel_size
      x = Icons::BOOK_ICON.width - width - 11
      y = Icons::BOOK_ICON.height - height - 11

      # Draw a red ellipse where the badge number should be.
      red_gc = Gdk::GC.new(pixmap)
      red_gc.rgb_fg_color = Gdk::Color.parse("red")
      red_gc.rgb_bg_color = Gdk::Color.parse("red")
      pixmap.draw_arc(red_gc,
                      true,
                      x - 5, y - 2,
                      width + 9, height + 4,
                      0, 360 * 64)

      # Draw the number badge.
      pixmap.draw_layout(Gdk::GC.new(pixmap), x, y, layout)

      # And set the drag icon.
      Gtk::Drag.set_icon(drag_context,
                         pixmap.colormap,
                         pixmap,
                         mask,
                         10,
                         10)
    end
  end

  view.signal_connect("drag-data-get") do |_, _, selection_data, _, _|
    idents = @parent.selected_books.map(&:ident)
    unless idents.empty?
      selection_data.set(Gdk::Selection::TYPE_STRING,
                         idents.join(","))
    end
  end

  view.enable_model_drag_source(:button1_mask,
                                Alexandria::UI::BOOKS_TARGET_TABLE,
                                :move)
end