home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1999 March
/
Chip_1999-03_cd.bin
/
zkuste
/
delphi
/
INFO
/
DI9805RS.ZIP
/
Thread.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1998-01-01
|
5KB
|
179 lines
unit Thread;
interface
uses
Windows, Messages, SysUtils, Graphics, Controls,
Forms, Dialogs, Menus, StdCtrls, Classes, ExtCtrls;
type
// The linked list cells.
PEmpCell = ^TEmPEmpCell;
TEmPEmpCell = record
LastName : String[20];
FirstName : String[20];
ID : Longint;
NextName : PEmpCell; // Next cell in name order.
NextID : PEmpCell; // Next cell in ID order.
end;
TThreadedListForm = class(TForm)
OrderOptions: TRadioGroup;
procedure FormCreate(Sender: TObject);
procedure Insert(last_name, first_name : String; emp_id : Longint);
procedure DrawList;
procedure FormPaint(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure OrderOptionsClick(Sender: TObject);
private
{ Private declarations }
top_sentinel : TEmPEmpCell;
bottom_sentinel : TEmPEmpCell;
public
{ Public declarations }
end;
var
ThreadedListForm: TThreadedListForm;
implementation
{$R *.DFM}
// Initialize the threaded list.
procedure TThreadedListForm.FormCreate(Sender: TObject);
begin
// Initialize the sentinels.
top_sentinel.NextName := @bottom_sentinel;
top_sentinel.NextID := @bottom_sentinel;
top_sentinel.LastName := '';
top_sentinel.FirstName := '';
top_sentinel.ID := -1;
bottom_sentinel.NextName := nil;
bottom_sentinel.NextID := nil;
bottom_sentinel.LastName := #255;
bottom_sentinel.FirstName := #255;
bottom_sentinel.ID := 2147483647;
// Create some data.
Insert('Anderson', 'John', 712);
Insert('Baker', 'Wendy', 132);
Insert('Cedras', 'Alice', 621);
Insert('Davis', 'Bill', 880);
Insert('Evans', 'Marvin', 204);
Insert('Fair', 'George', 492);
Insert('Gant', 'Cindy', 381);
Insert('Hennesey', 'Linda', 509);
Insert('Iverson', 'Sharon', 948);
end;
// Add a new cell after the selected cell.
procedure TThreadedListForm.Insert(last_name, first_name : String; emp_id : Longint);
var
after_me, next_cell, new_cell : PEmpCell;
new_value, next_value : String;
begin
// Create the new cell.
New(new_cell);
new_cell^.LastName := last_name;
new_cell^.FirstName := first_name;
new_cell^.ID := emp_id;
// Insert in the name thread.
new_value := last_name + ',' + first_name;
after_me := @top_sentinel;
repeat
next_cell := after_me^.NextName;
next_value := next_cell^.LastName + ',' +
next_cell^.FirstName;
if (next_value >= new_value) then break;
after_me := next_cell;
until (False);
new_cell^.NextName := next_cell;
after_me^.NextName := new_cell;
// Insert in the ID thread.
after_me := @top_sentinel;
repeat
next_cell := after_me^.NextID;
if (next_cell^.ID >= emp_id) then break;
after_me := next_cell;
until (False);
new_cell^.NextID := next_cell;
after_me^.NextID := new_cell;
end;
// Display the list, highlighting the selected item.
procedure TThreadedListForm.DrawList;
const
HGT = 17;
var
cell_ptr : PEmpCell;
x, y : Integer;
rect : TRect;
begin
// Clear the form.
rect.Left := 0;
rect.Top := 0;
rect.Right := ClientWidth;
rect.Bottom := ClientHeight;
Canvas.Brush.Color := clLtGray;
Canvas.FillRect(rect);
// Display the list items.
x := OrderOptions.Left;
y := OrderOptions.Top + OrderOptions.Height + 5;
if (OrderOptions.ItemIndex = 0) then
cell_ptr := top_sentinel.NextName // Order by name.
else
cell_ptr := top_sentinel.NextID; // Order by ID.
while (cell_ptr <> @bottom_sentinel) do
begin
// Display the text.
Canvas.TextOut(x, y,
Format('%-6d', [cell_ptr^.ID]) +
cell_ptr^.LastName + ', ' +
cell_ptr^.FirstName);
y := y + HGT;
// Move to the next cell.
if (OrderOptions.ItemIndex = 0) then
cell_ptr := cell_ptr^.NextName // Order by name.
else
cell_ptr := cell_ptr^.NextID; // Order by ID.
end;
end;
// Redraw the list.
procedure TThreadedListForm.FormPaint(Sender: TObject);
begin
DrawList;
end;
// Free all the linked list memory.
// This doesn't matter for this example program. It would
// be important if the program created and destroyed
// many forms.
procedure TThreadedListForm.FormDestroy(Sender: TObject);
var
target : PEmpCell;
begin
while (top_sentinel.NextName <> @bottom_sentinel) do
begin
target := top_sentinel.NextName;
top_sentinel.NextName := target^.NextName;
Dispose(target);
end;
end;
// Redraw using the new ordering.
procedure TThreadedListForm.OrderOptionsClick(Sender: TObject);
begin
DrawList;
end;
end.