home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / text / ncclib.zip / NCCDEMO.ZIP / G_VIEW.PRG < prev    next >
Text File  |  1993-05-01  |  16KB  |  508 lines

  1. //═══════════════════════════════════════════════════════╕
  2. //  Program .....: G_View                                │
  3. //  CopyRight ...: 1992 National Computer Consultants    │
  4. //                 All rights are reserved.              │
  5. //  Author ......: Greg Rice                             │
  6. //═══════════════════════════════════════════════════════╛
  7.  
  8. #include "dbstruct.ch"
  9. #include "fileio.ch"
  10. #include "inkey.ch"
  11. #include "g_menu.ch"
  12.  
  13.  
  14. #define ON        1
  15. #define OFF       0
  16. #define TO_SAVE   1
  17. #define TO_OPEN   2
  18. #define CRLF      chr(13)+chr(10)
  19.  
  20.  
  21. Function g_Viewcreate()
  22.  
  23.  
  24.     LOCAL nPickCount                                            , ;
  25.           fc                                                    , ;
  26.           i                                                     , ;
  27.           FullScreen                                            , ;
  28.           nBottom                                               , ;
  29.           reverse                                               , ;
  30.           d_Start                                               , ;
  31.           InsScreen                                             , ;
  32.           InsKey                                                , ;
  33.           InsBottom                                             , ;
  34.           pos                                                   , ;
  35.           scrn                                                  , ;
  36.           cColor        := SetColor()                           , ;
  37.           ok            := .f.                                  , ;
  38.           InsSele       := 0                                    , ;
  39.           nCounter      := 0                                    , ;
  40.           nFieldSele    := 0                                    , ;
  41.           nKey          := 0                                    , ;
  42.           aPickDisp     := {}                                   , ;
  43.           aPickFull     := {}                                   , ;
  44.           aPickRef      := {}                                   , ;
  45.           aNewStru      := {}                                   , ;
  46.           nExitKeys     := { 97, 65, K_F10, K_INS , K_DEL }     , ;
  47.           dbF_structure := ACLONE( WinViewStru() )              , ;
  48.           dbstruct      := dbstruct()
  49.  
  50.  
  51.  
  52.     if Empty(alias())
  53.       Return( NIL )
  54.     endif
  55.  
  56.     FullScreen := savescreen()
  57.  
  58.     ShowSelectBox(dbF_structure)
  59.  
  60.     setcolor(popup_color())
  61.     NccMesg('..LOADING..',07+min(maxrow()-14,len(dbF_structure)),'center,02,46')
  62.  
  63.     AEVAL(dbF_structure,{ |fi_stru| aadd(aPickDisp,space(7)+;
  64.                                          padr(fi_stru[DBS_NAME],10,' ')+ ;
  65.                                          space(4)+fi_stru[DBS_TYPE]+space(4)+;
  66.                                          str(fi_stru[DBS_LEN],3)+space(4)+ ;
  67.                                          str(fi_stru[DBS_DEC],3)+' '), ;
  68.                                          nCounter++ })
  69.  
  70.     AEVAL(dbstruct,{ |fi_stru| aadd(aPickFull,space(1)+;
  71.                                     padr(fi_stru[DBS_NAME],10,' ')+ ;
  72.                                     space(1)+fi_stru[DBS_TYPE]+space(1)+;
  73.                                     str(fi_stru[DBS_LEN],3)+space(1)+ ;
  74.                                     str(fi_stru[DBS_DEC],3)+' ') })
  75.  
  76.     nPickCount := 0
  77.  
  78.     WHILE len(aPickDisp) # 0
  79.       fc := ltrim(str(nCounter))
  80.       keyboard ''
  81.       nBottom := 05+min(maxrow()-14,nCounter)
  82.       nFieldSele := arraydsp( aPickDisp,;
  83.                               'Field List',;
  84.                               IF(nPickCount == 0,;
  85.                                 '',;
  86.                                 'Selections:'+ltrim(str(nPickCount))+'   ';
  87.                                 )+;
  88.                                   'Field Count:'+fc,;
  89.                               06,04,nBottom,43,nFieldSele,;
  90.                               IF(nFieldSele>((nBottom-6)/2)+1,;
  91.                                  int(((nBottom-6)/2)),;
  92.                                  nFieldSele-1;
  93.                                 ),;
  94.                               .f.,nExitKeys, ;
  95.                             { |o| dView_MouseReader(o,06,04,nBottom,43), ;
  96.                                   menu1Reader(o,06,04,nBottom,43) ;
  97.                             } ;
  98.                             )
  99.  
  100.       nKey := lastkey()
  101.  
  102.       Do Case
  103.         Case  nKey == K_RETURN
  104.            IF      subs(aPickDisp[nFieldSele],6,1) == chr(251)
  105.  
  106.              adel(aPickRef, ascan(aPickRef,nFieldSele))
  107.              asize(aPickRef, LEN(aPickRef)-1)
  108.  
  109.              aPickDisp[nFieldSele] := space(7) + subs(aPickDisp[nFieldSele],8)
  110.              nPickCount--
  111.  
  112.              i := 1
  113.              scrn := savescreen(07+min(maxrow()-14,nCounter),04,;
  114.                                 07+min(maxrow()-14,nCounter),43)
  115.              NccMesg('     Wait...Re-Sequencing       ',07+min(maxrow()-14,;
  116.                   nCounter),'center,04,43')
  117.  
  118.              FOR i = 1 to nCounter
  119.                 aPickDisp[i] := seq(aPickRef,i) + subs(aPickDisp[i],5)
  120.  
  121.              NEXT
  122.              restscreen(07+min(maxrow()-14,nCounter),04,;
  123.                         07+min(maxrow()-14,nCounter),43,scrn)
  124.  
  125.            ELSEIF      subs(aPickDisp[nFieldSele],6,1) # chr(251)
  126.  
  127.              aadd(aPickRef, nFieldSele)
  128.  
  129.              aPickDisp[nFieldSele] := seq(aPickRef,nFieldSele) + ' ' + ;
  130.                                       chr(251) + ' ' + ;
  131.                                       subs(aPickDisp[nFieldSele],8)
  132.              nPickCount++
  133.  
  134.            END
  135.  
  136.         Case nKey == K_INS .or. nKey == 48 .or. uppe(chr(nKey)) == 'A'
  137.  
  138.            InsScreen := savescreen(03,49,10+min(maxrow()-14,len(aPickFull)-1),79)
  139.            setcolor(popup_color())
  140.            WinBox(03,49,09+min(maxrow()-14,len(aPickFull)-1),77,0,4)
  141.            WinBox(10+min(maxrow()-14,len(aPickFull)-1),49,12+min(maxrow()-14,len(aPickFull)-1),77,0,4)
  142.            shadow(03,49,12+min(maxrow()-14,len(aPickFull)-1),77)
  143.            NccMesg('[Esc]-Abort [Enter]-Select',11+min(maxrow()-14,len(aPickFull)-1),'center,50,76')
  144.  
  145.  
  146.            InsBottom := 06+min(maxrow()-14,fcount()-1)
  147.            InsSele := arraydsp(aPickFull,;
  148.                      'Select Field to '+if(uppe(chr(nKey))='A','Add','Insert'),;
  149.                      'Field Count:'+ltrim(str(fcount())),;
  150.                      06,51,InsBottom,74,InsSele,;
  151.                      IF(InsSele>((InsBottom-6)/2)+1,;
  152.                        int(((InsBottom-6)/2)),;
  153.                         InsSele-1;
  154.                        ),;
  155.                      .f., ;
  156.                      , ;
  157.                      { |o| dView_MouseReader(o,06,51,InsBottom,74), ;
  158.                            menuReader(o,06,51,InsBottom,74) ;
  159.                      } ;
  160.                     )
  161.  
  162.  
  163.            InsKey := lastkey()
  164.  
  165.            IF InsKey == K_RETURN
  166.              IF uppe(chr(nKey)) == 'A'
  167.                 aadd(aPickDisp,space(7)+;
  168.                      padr(dbstruct[InsSele,DBS_NAME],10,' ')+ ;
  169.                      space(4)+dbstruct[InsSele,DBS_TYPE]+space(4)+;
  170.                      str(dbstruct[InsSele,DBS_LEN],3)+space(4)+ ;
  171.                      str(dbstruct[InsSele,DBS_DEC],3)+' ')
  172.                 aadd(dbF_structure,dbstruct[InsSele])
  173.                 nFieldSele := len(aPickDisp)
  174.  
  175.              ELSE
  176.                 aadd(aPickDisp,NIL)
  177.                 aadd(dbF_structure,NIL)
  178.                 ains(aPickDisp,nFieldSele)
  179.                 ains(dbF_structure,nFieldSele)
  180.                 aPickDisp[nFieldSele] := space(7)+;
  181.                      padr(dbstruct[InsSele,DBS_NAME],10,' ')+ ;
  182.                      space(4)+dbstruct[InsSele,DBS_TYPE]+space(4)+;
  183.                      str(dbstruct[InsSele,DBS_LEN],3)+space(4)+ ;
  184.                      str(dbstruct[InsSele,DBS_DEC],3)+' '
  185.                 dbF_structure[nFieldSele] := dbstruct[InsSele]
  186.  
  187.              END
  188.  
  189.              InsSele := if(InsSele < len(aPickFull),InsSele+1,InsSele)
  190.              nCounter++
  191.  
  192.            END
  193.            restscreen(03,49,10+min(maxrow()-14,len(aPickFull)-1),79,InsScreen)
  194.            restscreen(,,,,FullScreen)
  195.            ShowSelectBox(dbF_structure)
  196.  
  197.         Case nKey == K_DEL .or. nKey == 46
  198.            adel(dbF_structure,nFieldSele)
  199.            adel(aPickDisp,nFieldSele)
  200.            aSize(dbF_structure,len(dbF_structure)-1)
  201.            aSize(aPickDisp,len(aPickDisp)-1)
  202.            nCounter--
  203.            restscreen(0,0,maxrow(),maxcol(),FullScreen)
  204.            ShowSelectBox(dbF_structure)
  205.  
  206.         Otherwise
  207.  
  208.            exit
  209.  
  210.       EndCase
  211.  
  212.     END
  213.  
  214.     IF len(aPickDisp) # 0
  215.       IF nPickCount # 0 .and. nKey # K_ESC
  216.         NccMesg('     Wait...Creating table      ',07+min(maxrow()-14,nCounter),;
  217.               'center,04,43')
  218.  
  219.         FOR i = 1 to Len(aPickRef)
  220.            aadd(aNewStru, dbF_structure[aPickRef[i]])
  221.         NEXT
  222.  
  223.         WinViewStru( , aNewStru )
  224.         WinViewFields( , stru_load(aNewStru) )
  225.         ok := .t.
  226.  
  227.       ELSEIF nKey # K_ESC
  228.         WinViewStru( , dbF_structure )
  229.         WinViewFields( , stru_load(dbF_structure) )
  230.         ok := .t.
  231.  
  232.       END
  233.  
  234.     END
  235.  
  236.     restscreen(,,,,FullScreen)
  237.     SetColor( cColor )
  238.  
  239.  
  240. Return( NIL )
  241.  
  242.  
  243. STATIC Function seq( aArray,nRef )
  244.  
  245.     LOCAL ret_val := 0
  246.  
  247.     ret_val := ascan( aArray, nRef )
  248.  
  249. Return( if(ret_val == 0, space(4),str(ret_val,4,0)) )
  250.  
  251.  
  252.  
  253.  
  254. STATIC Function ShowSelectBox( aArray )
  255.  
  256.     setcolor(popup_color())
  257.     WinBox(03,02,08+min(maxrow()-14,len( aArray )),46,0,4)
  258.     WinBox(9+min(maxrow()-14,len( aArray )),02,12+min(maxrow()-14,len( aArray )),46,0,4)
  259.     shadow(03,02,12+min(maxrow()-14,len( aArray )),46)
  260.     NccMesg('[A]-Add  [Ins]-Insert  [Del]-Delete',10+min(maxrow()-14,len( aArray )),'center,03,45')
  261.     NccMesg('[Esc]-Abort  [Enter]-Select  [F10]-Save',11+min(maxrow()-14,len( aArray )),'center,03,45')
  262.  
  263. Return( NIL )
  264.  
  265.  
  266. Function Stru_Load( y )
  267.  
  268.     local i := {}
  269.  
  270.     AEVAL( y,;
  271.            {|x| aadd(i,padr(x[DBS_NAME],10,' ')+;
  272.                 space(2)+x[DBS_TYPE]+space(2)+str(x[DBS_LEN],3)+space(2)+;
  273.                 str(x[DBS_DEC],3));
  274.            };
  275.          )
  276.  
  277. Return( i )
  278.  
  279.  
  280.  
  281.  
  282. STATIC Function menu1Reader(o,TopRow,LeftColumn,BottomRow,RightColumn)
  283.  
  284.     LOCAL row                                 , ;
  285.           col                                 , ;
  286.           button                              , ;
  287.           Mouse  := MouseSys()                , ;
  288.           nKey   := 0
  289.  
  290.  
  291.     row    := Mouse:Row
  292.     col    := Mouse:Column
  293.     button := Mouse:Button
  294.  
  295.     if lastkey() # 0
  296.       nKey := lastkey()
  297.     endif
  298.  
  299.     if button # 0
  300.       Do Case
  301.         Case row == BottomRow+5 .and. col >= LeftColumn+3 .and. ;
  302.                                       col <= LeftColumn+9
  303.           nKey := asc('A')
  304.  
  305.         Case row == BottomRow+5 .and. col >= LeftColumn+12 .and. ;
  306.                                       col <= LeftColumn+23
  307.           nKey := K_INS
  308.  
  309.         Case row == BottomRow+5 .and. col >= LeftColumn+26 .and. ;
  310.                                       col <= LeftColumn+37
  311.           nKey := K_DEL
  312.  
  313.         Case row == BottomRow+6 .and. col >= LeftColumn+1 .and. ;
  314.                                       col <= LeftColumn+11
  315.           nKey := K_ESC
  316.  
  317.         Case row == BottomRow+6 .and. col >= LeftColumn+14 .and. ;
  318.                                       col <= LeftColumn+27
  319.           nKey := K_RETURN
  320.  
  321.         Case row == BottomRow+6 .and. col >= LeftColumn+30 .and. ;
  322.                                       col <= LeftColumn+39
  323.           nKey := K_F10
  324.  
  325.       EndCase
  326.     endif
  327.  
  328.     keyboard chr(nKey)
  329.     inkey()
  330.  
  331. Return( nKey )
  332.  
  333.  
  334. STATIC Function menuReader(o,TopRow,LeftColumn,BottomRow,RightColumn)
  335.  
  336.     LOCAL row                    , ;
  337.           col                    , ;
  338.           button                 , ;
  339.           Mouse  := MouseSys()   , ;
  340.           nKey   := 0
  341.  
  342.  
  343.     row    := Mouse:Row
  344.     col    := Mouse:Column
  345.     button := Mouse:Button
  346.  
  347.     if lastkey() # 0
  348.       nKey := lastkey()
  349.     endif
  350.     if button # 0
  351.       Do Case
  352.         Case row == BottomRow+5 .and. col >= LeftColumn-1 .and. ;
  353.                                       col <= LeftColumn+9
  354.           nKey := K_ESC
  355.  
  356.         Case row == BottomRow+5 .and. col >= LeftColumn+11 .and. ;
  357.                                       col <= LeftColumn+24
  358.           nKey := K_RETURN
  359.  
  360.       EndCase
  361.     endif
  362.  
  363.     keyboard chr(nKey)
  364.     inkey()
  365.  
  366. Return( nKey )
  367.  
  368.  
  369. Function g_Browse()
  370.  
  371.     WinTop(,    WinObj():TopRow      -4 )
  372.     WinLeft(,   WinObj():LeftColumn  -1 )
  373.     WinBottom(, WinObj():BottomRow   +1 )
  374.     WinRight(,  WinObj():RightColumn +1 )
  375.  
  376.     OpenWindow()
  377.  
  378. Return( NIL )
  379.  
  380.  
  381. Function g_Record()
  382.  
  383.     LOCAL y, head := Winobj():Head
  384.  
  385.  
  386.     WinTop(,    WinObj():TopRow      -4 )
  387.     WinLeft(,   WinObj():LeftColumn  -1 )
  388.     WinBottom(, WinObj():BottomRow   +1 )
  389.     WinRight(,  WinObj():RightColumn +1 )
  390.  
  391.     y :=  NccViewit():New( WinTop()    +4, ;
  392.                        WinLeft()   +1, ;
  393.                        WinBottom() -1, ;
  394.                        WinRight()  -1  ;
  395.                      )
  396.  
  397.     y:DataBlock     := { |l| RecDisplay( WinObj(), l ) }
  398.     y:GoTopBlock    := { || WinObj():UserSlot[2] := 1 }
  399.     y:GoBottomBlock := { || WinObj():UserSlot[2] :=     ;
  400.                             len(WinViewStru())          ;
  401.                        }
  402.     y:SkipBlock     := { |n| RecSkipper( WinObj(), n ) }
  403.     y:UserSlot      := {                                         ;
  404.                          { || FileBase(WinFilename(),".DBF") } , ;
  405.                          1                                     , ;
  406.                          { || drawline(head) }                 , ;
  407.                          y:StatusBlock                                           ;
  408.                        }
  409.     y:StatusBlock   := { |n| RecStatus( y, n ) }
  410.     y:UseStyle      := .t.
  411.  
  412.     WinObj( , y)
  413.  
  414.     WindowFrame(y)
  415.     WinObj():Activate()
  416.  
  417. Return( NIL )
  418.  
  419.  
  420. STATIC Function RecStatus( o, n )
  421.  
  422.     if ! eof()
  423.       @ o:TopRow-3,o:LeftColumn     say 'Record:'+;
  424.               padr(ltrim(str(recno())) + '/' + ltrim(str(lastrec())),14,' ')
  425.     endif
  426.  
  427.     if Deleted()
  428.       NccMesg( "<Deleted>", o:TopRow-3,'Center,' + str(o:RightColumn)+','+ str(o:LeftColumn)+['] )
  429.     else
  430.       NccMesg( "         ", o:TopRow-3,'Center,'+str(o:RightColumn)+','+str(o:LeftColumn)+['] )
  431.     endif
  432.  
  433.     @ o:TopRow-2,o:LeftColumn      say 'Field:'+;
  434.               padr(ltrim(str(o:UserSlot[2])) + '/' + ltrim(str(len(WinViewStru()))),14,' ')
  435.  
  436.     eval( o:userslot[4], n )
  437.  
  438. Return( NIL )
  439.  
  440.  
  441. STATIC Function RecDisplay( o, l )
  442.  
  443.     LOCAL x     := WinViewStru(Current_window())     , ;
  444.           y     := o:userslot[2]                     , ;
  445.           width := o:RightColumn - o:LeftColumn + 1
  446.  
  447.  
  448.  
  449.     if eof() .or. ! l
  450.       Return( space( width ) )
  451.     else
  452.       scroll( o:CurrentRow,o:LeftColumn,o:CurrentRow,o:RightColumn,0)
  453.       @row(),col() say subs(;
  454.                             padr(;
  455.                                  subs( x[y][1],1,1 )+;
  456.                                  subs( lower( x[y][1] ),2 ),;
  457.                                  10,;
  458.                                  '.';
  459.                                 ) + ;
  460.                             ":" +;
  461.                             dto_String( fieldget( fieldpos( x[y][1] ) ) ), ;
  462.                             1, ;
  463.                             width;
  464.                            )
  465.     endif
  466.  
  467. Return( '' )
  468.  
  469.  
  470. STATIC Function RecSkipper( o , n)
  471.  
  472.     local nActual := 0, nDirection := if(n<0,-1,1)
  473.  
  474.     if n == 0
  475.       Return( 0 )
  476.     endif
  477.  
  478.     while nActual # n
  479.       if n > 0
  480.         if o:userslot[2] # len(WinViewStru(Current_Window()))
  481.           o:userslot[2]++
  482.         else
  483.           exit
  484.         endif
  485.       else
  486.         if o:userslot[2] # 1
  487.           o:userslot[2]--
  488.         else
  489.           exit
  490.         endif
  491.       endif
  492.       nActual += nDirection
  493.     enddo
  494.  
  495. Return( nActual )
  496.  
  497.  
  498. STATIC Function Drawline( head )
  499.  
  500.     Local x := WinObj()
  501.  
  502.  
  503.     @x:TopRow - 1, x:LeftColumn say ;
  504.          Replicate(Head, x:RightColumn - x:LeftColumn + 1 )
  505.  
  506. Return( NIL )
  507.  
  508.