OSDN Git Service

67c0d9f889622f81baeace83f70fbc0675a304b4
[winbottle/winbottle.git] / bottleclient / LogForm.pas
1 unit LogForm;
2
3 interface
4
5 uses
6   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7   ComCtrls, ToolWin, StdCtrls, ExtCtrls, SsParser, BottleDef, Menus,
8   Clipbrd, Logs, ShellAPI, Commctrl, DirectSstp, Contnrs, StrUtils,
9   TalkShowFrame, SppList, HtmlOutputConfig, HtmlOutputProgress,
10   SearchLog, IniFiles, BRegExp;
11
12 type
13   // \83\8d\83O\82Ì\95Û\91\95û\96@
14   TSaveLogType = (stLog, stLogWithChannels, stText, stXML);
15
16   // \83\8a\83X\83g\83r\83\85\81[\82Ì\83X\83N\83\8d\81[\83\8b\95û\8cü
17   TLVScrollDir = (lvScrollUp, lvScrollDown);
18
19   TfrmLog = class(TForm)
20     ToolBar: TToolBar;
21     tbtnClear: TToolButton;
22     pnlUpper: TPanel;
23     SsParser: TSsParser;
24     StatusBar: TStatusBar;
25     tbtnSaveLog: TToolButton;
26     PopupMenuPreview: TPopupMenu;
27     mnPopCopy: TMenuItem;
28     tbtnVoteMessage: TToolButton;
29     PopupMenuListView: TPopupMenu;
30     mnPopUpVoteMessage: TMenuItem;
31     SaveDialog: TSaveDialog;
32     pnlPanel: TPanel;
33     Splitter: TSplitter;
34     mnPopUpCopyScript: TMenuItem;
35     PopupMenuSaveLog: TPopupMenu;
36     mnSaveLog: TMenuItem;
37     mnSaveLogChannel: TMenuItem;
38     mnSaveLogScript: TMenuItem;
39     mnSaveLogXML: TMenuItem;
40     ToolButton1: TToolButton;
41     mnJumpURL: TMenuItem;
42     mnPopUpAgreeMessage: TMenuItem;
43     tbtnAgreeMessage: TToolButton;
44     ToolButton2: TToolButton;
45     tbtnPreviewStyle: TToolButton;
46     PopupMenuPreviewStyle: TPopupMenu;
47     mnPreviewStyleConversation: TMenuItem;
48     mnPreviewStyleScript: TMenuItem;
49     mnPreviewStyleScriptWithLineBreak: TMenuItem;
50     Panel1: TPanel;
51     tabBottleLog: TTabControl;
52     lvwLog: TListView;
53     tbtnDownloadLog: TToolButton;
54     PopupMenuTab: TPopupMenu;
55     mnCloseTab: TMenuItem;
56     tbtnFindBottle: TToolButton;
57     tbtnOpenLog: TToolButton;
58     OpenDialog: TOpenDialog;
59     tbtnInsertCue: TToolButton;
60     mnInsertCue: TMenuItem;
61     PopupMenuListPreviewStyle: TPopupMenu;
62     mnListPreviewStyleNormal: TMenuItem;
63     mnListPreviewStyleTagStripped: TMenuItem;
64     tbtnListPreviewStyle: TToolButton;
65     mnListPreviewStyleNoColor: TMenuItem;
66     SsParserForTalkShow: TSsParser;
67     mnPreviewStyleConversationImage: TMenuItem;
68     pnlPreviewArea: TPanel;
69     TalkShowFrame: TfrmTalkShow;
70     edtScript: TRichEdit;
71     tbtnSendEditor: TToolButton;
72     mnSendEditor: TMenuItem;
73     timScrollTimer: TTimer;
74     mnChangeTabName: TMenuItem;
75     N1: TMenuItem;
76     N2: TMenuItem;
77     mnDeleteLogItem: TMenuItem;
78     mnTabSaveXMLLog: TMenuItem;
79     mnSaveHTML: TMenuItem;
80     mnPopupCopyGhost: TMenuItem;
81     procedure tbtnClearClick(Sender: TObject);
82     procedure FormCreate(Sender: TObject);
83     procedure lvwLogChange(Sender: TObject; Item: TListItem;
84       Change: TItemChange);
85     procedure lvwLogDblClick(Sender: TObject);
86     procedure lvwLogKeyPress(Sender: TObject; var Key: Char);
87     procedure FormDestroy(Sender: TObject);
88     procedure lvwLogClick(Sender: TObject);
89     procedure mnSaveLogClick(Sender: TObject);
90     procedure lvwLogColumnClick(Sender: TObject; Column: TListColumn);
91     procedure mnPopUpCopyScriptClick(Sender: TObject);
92     procedure mnSaveLogChannelClick(Sender: TObject);
93     procedure mnSaveLogScriptClick(Sender: TObject);
94     procedure mnSaveLogXMLClick(Sender: TObject);
95     procedure lvwLogData(Sender: TObject; Item: TListItem);
96     procedure PopupMenuListViewPopup(Sender: TObject);
97     procedure lvwLogCustomDrawItem(Sender: TCustomListView;
98       Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
99     procedure PopupMenuPreviewStylePopup(Sender: TObject);
100     procedure mnPreviewStyleClick(Sender: TObject);
101     procedure tbtnPreviewStyleClick(Sender: TObject);
102     procedure tabBottleLogChange(Sender: TObject);
103     procedure tabBottleLogChanging(Sender: TObject;
104       var AllowChange: Boolean);
105     procedure tabBottleLogContextPopup(Sender: TObject; MousePos: TPoint;
106       var Handled: Boolean);
107     procedure mnCloseTabClick(Sender: TObject);
108     procedure tbtnFindBottleClick(Sender: TObject);
109     procedure tbtnOpenLogClick(Sender: TObject);
110     procedure tabBottleLogMouseDown(Sender: TObject; Button: TMouseButton;
111       Shift: TShiftState; X, Y: Integer);
112     procedure tabBottleLogDragOver(Sender, Source: TObject; X, Y: Integer;
113       State: TDragState; var Accept: Boolean);
114     procedure tabBottleLogDragDrop(Sender, Source: TObject; X, Y: Integer);
115     procedure tabBottleLogEndDrag(Sender, Target: TObject; X, Y: Integer);
116     procedure lvwLogDrawItem(Sender: TCustomListView; Item: TListItem;
117       Rect: TRect; State: TOwnerDrawState);
118     procedure mnListPreviewStyleClick(Sender: TObject);
119     procedure tbtnListPreviewStyleClick(Sender: TObject);
120     procedure PopupMenuListPreviewStylePopup(Sender: TObject);
121     procedure lvwLogDragOver(Sender, Source: TObject; X, Y: Integer;
122       State: TDragState; var Accept: Boolean);
123     procedure lvwLogDragDrop(Sender, Source: TObject; X, Y: Integer);
124     procedure timScrollTimerTimer(Sender: TObject);
125     procedure mnChangeTabNameClick(Sender: TObject);
126     procedure lvwLogStartDrag(Sender: TObject;
127       var DragObject: TDragObject);
128     procedure lvwLogEndDrag(Sender, Target: TObject; X, Y: Integer);
129     procedure mnTabSaveXMLLogClick(Sender: TObject);
130     procedure mnSaveHTMLClick(Sender: TObject);
131     procedure mnPopupCopyGhostClick(Sender: TObject);
132   private
133     { Private \90é\8c¾ }
134     FLastScript: String; //\83X\83N\83\8a\83v\83g\8dÄ\95`\89æ\97}\90§\97p
135     FBottleLogList: TObjectList;
136     //
137     FDragTabIndex: integer; //\83^\83u\83h\83\89\83b\83O\83h\83\8d\83b\83v\8aÖ\98A
138     FDragTabDest: integer;  //\83h\83\8d\83b\83v\82·\82é\88Ê\92u(\82·\82®\89E\82É\82­\82é\83^\83u\82Ì\83C\83\93\83f\83b\83N\83X)
139     //
140     // \83\8a\83X\83g\83r\83\85\81[\83h\83\89\83b\83O\83h\83\8d\83b\83v\8aÖ\98A
141     FLVScrollDir: TLVScrollDir; // \83X\83N\83\8d\81[\83\8b\95û\8cü
142     FLVDragDest: integer;    //\83h\83\8d\83b\83v\82·\82é\88Ê\92u(\82·\82®\89º\82É\82­\82é\83A\83C\83e\83\80\82ÌIndex)
143     //
144     procedure UpdateScript(const Script: String);
145     procedure UpdateScriptConversationColor(const Script: String);
146     procedure UpdateScriptScript(const Script: String);
147     procedure mnURLClick(Sender: TObject);
148     procedure ExtractURLs(Script: String; Result: TStrings);
149     function GetDefaultFileName(const Name: String; const Ext: String): String;
150     function BottleLogTitled(const LogName: String): TBottleLogList;
151     procedure DrawSingleLineScript(LogItem: TLogItem; Rect: TRect;
152       Item: TListItem);
153     procedure PreviewStyleChange;
154     procedure DrawListViewDragBorder(const Rect: TRect);
155     procedure DoSaveLogXML(Log: TBottleLogList);
156     procedure DoCloseTab(const Index: integer);
157     function DoSearchLog(Condition: TSearchCond): TBottleLogList;
158     procedure SearchLogIndivisual(Condition: TSearchCond;
159       LogList, Result: TBottleLogList; UntilIndex: integer = -1);
160   protected
161     procedure CreateParams(var Params: TCreateParams); override;
162   public
163     { Public \90é\8c¾ }
164     function SelectedBottleLog: TBottleLogList;
165     property BottleLogList: TObjectList read FBottleLogList;
166     procedure AddCurrentScriptLog(const LogName, Script, Channel, MID, Ghost: String);
167     procedure AddCurrentSystemLog(const LogName, MessageString: String);
168     procedure VoteLog(const MID: String; const Vote: integer);
169     procedure AgreeLog(const MID: String; const Agree: integer);
170     procedure SetBottleState(const MID: String; State: TLogState);
171     procedure AllBottleOpened;
172     procedure LogLoaded(Sender: TObject);
173     procedure LogLoadFailure(Sender: TObject; const Message: String);
174     procedure LogLoadWork(Sender: TObject);
175     procedure HTMLOutputWork(Sender: TObject; const Count: integer;
176       var Canceled: boolean);
177     procedure UpdateTab;
178     procedure UpdateWindow;
179     procedure SelAndFocusMessage(const MID: String);
180   end;
181
182   TBottleLogDragObject = class(TDragControlObjectEx)
183   private
184     FBottleLogList: TBottleLogList;
185     FLogItem: TLogItem;
186     procedure SetBottleLogList(const Value: TBottleLogList);
187     procedure SetLogItem(const Value: TLogItem);
188   protected
189     function GetDragImages: TDragImageList; override;
190   public
191     property BottleLogList: TBottleLogList read FBottleLogList write SetBottleLogList;
192     property LogItem: TLogItem read FLogItem write SetLogItem;
193   end;
194
195 var
196   frmLog: TfrmLog;
197
198 const
199   IconBottle    = 17;
200   IconOpened    = 30;
201   IconPlaying   = 31;
202   IconSystemLog = 26;
203   IconURL       = 43;
204   SubChannel    = 0;
205   SubGhost      = 1;
206   SubVotes      = 2;
207   SubAgrees     = 3;
208   SubScript     = 4;
209
210 implementation
211
212 uses MainForm;
213
214 {$R *.DFM}
215
216 { TfrmLog }
217
218 procedure TfrmLog.AddCurrentScriptLog(const LogName, Script, Channel, MID, Ghost: String);
219 var Sel: integer;
220 begin
221   BottleLogTitled(LogName).AddScriptLog(Script, Channel, MID, Ghost);
222   if SelectedBottleLog <> BottleLogTitled(LogName) then Exit;
223   lvwLog.OnChange := nil; //\83C\83x\83\93\83g\94­\90¶(\82¢\82ë\82¢\82ë\8dÄ\95`\89æ\82ª\8bN\82«\82é)\82Ì\97}\90§
224   if lvwLog.Selected <> nil then Sel := lvwLog.Selected.Index else Sel := -1;
225   lvwLog.Items.Count := SelectedBottleLog.Count;
226   UpdateWindow;
227   if Sel >= 0 then begin
228     lvwLog.Selected := lvwLog.Items[Sel + 1];
229     lvwLog.Selected.Focused := true;
230   end;
231   if not lvwLog.Focused then
232     ListView_Scroll(lvwLog.Handle, 0, High(integer));
233   lvwLog.OnChange := lvwLogChange;
234 end;
235
236 procedure TfrmLog.AddCurrentSystemLog(const LogName, MessageString: String);
237 var Sel: integer;
238 begin
239   BottleLogTitled(LogName).AddSystemLog(MessageString);
240   if SelectedBottleLog <> BottleLogTitled(LogName) then Exit;
241   lvwLog.OnChange := nil;
242   if lvwLog.Selected <> nil then Sel := lvwLog.Selected.Index else Sel := -1;
243   lvwLog.Items.Count := SelectedBottleLog.Count;
244   UpdateWindow;
245   if Sel >= 0 then begin
246     lvwLog.Selected := lvwLog.Items[Sel + 1];
247     lvwLog.Selected.Focused := true;
248   end;
249   if not lvwLog.Focused then
250     ListView_Scroll(lvwLog.Handle, 0, High(integer));
251   lvwLog.OnChange := lvwLogChange;
252 end;
253
254
255
256 procedure TfrmLog.tbtnClearClick(Sender: TObject);
257 begin
258   if SelectedBottleLog = nil then Exit;
259   DoCloseTab(tabBottleLog.TabIndex);
260 end;
261
262 procedure TfrmLog.FormCreate(Sender: TObject);
263 var i: integer;
264 begin
265   FLVDragDest := -1; // \83\8a\83X\83g\83r\83\85\81[\82Ì\83h\83\89\83b\83O\92\86\82Å\82Í\82È\82¢
266   FBottleLogList := TObjectList.Create;
267
268   SsParser.TagPattern.Assign(frmSender.SsParser.TagPattern);
269   SsParser.MetaPattern.Assign(frmSender.SsParser.MetaPattern);
270
271   with Pref.LogWindowPosition do begin
272     Self.Left   := Left;
273     Self.Top    := Top;
274     Self.Width  := Right - Left + 1;
275     Self.Height := Bottom - Top + 1;
276   end;
277   lvwLog.DoubleBuffered := true;
278   pnlPreviewArea.Height := Pref.LogWindowDividerPos;
279
280   i := 0;
281   while Token(Pref.LogWindowColumnWidth, ',', i) <> '' do begin
282     lvwLog.Columns[i].Width := StrToIntDef(Token(Pref.LogWindowColumnWidth, ',', i), 100);
283     Inc(i);
284   end;
285
286   SsParserForTalkShow.TagPattern.Assign(SsParser.TagPattern);
287   SsParserForTalkShow.MetaPattern.Assign(SsParser.MetaPattern);
288   SsParserForTalkShow.EscapeInvalidMeta := false;
289   SsParserForTalkShow.LeaveEscape := false;
290   TalkShowFrame.SsParser := self.SsParserForTalkShow;
291
292   TalkShowFrame.SetPreviewFont(edtScript.Font);
293   TalkShowFrame.PrevControl := lvwLog;
294
295   PreviewStyleChange;
296   UpdateWindow; // Reset window color and enabled status of some buttons
297 end;
298
299 procedure TfrmLog.FormDestroy(Sender: TObject);
300 var i: integer;
301     WidthStr: String;
302 begin
303   WidthStr := '';
304   for i := 0 to lvwLog.Columns.Count-1 do begin
305     if i > 0 then WidthStr := WidthStr + ',';
306     WidthStr := WidthStr + IntToStr(lvwLog.Column[i].Width);
307   end;
308   Pref.LogWindowColumnWidth := WidthStr;
309
310   with Pref.LogWindowPosition do begin
311     Left   := Self.Left;
312     Top    := Self.Top;
313     Right  := Self.Left + Self.Width - 1;
314     Bottom := Self.Top + Self.Height - 1;
315   end;
316   Pref.LogWindowDividerPos := pnlPreviewArea.Height;
317
318   FreeAndNil(FBottleLogList);
319 end;
320
321 procedure TfrmLog.lvwLogChange(Sender: TObject; Item: TListItem;
322   Change: TItemChange);
323 var Script, Text: String;
324     Log: TLogItem;
325     Selected, IsNormalBottle: boolean;
326 begin
327   Selected := false;
328   IsNormalBottle := false;
329   if SelectedBottleLog <> nil then begin
330     if Change = ctState then begin
331       Script := '';
332       if lvwLog.Selected <> nil then begin
333         Selected := true;
334         StatusBar.Panels[0].Text := Format('%d/%d\8c\8f', [lvwLog.Selected.Index+1,
335           SelectedBottleLog.Count]);
336         Log := SelectedBottleLog.Bottles[lvwLog.Selected.Index];
337         if (Log.LogType = ltBottle) and not frmSender.Connecting then begin
338           IsNormalBottle := true;
339           Script := Log.Script;
340           Text := Format('%d\83o\83C\83g/%d\95b - \83_\83u\83\8b\83N\83\8a\83b\83N\82Å\8dÄ\90¶',
341             [Length(Log.Script), frmSender.SsPlayTime.PlayTime(Log.Script) div 1000]);
342           StatusBar.Panels[1].Text := Text;
343           if Pref.LogWindowPreviewStyle = psImageConversation then
344             TalkShowFrame.View(Log)
345           else
346             UpdateScript(Script);
347         end else begin
348           StatusBar.Panels[1].Text := '';
349           UpdateScript(''); // \83\8d\83O\83v\83\8c\83r\83\85\81[\95\94\82ð\83N\83\8a\83A
350         end;
351       end else begin
352         StatusBar.Panels[0].Text := IntToStr(SelectedBottleLog.Count) + '\8c\8f';
353         StatusBar.Panels[1].Text := '';
354         UpdateScript(Script); // \83\8d\83O\83v\83\8c\83r\83\85\81[\95\94\83N\83\8a\83A
355       end;
356     end;
357     tbtnSaveLog.Enabled := lvwLog.Items.Count > 0;
358   end else begin
359     StatusBar.Panels[0].Text := '';
360     UpdateScript(''); // \83\8d\83O\83v\83\8c\83r\83\85\81[\95\94\83N\83\8a\83A
361   end;
362   frmSender.actVoteMessage.Enabled := Selected and IsNormalBottle;
363   frmSender.actAgreeMessage.Enabled := Selected and IsNormalBottle;
364   frmSender.actSendToEditor.Enabled := Selected and IsNormalBottle;
365   frmSender.actInsertCue.Enabled := Selected;
366   frmSender.actDeleteLogItem.Enabled := Selected;
367   mnPopUpCopyScript.Enabled := Selected and IsNormalBottle;
368   mnPopupCopyGhost.Enabled := Selected and IsNormalBottle;
369 end;
370
371 procedure TfrmLog.lvwLogDblClick(Sender: TObject);
372 var Script, ErrorMes: String;
373     Log, CueItem: TLogItem;
374     Res: integer;
375 begin
376   if lvwLog.Selected = nil then
377     Exit;
378   Log := SelectedBottleLog.Bottles[lvwLog.Selected.Index];
379   if Log = nil then Exit;
380   if Log.LogType <> ltBottle then
381     Exit;
382   Script := frmSender.ScriptTransForSSTP(Log.Script, ErrorMes);
383   if ErrorMes <> '' then
384   begin
385     Res := MessageDlg('\96â\91è\82Ì\82 \82é\83X\83N\83\8a\83v\83g\82Å\82·\81B\8dÄ\90\82Å\82«\82Ü\82¹\82ñ\81B'#13#10+
386         ErrorMes + #13#10 +
387         '\8b­\90§\93I\82É\8dÄ\90\82µ\82Ü\82·\82©?'#13#10,
388         mtWarning, mbOkCancel, 0
389       );
390     if Res = mrCancel then
391       Exit;
392   end;
393
394   CueItem := TLogItem.Create(Log);
395   try
396     CueItem.Script := Script;
397     frmSender.BottleSstp.Unshift(CueItem);
398   except
399     CueItem.Free;
400   end;
401 end;
402
403 procedure TfrmLog.UpdateScriptConversationColor(const Script: String);
404 var i: integer;
405     scr: String;
406     UnyuTalking, Talked, InSynchronized: boolean;
407 begin
408   scr := Script;
409   frmSender.DoTrans(scr, [toConvertURL]);
410   SsParser.LeaveEscape := false;
411   SsParser.InputString := scr;
412   UnyuTalking := false;
413   Talked := false; //'\h\u\h\u'\82Ì\82æ\82¤\82È\83X\83N\83\8a\83v\83g\82Å\8bó\82«\8ds\82ð\8dì\82ç\82È\82¢\82½\82ß\82Ì\91[\92u
414   InSynchronized := false;
415   edtScript.Text := '';
416   edtScript.Color := Pref.BgColor;
417   for i := 0 to SsParser.Count-1 do begin
418     if (SsParser[i] = '\_s') and not InSynchronized then begin
419       InSynchronized := true;
420       if Talked then begin
421         edtScript.SelText := #13#10;
422         Talked := false;
423       end;
424     end else if (SsParser[i] = '\_s') and InSynchronized then begin
425       InSynchronized := false;
426       if Talked then begin
427         edtScript.SelText := #13#10;
428         Talked := false;
429       end;
430     end;
431     if (SsParser[i] = '\u') and not UnyuTalking then begin
432       UnyuTalking := true;
433       if Talked then begin
434         edtScript.SelText := #13#10;
435         Talked := false;
436       end;
437     end;
438     if (SsParser[i] = '\h') and UnyuTalking then begin
439       UnyuTalking := false;
440       if Talked then begin
441         edtScript.SelText := #13#10;
442         Talked := false;
443       end;
444     end;
445     if SsParser.MarkUpType[i] = mtStr then begin
446       if InSynchronized then
447         edtScript.SelAttributes.Color := Pref.TalkColorS
448       else if UnyuTalking then
449         edtScript.SelAttributes.Color := Pref.TalkColorU
450       else
451         edtScript.SelAttributes.Color := Pref.TalkColorH;
452       edtScript.SelText := SsParser[i];
453       Talked := true;
454     end;
455     if SsParser.MarkUpType[i] = mtMeta then begin
456       edtScript.SelAttributes.Color := Pref.MetaWordColor;
457       edtScript.SelText := SsParser[i];
458       Talked := true;
459     end;
460   end;
461 end;
462
463 procedure TfrmLog.lvwLogKeyPress(Sender: TObject; var Key: Char);
464 begin
465   if Key = #13 then lvwLogDblClick(Sender);
466 end;
467
468 procedure TfrmLog.CreateParams(var Params: TCreateParams);
469 begin
470   inherited;
471   Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
472 end;
473
474 procedure TfrmLog.lvwLogClick(Sender: TObject);
475 begin
476   //\89E\83N\83\8a\83b\83N\82Å\83\81\83j\83\85\81[\8fo\82·\82Æ\82«\82É\94­\90\82·\82é\95s\8bï\8d\87\91Î\8dô
477   with lvwLog do
478     Selected := Selected;
479 end;
480
481 procedure TfrmLog.lvwLogColumnClick(Sender: TObject; Column: TListColumn);
482 var SortType: TBottleLogSortType;
483     SelectedMID: String;
484     SortColumn: integer;
485 begin
486   if SelectedBottleLog = nil then
487     Exit;
488   if lvwLog.Selected <> nil then
489     SelectedMID := SelectedBottleLog.Bottles[lvwLog.Selected.Index].MID
490   else
491     SelectedMID := '';
492
493   SortColumn := Column.Index;
494   case SortColumn-1 of
495     -1: SortType := stLogTime;
496     subChannel: SortType := stChannel;
497     subGhost:   SortType := stGhost;
498     subVotes:   SortType := stVote;
499     subAgrees:  SortType := stAgree;
500     subScript:  SortType := stScript;
501   else
502     SortType := stLogTime;
503   end;
504
505   SelectedBottleLog.SortBottles(SortType);
506   lvwLog.Invalidate;
507   if Length(SelectedMID) > 0 then
508     SelAndFocusMessage(SelectedMID);
509 end;
510
511
512 procedure TfrmLog.mnPopUpCopyScriptClick(Sender: TObject);
513 var
514   Log: TLogItem;
515   Clip: TClipBoard;
516 begin
517   Log := SelectedBottleLog.Bottles[frmLog.lvwLog.Selected.Index];
518   if Log = nil then Exit;
519   Clip := ClipBoard();
520   Clip.SetTextBuf(PChar(Log.Script));
521 end;
522
523 procedure TfrmLog.SetBottleState(const MID: String; State: TLogState);
524 var i: integer;
525     Bottle: TLogItem;
526 begin
527   for i := 0 to FBottleLogList.Count-1 do begin
528     Bottle := (FBottleLogList[i] as TBottleLogList).Bottle(MID);
529     if Bottle <> nil then begin
530       Bottle.State := State;
531       lvwLog.OnChange := nil;
532       lvwLog.Invalidate;
533       lvwLog.OnChange := lvwLogChange;
534     end;
535   end;
536 end;
537
538 procedure TfrmLog.mnSaveLogClick(Sender: TObject);
539 begin
540   if SelectedBottleLog = nil then Exit;
541   SaveDialog.FileName := GetDefaultFileName(SelectedBottleLog.Title, '.log');
542   SaveDialog.InitialDir := ExtractFileDir(Application.ExeName);
543   SaveDialog.DefaultExt := 'log';
544   SaveDialog.FilterIndex := 1;
545   if SaveDialog.Execute then
546     SelectedBottleLog.SaveToSstpLog(SaveDialog.FileName, false);
547 end;
548
549 procedure TfrmLog.mnSaveLogChannelClick(Sender: TObject);
550 begin
551   if SelectedBottleLog = nil then Exit;
552   SaveDialog.FileName := GetDefaultFileName(SelectedBottleLog.Title, '.log');
553   SaveDialog.InitialDir := ExtractFileDir(Application.ExeName);
554   SaveDialog.DefaultExt := 'log';
555   SaveDialog.FilterIndex := 1;
556   if SaveDialog.Execute then
557     SelectedBottleLog.SaveToSstpLog(SaveDialog.FileName, true);
558 end;
559
560 procedure TfrmLog.mnSaveLogScriptClick(Sender: TObject);
561 begin
562   if SelectedBottleLog = nil then Exit;
563   SaveDialog.FileName := GetDefaultFileName(SelectedBottleLog.Title, '.txt');
564   SaveDialog.InitialDir := ExtractFileDir(Application.ExeName);
565   SaveDialog.DefaultExt := 'txt';
566   SaveDialog.FilterIndex := 2;
567   if SaveDialog.Execute then
568     SelectedBottleLog.SaveToText(SaveDialog.FileName);
569 end;
570
571 procedure TfrmLog.mnSaveLogXMLClick(Sender: TObject);
572 begin
573   if SelectedBottleLog = nil then Exit;
574   DoSaveLogXML(SelectedBottleLog);
575 end;
576
577 procedure TfrmLog.lvwLogData(Sender: TObject; Item: TListItem);
578 var i: integer;
579     Log: TLogItem;
580 begin
581   if Item = nil then Exit;
582   i := Item.Index;
583   Log := SelectedBottleLog.Bottles[i];
584   with Item do begin
585     Caption := FormatDateTime('yy/mm/dd hh:nn:ss', Log.LogTime);
586     SubItems.Clear;
587     SubItems.Add(Log.Channel);
588     SubItems.Add(Log.Ghost);
589     if Log.LogType = ltBottle then begin
590       if Log.Votes > 0 then
591         SubItems.Add(IntToStr(Log.Votes))
592       else
593         SubItems.Add('');
594       if Log.Agrees > 0 then
595         SubItems.Add(IntToStr(Log.Agrees))
596       else
597         SubItems.Add('');
598     end else begin
599       // \83V\83X\83e\83\80\83\8d\83O\82È\82Ç\82Í\93\8a\95[\81E\93¯\88Ó\82ð\95\\8e¦\82µ\82È\82¢
600       SubItems.Add('');
601       SubItems.Add('');
602     end;
603     SubItems.Add(Log.Script);
604
605     if Log.LogType = ltBottle then begin
606       case Log.State of
607         lsUnopened: ImageIndex := IconBottle;
608         lsPlaying:  ImageIndex := IconPlaying;
609         lsOpened:   ImageIndex := IconOpened;
610       end;
611     end else
612       ImageIndex := IconSystemLog;
613   end;
614 end;
615
616 procedure TfrmLog.UpdateWindow;
617 var EnabledFlag: boolean;
618 begin
619   lvwLog.Color := Pref.BgColor;
620   lvwLog.Font.Color := Pref.TextColor;
621   if SelectedBottleLog <> nil then begin
622     Caption := '\83\8d\83O - ' + SelectedBottleLog.Title;
623     StatusBar.Panels[0].Text := IntToStr(SelectedBottleLog.Count) + '\8c\8f';
624     lvwLog.Items.Count := SelectedBottleLog.Count;
625   end else begin
626     Caption := '\83\8d\83O';
627     StatusBar.Panels[0].Text := '';
628     StatusBar.Panels[1].Text := '';
629     lvwLog.Items.Count := 0;
630   end;
631
632   EnabledFlag := SelectedBottleLog <> nil;
633   tbtnClear.Enabled := EnabledFlag;
634   tbtnSaveLog.Enabled := EnabledFlag;
635   tbtnFindBottle.Enabled := EnabledFlag;
636
637   lvwLog.Invalidate;
638 end;
639
640 procedure TfrmLog.PopupMenuListViewPopup(Sender: TObject);
641 var Log: TLogItem;
642     Child: TMenuItem;
643     Urls: TStringList;
644     i: integer;
645 begin
646   for i := mnJumpURL.Count-1 downto 0 do begin
647     mnJumpURL.Items[i].Free;
648   end;
649   mnJumpURL.Enabled := false;
650   if lvwLog.Selected = nil then Exit;
651   Log := SelectedBottleLog.Bottles[lvwLog.Selected.Index];
652   if Log = nil then Exit;
653   Urls := TStringList.Create;
654   try
655     ExtractURLs(Log.Script, Urls);
656     for i := 0 to Urls.Count-1 do begin
657       Child := TMenuItem.Create(Self);
658       with Child do begin
659         Caption := Format('(&%d) %s', [i+1, StringReplace(Urls[i], '&', '&&', [rfReplaceAll])]);
660         Tag := i;
661         OnClick := mnURLClick;
662         AutoHotkeys := maManual;
663         mnJumpURL.Add(Child);
664       end;
665     end;
666     mnJumpURL.Enabled := Urls.Count > 0;
667   finally
668     Urls.Free;
669   end;
670 end;
671
672 procedure TfrmLog.mnURLClick(Sender: TObject);
673 var LogItem: TLogItem;
674     URL: String;
675     Urls: TStringList;
676 begin
677   if (lvwLog.Selected = nil) or (SelectedBottleLog = nil) then Exit;
678   LogItem := SelectedBottleLog[lvwLog.Selected.Index] as TLogItem;
679   Urls := TStringList.Create;
680   try
681     ExtractURLs(LogItem.Script, Urls);
682     URL := Urls[(Sender as TMenuItem).Tag];
683     ShellExecute(Handle, 'open', PChar(URL), nil, nil, SW_SHOW);
684   finally
685     Urls.Free;
686   end;
687 end;
688
689 procedure TfrmLog.ExtractURLs(Script: String; Result: TStrings);
690 var i, u, j: integer;
691     s: String;
692 begin
693   Result.Clear;
694   SsParser.InputString := Script;
695   SsParser.LeaveEscape := true;
696   for i := 0 to SsParser.Count-1 do begin
697     if (SsParser.Match(SsParser[i], '\URL%b') > 0)
698     and (SsParser.MarkUpType[i] = mtTag) then
699     begin
700       for u := 7 downto 1 do begin
701         if (SsParser.Match(SsParser[i],
702             '\URL%b'+StringReplace(StringOfChar('-', u*2),
703             '-', '%b', [rfReplaceAll]))) > 0 then begin
704           for j := 1 to u do begin
705             s := SsParser.GetParam(SsParser[i], j*2);
706             if Pos('http://', s) > 0 then Result.Add(s);
707           end;
708           Break;
709         end;
710       end;
711       if SsParser.Match(SsParser[i], '\URL%b%b') = 0 then begin //\8aÈ\88Õ\94ÅURL\95Ï\8a·
712         //\8aÈ\88Õ\8c`\8e®\URL\83^\83O\95Ï\8a·
713         s := SsParser.GetParam(SsParser[i], 1);
714         if Pos('http://', s) > 0 then Result.Add(s);
715       end;
716     end;
717   end;
718 end;
719
720 procedure TfrmLog.SelAndFocusMessage(const MID: String);
721 var i: integer;
722     Log: TLogItem;
723 begin
724   for i := 0 to SelectedBottleLog.Count-1 do begin
725     Log := SelectedBottleLog.Items[i] as TLogItem;
726     if Log.MID = MID then begin
727       lvwLog.Items[i].Selected := true;
728       lvwLog.Items[i].Focused := true;
729     end;
730   end;
731 end;
732
733 procedure TfrmLog.lvwLogCustomDrawItem(Sender: TCustomListView;
734   Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
735 begin
736   //
737 end;
738
739 procedure TfrmLog.UpdateScript(const Script: String);
740 begin
741   if Script <> FLastScript then begin
742     if Pref.LogWindowPreviewStyle = psConversation then begin
743       UpdateScriptConversationColor(Script);
744     end else begin
745       UpdateScriptScript(Script);
746     end;
747     SendMessage(edtScript.Handle, EM_LINESCROLL, Low(integer), Low(integer)); //\83X\83N\83\8d\81[\83\8b\96ß\82µ
748     FLastScript := Script;
749   end;
750 end;
751
752 procedure TfrmLog.PopupMenuPreviewStylePopup(Sender: TObject);
753 var i: integer;
754 begin
755   with PopupMenuPreviewStyle do
756     for i := 0 to Items.Count-1 do
757       Items[i].Checked := Items[i].Tag = Ord(Pref.LogWindowPreviewStyle)
758 end;
759
760 procedure TfrmLog.mnPreviewStyleClick(Sender: TObject);
761 var i: integer;
762 begin
763   with PopupMenuPreviewStyle do
764     for i := 0 to Items.Count-1 do
765       Items[i].Checked := (Sender as TMenuItem).Tag = Items[i].Tag;
766   Pref.LogWindowPreviewStyle := TLogWindowPreviewStyle((Sender as TMenuItem).Tag);
767   PreviewStyleChange;
768   FLastScript := '';
769   lvwLogChange(self, lvwLog.Selected, ctState);
770 end;
771
772 procedure TfrmLog.UpdateScriptScript(const Script: String);
773 var
774   UnyuTalking, InSynchronized: boolean;
775   i: integer;
776 begin
777   edtScript.Color := Pref.BgColor;
778   SsParser.LeaveEscape := true;
779   SsParser.InputString := Script;
780   edtScript.Text := '';
781   edtScript.SelAttributes.Color := clWindowText;
782   UnyuTalking := false;
783   InSynchronized := false;
784   for i := 0 to SsParser.Count-1 do begin
785     case SsParser.MarkUpType[i] of
786       mtStr: begin
787         if InSynchronized then
788           edtScript.SelAttributes.Color := Pref.TalkColorS
789         else if UnyuTalking then
790           edtScript.SelAttributes.Color := Pref.TalkColorU
791         else
792           edtScript.SelAttributes.Color := Pref.TalkColorH;
793       end;
794       mtTag: begin
795         edtScript.SelAttributes.Color := Pref.MarkUpColor;
796         if SsParser[i] = '\h' then
797           UnyuTalking := false
798         else if SsParser[i] = '\u' then
799           UnyuTalking := true
800         else if SsParser[i] = '\_s' then
801           InSynchronized := not InSynchronized;
802       end;
803       mtMeta:   edtScript.SelAttributes.Color := Pref.MetaWordColor;
804       mtTagErr: edtScript.SelAttributes.Color := Pref.MarkErrorColor;
805     end;
806     edtScript.SelText := SsParser[i];
807     if (SsParser[i] = '\n') and (Pref.LogWindowPreviewStyle = psScriptWithLineBreak) then
808       edtScript.SelText := #13#10;
809   end;
810 end;
811
812 procedure TfrmLog.tbtnPreviewStyleClick(Sender: TObject);
813 var sel: integer;
814 begin
815   sel := Ord(Pref.LogWindowPreviewStyle);
816   sel := sel + 1;
817   if sel > Ord(High(TLogWindowPreviewStyle)) then sel := 0;
818   Pref.LogWindowPreviewStyle := TLogWindowPreviewStyle(sel);
819   FLastScript := '';
820   PreviewStyleChange;
821   lvwLogChange(self, lvwLog.Selected, ctState);
822 end;
823
824 function TfrmLog.SelectedBottleLog: TBottleLogList;
825 begin
826   if tabBottleLog.TabIndex >= 0 then
827     Result := FBottleLogList.Items[tabBottleLog.TabIndex] as TBottleLogList
828   else
829     Result := nil;
830 end;
831
832 procedure TfrmLog.tabBottleLogChange(Sender: TObject);
833 begin
834   // StatusBar\82Ì\8c\8f\90\94\95\\8e¦\82âListView.Items.Count\82ð\8dX\90V\82·\82é
835   UpdateWindow;
836   // \83A\83C\83e\83\80\82Ì\91I\91ð\8fó\91Ô\82ð\95\9c\8bA\82·\82é
837   with SelectedBottleLog do
838     if (SelectedIndex >= 0) and (Count > SelectedIndex) then
839     begin
840       lvwLog.Items[SelectedIndex].Selected := true;
841       if lvwLog.Focused then lvwLog.Selected.Focused := true;
842     end;
843   lvwLogChange(Self, nil, ctState);
844 end;
845
846 procedure TfrmLog.LogLoaded(Sender: TObject);
847 begin
848   if SelectedBottleLog = Sender then begin
849     UpdateWindow;
850   end;
851 end;
852
853 procedure TfrmLog.UpdateTab;
854 var i, cur: integer;
855 begin
856   cur := tabBottleLog.tabIndex;
857   tabBottleLog.Tabs.Clear;
858   for i := 0 to FBottleLogList.Count - 1 do begin
859     tabBottleLog.Tabs.Add((FBottleLogList[i] as TBottleLogList).Title);
860   end;
861   if FBottleLogList.Count > 0 then begin
862     if cur < FBottleLogList.Count then
863       tabBottleLog.TabIndex := cur
864     else
865       tabBottleLog.TabIndex := FBottleLogList.Count-1;
866   end;
867 end;
868
869 procedure TfrmLog.LogLoadFailure(Sender: TObject; const Message: String);
870 begin
871   Beep;
872   ShowMessage(Message);
873   if Sender = SelectedBottleLog then UpdateWindow;
874 end;
875
876 procedure TfrmLog.AgreeLog(const MID: String; const Agree: integer);
877 var i: integer;
878     flag: boolean;
879 begin
880   flag := false;
881   for i := 0 to FBottleLogList.Count - 1 do begin
882     if (FBottleLogList[i] as TBottleLogList).Bottle(MID) <> nil then begin
883       (FBottleLogList[i] as TBottleLogList).Bottle(MID).Agrees := Agree;
884       flag := true;
885     end;
886   end;
887   if flag then lvwLog.Invalidate;
888 end;
889
890 procedure TfrmLog.VoteLog(const MID: String; const Vote: integer);
891 var i: integer;
892     flag: boolean;
893 begin
894   flag := false;
895   for i := 0 to FBottleLogList.Count - 1 do begin
896     if (FBottleLogList[i] as TBottleLogList).Bottle(MID) <> nil then begin
897       (FBottleLogList[i] as TBottleLogList).Bottle(MID).Votes := Vote;
898       flag := true;
899     end;
900   end;
901   if flag then lvwLog.Invalidate;
902 end;
903
904 procedure TfrmLog.tabBottleLogChanging(Sender: TObject;
905   var AllowChange: Boolean);
906 begin
907   // \8c»\8dÝ\91I\91ð\82³\82ê\82Ä\82¢\82é\83\8d\83O\82Ì\91I\91ð\8fó\91Ô\82ð\95Û\91
908   if SelectedBottleLog = nil then Exit;
909   if lvwLog.Selected <> nil then
910     SelectedBottleLog.SelectedIndex := lvwLog.Selected.Index
911   else
912     SelectedBottleLog.SelectedIndex := -1;
913 end;
914
915 procedure TfrmLog.tabBottleLogContextPopup(Sender: TObject;
916   MousePos: TPoint; var Handled: Boolean);
917 begin
918   with tabBottleLog do begin
919     Tag := IndexOfTabAt(MousePos.X, MousePos.Y);
920     if Tag < 0 then Handled := true;
921   end;
922 end;
923
924 procedure TfrmLog.mnCloseTabClick(Sender: TObject);
925 begin
926   DoCloseTab(tabBottleLog.Tag);
927 end;
928
929 procedure TfrmLog.tbtnFindBottleClick(Sender: TObject);
930 var ResultLog: TBottleLogList;
931     Cond: TSearchCond;
932     i: integer;
933     CList, GList: THashedStringList;
934 begin
935   Application.CreateForm(TfrmSearchLog, frmSearchLog);
936   Cond := TSearchCond.Create(nil);
937   try
938     try
939       with frmSearchLog do
940       begin
941         // \8c»\8dÝ\83\8d\83O\82É\82 \82é\83S\81[\83X\83g\82Æ\83`\83\83\83\93\83l\83\8b\82Ì\83\8a\83X\83g\82ð\8eæ\93¾
942         // \8fd\82½\82¢\82©\82à??
943         CList := THashedStringList.Create;
944         GList := THashedStringList.Create;
945         try
946           for i := 0 to BottleLogList.Count-1 do
947           begin
948             with BottleLogList[i] as TBottleLogList do
949             begin
950               ExtractUniqueChannels(CList);
951               ExtractUniqueGhosts(GList);
952             end;
953           end;
954           CList.Sort;
955           GList.Sort;
956           ChannelList := CList;
957           GhostList   := GList;
958         finally
959           CList.Free;
960           GList.Free;
961         end;
962         if not Execute then
963           Exit
964         else
965           Cond.Assign(Condition);
966       end;
967     finally
968       frmSearchLog.Release;
969     end;
970     // \8c\9f\8dõ\8eÀ\8ds
971     ResultLog := DoSearchLog(Cond);
972     // \90V\83^\83u\82ð\8dì\90¬\82µ\82Ä\89æ\96Ê\8dX\90V
973     BottleLogList.Add(ResultLog);
974     UpdateTab;
975     tabBottleLog.TabIndex := BottleLogList.Count-1;
976     UpdateWindow;
977   finally
978     Cond.Free;
979   end;
980 end;
981
982 procedure TfrmLog.tbtnOpenLogClick(Sender: TObject);
983 var BottleLog: TBottleLogList;
984     i, Index: integer;
985 begin
986   Index := -1;
987   if OpenDialog.Execute then begin
988     for i := 0 to OpenDialog.Files.Count-1 do begin
989       BottleLog := TBottleLogList.Create(ExtractFileName(OpenDialog.Files[i]));
990       try
991         with BottleLog do
992         begin
993           OnLoaded := LogLoaded;
994           OnLoadFailure := LogLoadFailure;
995           OnLoadWork := LogLoadWork;
996           BottleLog.LoadFromXMLFile(OpenDialog.Files[i]);
997         end;
998         Index := BottleLogList.Add(BottleLog); // \8dÅ\8cã\82É\8aJ\82¢\82½\83\8d\83O\82Ì\88Ê\92u\82ð\8bL\89¯
999       except
1000         BottleLog.Free;
1001       end;
1002     end;
1003     UpdateTab;
1004     if Index >= 0 then tabBottleLog.TabIndex := Index;
1005     UpdateWindow;
1006   end;
1007 end;
1008
1009 function TfrmLog.GetDefaultFileName(const Name, Ext: String): String;
1010 begin
1011   Result := StringReplace(Name, '/', '', [rfReplaceAll]);
1012   Result := StringReplace(Result, ' ', '', [rfReplaceAll]);
1013   Result := SafeFileName(Result);
1014   Result := ChangeFileExt(Result, Ext);
1015 end;
1016
1017 function TfrmLog.BottleLogTitled(const LogName: String): TBottleLogList;
1018 var i: integer;
1019 begin
1020   for i := 0 to FBottleLogList.Count-1 do begin
1021     if (FBottleLogList[i] as TBottleLogList).Title = LogName then begin
1022       Result := (FBottleLogList[i] as TBottleLogList);
1023       Exit;
1024     end;
1025   end;
1026   // \8c©\82Â\82©\82ç\82È\82¢\8fê\8d\87
1027   Result := TBottleLogList.Create(LogName); // \90V\82µ\82­\8dì\82é
1028   FBottleLogList.Add(Result);
1029   UpdateTab;
1030   if FBottleLogList.Count = 1 then tabBottleLog.TabIndex := 0;
1031 end;
1032
1033 procedure TfrmLog.AllBottleOpened;
1034 var i, j: integer;
1035     Log: TBottleLogList;
1036 begin
1037   for i := 0 to FBottleLogList.Count-1 do begin
1038     Log  := FBottleLogList[i] as TBottleLogList;
1039     for j := 0 to Log.Count-1 do begin
1040       Log.Bottles[j].State := lsOpened;
1041     end;
1042   end;
1043 end;
1044
1045 procedure TfrmLog.tabBottleLogMouseDown(Sender: TObject;
1046   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
1047 var Index: integer;
1048 begin
1049   with tabBottleLog do begin
1050     Index := IndexOfTabAt(X, Y);
1051     if Index = -1 then Exit; //\83^\83u\82ª\82È\82¢\82Ì\82Å\83h\83\89\83b\83O\82Å\82«\82È\82¢
1052     if Button = mbLeft then begin
1053       FDragTabIndex := Index; //\83h\83\89\83b\83O\82·\82é\83^\83u\82Ì\83C\83\93\83f\83b\83N\83X\82ð\95Û\91
1054       BeginDrag(False);
1055       FDragTabDest := -1;     //\83h\83\89\83b\83O\98g\90ü\95`\89æ\83t\83\89\83O\83N\83\8a\83A\82Ì\82½\82ß
1056     end;
1057   end;
1058 end;
1059
1060 procedure TfrmLog.tabBottleLogDragOver(Sender, Source: TObject; X,
1061   Y: Integer; State: TDragState; var Accept: Boolean);
1062 var TargetRect: TRect;
1063     OldDest, Index: integer;
1064     dummy: boolean;
1065 begin
1066   // \83^\83u\82Ì\83h\83\89\83b\83O(\83^\83u\82Ì\8f\87\94Ô\93ü\82ê\91Ö\82¦)\82Ü\82½\82Í\81A
1067   // \83\8d\83O\83A\83C\83e\83\80\82Ì\83h\83\89\83b\83O(\83\8d\83O\82ð\95Ê\82Ì\83^\83u\82É\88Ú\93®)\82Ì
1068   // \97¼\95û\82Ì\83h\83\89\83b\83O\82ð\8eó\82¯\95t\82¯\82é
1069   Accept := false;
1070   if Source = tabBottleLog then
1071   begin
1072     // \83^\83u\82Ì\8f\87\94Ô\93ü\82ê\91Ö\82¦\82Ì\8fê\8d\87
1073     Accept := true;
1074     with tabBottleLog do begin
1075       OldDest := FDragTabDest;
1076       FDragTabDest := IndexOfTabAt(X, Y);
1077       if FDragTabDest = -1 then begin
1078         Accept := false; //\82±\82Ì\8fê\8d\87\82Í\83h\83\8d\83b\83v\82ð\94F\82ß\82È\82¢
1079         Exit;
1080       end;
1081       with Canvas do begin
1082         Pen.Mode := pmNot;
1083         Pen.Width := 3;
1084       end;
1085       if (OldDest <> FDragTabDest) and (OldDest >= 0) then begin
1086         //\88È\91O\82Ì\98g\90ü\8fÁ\8b\8e
1087         TargetRect := TabRect(OldDest);
1088         with Canvas do begin
1089           Brush.Style := bsClear;
1090           Rectangle(TargetRect.Left, TargetRect.Top,
1091                     TargetRect.Right, TargetRect.Bottom);
1092         end;
1093       end;
1094       if (OldDest <> FDragTabDest) then begin
1095         //\90V\82µ\82¢\98g\90ü\95`\89æ
1096         TargetRect := TabRect(FDragTabDest);
1097         with Canvas do begin
1098           Brush.Style := bsClear;
1099           Rectangle(TargetRect.Left, TargetRect.Top,
1100                     TargetRect.Right, TargetRect.Bottom);
1101         end;
1102       end;
1103     end;
1104   end else if Source is TBottleLogDragObject then
1105   begin
1106     // \83\8d\83O\8d\80\96Ú\82Ì\83h\83\89\83b\83O(\83\8d\83O\82ð\95Ê\82Ì\83^\83u\82É\88Ú\93®\82·\82é)\82Ì\8fê\8d\87
1107     Index := tabBottleLog.IndexOfTabAt(X, Y);
1108     if tabBottleLog.TabIndex <> Index then
1109     begin
1110       FLVDragDest := -1; // \98g\90ü\82Í\82Ü\82¾\95\\8e¦\82³\82ê\82È\82¢\82Í\82¸
1111       // \83^\83u\82ð\90Ø\91Ö\82¦\82é
1112       tabBottleLogChanging(Self, dummy);
1113       tabBottleLog.TabIndex := Index;
1114       UpdateWindow;
1115     end;
1116   end;
1117 end;
1118
1119 procedure TfrmLog.tabBottleLogDragDrop(Sender, Source: TObject; X,
1120   Y: Integer);
1121 var DestIndex: integer;
1122 begin
1123   with tabBottleLog do begin
1124     DestIndex := IndexOfTabAt(X, Y);
1125     Tabs.Move(FDragTabIndex, DestIndex);
1126     FBottleLogList.Move(FDragTabIndex, DestIndex);
1127   end;
1128 end;
1129
1130 procedure TfrmLog.tabBottleLogEndDrag(Sender, Target: TObject; X,
1131   Y: Integer);
1132 begin
1133   //\8b­\90§\93I\82É\83^\83u\82ð\8dÄ\95`\89æ\82³\82¹\82é\81B\98g\90ü\8fÁ\82µ\91Î\8dô
1134   tabBottleLog.Tabs.BeginUpdate;
1135   tabBottleLog.Tabs.EndUpdate;
1136 end;
1137
1138 procedure TfrmLog.LogLoadWork(Sender: TObject);
1139 begin
1140   if Sender = SelectedBottleLog then
1141   begin
1142     lvwLog.Invalidate;
1143     lvwLog.Items.Count := SelectedBottleLog.Count;
1144   end;
1145 end;
1146
1147 procedure TfrmLog.lvwLogDrawItem(Sender: TCustomListView; Item: TListItem;
1148   Rect: TRect; State: TOwnerDrawState);
1149 var
1150   DestRect: TRect;
1151   Script: String;
1152   Ico: TIcon;
1153   sub, Ex: integer;
1154   Bottle: TLogItem;
1155   DummyStr: TStringList;
1156 begin
1157   Bottle := SelectedBottleLog.Bottles[Item.Index];
1158   if Bottle.HasURL = huUndefined then
1159   begin
1160     DummyStr := TStringList.Create;
1161     try
1162       ExtractURLs(Bottle.Script, DummyStr);
1163       if DummyStr.Count > 0 then
1164         Bottle.HasURL := huYes
1165       else
1166         Bottle.HasURL := huNo;
1167     finally
1168       DummyStr.Free;
1169     end;
1170   end;
1171
1172   // \94w\8ci\8fÁ\8b\8e
1173   ListView_GetItemRect(lvwLog.Handle, Item.Index, DestRect, LVIR_BOUNDS);
1174
1175   // \94w\8ci\82Ì\90F\82Í\91I\91ð\8fó\91Ô\81E\91I\91ð\94ñ\83A\83N\83e\83B\83u\8fó\91Ô\81E\92Ê\8fí\8fó\91Ô\82Ì3\92Ê\82è
1176   lvwLog.Canvas.Brush.Style := bsSolid;
1177   if Item.Selected then begin
1178     if lvwLog.Focused then
1179       lvwLog.Canvas.Brush.Color := clHighlight
1180     else
1181       lvwLog.Canvas.Brush.Color := clBtnFace;
1182   end else begin
1183     lvwLog.Canvas.Brush.Color := Pref.BgColor;
1184   end;
1185   lvwLog.Canvas.FillRect(DestRect);
1186   lvwLog.Canvas.Brush.Style := bsClear;
1187   // \83t\83H\81[\83J\83X\82ª\82 \82é\8fê\8d\87\82É\82Í\83t\83H\81[\83J\83X\82Ì\98g\90ü\82ð\88ø\82­
1188   if Item.Focused and lvwLog.Focused then
1189     lvwLog.Canvas.DrawFocusRect(DestRect);
1190
1191   // \83h\83\89\83b\83O\92\86\82È\82ç\98g\90ü\82ð\95`\89æ\82·\82é
1192   if FLVDragDest = Item.Index then
1193   begin
1194     DestRect := Item.DisplayRect(drBounds);
1195     DrawListViewDragBorder(DestRect);
1196   end;
1197
1198   if Item.Selected then
1199   begin
1200     if lvwLog.Focused then
1201       lvwLog.Canvas.Font.Color := clHighlightText
1202     else
1203       lvwLog.Canvas.Font.Color := clWindowText;
1204   end else
1205   lvwLog.Canvas.Font.Color := Pref.TextColor;
1206   lvwLog.Canvas.Refresh;
1207
1208   // \83L\83\83\83v\83V\83\87\83\93(\93ú\95t)
1209   ListView_GetItemRect(lvwLog.Handle, Item.Index, DestRect, LVIR_LABEL);
1210   Inc(DestRect.Left, 2);
1211   Inc(DestRect.Top, 2);
1212   Dec(DestRect.Right, 2);
1213   DrawTextEx(lvwLog.Canvas.Handle, PChar(Item.Caption), -1, DestRect,
1214     DT_SINGLELINE or DT_END_ELLIPSIS, nil);
1215   ListView_GetItemRect(lvwLog.Handle, Item.Index, DestRect, LVIR_ICON);
1216   Ico := TIcon.Create;
1217   try
1218     lvwLog.SmallImages.GetIcon(Item.ImageIndex, Ico);
1219     lvwLog.Canvas.Draw(DestRect.Left, DestRect.Top, Ico);
1220   finally
1221     Ico.Free;
1222   end;
1223   // \83L\83\83\83v\83V\83\87\83\93\82Å\82à\83X\83N\83\8a\83v\83g\82Å\82à\82È\82¢\82à\82Ì
1224   for sub := 0 to Item.SubItems.Count-1 do
1225   begin
1226     if sub = SubScript then Continue;
1227     ListView_GetSubItemRect(lvwLog.Handle, Item.Index, sub + 1,
1228       LVIR_BOUNDS, @DestRect);
1229     if DestRect.Right - DestRect.Left <= 16 then
1230     begin
1231       // \8b·\82·\82¬\82é\8fê\8d\87\82Í\95\8e\9a\97ñ\82ð\95`\89æ\82µ\82È\82¢\81B
1232       // 16\82Æ\82¢\82¤\90\94\8e\9a\82Í\8eÀ\91ª\92l\81B\89½\82©\82Ì\83o\83O\82Á\82Û
1233       lvwLog.Canvas.FillRect(DestRect);
1234       Continue;
1235     end;
1236     Inc(DestRect.Left, 2);
1237     Inc(DestRect.Top, 2);
1238     Dec(DestRect.Right, 2);
1239     Ex := DT_NOPREFIX or DT_SINGLELINE or DT_END_ELLIPSIS;
1240     if lvwLog.Columns[sub+1].Alignment = taRightJustify then
1241       Ex := Ex or DT_RIGHT;
1242     DrawTextEx(lvwLog.Canvas.Handle, PChar(Item.SubItems[sub]), -1, DestRect,
1243       Ex, nil);
1244   end;
1245   // \83X\83N\83\8a\83v\83g
1246   ListView_GetSubItemRect(lvwLog.Handle, Item.Index, SubScript + 1,
1247     LVIR_BOUNDS, @DestRect);
1248   Script := Item.SubItems[SubScript];
1249   DrawSingleLineScript(Bottle, DestRect, Item);
1250
1251 end;
1252
1253 procedure TfrmLog.DrawSingleLineScript(LogItem: TLogItem;
1254   Rect: TRect; Item: TListItem);
1255 var
1256   i, x, w: integer;
1257   UnyuTalking, Synchronized, Spaced: boolean;
1258   Mark: TSsMarkUpType;
1259   Script: String;
1260   Ico: TIcon;
1261   procedure ScopeChange;
1262   begin
1263     if (not Spaced) and (Pref.LogListPreviewStyle = psTagStripped) then
1264     begin
1265       Inc(x, 7);
1266       Spaced := true;
1267     end;
1268   end;
1269 begin
1270   Script := LogItem.Script;
1271   x := 3;
1272
1273   if LogItem.HasURL = huYes then
1274   begin
1275     Ico := TIcon.Create;
1276     try
1277       lvwLog.SmallImages.GetIcon(IconURL, Ico);
1278       lvwLog.Canvas.Draw(Rect.Left + x, Rect.Top, Ico);
1279       Inc(x, 20);
1280     finally
1281       Ico.Free;
1282     end;
1283   end;
1284
1285   if Pref.LogListPreviewStyle = psNoColor then
1286   begin
1287     Inc(Rect.Left, x);
1288     Inc(Rect.Top, 2);
1289     Dec(Rect.Right, 2);
1290     DrawTextEx(lvwLog.Canvas.Handle, PChar(Script), -1, Rect,
1291       DT_SINGLELINE or DT_END_ELLIPSIS or DT_NOPREFIX, nil);
1292     Exit;
1293   end;
1294
1295   SsParser.LeaveEscape := Pref.LogListPreviewStyle = psNormal;
1296   SsParser.InputString := Script;
1297
1298   UnyuTalking := false;
1299   Synchronized := false;
1300   Spaced := true; // \83^\83O\8fÈ\97ª\95\\8e¦\8e\9e\82É\95s\95K\97v\82É\83X\83R\81[\83v\95Ï\8a·\8e\9e\82Ì\83X\83y\81[\83X\82ð\8bó\82¯\82È\82¢
1301                   // \82½\82ß\82Ì\83t\83\89\83O
1302   for i := 0 to SsParser.Count - 1 do begin
1303     if SsParser[i] = '\h' then
1304     begin
1305       UnyuTalking := false;
1306       ScopeChange;
1307     end else if SsParser[i] = '\u' then
1308     begin
1309       UnyuTalking := true;
1310       ScopeChange;
1311     end else if SsParser[i] = '\_s' then
1312     begin
1313       Synchronized := not Synchronized;
1314       ScopeChange;
1315     end else if (Pos('\n', SsParser[i]) = 1) or (SsParser[i] = '\c') then
1316     begin
1317       ScopeChange;
1318     end;
1319     Mark := SsParser.MarkUpType[i];
1320     case Mark of
1321       mtMeta:
1322         begin
1323           lvwLog.Canvas.Font.Color := Pref.MetaWordColor;
1324           Spaced := false;
1325         end;
1326       mtTag:
1327         if Pref.LogListPreviewStyle = psNormal then
1328           lvwLog.Canvas.Font.Color := Pref.MarkUpColor
1329         else
1330         begin
1331           Continue;
1332         end;
1333       mtTagErr:
1334         lvwLog.Canvas.Font.Color := Pref.MarkErrorColor;
1335       else begin
1336         Spaced := false;
1337         if Synchronized then
1338           lvwLog.Canvas.Font.Color := Pref.TalkColorS
1339         else if UnyuTalking then
1340           lvwLog.Canvas.Font.Color := Pref.TalkColorU
1341         else
1342           lvwLog.Canvas.Font.Color := Pref.TalkColorH;
1343       end;
1344     end;
1345     if Item.Selected then
1346     begin
1347       if lvwLog.Focused then
1348         lvwLog.Canvas.Font.Color := clHighlightText
1349       else
1350         lvwLog.Canvas.Font.Color := clWindowText;
1351     end;
1352     lvwLog.Canvas.Refresh;
1353     w := lvwLog.Canvas.TextWidth(SsParser[i]);
1354     lvwLog.Canvas.TextRect(Rect, Rect.Left + x, Rect.Top + 2, SsParser[i]);
1355     x := x + w;
1356     if Rect.Right - Rect.Left < x then Break;
1357   end;
1358 end;
1359
1360 procedure TfrmLog.mnListPreviewStyleClick(Sender: TObject);
1361 var i: integer;
1362 begin
1363   with PopupMenuListPreviewStyle do
1364     for i := 0 to Items.Count-1 do
1365       Items[i].Checked := (Sender as TMenuItem).Tag = Items[i].Tag;
1366   Pref.LogListPreviewStyle := TLogListPreviewStyle((Sender as TMenuItem).Tag);
1367   lvwLog.Invalidate;
1368 end;
1369
1370 procedure TfrmLog.tbtnListPreviewStyleClick(Sender: TObject);
1371 var sel: integer;
1372 begin
1373   sel := Ord(Pref.LogListPreviewStyle);
1374   sel := sel + 1;
1375   if sel > Ord(High(TLogListPreviewStyle)) then sel := 0;
1376   Pref.LogListPreviewStyle := TLogListPreviewStyle(sel);
1377   lvwLog.Invalidate;
1378 end;
1379
1380 procedure TfrmLog.PopupMenuListPreviewStylePopup(Sender: TObject);
1381 var i: integer;
1382 begin
1383   with PopupMenuListPreviewStyle do
1384     for i := 0 to Items.Count-1 do
1385       Items[i].Checked := Items[i].Tag = Ord(Pref.LogListPreviewStyle)
1386 end;
1387
1388 procedure TfrmLog.PreviewStyleChange;
1389 begin
1390   if Pref.LogWindowPreviewStyle = psImageConversation then
1391   begin
1392     if Spps.Count = 0 then
1393       ShowMessage('\83T\81[\83t\83B\83X\83v\83\8c\83r\83\85\81[\97p\83v\83\89\83O\83C\83\93\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ\81B');
1394     edtScript.Visible := false;
1395     TalkShowFrame.Visible := true;
1396   end else
1397   begin
1398     edtScript.Visible := true;
1399     TalkShowFrame.Visible := false;
1400   end;
1401 end;
1402
1403 procedure TfrmLog.lvwLogDragOver(Sender, Source: TObject; X, Y: Integer;
1404   State: TDragState; var Accept: Boolean);
1405 var
1406   Target: TListItem;
1407   OldDest: integer;
1408   Rec: TRect; // \83_\83~\81[\81B
1409 begin
1410   Accept := False;
1411   // \82Æ\82è\82 \82¦\82¸\8eó\82¯\95t\82¯\82é\89Â\94\\90«\82ª\82 \82é\82Ì\82ÍTBottleLogDragObject\82¾\82¯
1412   if not (Source is TBottleLogDragObject) then
1413     Exit;
1414
1415   Target := lvwLog.GetItemAt(X, Y);
1416
1417   // \82±\82ê\88È\91O\82É\95`\89æ\82³\82ê\82Ä\82¢\82½\98g\82Ì\83C\83\93\83f\83b\83N\83X
1418   OldDest := FLVDragDest;
1419
1420   // \83h\83\8d\83b\83v\88Ê\92u\82É Item \82ª\82 \82ê\82Î\83h\83\8d\83b\83v\82ð\8b\96\89Â\82·\82é
1421   if Target <> nil then
1422   begin
1423     Accept := true;
1424     FLVDragDest := Target.Index;
1425   end else
1426   begin
1427     Accept := true;
1428     FLVDragDest := -1;
1429   end;
1430
1431   // \88È\91O\82Ì\98g\90ü\82ð\8dí\8f\9c
1432   if (OldDest > -1) and (FLVDragDest <> OldDest) then
1433   begin
1434     Rec := lvwLog.Items[OldDest].DisplayRect(drBounds);
1435     DrawListViewDragBorder(Rec);
1436   end;
1437   // \83h\83\89\83b\83O\90æ\82Ì\98g\90ü\82ð\95`\89æ
1438   if (Target <> nil) and (FLVDragDest <> OldDest) then
1439   begin
1440     Rec := Target.DisplayRect(drBounds);
1441     DrawListViewDragBorder(Rec);
1442   end;
1443
1444   // \83X\83N\83\8d\81[\83\8b\8aÖ\8cW
1445   if lvwLog.Items.Count > 0 then
1446   begin
1447     if (lvwLog.topItem <> nil) and (Y - lvwLog.TopItem.Top < 10) then
1448     begin
1449       FLVScrollDir := lvScrollDown;
1450       if not timScrollTimer.Enabled then
1451         timScrollTimer.Enabled := true;
1452     end else if (lvwLog.Height - Y) < 10 then
1453     begin
1454       FLVScrollDir := lvScrollUp;
1455       if not timScrollTimer.Enabled then
1456         timScrollTimer.Enabled := true;
1457     end
1458     else
1459       timScrollTimer.Enabled := false;
1460   end else
1461     timScrollTimer.Enabled := false;
1462 end;
1463
1464 procedure TfrmLog.lvwLogDragDrop(Sender, Source: TObject; X, Y: Integer);
1465 var
1466   TargetItem: integer;
1467   Src: TBottleLogDragObject;
1468   SrcLog: TObject;
1469 begin
1470   timScrollTimer.Enabled := false;
1471
1472   if not (Source is TBottleLogDragObject) then
1473     Exit;
1474   Src := Source as TBottleLogDragObject;
1475
1476   if lvwLog.GetItemAt(X, Y) <> nil then
1477     TargetItem := lvwLog.GetItemAt(X, Y).Index
1478   else
1479     TargetItem := -1;
1480
1481   lvwLog.Items.BeginUpdate; // \83h\83\8d\83b\83v\92\86\82Í\95\\8e¦\82ð\97}\8e~\82·\82é\81@\8fd\97v\81I
1482   try
1483     // \83h\83\8d\83b\83v\88Ê\92u\82É Item \82ð\88Ú\93®\82·\82é
1484     if (GetAsyncKeyState(VK_CONTROL) and $8000) > 0 then
1485     begin // \83R\83s\81[\88Ú\93®\82Ì\8fê\8d\87
1486       SrcLog := TLogItem.Create(Src.LogItem);
1487     end else // \88Ú\93®\82¾\82¯\82·\82é\8fê\8d\87
1488     begin
1489       SrcLog := Src.BottleLogList.Extract(Src.LogItem);
1490     end;
1491     if TargetItem >= 0 then
1492     begin
1493       // \82·\82Å\82É\91\8dÝ\82·\82é\83A\83C\83e\83\80\82Ì\8fã\82É\83h\83\8d\83b\83v\82µ\82½\8fê\8d\87
1494       SelectedBottleLog.Insert(TargetItem, SrcLog);
1495     end else
1496     begin
1497       // ListView\82Ì\97]\94\92\82É\83h\83\8d\83b\83v\82µ\82½\8fê\8d\87(Insert\82Å\82«\82È\82¢)
1498       TargetItem := SelectedBottleLog.Add(SrcLog);
1499     end;
1500     lvwLog.Items[TargetItem].Selected := true;
1501     lvwLog.Items[TargetItem].Focused := true;
1502   finally
1503     lvwLog.Items.EndUpdate;
1504     UpdateWindow;
1505   end;
1506 end;
1507
1508 procedure TfrmLog.timScrollTimerTimer(Sender: TObject);
1509 var
1510   ScrollHeight: Integer;
1511 begin
1512   // \83X\83N\83\8d\81[\83\8b\97Ê\82ð\8b\81\82ß\82é
1513   ScrollHeight := 0;
1514   if lvwLog.Items.Count > 2 then
1515   begin
1516     ScrollHeight := lvwLog.Items[1].Top - lvwLog.Items[0].Top;
1517   end;
1518
1519   case FLVScrollDir of
1520     lvScrollUp: lvwLog.Scroll(0, ScrollHeight);
1521     lvSCrollDown: lvwLog.Scroll(0, -ScrollHeight);
1522   end;
1523   lvwLog.Invalidate;  // \8dÅ\90V\82Ì\8fó\91Ô\82É\8dÄ\95`\89æ\82·\82é
1524   lvwLog.Update;
1525 end;
1526
1527 procedure TfrmLog.mnChangeTabNameClick(Sender: TObject);
1528 var Name: String;
1529 begin
1530   Name := (FBottleLogList[tabBottleLog.Tag] as TBottleLogList).Title;
1531   InputQuery('\96¼\91O\82Ì\95Ï\8dX', '\90V\82µ\82¢\83^\83u\82Ì\96¼\91O', Name);
1532   (FBottleLogList[tabBottleLog.Tag] as TBottleLogList).Title := Name;
1533   UpdateTab;
1534 end;
1535
1536 procedure TfrmLog.lvwLogStartDrag(Sender: TObject;
1537   var DragObject: TDragObject);
1538 var Drag: TBottleLogDragObject;
1539 begin
1540   // \92Ê\8fí\82ÌListView\97p\82Ì\83h\83\89\83b\83O\83I\83u\83W\83F\83N\83g\82Í
1541   // OS\82É\82æ\82Á\82Ä\82Í\81A\88Ú\93®\82·\82é\82Æ\82«\82É\83A\83C\83e\83\80\82Ì\83C\83\81\81[\83W\82ð\94¼\93§\96¾\82Å\95`\89æ\82µ\82Ä\82µ\82Ü\82¤\81B
1542   // TDragObject\82©\82ç\92¼\90Ú\8cp\8f³\82µ\82½\82¾\82¯\82Ì\82à\82Ì(\83C\83\81\81[\83W\82ð\8e\9d\82Á\82Ä\82¢\82È\82¢)\82ð\8eg\82¤\82Æ
1543   // \94¼\93§\96¾\83C\83\81\81[\83W\82Ì\95`\89æ\82Í\97}\90§\82Å\82«\82é\81B
1544   Drag := TBottleLogDragObject.Create(lvwLog);
1545   Drag.BottleLogList := SelectedBottleLog;
1546   Drag.LogItem := SelectedBottleLog.Bottles[lvwLog.Selected.Index];
1547   DragObject := Drag;
1548 end;
1549
1550 procedure TfrmLog.lvwLogEndDrag(Sender, Target: TObject; X, Y: Integer);
1551 begin
1552   // \98g\90ü\8fÁ\82µ\97p\82É\8b­\90§\93I\82É\8dÄ\95`\89æ\82³\82¹\82é
1553   timScrollTimer.Enabled := false;
1554   FLVDragDest := -1;
1555   UpdateWindow;
1556 end;
1557
1558 procedure TfrmLog.DrawListViewDragBorder(const Rect: TRect);
1559 var Rec: TRect;
1560 begin
1561   Rec := Rect;
1562   InflateRect(Rec, -1, -1);
1563   with lvwLog.Canvas do
1564   begin
1565     Pen.Mode := pmNot;
1566     Pen.Width := 3;
1567     Brush.Style := bsClear;
1568     Refresh; // \95K\97v
1569     Rectangle(Rec);
1570   end;
1571 end;
1572
1573 procedure TfrmLog.DoSaveLogXML(Log: TBottleLogList);
1574 begin
1575   SaveDialog.FileName := GetDefaultFileName(Log.Title, '.xml');
1576   SaveDialog.InitialDir := ExtractFileDir(Application.ExeName);
1577   SaveDialog.DefaultExt := 'xml';
1578   SaveDialog.FilterIndex := 3;
1579   if SaveDialog.Execute then
1580     Log.SaveToXmlFile(SaveDialog.FileName);
1581 end;
1582
1583 procedure TfrmLog.DoCloseTab(const Index: integer);
1584 var
1585   Confirm: String;
1586   PrevSelection: TBottleLogList; // \95Â\82\82½\82Æ\82«\83^\83u\82ª\82¸\82ê\82È\82¢\82æ\82¤\82É\82·\82é\8f\88\97\9d\97p
1587   i: integer;
1588 begin
1589   if Pref.ConfirmOnTabClose then
1590   begin
1591     Confirm := Format('\83^\83u"%s"\82ð\95Â\82\82Ü\82·\82©?', [(FBottleLogList[Index] as TBottleLogList).Title]);
1592     if MessageDlg(Confirm, mtConfirmation, mbOkCancel, 0) = mrCancel then
1593       Exit;
1594   end;
1595   PrevSelection := SelectedBottleLog;
1596   FBottleLogList.Delete(Index);
1597   UpdateTab;
1598   // \83^\83u\82¸\82ê\96h\8e~\8f\88\97\9d
1599   for i := 0 to FBottleLogList.Count-1 do
1600     if FBottleLogList[i] = PrevSelection then
1601       tabBottleLog.TabIndex := i;
1602   UpdateWindow;
1603   lvwLogChange(Self, nil, ctState);
1604 end;
1605
1606 procedure TfrmLog.HTMLOutputWork(Sender: TObject; const Count: integer;
1607   var Canceled: boolean);
1608 begin
1609   frmHTMLOutputProgress.ProgressBar.Position := Count;
1610   Application.ProcessMessages;
1611   if frmHTMLOutputProgress.Canceled then
1612     Canceled := true;
1613 end;
1614
1615 function TfrmLog.DoSearchLog(Condition: TSearchCond): TBottleLogList;
1616 var i, UntilIndex: integer;
1617 begin
1618   Result := TBottleLogList.Create('\8c\9f\8dõ\8c\8b\89Ê');
1619   if Condition.SearchLogRange in [srSelectedLogList, srAboveSelectedLog] then
1620   begin
1621     if SelectedBottleLog = nil then
1622     begin
1623       ShowMessage('\8c\9f\8dõ\91Î\8fÛ\82ª\82 \82è\82Ü\82¹\82ñ');
1624       Result.Free;
1625       Result := nil;
1626       Exit;
1627     end else
1628     begin
1629       if Condition.SearchLogRange = srSelectedLogList then
1630         UntilIndex := -1
1631       else if lvwLog.Selected = nil then
1632         UntilIndex := -1
1633       else
1634         UntilIndex := lvwLog.Selected.Index;
1635       SearchLogIndivisual(Condition, SelectedBottleLog, Result, UntilIndex);
1636     end;
1637   end else if Condition.SearchLogRange = srAllLogLists then
1638   begin
1639     for i := 0 to BottleLogList.Count-1 do
1640     begin
1641       SearchLogIndivisual(Condition, BottleLogList[i] as TBottleLogList,
1642         Result);
1643     end;
1644   end;
1645 end;
1646
1647 procedure TfrmLog.SearchLogIndivisual(Condition: TSearchCond; LogList,
1648   Result: TBottleLogList; UntilIndex: integer = -1);
1649 var
1650   i, Max: integer;
1651   Bottle, New: TLogItem;
1652   Ok: boolean;
1653 begin
1654   // 1\8cÂ\82Ì\83\8d\83O\83^\83u\82É\91Î\82µ\82Ä\8c\9f\8dõ\82ð\82©\82¯\82é\81BUntilIndex\82Å\94Í\88Í\8ew\92è(\8fÈ\97ª\8e\9e\82»\82Ì\83^\83u\91S\91Ì)
1655   if UntilIndex >= 0 then
1656     Max := UntilIndex
1657   else
1658     Max := LogList.Count-1;
1659   for i := 0 to Max do
1660   begin
1661     // \8fð\8c\8f\94»\92è
1662     Bottle := LogList.Bottles[i];
1663     if Bottle.LogType <> ltBottle then
1664       Continue;
1665     Ok := true;
1666     // \83X\83N\83\8a\83v\83g\83p\83^\81[\83\93\82Å\89ð\90Í
1667     if Condition.ScriptPattern <> '' then
1668     begin
1669       if Condition.ScriptRegExp then
1670       begin
1671         try
1672           if not RegExp.Match(Condition.ScriptPattern, Bottle.Script) then
1673             Ok := false;
1674         except
1675           on EBRegExpError do
1676             Ok := false; //\96­\82È\90³\8bK\95\\8c»\82ð\8fR\82é
1677         end;
1678       end else
1679       begin
1680         if not AnsiContainsText(Bottle.Script, Condition.ScriptPattern) then
1681           Ok := false;
1682       end;
1683     end;
1684     // \83`\83\83\83\93\83l\83\8b\96¼\81A\83S\81[\83X\83g\96¼\81A\93\8a\95[\93¯\88Ó
1685     if Condition.Channel <> '' then
1686       if not AnsiContainsText(Bottle.Channel, Condition.Channel) then
1687         Ok := false;
1688     if Condition.Ghost <> '' then
1689       if not AnsiContainsText(Bottle.Ghost, Condition.Ghost) then
1690         Ok := false;
1691     if Condition.MinVote > Bottle.Votes then
1692       Ok := false;
1693     if Condition.MinAgree > Bottle.Agrees then
1694       Ok := false;
1695     // \8fð\8c\8f\82É\88ê\92v\82µ\82½\82à\82Ì\82ð\8c\8b\89Ê\83\8a\83X\83g\82É\92Ç\89Á
1696     if Ok then
1697     begin
1698       New := TLogItem.Create(Bottle); // \83R\83s\81[\83R\83\93\83X\83g\83\89\83N\83^
1699       New.State := lsOpened;
1700       Result.Add(New);
1701     end;
1702   end;
1703 end;
1704
1705 { TBottleLogDragObject }
1706
1707 function TBottleLogDragObject.GetDragImages: TDragImageList;
1708 begin
1709   // \92\86\93r\94¼\92[\82È\83h\83\89\83b\83O\83C\83\81\81[\83W\82ð\95\\8e¦\82µ\82È\82¢\82æ\82¤\82É\82·\82é
1710   Result := nil;
1711 end;
1712
1713 procedure TBottleLogDragObject.SetBottleLogList(
1714   const Value: TBottleLogList);
1715 begin
1716   FBottleLogList := Value;
1717 end;
1718
1719 procedure TBottleLogDragObject.SetLogItem(const Value: TLogItem);
1720 begin
1721   FLogItem := Value;
1722 end;
1723
1724 procedure TfrmLog.mnTabSaveXMLLogClick(Sender: TObject);
1725 begin
1726   DoSaveLogXML(FBottleLogList[tabBottleLog.Tag] as TBottleLogList);
1727 end;
1728
1729 procedure TfrmLog.mnSaveHTMLClick(Sender: TObject);
1730 var
1731   LogList, SB: TBottleLogList;
1732   i: integer;
1733   Options: THTMLOutputOptions;
1734 begin
1735   SB := SelectedBottleLog;
1736   if SB = nil then
1737     Exit;
1738   if SB.Count = 0 then
1739     Exit;
1740   Application.CreateForm(TfrmHTMLOutputConfig, frmHTMLOutputConfig);
1741   with frmHTMLOutputConfig do
1742     try
1743       // Show HTML save option dialog
1744       if not Execute then
1745         Exit;
1746       LogList := TBottleLogList.Create('');
1747       try
1748         case Range of
1749           orAll:
1750             for i := SB.Count-1 downto 0 do
1751               if SB.Bottles[i].LogType = ltBottle then
1752                 LogList.Add(TLogItem.Create(SB.Bottles[i]));
1753           orSelected:
1754             if SB.Bottles[lvwLog.Selected.Index].LogType = ltBottle then
1755               LogList.Add(TLogItem.Create(SB.Bottles[lvwLog.Selected.Index]))
1756             else
1757               ShowMessage('\82±\82Ì\83\81\83b\83Z\81[\83W\82Í\95Û\91\82Å\82«\82Ü\82¹\82ñ');
1758           orUpward:
1759             for i := lvwLog.Selected.Index downto 0 do
1760               if SB.Bottles[i].LogType = ltBottle then
1761                 LogList.Add(TLogItem.Create(SB.Bottles[i]));
1762         end;
1763         Options.ImageDir := ImageDir;
1764         Options.UseColor := UseColor;
1765         Options.ImageType := ImageType;
1766         Application.CreateForm(TfrmHTMLOutputProgress, frmHTMLOutputProgress);
1767         try
1768           frmHTMLOutputProgress.Show;
1769           LogList.OnHTMLOutputWork := HTMLOutputWork;
1770           LogList.SaveToHTML(FileName, Options, SsParser);
1771         finally
1772           frmHTMLOutputProgress.Release;
1773         end;
1774       finally
1775         LogList.Free;
1776       end;
1777     finally
1778       Release;
1779     end;
1780 end;
1781
1782 procedure TfrmLog.mnPopupCopyGhostClick(Sender: TObject);
1783 var
1784   Log: TLogItem;
1785   Clip: TClipBoard;
1786 begin
1787   Log := SelectedBottleLog.Bottles[frmLog.lvwLog.Selected.Index];
1788   if Log = nil then Exit;
1789   Clip := ClipBoard();
1790   Clip.SetTextBuf(PChar(Log.Ghost));
1791 end;
1792
1793 end.