#
# $Id: displayscrollarea.icn,v 1.7 2006-07-09 23:43:07 rparlett Exp $
#
# This file is in the public domain.
#
# Author: Robert Parlett (parlett@dial.pipex.com)
#

package gui
link graphics

$include "guih.icn"

#
# This class extends LineBasedScrollArea to provide keyboard movement.  No
# selection, or cursor handling, and no events are generated.
#
class DisplayScrollArea : LineBasedScrollArea()
   method handle_key_page_up(e)
      local i
      if i := (\self.vsb).get_value() then {
         self.vsb.set_value(i - self.vsb.page_size) 
         self.refresh()
      }
   end

   method handle_key_page_down(e)
      local i
      if i := (\self.vsb).get_value() then {
         self.vsb.set_value(i + self.vsb.page_size) 
         self.refresh()
      }
   end

   method handle_key_up(e)
      local i
      if i := (\self.vsb).get_value() then {
         self.vsb.set_value(i - self.vsb.increment_size) 
         self.refresh()
      }
   end

   method handle_key_down(e)
      local i
      if i := (\self.vsb).get_value() then {
         self.vsb.set_value(i + self.vsb.increment_size) 
         self.refresh()
      }
   end

   method handle_key_left(e)
      local i
      if i := (\self.hsb).get_value() then {
         self.hsb.set_value(i - self.hsb.increment_size) 
         self.refresh()
      }
   end

   method handle_key_right(e)
      local i
      if i := (\self.hsb).get_value() then {
         self.hsb.set_value(i + self.hsb.increment_size) 
         self.refresh()
      }
   end

   method handle_key_home(e)
      if \self.vsb then {
         self.vsb.set_value(0)
         self.refresh()
      }
   end

   method handle_key_end(e)
      if \self.vsb then {
         self.vsb.set_value(get_subject_height() - self.view.h)
         self.refresh()
      }
   end

   method handle_event(e)
      (\self.vsb).handle_event(e)
      (\self.hsb).handle_event(e)

      if \self.has_focus then {
         case e of {
            Key_Home : handle_key_home(e)
            Key_End : handle_key_end(e)
            Key_PgUp : handle_key_page_up(e)
            Key_PgDn : handle_key_page_down(e)
            Key_Up : handle_key_up(e)
            Key_Down : handle_key_down(e)
            Key_Left : handle_key_left(e)
            Key_Right : handle_key_right(e)
         }
      }
   end

   method keeps(e)
      return e === (Key_Left | Key_Right | Key_Up | Key_Down)
   end

   initially
      self.ScrollArea.initially()
      self.set_accepts_focus()
end
