OSDN Git Service

TEditor、TTaskTrayの配布先変更に追従
[winbottle/winbottle.git] / bottleclient / SppList.pas
1 {
2  SppList - SPP\83v\83\89\83O\83C\83\93\82ð\8c\9f\8dõ\82µ\82Ä\95Û\8e\9d\82·\82é
3 }
4
5 unit SppList;
6
7 interface
8
9 uses Classes, Windows, SysUtils, Contnrs, Forms, Dialogs,
10   Graphics, ProgressWindow, Plugins;
11
12 type
13   TLoadPluginEventType = (lpPreLoad, lpPostLoad);
14   TLoadPluginEvent = procedure(Sender: TObject;
15     Status: TLoadPluginEventType; Plugin: String) of object;
16
17   TSppList = class(TObjectList)
18   private
19     FImageCache: TObjectList;
20     FOnLoadPlugin: TLoadPluginEvent;
21     function GetSppItem(Index: integer): TSppPlugin;
22     procedure SetOnLoadPlugin(const Value: TLoadPluginEvent);
23   public
24     procedure ClearImagePool;
25     constructor Create(OwnsObject: boolean);
26     destructor Destroy; override;
27     procedure LoadFromDirectory(Dir: String);
28     function TryGetImage(const Ghost: String; const Surface: integer;
29       Bitmap: TBitmap): boolean;
30     property SppItem[Index: integer]: TSppPlugin read GetSppItem;
31     property OnLoadPlugin: TLoadPluginEvent read FOnLoadPlugin write SetOnLoadPlugin;
32   end;
33
34 var
35   Spps: TSppList;
36
37 const
38   MaxPoolImage = 100;
39
40 implementation
41
42
43 { TSppList }
44
45 procedure TSppList.ClearImagePool;
46 begin
47   FImageCache.Clear;
48 end;
49
50 constructor TSppList.Create(OwnsObject: boolean);
51 begin
52   inherited Create(true);
53   if OwnsObject = false then
54     raise Exception.Create('TSppList.OwnsObject must be set to true');
55   FImageCache := TObjectList.Create;
56 end;
57
58 destructor TSppList.Destroy;
59 begin
60   FImageCache.Free;
61   inherited;
62 end;
63
64 function TSppList.GetSppItem(Index: integer): TSppPlugin;
65 begin
66   Result := Items[Index] as TSppPlugin;
67 end;
68
69 procedure TSppList.LoadFromDirectory(Dir: String);
70 var F: TSearchRec;
71     i: integer;
72     Item: TSppPlugin;
73     ProgressWindow: TfrmProgressWindow;
74 begin
75   Clear; // \91O\82É\93Ç\82Ý\8d\9e\82ñ\82¾\82Ì\82Í\91S\95\94\8fÁ\82¦\82é
76   ClearImagePool;
77   ProgressWindow := TfrmProgressWindow.Create(nil);
78   try
79     ProgressWindow.Show;
80     i := FindFirst(Dir + '\*.dll', 0, F);
81     if i = 0 then begin
82       repeat
83         try
84           if Assigned(FOnLoadPlugin) then
85             FOnLoadPlugin(Self, lpPreLoad, F.Name);
86           ProgressWindow.StatusText := Format('\83v\83\89\83O\83C\83\93%s\82ð\93Ç\82Ý\8d\9e\82Ý\92\86...', [F.Name]);
87           Item := TSppPlugin.Create(Dir + '\' + F.Name);
88           Self.Add(Item);
89           if Assigned(FOnLoadPlugin) then
90             FOnLoadPlugin(Self, lpPostLoad, F.Name);
91         except
92           on E: ESppException do
93           begin
94             ShowMessage('SPP\83v\83\89\83O\83C\83\93\83\8d\81[\83h\8e¸\94s:'#13#10 + E.Message);
95           end;
96         end;
97         i := FindNext(F);
98       until i <> 0;
99     end;
100     FindClose(F);
101   finally
102     ProgressWindow.Free;
103   end;
104 end;
105
106 procedure TSppList.SetOnLoadPlugin(const Value: TLoadPluginEvent);
107 begin
108   FOnLoadPlugin := Value;
109 end;
110
111 function TSppList.TryGetImage(const Ghost: String; const Surface: integer;
112   Bitmap: TBitmap): boolean;
113 var i, j: integer;
114     SppImage: TSppImage;
115 begin
116   Result := false;
117
118   // \8aù\82É\83L\83\83\83b\83V\83\85\8dÏ\82Ý\82Ì\8fê\8d\87
119   for i := 0 to FImageCache.Count-1 do
120   begin
121     if (FImageCache[i] as TSppImage).KeyMatchTest(Ghost, Surface, 0) then
122     begin
123       OutputDebugString(PChar('Cache Used: ' + Ghost + IntToStr(Surface)));
124       Result := true;
125       Bitmap.Assign((FImageCache[i] as TSppImage).Bitmap);
126       FImageCache.Move(i, 0); // \90æ\93ª\82É\8e\9d\82Á\82Ä\82­\82é
127       Exit;
128     end;
129   end;
130   SppImage := nil;
131   // DLL\82ð\8eÀ\8dÛ\82É\8cÄ\82Ñ\8fo\82µ\82Ä\8c\9f\8dõ\82·\82é
132   for i := 0 to Self.Count-1 do
133   begin
134     SppImage :=  (Items[i] as TSppPlugin).GetImage(Ghost, Surface, 0);
135     if SppImage <> nil then
136     begin
137       Result := true;
138       Bitmap.Assign(SppImage.Bitmap);
139       Break;
140     end;
141   end;
142   // DLL\8cÄ\82Ñ\8fo\82µ\82Ì\8c\8b\89Ê\82ð\83L\83\83\83b\83V\83\85\82·\82é
143   if SppImage <> nil then
144   begin
145     FImageCache.Insert(0, SppImage);
146   end else
147   begin
148     SppImage := TSppImage.Create;
149     SppImage.Ghost := Ghost;
150     SppImage.SurfaceKey := IntToStr(Surface) + ',*';
151     FImageCache.Insert(0, SppImage);
152   end;
153
154   // \82 \82Ó\82ê\82½\83L\83\83\83b\83V\83\85\82Ì\83N\83\8a\83A
155   if FImageCache.Count > MaxPoolImage then
156     for j := FImageCache.Count-1 downto MaxPoolImage do
157       FImageCache.Delete(j);
158 end;
159
160 initialization
161
162 Spps := TSppList.Create(true);
163
164 finalization
165
166 Spps.Free;
167
168 end.