home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / kolekce / d456 / VOLGAPAK.ZIP / Source / VolPeriod.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2001-09-20  |  11.4 KB  |  412 lines

  1. //---------------------------------------------------------------------------
  2. //  TVolgaPeriod - inherited from TWinControl
  3. //  Component for changing periods(ranges) od date
  4. //  Turn over months, quarters, halfyears, years
  5. //  properties StartDate, EndDate, Text, KindRange, Year, Month
  6. //  Autopopup menu for change Kind of range
  7. //---------------------------------------------------------------------------
  8. //  Copyright ⌐ 2000, Olga Vlasova, Russia
  9. //  http://volgatable.chat.ru
  10. //  E-mail: volgatable@chat.ru
  11. //---------------------------------------------------------------------------
  12. unit VolPeriod;
  13.  
  14. interface
  15.  
  16. uses
  17.   SysUtils, Windows, Messages, Classes, Graphics, Controls,
  18.   Forms, Buttons, ExtCtrls, StdCtrls, ComCtrls, menus, VolDBConst;
  19.  
  20. type
  21.   TKindRange = (ksMonth, ksQuarter, ksHalfYear, ksYear);
  22.   TVolgaPeriod = class(TWinControl)
  23.   private
  24.     { Private declarations }
  25.     FUpDownButton: TUpDown;
  26.     FButtonWidth: byte;
  27.     FColor: TColor;
  28.     FPanel: TPanel;
  29.     FStartDate: TDateTime;
  30.     FEndDate: TDateTime;
  31.     FEnablePopup: Boolean;
  32.     FKindRange: TKindRange;
  33.     FPopup: TPopupMenu;
  34.     FmnuItems: array[0..3] of TMenuItem;
  35.     FDay, FMonth, FYear: word;
  36.     FOnChange: TNotifyEvent;
  37.     FBeforeChange: TNotifyEvent;
  38.     function GetText: string;
  39.     procedure SetButtonWidth(Value: byte);
  40.     function CreateButton: TUpDown;
  41.     procedure SetColor(Value: TColor);
  42.     procedure SetYear(Value: word);
  43.     procedure SetMonth(Value: word);
  44.     procedure SetKindRange(Value: TKindRange);
  45.     procedure SetEnablePopup(Value: Boolean);
  46.     procedure UpDownButtonClick(Sender: TObject; Button: TUDBtnType);
  47.     procedure MonthPopup(Sender: TObject);
  48.     procedure SetStartDate(const Value: TDateTime);
  49.   protected
  50.     { Protected declarations }
  51.     procedure Change; dynamic;
  52.     procedure BeforeChanged; dynamic;
  53.   public
  54.     { Public declarations }
  55.     constructor Create(AOwner: TComponent); override;
  56.     destructor Destroy; override;
  57.     procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
  58.     property StartDate: TDateTime read FStartDate write SetStartDate;
  59.     property EndDate: TDateTime read FEndDate;
  60.     property Text: string read GetText;
  61.   published
  62.     { Published declarations }
  63.     property KindRange: TKindRange read FKindRange write SetKindRange;
  64.     property Year: word read FYear write SetYear;
  65.     property Month: word read FMonth write SetMonth;
  66.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  67.     property Align;
  68.     property Anchors;
  69.     property ButtonWidth: byte read FButtonWidth write SetButtonWidth;
  70.     property Color: TColor read FColor write SetColor default clWindow;
  71.     property Enabled;
  72.     property EnablePopup: Boolean read FEnablePopup write SetEnablePopup;
  73.     property Font;
  74.     property ParentFont;
  75.     property PopupMenu;
  76.     property ShowHint;
  77.     property Visible;
  78.     property BeforeChange: TNotifyEvent read FBeforeChange write FBeforeChange;
  79.   end;
  80.  
  81. implementation
  82.  
  83. constructor TVolgaPeriod.Create(AOwner: TComponent);
  84. begin
  85.   inherited Create(AOwner);
  86.   ControlStyle := ControlStyle - [csAcceptsControls, csSetCaption] +
  87.     [csFramed, csOpaque];
  88.   if FPanel = nil then
  89.   begin
  90.     FPanel := TPanel.Create(Self);        {owner -> TVolgaPeriod}
  91.     FPanel.Parent := Self;                {parent -> ≥ε≥ µσ,ΩαΩεΘ ≤ TVolgaPeriod}
  92.     FPanel.BorderStyle := bsNone;
  93.     FPanel.BevelInner := bvLowered;
  94. {  FPanel.BevelWidth:=2;}
  95.     FPanel.BevelOuter := bvNone;
  96.     FPanel.Color := clWindow;
  97.     DecodeDate(Date, FYear, FMonth, FDay);
  98.     FPanel.Visible := true;
  99.     FPanel.Invalidate;
  100.     FUpDownButton := CreateButton;
  101.     FButtonWidth := 17;
  102.     FColor := clWindow;
  103.     Width := 200;
  104.     Height := 26;
  105.   {Ωεφ±≥≡≤Φ≡≤σ∞ Popup}
  106.     FmnuItems[0] := NewItem(V_KINDMON, 0, false, true, MonthPopup, 0, 'mnuMonth');
  107.     FmnuItems[0].GroupIndex := 1;
  108.     FmnuItems[0].RadioItem := true;
  109.     FmnuItems[0].Checked := true;
  110.     FmnuItems[1] := NewItem(V_KINDQUART, 0, false, true, MonthPopup, 0, 'mnuQuarter');
  111.     FmnuItems[1].GroupIndex := 1;
  112.     FmnuItems[1].RadioItem := true;
  113.     FmnuItems[2] := NewItem(V_KINDHALF, 0, false, true, MonthPopup, 0, 'mnuHalfYear');
  114.     FmnuItems[2].GroupIndex := 1;
  115.     FmnuItems[2].RadioItem := true;
  116.     FmnuItems[3] := NewItem(V_KINDYEAR, 0, false, true, MonthPopup, 0, 'mnuYear');
  117.     FmnuItems[3].GroupIndex := 1;
  118.     FmnuItems[3].RadioItem := true;
  119.     FPopup := NewPopupMenu(Self, 'MonPopup', paLeft, true, FmnuItems);
  120.     FEnablePopup := true;
  121.     FPanel.PopupMenu := FPopup;
  122.     FKindRange := ksMonth;
  123.   end;
  124.   Visible := true;
  125.   Change;
  126. end;
  127.  
  128. destructor TVolgaPeriod.Destroy;
  129. begin
  130.   try
  131.     FPanel.PopupMenu := nil;
  132.     FmnuItems[0].Free;
  133.     FmnuItems[1].Free;
  134.     FmnuItems[2].Free;
  135.     FmnuItems[3].Free;
  136.     FPopup.Free;
  137.     FPopup := nil;
  138.   finally
  139.     inherited Destroy;
  140.   end;
  141. end;
  142.  
  143. function TVolgaPeriod.CreateButton: TUpDown;
  144. begin
  145.   Result := TUpDown.Create(Self);
  146.   Result.OnClick := UpDownButtonClick;
  147.   Result.Visible := True;
  148.   Result.Enabled := True;
  149.   Result.Parent := Self;
  150.   Result.Min := -200;
  151.   Result.Max := 200;
  152.   Result.Position := 0;
  153. end;
  154.  
  155. procedure TVolgaPeriod.MonthPopup(Sender: TObject);
  156. var i: integer;
  157. begin
  158.   {±φ ≥ⁿ "παδε≈Ω≤" ±ε Γ±σ⌡ ±≥≡εΩ}
  159.   for i := 0 to 3 do
  160.     FmnuItems[i].Checked := false;
  161.   {∩ε∞σ≥Φ≥ⁿ ≥εδⁿΩε ΩδΦΩφ≤≥≤■}
  162.   TMenuItem(Sender).Checked := true;
  163.   if Sender = FmnuItems[0] then
  164.     KindRange := ksMonth
  165.   else if Sender = FmnuItems[1] then
  166.     KindRange := ksQuarter
  167.   else if Sender = FmnuItems[2] then
  168.     KindRange := ksHalfYear
  169.   else
  170.     KindRange := ksYear;
  171. end;
  172.  
  173. procedure TVolgaPeriod.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
  174. var
  175.   W, H: Integer;
  176. begin
  177.   W := AWidth;
  178.   H := AHeight;
  179.   if W < 20 then W := 20;
  180.   if H < 18 then H := 18;
  181.   if (FUpDownButton <> nil) and (FPanel <> nil) then
  182.   begin
  183.     FPanel.SetBounds(0, 0, W - FButtonWidth, AHeight);
  184.     FUpDownButton.SetBounds(W - FButtonWidth, 0, FButtonWidth, H);
  185.   end;
  186.   inherited SetBounds(ALeft, ATop, W, H);
  187. end;
  188.  
  189. procedure TVolgaPeriod.SetEnablePopup(Value: Boolean);
  190. begin
  191.   if FEnablePopup <> Value then
  192.   begin
  193.     FEnablePopup := Value;
  194.     FPopup.AutoPopup := Value;
  195.   end;
  196. end;
  197.  
  198. procedure TVolgaPeriod.SetKindRange(Value: TKindRange);
  199. var i: integer;
  200. begin
  201.   if FKindRange <> Value then
  202.   begin
  203.     BeforeChanged;                        {Σε Γ±σ⌡ Φτ∞σφσφΦΘ}
  204.     FKindRange := Value;
  205.     {±φ ≥ⁿ "παδε≈Ω≤" ±ε Γ±σ⌡ ±≥≡εΩ}
  206.     for i := 0 to 3 do
  207.       FmnuItems[i].Checked := false;
  208.     {∩ε∞σ≥Φ≥ⁿ ≥εδⁿΩε ΩδΦΩφ≤≥≤■}
  209.     FmnuItems[ord(FKindRange)].Checked := true;
  210.     Change;
  211.   end;
  212. end;
  213.  
  214. procedure TVolgaPeriod.SetYear(Value: word);
  215. begin
  216.   if FYear <> Value then
  217.   begin
  218.     BeforeChanged;                        {Σε Γ±σ⌡ Φτ∞σφσφΦΘ}
  219.     FYear := Value;
  220.     Change;
  221.   end;
  222. end;
  223.  
  224. procedure TVolgaPeriod.SetMonth(Value: word);
  225. begin
  226.   if (Value >= 1) and (Value <= 12) then
  227.     if (FMonth <> Value) or (FStartDate <> EncodeDate(FYear, FMonth, 1)) then
  228.     begin
  229.       BeforeChanged;                      {Σε Γ±σ⌡ Φτ∞σφσφΦΘ}
  230.       FMonth := Value;
  231.       Change;
  232.     end;
  233. end;
  234.  
  235. procedure TVolgaPeriod.Change;
  236. var yy, mm, dd: word;
  237. begin
  238.   case FKindRange of
  239.     ksMonth:
  240.       begin
  241.         FPanel.Caption := LongMonthNames[FMonth] + ' ' + IntToStr(FYear) + V_SHORTYEAR;
  242.         FEndDate := EncodeDate(FYear, FMonth, 28) + 4;
  243.       end;
  244.     ksQuarter:
  245.       begin
  246.         case FMonth of
  247.           1..3:
  248.             begin
  249.               FMonth := 1;
  250.               FPanel.Caption := '1 '+ V_KINDQUART+ IntToStr(FYear) + V_SHORTYEAR;
  251.             end;
  252.           4..6:
  253.             begin
  254.               FMonth := 4;
  255.               FPanel.Caption := '2 '+ V_KINDQUART+ IntToStr(FYear) + V_SHORTYEAR;
  256.             end;
  257.           7..9:
  258.             begin
  259.               FMonth := 7;
  260.               FPanel.Caption := '3 '+ V_KINDQUART+ IntToStr(FYear) + V_SHORTYEAR;
  261.             end;
  262.           10..12:
  263.             begin
  264.               FMonth := 10;
  265.               FPanel.Caption := '4 '+ V_KINDQUART+ IntToStr(FYear) + V_SHORTYEAR;
  266.             end;
  267.         end;
  268.         FEndDate := EncodeDate(FYear, FMonth + 2, 28) + 4;
  269.       end;
  270.     ksHalfYear:
  271.       begin
  272.         case FMonth of
  273.           1..6:
  274.             begin
  275.               FMonth := 1;
  276.               FPanel.Caption := '1 ' +V_KINDHALF+ IntToStr(FYear) + V_SHORTYEAR;
  277.             end;
  278.           7..12:
  279.             begin
  280.               FMonth := 7;
  281.               FPanel.Caption := '2 '+V_KINDHALF + IntToStr(FYear) + V_SHORTYEAR;
  282.             end;
  283.         end;
  284.         FEndDate := EncodeDate(FYear, FMonth + 5, 28) + 4;
  285.       end;
  286.     ksYear:
  287.       begin
  288.         FMonth := 1;
  289.         FPanel.Caption := IntToStr(FYear) + V_LONGYEAR;
  290.         FEndDate := EncodeDate(FYear, 12, 31) + 1;
  291.       end;
  292.   end;
  293.   DecodeDate(FEndDate, yy, mm, dd);
  294.   FEndDate := FEndDate - dd;
  295.   FStartDate := EncodeDate(FYear, FMonth, 1);
  296.   if assigned(FOnChange) then FOnChange(Self);
  297. end;
  298.  
  299. procedure TVolgaPeriod.UpDownButtonClick(Sender: TObject; Button: TUDBtnType);
  300. begin
  301.   BeforeChanged;                          {Σε Γ±σ⌡ Φτ∞σφσφΦΘ}
  302.   if Button = btNext then
  303.   begin
  304.     case FKindRange of
  305.       ksMonth:
  306.         if FMonth < 12 then
  307.           Inc(FMonth)
  308.         else
  309.         begin
  310.           FMonth := 1;
  311.           Inc(FYear);
  312.         end;
  313.       ksQuarter:
  314.         case FMonth of
  315.           1..3: FMonth := 4;
  316.           4..6: FMonth := 7;
  317.           7..9: FMonth := 10;
  318.           10..12:
  319.             begin
  320.               FMonth := 1;
  321.               Inc(FYear);
  322.             end;
  323.         end;
  324.       ksHalfYear:
  325.         case FMonth of
  326.           1..6: FMonth := 7;
  327.           7..12:
  328.             begin
  329.               FMonth := 1;
  330.               Inc(FYear);
  331.             end;
  332.         end;
  333.       ksYear: Inc(FYear);
  334.     end;
  335.   end
  336.   else
  337.   begin
  338.     case FKindRange of
  339.       ksMonth:
  340.         if FMonth > 1 then
  341.           Dec(FMonth)
  342.         else
  343.         begin
  344.           FMonth := 12;
  345.           Dec(FYear);
  346.         end;
  347.       ksQuarter:
  348.         case FMonth of
  349.           1..3:
  350.             begin
  351.               FMonth := 10;
  352.               Dec(FYear);
  353.             end;
  354.           4..6: FMonth := 1;
  355.           7..9: FMonth := 4;
  356.           10..12: FMonth := 7;
  357.         end;
  358.       ksHalfYear:
  359.         case FMonth of
  360.           1..6:
  361.             begin
  362.               FMonth := 7;
  363.               Dec(FYear);
  364.             end;
  365.           7..12: FMonth := 1;
  366.         end;
  367.       ksYear: Dec(FYear);
  368.     end;
  369.   end;
  370.   Change;                                 {∩ε±δσ Γ±σ⌡ Φτ∞σφσφΦΘ}
  371. end;
  372.  
  373. procedure TVolgaPeriod.SetColor(Value: TColor);
  374. begin
  375.   if FColor <> Value then
  376.   begin
  377.     FColor := Value;
  378.     FPanel.Color := FColor;
  379.   end;
  380. end;
  381.  
  382. function TVolgaPeriod.GetText: string;
  383. begin
  384.   Result := FPanel.Caption;
  385. end;
  386.  
  387. procedure TVolgaPeriod.SetButtonWidth(Value: byte);
  388. begin
  389.   if FButtonWidth <> Value then
  390.   begin
  391.     FButtonWidth := Value;
  392.     SetBounds(Left, Top, Width, Height);
  393.   end;
  394. end;
  395.  
  396. procedure TVolgaPeriod.SetStartDate(const Value: TDateTime);
  397. var god, mes, den: word;
  398. begin
  399.   BeforeChanged;                          {Σε Γ±σ⌡ Φτ∞σφσφΦΘ}
  400.   DecodeDate(Value, god, mes, den);
  401.   FYear := god;
  402.   Month := mes;                           {Γ√τ√Γασ≥±  SetMonth}
  403. end;
  404.  
  405. procedure TVolgaPeriod.BeforeChanged;
  406. begin
  407.   if assigned(FBeforeChange) then FBeforeChange(Self);
  408. end;
  409.  
  410. end.
  411.  
  412.