Module: Alexandria::UI::DragAndDropable

Included in:
IconViewManager, ListViewManager
Defined in:
lib/alexandria/ui/dndable.rb

Constant Summary collapse

BADGE_MARKUP =
'<span weight="heavy" foreground="white">%d</span>'

Instance Method Summary collapse

Instance Method Details

#setup_view_source_dnd(view) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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