Function O4WI_FORMDESIGNER_PAGE_XXX(ACTION, headerInfo, formInfo_Orig, uniqueid, ctlentid, event, param1, param2, param3, status)
*#!Precompile

/*
*  This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without
*  written permission from Revelation Technologies, Inc.
!
*
*  VERSION    : 1.0
*
*
*  AUTHOR     : Revelation Software Inc., All Rights Reserved
*
*  CREATED    : July 20, 2015
*
*
!
*
*  REVISION HISTORY           (Most CURRENT first) :
*
*    DATE       IMPLEMENTOR     FUNCTION
*  --------     -----------     --------
*
*
*/
*
$Insert o4wcommon
$Insert o4wequates
$Insert O4W_DESIGN_FORM_EQUATES
$Insert O4W_COMMUTER_COMMON

Declare Function Repository, Function, RTI_VERIFY_PROC

If Assigned(ACTION) Else ACTION = ""
If Assigned(HEADERINFO) Else HEADERINFO = ""
If Assigned(FORMINFO_ORIG) Else FORMINFO_ORIG = ""
If Assigned(UNIQUEID) Else UNIQUEID = ""
If Assigned(param1) Else param1 = ""
If Assigned(param2) Else param2 = ""
If Assigned(param3) Else param3 = ""
If Assigned(status) Else status = ""

If pageNo@ = "" Then pageNo@ = 1
If pageNo@ <> headerinfo<HEADER_CURR_PAGE$> Then
   * must load in the current page
   Read formInfo From o4wtempfile%, uniqueID:"_PAGE_":pageNo@ Else formInfo = ""
End Else
   formInfo = formInfo_Orig
end

Equ page_desc$ To "Generic"
If formInfo<FORMINFO_PAGE_CONTROL_HDR$> = "" Then
   Gosub setDefaults
End Else
   Gosub updateHeader
end


RSLT = ""

If Not(Num(ACTION)) Or ACTION < PAGEWIDGET_ACTION_MIN_VALUE$ Or ACTION > PAGEWIDGET_ACTION_MAX_VALUE$ Then
     rslt = WIDGET_ACTION_INVALID$
End Else
     On action Gosub doPreDraw, doDraw, handleResults, doPropsShow, doPropsSave, doSaveDefaults
End

Return RSLT


doPropsShow:
   O4WSectionStart("specialSection", o4wmarkedoptions(1):o4wresponseoptions())
   num.fields = dcount(formInfo<FORMINFO_ID$>, @VM)
   o4wtablestart("fieldTable")
   For each.Field = 1 To num.fields
      id = forminfo<FORMINFO_ID$, each.Field>
      type = formInfo<FORMINFO_TYPE$, each.Field>
      supportName = UCASE(DESIGN_SUPPORT_PREFIX$:Type)
      IF RTI_VERIFY_PROC(supportName, 0, 5) = 0 THEN
          CALL SET_STATUS(0)
      END Else
         pInfo = Function(@supportName(WIDGET_ACTION_INFO$, headerInfo, formInfo, id, WIDGET_INFO_DB$))
         If pInfo <> "" Then
         end
      end
   Next each.field
   o4wbutton("OK", "BTN_PPROPS_OK", O4WMARKEDOPTIONS(1))
   o4wspace(3)
   o4wbutton("Cancel", "BTN_DISCARD_NO")
   O4WQUALIFYEVENT("BTN_PPROPS_OK", "CLICK")
   O4WQUALIFYEVENT("BTN_DISCARD_NO", "CLICK")
   o4wsectionend("specialSection")
   o4wdialog("specialSection", page_desc$:" Properties")
Return

doPropsSave:
   NAMES = "CLEAR"
   values = ""
   Gosub doUpdate2
   values = o4wgetvalue("KEYFIELD")
   names = "KEYFIELD"
   Gosub doUpdate2
Return

doUpdate:
   names = param1
   values = param2
doUpdate2:
   num.names = dcount(names, @FM)
   For each.name = 1 To num.names
      this.name = names<each.name>
      this.value = values<each.name>
      Begin Case
         Case this.name = "CLEAR"
            formInfo<FORMINFO_PAGE_CONTROL_HDR$> = HEADER_TAG_VERSION$
            formInfo<FORMINFO_PAGE_CONTROL$> = HEADER_TAG_VERSION_VALUE$
         Case this.name = "KEYFIELD"
      End Case
   Next each.name
Return

doPreDraw:
   DRROOverrideFlag@ = ""
   DRShowNoneOverrideFlag@ = ""

Return

doDraw:
Return

handleResults:
runMode = param1
bMobile = param2
bHandled = 0


If bHandled = 0 Then
     * call into the helper to handle this
     rslt = O4WI_FORMDESIGNER_PAGE_HELPER("EVENT",  headerInfo, formInfo, errs, runMode, bMobile, ctlentid, event, uniqueID)
end

Return

