autoscroll on keyrelease when textview or image is not visible
[booh] / bin / booh
index d2e889784263691d6058bddf8d907bd32df79196..522a54df2e31966239d6503c8b18e45a9a04fdc1 100755 (executable)
--- a/bin/booh
+++ b/bin/booh
@@ -213,7 +213,7 @@ def view_element(filename)
     w.show_all
 end
 
-def create_editzone(scrolledwindow, pagenum)
+def create_editzone(scrolledwindow, pagenum, image)
     frame = Gtk::Frame.new
     frame.add(textview = Gtk::TextView.new.set_wrap_mode(Gtk::TextTag::WRAP_WORD))
     frame.set_shadow_type(Gtk::SHADOW_IN)
@@ -267,6 +267,29 @@ def create_editzone(scrolledwindow, pagenum)
                       }, candidate_undo_text)
             candidate_undo_text = nil
         end
+
+        if ![Gdk::Keyval::GDK_Page_Up, Gdk::Keyval::GDK_Page_Down, Gdk::Keyval::GDK_Up, Gdk::Keyval::GDK_Down].include?(event.keyval)
+            #- autoscroll if cursor or image is not visible
+            ypos_top = (image && image.window) ? image.window.position[1] : textview.window.position[1]
+            ypos_bottom = max(textview.window.position[1] + textview.window.size[1], image && image.window ? image.window.position[1] + image.window.size[1] : -1)
+            current_miny_visible = scrolledwindow.vadjustment.value
+            current_maxy_visible = scrolledwindow.vadjustment.value + scrolledwindow.vadjustment.page_size
+            if ypos_top < current_miny_visible
+                newval = scrolledwindow.vadjustment.value -
+                         ((current_miny_visible - ypos_top - 1) / scrolledwindow.vadjustment.step_increment + 1) * scrolledwindow.vadjustment.step_increment
+                if newval < scrolledwindow.vadjustment.lower
+                    newval = scrolledwindow.vadjustment.lower
+                end
+                scrolledwindow.vadjustment.value = newval
+            elsif ypos_bottom > current_maxy_visible
+                newval = scrolledwindow.vadjustment.value +
+                         ((ypos_bottom - current_maxy_visible - 1) / scrolledwindow.vadjustment.step_increment + 1) * scrolledwindow.vadjustment.step_increment
+                if newval > scrolledwindow.vadjustment.upper - scrolledwindow.vadjustment.page_size
+                    newval = scrolledwindow.vadjustment.upper - scrolledwindow.vadjustment.page_size
+                end
+                scrolledwindow.vadjustment.value = newval
+            end
+        end
         false  #- propagate
     }
 
@@ -466,7 +489,7 @@ def add_thumbnail(autotable, filename, type, thumbnail_img, caption)
     tipname = from_utf8(File.basename(filename).gsub(/\.[^\.]+$/, ''))
     tooltips.set_tip(evtbox, utf8(type == 'video' ? (_("%s (video - %s KB)") % [tipname, commify(file_size(from_utf8("#{$current_path}/#{filename}"))/1024)]) : tipname), nil)
 
-    frame2, textview = create_editzone($autotable_sw, 1)
+    frame2, textview = create_editzone($autotable_sw, 1, img)
     textview.buffer.text = utf8(caption)
     textview.set_justification(Gtk::Justification::CENTER)
 
@@ -1093,7 +1116,7 @@ def change_dir
             $gesture_press = nil
         }
         
-        frame, textview = create_editzone($subalbums_sw, 0)
+        frame, textview = create_editzone($subalbums_sw, 0, img)
         textview.buffer.text = caption
         $subalbums.attach(Gtk::Alignment.new(0, 0.5, 0.5, 0).add(frame),
                           1, 2, current_y_sub_albums, current_y_sub_albums + 1, Gtk::FILL, Gtk::FILL, 2, 2)
@@ -1104,7 +1127,7 @@ def change_dir
 
     if $xmldir.elements['dir']
         #- title edition
-        frame, $subalbums_title = create_editzone($subalbums_sw, 0)
+        frame, $subalbums_title = create_editzone($subalbums_sw, 0, nil)
         $subalbums_title.buffer.text = $xmldir.attributes['subdirs-caption']
         $subalbums_title.set_justification(Gtk::Justification::CENTER)
         $subalbums_vb.pack_start(Gtk::Alignment.new(0.5, 0.5, 0.5, 0).add(frame), false, false)