home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D8 / TVFM.ZIP / FILEVIEW.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  9.3 KB  |  378 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Vision File Manager Demo               }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit FileView;  { File pane object }
  9.  
  10. {$X+}
  11. {$V-}
  12.  
  13. interface
  14.  
  15. uses Drivers, Objects, Views, App, Dos, Dialogs, Memory,
  16.   Globals, Equ, Tools, DragDrop;
  17.  
  18. type
  19.  
  20.   PFileView = ^TFileView;
  21.   TFileView = object(TDDList)
  22.     Foc: Integer;
  23.     Dir: PathStr;
  24.     List: PFileList;
  25.     DoneScanning: Boolean;
  26.     Search: SearchRec;
  27.     constructor Init(var Bounds: TRect; AHScrollBar, AVScrollBar: PScrollBar);
  28.     procedure HandleEvent(var Event: TEvent); virtual;
  29.     destructor Done; virtual;
  30.     function SearchForFiles(First: Boolean): Boolean;
  31.     procedure ScanSingleFile(FileName: PathStr);
  32.     function GetPalette : PPalette; virtual;
  33.     procedure Draw; virtual;
  34.     function GetText(Item: Integer; MaxLen: Integer): String; virtual;
  35.     procedure SetState(AState: Word; Enable: Boolean); virtual;
  36.     procedure PickUpItem(Item: Integer; Where: TPoint); virtual;
  37.   end;
  38.  
  39.  
  40. implementation
  41.  
  42. uses MsgBox;
  43.  
  44. { TFileView }
  45. constructor TFileView.Init(var Bounds: TRect; AHScrollBar,
  46.   AVScrollBar: PScrollBar);
  47. begin
  48.   inherited Init(Bounds, 1, AHScrollBar, AVScrollBar);
  49.   List := New(PFileList, Init(30,10));
  50.   Dir := '';
  51.   DoneScanning := True;
  52.   EventMask := EventMask or evIdle;
  53.   Foc := 0;
  54. end;
  55.  
  56. function TFileView.GetPalette: PPalette;
  57. const
  58.   MyPal : String[length(CListViewer)] = #6#6#7#6#1;
  59. begin
  60.   GetPalette := @MyPal;
  61. end;
  62.  
  63. procedure TFileView.Draw;
  64. var
  65.   B: TDrawBuffer;
  66.   C: Word;
  67. begin
  68.   inherited Draw;
  69.   if List^.Count = 0 then
  70.   begin
  71.     C := GetColor(1);
  72.     MoveChar(B, ' ', C, Size.X);
  73.     MoveStr(B, RezStrings^.Get(sNoFiles), C);
  74.     WriteLine(0, 0, Size.X, 1, B);
  75.   end;
  76. end;
  77.  
  78. function TFileView.GetText(Item: Integer; MaxLen: Integer): String;
  79. var
  80.   F: PFileRec;
  81.   S: String;
  82.   Params: array[0..3] of Pointer;
  83.   DOpt: Word;
  84. begin
  85.   if Item < List^.Count then
  86.   begin
  87.     F := List^.At(Item);
  88.     Params[0] := @F^.Name;
  89.     Params[1] := @F^.Ext;
  90.     Params[2] := Pointer(F^.Size);
  91.  
  92.     with ConfigRec do
  93.     begin
  94.       if DisplayFields and $1 <> 0 then FormatStr(S, ' %-8s%-4s %7d', Params)
  95.       else FormatStr(S, ' %-8s%-4s', Params);
  96.  
  97.       if F^.Tagged then S[1] := TagChar;
  98.  
  99.       DOpt := (DisplayFields and $6) shr 1;  { change 0xx0 -> 0,1,2,3 }
  100.  
  101.       { 0=none, 1=Date, 2=Time, 3=Date and Time }
  102.       if DOpt > 0 then S := S + ' ' + FormatDateTime(F^.Time, DOpt);
  103.  
  104.       if (DisplayFields and $8) <> 0 then S := S + ' ' + FormatAttr(F^.Attr);
  105.     end;
  106.  
  107.     if Length(S) > MaxLen then S[0] := Char(MaxLen);
  108.  
  109.     if ConfigRec.DisplayCase = 0 then LowerCase(S);
  110.     GetText := S;
  111.  
  112.   end else GetText := '';
  113. end;
  114.  
  115. function TFileView.SearchForFiles(First: Boolean): Boolean;
  116. var
  117.   F: PFileRec;
  118. begin
  119.   SearchForFiles := False;
  120.   if First then FindFirst(Dir + '\' + ConfigRec.FileMask, AnyFile, Search)
  121.   else FindNext(Search);
  122.   if DosError = 0 then
  123.   begin
  124.     if Search.Attr and UnwantedFiles = 0 then
  125.     begin
  126.       F := New(PFileRec, Init(Search));
  127.       List^.Insert(F);
  128.     end;
  129.   end else SearchForFiles := True;  { done searching }
  130. end;
  131.  
  132. procedure TFileView.ScanSingleFile(FileName: PathStr);
  133. var
  134.   F: PFileRec;
  135. begin
  136.   FindFirst(FileName, AnyFile, Search);
  137.   if DosError = 0 then
  138.   begin
  139.     if (Search.Attr and UnwantedFiles = 0) then
  140.     begin
  141.       F := New(PFileRec, Init(Search));
  142.       List^.Insert(F);
  143.       SetRange(List^.Count);
  144.       DrawView;
  145.     end;
  146.   end;
  147. end;
  148.  
  149. procedure TFileView.HandleEvent(var Event: TEvent);
  150. var
  151.   F: PFileRec;
  152.   P: PFileNameRec;
  153.   ScanInfo: TScanInfo;
  154.   Where: TPoint;
  155.   Mover: PFileMover;
  156.   I: Integer;
  157.   WildCard: string[12];
  158.   R: TRect;
  159.  
  160.   procedure ReverseTags(F: PFileRec); far;
  161.   begin
  162.     F^.Toggle;
  163.     Message(Owner, evBroadcast, cmTagChanged, F);
  164.   end;
  165.  
  166.   procedure ClearTags(F: PFileRec); far;
  167.   begin
  168.     if F^.Tagged then
  169.     begin
  170.       F^.Toggle;
  171.       Message(Owner, evBroadcast, cmTagChanged, F);
  172.     end;
  173.   end;
  174.  
  175.   procedure TagPerCard(F: PFileRec); far;
  176.   begin
  177.     if WildCardMatch(F^.Name + F^.Ext, WildCard) then
  178.     begin
  179.       F^.Tagged := True;
  180.       Message(Owner, evBroadcast, cmTagChanged, F);
  181.     end;
  182.   end;
  183.  
  184.   function MatchFile(F: PFileRec): Boolean; far;
  185.   begin
  186.     P := Event.InfoPtr;
  187.     MatchFile := (P^.Dir = Dir + '\') and (P^.Name = F^.Name) and
  188.       (P^.Ext = F^.Ext);
  189.   end;
  190.  
  191.   procedure CountBytes(F: PFileRec); far;
  192.   begin
  193.     Inc(ScanInfo.ScanBytes, F^.Size);
  194.   end;
  195.  
  196. begin
  197.   inherited HandleEvent(Event);
  198.  
  199.   if Event.What = evBroadcast then
  200.   begin
  201.     case Event.Command of
  202.  
  203.       { Scan a new directory, or rescan current directory }
  204.       cmNewDir,
  205.       cmRescan :
  206.         begin
  207.           if Event.Command = cmNewDir then Dir := PString(Event.InfoPtr)^;
  208.           Owner^.Last^.DrawView; {Force the frame to redraw }
  209.           DoneScanning := False;
  210.           List^.FreeAll;
  211.           DoneScanning := SearchForFiles(True); { search for the first file }
  212.           if (not DoneScanning) and LowMemory then
  213.           begin
  214.             DoneScanning := True;
  215.             Application^.OutOfMemory;
  216.           end;
  217.           if DoneScanning then
  218.           begin
  219.             SetRange(List^.Count);
  220.             DrawView;
  221.             ScanInfo.ScanCount := List^.Count;
  222.             ScanInfo.ScanBytes := 0;
  223.             List^.ForEach(@CountBytes);
  224.             Message(Owner, evBroadcast, cmScanComplete, @ScanInfo);
  225.           end;
  226.           if Event.Command = cmNewDir then ClearEvent(Event);
  227.         end;
  228.  
  229.       { Mark the current file as tagged }
  230.       cmListItemSelected :
  231.         begin
  232.           if List^.Count > 0 then
  233.           begin
  234.             F := List^.At(Focused);
  235.             F^.Toggle;
  236.             Message(Owner, evBroadcast, cmTagChanged, F);
  237.             DrawView;
  238.             ClearEvent(Event);
  239.           end;
  240.         end;
  241.  
  242.       { Reorder and redraw the list since the sort order may have changed }
  243.       cmRefreshDisplay :
  244.         begin
  245.           PFileList(List)^.Reorder;
  246.           DrawView;
  247.         end;
  248.  
  249.       cmItemDropped :
  250.         begin
  251.           Mover := Event.InfoPtr;
  252.           Desktop^.MakeGlobal(Mover^.Origin, Where);
  253.           if MouseInView(Where) then
  254.           begin
  255.             ClearEvent(Event);
  256.             DragDropCopy(Mover, Dir);
  257.           end;
  258.         end;
  259.  
  260.     end; { case }
  261.   end;
  262.  
  263.   if Event.What = evIdle then
  264.   begin
  265.     if not DoneScanning then
  266.     begin
  267.       DoneScanning := SearchForFiles(False);
  268.       if DoneScanning then
  269.       begin
  270.         SetRange(List^.Count);
  271.         DrawView;
  272.         ScanInfo.ScanCount := List^.Count;
  273.         ScanInfo.ScanBytes := 0;
  274.         List^.ForEach(@CountBytes);
  275.         Message(Owner, evBroadcast, cmScanComplete, @ScanInfo);
  276.       end;
  277.     end;
  278.   end;
  279.  
  280.   if Event.What = evCommand then
  281.   begin
  282.     case Event.Command of
  283.       cmReverseTags : List^.ForEach(@ReverseTags);
  284.       cmClearTags: List^.ForEach(@ClearTags);
  285.       cmTagPerCard:
  286.         begin
  287.           R.Assign(0,0,35,8);
  288.           R.Move((Desktop^.Size.X - R.B.X) div 2, (Desktop^.Size.Y - R.B.Y) div 2);
  289.           WildCard := '*.*';
  290.           if InputBoxRect(R, 'Tag per wildcard', 'Wildcard', WildCard, 12) = cmOK then
  291.           begin
  292.             UpperCase(WildCard);
  293.             List^.ForEach(@TagPerCard);
  294.           end;
  295.         end;
  296.       else Exit;
  297.     end;
  298.     DrawView;
  299.     ClearEvent(Event);
  300.   end;
  301. end;
  302.  
  303. procedure TFileView.PickUpItem(Item: Integer; Where: TPoint);
  304. var
  305.   R: TRect;
  306.   Mover: PMover;
  307.   E: TEvent;
  308.   Min, Max: TPoint;
  309.   F: PFileRec;
  310.   NewList: PCollection;
  311.   S: SearchRec;
  312.  
  313.   function CloneFileRec(Orig: PFileRec): PFileRec;
  314.   begin
  315.     S.Name := Orig^.Name + Orig^.Ext;
  316.     S.Attr := Orig^.Attr;
  317.     S.Size := Orig^.Size;
  318.     S.Time := Orig^.Time;
  319.     CloneFileRec := New(PFileRec, Init(S));
  320.   end;
  321.  
  322.   procedure AddIfTagged(FileRec: PFileRec); far;
  323.   begin
  324.     if FileRec^.Tagged then
  325.       NewList^.Insert(CloneFileRec(FileRec));
  326.   end;
  327.  
  328. begin
  329.   NewList := New(PCollection, Init(10, 5));
  330.  
  331.   F := List^.At(Item);  { are we dragging the tagged files? }
  332.   if F^.Tagged then List^.ForEach(@AddIfTagged)
  333.   else NewList^.Insert(CloneFileRec(F));
  334.  
  335.   Dec(Where.Y);
  336.   Mover := New(PFileMover, Init(Where, Dir, NewList));
  337.   Inc(Where.Y); 
  338.   Desktop^.Insert(Mover);
  339.   Desktop^.GetExtent(R);
  340.  
  341.   E.What := evMouseDown;
  342.   E.Where := Where;
  343.   Min := Mover^.Size;
  344.   Max := Min;
  345.   Mover^.DragView(E, dmDragMove, R, Min, Max);
  346.   Message(Desktop, evBroadcast, cmItemDropped, Mover);
  347.   Dispose(Mover, Done);
  348.   Dispose(NewList, Done);
  349. end;
  350.  
  351. procedure TFileView.SetState(AState: Word; Enable: Boolean);
  352.  
  353.   procedure ShowScrollBar(SBar: PScrollBar);
  354.   begin
  355.     if (SBar <> nil) then
  356.       if GetState(sfActive + sfSelected) then SBar^.Show
  357.       else SBar^.Hide;
  358.   end;
  359.  
  360. begin
  361.   inherited SetState(AState, Enable);
  362.   if AState and (sfActive + sfSelected) <> 0 then
  363.   begin
  364.     ShowScrollBar(HScrollBar);
  365.     ShowScrollBar(VScrollBar);
  366.   end;
  367. end;
  368.  
  369. destructor TFileView.Done;
  370. begin
  371.   if List <> nil then Dispose(List, Done);
  372.   inherited Done;
  373. end;
  374.  
  375.  
  376.  
  377. end. { unit }
  378.