lazarus/ide/viewunit_dlg.pp
paul 26a8d08b3b improve view unit/form dialog
git-svn-id: trunk@13683 -
2008-01-09 10:15:05 +00:00

260 lines
7.3 KiB
ObjectPascal

{ $Id$ }
{
/***************************************************************************
ViewUnit_dlg.pp
---------------
TViewUnit is the application dialog for displaying all units in a project.
It gets used for the "View Units", "View Forms" and "Remove from Project"
menu items.
Initial Revision : Sat Feb 19 17:42 CST 1999
***************************************************************************/
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
***************************************************************************
}
unit ViewUnit_Dlg;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes, Controls, Forms, Dialogs, LResources, Buttons, StdCtrls,
LazarusIdeStrConsts, LCLType, IDEWindowIntf, IDEContextHelpEdit;
type
TViewUnitsEntry = class
public
Name: string;
ID: integer;
Selected: boolean;
constructor Create(const AName: string; AnID: integer; ASelected: boolean);
end;
{ TViewUnitDialog }
TViewUnitDialog = class(TForm)
HelpButton: TBitBtn;
Edit: TEdit;
ListBox: TListBox;
btnOK: TBitBtn;
btnCancel: TBitBtn;
MultiSelectCheckBox: TCheckBox;
procedure EditChange(Sender: TObject);
procedure EditKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure HelpButtonClick(Sender: TObject);
Procedure btnOKClick(Sender :TObject);
Procedure btnCancelClick(Sender :TObject);
procedure ListboxClick(Sender: TObject);
procedure ListboxKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure MultiselectCheckBoxClick(Sender :TObject);
private
FBlockEditChange: Boolean;
FBlockListBoxChange: Boolean;
public
constructor Create(TheOwner: TComponent); override;
end;
function ShowViewUnitsDlg(Entries: TStringList; MultiSelect: boolean;
const Caption: string): TModalResult;
// Entries is a list of TViewUnitsEntry(s)
implementation
function ShowViewUnitsDlg(Entries: TStringList; MultiSelect: boolean;
const Caption: string): TModalResult;
var
ViewUnitDialog: TViewUnitDialog;
i: integer;
begin
ViewUnitDialog:=TViewUnitDialog.Create(nil);
try
ViewUnitDialog.Caption:=Caption;
ViewUnitDialog.MultiselectCheckBox.Enabled:=MultiSelect;
ViewUnitDialog.MultiselectCheckBox.Checked:=MultiSelect;
ViewUnitDialog.ListBox.Multiselect:=ViewUnitDialog.MultiselectCheckBox.Checked;
with ViewUnitDialog.ListBox.Items do begin
BeginUpdate;
Clear;
for i:=0 to Entries.Count-1 do
Add(TViewUnitsEntry(Entries.Objects[i]).Name);
EndUpdate;
end;
for i:=0 to Entries.Count-1 do
ViewUnitDialog.ListBox.Selected[i]:=TViewUnitsEntry(Entries.Objects[i]).Selected;
Result:=ViewUnitDialog.ShowModal;
if Result=mrOk then begin
for i:=0 to Entries.Count-1 do begin
TViewUnitsEntry(Entries.Objects[i]).Selected:=ViewUnitDialog.ListBox.Selected[i];
end;
end;
finally
ViewUnitDialog.Free;
end;
end;
function SearchItem(Items: TStrings; Text: String): Integer;
var
i: integer;
begin
// Items can be unsorted => use simple traverse
Result := -1;
Text := LowerCase(Text);
for i := 0 to Items.Count - 1 do
if Pos(Text, LowerCase(Items[i])) = 1 then
begin
Result := i;
break;
end;
end;
{ TViewUnitsEntry }
constructor TViewUnitsEntry.Create(const AName: string; AnID: integer;
ASelected: boolean);
begin
inherited Create;
Name := AName;
ID := AnID;
Selected := ASelected;
end;
{ TViewUnitDialog }
constructor TViewUnitDialog.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
IDEDialogLayoutList.ApplyLayout(Self,450,300);
btnOK.Caption := lisOkBtn;
btnOk.Left := ClientWidth-btnOk.Width-5;
btnCancel.Caption := dlgCancel;
btnCancel.Left := btnOk.Left;
CancelControl := btnCancel;
MultiSelectCheckBox.Caption := dlgMultiSelect;
MultiSelectCheckBox.Left := btnOk.Left;
FBlockEditChange := False;
FBlockListBoxChange := False;
end;
Procedure TViewUnitDialog.btnOKClick(Sender : TOBject);
Begin
IDEDialogLayoutList.SaveLayout(Self);
ModalResult := mrOK;
End;
procedure TViewUnitDialog.HelpButtonClick(Sender: TObject);
begin
ShowContextHelpForIDE(Self);
end;
procedure TViewUnitDialog.EditKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure MoveItemIndex(d: integer); inline;
begin
d := ListBox.ItemIndex + d;
if (d >= 0) and (d < ListBox.Items.Count) then
begin
FBlockEditChange := True;
FBlockListBoxChange := True;
ListBox.ItemIndex := d;
Edit.Text := ListBox.Items[d];
Edit.SelectAll;
FBlockListBoxChange := False;
FBlockEditChange := False;
end;
end;
begin
case Key of
VK_UP:
begin
MoveItemIndex(-1);
Key := 0;
end;
VK_DOWN:
begin
MoveItemIndex(1);
Key := 0;
end;
VK_RETURN: btnOKClick(nil);
end;
end;
procedure TViewUnitDialog.EditChange(Sender: TObject);
var
Index: Integer;
begin
if FBlockEditChange then
Exit;
FBlockListBoxChange := True;
Index := SearchItem(ListBox.Items, Edit.Text);
// WriteLn('Index = ', Index);
ListBox.ItemIndex := Index;
if ListBox.MultiSelect then
begin
ListBox.ClearSelection;
if Index <> -1 then
ListBox.Selected[Index] := True;
end;
FBlockListBoxChange := False;
end;
Procedure TViewUnitDialog.btnCancelClick(Sender : TOBject);
Begin
IDEDialogLayoutList.SaveLayout(Self);
ModalResult := mrCancel;
end;
procedure TViewUnitDialog.ListboxClick(Sender: TObject);
begin
if FBlockListBoxChange then
Exit;
FBlockEditChange := True;
if ListBox.ItemIndex <> -1 then
Edit.Text := ListBox.Items[ListBox.ItemIndex]
else
Edit.Text := '';
FBlockEditChange := False;
end;
procedure TViewUnitDialog.ListboxKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_RETURN then
btnOKClick(nil);
end;
procedure TViewUnitDialog.MultiselectCheckBoxClick(Sender :TObject);
begin
ListBox.Multiselect := MultiselectCheckBox.Checked;
end;
initialization
{$I viewunit_dlg.lrs}
end.