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, 
7 {$IF Defined(DELPRO) }
8         SHDocVw
9 {$ELSE}
10         SHDocVw_TLB
11 {$IFEND}
12 ;
13 type
14         TPreviewBrowser = class(TWebBrowser)
15         private
16         function makeHTML(const URL, Host, Document : String): String;
17         protected
18                 procedure CreateParams(var Params: TCreateParams); override;
19         public
20                 constructor Create(AOwner: TComponent); override;
21                 destructor Destroy; override;
22         procedure PreviewImage(URL : String);
23         function GetWindowRect(Point: TPoint) : TRect;
24         end;
25
26 implementation
27 uses MojuUtils, GikoSystem, Setting;
28
29 const
30         //\83v\83\8c\83r\83\85\81[\83t\83@\83C\83\8b\96¼
31         HTML_FILE_NAME  = 'temp_preview.html';
32     // \83}\83E\83X\83J\81[\83\\83\8b\82©\82ç\82Ì\82¸\82ç\82µ\88Ê\92u
33     DIV_X = 15;
34     DIV_Y = 15;
35     NICO = 'www.nicovideo.jp';
36
37 constructor TPreviewBrowser.Create(AOwner: TComponent);
38 begin
39         inherited Create(AOwner);
40 end;
41
42 destructor TPreviewBrowser.Destroy;
43 begin
44         inherited Destroy;
45 end;
46
47 procedure TPreviewBrowser.CreateParams(var Params: TCreateParams);
48 begin
49         inherited;
50 end;
51 {
52 \brief  \8ew\92è\82³\82ê\82½URL\82Ì\83v\83\8c\83r\83\85\81[
53 \param  URL \83v\83\8c\83r\83\85\81[\82·\82é\83C\83\81\81[\83W\82ÌURL
54 }
55 procedure TPreviewBrowser.PreviewImage(URL : String);
56 var
57     HtmlFileName : string;
58     sl : TStringList;
59     Protocol, Host, Path, Document, Port, Bookmark : string;
60     Referer : string;
61         Flags: OleVariant;
62         TargetFrameName: OleVariant;
63         PostData: OleVariant;
64         Headers: OleVariant;
65 begin
66         GikoSys.ParseURI(URL, Protocol, Host, Path, Document, Port, Bookmark);
67         Referer := Protocol + '://' + Host;
68         if Port <> '' then
69                 Referer := Referer + ':' + Port;
70         Referer := Referer + Path;
71         Headers := 'Referer: ' + Referer;
72     Flags := 0;
73     TargetFrameName := '';
74     PostData := '';
75
76     HtmlFileName := GikoSys.GetAppDir + HTML_FILE_NAME;
77         sl := TStringList.Create;
78         try
79                 try
80             sl.Text := makeHTML(URL, Host, Document);
81                         sl.SaveToFile(HtmlFileName);
82                 finally
83                         sl.Free;
84                 end;
85         except
86         end;
87
88
89         Navigate(HtmlFileName,Flags, TargetFrameName, PostData, Headers);
90
91 end;
92 {
93 \breif \95\\8e¦\82·\82é\83E\83B\83\93\83h\83E\83T\83C\83Y\82ð\8eæ\93¾\82·\82é
94 \param Point \83}\83E\83X\83J\81[\83\\83\8b\82Ì\8dÀ\95W
95 }
96 function TPreviewBrowser.GetWindowRect(Point: TPoint) : TRect;
97 var
98     WindowWidth, WindowHeight : Integer;
99 begin
100     // \90Ý\92è\82É\82æ\82é\8fê\8d\87\82í\82¯
101         case GikoSys.Setting.PreviewSize of
102                 gpsXSmall: begin
103                         WindowWidth := 128;
104                         WindowHeight := 96;
105                 end;
106                 gpsSmall: begin
107                         WindowWidth := 256;
108                         WindowHeight := 192;
109                 end;
110                 gpsLarge: begin
111                         WindowWidth := 512;
112                         WindowHeight := 384;
113                 end;
114                 gpsXLarge: begin
115                         WindowWidth := 640;
116                         WindowHeight := 480;
117                 end;
118                 else begin      //gpsMedium
119                         WindowWidth := 384;
120                         WindowHeight := 288;
121                 end;
122         end;
123
124         Result := Rect(0, 0, WindowWidth, WindowHeight);
125     // bata55\88È\91O\82Í\8d\89E\82ª\8aÔ\88á\82Á\82Ä\82½
126     // \8fo\82µ\88Ê\92u\82É\82æ\82é\95â\90³
127         case GikoSys.Setting.PopupPosition of
128                 gppLeftTop:             OffsetRect(Result,
129             Point.x - WindowWidth - DIV_X, Point.y - WindowHeight -     DIV_Y);
130                 gppLeft:                        OffsetRect(Result,
131             Point.x - WindowWidth - DIV_X, Point.y - (WindowHeight div 2));
132                 gppLeftBottom: OffsetRect(Result,
133             Point.x - WindowWidth - DIV_X, Point.y + DIV_Y);
134                 gppTop:                         OffsetRect(Result,
135             Point.x - (WindowWidth div 2), Point.y - WindowHeight - DIV_Y);
136                 gppCenter:                      OffsetRect(Result,
137             Point.x - (WindowWidth div 2), Point.y - (WindowHeight div 2));
138                 gppBottom:                      OffsetRect(Result,
139             Point.x - (WindowWidth div 2), Point.y + DIV_Y);
140                 gppRightTop:                    OffsetRect(Result,
141             Point.x + DIV_X, Point.y - WindowHeight - DIV_Y);
142                 gppRight:                       OffsetRect(Result,
143             Point.x + DIV_X, Point.y - (WindowHeight div 2));
144                 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Ó
145         end;
146
147 end;
148
149 {
150 \breif \83v\83\8c\83r\83\85\81[\97p\82ÌHTML\82ð\8dì\90¬\82·\82é
151 \param URL \83v\83\8c\83r\83\85\81[\82·\82é\83C\83\81\81[\83W\83t\83@\83C\83\8b
152 }
153 function TPreviewBrowser.makeHTML(const URL, Host, Document : String): String;
154 var
155     point :TPoint;
156     rect  :TRect;
157 begin
158     if (Pos('http://www.nicovideo.jp/watch/', URL) <> 1) then begin
159         Result := '<html><head>'#13#10
160                                 + '<SCRIPT>'#13#10
161                                 + 'function init() {'#13#10
162                                 + '     if ((document.body.clientHeight >= Image1.height) && (document.body.clientWidth >= Image1.width)) {'#13#10
163                                 + '     } else {'#13#10
164                                 + '             var dh, ih;'#13#10
165                                 + '             dh = document.body.clientWidth / document.body.clientHeight;'#13#10
166                                 + '             ih = Image1.width / Image1.height;'#13#10
167                                 + '             if (document.body.clientWidth < document.body.clientHeight) {'#13#10
168                                 + '                     if (ih > dh)'#13#10
169                                 + '                             Image1.width = document.body.clientWidth;'#13#10
170                                 + '                     else'#13#10
171                                 + '                             Image1.height = document.body.clientHeight;'#13#10
172                                 + '             } else {'#13#10
173                                 + '                     if (ih < dh)'#13#10
174                                 + '                             Image1.height = document.body.clientHeight;'#13#10
175                                 + '                     else'#13#10
176                                 + '                             Image1.width = document.body.clientWidth;'#13#10
177                                 + '             }'#13#10
178                                 + '     }'#13#10
179                                 + '     Message.style.display = "none";'#13#10
180                                 + '}'#13#10
181                                 + '</SCRIPT>'#13#10
182                                 + '</head>'#13#10
183                                 + '<body topmargin="0" leftmargin="0" style="border-width: 1px; overflow:hidden; border-style: solid;" onLoad="init()">'#13#10
184                                 + '<div align="center" id="Message">\83v\83\8c\83r\83\85\81[\8dì\90¬\92\86</div>'#13#10
185                                 + '<div align="center"><img name="Image1" border="0" src="' + URL + '"></div>'#13#10
186                                 + '</body></html>';
187     end else begin
188         // <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>
189         rect := GetWindowRect(point);
190
191         Result := '<html><head>'#13#10
192                 + '<SCRIPT>'#13#10
193                 + 'function init() {'#13#10
194                 + '     Message.style.display = "none";'#13#10
195                 + '}'#13#10
196                 + '</SCRIPT>'#13#10
197                 + '</head>'#13#10
198                 + '<body topmargin="0" leftmargin="0" style="border-width: 1px; overflow:hidden; border-style: solid;" onLoad="init()">'#13#10
199                 + '<div align="center" id="Message">\83v\83\8c\83r\83\85\81[\8dì\90¬\92\86</div>'#13#10
200                 + '<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>'
201                                 + '</body></html>';
202     end;
203 end;
204 end.