home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 June / Chip_2002-06_cd1.bin / zkuste / delphi / kolekce / d6 / rxlibsetup.exe / {app} / units / BOXPROCS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  2002-02-19  |  8.6 KB  |  297 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {         Delphi VCL Extensions (RX)                    }
  4. {                                                       }
  5. {         Copyright (c) 2001,2002 SGB Software          }
  6. {         Copyright (c) 1997, 1998 Fedor Koshevnikov,   }
  7. {                        Igor Pavluk and Serge Korolev  }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11.  
  12.  
  13. unit BoxProcs;
  14.  
  15. {$I RX.INC}
  16.  
  17. interface
  18.  
  19. uses Classes, Controls, StdCtrls, RxCtrls;
  20.  
  21. procedure BoxMoveSelectedItems(SrcList, DstList: TWinControl);
  22. procedure BoxMoveAllItems(SrcList, DstList: TWinControl);
  23. procedure BoxDragOver(List: TWinControl; Source: TObject;
  24.   X, Y: Integer; State: TDragState; var Accept: Boolean; Sorted: Boolean);
  25. procedure BoxMoveFocusedItem(List: TWinControl; DstIndex: Integer);
  26.  
  27. procedure BoxMoveSelected(List: TWinControl; Items: TStrings);
  28. procedure BoxSetItem(List: TWinControl; Index: Integer);
  29. function BoxGetFirstSelection(List: TWinControl): Integer;
  30. function BoxCanDropItem(List: TWinControl; X, Y: Integer;
  31.   var DragIndex: Integer): Boolean;
  32.  
  33. implementation
  34.  
  35. uses {$IFDEF WIN32} Windows {$ELSE} WinTypes, WinProcs {$ENDIF}, Graphics;
  36.  
  37. function BoxItems(List: TWinControl): TStrings;
  38. begin
  39.   if List is TCustomListBox then
  40.     Result := TCustomListBox(List).Items
  41.   else if List is TRxCustomListBox then
  42.     Result := TRxCustomListBox(List).Items
  43.   else Result := nil;
  44. end;
  45.  
  46. function BoxGetSelected(List: TWinControl; Index: Integer): Boolean;
  47. begin
  48.   if List is TCustomListBox then
  49.     Result := TCustomListBox(List).Selected[Index]
  50.   else if List is TRxCustomListBox then
  51.     Result := TRxCustomListBox(List).Selected[Index]
  52.   else Result := False;
  53. end;
  54.  
  55. procedure BoxSetSelected(List: TWinControl; Index: Integer; Value: Boolean);
  56. begin
  57.   if List is TCustomListBox then
  58.     TCustomListBox(List).Selected[Index] := Value
  59.   else if List is TRxCustomListBox then
  60.     TRxCustomListBox(List).Selected[Index] := Value;
  61. end;
  62.  
  63. function BoxGetItemIndex(List: TWinControl): Integer;
  64. begin
  65.   if List is TCustomListBox then
  66.     Result := TCustomListBox(List).ItemIndex
  67.   else if List is TRxCustomListBox then
  68.     Result := TRxCustomListBox(List).ItemIndex
  69.   else Result := LB_ERR;
  70. end;
  71.  
  72. {$IFNDEF WIN32}
  73. function BoxGetCanvas(List: TWinControl): TCanvas;
  74. begin
  75.   if List is TCustomListBox then
  76.     Result := TCustomListBox(List).Canvas
  77.   else if List is TRxCustomListBox then
  78.     Result := TRxCustomListBox(List).Canvas
  79.   else Result := nil;
  80. end;
  81. {$ENDIF}
  82.  
  83. procedure BoxSetItemIndex(List: TWinControl; Index: Integer);
  84. begin
  85.   if List is TCustomListBox then
  86.     TCustomListBox(List).ItemIndex := Index
  87.   else if List is TRxCustomListBox then
  88.     TRxCustomListBox(List).ItemIndex := Index;
  89. end;
  90.  
  91. function BoxMultiSelect(List: TWinControl): Boolean;
  92. begin
  93.   if List is TCustomListBox then
  94.     Result := TListBox(List).MultiSelect
  95.   else if List is TRxCustomListBox then
  96.     Result := TRxCheckListBox(List).MultiSelect
  97.   else Result := False;
  98. end;
  99.  
  100. function BoxSelCount(List: TWinControl): Integer;
  101. begin
  102.   if List is TCustomListBox then
  103.     Result := TCustomListBox(List).SelCount
  104.   else if List is TRxCustomListBox then
  105.     Result := TRxCustomListBox(List).SelCount
  106.   else Result := 0;
  107. end;
  108.  
  109. function BoxItemAtPos(List: TWinControl; Pos: TPoint;
  110.   Existing: Boolean): Integer;
  111. begin
  112.   if List is TCustomListBox then
  113.     Result := TCustomListBox(List).ItemAtPos(Pos, Existing)
  114.   else if List is TRxCustomListBox then
  115.     Result := TRxCustomListBox(List).ItemAtPos(Pos, Existing)
  116.   else Result := LB_ERR;
  117. end;
  118.  
  119. function BoxItemRect(List: TWinControl; Index: Integer): TRect;
  120. begin
  121.   if List is TCustomListBox then
  122.     Result := TCustomListBox(List).ItemRect(Index)
  123.   else if List is TRxCustomListBox then
  124.     Result := TRxCustomListBox(List).ItemRect(Index)
  125.   else FillChar(Result, SizeOf(Result), 0);
  126. end;
  127.  
  128. procedure BoxMoveSelected(List: TWinControl; Items: TStrings);
  129. var
  130.   I: Integer;
  131. begin
  132.   if BoxItems(List) = nil then Exit;
  133.   I := 0;
  134.   while I < BoxItems(List).Count do begin
  135.     if BoxGetSelected(List, I) then begin
  136.       Items.AddObject(BoxItems(List).Strings[I], BoxItems(List).Objects[I]);
  137.       BoxItems(List).Delete(I);
  138.     end
  139.     else Inc(I);
  140.   end;
  141. end;
  142.  
  143. function BoxGetFirstSelection(List: TWinControl): Integer;
  144. var
  145.   I: Integer;
  146. begin
  147.   Result := LB_ERR;
  148.   if BoxItems(List) = nil then Exit;
  149.   for I := 0 to BoxItems(List).Count - 1 do begin
  150.     if BoxGetSelected(List, I) then begin
  151.       Result := I;
  152.       Exit;
  153.     end;
  154.   end;
  155.   Result := LB_ERR;
  156. end;
  157.  
  158. procedure BoxSetItem(List: TWinControl; Index: Integer);
  159. var
  160.   MaxIndex: Integer;
  161. begin
  162.   if BoxItems(List) = nil then Exit;
  163.   with List do begin
  164.     if CanFocus then SetFocus;
  165.     MaxIndex := BoxItems(List).Count - 1;
  166.     if Index = LB_ERR then Index := 0
  167.     else if Index > MaxIndex then Index := MaxIndex;
  168.     if Index >= 0 then begin
  169.       if BoxMultiSelect(List) then BoxSetSelected(List, Index, True)
  170.       else BoxSetItemIndex(List, Index);
  171.     end;
  172.   end;
  173. end;
  174.  
  175. procedure BoxMoveSelectedItems(SrcList, DstList: TWinControl);
  176. var
  177.   Index, I, NewIndex: Integer;
  178. begin
  179.   Index := BoxGetFirstSelection(SrcList);
  180.   if Index <> LB_ERR then begin
  181.     BoxItems(SrcList).BeginUpdate;
  182.     BoxItems(DstList).BeginUpdate;
  183.     try
  184.       I := 0;
  185.       while I < BoxItems(SrcList).Count do begin
  186.         if BoxGetSelected(SrcList, I) then begin
  187.           NewIndex := BoxItems(DstList).AddObject(BoxItems(SrcList).Strings[I],
  188.             BoxItems(SrcList).Objects[I]);
  189.           if (SrcList is TRxCheckListBox) and (DstList is TRxCheckListBox) then
  190.           begin
  191.             TRxCheckListBox(DstList).State[NewIndex] :=
  192.               TRxCheckListBox(SrcList).State[I];
  193.           end;
  194.           BoxItems(SrcList).Delete(I);
  195.         end
  196.         else Inc(I);
  197.       end;
  198.       BoxSetItem(SrcList, Index);
  199.     finally
  200.       BoxItems(SrcList).EndUpdate;
  201.       BoxItems(DstList).EndUpdate;
  202.     end;
  203.   end;
  204. end;
  205.  
  206. procedure BoxMoveAllItems(SrcList, DstList: TWinControl);
  207. var
  208.   I, NewIndex: Integer;
  209. begin
  210.   for I := 0 to BoxItems(SrcList).Count - 1 do begin
  211.     NewIndex := BoxItems(DstList).AddObject(BoxItems(SrcList)[I],
  212.       BoxItems(SrcList).Objects[I]);
  213.     if (SrcList is TRxCheckListBox) and (DstList is TRxCheckListBox) then
  214.     begin
  215.       TRxCheckListBox(DstList).State[NewIndex] :=
  216.         TRxCheckListBox(SrcList).State[I];
  217.     end;
  218.   end;
  219.   BoxItems(SrcList).Clear;
  220.   BoxSetItem(SrcList, 0);
  221. end;
  222.  
  223. function BoxCanDropItem(List: TWinControl; X, Y: Integer;
  224.   var DragIndex: Integer): Boolean;
  225. var
  226.   Focused: Integer;
  227. begin
  228.   Result := False;
  229.   if (BoxSelCount(List) = 1) or (not BoxMultiSelect(List)) then begin
  230.     Focused := BoxGetItemIndex(List);
  231.     if Focused <> LB_ERR then begin
  232.       DragIndex := BoxItemAtPos(List, Point(X, Y), True);
  233.       if (DragIndex >= 0) and (DragIndex <> Focused) then begin
  234.         Result := True;
  235.       end;
  236.     end;
  237.   end;
  238. end;
  239.  
  240. procedure BoxDragOver(List: TWinControl; Source: TObject;
  241.   X, Y: Integer; State: TDragState; var Accept: Boolean; Sorted: Boolean);
  242. var
  243.   DragIndex: Integer;
  244.   R: TRect;
  245.  
  246.   procedure DrawItemFocusRect(Idx: Integer);
  247. {$IFDEF WIN32}
  248.   var
  249.     P: TPoint;
  250.     DC: HDC;
  251. {$ENDIF}
  252.   begin
  253.     R := BoxItemRect(List, Idx);
  254. {$IFDEF WIN32}
  255.     P := List.ClientToScreen(R.TopLeft);
  256.     R := Bounds(P.X, P.Y, R.Right - R.Left, R.Bottom - R.Top);
  257.     DC := GetDC(0);
  258.     DrawFocusRect(DC, R);
  259.     ReleaseDC(0, DC);
  260. {$ELSE}
  261.     BoxGetCanvas(List).DrawFocusRect(R);
  262. {$ENDIF}
  263.   end;
  264.  
  265. begin
  266.   if Source <> List then
  267.     Accept := (Source is TWinControl) or (Source is TRxCustomListBox)
  268.   else begin
  269.     if Sorted then Accept := False
  270.     else begin
  271.       Accept := BoxCanDropItem(List, X, Y, DragIndex);
  272.       if ((List.Tag - 1) = DragIndex) and (DragIndex >= 0) then begin
  273.         if State = dsDragLeave then begin
  274.           DrawItemFocusRect(List.Tag - 1);
  275.           List.Tag := 0;
  276.         end;
  277.       end
  278.       else begin
  279.         if List.Tag > 0 then DrawItemFocusRect(List.Tag - 1);
  280.         if DragIndex >= 0 then DrawItemFocusRect(DragIndex);
  281.         List.Tag := DragIndex + 1;
  282.       end;
  283.     end;
  284.   end;
  285. end;
  286.  
  287. procedure BoxMoveFocusedItem(List: TWinControl; DstIndex: Integer);
  288. begin
  289.   if (DstIndex >= 0) and (DstIndex < BoxItems(List).Count) then
  290.     if (DstIndex <> BoxGetItemIndex(List)) then begin
  291.       BoxItems(List).Move(BoxGetItemIndex(List), DstIndex);
  292.       BoxSetItem(List, DstIndex);
  293.     end;
  294. end;
  295.  
  296. end.
  297.