home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d6 / RX275D6.ZIP / Units / BOXPROCS.PAS < prev    next >
Pascal/Delphi Source File  |  1999-10-12  |  9KB  |  294 lines

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