home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / WIN / Programa / COLCAL.ZIP / COLORC~1.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-08  |  6.1 KB  |  228 lines

  1. unit ColorCalendar;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Grids, Calendar;
  8.  
  9. type
  10.   TDayColorArray = array[1..31] of TColor;
  11.   TDayBrushStyleArray = array[1..31] of TBrushStyle;
  12.   TColorCalendar = class(TCalendar)
  13.   private
  14.     { Private declarations }
  15.  
  16.     FBaseColor: TColor; {color of days not assigned a color,
  17.                         the inherited Color property gives the color of the cells that don't contain a date}
  18.     FBaseBrushStyle: TBrushStyle; {brush style of days not assigned a brush style}
  19.     FDayColor: TDayColorArray; {color of each day}
  20.     FDayBrushStyle: TDayBrushStyleArray; {brush style of each day}
  21.     function GetDayColor(Index: integer): TColor;
  22.     procedure SetDayColor(Index: integer; Value: TColor);
  23.     procedure SetCellColor(ACol, ARow: integer; ARect: TRect);
  24.     function GetDayBrushStyle(Index: integer): TBrushStyle;
  25.     procedure SetDayBrushStyle(Index: integer; Value: TBrushStyle);
  26.     procedure SetCellBrushStyle(ACol, ARow: integer; ARect: TRect);
  27.     procedure SetBaseColor(Value: TColor);
  28.     procedure SetBaseBrushStyle(Value: TBrushStyle);
  29.     function DayNum(ACol, ARow: Longint): integer; {find the integer day of a cell}
  30.   protected
  31.     { Protected declarations }
  32.     procedure DrawCell(ACol, ARow: LongInt; ARect: TRect; AState: TGridDrawState); override;
  33.   public
  34.     { Public declarations }
  35.     constructor Create(AOwner: TComponent); override;
  36.     property DayColor[Index: integer]: TColor read GetDayColor write SetDayColor;
  37.     property DayBrushStyle[Index: integer]: TBrushStyle read GetDayBrushStyle write SetDayBrushStyle;
  38.     procedure NextMonth;
  39.     procedure NextYear;
  40.     procedure PrevMonth;
  41.     procedure PrevYear;
  42.   published
  43.     { Published declarations }
  44.     property BaseColor: TColor read FBaseColor write SetBaseColor default clWhite;
  45.     property BaseBrushStyle: TBrushStyle read FBaseBrushStyle write SetBaseBrushStyle default bsSolid;
  46.     property DefaultDrawing;
  47.   end;
  48.  
  49. procedure Register;
  50.  
  51. implementation
  52.  
  53. constructor TColorCalendar.Create(AOwner: TComponent);
  54. var
  55.   i: integer;
  56. begin
  57.   inherited Create(AOwner);
  58.   FBaseColor := clWhite;
  59.   FBaseBrushStyle := bsSolid;
  60.   for i := 1 to 31 do
  61.   begin
  62.     FDayColor[i] := FBaseColor;
  63.     FDayBrushStyle[i] := FBaseBrushStyle;
  64.   end;
  65. end;
  66.  
  67. function TColorCalendar.GetDayColor(Index: integer): TColor;
  68. begin
  69.   Result := FDayColor[Index];
  70. end;
  71.  
  72. procedure TColorCalendar.SetDayColor(Index: integer; Value: TColor);
  73. begin
  74.   FDayColor[Index] := Value;
  75.   Invalidate;
  76. end;
  77.  
  78. procedure TColorCalendar.DrawCell(ACol, ARow: LongInt; ARect: TRect; AState: TGridDrawState);
  79. var
  80.   TheText: string;
  81.   X, Y: integer;
  82. begin
  83.   inherited DrawCell(ACol, ARow, ARect, AState);
  84.   SetCellColor(ACol, ARow, ARect);
  85.   SetCellBrushStyle(ACol, ARow, ARect);
  86.   TheText := CellText[ACol, ARow];
  87.   Canvas.FillRect(ARect);
  88.   X := ARect.Left + (ARect.Right - ARect.Left - Canvas.TextWidth(TheText)) div 2;
  89.   Y := ARect.Top + (ARect.Bottom - ARect.Top - Canvas.TextHeight(TheText)) div 2;
  90.   Canvas.TextOut(X, Y, TheText);
  91. end;
  92.  
  93. procedure TColorCalendar.SetCellColor(ACol, ARow: integer; ARect: TRect);
  94. begin
  95.   if DayNum(ACol, ARow) > 0 then
  96.   begin
  97.     Canvas.Brush.Color := FDayColor[DayNum(ACol, ARow)];
  98.     {I just picked clMenuText because, an enhancement would be to have differnt fonts for days}
  99.     Canvas.Font.Color := clMenuText;
  100.   end
  101.   else
  102.   begin
  103.     Canvas.Brush.Color := Color;
  104.     Canvas.Font.Color := clMenuText;
  105.   end;
  106. end;
  107.  
  108. function TColorCalendar.DayNum(ACol, ARow: Longint): integer;
  109. {This routine finds the integer value of day cell.
  110. If the cell doesn't contain a day then it returns 0}
  111. var
  112.   FirstDate: TDateTime;
  113.   MonthOffset: integer;
  114. begin
  115.   if (ARow > 0) then
  116.   begin
  117.     FirstDate := EncodeDate(Year, Month, 1);
  118.     MonthOffset := 2 - ((DayOfWeek(FirstDate) - StartOfWeek + 7) mod 7); { day of week for 1st of month }
  119.     if MonthOffset = 2 then
  120.       MonthOffset := -5;
  121.     Result := MonthOffset + ACol + (ARow - 1) * 7;
  122.     if Result > DaysThisMonth then
  123.       Result := 0;
  124.   end
  125.   else
  126.     Result := 0;
  127. end;
  128.  
  129. function TColorCalendar.GetDayBrushStyle(Index: integer): TBrushStyle;
  130. begin
  131.   Result := FDayBrushStyle[Index];
  132.   Invalidate;
  133. end;
  134.  
  135. procedure TColorCalendar.SetDayBrushStyle(Index: integer; Value: TBrushStyle);
  136. begin
  137.   FDayBrushStyle[Index] := Value;
  138. end;
  139.  
  140. procedure TColorCalendar.SetCellBrushStyle(ACol, ARow: integer; ARect: TRect);
  141. begin
  142.   if DayNum(ACol, ARow) > 0 then
  143.   begin
  144.     Canvas.Brush.Style := FDayBrushStyle[DayNum(ACol, ARow)];
  145.   end
  146.   else
  147.   begin
  148.     Canvas.Brush.Style := bsSolid;
  149.   end;
  150. end;
  151.  
  152. procedure TColorCalendar.SetBaseColor(Value: TColor);
  153. var
  154.   i: integer;
  155. begin
  156.   FBaseColor := Value;
  157.   for i := 1 to 31 do
  158.     FDayColor[i] := FBaseColor;
  159.   Invalidate;
  160. end;
  161.  
  162. procedure TColorCalendar.SetBaseBrushStyle(Value: TBrushStyle);
  163. var
  164.   i: integer;
  165. begin
  166.   FBaseBrushStyle := Value;
  167.   for i := 1 to 31 do
  168.     FDayBrushStyle[i] := FBaseBrushStyle;
  169.   Invalidate;
  170. end;
  171.  
  172. procedure TColorCalendar.NextMonth;
  173. {It seemed reasonable to me that when a month changed that the day colors should
  174. revert to the base color}
  175. var
  176.   i: integer;
  177. begin
  178.   inherited NextMonth;
  179.   for i := 1 to 31 do
  180.   begin
  181.     FDayColor[i] := FBaseColor;
  182.     FDayBrushStyle[i] := FBaseBrushStyle;
  183.   end;
  184. end;
  185.  
  186. procedure TColorCalendar.NextYear;
  187. var
  188.   i: integer;
  189. begin
  190.   inherited NextYear;
  191.   for i := 1 to 31 do
  192.   begin
  193.     FDayColor[i] := FBaseColor;
  194.     FDayBrushStyle[i] := FBaseBrushStyle;
  195.   end;
  196. end;
  197.  
  198. procedure TColorCalendar.PrevMonth;
  199. var
  200.   i: integer;
  201. begin
  202.   inherited PrevMonth;
  203.   for i := 1 to 31 do
  204.   begin
  205.     FDayColor[i] := FBaseColor;
  206.     FDayBrushStyle[i] := FBaseBrushStyle;
  207.   end;
  208. end;
  209.  
  210. procedure TColorCalendar.PrevYear;
  211. var
  212.   i: integer;
  213. begin
  214.   inherited NextYear;
  215.   for i := 1 to 31 do
  216.   begin
  217.     FDayColor[i] := FBaseColor;
  218.     FDayBrushStyle[i] := FBaseBrushStyle;
  219.   end;
  220. end;
  221.  
  222. procedure Register;
  223. begin
  224.   RegisterComponents('My Controls', [TColorCalendar]);
  225. end;
  226.  
  227. end.
  228.