Debian Codegraph @

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

raw
1 unit mbColorPalette;
2
3 //{$MODE DELPHI}
4 {$MODE ObjFPC}{$H+}
5
6 interface
7
8 uses
9 LCLIntf, LCLType, LMessages, SysUtils, Classes, Controls, Graphics,
10 Forms, Dialogs, Themes,
11 HTMLColors, PalUtils, mbBasicPicker;
12
13 type
14 TMouseLoc = (mlNone, mlOver, mlDown);
15 TTransparentStyle = (tsPhotoshop, tsPhotoshop2, tsCorel, tsMicroangelo, tsNone);
16 TCellStyle = (csDefault, csCorel);
17 TColorCellState = (ccsNone, ccsOver, ccsDown, ccsChecked, ccsCheckedHover);
18 TMoveDirection = (mdLeft, mdRight, mdUp, mdDown);
19 TPaintCellEvent = procedure (ACanvas: TCanvas; ACellRect: TRect; AColor: TColor; Index: integer; AState: TColorCellState; var AStyle: TTransparentStyle; var PaintingHandled: boolean) of object;
20 TCellClickEvent = procedure (Button: TMouseButton; Shift: TShiftState; Index: integer; AColor: TColor; var DontCheck: boolean) of object;
21 TGetHintTextEvent = procedure (AColor: TColor; Index: integer; var HintStr: string; var Handled: boolean) of object;
22 TArrowKeyEvent = procedure (Key: Word; Shift: TShiftState) of object;
23
24 TmbColorPalette = class(TmbBasicPicker)
25 private
26 FMouseLoc: TMouseLoc;
27 FMouseOver, FMouseDown, FAutoHeight: boolean;
28 FColCount, FRowCount, FTop, FLeft, FIndex, FCheckedIndex, FCellSize, FTotalCells: integer;
29 FState: TColorCellState;
30 FColors, FNames: TStrings;
31 FPalette: TFileName;
32 FHintFormat: string;
33 FOnChange, FOnColorsChange: TNotifyEvent;
34 FMinColors, FMaxColors: integer;
35 FSort: TSortMode;
36 FOrder: TSortOrder;
37 FOld: TColor;
38 FOnPaintCell: TPaintCellEvent;
39 FTStyle: TTransparentStyle;
40 FOnCellClick: TCellClickEvent;
41 FOldIndex: integer;
42 FOnGetHintText: TGetHintTextEvent;
43 FCellStyle: TCellStyle;
44 FOnArrowKey: TArrowKeyEvent;
45 procedure CalcAutoHeight;
46 function GetMoveCellIndex(move: TMoveDirection): integer;
47 function GetSelColor: TColor;
48 function GetTotalRowCount: integer;
49 procedure LoadPalette(FileName: TFileName);
50 procedure SetAutoHeight(auto: boolean);
51 procedure SetCellSize(s: integer);
52 procedure SetCellStyle(s: TCellStyle);
53 procedure SetMaxColors(m: integer);
54 procedure SetMinColors(m: integer);
55 procedure SetNames(n: TStrings);
56 procedure SetSelColor(k: TColor);
57 procedure SetSortMode(s: TSortMode);
58 procedure SetSortOrder(s: TSortOrder);
59 procedure SetStrings(s: TStrings);
60 procedure SetTStyle(s: TTransparentStyle);
61 procedure SortColors;
62 protected
63 procedure ColorsChange(Sender: TObject);
64 procedure DrawCell(ACanvas: TCanvas; AColor: string);
65 procedure DrawCellBack(ACanvas: TCanvas; R: TRect; AIndex: integer);
66 function GetColorUnderCursor: TColor; override;
67 function GetHintStr({%H-}X, {%H-}Y: Integer): String; override;
68 function GetIndexUnderCursor: integer;
69 procedure KeyDown(var Key: Word; Shift: TShiftState); override;
70 procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
71 procedure MouseEnter; override;
72 procedure MouseLeave; override;
73 procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
74 procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
75 procedure Paint; override;
76 procedure PaintTransparentGlyph(ACanvas: TCanvas; R: TRect);
77 procedure Resize; override;
78 procedure SelectCell(i: integer);
79 procedure CMGotFocus(var Message: TLMessage); message CM_ENTER;
80 procedure CMHintShow(var Message: TLMessage); message CM_HINTSHOW;
81 procedure CMLostFocus(var Message: TLMessage); message CM_EXIT;
82
83 public
84 constructor Create(AOwner: TComponent); override;
85 destructor Destroy; override;
86 procedure GeneratePalette(BaseColor: TColor);
87 procedure GenerateGradientPalette(Colors: array of TColor);
88 function GetSelectedCellRect: TRect;
89 procedure SaveColorsAsPalette(FileName: TFileName);
90 property ColorUnderCursor;
91 property RowCount: integer read GetTotalRowCount;
92 property ColCount: integer read FColCount;
93 property IndexUnderCursor: integer read GetIndexUnderCursor;
94 property VisibleRowCount: integer read FRowCount;
95
96 published
97 property Align;
98 property Anchors;
99 property Enabled;
100 property SortMode: TSortMode read FSort write SetSortMode default smNone;
101 property SortOrder: TSortOrder read FOrder write SetSortOrder default soAscending;
102 property MinColors: integer read FMinColors write SetMinColors default 0;
103 property MaxColors: integer read FMaxColors write SetMaxColors default 0;
104 property SelectedCell: integer read FCheckedIndex write SelectCell default -1;
105 property SelectedColor: TColor read GetSelColor write SetSelColor default clNone;
106 property Colors: TStrings read FColors write SetStrings;
107 property Palette: TFileName read FPalette write LoadPalette;
108 property HintFormat: string read FHintFormat write FHintFormat;
109 property AutoHeight: boolean read FAutoHeight write SetAutoHeight default false;
110 property CellSize: integer read FCellSize write SetCellSize default 18;
111 property TransparentStyle: TTransparentStyle read FTStyle write SetTStyle default tsNone;
112 property CellStyle: TCellStyle read FCellStyle write SetCellStyle default csDefault;
113 property ColorNames: TStrings read FNames write SetNames;
114 property TabStop default true;
115 property TabOrder;
116 property ShowHint default false;
117 property Constraints;
118 property ParentShowHint default true;
119 property PopupMenu;
120 property Visible;
121 property DragCursor;
122 property DragKind;
123 property DragMode;
124 property OnDragDrop;
125 property OnDragOver;
126 property OnEndDock;
127 property OnEndDrag;
128 property OnStartDock;
129 property OnStartDrag;
130 property OnSelColorChange: TNotifyEvent read FOnChange write FOnChange;
131 property OnColorsChange: TNotifyEvent read FOnColorsChange write FOnColorsChange;
132 property OnPaintCell: TPaintCellEvent read FOnPaintCell write FOnPaintCell;
133 property OnCellClick: TCellClickEvent read FOnCellClick write FOnCellClick;
134 property OnGetHintText: TGetHintTextEvent read FOnGetHintText write FOnGetHintText;
135 property OnArrowKey: TArrowKeyEvent read FOnArrowKey write FOnArrowKey;
136 property OnContextPopup;
137 property OnMouseMove;
138 property OnMouseDown;
139 property OnMouseUp;
140 property OnKeyDown;
141 property OnKeyUp;
142 property OnKeyPress;
143 property OnResize;
144 property OnClick;
145 end;
146
147 implementation
148
149
150 { TmbColorPalette }
151
152 constructor TmbColorPalette.Create(AOwner: TComponent);
153 begin
154 inherited Create(AOwner);
155 // ControlStyle := ControlStyle - [csAcceptsControls] + [csOpaque];
156
157 TabStop := true;
158 ParentShowHint := true;
159 ShowHint := false;
160 SetInitialBounds(0, 0, 180, 126);
161 FMouseLoc := mlNone;
162 FMouseOver := false;
163 FMouseDown := false;
164 FColCount := 0;
165 FRowCount := 0;
166 FIndex := -1;
167 FCheckedIndex := -1;
168 FTop := 0;
169 FLeft := 0;
170 FCellSize := 18;
171 FState := ccsNone;
172 FNames := TStringList.Create;
173 FColors := TStringList.Create;
174 (FColors as TStringList).OnChange := @ColorsChange;
175 FTotalCells := 0;
176 FHintFormat := 'RGB(%r, %g, %b)'#13'Hex: #%hex';
177 FAutoHeight := false;
178 FMinColors := 0;
179 FMaxColors := 0;
180 FSort := smNone;
181 FOrder := soAscending;
182 FOld := clNone;
183 FTStyle := tsNone;
184 FCellStyle := csDefault;
185 end;
186
187 destructor TmbColorPalette.Destroy;
188 begin
189 //FBufferBmp.Free; -- is destroy by TmbBasicPicker
190 FNames.Free;
191 FColors.Free;
192 inherited Destroy;
193 end;
194
195 procedure TmbColorPalette.CalcAutoHeight;
196 begin
197 if Parent = nil then
198 exit;
199 FColCount := Width div FCellSize;
200 FRowCount := Height div FCellSize;
201 end;
202
203 procedure TmbColorPalette.CMGotFocus(var Message: TLMessage);
204 begin
205 inherited;
206 Invalidate;
207 end;
208
209 procedure TmbColorPalette.CMHintShow(var Message: TLMessage);
210 var
211 clr: TColor;
212 Handled: boolean;
213 cp: TPoint;
214 begin
215 if (Colors.Count > 0) and (FIndex > -1) then
216 with TCMHintShow(Message) do
217 begin
218 if not ShowHint then
219 Message.Result := 1
220 else
221 begin
222 with HintInfo^ do
223 begin
224 // show that we want a hint
225 Result := 0;
226 ReshowTimeout := 0; //1;
227 cp := CursorPos;
228 HintInfo^.CursorRect := Rect(cp.X, cp.Y, cp.X+1, cp.Y+1);
229 HideTimeout := Application.HintHidePause; // was: 5000
230 clr := GetColorUnderCursor;
231 //fire event
232 Handled := false;
233 if Assigned(FOnGetHintText) then
234 FOnGetHintText(clr, GetIndexUnderCursor, HintStr, Handled);
235 //do default
236 if not Handled then
237 HintStr := GetHintStr(CursorPos.X, CursorPos.Y);
238 end;
239 end;
240 end;
241 end;
242
243 procedure TmbColorPalette.CMLostFocus(var Message: TLMessage);
244 begin
245 inherited;
246 if FMouseOver then
247 FMouseLoc := mlOver
248 else
249 FMouseLoc := mlNone;
250 Invalidate;
251 end;
252
253 procedure TmbColorPalette.ColorsChange(Sender: TObject);
254 begin
255 if Assigned(FOnColorsChange) then
256 FOnColorsChange(Self);
257 FTotalCells := FColors.Count - 1;
258 CalcAutoHeight;
259 Invalidate;
260 end;
261
262 procedure TmbColorPalette.DrawCell(ACanvas: TCanvas; AColor: string);
263 var
264 R: Trect;
265 FCurrentIndex: integer;
266 c: TColor;
267 Handled: boolean;
268 begin
269 // set props
270 if (FLeft + 1) * FCellSize > Width then
271 begin
272 Inc(FTop);
273 FLeft := 0;
274 end;
275
276 FCurrentIndex := FTop * FColCount + FLeft;
277 R := Rect(FLeft * FCellSize, FTop * FCellSize, (FLeft + 1) * FCellSize, (FTop + 1) * FCellSize);
278
279 //start drawing
280
281 //get current state
282 if FCurrentIndex = FCheckedIndex then
283 begin
284 if FCheckedIndex = FIndex then
285 begin
286 if FMouseDown then
287 FState := ccsDown
288 else
289 FState := ccsCheckedHover;
290 end
291 else
292 FState := ccsChecked;
293 end
294 else
295 if FIndex = FCurrentIndex then
296 case FMouseLoc of
297 mlNone: FState := ccsNone;
298 mlOver: FState := ccsOver;
299 end
300 else
301 FState := ccsNone;
302
303 //paint
304 DrawCellBack(ACanvas, R, FCurrentIndex);
305
306 // fire the event
307 Handled := false;
308 c := mbStringToColor(AColor);
309 if Assigned(FOnPaintCell) then
310 case FCellStyle of
311 csDefault:
312 FOnPaintCell(ACanvas, R, c, FCurrentIndex, FState, FTStyle, Handled);
313 csCorel:
314 if FColCount = 1 then
315 FOnPaintCell(ACanvas, R, c, FCurrentIndex, FState, FTStyle, Handled)
316 else
317 FOnPaintCell(ACanvas, Rect(R.Left, R.Top, R.Right + 1, R.Bottom), c,
318 FCurrentIndex, FState, FTStyle, Handled);
319 end;
320
321 if not Handled then
322 begin
323 // if standard colors draw the rect
324 if not SameText(AColor, 'clCustom') and not SameText(AColor, 'clTransparent') then
325 case FCellStyle of
326 csDefault:
327 begin
328 InflateRect(R, -3, -3);
329 if Enabled then
330 begin
331 ACanvas.Brush.Color := c;
332 ACanvas.Pen.Color := clBtnShadow;
333 end
334 else
335 begin
336 ACanvas.Brush.Color := clGray;
337 ACanvas.Pen.Color := clGray;
338 end;
339 ACanvas.Rectangle(R);
340 Exit;
341 end;
342
343 csCorel:
344 begin
345 if (FState <> ccsNone) then
346 InflateRect(R, -2, -2)
347 else
348 begin
349 Inc(R.Left);
350 Dec(R.Bottom);
351 if R.Top <= 1 then
352 Inc(R.Top);
353 if R.Right = Width then
354 Dec(R.Right);
355 end;
356 if Enabled then
357 ACanvas.Brush.Color := c
358 else
359 ACanvas.Brush.Color := clGray;
360 ACanvas.FillRect(R);
361 Exit;
362 end;
363 end;
364
365 //if transparent draw the glyph
366 if SameText(AColor, 'clTransparent') then
367 PaintTransparentGlyph(ACanvas, R);
368 end;
369 end;
370
371 procedure TmbColorPalette.DrawCellBack(ACanvas: TCanvas; R: TRect; AIndex: integer);
372 begin
373 case FCellStyle of
374 csDefault:
375 begin
376 if ThemeServices.ThemesEnabled then
377 begin
378 with ThemeServices do
379 if Enabled then
380 case FState of
381 ccsNone: ; //PaintParentBack(ACanvas, R);
382 // ccsNone: ACanvas.CopyRect(R, PBack.Canvas, R);
383 ccsOver: DrawElement(ACanvas.Handle, GetElementDetails(ttbButtonHot), R);
384 ccsDown: DrawElement(ACanvas.Handle, GetElementDetails(ttbButtonPressed), R);
385 ccsChecked: DrawElement(ACanvas.Handle, GetElementDetails(ttbButtonChecked), R);
386 ccsCheckedHover: DrawElement(ACanvas.Handle, GetElementDetails(ttbButtonCheckedHot), R);
387 end
388 else
389 DrawElement(ACanvas.Handle, GetElementDetails(ttbButtonDisabled), R);
390 end
391 else
392 if Enabled then
393 case FState of
394 ccsNone: ACanvas.FillRect(R);
395 ccsOver: DrawEdge(ACanvas.Handle, R, BDR_RAISEDINNER, BF_RECT);
396 ccsDown, ccsChecked, ccsCheckedHover: DrawEdge(ACanvas.Handle, R, BDR_SUNKENOUTER, BF_RECT);
397 end
398 else
399 DrawFrameControl(ACanvas.Handle, R, DFC_BUTTON, 0 or DFCS_BUTTONPUSH or DFCS_FLAT or DFCS_INACTIVE);
400 end;
401
402 csCorel:
403 begin
404 if Enabled then
405 begin
406 if ThemeServices.ThemesEnabled then
407 case FState of
408 ccsNone:
409 begin
410 ACanvas.Brush.Color := clWhite;
411 ACanvas.Pen.Color := clBlack;
412 //left
413 ACanvas.MoveTo(R.Left, R.Top);
414 ACanvas.LineTo(R.Left, R.Bottom-1);
415 //bottom
416 ACanvas.MoveTo(R.Left, R.Bottom-1);
417 ACanvas.LineTo(R.Right, R.Bottom-1);
418 //top
419 if R.Top = 0 then
420 begin
421 ACanvas.MoveTo(R.Left, R.Top);
422 ACanvas.LineTo(R.Right, R.Top);
423 end;
424 //right
425 if (R.Right = Width) then
426 begin
427 ACanvas.MoveTo(R.Right-1, R.Top);
428 ACanvas.LineTo(R.Right-1, R.Bottom-1);
429 end
430 else
431 if (AIndex = FTotalCells) then
432 begin
433 ACanvas.MoveTo(R.Right, R.Top);
434 ACanvas.LineTo(R.Right, R.Bottom);
435 end;
436 end;
437
438 ccsOver:
439 ThemeServices.DrawElement(ACanvas.Handle, ThemeServices.GetElementDetails(ttbButtonHot), R);
440
441 ccsDown:
442 ThemeServices.DrawElement(ACanvas.Handle, ThemeServices.GetElementDetails(ttbButtonPressed), R);
443
444 ccsChecked:
445 ThemeServices.DrawElement(ACanvas.Handle, ThemeServices.GetElementDetails(ttbButtonChecked), R);
446
447 ccsCheckedHover:
448 ThemeServices.DrawElement(ACanvas.Handle, ThemeServices.GetElementDetails(ttbButtonCheckedHot), R);
449 end // case
450 else // if Themeservices.ThemesEnables...
451 case FState of
452 ccsNone:
453 begin
454 ACanvas.Brush.Color := clWhite;
455 ACanvas.Pen.Color := clBlack;
456 ACanvas.Brush.Color := clWhite;
457 ACanvas.Pen.Color := clBlack;
458 //left
459 ACanvas.MoveTo(R.Left, R.Top);
460 ACanvas.LineTo(R.Left, R.Bottom-1);
461 //bottom
462 ACanvas.MoveTo(R.Left, R.Bottom-1);
463 ACanvas.LineTo(R.Right, R.Bottom-1);
464 //top
465 if R.Top = 0 then
466 begin
467 ACanvas.MoveTo(R.Left, R.Top);
468 ACanvas.LineTo(R.Right, R.Top);
469 end;
470 //right
471 if (R.Right = Width) then
472 begin
473 ACanvas.MoveTo(R.Right-1, R.Top);
474 ACanvas.LineTo(R.Right-1, R.Bottom-1);
475 end
476 else
477 if (AIndex = FTotalCells) then
478 begin
479 ACanvas.MoveTo(R.Right, R.Top);
480 ACanvas.LineTo(R.Right, R.Bottom);
481 end;
482 end;
483
484 ccsOver:
485 begin
486 OffsetRect(R, 1,1);
487 DrawEdge(ACanvas.Handle, R, BDR_RAISED, BF_RECT);
488 end;
489
490 ccsDown, ccsChecked, ccsCheckedHover:
491 DrawEdge(ACanvas.Handle, R, BDR_SUNKENOUTER, BF_RECT);
492 end; // case
493 end // if Enabled ...
494 else
495 if ThemeServices.ThemesEnabled then
496 ThemeServices.DrawElement(ACanvas.Handle, ThemeServices.GetElementDetails(ttbButtonDisabled), R)
497 else
498 begin
499 if Color = clDefault then
500 ACanvas.Brush.Color := GetDefaultColor(dctBrush)
501 else
502 ACanvas.Brush.Color := Color;
503 ACanvas.FillRect(R);
504 end;
505 end; // bsCorel
506
507 end; // case FCellStyle
508 end;
509
510 procedure TmbColorPalette.GenerateGradientPalette(Colors: array of TColor);
511 begin
512 FColors.Text := MakeGradientPalette(Colors);
513 CalcAutoHeight;
514 SortColors;
515 Invalidate;
516 if Assigned(FOnChange) then FOnChange(Self);
517 end;
518
519 procedure TmbColorPalette.GeneratePalette(BaseColor: TColor);
520 begin
521 FColors.Text := MakePalette(BaseColor, FOrder);
522 CalcAutoHeight;
523 SortColors;
524 Invalidate;
525 if Assigned(FOnChange) then FOnChange(Self);
526 end;
527
528 function TmbColorPalette.GetColorUnderCursor: TColor;
529 begin
530 Result := clNone;
531 if FIndex > -1 then
532 if FIndex < FColors.Count then
533 Result := mbStringToColor(FColors.Strings[FIndex]);
534 end;
535
536 function TmbColorPalette.GetHintStr(X, Y: Integer): String;
537 var
538 idx: Integer;
539 begin
540 idx := GetIndexUnderCursor;
541 if FIndex < FNames.Count then
542 Result := FNames.Strings[FIndex]
543 else
544 if SameText(FColors.Strings[idx], 'clCustom') or
545 SameText(FColors.Strings[idx], 'clTransparent')
546 then
547 Result := StringReplace(FColors.Strings[idx], 'cl', '', [rfReplaceAll])
548 else
549 Result := FormatHint(FHintFormat, ColorUnderCursor);
550 end;
551
552 function TmbColorPalette.GetIndexUnderCursor: integer;
553 begin
554 Result := -1;
555 if FIndex > -1 then
556 if FIndex < FColors.Count then
557 Result := FIndex;
558 end;
559
560 function TmbColorPalette.GetMoveCellIndex(move: TMoveDirection): integer;
561 var
562 FBefore: integer;
563 begin
564 Result := -1;
565 case move of
566 mdLeft:
567 if FCheckedIndex -1 < 0 then
568 Result := FTotalCells
569 else
570 Result := FCheckedIndex - 1;
571 mdRight:
572 if FCheckedIndex + 1 > FTotalCells then
573 Result := 0
574 else
575 Result := FCheckedIndex + 1;
576 mdUp:
577 if FCheckedIndex - FColCount < 0 then
578 begin
579 FBefore := (FTotalcells div FColCount) * FColCount;
580 if FBefore + FCheckedIndex - 1 > FTotalCells then Dec(FBefore, FColCount);
581 Result := FBefore + FCheckedIndex - 1;
582 end
583 else
584 Result := FCheckedIndex - FColCount;
585 mdDown:
586 if FCheckedIndex + FColCount > FTotalCells then
587 Result := FCheckedIndex mod FColCount + 1
588 else
589 Result := FCheckedIndex + FColCount;
590 end;
591 if Result > FColors.Count - 1 then
592 Result := 0;
593 end;
594
595 function TmbColorPalette.GetSelColor: TColor;
596 begin
597 if (FCheckedIndex > -1) and (FCheckedIndex <= FTotalCells) then
598 Result := mbStringToColor(FColors.Strings[FCheckedIndex])
599 else
600 Result := FOld;
601 end;
602
603 function TmbColorPalette.GetSelectedCellRect: TRect;
604 var
605 row, lBottom, lLeft: integer;
606 begin
607 if FCheckedIndex > -1 then
608 begin
609 if FCheckedIndex mod FColCount = 0 then
610 begin
611 row := FCheckedIndex div FColCount;
612 lLeft := Width - FCellSize;
613 end
614 else
615 begin
616 row := FCheckedIndex div FColCount + 1;
617 lLeft := (FCheckedIndex mod FColCount - 1) * FCellSize;
618 end;
619 lBottom := row * FCellSize;
620 Result := Rect(lLeft, lBottom - FCellSize, lLeft + FCellSize, lBottom);
621 end
622 else
623 Result := Rect(0, 0, 0, 0);
624 end;
625
626 function TmbColorPalette.GetTotalRowCount: integer;
627 begin
628 if FColCount <> 0 then
629 Result := FTotalCells div FColCount
630 else
631 Result := 0;
632 end;
633
634 procedure TmbColorPalette.KeyDown(var Key: Word; Shift: TShiftState);
635 begin
636 case Key of
637 VK_LEFT:
638 begin
639 FCheckedIndex := GetMoveCellIndex(mdLeft);
640 if Assigned(FOnArrowKey) then FOnArrowKey(Key, Shift);
641 end;
642 VK_RIGHT:
643 begin
644 FCheckedIndex := GetMoveCellIndex(mdRight);
645 if Assigned(FOnArrowKey) then FOnArrowKey(Key, Shift);
646 end;
647 VK_UP:
648 begin
649 FCheckedIndex := GetMoveCellIndex(mdUp);
650 if Assigned(FOnArrowKey) then FOnArrowKey(Key, Shift);
651 end;
652 VK_DOWN:
653 begin
654 FCheckedIndex := GetMoveCellIndex(mdDown);
655 if Assigned(FOnArrowKey) then FOnArrowKey(Key, Shift);
656 end;
657 VK_SPACE, VK_RETURN:
658 ; // fire OnChange event below
659
660 else
661 Key := 0;
662 inherited;
663 exit;
664 end;
665
666 Invalidate;
667 if Assigned(FOnChange) then FOnChange(Self);
668
669 inherited;
670 end;
671
672 procedure TmbColorPalette.LoadPalette(FileName: TFileName);
673 var
674 supported: boolean;
675 a: AcoColors;
676 i: integer;
677 begin
678 supported := false;
679 if SameText(ExtractFileExt(FileName), '.pal') then
680 begin
681 supported := true;
682 FNames.Clear;
683 FColors.Text := ReadJASCPal(FileName);
684 end
685 else if SameText(ExtractFileExt(FileName), '.aco') then
686 begin
687 supported := true;
688 a := ReadPhotoshopAco(FileName);
689 FColors.Clear;
690 for i := 0 to Length(a.Colors) - 1 do
691 FColors.Add(ColorToString(a.Colors[i]));
692 FNames.Clear;
693 if a.HasNames then
694 for i := 0 to Length(a.Names) - 1 do
695 FNames.Add(AnsiString(a.Names[i]));
696 end
697 else if SameText(ExtractFileExt(FileName), '.act') then
698 begin
699 supported := true;
700 FNames.Clear;
701 FColors.Text := ReadPhotoshopAct(FileName);
702 end
703 else
704 raise Exception.Create('The file format you are trying to load is not supported in this version of the palette'#13'Please send a request to MXS along with the files of this format so'#13'loading support for this file can be added too');
705 if supported then
706 begin
707 CalcAutoHeight;
708 SortColors;
709 Invalidate;
710 if Assigned(FOnChange) then FOnChange(Self);
711 end;
712 end;
713
714 procedure TmbColorPalette.MouseEnter;
715 begin
716 FMouseOver := true;
717 FMouseLoc := mlOver;
718 Invalidate;
719 inherited;
720 end;
721
722 procedure TmbColorPalette.MouseLeave;
723 begin
724 FMouseOver := false;
725 FMouseLoc := mlNone;
726 FIndex := -1;
727 Invalidate;
728 inherited;
729 end;
730
731 procedure TmbColorPalette.MouseMove(Shift: TShiftState; X, Y: Integer);
732 var
733 newIndex: Integer;
734 begin
735 newIndex := (y div FCellSize) * FColCount + (x div FCellSize);
736 if FIndex <> newIndex then
737 begin
738 FIndex := newIndex;
739 if FIndex > FTotalCells then FIndex := -1;
740 Invalidate;
741 end;
742 inherited;
743 end;
744
745 procedure TmbColorPalette.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
746 begin
747 if Button = mbLeft then
748 begin
749 SetFocus;
750 FMouseDown := true;
751 FMouseLoc := mlDown;
752 if (y div FCellSize)* FColCount + (x div FCellSize) <= FTotalCells then
753 if FCheckedIndex <> (y div FCellSize)* FColCount + (x div FCellSize) then
754 begin
755 FOldIndex := FCheckedIndex;
756 FCheckedIndex := (y div FCellSize)* FColCount + (x div FCellSize);
757 end;
758 Invalidate;
759 end;
760 inherited;
761 end;
762
763 procedure TmbColorPalette.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
764 var
765 DontCheck: boolean;
766 AColor: TColor;
767 begin
768 FMouseDown := false;
769 if FMouseOver then
770 FMouseLoc := mlOver
771 else
772 FMouseLoc := mlNone;
773 DontCheck := false;
774 if (FCheckedIndex > -1) and (FCheckedIndex < FColors.Count) then
775 AColor := mbStringToColor(FColors.Strings[FCheckedIndex])
776 else
777 AColor := clNone;
778 if (Button = mbLeft) and PtInRect(ClientRect, Point(x, y)) then
779 if Assigned(FOnCellClick) then
780 FOnCellClick(Button, Shift, FCheckedIndex, AColor, DontCheck);
781 if DontCheck then FCheckedIndex := FOldIndex;
782 Invalidate;
783 inherited;
784 if Assigned(FOnChange) then FOnChange(Self);
785 end;
786
787 procedure TmbColorPalette.Paint;
788 var
789 i: integer;
790 bmp: TBitmap;
791 begin
792 //make bmp
793 if FBufferBmp = nil then
794 FBufferBmp := TBitmap.Create;
795 FBufferBmp.Width := Width;
796 FBufferBmp.Height := Height;
797 PaintParentBack(FBufferBmp);
798 FBufferBmp.Transparent := false; // a transparent bitmap does not show the selection ?!
799
800 //reset counters
801 FTotalCells := FColors.Count - 1;
802 FTop := 0;
803 FLeft := 0;
804
805 //draw the cells
806 for i := 0 to FColors.Count - 1 do
807 begin
808 if FColors.Strings[i] <> '' then
809 DrawCell(FBufferBmp.Canvas, FColors.Strings[i]);
810 Inc(FLeft);
811 end;
812
813 //draw the bmp
814 if Color = clDefault then
815 begin
816 // Use temporary bitmap to draw the buffer bitmap transparently
817 bmp := TBitmap.Create;
818 try
819 bmp.SetSize(Width, Height);
820 if Color = clDefault then begin
821 bmp.Transparent := true;
822 bmp.TransparentColor := clForm;
823 end;
824 bmp.Canvas.Draw(0, 0, FBufferBmp);
825 Canvas.Draw(0, 0, bmp);
826 finally
827 bmp.Free;
828 end;
829 end
830 else
831 Canvas.Draw(0, 0, FBufferBmp);
832
833 //csDesiging border
834 if csDesigning in ComponentState then
835 begin
836 Canvas.Brush.Style := bsClear;
837 Canvas.Pen.Style := psDot;
838 Canvas.Pen.Color := clBtnShadow;
839 Canvas.Rectangle(ClientRect);
840 Canvas.Brush.Style := bsSolid;
841 Canvas.Pen.Style := psSolid;
842 end;
843 end;
844
845 procedure TmbColorPalette.PaintTransparentGlyph(ACanvas: TCanvas; R: TRect);
846 begin
847 InflateRect(R, -3, -3);
848 if FCellStyle = csCorel then
849 begin
850 if FState <> ccsNone then
851 InflateRect(R, -2, -2)
852 else if FColCount > 1 then
853 Inc(R.Right);
854 end;
855
856 with ACanvas do
857 case FTStyle of
858 tsPhotoshop:
859 begin
860 if Enabled then
861 Pen.Color := clBtnShadow
862 else
863 Pen.Color := clGray;
864 Brush.Color := clWhite;
865 Rectangle(R);
866 Brush.Color := clSilver;
867 FillRect(Rect(R.Left + (R.Right - R.Left) div 2, R.Top + 1, R.Right - 1, R.Top + (R.Bottom - R.Top) div 2));
868 FillRect(Rect(R.Left + 1, R.Top + (R.Bottom - R.Top) div 2, R.Left + (R.Right - R.Left) div 2, R.Bottom - 1));
869 end;
870 tsPhotoshop2:
871 begin
872 InflateRect(R, -1, -1);
873 Brush.Color := clWhite;
874 Rectangle(R);
875 Pen.Color := clRed;
876 Pen.Width := 2;
877 InflateRect(R, 1, 1);
878 MoveTo(R.Left, R.Top);
879 LineTo(R.Right - 1, R.Bottom - 1);
880 Pen.Width := 1;
881 Pen.Color := clBlack;
882 end;
883 tsCorel:
884 begin
885 if FCellStyle = csCorel then
886 begin
887 Pen.Color := clBlack;
888 InflateRect(R, 3, 3);
889 Brush.Color := clWhite;
890 Rectangle(R);
891 //the \ line
892 MoveTo(R.Left, R.Top);
893 LineTo(R.Right, R.Bottom);
894 //the / line
895 MoveTo(R.Right-1, R.Top);
896 LineTo(R.Left-1, R.Bottom);
897 end
898 else
899 begin
900 if Enabled then
901 Pen.Color := clBtnShadow
902 else
903 Pen.Color := clGray;
904 Brush.Color := clWhite;
905 Rectangle(R);
906 MoveTo(R.Left, R.Top);
907 LineTo(R.Right, R.Bottom);
908 MoveTo(R.Right - 1, R.Top);
909 LineTo(R.Left - 1, R.Bottom);
910 end;
911 end;
912 tsMicroangelo:
913 begin
914 InflateRect(R, -1, -1);
915 Dec(R.Bottom);
916 Pen.Color := clBlack;
917 Brush.Color := clTeal;
918 Rectangle(R);
919 Pixels[R.Left + 2, R.Top + 2] := clWhite;
920 Pixels[R.Left + (R.Right - R.Left) div 2, R.Bottom] := clBlack;
921 MoveTo(R.Left + (R.Right - R.Left) div 2 - 2, R.Bottom + 1);
922 LineTo(R.Left + (R.Right - R.Left) div 2 + 3, R.Bottom + 1);
923 end;
924 end;
925 end;
926
927 procedure TmbColorPalette.Resize;
928 begin
929 inherited;
930 CalcAutoHeight;
931 Invalidate;
932 end;
933
934 procedure TmbColorPalette.SelectCell(i: integer);
935 begin
936 if i < FColors.Count - 1 then
937 FCheckedIndex := i
938 else
939 FCheckedIndex := -1;
940 Invalidate;
941 if Assigned(FOnChange) then FOnChange(Self);
942 end;
943
944 procedure TmbColorPalette.SetTStyle(s: TTransparentStyle);
945 begin
946 if FTStyle <> s then
947 begin
948 FTStyle := s;
949 Invalidate;
950 end;
951 end;
952
953 procedure TmbColorPalette.SetCellStyle(s: TCellStyle);
954 begin
955 if FCellStyle <> s then
956 begin
957 FCellStyle := s;
958 Invalidate;
959 end;
960 end;
961
962 procedure TmbColorPalette.SetSelColor(k: TColor);
963 var
964 s: string;
965 i: integer;
966 begin
967 s := mbColorToString(k);
968 for i:= 0 to FColors.Count - 1 do
969 if SameText(s, FColors.Strings[i]) then
970 begin
971 FCheckedIndex := i;
972 Break;
973 end
974 else
975 FCheckedIndex := -1;
976 Invalidate;
977 FOld := k;
978 if Assigned(FOnChange) then FOnChange(Self);
979 end;
980
981 procedure TmbColorPalette.SetStrings(s: TStrings);
982 var
983 i: integer;
984 begin
985 FColors.Clear;
986 FColors.AddStrings(s);
987 if FColors.Count < FMinColors then
988 for i := 0 to FMinColors - FColors.Count - 1 do
989 FColors.Add('clNone');
990 if (FColors.Count > FMaxColors) and (FMaxColors > 0) then
991 for i := FColors.Count - 1 downto FMaxColors do
992 FColors.Delete(i);
993 CalcAutoHeight;
994 SortColors;
995 Invalidate;
996 end;
997
998 procedure TmbColorPalette.SetNames(n: TStrings);
999 var
1000 i: integer;
1001 begin
1002 FNames.Clear;
1003 FNames.AddStrings(n);
1004 if (FNames.Count > FMaxColors) and (FMaxColors > 0) then
1005 for i := FNames.Count - 1 downto FMaxColors do
1006 FNames.Delete(i);
1007 end;
1008
1009 procedure TmbColorPalette.SaveColorsAsPalette(FileName: TFileName);
1010 begin
1011 if SameText(ExtractFileExt(FileName), '.pal') then
1012 SaveJASCPal(FColors, FileName)
1013 else
1014 raise Exception.Create('The file extension specified does not identify a supported file format!'#13'Supported files formats are: .pal .aco .act');
1015 end;
1016
1017 procedure TmbColorPalette.SetAutoHeight(auto: boolean);
1018 begin
1019 FAutoHeight := auto;
1020 CalcAutoHeight;
1021 Invalidate;
1022 end;
1023
1024 procedure TmbColorPalette.SetCellSize(s: integer);
1025 begin
1026 FCellSize := s;
1027 CalcAutoHeight;
1028 Invalidate;
1029 end;
1030
1031 procedure TmbColorPalette.SetMaxColors(m: integer);
1032 var
1033 i: integer;
1034 begin
1035 if m < 0 then m := 0;
1036 FMaxColors := m;
1037 if (m < FMinColors) and (m > 0) then
1038 SetMinColors(m);
1039 if (FColors.Count > FMaxColors) and (FMaxColors > 0) then
1040 for i := FColors.Count - 1 downto FMaxColors do
1041 FColors.Delete(i);
1042 CalcAutoHeight;
1043 SortColors;
1044 Invalidate;
1045 end;
1046
1047 procedure TmbColorPalette.SetMinColors(m: integer);
1048 var
1049 i: integer;
1050 begin
1051 if (FMaxColors > 0) and (m > FMaxColors) then
1052 m := FMaxColors;
1053 FMinColors := m;
1054 if FColors.Count < m then
1055 for i := 0 to m - FColors.Count - 1 do
1056 FColors.Add('clNone');
1057 CalcAutoHeight;
1058 SortColors;
1059 Invalidate;
1060 end;
1061
1062 procedure TmbColorPalette.SetSortMode(s: TSortMode);
1063 begin
1064 if FSort <> s then
1065 begin
1066 FSort := s;
1067 SortColors;
1068 Invalidate;
1069 end;
1070 end;
1071
1072 procedure TmbColorPalette.SetSortOrder(s: TSortOrder);
1073 begin
1074 if FOrder <> s then
1075 begin
1076 FOrder := s;
1077 SortColors;
1078 Invalidate;
1079 end;
1080 end;
1081
1082 procedure TmbColorPalette.SortColors;
1083 var
1084 old: TColor;
1085 begin
1086 if FSort <> smNone then
1087 begin
1088 if FColors.Count = 0 then Exit;
1089 old := GetSelColor;
1090 SortPalColors(FColors, FSort, FOrder);
1091 SetSelColor(old);
1092 Invalidate;
1093 end;
1094 end;
1095
1096 end.