//--------------------------------------------------------------
//
//  NavAction.Cc
//
//  Rowset navigation method and toolbar classes
//
//  Example of use:
//
//    oForm = new form()
//    set procedure to NavAction.Cc additive
//    oForm.NavAction = new NavAction( oForm )
//    new NavBar().attach((oForm)
//
//
//  dBASE Samples Group
//
//  Copyright (c) 2000, dBASE, Inc.
//  All rights reserved.
//
//---------------------------------------------------------------
class NavAction( oForm )

   this.Form = oForm

   function goFirst
      local bFirst
      bFirst = false 
      if ( class::rowActive() )         
         bFirst := this.Form.rowset.first()
      endif
      this.refreshMenu()
      return ( bFirst )

   function goPrev
      local bPrev
      bPrev = false
      if ( class::rowActive() )
         if ( not this.Form.rowset.atFirst() ) 
            bPrev := this.Form.rowset.next( -1 )
            if ( not bPrev ) 
               this.Form.rowset.next() 
            endif
         endif
      endif
      this.refreshMenu()
      return ( bPrev )
 
   function goNext
      local bNext
      bNext = false
      if ( class::rowActive() )
         if ( not this.Form.rowset.atLast() )
            bNext := this.Form.rowset.next()
            if ( not bNext  )
               this.Form.rowset.next(-1)
            endif
         endif
      endif
      this.refreshMenu()
      return ( bNext )

   function goLast
      local bLast
      bLast = false
      if ( class::rowActive() )
         bLast := this.Form.rowset.last()
      endif
      this.refreshMenu()
      return ( bLast )

   function rowActive()
      local bActive
      bActive = true
      if ( this.Form.rowset == null ) or ;
         ( not this.Form.rowset.parent.active ) 
         bActive := false      
      endif
      return ( bActive )

   function refreshMenu
       local bAtFirst, bAtLast
       bAtFirst = this.Form.rowset.atFirst()
       bAtLast  = this.Form.rowset.atLast()
       if type("this.Form.root.NavMenu") == "O"
          with ( this.Form.root.NavMenu )
             menuFirst.enabled := ( not bAtFirst )
             menuPrev.enabled  := ( not bAtFirst )
             menuNext.enabled  := ( not bAtLast )
             menuLast.enabled  := ( not bAtLast )
          endwith
      endif
      return ( ( not bAtFirst ) and ( not bAtLast ) )

endclass

class NavBar of toolBar

   this.text := "Navigation"

   this.tFirst = new ToolButton( this )
   with ( this.tFirst )
      onClick  := { ;this.parent.form.navAction.goFirst() }
      bitmap   := "RESOURCE TS_FIRST"
      speedTip := "First Row"
   endwith

   this.tPrev = new ToolButton( this )
   with ( this.tPrev )
      onClick := { ;this.parent.form.navAction.goPrev() }
      bitmap  := "RESOURCE TS_PREV"
      speedTip := "Previous Row"
   endwith

   this.tNext = new ToolButton( this )
   with ( this.tNext )
      onClick := { ;this.parent.form.navAction.goNext() }
      bitmap  := "RESOURCE TS_NEXT"
      speedTip := "Next Row"
   endwith

   this.tLast = new ToolButton( this )
   with ( this.tLast )
      onClick := { ;this.parent.form.navAction.goLast() }
      bitmap  := "RESOURCE TS_LAST"
      speedTip := "Last Row"
   endwith

   function onUpdate
      local bActiveRow, bAtFirst, bAtLast
      bActiveRow = false
      bAtFirst   = true
      bAtLast    = true

      if ( type("this.form.rowset.parent.active") == "L" )
         bActiveRow := this.form.rowset.parent.active
      endif

      if ( bActiveRow and this.form.rowset.state # 5)
         bAtFirst := this.form.rowset.atFirst()
         bAtLast  := this.form.rowset.atLast()
      endif

      with ( this )
         tFirst.enabled   := ( not bAtFirst )
         tPrev.enabled    := ( not bAtFirst )
         tNext.enabled    := ( not bAtLast )
         tLast.enabled    := ( not bAtLast )     
      endwith

endclass