setDefaults:
formInfo<FORMINFO_PAGE_CONTROL_HDR$> = HEADER_TAG_VERSION$
formInfo<FORMINFO_PAGE_CONTROL$> = HEADER_TAG_VERSION_VALUE$
num.fields = dcount(formInfo<FORMINFO_ID$>, @VM)
buttons = ""
firstField = ""
For each.Field = 1 To num.fields
   id = forminfo<FORMINFO_ID$, each.Field>
   type = formInfo<FORMINFO_TYPE$, each.Field>
   supportName = UCASE(DESIGN_SUPPORT_PREFIX$:Type)
   IF RTI_VERIFY_PROC(supportName, 0, 5) = 0 THEN
       CALL SET_STATUS(0)
   END Else
      If type = CONTROL_TYPE_BUTTON$ Or type = CONTROL_TYPE_M_BUTTON$ Then
           pInfo = Function(@supportName(WIDGET_ACTION_INFO$, headerInfo, formInfo, id, WIDGET_INFO_NAME$))
           bName = ID
           If pInfo <> "" Then
               bName = pInfo<1>
           End
           buttons<1,-1> = id
           buttons<2,-1> = bName
      End else
           pInfo = Function(@supportName(WIDGET_ACTION_INFO$, headerInfo, formInfo, id, WIDGET_INFO_DB$:@FM:WIDGET_INFO_INPUT$))
           If pInfo<1,1> <> ""  And pInfo[1,1] <> "<" And pInfo<2> = "1" Then
              Locate id In formInfo<FORMINFO_PAGE_CONTROL_HDR$> using @VM setting posn Else null 
              formInfo<FORMINFO_PAGE_CONTROL_HDR$, posn> = id
              tableName = pInfo<1,1>
              fieldName = pInfo<1,2>
              conv_o = pInfo<1,5>
              pType = "0" ;* default to prompt
              skipIfNull = 1 ;* default to true
              sType = 0 ;* default to unchanged
              jType = "0" ;* default to and
              addl = ""
              details = "IMP":@SVM:ptype:@SVM:addl:@SVM:type:@SVM:tableName:@SVM:fieldName:@SVM:conv_o:@SVM:skipIfNull:@SVM:sType:@SVM:jType
              formInfo<FORMINFO_PAGE_CONTROL$, posn> = details
              If pInfo<2> = "1" And firstField = "" Then firstField = id
           End
       end
   End
Next each.Field
num.buttons = dcount(buttons<1>, @VM)
For each.button = 1 To num.buttons
     this.button = buttons<1, each.button>
     this.button.name = buttons<2, each.button>
     If this.button = "" Then this.button = " "
     bType = ""
     unassignedCnt = 1
     Begin Case
     Case indexc(this.button.name, "read", 1) Or indexc(this.button.name, "go", 1)
          bType = TRIGGER_EVENT_READ$     
     Case indexc(this.button.name, "search", 1)
          bType = TRIGGER_EVENT_SEARCH$
     Case indexc(this.button.name, "forward", 1) Or indexc(this.button.name, "next", 1)
          bType = TRIGGER_EVENT_FORWARD$
     Case indexc(this.button.name, "prev", 1) Or indexc(this.button.name, "back", 1)
          bType = TRIGGER_EVENT_BACK$  
     Case indexc(this.button.name, "save", 1)
          btype = TRIGGER_EVENT_SAVE$
     Case indexc(this.button.name, "cancel", 1)
          btype = TRIGGER_EVENT_CANCEL$
     Case indexc(this.button.name, "del", 1)
          btype = TRIGGER_EVENT_DEL$
     Case indexc(this.button.name, "new", 1)
          btype = TRIGGER_EVENT_NEW$
     Case indexc(this.button.name, "qbf", 1)
          * not an assignable type
     CASE unassignedCnt = 1
          bType = TRIGGER_EVENT_SEARCH$
          unassignedCnt += 1
     CASE unassignedCnt = 2 
          bType =  TRIGGER_EVENT_CANCEL$
          unassignedCnt += 1
     CASE unassignedCnt = 3
          bType = TRIGGER_EVENT_FORWARD$
          unassignedCnt += 1
     CASE unassignedCnt = 4
          bType = TRIGGER_EVENT_BACK$
          unassignedCnt += 1
     CASE unassignedCnt = 5 
          bType = TRIGGER_EVENT_NEW$
          unassignedCnt += 1
     CASE unassignedCnt = 6 
          bType = TRIGGER_EVENT_DEL$
          unassignedCnt += 1
     CASE unassignedCnt = 7 
          bType = TRIGGER_EVENT_READ$
          unassignedCnt += 1
     CASE unassignedCnt = 8
          btype = TRIGGER_EVENT_SAVE$
          unassignedCnt += 1
     End CASE
     If bType <> "" Then
          * bzs 011721 swapped for version 2.0
          formInfo = Insert(formInfo, FORMINFO_PAGE_CONTROL_HDR$, 1, 0, this.button)
          formInfo = Insert(formInfo, FORMINFO_PAGE_CONTROL$, 1, 0, bType)
          If bType = TRIGGER_EVENT_NEW$ Then
               * default to 'user entered' key
               formInfo = Insert(formInfo, FORMINFO_PAGE_CONTROL_HDR$, 1, 0, HEADER_TAG_NEW_DETAIL$)
               formInfo = Insert(formInfo, FORMINFO_PAGE_CONTROL$, 1, 0, "U")
          end
      end
