Debian Codegraph @

tools/castle-editor/components/mbColorLib/mbColorTree.pas

raw
1 unit mbColorTree;
2
3 {$MODE DELPHI}
4
5 interface
6
7 uses
8 LCLIntf, LCLType, SysUtils, Classes, Controls, ComCtrls, Graphics, Themes,
9 GraphUtil, ImgList, Forms,
10 HTMLColors;
11
12 type
13 TmbColor = record
14 Name: string;
15 Value: TColor;
16 end;
17
18 TDrawCaptionEvent = procedure (Sender: TObject; AIndex: integer; AFont: TFont; var AText: string; Selected: boolean) of object;
19 TDrawLabelEvent = procedure (Sender: TObject; AIndex: integer; AFont: TFont; var AText: string) of object;
20 TGetHintEvent = procedure (AIndex: integer; var AHint: string; var Handled: boolean) of object;
21
22 TmbColorTree = class(TCustomTreeView)
23 private
24 FInfo1, FInfo2: string;
25 FInfoLabel: string;
26 FDraw: TDrawCaptionEvent;
27 FDraw1, FDraw2, FDraw3: TDrawLabelEvent;
28 mx, my: integer;
29 FGetHint: TGetHintEvent;
30 FOnStartDrag: TStartDragEvent;
31 FOnEndDrag: TEndDragEvent;
32 procedure SetInfo1(Value: string);
33 procedure SetInfo2(Value: string);
34 procedure SetInfoLabel(Value: string);
35 protected
36 procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW;
37 function CustomDrawItem(Node: TTreeNode; State: TCustomDrawState;
38 {%H-}Stage: TCustomDrawStage; var {%H-}PaintImages: Boolean): Boolean; override;
39 procedure DoArrow(c: TCanvas; dir: TScrollDirection; p: TPoint; sel: boolean);
40 procedure DrawColorItem(R: TRect; Selected: boolean; AIndex: Integer;
41 AItemText: String; Expanded: boolean); dynamic;
42 procedure DrawInfoItem(R: TRect; Index: integer); dynamic;
43 function IsCustomDrawn({%H-}Target: TCustomDrawTarget; {%H-}Stage: TCustomDrawStage): Boolean; override;
44 procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
45 procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
46 public
47 Colors: array of TmbColor;
48 constructor Create(AOwner: TComponent); override;
49 procedure AddColor(AName: string; AValue: TColor; ARefresh: boolean = true);
50 procedure ClearColors;
51 function ColorCount: integer;
52 procedure DeleteColor(AIndex: integer; ARefresh: boolean = true);
53 procedure DeleteColorByName(AName: string; All: boolean);
54 procedure DeleteColorByValue(AValue: TColor; All: boolean);
55 procedure InsertColor(AIndex: integer; AName: string; AValue: TColor);
56 procedure UpdateColors;
57 published
58 property InfoLabelText: string read FInfoLabel write SetInfoLabel;
59 property InfoDisplay1: string read FInfo1 write SetInfo1;
60 property InfoDisplay2: string read FInfo2 write SetInfo2;
61 property Align;
62 property Anchors;
63 property AutoExpand;
64 property BorderStyle;
65 property BorderWidth;
66 property Constraints;
67 property Color;
68 property DragKind;
69 property DragCursor;
70 property DragMode;
71 property Enabled;
72 property Font;
73 property Indent;
74 property ParentColor default False;
75 property ParentFont;
76 property ParentShowHint;
77 property PopupMenu;
78 property RightClickSelect;
79 property ShowHint;
80 property SortType;
81 property TabOrder;
82 property TabStop default True;
83 property ToolTips;
84 property Visible;
85 property OnGetHint: TGetHintEvent read FGetHint write FGetHint;
86 property OnDrawCaption: TDrawCaptionEvent read FDraw write FDraw;
87 property OnDrawInfoLabel: TDrawLabelEvent read FDraw1 write FDraw1;
88 property OnDrawInfoDisplay1: TDrawLabelEvent read FDraw2 write FDraw2;
89 property OnDrawInfoDisplay2: TDrawLabelEvent read FDraw3 write FDraw3;
90 property OnAdvancedCustomDraw;
91 property OnAdvancedCustomDrawItem;
92 property OnChange;
93 property OnChanging;
94 property OnClick;
95 property OnCollapsed;
96 property OnCollapsing;
97 property OnCompare;
98 property OnContextPopup;
99 property OnCustomDraw;
100 property OnCustomDrawItem;
101 property OnDblClick;
102 property OnDeletion;
103 property OnDragDrop;
104 property OnDragOver;
105 property OnEndDock;
106 property OnEndDrag: TEndDragEvent read FOnEndDrag write FOnEndDrag;
107 property OnEnter;
108 property OnExit;
109 property OnExpanding;
110 property OnExpanded;
111 property OnKeyDown;
112 property OnKeyPress;
113 property OnKeyUp;
114 property OnMouseDown;
115 property OnMouseMove;
116 property OnMouseUp;
117 property OnStartDock;
118 property OnStartDrag: TStartDragEvent read FOnStartDrag write FOnStartDrag;
119 property Items;
120 end;
121
122 implementation
123
124 uses
125 PalUtils, mbUtils;
126
127 { TmbColorTree }
128
129 constructor TmbColorTree.Create(AOwner: TComponent);
130 begin
131 inherited;
132 ControlStyle := ControlStyle + [csDisplayDragImage];
133 ReadOnly := true;
134 ShowButtons := false;
135 ShowLines := false;
136 ShowRoot := true;
137 RowSelect := true;
138 HotTrack := false;
139 SetLength(Colors, 0);
140 Images := TImageList.Create(Self);
141 Images.Width := 48;
142 Images.Height := 48;
143 FInfoLabel := 'Color Values:';
144 FInfo1 := 'RGB: %r.%g.%b';
145 FInfo2 := 'HEX: #%hex';
146 end;
147
148 procedure TmbColorTree.AddColor(AName: string; AValue: TColor;
149 ARefresh: boolean = true);
150 var
151 L: integer;
152 begin
153 L := Length(Colors);
154 SetLength(Colors, L + 1);
155 Colors[L].Name := AName;
156 Colors[L].Value := AValue;
157 if ARefresh then
158 UpdateColors;
159 end;
160
161 procedure TmbColorTree.ClearColors;
162 begin
163 SetLength(Colors, 0);
164 UpdateColors;
165 end;
166
167 procedure TmbColorTree.CMHintShow(var Message: TCMHintShow);
168 var
169 Handled: boolean;
170 i: integer;
171 n: TTreeNode;
172 begin
173 if PtInRect(ClientRect, Point(mx, my)) and ShowHint and not Dragging then
174 begin
175 n := GetNodeAt(mx, my);
176 if n <> nil then
177 begin
178 if not n.HasChildren then
179 i := n.Parent.Index
180 else
181 i := n.Index;
182 with TCMHintShow(Message) do
183 if not ShowHint then
184 Message.Result := 1
185 else
186 with HintInfo^ do
187 begin
188 Result := 0;
189 ReshowTimeout := 2000;
190 HideTimeout := 1000;
191 Handled := false;
192 if Assigned(FGetHint) then
193 FGetHint(i, HintStr, Handled);
194 if Handled then
195 HintStr := FormatHint(HintStr, Colors[i].Value)
196 else
197 HintStr := Colors[i].Name;
198 end;
199 end;
200 end;
201 inherited;
202 end;
203
204 function TmbColorTree.ColorCount: integer;
205 begin
206 Result := Length(Colors);
207 end;
208
209 function TmbColorTree.CustomDrawItem(Node: TTreeNode; State: TCustomDrawState;
210 Stage: TCustomDrawStage; var PaintImages: Boolean): Boolean;
211 begin
212 Result := true;
213 if Length(Colors) = 0 then Exit;
214 if Node.HasChildren then
215 DrawColorItem(Node.DisplayRect(false), cdsSelected in State, node.Index, node.Text, node.Expanded)
216 else
217 DrawInfoItem(Node.DisplayRect(false), node.Parent.Index);
218 end;
219
220 procedure TmbColorTree.DeleteColorByValue(AValue: TColor; All: boolean);
221 var
222 i: integer;
223 begin
224 for i := Length(Colors) - 1 downto 0 do
225 if Colors[i].Value = AValue then
226 begin
227 DeleteColor(i, false);
228 if not All then
229 begin
230 UpdateColors;
231 Exit;
232 end;
233 end;
234 UpdateColors;
235 end;
236
237 procedure TmbColorTree.DoArrow(c: TCanvas; dir: TScrollDirection; p: TPoint;
238 sel: boolean);
239 var
240 b: TBitmap;
241 begin
242 b := TBitmap.Create;
243 try
244 b.Height := 12;
245 b.Width := 12;
246 if Sel then
247 begin
248 b.Canvas.Brush.Color := clHighlight;
249 b.Canvas.Pen.Color := clHighlightText;
250 end
251 else
252 begin
253 b.Canvas.Brush.Color := clFuchsia;
254 b.Canvas.Pen.Color := clWindowText;
255 b.Transparent := true;
256 b.TransparentColor := clFuchsia;
257 end;
258 b.Canvas.FillRect(B.Canvas.ClipRect);
259 case dir of
260 sdDown : DrawArrow(b.Canvas, dir, Point(2, 3), 3);
261 sdRight : DrawArrow(b.Canvas, dir, Point(1, 2), 3);
262 end;
263 c.Draw(p.x, p.y, b);
264 finally
265 b.Free;
266 end;
267 end;
268
269 procedure TmbColorTree.DrawColorItem(R: TRect; Selected: boolean; AIndex: integer;
270 AItemText: string; Expanded: boolean);
271 const
272 FLAGS = DT_LEFT or DT_NOCLIP or DT_END_ELLIPSIS;
273 var
274 SR, TR: TRect;
275 begin
276 with Canvas do
277 begin
278 //background
279 Pen.Color := clWindow;
280 if Selected then
281 Brush.Color := clHighlight
282 else
283 Brush.Color := Color;
284 FillRect(R);
285 MoveTo(R.Left, R.Bottom - 1);
286 LineTo(R.Right, R.Bottom - 1);
287
288 //swatches
289 SR := Rect(R.Left + 6, R.Top + 6, R.Left + 42, R.Top + 42);
290 Brush.Color := Self.Colors[AIndex].value;
291 if Selected then
292 begin
293 if ThemeServices.ThemesEnabled then
294 begin
295 ThemeServices.DrawElement(Canvas.Handle,
296 ThemeServices.GetElementDetails(teEditTextNormal), SR);
297 InflateRect(SR, -2, -2);
298 Brush.Color := Blend(Self.Colors[AIndex].value, clBlack, 80);
299 FillRect(SR);
300 InflateRect(SR, -1, -1);
301 Brush.Color := Blend(Self.Colors[AIndex].value, clBlack, 90);
302 FillRect(SR);
303 InflateRect(SR, -1, -1);
304 Brush.Color := Self.Colors[AIndex].value;
305 FillRect(SR);
306 end
307 else
308 //windows 9x
309 begin
310 Pen.Color := clBackground;
311 Brush.Color := clWindow;
312 Rectangle(SR);
313 InflateRect(SR, -1, -1);
314 FillRect(SR);
315 InflateRect(SR, 1, 1);
316 InflateRect(SR, -2, -2);
317 Brush.Color := Blend(Self.Colors[AIndex].value, clBlack, 75);
318 FillRect(SR);
319 InflateRect(SR, -1, -1);
320 Brush.Color := Blend(Self.Colors[AIndex].value, clBlack, 87);
321 FillRect(SR);
322 InflateRect(SR, -1, -1);
323 Brush.Color := Self.Colors[AIndex].value;
324 FillRect(SR);
325 end;
326 end
327 else
328 //not selected
329 begin
330 //windows XP
331 if ThemeServices.ThemesEnabled then
332 begin
333 ThemeServices.DrawElement(Canvas.Handle, ThemeServices.GetElementDetails(teEditTextNormal), SR);
334 InflateRect(SR, -2, -2);
335 Brush.Color := Self.Colors[AIndex].value;
336 FillRect(SR);
337 end
338 else
339 //windows 9x
340 begin
341 DrawEdge(Canvas.Handle, SR, BDR_SUNKENOUTER, BF_RECT);
342 InflateRect(SR, -2, -2);
343 Brush.Color := Self.Colors[AIndex].value;
344 Pen.Color := clBlack;
345 Rectangle(SR);
346 InflateRect(SR, -1, -1);
347 FillRect(SR);
348 InflateRect(SR, 1, 1);
349 end;
350 end;
351 //names
352 Font.Style := [fsBold];
353 if Selected then
354 begin
355 //Brush.Color := clHighlightText;
356 Pen.Color := clHighlightText;
357 Font.Color := clHighlightText;
358 end
359 else
360 begin
361 //Brush.Color := clWindowText;
362 Pen.Color := clWindowText;
363 Font.Color := clWindowText;
364 end;
365 TR := Rect(R.Left + 48, R.Top + (48 - TextHeight(AItemText)) div 2, R.Right - 15, R.Bottom);
366 if Assigned(FDraw) then FDraw(Self, AIndex, Canvas.Font, AItemText, Selected);
367 SetBkMode(Canvas.Handle, TRANSPARENT);
368 DrawText(Canvas.Handle, PChar(AItemText), Length(AItemText), TR, FLAGS);
369 SetBkMode(Canvas.Handle, OPAQUE);
370 if R.Right > 60 then
371 begin
372 if Expanded then
373 DoArrow(Canvas, sdDown, Point(R.Right - 13, R.Top + 20), selected)
374 else
375 DoArrow(Canvas, sdRight, Point(R.Right - 10, R.Top + 18), selected);
376 end;
377 end;
378 end;
379
380 procedure TmbColorTree.DrawInfoItem(R: TRect; Index: integer);
381 const
382 FLAGS = DT_LEFT or DT_END_ELLIPSIS or DT_NOCLIP;
383 DELTA = 2;
384 var
385 b: TBitmap;
386 BR, TR: TRect;
387 s: string;
388 h: Integer;
389 begin
390 b := TBitmap.Create;
391 try
392 b.Width := R.Right - R.Left;
393 b.Height := R.Bottom - R.Top;
394 BR := b.Canvas.ClipRect;
395 with b.Canvas do
396 begin
397 Canvas.Font.Assign(Self.Font);
398 Brush.Color := Blend(clBtnFace, clWindow, 30);
399 FillRect(BR);
400 BR := Rect(BR.Left + 42, BR.Top, BR.Right, BR.Bottom);
401 FillRect(BR);
402 Inc(BR.Left, 6);
403 Font.Style := [];
404 Font.Size := 7;
405
406 s := FInfoLabel;
407 h := TextHeight(s);
408 TR := Rect(BR.Left, BR.Top{ + 2}, BR.Right, BR.Top + {2 + }h + DELTA);
409 if Assigned(FDraw1) then FDraw1(Self, Index, Canvas.Font, s);
410 DrawText(b.Canvas.Handle, PChar(s), Length(s), TR, FLAGS);
411
412 DrawHorDottedLine(b.Canvas, BR.Left, BR.Right, TR.Bottom + DELTA, clGray);
413
414 s := FormatHint(FInfo1, Self.Colors[Index].value);
415 TR.Top := TR.Bottom + 2 * DELTA;
416 TR.Bottom := TR.Top + h + DELTA;
417 if Assigned(FDraw2) then FDraw2(Self, Index, Canvas.Font, s);
418 DrawText(b.Canvas.Handle, PChar(s), Length(s), TR, FLAGS);
419
420 DrawHorDottedLine(b.Canvas, BR.LEft, BR.Right, TR.Bottom + DELTA, clGray);
421
422 s := FormatHint(FInfo2, Self.Colors[Index].value);
423 TR.Top := TR.Bottom + 2 * DELTA;
424 TR.Bottom := TR.Top + h + DELTA;
425 if Assigned(FDraw3) then FDraw3(Self, Index, Canvas.Font, s);
426 DrawText(b.Canvas.Handle, PChar(s), Length(s), TR, FLAGS);
427 end;
428
429 Canvas.Draw(R.Left, R.Top, b);
430 finally
431 b.Free;
432 end;
433 end;
434
435 procedure TmbColorTree.InsertColor(AIndex: integer; AName: string; AValue: TColor);
436 var
437 i: integer;
438 begin
439 if AIndex > Length(Colors) - 1 then
440 raise Exception.Create(Format('List index out of bounds (%d)', [AIndex]));
441
442 SetLength(Colors, Length(Colors) + 1);
443 for i := Length(Colors) - 1 downto AIndex do
444 Colors[i] := Colors[i-1];
445
446 Colors[AIndex].Name := AName;
447 Colors[AIndex].Value := AValue;
448
449 UpdateColors;
450 end;
451
452 function TmbColorTree.IsCustomDrawn(Target: TCustomDrawTarget;
453 Stage: TCustomDrawStage): Boolean;
454 begin
455 Result := true;
456 end;
457
458 procedure TmbColorTree.MouseDown(Button: TMouseButton; Shift: TShiftState;
459 X, Y: Integer);
460 var
461 r: TRect;
462 begin
463 inherited;
464 if (ssShift in Shift) or (ssCtrl in Shift) then
465 Exit;
466 if Selected <> nil then
467 r := Selected.DisplayRect(false)
468 else
469 exit;
470 if (x > r.Right - 15) and (x < r.Right - 3) and (y > r.Top + 13) and (y < r.Top + 30) then
471 if (Selected.HasChildren) and PtInRect(r, Point(x, y)) then
472 begin
473 if selected.Expanded then
474 Selected.Collapse(false)
475 else
476 Selected.Expand(false);
477 Invalidate;
478 end;
479 end;
480
481 procedure TmbColorTree.MouseMove(Shift: TShiftState; X, Y: Integer);
482 var
483 r: TRect;
484 begin
485 inherited;
486 mx := x;
487 my := y;
488 if GetNodeAt(x, y) <> nil then
489 r := GetNodeAt(x, y).DisplayRect(false)
490 else
491 begin
492 Cursor := crDefault;
493 exit;
494 end;
495
496 if (x > r.Right - 15) and (x < r.Right - 3) and (y > r.Top + 13) and (y < r.Top + 30) then
497 begin
498 if (GetNodeAt(x, y).HasChildren) and PtInRect(r, Point(x, y)) then
499 Cursor := crHandPoint
500 else
501 Cursor := crDefault;
502 end
503 else
504 Cursor := crDefault;
505 end;
506
507 procedure TmbColorTree.SetInfoLabel(Value: string);
508 begin
509 if FInfoLabel <> Value then
510 begin
511 FInfoLabel := Value;
512 Invalidate;
513 end;
514 end;
515
516 procedure TmbColorTree.SetInfo1(Value: string);
517 begin
518 if FInfo1 <> Value then
519 begin
520 FInfo1 := Value;
521 Invalidate;
522 end;
523 end;
524
525 procedure TmbColorTree.SetInfo2(Value: string);
526 begin
527 if FInfo2 <> Value then
528 begin
529 FInfo2 := Value;
530 Invalidate;
531 end;
532 end;
533
534 procedure TmbColorTree.DeleteColor(AIndex: integer; ARefresh: boolean = true);
535 var
536 i: integer;
537 begin
538 if Length(Colors) = 0 then
539 raise Exception.Create('There''s nothing to delete! The length of the array is 0.');
540
541 if AIndex > Length(Colors) - 1 then
542 raise Exception.Create(Format('List index out of bounds (%d)', [AIndex]));
543
544 for i := AIndex to Length(Colors) - 2 do
545 Colors[i] := Colors[i+1];
546 SetLength(Colors, Length(Colors) - 1);
547 if ARefresh then
548 UpdateColors;
549 end;
550
551 procedure TmbColorTree.DeleteColorByName(AName: string; All: boolean);
552 var
553 i: integer;
554 begin
555 for i := Length(Colors) - 1 downto 0 do
556 if SameText(Colors[i].Name, AName) then
557 begin
558 DeleteColor(i, false);
559 if not All then
560 begin
561 UpdateColors;
562 Exit;
563 end;
564 end;
565 UpdateColors;
566 end;
567
568 procedure TmbColorTree.UpdateColors;
569 var
570 i: integer;
571 n: TTreeNode;
572 begin
573 Items.Clear;
574 for i := 0 to Length(Colors) - 1 do
575 begin
576 n := Items.Add(TopItem, Colors[i].name);
577 Items.AddChild(n, '');
578 end;
579 end;
580
581 end.