home *** CD-ROM | disk | FTP | other *** search
- // Tb17.prg - Demonstrate editing and appending records during tbrowse
- // display. Append mode is indicated by an entry in the object's
- // cargo instance variable. Handles empty databases as well.
- // AUTOMATIC append mode
-
- // Compile with /a /m /n /w
- // Link with tbutils, dict
-
- #include "Inkey.ch"
- #include "Setcurs.ch"
- #include "Tbutils.ch"
-
- MEMVAR getList
-
- FUNCTION Tb17
-
- FIELD Lname, Fname, Addr1, Addr2, Addr3 IN Tbdbf1
-
- LOCAL oTbr
- LOCAL lExitRequested := .F.
- LOCAL nKey
- LOCAL nSr, nSc
- LOCAL bNext
- LOCAL bPrev
-
- Set(_SET_SCOREBOARD, .F.)
- Set(_SET_CURSOR, SC_NONE)
-
- USE Tbdbf1
- IF !File("Tbdbf1.ntx")
- INDEX ON Upper(Lname + Fname) TO Tbdbf1
- ELSE
- SET INDEX TO Tbdbf1
- ENDIF
-
- CLEAR SCREEN
- @ 0, 0 TO 23, 79
-
- oTbr := TBrowseDb(1, 1, 22, 78)
- AddAllFields(oTbr)
-
- oTbr:cargo := DictNew()
-
- // For automatic append mode ...
- APPEND_MODE(oTbr, .T.)
-
- bNext := {|| TBaGoNext(APPEND_MODE(oTbr)) }
- bPrev := {|| TBaGoPrev() }
-
- oTbr:skipBlock := {|n| DbSkipBlock(n, bNext, bPrev) }
- oTbr:goTopBlock := {|| TBaGoFirst() }
- oTbr:goBottomBlock := {|| TBaGoLast(APPEND_MODE(oTbr)) }
-
- DO WHILE !lExitRequested
- FullStabilize(oTbr)
-
- IF !lExitRequested
- nKey := InKey(0)
- IF nKey == K_ESC
- lExitRequested := .T.
- ELSE
- KeyHandle(nKey, oTbr)
- ENDIF
- ENDIF
- ENDDO
-
- CLEAR SCREEN
-
- RETURN NIL
-
-
- FUNCTION KeyHandle(nKey, oTbr)
-
- IF !StdMeth(nKey, oTbr)
- DO CASE
- CASE nKey == K_ENTER // EDIT key
- IF oTbr:stable
- FldEdit(oTbr)
- ENDIF
-
- CASE nKey == K_INS
- oTbr:goBottom()
-
- CASE nKey >= 32 .AND. nKey <= 255
- IF oTbr:stable
- KEYBOARD chr(K_ENTER) + chr(nKey)
- ENDIF
- ENDCASE
- ENDIF
-
- RETURN NIL
-
-
- FUNCTION FldEdit(oTbr)
-
- LOCAL bFldGsb, cFval, lSaveCurs
- LOCAL cNewKeyVal, cOldKeyVal
- LOCAL aCols := TbcColPos(oTbr)
- LOCAL aFvals := Array(oTbr:rightVisible)
- LOCAL i, lMemoUpdated := .F.
- LOCAL cMemoDisplay := "Memo"
- LOCAL aMemoVals := {}
- LOCAL nMemoNum
-
- nMemoNum := 0 // How many memos we've had so far
- FOR i := oTbr:leftVisible TO oTbr:rightVisible
- // tbColumn's block instance var is a get / set block
- bFldGsb := oTbr:getColumn(i):block
-
- // Get its contents to check whether really a memo ...
- aFvals[i] := Eval(bFldGsb)
- IF valtype(aFvals[i]) == "C" .AND. aFvals[i] == "Memo"
- // Evaluating bFldGsb with ANY parameter returns a get / set
- // block for memos
- bFldGsb := Eval(bFldGsb, .T.)
-
- // Evaluate this once to get the memo's contents and store in
- // aMemoVals array
- Aadd(aMemoVals, eval(bFldGsb))
-
- // Increment memo count
- nMemoNum++
-
- // Issue a GET on the string "Memo". Pass it the array containing
- // the memos and set the get object's cargo instance variable
- // to the index. The function can then access this subscript with
- // GetActive():cargo.
- @ Row(), aCols[i] GET cMemoDisplay ;
- WHEN MemoFunc(@lMemoUpdated, aMemoVals)
-
- GetList[len(GetList)]:cargo := nMemoNum
-
- ELSE
- @ Row(), aCols[i] GET aFvals[i]
- ENDIF
-
- NEXT
-
- // We must save old index key val to see whether
- // it changed after the edit
- cOldKeyVal := &(IndexKey())
-
- lSaveCurs := Set(_SET_CURSOR, SC_NORMAL)
- READ
- Set(_SET_CURSOR, lSaveCurs)
-
- IF (Updated() .OR. lMemoUpdated) .AND. LastKey() != K_ESC
- IF Eof()
- APPEND BLANK
- DO WHILE NetErr()
- APPEND BLANK
- ENDDO
- ELSE
- DO WHILE !Rlock()
- ENDDO
- ENDIF
-
- // Update all fields
- nMemoNum := 0
- FOR i := oTbr:leftVisible TO oTbr:rightVisible
- // tbColumn's block instance var is a get / set block
- bFldGsb := oTbr:getColumn(i):block
- cFval := Eval(bFldGsb)
-
- // Special handling for memos
- IF valtype(cFval) == "C" .AND. cFval == "Memo"
- nMemoNum++
-
- // Evaluating this with ANY parameter returns a get / set
- // block for the memo field
- bFldGsb := Eval(bFldGsb, .T.)
-
- // Now update the memo field to what we saved in the array
- Eval(bFldGsb, aMemoVals[nMemoNum])
- ELSE
- // Update the field
- Eval(bFldGsb, aFvals[i])
- ENDIF
- NEXT
-
- UNLOCK
-
- // Get new key value - If you already have a code block
- // on the current index key, simply eval it here instead
- cNewKeyVal := &(IndexKey())
- // If the key changed, refreshAll()
- IF cOldKeyVal != cNewKeyVal
- DbStabilize(oTbr)
- ELSE
- oTbr:refreshcurrent()
- ENDIF
- ELSE // !updated()
- // To remove reverse video from gets
- oTbr:refreshCurrent()
- ENDIF
-
- RETURN .F.
-
-
- #define MEMO_TOP 10
- #define MEMO_LEFT 20
- #define MEMO_BOTTOM 20
- #define MEMO_RIGHT 60
-
- // Edit memo field. lMemoUpdated is passed by reference. Set it .T.
- // if this memo was updated, otherwise do not change it. aMemoVals
- // contains a reference to the array containing the memo fields' values
- FUNCTION MemoFunc(lMemoUpdated, aMemoVals)
-
- LOCAL cSaveScr, cFval
- LOCAL lThisMemoUpdated
-
- cSaveScr := SaveScreen(MEMO_TOP, MEMO_LEFT, ;
- MEMO_BOTTOM, MEMO_RIGHT)
- @ MEMO_TOP, MEMO_LEFT TO ;
- MEMO_BOTTOM, MEMO_RIGHT
- cFval := MemoEdit(aMemoVals[GetActive():cargo], ;
- MEMO_TOP + 1, MEMO_LEFT + 1, ;
- MEMO_BOTTOM - 1, MEMO_RIGHT - 1)
- lThisMemoUpdated := LastKey() == K_CTRL_W
- RestScreen(MEMO_TOP, MEMO_LEFT, MEMO_BOTTOM, ;
- MEMO_RIGHT, cSaveScr)
-
- IF lThisMemoUpdated
- // This updates the array element, but not the field. This is
- // done later.
- aMemoVals[GetActive():cargo] := cFval
- lMemoUpdated := .T.
- ENDIF
-
- RETURN .F.