home *** CD-ROM | disk | FTP | other *** search
/ PC Format Collection 48 / SENT14D.ISO / tech / delphi / disk15 / ushlpdmo.pak / STEREO.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-08-24  |  9.1 KB  |  364 lines

  1. unit Stereo;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Buttons;
  8.  
  9. type
  10.   TStereoButton = class(TGraphicControl)
  11.   private
  12.     { Private declarations }
  13.     FDown: Boolean;
  14.     FDragging: Boolean;
  15.     FLightColor: array[Boolean] of TColor;
  16.     FState: TButtonState;
  17.     FOn: Boolean;
  18.     function GetStateColor(Index: Integer): TColor;
  19.     procedure SetStateColor(Index: Integer; Value: TColor);
  20.     procedure SetOn(Value: Boolean);
  21.     procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
  22.     procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
  23.     procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
  24.     procedure CMSysColorChange(var Message: TMessage); message CM_SYSCOLORCHANGE;
  25.     procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
  26.   protected
  27.     procedure Paint; override;
  28.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  29.       X, Y: Integer); override;
  30.     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  31.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  32.       X, Y: Integer); override;
  33.     procedure Click; override;
  34.   public
  35.     { Public declarations }
  36.     constructor Create(AOwner: TComponent); override;
  37.   published
  38.     { Published declarations }
  39.     property OnColor: TColor index Ord(True) read GetStateColor write SetStateColor;
  40.     property OffColor: TColor index Ord(False) read GetStateColor write SetStateColor;
  41.     property IsOn: Boolean read FOn write SetOn;
  42.     property Caption;
  43.     property Font;
  44.     property ParentFont;
  45.     property ShowHint;
  46.     property ParentShowHint;
  47.     property DragMode;
  48.     property Visible;
  49.     property OnClick;
  50.     property OnMouseDown;
  51.     property OnMouseMove;
  52.     property OnMouseUp;
  53.   end;
  54.  
  55.   TStereoSpeaker = class(TGraphicControl)
  56.   private
  57.     FWaveFile: PString;
  58.     Bitmap: TBitmap;
  59.     function GetWaveFile: string;
  60.     procedure SetWaveFile(const Value: string);
  61.   protected
  62.     procedure Click; override;
  63.     procedure Paint; override;
  64.   public
  65.     constructor Create(AOwner: TComponent); override;
  66.     destructor Destroy; override;
  67.   published
  68.     property WaveFile: string read GetWaveFile write SetWaveFile;
  69.     property Visible;
  70.     property OnClick;
  71.     property OnMouseDown;
  72.     property OnMouseMove;
  73.     property OnMouseUp;
  74.     property ShowHint;
  75.     property ParentShowHint;
  76.     property DragMode;
  77.   end;
  78.  
  79. procedure Register;
  80.  
  81. implementation
  82.  
  83. uses MMSystem, DsgnIntf;
  84.  
  85. {$R STEREO.RES}
  86.  
  87. type
  88.   TWaveFileProperty = class(TStringProperty)
  89.   public
  90.     procedure Edit; override;
  91.     function GetAttributes: TPropertyAttributes; override;
  92.   end;
  93.  
  94. { TStereoButton }
  95.  
  96. constructor TStereoButton.Create(AOwner: TComponent);
  97. begin
  98.   inherited Create(AOwner);
  99.   Width := 65;
  100.   Height := 35;
  101.   FLightColor[True] := clLime;
  102.   FLightColor[False] := clMaroon;
  103.   ParentFont := True;
  104. end;
  105.  
  106. function TStereoButton.GetStateColor(Index: Integer): TColor;
  107. begin
  108.   Result := FLightColor[Boolean(Index)];
  109. end;
  110.  
  111. procedure TStereoButton.SetStateColor(Index: Integer; Value: TColor);
  112. begin
  113.   FLightColor[Boolean(Index)] := Value;
  114.   Repaint;
  115. end;
  116.  
  117. procedure TStereoButton.SetOn(Value: Boolean);
  118. begin
  119.   if Value <> FOn then
  120.   begin
  121.     FOn := Value;
  122.     Repaint;
  123.   end;
  124. end;
  125.  
  126. procedure TStereoButton.Paint;
  127. const
  128.   LightWidth = 15;
  129.   LightHeight = 8;
  130.   LightTop = 6;
  131. var
  132.   TextLeft: Integer;
  133.   R: TRect;
  134.   CStr: array[0..255] of Char;
  135. begin
  136.   with Canvas do
  137.   begin
  138.     Pen.Color := clWindowFrame;
  139.     Brush.Color := clBtnFace;
  140.     Brush.Style := bsSolid;
  141.     Rectangle(0, 0, Width, Height);
  142.  
  143.     if FState = bsDown then Pen.Color := clBtnShadow
  144.     else Pen.Color := clBtnHighlight;
  145.     MoveTo(1, 1); LineTo(Width - 1, 1);
  146.     MoveTo(2, 2); LineTo(Width - 2, 2);
  147.  
  148.     if FState = bsDown then Pen.Color := clBtnHighlight
  149.     else Pen.Color := clBtnShadow;
  150.     MoveTo(1, Height - 2); LineTo(Width - 1, Height - 2);
  151.     if FState <> bsDown then
  152.     begin
  153.       MoveTo(2, Height - 3);
  154.       LineTo(Width - 2, Height - 3);
  155.     end;
  156.  
  157.     R := Bounds((Width div 2) - (LightWidth div 2), LightTop, LightWidth,
  158.       LightHeight);
  159.     if FState = bsDown then OffsetRect(R, 0, 1);
  160.  
  161.     { draw light's bounding rectangle }
  162.     Pen.Color := clBtnShadow;
  163.     PolyLine([Point(R.Left, R.Bottom), R.TopLeft, Point(R.Right + 1, R.Top)]);
  164.     Pen.Color := clBtnHighlight;
  165.     PolyLine([Point(R.Left, R.Bottom), R.BottomRight, Point(R.Right, R.Top)]);
  166.  
  167.     { draw the light }
  168.     OffsetRect(R, 1, 1);
  169.     Dec(R.Right);
  170.     Dec(R.Bottom);
  171.     if Enabled then Brush.Color := FLightColor[FOn]
  172.     else Brush.Color := clBtnFace;
  173.     FillRect(R);
  174.  
  175.     { draw the button's caption }
  176.     Canvas.Font := Self.Font;
  177.     Brush.Style := bsClear;
  178.     R := Bounds(0, R.Bottom, Self.Width, 0);
  179.     R.Bottom := Self.Height;
  180.     if FState = bsDown then OffsetRect(R, 0, 1);
  181.  
  182.     StrPCopy(CStr, Caption);
  183.     if not Enabled then
  184.     begin
  185.       OffsetRect(R, 1, 1);
  186.       Font.Color := clBtnHighlight;
  187.       DrawText(Handle, CStr, -1, R, DT_VCENTER or DT_CENTER or DT_SINGLELINE);
  188.       OffsetRect(R, -1, -1);
  189.       Font.Color := clBtnShadow;
  190.       DrawText(Handle, CStr, -1, R, DT_VCENTER or DT_CENTER or DT_SINGLELINE);
  191.     end
  192.     else
  193.       DrawText(Handle, CStr, -1, R, DT_VCENTER or DT_CENTER or DT_SINGLELINE);
  194.   end;
  195. end;
  196.  
  197. procedure TStereoButton.MouseDown(Button: TMouseButton; Shift: TShiftState;
  198.   X, Y: Integer);
  199. begin
  200.   inherited MouseDown(Button, Shift, X, Y);
  201.   if (Button = mbLeft) and Enabled then
  202.   begin
  203.     if not FDown then
  204.     begin
  205.       FState := bsDown;
  206.       Repaint;
  207.     end;
  208.     FDragging := True;
  209.   end;
  210. end;
  211.  
  212. procedure TStereoButton.MouseMove(Shift: TShiftState; X, Y: Integer);
  213. var
  214.   NewState: TButtonState;
  215. begin
  216.   inherited MouseMove(Shift, X, Y);
  217.   if FDragging then
  218.   begin
  219.     if not FDown then NewState := bsUp;
  220.  
  221.     if (X >= 0) and (X < ClientWidth) and (Y >= 0) and (Y <= ClientHeight) then
  222.       if FDown then NewState := bsUp else NewState := bsDown;
  223.     if NewState <> FState then
  224.     begin
  225.       FState := NewState;
  226.       Repaint;
  227.     end;
  228.   end;
  229. end;
  230.  
  231. procedure TStereoButton.MouseUp(Button: TMouseButton; Shift: TShiftState;
  232.   X, Y: Integer);
  233. var
  234.   DoClick: Boolean;
  235. begin
  236.   inherited MouseUp(Button, Shift, X, Y);
  237.   if FDragging then
  238.   begin
  239.     FDragging := False;
  240.     DoClick := FDown and (X >= 0) and (X < ClientWidth) and (Y >= 0) and
  241.       (Y <= ClientHeight);
  242.     FState := bsUp;
  243.     Repaint;
  244.     if DoClick then Click;
  245.   end;
  246. end;
  247.  
  248. procedure TStereoButton.Click;
  249. begin
  250.   FOn := not FOn;
  251.   inherited Click;
  252. end;
  253.  
  254. procedure TStereoButton.CMEnabledChanged(var Message: TMessage);
  255. begin
  256.   inherited;
  257.   Invalidate;
  258. end;
  259.  
  260. procedure TStereoButton.CMFontChanged(var Message: TMessage);
  261. begin
  262.   Invalidate;
  263. end;
  264.  
  265. procedure TStereoButton.CMTextChanged(var Message: TMessage);
  266. begin
  267.   Invalidate;
  268. end;
  269.  
  270. procedure TStereoButton.CMSysColorChange(var Message: TMessage);
  271. begin
  272.   Invalidate;
  273. end;
  274.  
  275. procedure TStereoButton.CMDialogChar(var Message: TCMDialogChar);
  276. begin
  277.   with Message do
  278.     if IsAccel(CharCode, Caption) and Enabled then
  279.     begin
  280.       Click;
  281.       Result := 1;
  282.     end else
  283.       inherited;
  284. end;
  285.  
  286. { TStereoSpeaker }
  287. constructor TStereoSpeaker.Create(AOwner: TComponent);
  288. begin
  289.   inherited Create(AOwner);
  290.   Bitmap := TBitmap.Create;
  291.   Bitmap.Handle := LoadBitmap(HInstance, 'SPEAKER');
  292.   FWaveFile := NullStr;
  293.   Width := Bitmap.Width;
  294.   Height := Bitmap.Height;
  295. end;
  296.  
  297. destructor TStereoSpeaker.Destroy;
  298. begin
  299.   Bitmap.Free;
  300.   DisposeStr(FWaveFile);
  301.   inherited Destroy;
  302. end;
  303.  
  304. function TStereoSpeaker.GetWaveFile: string;
  305. begin
  306.   Result := FWaveFile^;
  307. end;
  308.  
  309. procedure TStereoSpeaker.SetWaveFile(const Value: string);
  310. begin
  311.   AssignStr(FWaveFile, Value);
  312. end;
  313.  
  314. procedure TStereoSpeaker.Paint;
  315. var
  316.   F: TForm;
  317. begin
  318.   F := GetParentForm(Self);
  319.   if F <> nil then Canvas.Brush.Color := F.Color;
  320.   Canvas.BrushCopy(Bounds(0, 0, Bitmap.Width, Bitmap.Height), Bitmap,
  321.     Bounds(0, 0, Bitmap.Width, Bitmap.Height),
  322.     Bitmap.Canvas.Pixels[0, Bitmap.Height - 1]);
  323. end;
  324.  
  325. procedure TStereoSpeaker.Click;
  326. var
  327.   CWaveFile: array[0..255] of Char;
  328. begin
  329.   if WaveFile > '' then
  330.     sndPlaySound(StrPCopy(CWaveFile, WaveFile), snd_Sync);
  331. end;
  332.  
  333. { TWaveFileProperty }
  334. procedure TWaveFileProperty.Edit;
  335. var
  336.   FileOpen: TOpenDialog;
  337. begin
  338.   FileOpen := TOpenDialog.Create(Application);
  339.   FileOpen.Filename := GetValue;
  340.   FileOpen.Filter := 'Wave files (*.wav)|*.WAV|All files (*.*)|*.*';
  341.   FileOpen.Options := FileOpen.Options + [ofPathMustExist, ofFileMustExist];
  342.   try
  343.     if FileOpen.Execute then SetValue(FileOpen.Filename);
  344.   finally
  345.     FileOpen.Free;
  346.   end;
  347. end;
  348.  
  349. function TWaveFileProperty.GetAttributes: TPropertyAttributes;
  350. begin
  351.   Result := [paDialog];
  352. end;
  353.  
  354. procedure Register;
  355. begin
  356.   RegisterComponents('Samples', [TStereoButton, TStereoSpeaker]);
  357.   RegisterPropertyEditor(TypeInfo(string), TStereoSpeaker, 'WaveFile',
  358.     TWaveFileProperty);
  359. end;
  360.  
  361. end.
  362.  
  363.  
  364.