-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDebugWindow.pas
More file actions
540 lines (478 loc) · 14.9 KB
/
DebugWindow.pas
File metadata and controls
540 lines (478 loc) · 14.9 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
(*
* GExperts Debug Window Interface
* http://www.gexperts.org
*
* You are free to use this code in any application to send commands to the
* GExperts debug window. This includes usage in commercial, shareware,
* freeware, public domain, and other applications.
*)
//
// Baz Cuda, 2022: Renamed, rejigged, camelhumped and augmented.
// All credit rests with the original authors for this very handy tool.
unit DebugWindow;
// {$I GX_CondDefine.inc} // BAZ
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, ImgList, Menus, ComCtrls, TrayIcon, ActnList, ToolWin,
System.Actions, System.ImageList;
type
TfmDebug = class(TForm)
pmuTaskBar: TPopupMenu;
mitTrayShutdown: TMenuItem;
mitTrayShow: TMenuItem;
MainMenu: TMainMenu;
mitFile: TMenuItem;
mitClearWindow: TMenuItem;
mitFileSep1: TMenuItem;
mitFileShutdown: TMenuItem;
mitFileExit: TMenuItem;
mitFileOptions: TMenuItem;
ilDebug: TImageList;
lvMessages: TListView;
mitEdit: TMenuItem;
mitCopySelectedLines: TMenuItem;
imgMessage: TImage;
mitTrayClear: TMenuItem;
mitSaveToFile: TMenuItem;
dlgSaveLog: TSaveDialog;
tbnClear: TToolButton;
tbnCopy: TToolButton;
tbnSave: TToolButton;
tbnSep1: TToolButton;
Actions: TActionList;
actFileOptions: TAction;
actFileShutdown: TAction;
actFileHideWindow: TAction;
actEditCopySelectedLines: TAction;
actEditClearWindow: TAction;
actEditSaveToFile: TAction;
actViewShow: TAction;
actShowItemInDialog: TAction;
ToolBar: TToolBar;
tbnSep2: TToolButton;
tbnOptions: TToolButton;
tbnPause: TToolButton;
actFilePause: TAction;
mitFilePause: TMenuItem;
pmuListbox: TPopupMenu;
mitListClearWindow: TMenuItem;
mitListShowItem: TMenuItem;
mitView: TMenuItem;
actViewStayOnTop: TAction;
actViewToolBar: TAction;
mitViewShowToolBar: TMenuItem;
mitViewStayOnTop: TMenuItem;
actEditSelectAll: TAction;
mitEditSelectAll: TMenuItem;
mitListSelectAll: TMenuItem;
ilImages: TImageList;
ilDisabled: TImageList;
procedure TrayIconDblClick(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure lvMessagesDblClick(Sender: TObject);
procedure actEditCopySelectedLinesExecute(Sender: TObject);
procedure actEditClearWindowExecute(Sender: TObject);
procedure actEditSaveToFileExecute(Sender: TObject);
procedure actFileOptionsExecute(Sender: TObject);
procedure actFileShutdownExecute(Sender: TObject);
procedure actFileHideWindowExecute(Sender: TObject);
procedure actViewShowExecute(Sender: TObject);
procedure actShowItemInDialogExecute(Sender: TObject);
procedure actShowItemInDialogUpdate(Sender: TObject);
procedure actFilePauseExecute(Sender: TObject);
procedure ActionsUpdate(Action: TBasicAction; var Handled: Boolean);
procedure actViewToolBarExecute(Sender: TObject);
procedure actViewStayOnTopExecute(Sender: TObject);
procedure actEditSelectAllExecute(Sender: TObject);
procedure FormCreate(Sender: TObject); // BAZ
private
FAllowClose: Boolean;
FStayOnTop: Boolean;
FTaskIcon: TTrayIcon;
procedure SetStayOnTop(OnTop: Boolean);
procedure SaveSettings;
procedure LoadSettings;
procedure ApplicationMsgHandler(var Msg: TMsg; var Handled: Boolean);
procedure WMEndSession(var Message: TMessage); message WM_ENDSESSION;
procedure WMCopyData(var Message: TWMCopyData); message WM_COPYDATA;
procedure WMQueryEndSession(var Message: TMessage); message WM_QUERYENDSESSION;
property StayOnTop: Boolean read FStayOnTop write SetStayOnTop;
procedure setWindowPosBottomRight; // BAZ
protected
procedure CreateParams(var Params: TCreateParams); override;
public
constructor Create(AOwner: TComponent); override; // BAZ
destructor Destroy; override;
end;
TGXUnicodeChar = WideChar; // BAZ
TGXUnicodeString = WideString; // BAZ
var
fmDebug: TfmDebug;
implementation
{$R *.dfm}
uses
Clipbrd, Registry, DebugOptions; // BAZ
type
TDebugType = (dtAnsiMessage, dtUnicodeMessage, dtSQL, dtCommand);
TDebugMessage = record
DebugType: TDebugType;
MessageType: TMsgDlgType;
AnsiMsg: AnsiString;
UnicodeMsg:TGXUnicodeString;
end;
const
TopMenu = 100;
{ TfmDebug }
procedure TfmDebug.setWindowPosBottomRight; // BAZ
begin
var vRect := Screen.WorkAreaRect;
setWindowPos(handle, 0, vRect.right - width, vRect.bottom - height, 0, 0, SWP_NOSIZE); // always position in bottom right-hand corner
end;
procedure TfmDebug.WMCopyData(var Message: TWMCopyData);
var
NewMsg: TDebugMessage;
ListItem: TListItem;
vClearWindow: boolean; // BAZ
procedure AddMessage(MessageType: TMsgDlgType; const MessageText: string);
begin
if ConfigInfo.Bottom or (lvMessages.Items.Count = 0) then
begin
ListItem := lvMessages.Items.Add;
ListItem.MakeVisible(True);
end
else
ListItem := lvMessages.Items.Insert(0);
ListItem.Caption := MessageText;
ListItem.ImageIndex := Ord(MessageType);
ListItem.SubItems.Add(TimeToStr(Time));
end;
procedure GetMessage;
const
chrDebugTypeANSI = #1;
chrDebugTypeSQL = #2;
chrClearCommand = #3;
chrDebugTypeUnicode = #4;
var
CData: TCopyDataStruct;
MessageContent: PAnsiChar;
i: Integer;
CharWord: Word;
UChar: TGXUnicodeChar;
begin
CData := Message.CopyDataStruct^;
MessageContent := CData.lpData; // TODO: Make this use the passed-in byte count for the payload length
vClearWindow := MessageContent[0] = chrClearCommand; // BAZ
if MessageContent[0] = chrClearCommand then
begin
NewMsg.DebugType := dtCommand;
actEditClearWindow.Execute;
Exit;
end;
if MessageContent[0] = chrDebugTypeSQL then
NewMsg.DebugType := dtSQL
else if MessageContent[0] = chrDebugTypeUnicode then
NewMsg.DebugType := dtUnicodeMessage
else
NewMsg.DebugType := dtAnsiMessage;
NewMsg.MessageType := TMsgDlgType(Integer(MessageContent[1]) - 1);
i := 2; // Byte 0 = payload, Byte 1 = message type
while MessageContent[i] <> #0 do
begin
if NewMsg.DebugType = dtUnicodeMessage then
begin
WordRec(CharWord).Lo := Byte(MessageContent[i]);
WordRec(CharWord).Hi := Byte(MessageContent[i+1]);
UChar := TGXUnicodeChar(CharWord);
NewMsg.UnicodeMsg := NewMsg.UnicodeMsg + UChar; // TODO: This is very slow....
Inc(i, 2);
end
else
begin
NewMsg.AnsiMsg := NewMsg.AnsiMsg + MessageContent[i];
Inc(i);
end;
end;
end;
var
OldClientWidth: Integer;
begin
GetMessage;
if actFilePause.Checked then
Exit;
OldClientWidth := lvMessages.ClientWidth;
if NewMsg.DebugType = dtAnsiMessage then
AddMessage(NewMsg.MessageType, string(NewMsg.AnsiMsg))
else if NewMsg.DebugType = dtUnicodeMessage then
AddMessage(NewMsg.MessageType, NewMsg.UnicodeMsg);
// Resize the header when the scrollbar is added
if not (lvMessages.ClientWidth = OldClientWidth) then
FormResize(Self);
if not Visible then
FTaskIcon.Icon := imgMessage.Picture.Icon;
if (ConfigInfo.OnMessage) and NOT vClearWindow then // BAZ don't show the window just because of a clearWindow command
actViewShow.Execute;
end;
procedure TfmDebug.TrayIconDblClick(Sender: TObject);
begin
actViewShow.Execute;
end;
procedure TfmDebug.FormResize(Sender: TObject);
begin
if lvMessages.ClientWidth > 100 then
lvMessages.Column[0].Width := lvMessages.ClientWidth - lvMessages.Column[1].Width;
end;
procedure TfmDebug.FormShow(Sender: TObject);
begin
FTaskIcon.Icon := Icon;
FormResize(Self);
MainMenu.Images := ilImages;
end;
procedure TfmDebug.SetStayOnTop(OnTop: Boolean);
begin
FStayOnTop := OnTop;
if OnTop then
SetWindowPos(Self.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE)
else
SetWindowPos(Self.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE);
end;
procedure TfmDebug.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := FAllowClose;
if not CanClose then
Hide;
end;
procedure TfmDebug.FormCreate(Sender: TObject); // BAZ
begin
// Force the working directory to the EXE folder to prevent handle leaks
setCurrentDir(extractFilePath(paramStr(0)));
popupMode := pmExplicit;
borderIcons := [biSystemMenu];
setWindowPosBottomRight;
position := poDesigned;
end;
procedure TfmDebug.SaveSettings;
var
Settings: TRegIniFile;
begin
Settings := TRegIniFile.Create('Software\Baz');
try
// Settings.WriteInteger('Debug', 'left', Left); // BAZ
// Settings.WriteInteger('Debug', 'top', Top); // BAZ
Settings.WriteInteger('Debug', 'width', Width);
Settings.WriteInteger('Debug', 'height', Height);
Settings.WriteString('Debug', 'savePath', dlgSaveLog.InitialDir);
Settings.WriteBool('Debug', 'viewToolBar', ToolBar.Visible);
Settings.WriteBool('Debug', 'stayOnTop', FStayOnTop);
finally
FreeAndNil(Settings);
end;
end;
procedure TfmDebug.LoadSettings;
var
Settings: TRegIniFile;
begin
setWindowPosBottomRight; // BAZ
// Left := (Screen.Width - Width) div 2; // BAZ
// Top := (Screen.Height - Height) div 2; // BAZ
Settings := TRegIniFile.Create('Software\Baz');
try
// Left := Settings.ReadInteger('Debug', 'Left', Left); // BAZ
// Top := Settings.ReadInteger('Debug', 'Top', Top); // BAZ
Width := Settings.ReadInteger('Debug', 'width', Width);
Height := Settings.ReadInteger('Debug', 'height', Height);
ToolBar.Visible := Settings.ReadBool('Debug', 'viewToolBar', True);
StayOnTop := Settings.ReadBool('Debug', 'stayOnTop', False);
dlgSaveLog.InitialDir := Settings.ReadString('Debug', 'savePath', '');
finally
FreeAndNil(Settings);
end;
end;
procedure TfmDebug.ApplicationMsgHandler(var Msg: TMsg; var Handled: Boolean);
begin
if (Msg.Message = WM_SYSCOMMAND) and (Msg.wParam = SC_RESTORE) then Show;
end;
procedure TfmDebug.WMEndSession(var Message: TMessage);
begin
FAllowClose := True;
Close;
inherited;
end;
procedure TfmDebug.WMQueryEndSession(var Message: TMessage);
begin
FTaskIcon.Active := False;
FAllowClose := True;
Close;
inherited;
end;
procedure TfmDebug.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TfmDebug.lvMessagesDblClick(Sender: TObject);
begin
actShowItemInDialog.Execute;
end;
procedure TfmDebug.actEditCopySelectedLinesExecute(Sender: TObject);
var
CopyStrings: TStrings;
i: Integer;
ListItem: TListItem;
NewLine: string;
begin
CopyStrings := TStringList.Create;
try
for i := 0 to lvMessages.Items.Count - 1 do
begin
if lvMessages.Items[i].Selected then
begin
ListItem := lvMessages.Items[i];
NewLine := Format('%d'#9'%s'#9'%s', [ListItem.ImageIndex, ListItem.Caption,
ListItem.SubItems[0]]);
CopyStrings.Add(NewLine);
end;
end;
Clipboard.AsText := CopyStrings.Text;
finally
FreeAndNil(CopyStrings);
end;
end;
procedure TfmDebug.actEditClearWindowExecute(Sender: TObject);
begin
lvMessages.Items.BeginUpdate;
try
lvMessages.Items.Clear;
finally
lvMessages.Items.EndUpdate;
end;
FTaskIcon.Icon := Icon;
FormResize(Self);
end;
procedure TfmDebug.actEditSaveToFileExecute(Sender: TObject);
resourcestring
SSaveError = 'Error saving messages: %s';
var
i: Integer;
Messages: TStrings;
begin
if lvMessages.Items.Count > 0 then
begin
// Note: this is not part of the IDE, hence we
// do not do any "current folder" protection.
if dlgSaveLog.Execute then
try
Messages := TStringList.Create;
try
for i := 0 to lvMessages.Items.Count - 1 do
begin
Messages.Add(
IntToStr(lvMessages.Items[i].ImageIndex) + #9 +
lvMessages.Items[i].Caption + #9 +
lvMessages.Items[i].SubItems[0]);
end;
Messages.SaveToFile(dlgSaveLog.FileName);
dlgSaveLog.InitialDir := ExtractFilePath(dlgSaveLog.FileName);
finally
FreeAndNil(Messages);
end;
except
on E: Exception do
MessageDlg(Format(SSaveError, [E.Message]), mtError, [mbOK], 0);
end;
end;
end;
procedure TfmDebug.actFileOptionsExecute(Sender: TObject);
begin
with TfmDebugOptions.Create(nil) do
try
ShowModal;
finally
Free;
end;
end;
procedure TfmDebug.actFileShutdownExecute(Sender: TObject);
begin
FAllowClose := True;
Close;
end;
procedure TfmDebug.actFileHideWindowExecute(Sender: TObject);
begin
Close;
end;
procedure TfmDebug.actViewShowExecute(Sender: TObject);
begin
Show;
// setWindowPosBottomRight;
BringToFront;
// Application.BringToFront;
// EnsureFormVisible(Self); // BAZ
end;
constructor TfmDebug.Create(AOwner: TComponent);
resourcestring
SAlwaysFStayOnTop = '&Always On Top';
begin
inherited Create(AOwner);
// SetDefaultFont(Self); // BAZ
// SetToolbarGradient(Toolbar); // BAZ
Application.OnMessage := ApplicationMsgHandler;
FStayOnTop := False;
Application.ShowHint := True;
Caption := Application.Title;
FTaskIcon := TTrayIcon.Create(Self);
FTaskIcon.Icon := Icon;
FTaskIcon.Active := True;
FTaskIcon.PopupMenu := pmuTaskBar;
FTaskIcon.ToolTip := Application.Title;
FTaskIcon.OnDblClick := TrayIconDblClick;
FAllowClose := False;
LoadSettings;
end;
procedure TfmDebug.CreateParams(var Params: TCreateParams); // BAZ
// no taskbar icon for this window
begin
inherited;
Params.ExStyle := Params.ExStyle AND (NOT WS_EX_APPWINDOW); // don't display an icon on the taskbar for this window
// Params.WndParent := Application.Handle;
end;
destructor TfmDebug.Destroy;
begin
SaveSettings;
inherited Destroy;
end;
procedure TfmDebug.actShowItemInDialogExecute(Sender: TObject);
begin
if lvMessages.Selected <> nil then
MessageDlg(lvMessages.Selected.Caption, mtInformation, [mbOK], 0);
end;
procedure TfmDebug.actShowItemInDialogUpdate(Sender: TObject);
begin
(Sender as TCustomAction).Enabled := (lvMessages.Selected <> nil);
end;
procedure TfmDebug.actFilePauseExecute(Sender: TObject);
begin
actFilePause.Checked := not actFilePause.Checked;
end;
procedure TfmDebug.ActionsUpdate(Action: TBasicAction; var Handled: Boolean);
begin
actViewStayOnTop.Checked := StayOnTop;
actViewToolBar.Checked := ToolBar.Visible;
end;
procedure TfmDebug.actViewToolBarExecute(Sender: TObject);
begin
ToolBar.Visible := not ToolBar.Visible;
end;
procedure TfmDebug.actViewStayOnTopExecute(Sender: TObject);
begin
StayOnTop := not StayOnTop;
end;
procedure TfmDebug.actEditSelectAllExecute(Sender: TObject);
var
i: Integer;
begin
for i := 0 to lvMessages.Items.Count - 1 do
lvMessages.Items[i].Selected := True;
end;
end.