/usr/lib/lazarus/0.9.30.4/ide/srcedithintfrm.pas is in lazarus-src-0.9.30.4 0.9.30.4-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | {
***************************************************************************
* *
* 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. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
The base class for hint windows for the source editor for the online help.
For example for the fpdoc and comment help.
}
unit SrcEditHintFrm;
{$mode objfpc}{$H+}
interface
uses
Classes, Math, SysUtils, LCLProc, LCLType, LCLIntf, Forms, Controls, Graphics,
SynEdit, SynEditKeyCmds,
SrcEditorIntf;
type
{ TCodeHintProvider }
TCodeHintProvider = class(TComponent)
private
FControl: TWinControl;
protected
procedure SetControl(const AValue: TWinControl); virtual;
public
procedure GetPreferredSize(var PreferredWidth, PreferredHeight: integer); virtual;
procedure UpdateHint; virtual;
property Control: TWinControl read FControl write SetControl;
end;
{ TSrcEditHintWindow }
TSrcEditHintWindow = class(THintWindow)
procedure ApplicationIdle(Sender: TObject; var Done: Boolean);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormUTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);
private
FAnchorForm: TCustomForm;
FHelpEnabled: boolean;
FPreferredHeight: integer;
FPreferredWidth: integer;
FProvider: TCodeHintProvider;
FSrcEditCaret: TPoint;
procedure SetAnchorForm(const AValue: TCustomForm);
procedure OnAnchorFormChangeBounds(Sender: TObject);
procedure SetHelpEnabled(const AValue: boolean);
procedure SetPreferredHeight(const AValue: integer);
procedure SetPreferredWidth(const AValue: integer);
procedure SetProvider(const AValue: TCodeHintProvider);
procedure SetSrcEditCaret(const AValue: TPoint);
procedure UpdatePosition;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure Paint; override;
procedure UpdateHints;// update content
function NeedVisible: boolean;
property AnchorForm: TCustomForm read FAnchorForm write SetAnchorForm;
property HelpEnabled: boolean read FHelpEnabled write SetHelpEnabled;
property SrcEditCaret: TPoint read FSrcEditCaret write SetSrcEditCaret;// 0,0 means use current position, should be ScreenXY, not TextXY
property PreferredWidth: integer read FPreferredWidth write SetPreferredWidth;
property PreferredHeight: integer read FPreferredHeight write SetPreferredHeight;
property Provider: TCodeHintProvider read FProvider write SetProvider;
end;
var
SrcEditHintWindow: TSrcEditHintWindow = nil;
implementation
type
TWinControlAccess = class(TWinControl);
{ TSrcEditHintWindow }
procedure TSrcEditHintWindow.ApplicationIdle(Sender: TObject; var Done: Boolean);
begin
//DebugLn(['TCodeHintFrm.ApplicationIdle NeedVisible=',NeedVisible]);
if not NeedVisible then begin
Hide;
exit;
end;
UpdatePosition;
end;
procedure TSrcEditHintWindow.FormCreate(Sender: TObject);
begin
Application.AddOnIdleHandler(@ApplicationIdle);
end;
procedure TSrcEditHintWindow.FormDestroy(Sender: TObject);
begin
end;
procedure TSrcEditHintWindow.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
SrcEdit: TSourceEditorInterface;
begin
if (Key=VK_ESCAPE) and (Shift=[]) then
Hide
else if SourceEditorManagerIntf<>nil then begin
SrcEdit:=SourceEditorManagerIntf.ActiveEditor;
if SrcEdit=nil then
Hide
else begin
// redirect keys
TWinControlAccess(SrcEdit.EditorControl).KeyDown(Key,Shift);
SetActiveWindow(SourceEditorManagerIntf.ActiveSourceWindow.Handle);
end;
end;
end;
procedure TSrcEditHintWindow.FormUTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char
);
var
SrcEdit: TSourceEditorInterface;
ASynEdit: TCustomSynEdit;
begin
SrcEdit:=SourceEditorManagerIntf.ActiveEditor;
if SrcEdit=nil then begin
Hide;
end else begin
ASynEdit:=(SrcEdit.EditorControl as TCustomSynEdit);
ASynEdit.CommandProcessor(ecChar,UTF8Key,nil);
end;
end;
procedure TSrcEditHintWindow.SetAnchorForm(const AValue: TCustomForm);
begin
if FAnchorForm=AValue then exit;
if FAnchorForm<>nil then
FAnchorForm.RemoveAllHandlersOfObject(Self);
FAnchorForm:=AValue;
if FAnchorForm<>nil then
FAnchorForm.AddHandlerOnChangeBounds(@OnAnchorFormChangeBounds);
UpdatePosition;
end;
procedure TSrcEditHintWindow.OnAnchorFormChangeBounds(Sender: TObject);
begin
//DebugLn(['TCodeHintFrm.OnAnchorFormChangeBounds ',dbgs(BoundsRect),' Sender=',dbgsName(Sender),' SenderVisible=',TControl(Sender).Visible,' SenderBounds=',dbgs(TControl(Sender).BoundsRect)]);
UpdatePosition;
end;
procedure TSrcEditHintWindow.SetHelpEnabled(const AValue: boolean);
begin
if FHelpEnabled=AValue then exit;
FHelpEnabled:=AValue;
UpdatePosition;
end;
procedure TSrcEditHintWindow.SetPreferredHeight(const AValue: integer);
begin
if FPreferredHeight=AValue then exit;
FPreferredHeight:=AValue;
end;
procedure TSrcEditHintWindow.SetPreferredWidth(const AValue: integer);
begin
if FPreferredWidth=AValue then exit;
FPreferredWidth:=AValue;
end;
procedure TSrcEditHintWindow.SetProvider(const AValue: TCodeHintProvider);
begin
if FProvider=AValue then exit;
if FProvider<>nil then begin
FProvider.Control:=nil;
end;
FProvider:=AValue;
if FProvider<>nil then begin
FProvider.Control:=Self;
FProvider.GetPreferredSize(FPreferredWidth,FPreferredHeight);
end;
end;
procedure TSrcEditHintWindow.SetSrcEditCaret(const AValue: TPoint);
begin
if ComparePoints(FSrcEditCaret,AValue)=0 then exit;
FSrcEditCaret:=AValue;
end;
procedure TSrcEditHintWindow.UpdatePosition;
var
NewBounds: TRect;
DesktopBounds: TRect;
procedure TryPosition(TryBounds: TRect; TheAnchors: TAnchors);
begin
TryBounds.Right:=Max(TryBounds.Left,TryBounds.Right);
TryBounds.Bottom:=Max(TryBounds.Top,TryBounds.Bottom);
if TryBounds.Right>DesktopBounds.Right then begin
if not (akLeft in TheAnchors) then begin
// move to the left
dec(TryBounds.Left,TryBounds.Right-DesktopBounds.Right);
TryBounds.Left:=Max(TryBounds.Left,DesktopBounds.Left);
end;
TryBounds.Right:=DesktopBounds.Right;
end;
if TryBounds.Left<DesktopBounds.Left then begin
if not (akRight in TheAnchors) then begin
// move to the right
inc(TryBounds.Right,DesktopBounds.Left-TryBounds.Left);
TryBounds.Left:=Min(TryBounds.Right,DesktopBounds.Right);
end;
TryBounds.Left:=DesktopBounds.Left;
end;
if TryBounds.Bottom>DesktopBounds.Bottom then begin
if not (akTop in TheAnchors) then begin
// move to the top
dec(TryBounds.Top,TryBounds.Bottom-DesktopBounds.Bottom);
TryBounds.Top:=Max(TryBounds.Top,DesktopBounds.Top);
end;
TryBounds.Bottom:=DesktopBounds.Bottom;
end;
if TryBounds.Top<DesktopBounds.Top then begin
if not (akBottom in TheAnchors) then begin
// move to the bottom
inc(TryBounds.Bottom,DesktopBounds.Top-TryBounds.Top);
TryBounds.Bottom:=Min(TryBounds.Bottom,DesktopBounds.Bottom);
end;
TryBounds.Top:=DesktopBounds.Top;
end;
// check if TryBounds are better than NewBounds
if (TryBounds.Right-TryBounds.Left)*(TryBounds.Bottom-TryBounds.Top)
> (NewBounds.Right-NewBounds.Left)*(NewBounds.Bottom-NewBounds.Top)
then
NewBounds:=TryBounds;
end;
var
CurCaret: TPoint;
SrcEdit: TSourceEditorInterface;
AnchorBounds: TRect;
begin
if (not NeedVisible) or Visible then exit;
DesktopBounds:=Rect(30,30,Screen.DesktopWidth-30,Screen.DesktopHeight-50);
NewBounds:=Bounds(DesktopBounds.Left,DesktopBounds.Top,30,30);
if AnchorForm<>nil then begin
// place near the AnchorForm
AnchorBounds:=AnchorForm.BoundsRect;
// try right of AnchorForm
TryPosition(Bounds(AnchorBounds.Right+6,AnchorBounds.Top,
PreferredWidth,PreferredHeight),[akLeft,akTop]);
// try left of AnchorForm
TryPosition(Bounds(AnchorBounds.Left-6-PreferredWidth,AnchorBounds.Top,
PreferredWidth,PreferredHeight),[akRight,akTop]);
// try below
TryPosition(Bounds(AnchorBounds.Left,AnchorBounds.Bottom+6,
PreferredWidth,PreferredHeight),[akTop]);
end else begin
// place near the source editor caret
CurCaret:=SrcEditCaret;
SrcEdit:=SourceEditorManagerIntf.ActiveEditor;
if CurCaret.Y<1 then
CurCaret:=SrcEdit.CursorScreenXY;
CurCaret:=SrcEdit.EditorControl.ClientToScreen(SrcEdit.ScreenToPixelPosition(CurCaret));
// try below
TryPosition(Bounds(CurCaret.X-(PreferredWidth div 2),CurCaret.Y+6,
PreferredWidth,PreferredHeight),[akTop]);
// try above
TryPosition(Bounds(CurCaret.X-(PreferredWidth div 2),
CurCaret.Y-6-PreferredHeight,
PreferredWidth,PreferredHeight),[akBottom]);
end;
//DebugLn(['TCodeHintFrm.UpdatePosition NewBounds=',dbgs(NewBounds),' BoundsRect=',dbgs(BoundsRect)]);
BoundsRect:=NewBounds;
Visible:=true;
end;
procedure TSrcEditHintWindow.Paint;
begin
end;
constructor TSrcEditHintWindow.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
OnDestroy:=@FormDestroy;
OnKeyDown:=@FormKeyDown;
OnUTF8KeyPress:=@FormUTF8KeyPress;
FPreferredWidth:=300;
FPreferredHeight:=200;
FormCreate(Self);
end;
destructor TSrcEditHintWindow.Destroy;
begin
Application.RemoveAllHandlersOfObject(Self);
if SrcEditHintWindow=Self then
SrcEditHintWindow:=nil;
inherited Destroy;
end;
procedure TSrcEditHintWindow.UpdateHints;
begin
if not Visible then exit;
//DebugLn(['TCodeHintFrm.UpdateHints ']);
if Provider<>nil then Provider.UpdateHint;
end;
function TSrcEditHintWindow.NeedVisible: boolean;
begin
if not HelpEnabled then exit(false);
if (AnchorForm<>nil) then begin
Result:=AnchorForm.Visible;
end else begin
Result:=(SourceEditorManagerIntf<>nil)
and (SourceEditorManagerIntf.ActiveEditor<>nil);
end;
end;
{ TCodeHintProvider }
procedure TCodeHintProvider.SetControl(const AValue: TWinControl);
begin
if FControl=AValue then exit;
FControl:=AValue;
end;
procedure TCodeHintProvider.GetPreferredSize(var PreferredWidth,
PreferredHeight: integer);
begin
end;
procedure TCodeHintProvider.UpdateHint;
begin
end;
end.
|