ruby 1.9 array stringification has changed, I need to use iconv the proper way now
[booh] / lib / booh / GtkAutoTable.rb
index 962a224a77a4ecd1f5ffe30018e5bdfdd02f10a4..6c45cf983445b9833a79e8492e52031389b4c221 100644 (file)
@@ -1,3 +1,22 @@
+#                         *  BOOH  *
+#
+# A.k.a `Best web-album Of the world, Or your money back, Humerus'.
+#
+# The acronyn sucks, however this is a tribute to Dragon Ball by
+# Akira Toriyama, where the last enemy beaten by heroes of Dragon
+# Ball is named "Boo". But there was already a free software project
+# called Boo, so this one will be it "Booh". Or whatever.
+#
+#
+# Copyright (c) 2004 Guillaume Cottenceau <gc3 at bluewin.ch>
+#
+# This software may be freely redistributed under the terms of the GNU
+# public license version 2.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
 require 'gtk2'
 
 require 'booh/booh-lib'
@@ -9,16 +28,19 @@ class Gtk::Allocation
     end
 end
 
-class Gtk::AutoTable < Gtk::VBox
+class Gtk::AutoTable < Gtk::EventBox
+
+    attr_accessor :queue_draws
 
     def initialize(row_spacings)
         @children = []
         @containers = []
         @width = -1
         @old_widths = []
+        @queue_draws = []
         @row_spacings = row_spacings
         @table = nil
-        super
+        super()
         recreate_table
         signal_connect('size-allocate') { |w, allocation|
             msg 3, "got self allocation: #{allocation}"
@@ -34,9 +56,9 @@ class Gtk::AutoTable < Gtk::VBox
             end
         }
         @timeout = Gtk.timeout_add(100) {
-            if @queue_draw
-                queue_draw
-                @queue_draw = false
+            if @queue_draws.size > 0
+                @queue_draws.each { |elem| elem.call }
+                @queue_draws.clear
             end
             true
         }
@@ -77,24 +99,10 @@ class Gtk::AutoTable < Gtk::VBox
                 warn "Critical: child not found!"
             end
         }
-
-        #- handle reordering with drag and drop
-        Gtk::Drag.source_set(widget, Gdk::Window::BUTTON1_MASK, [['reorder-elements', Gtk::Drag::TARGET_SAME_APP, 1]], Gdk::DragContext::ACTION_MOVE)
-        Gtk::Drag.dest_set(widget, Gtk::Drag::DEST_DEFAULT_ALL, [['reorder-elements', Gtk::Drag::TARGET_SAME_APP, 1]], Gdk::DragContext::ACTION_MOVE)
-        widget.signal_connect('drag-data-get') { |w, ctxt, selection_data, info, time|
-            selection_data.set(Gdk::Selection::TYPE_STRING, get_current_number(widget).to_s)
-        }
-        widget.signal_connect('drag-data-received') { |w, ctxt, x, y, selection_data, info, time|
-            ctxt.targets.each { |target|
-                if target.name == 'reorder-elements'
-                    insert(selection_data.data.to_i, get_current_number(widget))
-                end
-            }
-        }
     end
 
     #- remove a widget from the list of automatically handled widgets
-    def remove(widget)
+    def remove_widget(widget)
         @children.each_with_index { |chld, index|
             if chld[:widget] == widget
                 @children.delete_at(index)
@@ -105,6 +113,13 @@ class Gtk::AutoTable < Gtk::VBox
         return false
     end
 
+    #- re-insert a widget at a given pos
+    def reinsert(pos, widget, name)
+        child = { :widget => widget, :name => name }
+        @children[pos, 0] = child
+        redistribute(true)
+    end
+
     #- remove all widgets
     def clear
         @children = []
@@ -136,8 +151,8 @@ class Gtk::AutoTable < Gtk::VBox
         return -1
     end
 
-    #- insert a widget before another by numbers
-    def insert(src, dst)
+    #- move widgets by numbers
+    def move(src, dst)
         if src != dst
             chld = @children.delete_at(src)
             @children[dst > src ? dst - 1 : dst, 0] = chld
@@ -275,7 +290,9 @@ class Gtk::AutoTable < Gtk::VBox
         @containers = []
         if @table
             remove(@table)
-            @table.destroy
+            @table.hide     #- should be #destroy, but that triggers an Abort in booh, and I cannot really understand why and fix 
+                            #- this is a memory leak, so ideally it should be either fixed in ruby-gtk2 0.16.0, or at least checked if
+                            #- it's not fixed from a side effect of another fix in the future
         end
         add(@table = Gtk::Table.new(0, 0, true))
         @table.set_row_spacings(@row_spacings)
@@ -287,7 +304,7 @@ class Gtk::AutoTable < Gtk::VBox
         if unallocated = @children.find { |e| !e[:allocation] }
             #- waiting for allocations. replace last displayed widget with first unallocated.
             last_container = @containers[-1]
-            put(unallocated, last_container[:x], last_container[:y])
+            put(unallocated, last_container ? last_container[:x] : 0, last_container ? last_container[:y]: 0)
 
         else
             if @children.size == 0
@@ -319,7 +336,7 @@ class Gtk::AutoTable < Gtk::VBox
                         put(e, e[:xnew], e[:ynew])
                     }
                     show_all
-                    @queue_draw = true
+                    @queue_draws << proc { queue_draw }
                 end
             end
         end