home *** CD-ROM | disk | FTP | other *** search
- {*********************************************************}
- {* BTNEDIT.PAS 1.00 *}
- {* COMPONENT FOR DELPHI Ver 1.x and 2.0 *}
- {* Copyright (c) Jeffrey Cooke 1996 *}
- {* email:100026.3107@compuserve.com *}
- {* All rights reserved. *}
- {*********************************************************}
-
- {-Simple Borland Delphi component that is derived from TCustomEdit
- and is basically an edit box with a speedbutton, glyph property
- and a OnBtnClick event. Standard glyph in Glyph property is an
- ellipsis. Minimun button width is 15 pixels otherwise button width
- is always 0.6 x Height}
-
- {- WIN32 conditional define controls which code is compiled}
-
- unit BtnEdit;
-
- interface
-
- uses WinTypes, WinProcs, Classes, StdCtrls, ExtCtrls, Controls, Messages, SysUtils,
- Forms, Graphics, Menus, Buttons;
-
- type
- TBtnEditButton = class (TWinControl)
- private
- FEditButton: TSpeedButton;
- FFocusControl: TWinControl;
- FOnBtnClick: TNotifyEvent;
- function CreateButton: TSpeedButton;
- function GetGlyph: TBitmap;
- procedure SetGlyph(Value: TBitmap);
- procedure BtnClick(Sender: TObject);
- procedure AdjustSize (var W: Integer; var H: Integer);
- procedure WMSize(var Message: TWMSize); message WM_SIZE;
- protected
- procedure Loaded; override;
- public
- constructor Create(AOwner: TComponent); override;
- procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
- published
- property Align;
- property Ctl3D;
- property DragCursor;
- property DragMode;
- property Enabled;
- property FocusControl: TWinControl read FFocusControl write FFocusControl;
- property ParentCtl3D;
- property ParentShowHint;
- property PopupMenu;
- property ShowHint;
- property TabOrder;
- property TabStop;
- property Glyph: TBitmap read GetGlyph write SetGlyph;
- property Visible;
- property OnDragDrop;
- property OnDragOver;
- property OnEndDrag;
- property OnEnter;
- property OnExit;
- property OnBtnClick: TNotifyEvent read FOnBtnClick write FOnBtnClick;
- end;
-
- { TBtnEdit }
-
- TBtnEdit = class(TCustomEdit)
- private
- FCanvas: TCanvas;
- FButton: TBtnEditButton;
- FEditorEnabled: Boolean;
- function GetMinHeight: Integer;
- procedure SetEditRect;
- function GetGlyph: TBitmap;
- procedure SetGlyph(Value: TBitmap);
- Function GetOnBtnClick: TNotifyEvent;
- Procedure SetOnBtnClick(Value:TNotifyEvent);
- procedure WMSize(var Message: TWMSize); message WM_SIZE;
- procedure CMEnter(var Message: TCMGotFocus); message CM_ENTER;
- procedure WMPaste(var Message: TWMPaste); message WM_PASTE;
- procedure WMCut(var Message: TWMCut); message WM_CUT;
- protected
- procedure BtnClick (Sender: TObject); virtual;
- procedure CreateParams(var Params: TCreateParams); override;
- procedure CreateWnd; override;
- public
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- property Button: TBtnEditButton read FButton;
- published
- property AutoSelect;
- property AutoSize;
- property Color;
- property Ctl3D;
- property DragCursor;
- property DragMode;
- property EditorEnabled: Boolean read FEditorEnabled write FEditorEnabled default True;
- property Enabled;
- property Font;
- property Glyph: TBitmap read GetGlyph write SetGlyph;
- property MaxLength;
- property ParentColor;
- property ParentCtl3D;
- property ParentFont;
- property ParentShowHint;
- property PopupMenu;
- property ReadOnly;
- property ShowHint;
- property TabOrder;
- property TabStop;
- property Text;
- property Visible;
- property OnChange;
- property OnClick;
- property OnBtnClick: TNotifyEvent read GetOnBtnClick write SetOnBtnClick;
- property OnDblClick;
- property OnDragDrop;
- property OnDragOver;
- property OnEndDrag;
- property OnEnter;
- property OnExit;
- property OnKeyDown;
- property OnKeyPress;
- property OnKeyUp;
- property OnMouseDown;
- property OnMouseMove;
- property OnMouseUp;
- end;
-
- procedure Register;
-
- implementation
-
- {$IFDEF WIN32}
- {$R BTNEDT32}
- {$ELSE}
- {$R BTNEDT16}
- {$ENDIF}
-
- { TBtnEditButton }
-
- constructor TBtnEditButton.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- ControlStyle := ControlStyle - [csAcceptsControls, csSetCaption] +
- [csFramed, csOpaque];
-
- FEditButton := CreateButton;
- Glyph := nil;
- Width := 20;
- Height := 25;
- FOnBtnClick := Nil;
- end;
-
- function TBtnEditButton.CreateButton: TSpeedButton;
- begin
- Result := TSpeedButton.Create (Self);
- Result.OnClick := BtnClick;
- Result.Visible := True;
- Result.Enabled := True;
- Result.NumGlyphs := 1;
- Result.Parent := Self;
- end;
-
- procedure TBtnEditButton.AdjustSize (var W: Integer; var H: Integer);
- begin
- if (FEditButton = nil) or (csLoading in ComponentState) then Exit;
- if W < 15 then W := 15;
- FEditButton.SetBounds (0, 0, W, H);
- end;
-
- procedure TBtnEditButton.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
- var
- W, H: Integer;
- begin
- W := AWidth;
- H := AHeight;
- AdjustSize (W, H);
- inherited SetBounds (ALeft, ATop, W, H);
- end;
-
- procedure TBtnEditButton.WMSize(var Message: TWMSize);
- var
- W, H: Integer;
- begin
- inherited;
-
- { check for minimum size }
- W := Width;
- H := Height;
- AdjustSize (W, H);
- if (W <> Width) or (H <> Height) then
- inherited SetBounds(Left, Top, W, H);
- Message.Result := 0;
- end;
-
- procedure TBtnEditButton.BtnClick(Sender: TObject);
- begin
- if Assigned(FOnBtnClick) then
- FOnBtnClick(Self);
- end;
-
- procedure TBtnEditButton.Loaded;
- var
- W, H: Integer;
- begin
- inherited Loaded;
- W := Width;
- H := Height;
- AdjustSize (W, H);
- if (W <> Width) or (H <> Height) then
- inherited SetBounds (Left, Top, W, H);
- end;
-
- function TBtnEditButton.GetGlyph: TBitmap;
- begin
- Result := FEditButton.Glyph;
- end;
-
- procedure TBtnEditButton.SetGlyph(Value: TBitmap);
- begin
- if Value <> nil then
- FEditButton.Glyph := Value
- else
- begin
- FEditButton.Glyph.Handle := LoadBitmap(HInstance, 'Ellipsis');
- FEditButton.NumGlyphs := 1;
- FEditButton.Invalidate;
- end;
- end;
-
- { TBtnEdit }
-
- constructor TBtnEdit.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FButton := TBtnEditButton.Create (Self);
- FButton.Width := 15;
- FButton.Height := 17;
- FButton.Visible := True;
- FButton.Parent := Self;
- FButton.FocusControl := Self;
- FButton.OnBtnClick := BtnClick;
- Text := '';
- ControlStyle := ControlStyle - [csSetCaption];
- FEditorEnabled := True;
- end;
-
- destructor TBtnEdit.Destroy;
- begin
- FButton.Free;
- FButton := nil;
- inherited Destroy;
- end;
-
- procedure TBtnEdit.CreateParams(var Params: TCreateParams);
- begin
- inherited CreateParams(Params);
- Params.Style := Params.Style or ES_MULTILINE or WS_CLIPCHILDREN;
- end;
-
- procedure TBtnEdit.CreateWnd;
- begin
- inherited CreateWnd;
- SetEditRect;
- end;
-
- procedure TBtnEdit.SetEditRect;
- var
- Loc: TRect;
- begin
- SendMessage(Handle, EM_GETRECT, 0, LongInt(@Loc));
- Loc.Bottom := ClientHeight + 1; {+1 is workaround for windows paint bug}
- Loc.Right := ClientWidth - FButton.Width - 2;
- Loc.Top := 0;
- Loc.Left := 0;
- SendMessage(Handle, EM_SETRECT, 0, LongInt(@Loc));
- SendMessage(Handle, EM_GETRECT, 0, LongInt(@Loc)); {debug}
- end;
-
- function TBtnEdit.GetGlyph: TBitmap;
- begin
- Result := FButton.Glyph;
- end;
-
- procedure TBtnEdit.SetGlyph(Value: TBitmap);
- begin
- FButton.Glyph := Value;
- end;
-
- procedure TBtnEdit.WMSize(var Message: TWMSize);
- var
- MinHeight: Integer;
- begin
- inherited;
- MinHeight := GetMinHeight;
- { text edit bug: if size to less than minheight, then edit ctrl does
- not display the text }
- if Autosize and (Height < MinHeight) then
- Height := MinHeight
- else if FButton <> nil then
- begin
- {adjust button to right size}
- FButton.width := Round(Height*0.6);
- if NewStyleControls then
- {$IFDEF WIN32}
- FButton.SetBounds(Width - FButton.Width - 5, 0, FButton.Width, Height - 5)
- {$ELSE}
- FButton.SetBounds(Width - FButton.Width, 0, FButton.Width, Height)
- {$ENDIF}
- else FButton.SetBounds (Width - FButton.Width, 0, FButton.Width, Height);
- SetEditRect;
- end;
- end;
-
- function TBtnEdit.GetMinHeight: Integer;
- var
- DC: HDC;
- SaveFont: HFont;
- I: Integer;
- SysMetrics, Metrics: TTextMetric;
- begin
- DC := GetDC(0);
- GetTextMetrics(DC, SysMetrics);
- SaveFont := SelectObject(DC, Font.Handle);
- GetTextMetrics(DC, Metrics);
- SelectObject(DC, SaveFont);
- ReleaseDC(0, DC);
- I := SysMetrics.tmHeight;
- if I > Metrics.tmHeight then I := Metrics.tmHeight;
- Result := Metrics.tmHeight + I div 4 + GetSystemMetrics(SM_CYBORDER) * 4 + 2;
- end;
-
- procedure TBtnEdit.BtnClick (Sender: TObject);
- begin
- if ReadOnly then MessageBeep(0)
- end;
-
- procedure TBtnEdit.WMPaste(var Message: TWMPaste);
- begin
- if not FEditorEnabled or ReadOnly then Exit;
- inherited;
- end;
-
- procedure TBtnEdit.WMCut(var Message: TWMPaste);
- begin
- if not FEditorEnabled or ReadOnly then Exit;
- inherited;
- end;
-
- Procedure TBtnEdit.SetOnBtnClick(Value:TNotifyEvent);
- begin
- FButton.OnBtnClick := Value;
- end;
-
- Function TBtnEdit.GetOnBtnClick:TNotifyEvent;
- begin
- Result := FButton.OnBtnClick;
- end;
-
- procedure TBtnEdit.CMEnter(var Message: TCMGotFocus);
- begin
- if AutoSelect and not (csLButtonDown in ControlState) then
- SelectAll;
- inherited;
- end;
-
- procedure Register;
- begin
- RegisterComponents('Samples', [TBtnEdit]);
- end;
-
- end.
-
-
-
-