ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/oup/current/Tools/Template.pas
(Generate patch)

Comparing:
oup/rewrite/Tools/Template.pas (file contents), Revision 93 by alloc, Thu Jan 18 17:15:59 2007 UTC vs.
oup/current/Tools/Template.pas (file contents), Revision 109 by alloc, Wed Feb 21 03:12:33 2007 UTC

# Line 5 | Line 5 | interface
5   uses
6    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7    Dialogs, ExtCtrls, StdCtrls, StrUtils,
8 <  ConnectionManager, {Functions,} Data, TypeDefs, Menus, Buttons;
8 >  Data, TypeDefs, Menus, Buttons;
9  
10   type
11    TNewFileSelectedEvent = procedure(FileInfo: TFileInfo) of object;
12    TNewConnectionEvent = procedure(Connection: Integer) of object;
13 +  TCheckCloseableEvent = function: Boolean of object;
14  
15    TForm_ToolTemplate = class(TForm)
16      panel_files: TPanel;
# Line 39 | Line 40 | type
40      Label3: TLabel;
41      combo_connection: TComboBox;
42      Bevel1: TBevel;
43 <    procedure RecreateList;
44 <    procedure UpdateList;
43 >    popup_linkshere: TMenuItem;
44 >    popup_separator2: TMenuItem;
45      procedure RecreateExtList;
46 +    procedure UpdateConList;
47      procedure LoadFileNames;
48 <    procedure SelectFileName(connection: Integer; FileName: String);
49 <    procedure SelectFileID(connection: Integer; FileID: Integer);
48 >    procedure SelectFileName(ConnectionID: Integer; FileName: String);
49 >    procedure SelectFileID(ConnectionID, FileID: Integer);
50 >    procedure SelectConnection(ConnectionID: Integer);
51      procedure check_filternameClick(Sender: TObject);
52      procedure check_zerobyteClick(Sender: TObject);
53      procedure combo_extensionClick(Sender: TObject);
# Line 52 | Line 55 | type
55      procedure listMouseDown(Sender: TObject; Button: TMouseButton;
56        Shift: TShiftState; X, Y: Integer);
57  
55    procedure FormResize(Sender: TObject);
56    procedure FormCreate(Sender: TObject);
58      procedure FormClose(Sender: TObject; var Action: TCloseAction);
59      procedure popup_importClick(Sender: TObject);
60      procedure popup_exportClick(Sender: TObject);
# Line 62 | Line 63 | type
63      procedure btn_sortClick(Sender: TObject);
64      procedure FormActivate(Sender: TObject);
65      procedure combo_connectionChange(Sender: TObject);
66 +    procedure popup_linkshereClick(Sender: TObject);
67    private
68      FSortBy: TSortType;
69      FOnNewFileSelected: TNewFileSelectedEvent;
70      FOnNewConnection: TNewConnectionEvent;
71 +    FOnCheckCloseable: TCheckCloseableEvent;
72      FAllowedExts: String;
73      FAllowMultiSelect: Boolean;
74      FSelectedFile: TFileInfo;
75 <    FConnection: Integer;
75 >    FConnectionID: Integer;
76      procedure SetAllowedExts(exts: String);
77      procedure SetMultiSelect(allow: Boolean);
78 +    function GetToolCloseable: Boolean;
79    public
80      constructor Create(AOwner: TComponent); override;
81      procedure SetFileFilters(pattern, extension: String; zerobytes: Boolean);
82    published
83      property OnNewFileSelected: TNewFileSelectedEvent read FOnNewFileSelected write FOnNewFileSelected;
84      property OnNewConnection: TNewConnectionEvent read FOnNewConnection write FOnNewConnection;
85 +    property OnCheckCloseable: TCheckCloseableEvent read FOnCheckCloseable write FOnCheckCloseable;
86      property AllowedExts: String read FAllowedExts write SetAllowedExts;
87      property AllowMultiSelect: Boolean read FAllowMultiSelect write SetMultiSelect;
88      property SelectedFile: TFileInfo read FSelectedFile;
89 <    property Connection: Integer read FConnection;
89 >    property ConnectionID: Integer read FConnectionID;
90 >    property Closeable: Boolean read GetToolCloseable;
91    end;
92  
93   var
# Line 90 | Line 96 | procedure AddToolListEntry(context, name
96  
97   implementation
98   {$R *.dfm}
99 < uses Main, Exporters;
99 > uses Main, ConnectionManager, Exporters, Functions, WhatLinksHere;
100  
101  
102 < procedure TForm_ToolTemplate.UpdateList;
102 > procedure TForm_ToolTemplate.UpdateConList;
103   var
104    i: Integer;
105    fn, datatype, boxstring: String;
106    level: Integer;
101  oldcon: String;
107   begin
103  oldcon := combo_connection.Items.Strings[combo_connection.ItemIndex];
108    combo_connection.ItemIndex := -1;
109    combo_connection.Items.Clear;
110 <  if Length(DataConnections) > 0 then
110 >  if ConManager.Count > 0 then
111    begin
112 <    for i := 0 to High(DataConnections) do
112 >    for i := 0 to ConManager.Count - 1 do
113      begin
114 <      level := DataConnections[i].LevelInfo.LevelNumber;
115 <      fn := ExtractFileName(DataConnections[i].FileName);
116 <      if DataConnections[i].Backend = ODB_Dat then
114 >      level := ConManager.ConnectionByIndex[i].LevelNumber;
115 >      fn := ExtractFileName(ConManager.ConnectionByIndex[i].FileName);
116 >      if ConManager.ConnectionByIndex[i].Backend = DB_ONI then
117          datatype := 'ONI-.dat: '
118 <      else if DataConnections[i].Backend = ODB_ADB then
118 >      else if ConManager.ConnectionByIndex[i].Backend = DB_ADB then
119          datatype := 'OUP-DB: '
120        else
121          datatype := 'Unknown: ';
122 <      boxstring := datatype + fn + ' (Level: ' + IntToStr(level) + ')';
122 >      boxstring := datatype + fn + ' (Level: ' + IntToStr(level) + ') [' + IntToStr(ConManager.ConnectionByIndex[i].ConnectionID) + ']';
123        combo_connection.Items.Add(boxstring);
124 <      if oldcon = boxstring then
124 >      if ConManager.ConnectionByIndex[i].ConnectionID = FConnectionID then
125          combo_connection.ItemIndex := combo_connection.Items.Count - 1;
126      end;
127      if combo_connection.ItemIndex = -1 then
# Line 128 | Line 132 | begin
132    end
133    else
134    begin
135 <    FConnection := nil;
135 >    FConnectionID := 0;
136      filelist.Items.Clear;
137      combo_extension.Items.Clear;
138      combo_connectionChange(Self);
139 <    FSelectedFile := GetEmptyFileInfo;
139 >    FSelectedFile.ID := -1;
140      if Assigned(FOnNewFileSelected) then
141        FOnNewFileSelected(FSelectedFile);
142    end;
143   end;
144  
141 procedure TForm_ToolTemplate.RecreateList;
142 var
143  i: Integer;
144  fn, datatype: String;
145  level: Integer;
146 begin
147  combo_connection.Items.Clear;
148  if Length(DataConnections) > 0 then
149  begin
150    for i := 0 to High(DataConnections) do
151    begin
152      level := DataConnections[i].LevelInfo.LevelNumber;
153      fn := ExtractFileName(DataConnections[i].FileName);
154      if DataConnections[i].Backend = ODB_Dat then
155        datatype := 'ONI-.dat: '
156      else if DataConnections[i].Backend = ODB_ADB then
157        datatype := 'OUP-DB: '
158      else
159        datatype := 'Unknown: ';
160      combo_connection.Items.Add(datatype + fn + ' (Level: ' + IntToStr(level) + ')');
161    end;
162    FConnection := DataConnections[0];
163    combo_connection.ItemIndex := 0;
164    combo_connectionChange(Self);
165  end
166  else
167  begin
168    FConnection := nil;
169    filelist.Items.Clear;
170    combo_extension.Items.Clear;
171    combo_connectionChange(Self);
172  end;
173 end;
174
145   procedure TForm_ToolTemplate.RecreateExtList;
146   var
147 <  i:    LongWord;
148 <  exts: TStringArray;
147 >  i:    Integer;
148 >  exts: TStrings;
149   begin
150    combo_extension.Items.Clear;
151 <  if FConnection <> nil then
151 >  if FConnectionID > -1 then
152    begin
153      combo_extension.Items.Add('_All files_ (' +
154 <      IntToStr(FConnection.GetFilesCount) + ')');
155 <    exts := FConnection.GetExtensionsList;
156 <    for i := 0 to High(exts) do
154 >      IntToStr(ConManager.Connection[FConnectionID].GetFileCount) + ')');
155 >    exts := ConManager.Connection[FConnectionID].GetExtensionsList(EF_ExtCount);
156 >    for i := 0 to exts.Count - 1 do
157        if Length(FAllowedExts) > 0 then
158        begin
159 <        if Pos(MidStr(exts[i],1,4), FAllowedExts) > 0 then
160 <        begin
161 <          combo_extension.Items.Add(exts[i]);
162 <        end;
163 <      end else
194 <        combo_extension.Items.Add(exts[i]);
159 >        if Pos(MidStr(exts.Strings[i],1,4), FAllowedExts) > 0 then
160 >          combo_extension.Items.Add(exts.Strings[i]);
161 >      end
162 >      else
163 >        combo_extension.Items.Add(exts.Strings[i]);
164      combo_extension.ItemIndex := 0;
165      combo_extensionClick(Self);
166 +    exts.Free;
167    end;
168   end;
169  
# Line 205 | Line 175 | var
175    Extension: String;
176    no_zero_bytes: Boolean;
177    pattern: String;
178 <  files: TStringArray;
179 <  i: LongWord;
178 >  files: TStrings;
179 >  i: Integer;
180   begin
181 <  if FConnection <> nil then
181 >  if FConnectionID > -1 then
182    begin
183      Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4);
184      no_zero_bytes := not check_zerobyte.Checked;
# Line 221 | Line 191 | begin
191        else
192          Extension := '';
193  
194 <    files := FConnection.GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
194 >    files := ConManager.Connection[FConnectionID].GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
195  
196      filelist.Visible := False;
197      filelist.Items.Clear;
198 <    if Length(files) > 0 then
199 <      for i := 0 to High(files) do
230 <        filelist.Items.Add(files[i]);
198 >    if files.Count > 0 then
199 >      filelist.Items.AddStrings(files);
200      filelist.Visible := True;
201    end;
202   end;
# Line 238 | Line 207 | var
207    id: Integer;
208    ext: String;
209   begin
210 <  id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
210 >  id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
211    ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
212    exportd.Filter := 'Files of matching extension (*.' + ext + ')|*.' + ext + '|All files|*.*';
213    exportd.DefaultExt := ext;
214    if exportd.Execute then
215 <    ExportDatFile(FConnection, id, exportd.FileName);
215 >    ExportDatFile(FConnectionID, id, exportd.FileName);
216   end;
217  
218   procedure TForm_ToolTemplate.popup_importClick(Sender: TObject);
# Line 251 | Line 220 | var
220    id: Integer;
221    finfo: TFileInfo;
222    fs: TFileStream;
254  data: TData;
223   begin
224 <  id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
225 <  finfo := FConnection.GetFileInfo(id);
224 >  if CR_EditDat in ConManager.Connection[FConnectionID].ChangeRights then
225 >  begin
226 >    id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
227 >    finfo := ConManager.Connection[FConnectionID].GetFileInfo(id);
228  
229 <  importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' +
230 <        finfo.Extension + '|All files|*.*';
231 <  if importd.Execute then
232 <  begin
233 <    fs := TFileStream.Create(importd.FileName, fmOpenRead);
234 <    if fs.Size <> finfo.Size then
235 <      ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) +
236 <        ', file has to have same size as file in .dat.' + CrLf +
237 <        'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
238 <        'Size of chosen file: ' + FormatFileSize(fs.Size))
239 <    else begin
240 <      SetLength(data, fs.Size);
241 <      fs.Read(data[0], fs.Size);
242 <      FConnection.UpdateDatFile(id, data);
229 >    importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' +
230 >          finfo.Extension + '|All files|*.*';
231 >    if importd.Execute then
232 >    begin
233 >      fs := TFileStream.Create(importd.FileName, fmOpenRead);
234 >      if fs.Size <> finfo.Size then
235 >      begin
236 >        if not (CR_ResizeDat in ConManager.Connection[FConnectionID].ChangeRights) then
237 >        begin
238 >          ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) +
239 >            ', file has to have same size as file in .dat with this backend.' + CrLf +
240 >            'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
241 >            'Size of chosen file: ' + FormatFileSize(fs.Size));
242 >          Exit;
243 >        end else begin
244 >          if MessageBox(Self.Handle,
245 >              PChar('File has different size from the file in the .dat.' + CrLf +
246 >                    'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
247 >                    'Size of chosen file: ' + FormatFileSize(fs.Size) + CrLf +
248 >                    'Replace anyway?'), PChar('Different size'), MB_YESNO + MB_ICONWARNING) = ID_NO then
249 >          begin
250 >            Exit;
251 >          end;
252 >        end;
253 >      end;
254 >      ConManager.Connection[FConnectionID].UpdateDatFile(id, fs);
255        Self.listClick(Self);
256 +      fs.Free;
257      end;
258 <    fs.Free;
258 >  end else begin
259 >    ShowMessage('Editing .dat-contents not allowed with this backend.');
260    end;
261   end;
262  
263 + procedure TForm_ToolTemplate.popup_linkshereClick(Sender: TObject);
264 + begin
265 +  Form_WhatLinksHere.ConID := FConnectionID;
266 +  Form_WhatLinksHere.FileID := FSelectedFile.ID;
267 +  Form_WhatLinksHere.SenderForm := Self;
268 +  Form_WhatLinksHere.Show;
269 + end;
270 +
271   procedure TForm_ToolTemplate.popup_opentool(Sender: TObject);
272   var
273    sender_name, context: String;
274    id: Integer;
275   begin
276    sender_name := TComponent(Sender).Name;
277 <  id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
277 >  id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
278    context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name));
279 <  Form_Main.open_child(context, FConnection, id);
279 >  Form_Main.open_child(context, FConnectionID, id);
280   end;
281  
282   procedure TForm_ToolTemplate.combo_connectionChange(Sender: TObject);
# Line 296 | Line 288 | begin
288    if combo_connection.ItemIndex >= 0 then
289    begin
290      name := combo_connection.Items.Strings[combo_connection.ItemIndex];
291 <    nstart := Pos(' ', name) + 1;
292 <    nend := Pos('(', name) - 1;
293 <    name := MidStr(name, nstart, nend - nstart);
294 <
295 <    for i := 0 to High(DataConnections) do
296 <    begin
297 <      if ExtractFileName(DataConnections[i].FileName) = name then
306 <      begin
307 <        FConnection := DataConnections[i];
308 <        Break;
309 <      end;
310 <    end;
311 <    if i = Length(DataConnections) then
312 <      FConnection := nil;
313 <
314 <    RecreateExtList;
315 <    if Assigned(FOnNewConnection) then
316 <      FOnNewConnection(FConnection);
317 <  end;
291 >    FConnectionID := StrToInt(MidStr(name, Pos('[', name) + 1, Pos(']', name) - Pos('[', name)  - 1));
292 >  end
293 >  else
294 >    FConnectionID := -1;
295 >  RecreateExtList;
296 >  if Assigned(FOnNewConnection) then
297 >    FOnNewConnection(FConnectionID);
298   end;
299  
300   procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject);
# Line 329 | Line 309 | var
309    item: TMenuItem;
310   begin
311    inherited;
312 <  RecreateList;
313 <  FSelectedFile := GetEmptyFileInfo;
312 >  Self.Width  := 260;
313 >  Self.Height := 300;
314 >  FAllowedExts := '';
315 >  FAllowMultiSelect := False;
316 >  FOnNewFileSelected := nil;
317 >  FOnNewConnection := nil;
318 >  FOnCheckCloseable := nil;
319 >  FConnectionID := -1;
320 >  FSelectedFile.ID := -1;
321 >  UpdateConList;
322    if Length(ToolList) > 0 then
323    begin
324      for i := 0 to High(ToolList) do
# Line 357 | Line 345 | begin
345        if Pos(ext, ToolList[i].exts) = 0 then
346          filepopup.Items.Items[i].Enabled := False;
347    end;
348 +  filepopup.Items.Find('What links here?').Enabled :=
349 +      ConManager.Connection[FConnectionID].Backend = DB_ADB;
350   end;
351  
352   procedure TForm_ToolTemplate.check_zerobyteClick(Sender: TObject);
# Line 367 | Line 357 | end;
357   procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject);
358   begin
359    if btn_sort_id_asc.Down then
360 <    FSortBy := stIDAsc
360 >    FSortBy := ST_IDAsc
361    else if btn_sort_id_desc.Down then
362 <    FSortBy := stIDDesc
362 >    FSortBy := ST_IDDesc
363    else if btn_sort_name_asc.Down then
364 <    FSortBy := stNameAsc
364 >    FSortBy := ST_NameAsc
365    else if btn_sort_name_desc.Down then
366 <    FSortBy := stNameDesc
366 >    FSortBy := ST_NameDesc
367    else if btn_sort_ext_asc.Down then
368 <    FSortBy := stExtAsc
368 >    FSortBy := ST_ExtAsc
369    else if btn_sort_ext_desc.Down then
370 <    FSortBy := stExtDesc;
370 >    FSortBy := ST_ExtDesc;
371    LoadFileNames;
372   end;
373  
# Line 393 | Line 383 | var
383   begin
384    if filelist.ItemIndex > -1 then
385    begin
386 <    fileid := FConnection.ExtractFileID(
386 >    fileid := ConManager.Connection[FConnectionID].ExtractFileIDOfName(
387            filelist.Items.Strings[filelist.ItemIndex]);
388 <    FSelectedFile := FConnection.GetFileInfo(fileid);
388 >    FSelectedFile := ConManager.Connection[FConnectionID].GetFileInfo(fileid);
389      if Assigned(FOnNewFileSelected) then
390        FOnNewFileSelected(FSelectedFile);
391    end;
# Line 408 | Line 398 | var
398   begin
399    pt.X := x;
400    pt.Y := y;
401 < //  filelist.ItemIndex := filelist.ItemAtPos(pt, true);
402 < //  Self.listClick(Self);
401 >  if Shift = [ssRight] then
402 >  begin
403 >    filelist.ItemIndex := filelist.ItemAtPos(pt, true);
404 >    Self.listClick(Self);
405 >  end;
406   end;
407  
408  
409  
410 < procedure TForm_ToolTemplate.SelectFileID(connection: TOniData; id: Integer);
418 < var
419 <  i: Integer;
420 <  name: String;
421 <  nstart, nend: Integer;
410 > procedure TForm_ToolTemplate.SelectConnection(ConnectionID: Integer);
411   begin
412 <  for i := 0 to combo_connection.Items.Count - 1 do
412 >  if FConnectionID <> ConnectionID then
413    begin
414 <    name := combo_connection.Items.Strings[i];
415 <    nstart := Pos(' ', name) + 1;
427 <    nend := Pos('(', name) - 1;
428 <    name := MidStr(name, nstart, nend - nstart);
429 <
430 <    if ExtractFileName(connection.FileName) = name then
431 <    begin
432 <      combo_connection.ItemIndex := i;
433 <      combo_connectionChange(Self);
434 <    end;
414 >    combo_connection.ItemIndex := ConManager.ConnectionIndexByID[ConnectionID];
415 >    combo_connectionChange(Self);
416    end;
417 + end;
418 +
419 + procedure TForm_ToolTemplate.SelectFileID(ConnectionID, FileID: Integer);
420 + var
421 +  i: Integer;
422 + begin
423 +  if FConnectionID <> ConnectionID then
424 +    SelectConnection(ConnectionID);
425  
426    filelist.ItemIndex := -1;
427    if filelist.Items.Count > 0 then
428      for i := 0 to filelist.Items.Count - 1 do
429 <      if FConnection.ExtractFileID(filelist.Items.Strings[i]) = id then
429 >      if ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[i]) = FileID then
430        begin
431          filelist.ItemIndex := i;
432          Break;
# Line 445 | Line 434 | begin
434    Self.listClick(Self);
435   end;
436  
437 < procedure TForm_ToolTemplate.SelectFileName(connection: TOniData; filename: String);
437 > procedure TForm_ToolTemplate.SelectFileName(ConnectionID: Integer; filename: String);
438   var
439    i: Integer;
451  name: String;
452  nstart, nend: Integer;
440   begin
441 <  for i := 0 to combo_connection.Items.Count - 1 do
442 <  begin
456 <    name := combo_connection.Items.Strings[i];
457 <    nstart := Pos(' ', name) + 1;
458 <    nend := Pos('(', name) - 1;
459 <    name := MidStr(name, nstart, nend - nstart);
460 <
461 <    if ExtractFileName(connection.FileName) = name then
462 <    begin
463 <      combo_connection.ItemIndex := i;
464 <      combo_connectionChange(Self);
465 <    end;
466 <  end;
441 >  if FConnectionID <> ConnectionID then
442 >    SelectConnection(ConnectionID);
443  
444    filelist.ItemIndex := -1;
445    if filelist.Items.Count > 0 then
# Line 508 | Line 484 | begin
484   end;
485  
486  
487 < procedure TForm_ToolTemplate.FormResize(Sender: TObject);
512 < begin
513 <  if Self.Width < 300 then
514 <    Self.Width := 300;
515 <  if Self.Height < 200 then
516 <    Self.Height := 200;
517 < end;
518 <
519 <
520 <
521 < procedure TForm_ToolTemplate.FormCreate(Sender: TObject);
487 > function TForm_ToolTemplate.GetToolCloseable: Boolean;
488   begin
489 <  Self.Width  := 260;
490 <  Self.Height := 300;
491 <  FOnNewFileSelected := nil;
492 <  FOnNewConnection := nil;
527 <  FAllowedExts := '';
528 <  FAllowMultiSelect := False;
489 >  if Assigned(FOnCheckCloseable) then
490 >    Result := FOnCheckCloseable
491 >  else
492 >    Result := True;
493   end;
494  
495   procedure TForm_ToolTemplate.FormActivate(Sender: TObject);

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)