home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sibylft1.zip / SAMPLES.DAT / SAMPLES / DRAGDROP / EXTERN / DRGDRPU1.PAS < prev   
Pascal/Delphi Source File  |  1997-03-18  |  6KB  |  171 lines

  1. Unit DrgDrpU1;
  2.  
  3. //Sibyl Drag & Drop example
  4. //This example demonstrates drag 'n drop between two different applications.
  5. //For this purpose a shared memory object is used that transfers data
  6. //between the processes.
  7.  
  8. Interface
  9.  
  10. Uses
  11.   Classes, Forms, Graphics, StdCtrls, Dialogs, Buttons;
  12.  
  13. Type
  14.   TDragDropForm = Class (TForm)
  15.     Label1: TLabel;
  16.     Label2: TLabel;
  17.     ListBox1: TListBox;
  18.     BitBtn1: TBitBtn;
  19.     Procedure BitBtn1OnClick (Sender: TObject);
  20.     Procedure ListBox1OnEndDrag (Sender: TObject; Target: TObject; X: LongInt;
  21.                                  Y: LongInt);
  22.     Procedure ListBox1OnDragDrop (Sender: TObject; Source: TObject; X: LongInt;
  23.                                   Y: LongInt);
  24.     Procedure ListBox1OnDragOver (Sender: TObject; Source: TObject; X: LongInt;
  25.                                   Y: LongInt; State: TDragState; Var Accept: Boolean);
  26.     Procedure ListBox1OnCanDrag (Sender: TObject; X: LongInt; Y: LongInt;
  27.                                  Var Accept: Boolean);
  28.     Procedure ListBox1OnStartDrag (Sender: TObject;Var DragData: TDragDropData);
  29.   Private
  30.     {Insert private declarations here}
  31.   Public
  32.     {Insert public declarations here}
  33.   End;
  34.  
  35. Var
  36.   DragDropForm: TDragDropForm;
  37.  
  38. Implementation
  39.  
  40. //Name of the shared memory object
  41. Const
  42.     SharedMemName='DragDrop_ItemsInfo';
  43.  
  44. //Drag source identification
  45. Const
  46.     DragSourceId='Sibyl DragDrop Demo';
  47.  
  48. //information structure for drag/drop
  49. Type
  50.     TDragDropItem=Record
  51.                         Len:Byte;         //length of string
  52.                         Data:Array[0..0] Of Char;
  53.                   End;
  54.  
  55.     PDragDropInfo=^TDragDropInfo;
  56.     TDragDropInfo=Record
  57.                         Count:LongWord;   //Count of elements
  58.                         Items:Array[0..0] Of TDragDropItem;
  59.                   End;
  60.  
  61. Procedure TDragDropForm.BitBtn1OnClick (Sender: TObject);
  62. Var s:String;
  63. Begin
  64.    //Display an input box to add an item
  65.    If InputQuery('Add an item','Type string to add:',s) Then
  66.       ListBox1.Items.Add(s);
  67. End;
  68.  
  69. Procedure TDragDropForm.ListBox1OnEndDrag (Sender: TObject; Target: TObject;
  70.                                            X: LongInt; Y: LongInt);
  71. Var t:LongInt;
  72. Begin
  73.     //Look if the items were dropped successfully...
  74.     If Target Is TExternalDragDropObject Then
  75.     Begin
  76.        //remove the items dragged
  77.        For t:=ListBox1.Items.Count-1 DownTo 0 Do
  78.          If ListBox1.Selected[t] Then ListBox1.Items.Delete(t);
  79.     End;
  80.  
  81.     //each process must free the shared memory !!
  82.     FreeNamedSharedMem(SharedMemName);
  83. End;
  84.  
  85. Procedure TDragDropForm.ListBox1OnDragDrop (Sender: TObject; Source: TObject;
  86.                                             X: LongInt; Y: LongInt);
  87. Var SharedMem:PDragDropInfo;
  88.     t:Longint;
  89.     Temp:^String;
  90. Begin
  91.     //Look if the target is valid for us...
  92.     If ((Source Is TExternalDragDropObject)And
  93.         (TExternalDragDropObject(Source).SourceWindow<>ListBox1.Handle)And
  94.         (TExternalDragDropObject(Source).SourceType=drtString)And
  95.         (TExternalDragDropObject(Source).SourceString=DragSourceId)) Then
  96.     Begin  //accepted
  97.        If not AccessNamedSharedMem(TExternalDragDropObject(Source).SourceFileName,
  98.                                    SharedMem) Then exit;  //some error
  99.  
  100.        Temp:=@SharedMem^.Items;
  101.        For t:=1 To SharedMem^.Count Do
  102.        Begin
  103.           ListBox1.Items.Add(Temp^);
  104.           inc(Temp,length(Temp^)+1); //next item
  105.        End;
  106.  
  107.        //each process must free the shared memory !!
  108.        FreeNamedSharedMem(TExternalDragDropObject(Source).SourceFileName);
  109.     End;
  110. End;
  111.  
  112. Procedure TDragDropForm.ListBox1OnDragOver (Sender: TObject; Source: TObject;
  113.                                             X: LongInt; Y: LongInt; State: TDragState; Var Accept: Boolean);
  114. Begin
  115.     //Look if we can accept the target
  116.     Accept:=((Source Is TExternalDragDropObject)And
  117.              (TExternalDragDropObject(Source).SourceWindow<>ListBox1.Handle)And
  118.              (TExternalDragDropObject(Source).SourceType=drtString)And
  119.              (TExternalDragDropObject(Source).SourceString=DragSourceId));
  120. End;
  121.  
  122. Procedure TDragDropForm.ListBox1OnCanDrag (Sender: TObject; X: LongInt;
  123.                                            Y: LongInt; Var Accept: Boolean);
  124. Begin
  125.    //Look if dragging is allowed
  126.    Accept:=ListBox1.SelCount>0;
  127. End;
  128.  
  129. Procedure TDragDropForm.ListBox1OnStartDrag (Sender: TObject;
  130.                                              Var DragData: TDragDropData);
  131. Var t:Longint;
  132.     MemLen:LongWord;
  133.     SharedMem:PDragDropInfo;
  134.     Temp:^String;
  135. Begin
  136.    //Look how much memory we need
  137.    MemLen:=Sizeof(TDragDropInfo)-sizeof(TDragDropItem);
  138.    For t:=0 To ListBox1.Items.Count-1 Do
  139.     If ListBox1.Selected[t] Then inc(MemLen,length(ListBox1.Items[t])+1);
  140.  
  141.    //allocate the memory. We need shared memory because we want to give
  142.    //the information to another process
  143.    //free the shared memory
  144.    GetNamedSharedMem(SharedMemName,SharedMem,MemLen);
  145.  
  146.    SharedMem^.Count:=0;
  147.    Temp:=@SharedMem^.Items;  //start of variable information
  148.    For t:=0 To ListBox1.Items.Count-1 Do
  149.      If ListBox1.Selected[t] Then
  150.      Begin
  151.           Inc(SharedMem^.Count);
  152.           Temp^:=ListBox1.Items[t];
  153.           inc(Temp,length(Temp^)+1);  //next entry
  154.      End;
  155.  
  156.    //Fill up the DragData structure
  157.    DragData.SourceWindow := Handle;
  158.    DragData.SourceType := drtString;
  159.    DragData.SourceString:=DragSourceId;
  160.    DragData.RenderType := drmSibyl;
  161.    DragData.ContainerName := '';
  162.    DragData.SourceFileName := SharedMemName;
  163.    DragData.TargetFileName := '';
  164.    DragData.SupportedOps := [doMoveable];
  165.    DragData.DragOperation := doMove;
  166. End;
  167.  
  168. Initialization
  169.   RegisterClasses ([TDragDropForm, TLabel, TListBox, TBitBtn]);
  170. End.
  171.