OSDN Git Service

・板一覧更新の処理メッセージを追加
[gikonavigoeson/gikonavi.git] / Preview.pas
1 unit Preview;
2
3 interface
4 uses
5         Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
6         ActiveX, OleCtrls, HTMLDocumentEvent,
7 {$IF Defined(DELPRO) }
8         SHDocVw,
9         MSHTML
10 {$ELSE}
11         SHDocVw_TLB,
12         MSHTML_TLB
13 {$IFEND}
14 ;
15 type
16         TPreviewBrowser = class(TWebBrowser)
17         private
18         FEvent: THTMLDocumentEventSink;//\83u\83\89\83E\83U\83h\83L\83\85\83\81\83\93\83g\83C\83x\83\93\83g
19         function makeHTML(const URL, Host, Document : String): String;
20         procedure BrowserDocumentComplete(Sender: TObject;
21                     const pDisp: IDispatch; var URL: OleVariant);
22         function PreviewDbClick(Sender: TObject): WordBool;
23         protected
24                 procedure CreateParams(var Params: TCreateParams); override;
25         public
26                 constructor Create(AOwner: TComponent); override;
27                 destructor Destroy; override;
28         procedure PreviewImage(URL : String);
29         function GetWindowRect(Point: TPoint) : TRect;
30         end;
31
32 implementation
33 uses MojuUtils, GikoSystem, Setting, Giko;
34
35 const
36         //\83v\83\8c\83r\83\85\81[\83t\83@\83C\83\8b\96¼
37         HTML_FILE_NAME  = 'temp_preview.html';
38     // \83}\83E\83X\83J\81[\83\\83\8b\82©\82ç\82Ì\82¸\82ç\82µ\88Ê\92u
39     DIV_X = 15;
40     DIV_Y = 15;
41     NICO = 'www.nicovideo.jp';
42
43 constructor TPreviewBrowser.Create(AOwner: TComponent);
44 begin
45         inherited Create(AOwner);
46     FEvent := nil;
47     OnDocumentComplete := BrowserDocumentComplete;
48 end;
49
50 destructor TPreviewBrowser.Destroy;
51 begin
52     if (FEvent <> nil) then begin
53         FreeAndNil(FEvent);
54     end;
55         inherited Destroy;
56 end;
57
58 procedure TPreviewBrowser.CreateParams(var Params: TCreateParams);
59 begin
60         inherited;
61 end;
62 {
63 \brief  \8ew\92è\82³\82ê\82½URL\82Ì\83v\83\8c\83r\83\85\81[
64 \param  URL \83v\83\8c\83r\83\85\81[\82·\82é\83C\83\81\81[\83W\82ÌURL
65 }
66 procedure TPreviewBrowser.PreviewImage(URL : String);
67 var
68     HtmlFileName : string;
69     sl : TStringList;
70     Protocol, Host, Path, Document, Port, Bookmark : string;
71     Referer : string;
72         Flags: OleVariant;
73         TargetFrameName: OleVariant;
74         PostData: OleVariant;
75         Headers: OleVariant;
76 begin
77         GikoSys.ParseURI(URL, Protocol, Host, Path, Document, Port, Bookmark);
78         Referer := Protocol + '://' + Host;
79         if Port <> '' then
80                 Referer := Referer + ':' + Port;
81         Referer := Referer + Path;
82         Headers := 'Referer: ' + Referer;
83     Flags := 0;
84     TargetFrameName := '';
85     PostData := '';
86
87     HtmlFileName := GikoSys.GetAppDir + HTML_FILE_NAME;
88         sl := TStringList.Create;
89         try
90                 try
91             sl.Text := makeHTML(URL, Host, Document);
92                         sl.SaveToFile(HtmlFileName);
93                 finally
94                         sl.Free;
95                 end;
96         except
97         end;
98
99
100         Navigate(HtmlFileName,Flags, TargetFrameName, PostData, Headers);
101
102 end;
103 {
104 \breif \95\\8e¦\82·\82é\83E\83B\83\93\83h\83E\83T\83C\83Y\82ð\8eæ\93¾\82·\82é
105 \param Point \83}\83E\83X\83J\81[\83\\83\8b\82Ì\8dÀ\95W
106 }
107 function TPreviewBrowser.GetWindowRect(Point: TPoint) : TRect;
108 var
109     WindowWidth, WindowHeight : Integer;
110 begin
111     // \90Ý\92è\82É\82æ\82é\8fê\8d\87\82í\82¯
112         case GikoSys.Setting.PreviewSize of
113                 gpsXSmall: begin
114                         WindowWidth := 128;
115                         WindowHeight := 96;
116                 end;
117                 gpsSmall: begin
118                         WindowWidth := 256;
119                         WindowHeight := 192;
120                 end;
121                 gpsLarge: begin
122                         WindowWidth := 512;
123                         WindowHeight := 384;
124                 end;
125                 gpsXLarge: begin
126                         WindowWidth := 640;
127                         WindowHeight := 480;
128                 end;
129                 else begin      //gpsMedium
130                         WindowWidth := 384;
131                         WindowHeight := 288;
132                 end;
133         end;
134
135         Result := Rect(0, 0, WindowWidth, WindowHeight);
136     // bata55\88È\91O\82Í\8d\89E\82ª\8aÔ\88á\82Á\82Ä\82½
137     // \8fo\82µ\88Ê\92u\82É\82æ\82é\95â\90³
138         case GikoSys.Setting.PopupPosition of
139                 gppLeftTop:             OffsetRect(Result,
140             Point.x - WindowWidth - DIV_X, Point.y - WindowHeight -     DIV_Y);
141                 gppLeft:                        OffsetRect(Result,
142             Point.x - WindowWidth - DIV_X, Point.y - (WindowHeight div 2));
143                 gppLeftBottom: OffsetRect(Result,
144             Point.x - WindowWidth - DIV_X, Point.y + DIV_Y);
145                 gppTop:                         OffsetRect(Result,
146             Point.x - (WindowWidth div 2), Point.y - WindowHeight - DIV_Y);
147                 gppCenter:                      OffsetRect(Result,
148             Point.x - (WindowWidth div 2), Point.y - (WindowHeight div 2));
149                 gppBottom:                      OffsetRect(Result,
150             Point.x - (WindowWidth div 2), Point.y + DIV_Y);
151                 gppRightTop:                    OffsetRect(Result,
152             Point.x + DIV_X, Point.y - WindowHeight - DIV_Y);
153                 gppRight:                       OffsetRect(Result,
154             Point.x + DIV_X, Point.y - (WindowHeight div 2));
155                 gppRightBottom:         OffsetRect(Result, Point.x + DIV_X, Point.y + DIV_Y);           //\83M\83R\83i\83r\83X\83\8c \83p\81[\83g\82P\82Ì453\8e\81\82É\8a´\8eÓ
156         end;
157
158 end;
159
160 {
161 \breif \83v\83\8c\83r\83\85\81[\97p\82ÌHTML\82ð\8dì\90¬\82·\82é
162 \param URL \83v\83\8c\83r\83\85\81[\82·\82é\83C\83\81\81[\83W\83t\83@\83C\83\8b
163 }
164 function TPreviewBrowser.makeHTML(const URL, Host, Document : String): String;
165 var
166     point :TPoint;
167     rect  :TRect;
168 begin
169     if (Pos('http://www.nicovideo.jp/watch/', URL) <> 1) then begin
170         Result := '<html><head>'#13#10
171                                 + '<SCRIPT>'#13#10
172                                 + 'function init() {'#13#10
173                                 + '     if ((document.body.clientHeight >= Image1.height) && (document.body.clientWidth >= Image1.width)) {'#13#10
174                                 + '     } else {'#13#10
175                                 + '             var dh, ih;'#13#10
176                                 + '             dh = document.body.clientWidth / document.body.clientHeight;'#13#10
177                                 + '             ih = Image1.width / Image1.height;'#13#10
178                                 + '             if (document.body.clientWidth < document.body.clientHeight) {'#13#10
179                                 + '                     if (ih > dh)'#13#10
180                                 + '                             Image1.width = document.body.clientWidth;'#13#10
181                                 + '                     else'#13#10
182                                 + '                             Image1.height = document.body.clientHeight;'#13#10
183                                 + '             } else {'#13#10
184                                 + '                     if (ih < dh)'#13#10
185                                 + '                             Image1.height = document.body.clientHeight;'#13#10
186                                 + '                     else'#13#10
187                                 + '                             Image1.width = document.body.clientWidth;'#13#10
188                                 + '             }'#13#10
189                                 + '     }'#13#10
190                                 + '     Message.style.display = "none";'#13#10
191                                 + '}'#13#10
192                                 + '</SCRIPT>'#13#10
193                                 + '</head>'#13#10
194                                 + '<body topmargin="0" leftmargin="0" style="border-width: 1px; overflow:hidden; border-style: solid;" onLoad="init()">'#13#10
195                                 + '<div align="center" id="Message">\83v\83\8c\83r\83\85\81[\8dì\90¬\92\86</div>'#13#10
196                                 + '<div align="center"><img name="Image1" border="0" src="' + URL + '"></div>'#13#10
197                                 + '</body></html>';
198     end else begin
199         // <div><iframe width="340" height="185" src="http://www.nicovideo.jp/thumb/sm2494604" scrolling="no" style="border:solid 1px #CCC;" frameborder="0"></iframe></div><div class=link_nicovideo_title><a href="" title="" target="_blank"></a></div>
200         rect := GetWindowRect(point);
201
202         Result := '<html><head>'#13#10
203                 + '<SCRIPT>'#13#10
204                 + 'function init() {'#13#10
205                 + '     Message.style.display = "none";'#13#10
206                 + '}'#13#10
207                 + '</SCRIPT>'#13#10
208                 + '</head>'#13#10
209                 + '<body topmargin="0" leftmargin="0" style="border-width: 1px; overflow:hidden; border-style: solid;" onLoad="init()">'#13#10
210                 + '<div align="center" id="Message">\83v\83\8c\83r\83\85\81[\8dì\90¬\92\86</div>'#13#10
211                 + '<div><iframe width="' + IntToStr(rect.Right - rect.Left) +'" height="' + IntToStr(rect.Bottom - rect.Top) + '" src="http://' + Host + '/thumb/' + Document + '" scrolling="no" style="border:solid 1px #CCC;" frameborder="0"></iframe></div>'
212                                 + '</body></html>';
213     end;
214 end;
215 //! \83h\83L\83\85\83\81\83\93\83g\8a®\97¹\83C\83x\83\93\83g
216 procedure TPreviewBrowser.BrowserDocumentComplete(Sender: TObject;
217     const pDisp: IDispatch; var URL: OleVariant);
218 begin
219     if (URL <> 'about:blank') then begin
220         FEvent := THTMLDocumentEventSink.Create(Self, Self.OleObject.Document,
221             HTMLDocumentEvents2);
222         FEvent.OnDoubleClick := PreviewDbClick;
223     end else begin
224         if (FEvent <> nil) then begin
225             FreeAndNil(FEvent);
226         end;
227     end;
228 end;
229 function TPreviewBrowser.PreviewDbClick(Sender: TObject): WordBool;
230 begin
231     // \8e©\95ª\82Å\8e©\95ª\82Í\8fÁ\82¹\82È\82¢\82Ì\82Å\81A\83\81\83b\83Z\81[\83W\8co\97R\82Å\8fÁ\82µ\82Ä\82à\82ç\82¤
232     PostMessage( GikoForm.Handle , USER_POPUPCLEAR, Integer( Self ), 0 );
233     Result := True;
234 end;
235
236 end.