Menu

[r2018]: / trunk / Src / UCompileResultsLBMgr.pas  Maximize  Restore  History

Download this file

801 lines (748 with data), 28.8 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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
{
* UCompileResultsLBMgr.pas
*
* Defines classes that manages display, editing and interaction with a list box
* that displays compiler results.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UCompileResultsLBMgr.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2009-2010 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UCompileResultsLBMgr;
interface
uses
// Delphi
Classes, Controls, StdCtrls, Types,
// Project
Compilers.UGlobals, UDropDownButtons, ULEDImageList, UStructs;
type
{
TCompileResultsLBMgr:
Class that manages display and interaction with a list box that displays
compiler results. Provides a means of editing the results for individual
compilers.
}
TCompileResultsLBMgr = class(TObject)
strict private
type
{
TCompilerInfo:
Class that records information about a snippet's compilation results.
Resulting objects are stored in list box items Objects[] array.
}
TCompilerInfo = class(TObject)
strict private
fCompilerID: TCompilerID; // Value of Compiler property
fCompileResult: TCompileResult; // Value of CompileResult property
public
constructor Create(const CompilerID: TCompilerID;
const CompileResult: TCompileResult);
{Class constructor. Sets up and initialises object.
@param CompilerID [in] Id of compiler that result applies to.
@param CompileResult [in] Compiler result for compiler.
}
property CompilerID: TCompilerID read fCompilerID;
{Id of compiler to which CompileResult applies}
property CompileResult: TCompileResult
read fCompileResult write fCompileResult;
{Result of compilation with associated compiler}
end;
var
fLB: TListBox; // Managed list box
fCompilers: ICompilers; // Compilers to be displayed in list box
fLEDImages: TLEDImageList; // Image list containing LEDs
fLastHotDropDown: TRectEx; // Bounds of last drop down highlighted
fDropDownBtns: TDropDownButtons; // Provides drop down button glyphs
procedure PopulateListBox;
{Adds details of each compiler to list box with unknown compile results.
}
function IndexOf(const CompID: TCompilerID): Integer;
{Finds index of list item corresponding to a compiler.
@param CompID [in] ID of compiler.
@return Index of corresponding list item or -1 if compiler not found.
}
procedure SetCompileResult(const Index: Integer; const Res: TCompileResult);
{Sets compile result of a list item.
@param Index [in] Index of list item.
@param Res [in] Required compiler result.
}
function GetCompilerInfo(const Index: Integer): TCompilerInfo;
overload;
{Retrieves the compiler info associated with a specified list item.
@param Index [in] Index of list item.
@return Required compiler info.
}
function GetCompilerInfo(const CompID: TCompilerID): TCompilerInfo;
overload;
{Retrieves the compiler info associated with a specified compiler ID.
@param CompID [in] Compiler ID.
@return Required compiler info.
}
procedure MenuSelectHandler(Sender: TObject);
{Handles menu selection on popup menu activated from drop down button.
Sets compile result of selected list item per menu selection.
@param Sender [in] Menu item that was clicked.
}
procedure DrawItem(Control: TWinControl; Index: Integer; Rect: TRect;
State: TOwnerDrawState);
{OnDrawItem event handler for managed list box. Custom draws compiler list
item to show compiler glyph, name of compiler, current compile result
"LED" and a drop-down button.
@param Control [in] Reference to list box that triggered event. Must be
the managed control.
@param Index [in] Index of list item being drawn.
@param Rect [in] Rectangle in list box canvas where item being drawn.
@param State [in] State of list item.
}
procedure MouseDown(Sender: TObject; Button: TMouseButton; Shift:
TShiftState; X, Y: Integer);
{Handles list box's OnMouseDown event. Displays compile result menu if
user left clicked on drop down button with no modifier keys pressed.
@param Sender [in] Not used.
@param Button [in] Mouse button pressed.
@param Shift [in] Modifier keys etc.
@param X [in] X coordinate of mouse pointer in list box.
@param Y [in] Y coordinate of mouse pointer in list box.
}
procedure MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
{Handles list box's OnMouseMove event. Updates display of hot and normal
drop down buttons depending on whether mouse is over such a button.
@param Sender [in] List box that triggered event. Not used.
@param Shift [in] State of shift keys. Not used.
@param X [in] X coordinate of mouse pointer in list box.
@param Y [in] Y coordinate of mouse pointer in list box.
}
procedure MouseLeave(Sender: TObject);
{Handles list box's OnMouseLeave event. Redisplays any drop down button
that was displayed as hot.
@param Sender [in] Not used.
}
procedure KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
{Handles list box's OnKeyUp event. Displays popup menu over any currently
selected list item if user pressed space bar with no modifier keys.
@param Sender [in] Not used.
@param Key [in/out] Code of key pressed. Left unchanged.
@param Shift [in] State of modifier keys.
}
function GetLEDBounds(const ItemRect: TRect): TRect;
{Gets bounds of a LED glyph within an item rectangle in list box.
@param ItemRect [in] Rectangle containing LED glyph.
@return Bounding rectangle of glyph.
}
function GetDropDownBmpBounds(const ItemRect: TRect): TRectEx;
{Gets bounds of a drop down button glyph within an item rectangle in list
box.
@param ItemRect [in] Rectangle containing drop down button glyph.
@return Bounding rectangle of glyph.
}
procedure InvalidateLBRect(const R: TRect);
{Invalidates a specified rectangle in list box for redrawing.
@param R [in] Rectangle to be invalidated.
}
function IsMouseInRect(const Rect: TRectEx): Boolean;
{Checks if the mouse pointer is within a specified rectangle in list box.
@param Rect [in] Rectangle to be queried.
@return True if mouse pointer in rectangle, False if not.
}
procedure ShowPopupMenu(const ItemIdx: Integer);
{Displays popup menu over a list item that is used to select a compile
result.
@param ItemIdx [in] Index of list item for which menu is required.
}
procedure ThemesChangeHandler(Sender: TObject);
{Handles themes change event. Redisplays list box to use new themes.
@param Sender [in] Not used.
}
public
constructor Create(LB: TListBox; const Compilers: ICompilers);
{Class constructor. Sets up manager for a list box.
@param LB [in] Listbox to be managed.
@param Compilers [in] Compilers to be displayed in listbox.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
procedure SetCompileResults(const Res: TCompileResults); overload;
{Sets the compile results displayed by the list box. All items are set.
Each may have different value.
@param Res [in] Array of compiler results addressed by compiler ID.
}
procedure SetCompileResults(const Res: TCompileResult); overload;
{Sets all the compile results displayed by the list box to same value.
@param Res [in] Required compiler result.
}
procedure SetCurrentCompileResult(const Res: TCompileResult);
{Sets compile result of currently selected compiler.
@param Res [in] Required compile result.
}
function GetCompileResults: TCompileResults;
{Retrieves an array of all compile results per list box.
@return Results array, indexed by compiler ID.
}
function GetCurrentCompileResult: TCompileResult;
{Retrieves the compile result for the currently selected list item.
@return Require compile result.
}
end;
implementation
uses
// Delphi
SysUtils, Menus, Windows, Graphics,
// Project
UKeysHelper;
type
{
TCompResMenuItem:
Enhanced menu item that stores a compile result along with menu item.
Displayed as part of menu that is popped up from compile results list box.
Selecting the menu item updates current list box item to display the stored
compile result.
}
TCompResMenuItem = class(TMenuItem)
strict private
var fCompileResult: TCompileResult; // Value of CompileResult property
public
constructor Create(AOwner: TComponent; const ACompRes: TCompileResult;
const AClickEventHandler: TNotifyEvent); reintroduce;
{Class constructor. Creates menu item for a compile result.
@param AOwner [in] Component that owns this menu item.
@param ACompRes [in] Associated compile result. Determines caption and
displayed image.
@param AClickEventHandler [in] Event handler trigerred when menu item
clicked.
}
property CompileResult: TCompileResult read fCompileResult;
{Compile result associated with menu item}
end;
{
TCompResSelectMenu:
Special popup menu that is displayed by the compile results list box that
enables the compile result associated with a menu item to be edited.
}
TCompResSelectMenu = class(TPopupMenu)
strict private
var
fCompileResult: TCompileResult; // Value of CompileResult property
fOnSelect: TNotifyEvent; // OnSelect event handler
procedure MenuItemClickHandler(Sender: TObject);
{Handles clicks on all menu items. Records compile result associated with
selected menu item in CompileResult property and triggers OnSelect event.
@param Sender [in] Menu item that triggered event.
}
public
constructor Create(AOwner: TComponent); override;
{Class constructor. Creates menu and populates it with items for all
possible compile results.
@param AOwner [in] Component that owns the menu.
}
property CompileResult: TCompileResult read fCompileResult default crQuery;
{Last selected compiler result}
property OnSelect: TNotifyEvent read fOnSelect write fOnSelect;
{Event triggered when user selects a compile result}
end;
{ TCompileResultsLBMgr }
constructor TCompileResultsLBMgr.Create(LB: TListBox;
const Compilers: ICompilers);
{Class constructor. Sets up manager for a list box.
@param LB [in] Listbox to be managed.
@param Compilers [in] Compilers to be displayed in listbox.
}
begin
Assert(Assigned(LB), ClassName + '.Create: LB is nil');
Assert(Assigned(Compilers), ClassName + '.Create: Compilers is nil');
inherited Create;
// record compilers
fCompilers := Compilers;
// customise list box
fLB := LB;
fLB.Style := lbOwnerDrawFixed;
fLB.PopupMenu := TCompResSelectMenu.Create(fLB);
(fLB.PopupMenu as TCompResSelectMenu).OnSelect := MenuSelectHandler;
fLB.PopupMenu.Alignment := paRight;
// assign event handlers
fLB.OnDrawItem := DrawItem;
fLB.OnMouseDown := MouseDown;
fLB.OnMouseMove := MouseMove;
fLB.OnMouseLeave := MouseLeave;
fLB.OnKeyUp := KeyUp;
// create list box items and select first one
PopulateListBox;
fLB.ItemIndex := 0; // select first item
// create owned objects
fDropDownBtns := TDropDownButtons.Create(LB);
fDropDownBtns.OnChange := ThemesChangeHandler;
fLEDImages := TLEDImageList.Create(LB);
// record that no drop down button is under mouse
fLastHotDropDown.MakeEmpty;
end;
destructor TCompileResultsLBMgr.Destroy;
{Class destructor. Tears down object.
}
var
Idx: Integer; // loops through all list items
Tmp: TPopupMenu; // temporary menu reference used for thread safe freeing
begin
FreeAndNil(fDropDownBtns);
for Idx := Pred(fLB.Items.Count) downto 0 do
fLB.Items.Objects[Idx].Free; // free all compiler info records
// free list box's popup menu in thread safe way
Tmp := fLB.PopupMenu;
fLB.PopupMenu := nil;
FreeAndNil(Tmp);
inherited;
end;
procedure TCompileResultsLBMgr.DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
{OnDrawItem event handler for managed list box. Custom draws compiler list
item to show compiler glyph, name of compiler, current compile result "LED"
and a drop-down button.
@param Control [in] Reference to list box that triggered event. Must be the
managed control.
@param Index [in] Index of list item being drawn.
@param Rect [in] Rectangle in list box canvas where item being drawn.
@param State [in] State of list item.
}
var
Cvs: TCanvas; // list box's canvas
CompInfo: TCompilerInfo; // info about compile result associated with item
Text: string; // text to be displayed
TextRect: TRect; // rectangle in which to display text
CompGlyph: TBitmap; // gylph associated with compiler
GlyphRect: TRect; // rectangle in which to display compiler glyph
DropDownRect: TRect; // rectangle in which to display drop-down "button"
LEDRect: TRectEx; // rectangle in which to display LED glyph
begin
Assert(Control = fLB, ClassName + '.DrawItem: Control <> fLB');
// Store canvas and associated compiler info
Cvs := fLB.Canvas;
CompInfo := GetCompilerInfo(Index);
// Clear item's rectangle
Cvs.FillRect(Rect);
// Display text
Text := fLB.Items[Index];
TextRect := TRectEx.Create(
Rect.Left + 24,
(Rect.Bottom + Rect.Top - Cvs.TextHeight(Text)) div 2,
Rect.Right - 24,
Rect.Bottom
);
if odDisabled in State then
Cvs.Font.Color := clGrayText
else
Cvs.Font.Color := fLB.Font.Color;
Cvs.TextRect(TextRect, Text, [tfLeft, tfNoPrefix, tfEndEllipsis, tfTop]);
// Display any compiler glyph
CompGlyph := fCompilers[CompInfo.CompilerID].GetGlyph;
if Assigned(CompGlyph) then
begin
GlyphRect := TRectEx.CreateBounds(
Rect.Left + 2,
(Rect.Bottom + Rect.Top - CompGlyph.Height) div 2,
CompGlyph.Width,
CompGlyph.Height
);
Cvs.BrushCopy(
GlyphRect,
CompGlyph,
TRectEx.Create(0, 0, CompGlyph.Width, CompGlyph.Height),
clFuchsia
);
end;
// Display compile result "LED": assumes image index = Ord(CompileResult)
LEDRect := GetLEDBounds(Rect);
fLEDImages.Draw(Cvs, LEDRect.TopLeft, CompInfo.CompileResult);
// Display drop-down glyph
DropDownRect := GetDropDownBmpBounds(Rect);
fDropDownBtns.Draw(
Cvs,
DropDownRect.TopLeft,
IsMouseInRect(DropDownRect), // hot
(odSelected in State) and (odFocused in State) // focussed
);
end;
function TCompileResultsLBMgr.GetCompileResults: TCompileResults;
{Retrieves an array of all compile results per list box.
@return Results array, indexed by compiler ID.
}
var
CompID: TCompilerID; // loops through all elements of result array
begin
for CompID := Low(Result) to High(Result) do
Result[CompID] := GetCompilerInfo(CompID).CompileResult;
end;
function TCompileResultsLBMgr.GetCompilerInfo(
const Index: Integer): TCompilerInfo;
{Retrieves the compiler info associated with a specified list item.
@param Index [in] Index of list item.
@return Required compiler info.
}
begin
Result := fLB.Items.Objects[Index] as TCompilerInfo;
end;
function TCompileResultsLBMgr.GetCompilerInfo(
const CompID: TCompilerID): TCompilerInfo;
{Retrieves the compiler info associated with a specified compiler ID.
@param CompID [in] Compiler ID.
@return Required compiler info.
}
begin
Result := GetCompilerInfo(IndexOf(CompID));
end;
function TCompileResultsLBMgr.GetCurrentCompileResult: TCompileResult;
{Retrieves the compile result for the currently selected list item.
@return Require compile result.
}
begin
Result := GetCompilerInfo(fLB.ItemIndex).CompileResult;
end;
function TCompileResultsLBMgr.GetDropDownBmpBounds(
const ItemRect: TRect): TRectEx;
{Gets bounds of a drop down button glyph within an item rectangle in list box.
@param ItemRect [in] Rectangle containing drop down button glyph.
@return Bounding rectangle of glyph.
}
begin
Result := TRectEx.CreateBounds(
ItemRect.Right - 2 - fDropDownBtns.Images.Width,
(ItemRect.Bottom + ItemRect.Top - fDropDownBtns.Images.Height) div 2,
fDropDownBtns.Images.Width,
fDropDownBtns.Images.Height
);
end;
function TCompileResultsLBMgr.GetLEDBounds(const ItemRect: TRect): TRect;
{Gets bounds of a LED glyph within an item rectangle in list box.
@param ItemRect [in] Rectangle containing LED glyph.
@return Bounding rectangle of glyph.
}
begin
Result := TRectEx.CreateBounds(
ItemRect.Right - 2 - fLEDImages.Width - 2 - fDropDownBtns.Images.Width,
(ItemRect.Bottom + ItemRect.Top - fLEDImages.Height) div 2,
fLEDImages.Width,
fLEDImages.Height
);
end;
function TCompileResultsLBMgr.IndexOf(const CompID: TCompilerID): Integer;
{Finds index of list item corresponding to a compiler.
@param CompID [in] ID of compiler.
@return Index of corresponding list item or -1 if compiler not found.
}
var
Idx: Integer; // loops thru list box items
begin
Result := -1;
for Idx := 0 to Pred(fLB.Items.Count) do
if GetCompilerInfo(Idx).CompilerID = CompID then
begin
Result := Idx;
Break;
end;
Assert(Result >= 0, ClassName + '.IndexOf: CompID not found');
end;
procedure TCompileResultsLBMgr.InvalidateLBRect(const R: TRect);
{Invalidates a specified rectangle in list box for redrawing.
@param R [in] Rectangle to be invalidated.
}
begin
Windows.InvalidateRect(fLB.Handle, @R, False);
end;
function TCompileResultsLBMgr.IsMouseInRect(const Rect: TRectEx): Boolean;
{Checks if the mouse pointer is within a specified rectangle in list box.
@param Rect [in] Rectangle to be queried.
@return True if mouse pointer in rectangle, False if not.
}
begin
Result := Rect.ContainsPoint(fLB.ScreenToClient(Mouse.CursorPos));
end;
procedure TCompileResultsLBMgr.KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
{Handles list box's OnKeyUp event. Displays popup menu over any currently
selected list item if user pressed space bar with no modifier keys.
@param Sender [in] Not used.
@param Key [in/out] Code of key pressed. Left unchanged.
@param Shift [in] State of modifier keys.
}
begin
if fLB.ItemIndex = -1 then
Exit;
// only space with no shift keys is processed
if HasShiftKeys(Shift) or (Key <> VK_SPACE) then
Exit;
// space key pressed: display menu
ShowPopupMenu(fLB.ItemIndex);
end;
procedure TCompileResultsLBMgr.MenuSelectHandler(Sender: TObject);
{Handles menu selection on popup menu activated from drop down button. Sets
compile result of selected list item per menu selection.
@param Sender [in] Menu item that was clicked.
}
begin
SetCurrentCompileResult((Sender as TCompResSelectMenu).CompileResult);
end;
procedure TCompileResultsLBMgr.MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
{Handles list box's OnMouseDown event. Displays compile result menu if user
left clicked on drop down button with no modifier keys pressed.
@param Sender [in] Not used.
@param Button [in] Mouse button pressed.
@param Shift [in] Modifier keys etc.
@param X [in] X coordinate of mouse pointer in list box.
@param Y [in] Y coordinate of mouse pointer in list box.
}
var
ItemIdx: Integer; // list item under mouse (-1 if mouse not on list item)
MousePos: TPoint; // position of mouse
DDBounds: TRectEx; // bounds of drop down button
begin
// left mouse button must have been pressed with no modifier keys
if HasShiftKeys(Shift) or (Button <> mbLeft) then
Exit;
// find list item from modifier keys
MousePos := Point(X, Y);
ItemIdx := fLB.ItemAtPos(MousePos, True);
if ItemIdx = -1 then
Exit;
// find if drop down button pressed in list item and display menu if so
DDBounds := GetDropDownBmpBounds(fLB.ItemRect(ItemIdx));
if not DDBounds.ContainsPoint(MousePos) then
Exit;
ShowPopupMenu(ItemIdx);
end;
procedure TCompileResultsLBMgr.MouseLeave(Sender: TObject);
{Handles list box's OnMouseLeave event. Redisplays any drop down button that
was displayed as hot.
@param Sender [in] Not used.
}
begin
if not fLastHotDropDown.IsEmpty then
begin
InvalidateLBRect(fLastHotDropDown);
fLastHotDropDown.MakeEmpty;
end;
end;
procedure TCompileResultsLBMgr.MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
{Handles list box's OnMouseMove event. Updates display of hot and normal drop
down buttons depending on whether mouse is over such a button.
@param Sender [in] List box that triggered event. Not used.
@param Shift [in] State of shift keys. Not used.
@param X [in] X coordinate of mouse pointer in list box.
@param Y [in] Y coordinate of mouse pointer in list box.
}
var
ItemIdx: Integer; // index of list item under mouse (-1 if none)
DropDownRect: TRectEx; // bounds list item's drop down button
begin
// find list item under mouse, if any
ItemIdx := fLB.ItemAtPos(Point(X, Y), True);
if ItemIdx >= 0 then
begin
// there is a list item under mouse: find bounds of item's drop down button
DropDownRect := GetDropDownBmpBounds(fLB.ItemRect(ItemIdx));
if DropDownRect.ContainsPoint(Point(X, Y)) then
begin
// we are inside drop down button
if DropDownRect <> fLastHotDropDown then
begin
// button has changed: need to redraw
if not fLastHotDropDown.IsEmpty then
begin
// redraw old drop down: no longer hot
InvalidateLBRect(fLastHotDropDown);
end;
// redraw new drop down: now hot
InvalidateLBRect(DropDownRect);
// record current drop down
fLastHotDropDown := DropDownRect;
end;
end
else
begin
// not inside drop down button
if not fLastHotDropDown.IsEmpty then
begin
// redraw old drop down: no longer hot
InvalidateLBRect(fLastHotDropDown);
fLastHotDropDown.MakeEmpty;
end;
end;
end
else
begin
// not inside any item
if not fLastHotDropDown.IsEmpty then
begin
// redraw old drop down: no longer hot
InvalidateLBRect(fLastHotDropDown);
fLastHotDropDown.MakeEmpty;
end;
end;
end;
procedure TCompileResultsLBMgr.PopulateListBox;
{Adds details of each compiler to list box with unknown compile results.
}
var
Compiler: ICompiler; // each supported compiler
begin
// Display all compilers
for Compiler in fCompilers do
fLB.Items.AddObject(
Compiler.GetName,
TCompilerInfo.Create(Compiler.GetID, crQuery)
);
end;
procedure TCompileResultsLBMgr.SetCompileResult(const Index: Integer;
const Res: TCompileResult);
{Sets compile result of a list item.
@param Index [in] Index of list item.
@param Res [in] Required compiler result.
}
begin
GetCompilerInfo(Index).CompileResult := Res;
InvalidateLBRect(GetLEDBounds(fLB.ItemRect(Index)));
end;
procedure TCompileResultsLBMgr.SetCompileResults(const Res: TCompileResult);
{Sets all the compile results displayed by the list box to same value.
@param Res [in] Required compiler result.
}
var
Idx: Integer; // loops through all list items
begin
for Idx := 0 to Pred(fLB.Items.Count) do
SetCompileResult(Idx, Res);
end;
procedure TCompileResultsLBMgr.SetCompileResults(const Res: TCompileResults);
{Sets the compile results displayed by the list box. All items are set. Each
may have different value.
@param Res [in] Array of compiler results addressed by compiler ID.
}
var
CompID: TCompilerID; // loops through all compiler ids
begin
for CompID := Low(Res) to High(Res) do
SetCompileResult(IndexOf(CompID), Res[CompID]);
end;
procedure TCompileResultsLBMgr.SetCurrentCompileResult(
const Res: TCompileResult);
{Sets compile result of currently selected compiler.
@param Res [in] Required compile result.
}
begin
SetCompileResult(fLB.ItemIndex, Res);
end;
procedure TCompileResultsLBMgr.ShowPopupMenu(const ItemIdx: Integer);
{Displays popup menu over a list item that is used to select a compile result.
@param ItemIdx [in] Index of list item for which menu is required.
}
var
DropDownBmpBounds: TRectEx; // bounds of list item's drop down button
PopupPos: TPoint; // point on screen where menu is popup
begin
// display right aligned menu below drop down button
DropDownBmpBounds := GetDropDownBmpBounds(fLB.ItemRect(ItemIdx));
PopupPos := fLB.ClientToScreen(DropDownBmpBounds.BottomRight);
fLB.PopupMenu.Popup(PopupPos.X, PopupPos.Y);
end;
procedure TCompileResultsLBMgr.ThemesChangeHandler(Sender: TObject);
{Handles themes change event. Redisplays list box to use new themes.
@param Sender [in] Not used.
}
begin
fLB.Invalidate;
end;
{ TCompileResultsLBMgr.TCompilerInfo }
constructor TCompileResultsLBMgr.TCompilerInfo.Create(
const CompilerID: TCompilerID; const CompileResult: TCompileResult);
{Class constructor. Sets up and initialises object.
@param CompilerID [in] Id of compiler that result applies to.
@param CompileResult [in] Compiler result for compiler.
}
begin
inherited Create;
fCompilerID := CompilerID;
fCompileResult := CompileResult;
end;
{ TCompResMenuItem }
constructor TCompResMenuItem.Create(AOwner: TComponent;
const ACompRes: TCompileResult; const AClickEventHandler: TNotifyEvent);
{Class constructor. Creates menu item for a compile result.
@param AOwner [in] Component that owns this menu item.
@param ACompRes [in] Associated compile result. Determines caption and
displayed image.
@param AClickEventHandler [in] Event handler trigerred when menu item
clicked.
}
resourcestring
// Text for list items in Compiler Result list box
sSuccess = 'Success';
sWarning = 'Warning';
sError = 'Error';
sQuery = 'Unknown';
const
// Map of compiler results onto descriptions
cCompResCaptions: array[TCompileResult] of string = (
sSuccess, sWarning, sError, sQuery
);
begin
Assert(Assigned(AClickEventHandler),
ClassName + '.Create: AClickEventHandler not assigned');
inherited Create(AOwner);
Caption := cCompResCaptions[ACompRes];
fCompileResult := ACompRes;
ImageIndex := Ord(ACompRes); // image list index must map to compile result
OnClick := AClickEventHandler;
end;
{ TCompResSelectMenu }
constructor TCompResSelectMenu.Create(AOwner: TComponent);
{Class constructor. Creates menu and populates it with items for all possible
compile results.
@param AOwner [in] Component that owns the menu.
}
var
CompRes: TCompileResult; // loops thru all possible compile results
begin
inherited;
AutoPopup := False;
// Create image list containing compiler result LEDs
Images := TLEDImageList.Create(AOwner);
// Populate menu
for CompRes := Low(TCompileResult) to High(TCompileResult) do
Items.Add(TCompResMenuItem.Create(Self, CompRes, MenuItemClickHandler));
// Set default "unknown" compiler result
fCompileResult := crQuery;
end;
procedure TCompResSelectMenu.MenuItemClickHandler(Sender: TObject);
{Handles clicks on all menu items. Records compile result associated with
selected menu item in CompileResult property and triggers OnSelect event.
@param Sender [in] Menu item that triggered event.
}
begin
fCompileResult := (Sender as TCompResMenuItem).CompileResult;
if Assigned(fOnSelect) then
fOnSelect(Self);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.