2009-10-26 21:03:37 -04:00
|
|
|
{
|
|
|
|
This file is part of the Mufasa Macro Library (MML)
|
|
|
|
Copyright (c) 2009 by Raymond van Venetië and Merlijn Wajer
|
|
|
|
|
|
|
|
MML 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
MML 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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with MML. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
See the file COPYING, included in this distribution,
|
|
|
|
for details about the copyright.
|
|
|
|
|
|
|
|
Colourpicker for the Mufasa Macro Library
|
|
|
|
}
|
|
|
|
|
|
|
|
unit colourpicker;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, LCLIntf,LCLType,InterfaceBase,Forms,Controls,ExtCtrls,
|
|
|
|
Graphics,
|
2010-01-02 22:45:02 -05:00
|
|
|
Window,MufasaTypes, colourhistory,bitmaps,input
|
2009-10-26 21:03:37 -04:00
|
|
|
|
|
|
|
{$IFNDEF PICKER_CLIENT}
|
|
|
|
{$IFDEF LINUX}
|
|
|
|
,x
|
|
|
|
{$ENDIF}
|
|
|
|
{$ENDIF}
|
|
|
|
;
|
|
|
|
|
2010-01-04 13:40:10 -05:00
|
|
|
|
2009-10-26 21:03:37 -04:00
|
|
|
type
|
|
|
|
TPickEvent = procedure (Sender: TObject; Color, X, Y: Integer);
|
|
|
|
|
|
|
|
TMColorPicker = class(TObject)
|
|
|
|
constructor Create(aWindow: TMWindow);
|
|
|
|
destructor Destroy; override;
|
|
|
|
|
|
|
|
procedure Pick(Out C, X, Y: Integer);
|
|
|
|
|
2009-12-21 15:49:50 -05:00
|
|
|
procedure ImageMainMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
|
|
|
|
procedure ImageInfoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
|
2009-12-21 19:00:48 -05:00
|
|
|
Procedure ColorPickUp(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
|
2009-10-26 21:03:37 -04:00
|
|
|
public
|
|
|
|
// Will give us CopyClientToBitmap
|
|
|
|
Window: TMWindow;
|
|
|
|
|
2010-01-02 22:45:02 -05:00
|
|
|
// Created and freed in Pick.
|
|
|
|
Input: TMInput;
|
|
|
|
|
2009-11-13 20:22:06 -05:00
|
|
|
{ Form components }
|
2009-12-21 15:49:50 -05:00
|
|
|
ScreenForm, InfoForm : TForm;
|
|
|
|
ImageMain, ImageInfo: TImage;
|
2009-10-26 21:03:37 -04:00
|
|
|
Text : string;
|
|
|
|
FPickEvent : TPickEvent;
|
|
|
|
|
2009-11-13 20:22:06 -05:00
|
|
|
{ Some temp vars }
|
2009-10-26 21:03:37 -04:00
|
|
|
oldx, oldy, Color, colorx, colory: Integer;
|
2009-12-21 15:49:50 -05:00
|
|
|
// targetleft,targettop : integer;
|
2009-10-26 21:03:37 -04:00
|
|
|
|
|
|
|
TheChangedEvent,TheChangingEvent : TNotifyEvent;
|
2009-11-13 20:22:06 -05:00
|
|
|
|
|
|
|
{ Handles }
|
2009-12-21 15:49:50 -05:00
|
|
|
InfoHandle, ImageHandle : HDC;
|
2009-10-26 21:03:37 -04:00
|
|
|
public
|
|
|
|
property OnPick: TPickEvent read FPickEvent write FPickEvent;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
constructor TMColorPicker.Create(aWindow: TMWindow);
|
2010-01-02 22:45:02 -05:00
|
|
|
|
2009-10-26 21:03:37 -04:00
|
|
|
begin
|
2009-11-07 10:40:45 -05:00
|
|
|
inherited Create;
|
2010-01-02 22:45:02 -05:00
|
|
|
|
2009-10-26 21:03:37 -04:00
|
|
|
Self.Window := aWindow;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TMColorPicker.Destroy;
|
|
|
|
begin
|
|
|
|
|
2009-11-07 10:40:45 -05:00
|
|
|
inherited Destroy;
|
2009-10-26 21:03:37 -04:00
|
|
|
end;
|
|
|
|
|
2009-12-21 15:49:50 -05:00
|
|
|
var
|
|
|
|
closed: Boolean;
|
|
|
|
|
2009-10-26 21:03:37 -04:00
|
|
|
procedure TMColorPicker.Pick(Out C, X, Y: Integer);
|
|
|
|
var
|
|
|
|
w, h: integer;
|
2009-12-21 19:00:48 -05:00
|
|
|
SS : TShiftState;
|
|
|
|
p : TPoint;
|
2009-10-26 21:03:37 -04:00
|
|
|
|
2009-12-23 13:45:03 -05:00
|
|
|
bmp: TMufasaBitmap;
|
2010-01-04 13:40:10 -05:00
|
|
|
Desktop : TMWindow;
|
2009-10-26 21:03:37 -04:00
|
|
|
|
2010-01-02 22:45:02 -05:00
|
|
|
|
2009-10-26 21:03:37 -04:00
|
|
|
begin
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Disable both of the color pick buttons }
|
2010-01-04 13:40:10 -05:00
|
|
|
w := 0;
|
|
|
|
h := 0;
|
|
|
|
if not Self.Window.TargetValid then
|
|
|
|
self.Window.SetDesktop;
|
2010-01-02 22:45:02 -05:00
|
|
|
|
2010-01-04 13:40:10 -05:00
|
|
|
Desktop := TMWindow.Create;
|
|
|
|
Desktop.SetDesktop;
|
|
|
|
Input := TMInput.Create(Self.Window);
|
|
|
|
Desktop.GetDimensions(w, h);
|
2010-01-02 22:45:02 -05:00
|
|
|
|
2009-12-21 16:52:41 -05:00
|
|
|
Application.MainForm.Enabled := False;
|
2009-12-21 19:00:48 -05:00
|
|
|
ColourHistoryForm.Enabled := False;
|
2009-12-21 16:52:41 -05:00
|
|
|
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Create a form that will hold the client image and a form that will show cursor and color data }
|
2009-12-21 15:49:50 -05:00
|
|
|
ScreenForm := TForm.Create(Application.MainForm);
|
|
|
|
InfoForm := TForm.Create(ScreenForm);
|
|
|
|
|
2010-01-04 13:40:10 -05:00
|
|
|
|
|
|
|
|
2009-10-26 21:03:37 -04:00
|
|
|
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Initialize the form that will hold the client image }
|
2009-12-21 15:49:50 -05:00
|
|
|
ScreenForm.Caption := 'SimbaColourPicker';
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Set the form's dimensions to match that of the screen }
|
2009-12-21 15:49:50 -05:00
|
|
|
ScreenForm.Width := w;
|
|
|
|
ScreenForm.Height := h;
|
|
|
|
ScreenForm.Top := 0;
|
|
|
|
ScreenForm.left := 0;
|
|
|
|
ScreenForm.WindowState := wsmaximized;
|
|
|
|
ScreenForm.BorderStyle:= bsNone;
|
|
|
|
ScreenForm.FormStyle := fsStayOnTop;
|
|
|
|
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Initialize the form that will hold the cursor and color info }
|
2009-12-21 15:49:50 -05:00
|
|
|
InfoForm.Width := 173;
|
|
|
|
InfoForm.Height := 33;
|
|
|
|
InfoForm.BorderStyle := bsNone;
|
|
|
|
InfoForm.FormStyle := fsStayOnTop;
|
|
|
|
InfoForm.Left := Mouse.CursorPos.X + 5;
|
2009-12-21 19:00:48 -05:00
|
|
|
InfoForm.Top := Mouse.CursorPos.Y - 15;
|
2009-12-21 15:49:50 -05:00
|
|
|
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Initialize the image that will hold the cursor and color info }
|
2009-12-21 15:49:50 -05:00
|
|
|
ImageInfo := TImage.Create(InfoForm);
|
|
|
|
ImageInfo.Parent := InfoForm;
|
|
|
|
ImageInfo.Left := 0;
|
|
|
|
ImageInfo.Top := 0;
|
|
|
|
ImageInfo.Width := 173;
|
|
|
|
ImageInfo.Height := 33;
|
|
|
|
ImageInfo.Cursor := crCross;
|
|
|
|
ImageInfo.OnMouseMove := @ImageInfoMouseMove;
|
|
|
|
ImageInfo.Canvas.Brush.Color := 14811135;
|
|
|
|
ImageInfo.Canvas.Rectangle(0, 0, 173, 33);
|
|
|
|
ImageInfo.Canvas.Rectangle(114, 3, 140, 29);
|
|
|
|
ImageInfo.Canvas.Rectangle(142, 3, 168, 29);
|
|
|
|
ImageInfo.Canvas.Pen.Style := psClear;
|
|
|
|
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Initialize the image that will hold the client image }
|
2009-12-21 15:49:50 -05:00
|
|
|
ImageMain := TImage.Create(ScreenForm);
|
|
|
|
ImageMain.Parent := ScreenForm;
|
|
|
|
ImageMain.left := 0;
|
|
|
|
ImageMain.top := 0;
|
|
|
|
ImageMain.width := ScreenForm.Width;
|
|
|
|
ImageMain.Height := ScreenForm.Height;
|
|
|
|
ImageMain.Cursor:= crCross;
|
2009-12-21 19:00:48 -05:00
|
|
|
ImageMain.OnMouseUp:= @ColorPickUp;
|
2009-12-21 15:49:50 -05:00
|
|
|
ImageMain.OnMouseMove:=@ImageMainMouseMove;
|
|
|
|
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Copy the client to ImageMain }
|
2009-12-23 13:45:03 -05:00
|
|
|
bmp:=TMufasaBitmap.Create;
|
2010-01-04 13:40:10 -05:00
|
|
|
bmp.CopyClientToBitmap(Desktop, true, 0, 0, w-1, h-1);
|
2010-01-02 08:02:57 -05:00
|
|
|
ImageMain.Picture.Bitmap.Free;
|
2009-12-23 13:45:03 -05:00
|
|
|
ImageMain.Picture.Bitmap := bmp.ToTBitmap;
|
|
|
|
bmp.Free;
|
2009-12-21 15:49:50 -05:00
|
|
|
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Set up handles and events }
|
2009-12-21 15:49:50 -05:00
|
|
|
ImageHandle:= ImageMain.Canvas.Handle;
|
|
|
|
InfoHandle:= ImageInfo.Canvas.Handle;
|
|
|
|
TheChangedEvent := ImageMain.Canvas.OnChange;
|
|
|
|
TheChangingEvent := ImageMain.Canvas.OnChanging;
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Show the forms }
|
2009-12-21 15:49:50 -05:00
|
|
|
ScreenForm.Show;
|
|
|
|
InfoForm.Show;
|
|
|
|
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Display the data on the info form }
|
|
|
|
p := ImageMain.ScreenToClient(Mouse.CursorPos);
|
|
|
|
ImageMainMouseMove(nil, SS, p.x, p.y);
|
2009-12-21 16:52:41 -05:00
|
|
|
|
2009-12-21 15:49:50 -05:00
|
|
|
closed := False;
|
|
|
|
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Wait while the forms are still open }
|
|
|
|
while not Closed do
|
2009-12-21 15:49:50 -05:00
|
|
|
begin
|
|
|
|
sleep(1);
|
|
|
|
Application.ProcessMessages;
|
|
|
|
end;
|
2009-10-26 21:03:37 -04:00
|
|
|
|
|
|
|
// add x to history here.
|
|
|
|
c := Color;
|
|
|
|
x := Colorx;
|
|
|
|
y := Colory;
|
|
|
|
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Free forms and images }
|
2009-12-21 15:49:50 -05:00
|
|
|
ImageMain.Free;
|
|
|
|
ImageInfo.Free;
|
|
|
|
InfoForm.Free;
|
|
|
|
ScreenForm.Free;
|
2009-12-21 16:52:41 -05:00
|
|
|
|
2010-01-02 22:45:02 -05:00
|
|
|
Input.Free;
|
2010-01-04 13:40:10 -05:00
|
|
|
Desktop.free;
|
2010-01-02 22:45:02 -05:00
|
|
|
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Re-enable the color pick buttons }
|
2009-12-21 16:52:41 -05:00
|
|
|
Application.MainForm.Enabled := True;
|
2009-12-21 19:00:48 -05:00
|
|
|
ColourHistoryForm.Enabled := True;
|
2009-10-26 21:03:37 -04:00
|
|
|
end;
|
|
|
|
|
2009-12-21 15:49:50 -05:00
|
|
|
procedure TMColorPicker.ImageMainMouseMove(Sender: TObject; Shift: TShiftState; X,
|
2009-10-26 21:03:37 -04:00
|
|
|
Y: Integer);
|
2009-11-01 12:58:57 -05:00
|
|
|
var
|
|
|
|
TempPoint : TPoint;
|
2009-12-21 15:49:50 -05:00
|
|
|
R : TRect;
|
|
|
|
px, py : Integer;
|
2010-01-02 22:45:02 -05:00
|
|
|
MouseX, MouseY: Integer;
|
2009-11-01 12:58:57 -05:00
|
|
|
begin
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Move the info form }
|
2010-01-02 22:45:02 -05:00
|
|
|
Input.GetMousePos(MouseX, MouseY);
|
2009-12-21 15:49:50 -05:00
|
|
|
InfoForm.Left := Mouse.CursorPos.X + 5;
|
2009-12-21 19:00:48 -05:00
|
|
|
InfoForm.Top := Mouse.CursorPos.Y - 15;
|
2009-11-01 12:58:57 -05:00
|
|
|
|
2009-12-21 15:49:50 -05:00
|
|
|
TempPoint := Point(x, y);
|
2009-11-01 12:58:57 -05:00
|
|
|
|
|
|
|
{ If a form cannot be fully set to 0,0 }
|
2009-12-21 15:49:50 -05:00
|
|
|
TempPoint.X := TempPoint.X - ScreenForm.Left;
|
|
|
|
TempPoint.Y := TempPoint.Y - ScreenForm.Top;
|
2009-12-21 19:00:48 -05:00
|
|
|
|
|
|
|
{ Get the pixel that the cursor is currently on }
|
2009-12-21 15:49:50 -05:00
|
|
|
Color := WidgetSet.DCGetPixel(ImageHandle, X, Y);
|
2009-12-21 19:00:48 -05:00
|
|
|
|
|
|
|
{ Draw the current pixel to the right color box }
|
2009-12-21 15:49:50 -05:00
|
|
|
ImageInfo.Canvas.Brush.Color := Color;
|
|
|
|
ImageInfo.Canvas.Rectangle(143, 4, 168, 29);
|
2009-12-21 19:00:48 -05:00
|
|
|
|
|
|
|
{ Draw the cursor and color info }
|
2009-12-21 15:49:50 -05:00
|
|
|
SetBkColor(InfoHandle, 14811135);
|
2010-01-02 22:45:02 -05:00
|
|
|
Text := Format('Pos: %d, %d', [MouseX, MouseY]);
|
2009-12-21 15:49:50 -05:00
|
|
|
R := Rect(5, 6, 114, 18);
|
|
|
|
ExtTextOut(InfoHandle, 5, 3, ETO_OPAQUE, @R, pchar(text), length(text), nil);
|
2009-12-21 19:00:48 -05:00
|
|
|
Text := Format('Color: %d', [Color]);
|
2009-12-21 15:49:50 -05:00
|
|
|
R := Rect(5, 18, 114, 28);
|
|
|
|
ExtTextOut(InfoHandle, 5, 15, ETO_OPAQUE, @R, pchar(text), length(text), nil);
|
2009-12-21 19:00:48 -05:00
|
|
|
|
|
|
|
{ Draw the left, slightly zoomed out, color box }
|
2009-12-21 15:49:50 -05:00
|
|
|
for px := -1 to 1 do
|
|
|
|
for py := -1 to 1 do
|
|
|
|
begin
|
|
|
|
ImageInfo.Canvas.Brush.Color := WidgetSet.DCGetPixel(ImageHandle, x + px, y + py);
|
|
|
|
ImageInfo.Canvas.Rectangle((px + 1) * 8 + 115, (py + 1) * 8 + 4, (px + 1) * 8 + 124, (py + 1) * 8 + 13);
|
|
|
|
end;
|
2009-12-21 19:00:48 -05:00
|
|
|
|
2009-11-01 12:58:57 -05:00
|
|
|
Oldx := TempPoint.x;
|
|
|
|
Oldy := TempPoint.y;
|
|
|
|
end;
|
2009-10-26 21:03:37 -04:00
|
|
|
|
2009-12-21 15:49:50 -05:00
|
|
|
procedure TMColorPicker.ImageInfoMouseMove(Sender: TObject; Shift: TShiftState; X,
|
|
|
|
Y: Integer);
|
2009-10-26 21:03:37 -04:00
|
|
|
begin
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Move the info form }
|
2010-01-04 14:58:12 -05:00
|
|
|
InfoForm.Top := Mouse.CursorPos.Y - 15;
|
|
|
|
InfoForm.Left := Mouse.CursorPos.X + 5;
|
2009-10-26 21:03:37 -04:00
|
|
|
end;
|
|
|
|
|
2009-12-21 19:00:48 -05:00
|
|
|
procedure TMColorPicker.ColorPickUp(Sender: TObject; Button: TMouseButton;
|
2009-10-26 21:03:37 -04:00
|
|
|
Shift: TShiftState; X, Y: Integer);
|
|
|
|
begin;
|
2009-12-21 19:00:48 -05:00
|
|
|
{ Set the coordinates and color that the user cliked on }
|
2009-12-21 15:49:50 -05:00
|
|
|
Color:= WidgetSet.DCGetPixel(ImageMain.Canvas.Handle,x,y);
|
|
|
|
Self.Colorx := x;
|
|
|
|
Self.Colory := y;
|
2009-12-21 19:00:48 -05:00
|
|
|
|
2009-10-26 21:03:37 -04:00
|
|
|
if OnPick <> nil then
|
2009-11-01 12:58:57 -05:00
|
|
|
Onpick(Sender,Color,Colorx,Colory);
|
2009-12-21 19:00:48 -05:00
|
|
|
|
|
|
|
{ Close the forms }
|
2009-12-21 15:49:50 -05:00
|
|
|
InfoForm.Close;
|
|
|
|
ScreenForm.Close;
|
2009-12-21 19:00:48 -05:00
|
|
|
|
|
|
|
{ Tell Pick() that we are done }
|
2009-12-21 15:49:50 -05:00
|
|
|
closed := True;
|
2009-10-26 21:03:37 -04:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|