home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sibdemo3.zip / SOURCE.DAT / SOURCE / ADDON / EDITLIST.PAS < prev    next >
Pascal/Delphi Source File  |  1998-01-24  |  13KB  |  544 lines

  1. Unit EditList;
  2.  
  3.  
  4. Interface
  5.  
  6. Uses
  7.   SysUtils, Classes, Forms, Buttons, StdCtrls, ExtCtrls, XplorBtn;
  8.  
  9.  
  10. Type
  11.   TEditBrowseEvent = Procedure(Sender:TObject;Var Item:String) Of Object;
  12.  
  13.   TEditListBox = Class(TControl)
  14.     Private
  15.       Edit:TEdit;
  16.       BtnBrowse:TSpeedButton;
  17.       FEditIndex:LongInt;
  18.       FIgnoreExit:Boolean;
  19.       FOnEnter:TNotifyEvent;
  20.       FOnExit:TNotifyEvent;
  21.       FOnBrowse:TEditBrowseEvent;
  22.       Function GetItems:TStrings;
  23.       Procedure SetItems(AStrings:TStrings);
  24.       Function GetCaption:String;
  25.       Procedure SetCaption(Value:String);
  26.       Function GetHorzScroll:Boolean;
  27.       Procedure SetHorzScroll(Value:Boolean);
  28.       Function GetItemIndex:LongInt;
  29.       Procedure SetItemIndex(Value:LongInt);
  30.       Function GetTopIndex:LongInt;
  31.       Procedure SetTopIndex(Value:LongInt);
  32.       Procedure SetHint(Value:String);
  33.       Procedure EvBtnInsert(Sender:TObject);
  34.       Procedure EvBtnDelete(Sender:TObject);
  35.       Procedure EvBtnUp(Sender:TObject);
  36.       Procedure EvBtnDown(Sender:TObject);
  37.       Procedure EvEditExit(Sender:TObject);
  38.       Procedure EvEditScan(Sender:TObject;Var KeyCode:TKeyCode);
  39.       Procedure EvItemFocus(Sender:TObject;Index:LongInt);
  40.       Procedure EvItemSelect(Sender:TObject;Index:LongInt);
  41.       Procedure EvItemKeyPress(Sender:TObject;Var key:Char);
  42.       Procedure EvItemScan(Sender:TObject;Var KeyCode:TKeyCode);
  43.       Procedure EvListEnter(Sender:TObject);
  44.       Procedure EvListExit(Sender:TObject);
  45.       Procedure EvBrowse(Sender:TObject);
  46.       Procedure WriteBack;
  47.       Procedure DoEdit(Index:LongInt);
  48.     Protected
  49.       ListBox:TListBox;
  50.       Panel:TPanel;
  51.       BtnNew:TExplorerButton;
  52.       BtnDel:TExplorerButton;
  53.       BtnUp:TExplorerButton;
  54.       BtnDown:TExplorerButton;
  55.       Procedure SetupComponent;Override;
  56.       Procedure SetupShow;Override;
  57.       Procedure SetFocus;Override;
  58.     Public
  59.       Function WriteSCUResource(Stream:TResourceStream):Boolean;Override;
  60.       Procedure ReadSCUResource(Const ResName:TResourceName;Var Data;DataLen:LongInt);Override;
  61.       Procedure Clear;Virtual;
  62.       Procedure BeginUpdate;
  63.       Procedure EndUpdate;
  64.       Property ItemIndex:LongInt Read GetItemIndex Write SetItemIndex;
  65.       Property TopIndex:LongInt Read GetTopIndex Write SetTopIndex;
  66.       Property XAlign;
  67.       Property XStretch;
  68.       Property YAlign;
  69.       Property YStretch;
  70.     Published
  71.       Property Align;
  72.       Property Caption:String Read GetCaption write SetCaption;
  73.       Property Color;
  74.       Property Enabled;
  75.       Property Font;
  76.       Property Hint Write SetHint;
  77.       Property HorzScroll:Boolean Read GetHorzScroll Write SetHorzScroll;
  78.       Property Items:TStrings Read GetItems Write SetItems;
  79.       Property ParentColor;
  80.       Property ParentPenColor;
  81.       Property ParentFont;
  82.       Property ParentShowHint;
  83.       Property PenColor;
  84.       Property PopupMenu;
  85.       Property ShowHint;
  86.       Property TabOrder;
  87.       Property TabStop;
  88.       Property Visible;
  89.       Property ZOrder;
  90.  
  91.       Property OnBrowse:TEditBrowseEvent Read FOnBrowse Write FOnBrowse;
  92.       Property OnEnter:TNotifyEvent Read FOnEnter Write FOnEnter;
  93.       Property OnExit:TNotifyEvent Read FOnExit Write FOnExit;
  94.       Property OnSetupShow;
  95.   End;
  96.  
  97.  
  98. Exports
  99.   TEditListBox,'User','';
  100.  
  101.  
  102. Implementation
  103.  
  104. {$r EditList}
  105.  
  106. Const
  107.   BtnSize=26;
  108.  
  109.  
  110. Procedure TEditListBox.SetupComponent;
  111. Begin
  112.   Inherited SetupComponent;
  113.  
  114.   Name := 'EditListBox';
  115.   Width := 220;
  116.   Height := 150;
  117.   Color := clEntryField;
  118.   Ownerdraw := False;
  119.  
  120.   FEditIndex := -1;
  121.   FIgnoreExit := False;
  122.  
  123.   ListBox := InsertListBox(SELF,0,0,Width,Height-(BtnSize+2),'');
  124.   ListBox.Align := alFrame;
  125.   ListBox.ParentColor := True;
  126.   ListBox.ParentPenColor := True;
  127.   ListBox.OnItemFocus := EvItemFocus;
  128.   ListBox.OnItemSelect := EvItemSelect;
  129.   ListBox.OnKeyPress := EvItemKeyPress;
  130.   ListBox.OnScan := EvItemScan;
  131.   ListBox.OnEnter := EvListEnter;
  132.   ListBox.OnExit := EvListExit;
  133.   Include(ListBox.ComponentState, csDetail);
  134.  
  135.   Panel := InsertPanel(SELF,0,Height-(BtnSize+2),Width,(BtnSize+2),
  136.                        bvNone,bvLowered,1,' EditListBox');
  137.   Panel.Align := alTop;
  138.   Panel.Alignment := taLeftJustify;
  139.   Panel.Color := clLtGray;
  140.   Panel.ParentPenColor := True;
  141.   Include(Panel.ComponentState, csDetail);
  142.  
  143.   BtnNew.Create(Panel);
  144.   BtnNew.Parent := Panel;
  145.   BtnNew.SetWindowPos(Panel.Width-4*BtnSize-1,1,BtnSize,BtnSize);
  146.   BtnNew.Align := alFixedRightBottom;
  147.   BtnNew.Hint := 'New';
  148.   BtnNew.NumGlyphs :=2 ;
  149.   BtnNew.Glyph.LoadFromResourceName('BmpBtnNew');
  150.   BtnNew.OnClick := EvBtnInsert;
  151.   Include(BtnNew.ComponentState, csDetail);
  152.  
  153.   BtnDel.Create(Panel);
  154.   BtnDel.Parent := Panel;
  155.   BtnDel.SetWindowPos(Panel.Width-3*BtnSize-1,1,BtnSize,BtnSize);
  156.   BtnDel.Align := alFixedRightBottom;
  157.   BtnDel.NumGlyphs := 2;
  158.   BtnDel.Hint := 'Del';
  159.   BtnDel.Glyph.LoadFromResourceName('BmpBtnDel');
  160.   BtnDel.OnClick := EvBtnDelete;
  161.   Include(BtnDel.ComponentState, csDetail);
  162.  
  163.   BtnUp.Create(Panel);
  164.   BtnUp.Parent := Panel;
  165.   BtnUp.SetWindowPos(Panel.Width-2*BtnSize-1,1,BtnSize,BtnSize);
  166.   BtnUp.Align := alFixedRightBottom;
  167.   BtnUp.Hint := 'Up';
  168.   BtnUp.NumGlyphs := 2;
  169.   BtnUp.Glyph.LoadFromResourceName('BmpBtnUp');
  170.   BtnUp.OnClick := EvBtnUp;
  171.   Include(BtnUp.ComponentState, csDetail);
  172.  
  173.   BtnDown.Create(Panel);
  174.   BtnDown.Parent := Panel;
  175.   BtnDown.SetWindowPos(Panel.Width-BtnSize-1,1,BtnSize,BtnSize);
  176.   BtnDown.Align := alFixedRightBottom;
  177.   BtnDown.Hint := 'Down';
  178.   BtnDown.NumGlyphs := 2;
  179.   BtnDown.Glyph.LoadFromResourceName('BmpBtnDown');
  180.   BtnDown.OnClick := EvBtnDown;
  181.   Include(BtnDown.ComponentState, csDetail);
  182.  
  183.   Edit.Create(ListBox);
  184.   Edit.Parent := ListBox;
  185.   Edit.Visible := False;
  186.   Edit.OnExit := EvEditExit;
  187.   Edit.OnScan := EvEditScan;
  188.   Include(Edit.ComponentState, csDetail);
  189.   Include(Edit.ComponentState, csAcceptsControls);
  190.  
  191.   BtnBrowse := InsertSpeedButton(Edit,0,0,0,0,0,'...','Browse');
  192.   BtnBrowse.OnClick := EvBrowse;
  193.   Include(BtnBrowse.ComponentState, csDetail);
  194. End;
  195.  
  196.  
  197. Procedure TEditListBox.SetupShow;
  198. Begin
  199.   Inherited SetupShow;
  200.  
  201.   IF Not Designed Then
  202.   Begin
  203.     ListBox.Items.Add('');
  204.   End;
  205. End;
  206.  
  207.  
  208. Procedure TEditListBox.SetFocus;
  209. Begin
  210.      Inherited SetFocus;
  211.  
  212.      If Not Designed Then ListBox.CaptureFocus;
  213. End;
  214.  
  215.  
  216. Function TEditListBox.WriteSCUResource(Stream:TResourceStream):Boolean;
  217. Var  aText:PChar;
  218. Begin
  219.      Result := Inherited WriteSCUResource(Stream);
  220.      If Not Result Then Exit;
  221.  
  222.      aText := ListBox.Items.GetText;
  223.      If aText <> Nil Then
  224.      Begin
  225.           Result := Stream.NewResourceEntry(rnItems,aText^,Length(aText^)+1);
  226.           StrDispose(aText);
  227.      End;
  228. End;
  229.  
  230.  
  231. Procedure TEditListBox.ReadSCUResource(Const ResName:TResourceName;Var Data;DataLen:LongInt);
  232. Var  aText:PChar;
  233. Begin
  234.      If ResName = rnItems Then
  235.      Begin
  236.           aText := @Data;
  237.           ListBox.Items.SetText(aText);
  238.      End
  239.      Else Inherited ReadSCUResource(ResName,Data,DataLen)
  240. End;
  241.  
  242.  
  243. Function TEditListBox.GetItemIndex:LongInt;
  244. Begin
  245.   Result := ListBox.ItemIndex;
  246. End;
  247.  
  248.  
  249. Procedure TEditListBox.SetItemIndex(Value:LongInt);
  250. Begin
  251.   ListBox.ItemIndex := Value;
  252. End;
  253.  
  254.  
  255. Function TEditListBox.GetTopIndex:LongInt;
  256. Begin
  257.   Result := ListBox.TopIndex;
  258. End;
  259.  
  260.  
  261. Procedure TEditListBox.SetTopIndex(Value:LongInt);
  262. Begin
  263.   ListBox.TopIndex := Value;
  264. End;
  265.  
  266.  
  267. Procedure TEditListBox.SetHint(Value:String);
  268. Begin
  269.   Panel.Hint := Value;
  270.   ListBox.Hint := Value;
  271.   TControl.Hint := Value;
  272. End;
  273.  
  274.  
  275. Procedure TEditListBox.Clear;
  276. Begin
  277.   ListBox.Clear;
  278. End;
  279.  
  280.  
  281. Procedure TEditListBox.BeginUpdate;
  282. Begin
  283.   ListBox.BeginUpdate;
  284. End;
  285.  
  286.  
  287. Procedure TEditListBox.EndUpdate;
  288. Begin
  289.   ListBox.EndUpdate;
  290. End;
  291.  
  292.  
  293. Function TEditListBox.GetItems:TStrings;
  294. Begin
  295.   Result := ListBox.Items;
  296. End;
  297.  
  298.  
  299. Procedure TEditListBox.SetItems(AStrings:TStrings);
  300. Begin
  301.   ListBox.Items := AStrings;
  302. End;
  303.  
  304.  
  305. Function TEditListBox.GetCaption:String;
  306. Begin
  307.   Result := Panel.Caption;
  308. End;
  309.  
  310.  
  311. Procedure TEditListBox.SetCaption(Value:String);
  312. Begin
  313.   Panel.Caption := Value;
  314. End;
  315.  
  316.  
  317. Function TEditListBox.GetHorzScroll:Boolean;
  318. Begin
  319.   Result := ListBox.HorzScroll;
  320. End;
  321.  
  322.  
  323. Procedure TEditListBox.SetHorzScroll(Value:Boolean);
  324. Begin
  325.   ListBox.HorzScroll := Value;
  326. End;
  327.  
  328.  
  329. Procedure TEditListBox.EvBtnInsert(Sender:TObject);
  330. Var
  331.   i:LongInt;
  332. Begin
  333.   If Edit.Showing Then WriteBack;
  334.  
  335.   If (ListBox.ItemIndex >= 0) And (ListBox.ItemIndex < ListBox.Items.Count) Then
  336.   Begin
  337.     i := ListBox.ItemIndex;
  338.     ListBox.Items.Insert(ListBox.ItemIndex,'');
  339.     ListBox.ItemIndex := i;
  340.   End
  341.   Else i := ListBox.Items.Add('');
  342.  
  343.   DoEdit(i);
  344. End;
  345.  
  346.  
  347. Procedure TEditListBox.EvBtnDelete(Sender:TObject);
  348. Var
  349.   i:LongInt;
  350. Begin
  351.   If Edit.Showing Then WriteBack;
  352.  
  353.   If (ListBox.ItemIndex >= 0) And (ListBox.ItemIndex < ListBox.Items.Count) Then
  354.   Begin
  355.     i := ListBox.ItemIndex;
  356.     ListBox.Items.Delete(i);
  357.     ListBox.ItemIndex := i;
  358.   End;
  359. End;
  360.  
  361.  
  362. Procedure TEditListBox.EvBtnUp(Sender:TObject);
  363. Var
  364.   i:LongInt;
  365. Begin
  366.   If Edit.Showing Then WriteBack;
  367.  
  368.   If (ListBox.ItemIndex >= 1) And (ListBox.ItemIndex < ListBox.Items.Count) Then
  369.   Begin
  370.     i := ListBox.ItemIndex;
  371.     ListBox.Items.Exchange(i,i-1);
  372.     ListBox.ItemIndex := i-1;
  373.   End;
  374. End;
  375.  
  376.  
  377. Procedure TEditListBox.EvBtnDown(Sender:TObject);
  378. Var
  379.   i:LongInt;
  380. Begin
  381.   If Edit.Showing Then WriteBack;
  382.  
  383.   If (ListBox.ItemIndex >= 0) And (ListBox.ItemIndex < ListBox.Items.Count-1) Then
  384.   Begin
  385.     i := ListBox.ItemIndex;
  386.     ListBox.Items.Exchange(i,i+1);
  387.     ListBox.ItemIndex := i+1;
  388.   End;
  389. End;
  390.  
  391.  
  392. Procedure TEditListBox.WriteBack;
  393. Begin
  394.   If (FEditIndex >= 0) And (FEditIndex < ListBox.Items.Count) Then
  395.   Begin
  396.     ListBox.Items[FEditIndex] := Edit.Caption;
  397.     ListBox.ItemIndex := FEditIndex;
  398.  
  399.     If FEditIndex = ListBox.Items.Count-1 Then
  400.       If Edit.Caption <> '' Then ListBox.Items.Add('');
  401.   End;
  402.  
  403.  
  404.   Edit.Visible := False;
  405.   ListBox.Focus;
  406. End;
  407.  
  408.  
  409. Procedure TEditListBox.DoEdit(Index:LongInt);
  410. Var
  411.   rc:TRect;
  412. Begin
  413.   rc := ListBox.ItemRect(Index);
  414.   Edit.SetWindowPos(1,rc.Bottom-2,ListBox.Width-2,Edit.Height);
  415.   Edit.Caption := ListBox.Items[Index];
  416.   Edit.Visible := True;
  417.   Edit.Focus;
  418.   Edit.SelectAll;
  419.  
  420.  
  421.   If @FOnBrowse <> Nil Then
  422.   Begin
  423.     BtnBrowse.SetWindowPos(Edit.Width-25-2,2,25,Edit.Height-4);
  424.     BtnBrowse.Parent := Edit;
  425.   End
  426.   Else BtnBrowse.Parent := Nil;
  427.  
  428.  
  429.   FEditIndex := Index;
  430. End;
  431.  
  432.  
  433. Procedure TEditListBox.EvEditExit(Sender:TObject);
  434. Begin
  435.   If FIgnoreExit Then Exit;
  436.  
  437.   If Edit.Showing Then WriteBack;
  438. End;
  439.  
  440.  
  441. Procedure TEditListBox.EvEditScan(Sender:TObject;Var KeyCode:TKeyCode);
  442. Begin
  443.   Case KeyCode Of
  444.     kbCUp:
  445.     Begin
  446.       WriteBack;
  447.       If ListBox.ItemIndex > 0 Then ListBox.ItemIndex := ListBox.ItemIndex -1;
  448.       KeyCode := 0;
  449.     End;
  450.     kbCDown:
  451.     Begin
  452.       WriteBack;
  453.       ListBox.ItemIndex := ListBox.ItemIndex +1;
  454.       KeyCode := 0;
  455.     End;
  456.     kbCR
  457.     {$IFDEF OS2}
  458.     ,kbEnter
  459.     {$ENDIF}
  460.     :
  461.     Begin
  462.       WriteBack;
  463.       KeyCode := 0;
  464.     End;
  465.     kbEsc:
  466.     Begin
  467.       Edit.Visible := False;
  468.       ListBox.Focus;
  469.       KeyCode := 0;
  470.     End;
  471.   End;
  472. End;
  473.  
  474.  
  475. Procedure TEditListBox.EvItemFocus(Sender:TObject;Index:LongInt);
  476. Begin
  477.   If Edit.Showing Then WriteBack;
  478. End;
  479.  
  480.  
  481. Procedure TEditListBox.EvItemSelect(Sender:TObject;Index:LongInt);
  482. Begin
  483.   DoEdit(Index);
  484. End;
  485.  
  486.  
  487. Procedure TEditListBox.EvItemKeyPress(Sender:TObject;Var key:Char);
  488. Begin
  489.   If (ListBox.ItemIndex >= 0) And (ListBox.ItemIndex < ListBox.Items.Count) Then
  490.   Begin
  491.     DoEdit(ListBox.ItemIndex);
  492.     Edit.Caption := key;
  493.     key := #0;
  494.   End;
  495. End;
  496.  
  497.  
  498. Procedure TEditListBox.EvItemScan(Sender:TObject;Var KeyCode:TKeyCode);
  499. Begin
  500.   Case KeyCode Of
  501.     kbIns:
  502.     Begin
  503.       EvBtnInsert(BtnNew);
  504.       KeyCode := 0;
  505.     End;
  506.     kbDel:
  507.     Begin
  508.       EvBtnDelete(BtnDel);
  509.       KeyCode := 0;
  510.     End;
  511.   End;
  512. End;
  513.  
  514.  
  515. Procedure TEditListBox.EvListEnter(Sender:TObject);
  516. Begin
  517.   If FOnEnter <> Nil Then FOnEnter(Self);
  518. End;
  519.  
  520.  
  521. Procedure TEditListBox.EvListExit(Sender:TObject);
  522. Begin
  523.   If FOnExit <> Nil Then FOnExit(Self);
  524. End;
  525.  
  526.  
  527. Procedure TEditListBox.EvBrowse(Sender:TObject);
  528. Var
  529.   s:String;
  530. Begin
  531.   FIgnoreExit := True;
  532.   s := Edit.Caption;
  533.   If FOnBrowse <> Nil Then FOnBrowse(Self, s);
  534.   Edit.Caption := s;
  535.   WriteBack;
  536.   FIgnoreExit := False;
  537. End;
  538.  
  539.  
  540.  
  541. Begin
  542. End.
  543.  
  544.