Changeset 8


Ignore:
Timestamp:
Jun 2, 2013, 10:46:40 PM (11 years ago)
Author:
chronos
Message:
  • Added: Implemented simple TEdit control which react to key press messages.
Location:
os/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • os/trunk/Applications/TestApplication.pas

    r7 r8  
    1212    Button: TButton;
    1313    Label1: TLabel;
     14    Edit1: TEdit;
    1415    Timer1: TTimer;
    1516    procedure Run; override;
     
    5455  Button.Caption := 'Start';
    5556  Button.OnClick := ButtonClick;
    56   Form1.Controls.Add(Button);
    5757  Label1 := TLabel.Create;
    5858  Label1.Parent := Form1;
     
    6161  Label1.Visible := True;
    6262  Label1.Caption := '0';
    63   Form1.Controls.Add(Label1);
     63  Edit1 := TEdit.Create;
     64  Edit1.Parent := Form2;
     65  Edit1.Owner := Form2;
     66  Edit1.Bounds := TRectangle.Create(60, 80, 60, 24);
     67  Edit1.Visible := True;
     68  Edit1.Text := 'Text';
    6469  MainForm := Form1;
    6570  TScreen(Screen).Forms.Add(Form1);
  • os/trunk/Applications/UDesktop.pas

    r7 r8  
    9090  TaskBar.Visible := True;
    9191  TaskBar.Caption := 'dds';
    92   MainBar.Controls.Add(TaskBar);
    9392  MenuButton := TButton.Create;
    94   MenuButton.Parent := TaskBar;
    95   MenuButton.Owner := TaskBar;
     93  MenuButton.Parent := MainBar;
     94  MenuButton.Owner := MainBar;
    9695  MenuButton.Bounds := TRectangle.Create(0, 0, 50, 24);
    9796  MenuButton.Visible := True;
    9897  MenuButton.Caption := 'Menu';
    9998  MenuButton.OnClick := ButtonClick;
    100   MainBar.Controls.Add(MenuButton);
    10199  TScreen(Screen).Forms.Add(MainBar);
    102100  MainForm := MainBar;
  • os/trunk/Drivers/Driver.KeyboardVCL.pas

    r5 r8  
    3434begin
    3535  Kernel.Keyboard.KeysState[Key] := False;
     36  Kernel.Keyboard.HandleKeyPress(Key);
    3637end;
    3738
  • os/trunk/System/LDOS.Kernel.pas

    r7 r8  
    7373    function ReadKey: Char;
    7474    function KeyPressed: Boolean;
     75    procedure HandleKeyPress(Key: Word);
    7576  end;
    7677
     
    303304{ TKeyboard }
    304305
     306procedure TKeyboard.HandleKeyPress(Key: Word);
     307var
     308  Form: TForm;
     309  NewMessage: TMessageKeyPress;
     310begin
     311  NewMessage := TMessageKeyPress.Create;
     312  NewMessage.KeyCode := Key;
     313  try
     314    for Form in Kernel.Screen.Forms do
     315    if Form.HandleMessage(NewMessage) then begin
     316      Break;
     317    end;
     318  finally
     319    NewMessage.Destroy;
     320  end;
     321end;
     322
    305323function TKeyboard.KeyPressed: Boolean;
    306324begin
  • os/trunk/Xvcl/Xvcl.Controls.pas

    r5 r8  
    2929  TKeyState = (ksShift, ksAlt, ksOS);
    3030  TKeyStateSet = set of TKeyState;
    31   TMessageKeyboard = class(TMessage)
     31  TMessageKeyPress = class(TMessage)
    3232    KeyCode: Integer;
    3333    States: TKeyStateSet;
     
    5959    FOnMouseDown: TNotifyEvent;
    6060    FOnMouseUp: TNotifyEvent;
     61    FOnKeyPress: TNotifyEvent;
    6162    function GetCanvas: TCanvas;
    6263    procedure SetParent(const Value: TWinControl); virtual;
     
    8182    property OnMouseDown: TNotifyEvent read FOnMouseDown write FOnMouseDown;
    8283    property OnMouseUp: TNotifyEvent read FOnMouseUp write FOnMouseUp;
     84    property OnKeyPress: TNotifyEvent read FOnKeyPress write FOnKeyPress;
    8385  end;
    8486
     
    9799    FCaption: string;
    98100    procedure SetCaption(const Value: string);
     101  protected
     102    function HandleMessage(Message: TMessage): Boolean; override;
    99103  public
    100104    procedure Paint; override;
     
    112116  end;
    113117
     118  TEdit = class(TControl)
     119  private
     120    FText: string;
     121    procedure SetText(const Value: string);
     122  protected
     123    function HandleMessage(Message: TMessage): Boolean; override;
     124  public
     125    procedure Paint; override;
     126    constructor Create; override;
     127    property Text: string read FText write SetText;
     128  end;
     129
    114130
    115131implementation
     
    153169
    154170function TControl.HandleMessage(Message: TMessage): Boolean;
     171begin
     172  Result := False;
     173end;
     174
     175procedure TControl.Paint;
     176begin
     177  with Canvas do begin
     178    Brush.Color := Color;
     179    FillRect(TRectangle.Create(0, 0, Bounds.Width, Bounds.Height));
     180  end;
     181end;
     182
     183function TControl.ScreenToClient(Position: TPoint): TPoint;
     184begin
     185  Result := Position.Substract(Bounds.TopLeft);
     186  if Assigned(Parent) then Result := Parent.ClientToScreen(Result);
     187end;
     188
     189procedure TControl.SetColor(const Value: TColor);
     190begin
     191  if FColor <> Value then begin
     192    FColor := Value;
     193    if Visible then Paint;
     194  end;
     195end;
     196
     197procedure TControl.SetParent(const Value: TWinControl);
     198begin
     199  if FParent <> Value then begin
     200    if Assigned(FParent) then Parent.Controls.Remove(Self);
     201    FParent := Value;
     202    if Assigned(FParent) then Parent.Controls.Add(Self);
     203  end;
     204end;
     205
     206{ TButton }
     207
     208function TButton.HandleMessage(Message: TMessage): Boolean;
    155209begin
    156210  Result := False;
     
    170224end;
    171225
    172 procedure TControl.Paint;
    173 begin
    174   with Canvas do begin
    175     Brush.Color := Color;
    176     FillRect(TRectangle.Create(0, 0, Bounds.Width, Bounds.Height));
    177   end;
    178 end;
    179 
    180 function TControl.ScreenToClient(Position: TPoint): TPoint;
    181 begin
    182   Result := Position.Substract(Bounds.TopLeft);
    183   if Assigned(Parent) then Result := Parent.ClientToScreen(Result);
    184 end;
    185 
    186 procedure TControl.SetColor(const Value: TColor);
    187 begin
    188   if FColor <> Value then begin
    189     FColor := Value;
    190     if Visible then Paint;
    191   end;
    192 end;
    193 
    194 procedure TControl.SetParent(const Value: TWinControl);
    195 begin
    196   if FParent <> Value then begin
    197     if Assigned(FParent) then Parent.Controls.Remove(Self);
    198     FParent := Value;
    199     if Assigned(FParent) then Parent.Controls.Add(Self);
    200   end;
    201 end;
    202 
    203 { TButton }
    204 
    205226constructor TButton.Create;
    206227begin
     
    268289    if Message is TMessageMouse then
    269290    with TMessageMouse(Message) do begin
    270     if Control.Bounds.Contains(ScreenToClient(Position)) then begin
     291      if Control.Bounds.Contains(ScreenToClient(Position)) then begin
     292        if TWinControl(Control).HandleMessage(Message) then begin
     293          Result := True;
     294          Break;
     295        end;
     296      end;
     297    end else
     298    if Message is TMessageKeyPress then
    271299      if TWinControl(Control).HandleMessage(Message) then begin
    272300        Result := True;
    273301        Break;
    274302      end;
    275     end;
    276   end;
    277303  end;
    278304end;
     
    305331end;
    306332
     333{ TEdit }
     334
     335constructor TEdit.Create;
     336begin
     337  inherited;
     338  FColor := clWhite;
     339end;
     340
     341function TEdit.HandleMessage(Message: TMessage): Boolean;
     342begin
     343  Result := False;
     344  if Message is TMessageKeyPress then
     345  with TMessageKeyPress(Message) do begin
     346    if Assigned(FOnKeyPress) then FOnKeyPress(Self);
     347    if KeyCode = 8 then Text := Copy(Text, 1, Length(Text) - 1)
     348      else Text := Text + Chr(TMessageKeyPress(Message).KeyCode);
     349    Paint;
     350    Result := True;
     351  end else
     352end;
     353
     354procedure TEdit.Paint;
     355var
     356  TextShort: string;
     357begin
     358  inherited;
     359  with Canvas do begin
     360    MoveTo(TPoint.Create(0, 0));
     361    LineTo(TPoint.Create(Bounds.Width - 1, 0));
     362    LineTo(TPoint.Create(Bounds.Width - 1, Bounds.Height - 1));
     363    LineTo(TPoint.Create(0, Bounds.Height - 1));
     364    LineTo(TPoint.Create(0, 0));
     365    TextShort := Text;
     366    while (GetTextSize(TextShort).X > Bounds.Width) and
     367      (Length(TextShort) > 0) do
     368      Delete(TextShort, 1, 1);
     369
     370    TextOut(TPoint.Create(3,
     371      (Bounds.Height - GetTextSize(TextShort).Y) div 2), TextShort);
     372  end;
     373end;
     374
     375procedure TEdit.SetText(const Value: string);
     376begin
     377  if FText <> Value then begin
     378    FText := Value;
     379    if Visible then Paint;
     380  end;
     381end;
     382
    307383end.
Note: See TracChangeset for help on using the changeset viewer.