Next each.button
If firstField <> "" Then
     formInfo<FORMINFO_PAGE_CONTROL_HDR$, -1> = HEADER_TAG_FOCUS$
     formInfo<FORMINFO_PAGE_CONTROL$, -1> = firstField
end
Return

updateHeader:
* bzs 011721 make sure header info is in new format
If formInfo<FORMINFO_PAGE_CONTROL_HDR$,1> <> HEADER_TAG_VERSION$ Or formInfo<FORMINFO_PAGE_CONTROL$,1> <> HEADER_TAG_VERSION_VALUE$ Then
     NUM.HDR = DCOUNT(forminfo<FORMINFO_PAGE_CONTROL_HDR$>, @VM)
     NEW.HDR = HEADER_TAG_VERSION$
     NEW.DETAILS = HEADER_TAG_VERSION_VALUE$
     For EACH.HDR = 1 To NUM.HDR
          this.element = formInfo<FORMINFO_PAGE_CONTROL_HDR$, each.hdr>
          this.detail = formInfo<FORMINFO_PAGE_CONTROL$, each.hdr>
          Begin Case
               Case THIS.ELEMENT = HEADER_TAG_VERSION$
                    * ignored - already updated
               Case this.element = TRIGGER_EVENT_DEL$ Or this.element = TRIGGER_EVENT_CANCEL$ Or this.element = TRIGGER_EVENT_NEW$ Or this.element = TRIGGER_EVENT_SAVE$ Or this.element = TRIGGER_EVENT_READ$
                    * swap these
                    NEW.HDR<1,-1> = this.detail ;* button ID
                    NEW.DETAILS<1,-1> = this.element ;* action name
               Case this.element = HEADER_TAG_FOCUS$ Or THIS.ELEMENT = HEADER_TAG_NEW_DETAIL$ Or THIS.ELEMENT = HEADER_TAG_PREDRAW_EVENT$ Or THIS.ELEMENT = HEADER_TAG_POSTDRAW_EVENT$ Or THIS.ELEMENT = HEADER_TAG_CUSTOM_EVENT$
                    * these belong in the options
                    formInfo<FORMINFO_PAGE_OPTIONS_HDR$, -1> = THIS.ELEMENT
                    formInfo<FORMINFO_PAGE_OPTIONS$, -1> = THIS.DETAIL
               Case 1
                    * these are OK
                    NEW.HDR<1,-1> = this.element
                    NEW.DETAILS<1,-1> = this.detail
          End case
     Next EACH.HDR
     formInfo<FORMINFO_PAGE_CONTROL_HDR$> = NEW.HDR
     formInfo<FORMINFO_PAGE_CONTROL$> = NEW.DETAILS
     NUM.OPTIONS = DCOUNT(formInfo<FORMINFO_PAGE_OPTIONS_HDR$>, @VM)
     NEW.HDR = ""
     NEW.DETAILS = ""
     For EACH.HDR = 1 To NUM.HDR
          THIS.ELEMENT = formInfo<FORMINFO_PAGE_OPTIONS_HDR$, each.hdr>
          THIS.DETAIL = formInfo<FORMINFO_PAGE_OPTIONS$, each.hdr>
          Begin Case
               Case this.element = "MAXSEARCH" Or THIS.element = "WARNSEARCH" Or this.element = "TRIGGER_TYPE"
                    NEW.HDR<1,-1> = "<":THIS.ELEMENT:">"
                    NEW.DETAILS<1,-1> = THIS.DETAIL
               Case this.element = "QBFDISABLE" or this.element = "NOSAVEOK" Or THIS.element = "REDIRECTTO"
                    NEW.HDR<1,-1> = "<":THIS.ELEMENT:">"
                    NEW.DETAILS<1,-1> = THIS.DETAIL
               Case 1
                    * these are OK
                    NEW.HDR<1,-1> = this.element
                    NEW.DETAILS<1,-1> = this.detail          
          End case
     Next EACH.HDR
     formInfo<FORMINFO_PAGE_OPTIONS_HDR$> = NEW.HDR
     formInfo<FORMINFO_PAGE_OPTIONS$> = NEW.DETAILS
     Gosub doSaveDefaults
End
return


doSaveDefaults:
* property defaults must already be set; just make sure To save them
Write formInfo On o4wtempfile%, uniqueID:"_PAGE_":pageNo@
If pageNo@ = headerInfo<HEADER_CURR_PAGE$> Then
 formInfo_ORIG = formInfo
end   
return
