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.
Revision 105 by alloc, Wed Feb 21 00:29:27 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;
42    procedure RecreateList;
43    procedure UpdateList;
43      procedure RecreateExtList;
44 +    procedure UpdateConList;
45      procedure LoadFileNames;
46 <    procedure SelectFileName(connection: Integer; FileName: String);
47 <    procedure SelectFileID(connection: Integer; FileID: Integer);
46 >    procedure SelectFileName(ConnectionID: Integer; FileName: String);
47 >    procedure SelectFileID(ConnectionID, FileID: Integer);
48 >    procedure SelectConnection(ConnectionID: Integer);
49      procedure check_filternameClick(Sender: TObject);
50      procedure check_zerobyteClick(Sender: TObject);
51      procedure combo_extensionClick(Sender: TObject);
# Line 52 | Line 53 | type
53      procedure listMouseDown(Sender: TObject; Button: TMouseButton;
54        Shift: TShiftState; X, Y: Integer);
55  
55    procedure FormResize(Sender: TObject);
56    procedure FormCreate(Sender: TObject);
56      procedure FormClose(Sender: TObject; var Action: TCloseAction);
57      procedure popup_importClick(Sender: TObject);
58      procedure popup_exportClick(Sender: TObject);
# Line 66 | Line 65 | type
65      FSortBy: TSortType;
66      FOnNewFileSelected: TNewFileSelectedEvent;
67      FOnNewConnection: TNewConnectionEvent;
68 +    FOnCheckCloseable: TCheckCloseableEvent;
69      FAllowedExts: String;
70      FAllowMultiSelect: Boolean;
71      FSelectedFile: TFileInfo;
72 <    FConnection: Integer;
72 >    FConnectionID: Integer;
73      procedure SetAllowedExts(exts: String);
74      procedure SetMultiSelect(allow: Boolean);
75 +    function GetToolCloseable: Boolean;
76    public
77      constructor Create(AOwner: TComponent); override;
78      procedure SetFileFilters(pattern, extension: String; zerobytes: Boolean);
79    published
80      property OnNewFileSelected: TNewFileSelectedEvent read FOnNewFileSelected write FOnNewFileSelected;
81      property OnNewConnection: TNewConnectionEvent read FOnNewConnection write FOnNewConnection;
82 +    property OnCheckCloseable: TCheckCloseableEvent read FOnCheckCloseable write FOnCheckCloseable;
83      property AllowedExts: String read FAllowedExts write SetAllowedExts;
84      property AllowMultiSelect: Boolean read FAllowMultiSelect write SetMultiSelect;
85      property SelectedFile: TFileInfo read FSelectedFile;
86 <    property Connection: Integer read FConnection;
86 >    property ConnectionID: Integer read FConnectionID;
87 >    property Closeable: Boolean read GetToolCloseable;
88    end;
89  
90   var
# Line 90 | Line 93 | procedure AddToolListEntry(context, name
93  
94   implementation
95   {$R *.dfm}
96 < uses Main, Exporters;
96 > uses Main, ConnectionManager, Exporters, Functions;
97  
98  
99 < procedure TForm_ToolTemplate.UpdateList;
99 > procedure TForm_ToolTemplate.UpdateConList;
100   var
101    i: Integer;
102    fn, datatype, boxstring: String;
103    level: Integer;
101  oldcon: String;
104   begin
103  oldcon := combo_connection.Items.Strings[combo_connection.ItemIndex];
105    combo_connection.ItemIndex := -1;
106    combo_connection.Items.Clear;
107 <  if Length(DataConnections) > 0 then
107 >  if ConManager.Count > 0 then
108    begin
109 <    for i := 0 to High(DataConnections) do
109 >    for i := 0 to ConManager.Count - 1 do
110      begin
111 <      level := DataConnections[i].LevelInfo.LevelNumber;
112 <      fn := ExtractFileName(DataConnections[i].FileName);
113 <      if DataConnections[i].Backend = ODB_Dat then
111 >      level := ConManager.ConnectionByIndex[i].LevelNumber;
112 >      fn := ExtractFileName(ConManager.ConnectionByIndex[i].FileName);
113 >      if ConManager.ConnectionByIndex[i].Backend = DB_ONI then
114          datatype := 'ONI-.dat: '
115 <      else if DataConnections[i].Backend = ODB_ADB then
115 >      else if ConManager.ConnectionByIndex[i].Backend = DB_ADB then
116          datatype := 'OUP-DB: '
117        else
118          datatype := 'Unknown: ';
119 <      boxstring := datatype + fn + ' (Level: ' + IntToStr(level) + ')';
119 >      boxstring := datatype + fn + ' (Level: ' + IntToStr(level) + ') [' + IntToStr(ConManager.ConnectionByIndex[i].ConnectionID) + ']';
120        combo_connection.Items.Add(boxstring);
121 <      if oldcon = boxstring then
121 >      if ConManager.ConnectionByIndex[i].ConnectionID = FConnectionID then
122          combo_connection.ItemIndex := combo_connection.Items.Count - 1;
123      end;
124      if combo_connection.ItemIndex = -1 then
# Line 128 | Line 129 | begin
129    end
130    else
131    begin
132 <    FConnection := nil;
132 >    FConnectionID := 0;
133      filelist.Items.Clear;
134      combo_extension.Items.Clear;
135      combo_connectionChange(Self);
136 <    FSelectedFile := GetEmptyFileInfo;
136 >    FSelectedFile.ID := -1;
137      if Assigned(FOnNewFileSelected) then
138        FOnNewFileSelected(FSelectedFile);
139    end;
140   end;
141  
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
142   procedure TForm_ToolTemplate.RecreateExtList;
143   var
144 <  i:    LongWord;
145 <  exts: TStringArray;
144 >  i:    Integer;
145 >  exts: TStrings;
146   begin
147    combo_extension.Items.Clear;
148 <  if FConnection <> nil then
148 >  if FConnectionID > -1 then
149    begin
150      combo_extension.Items.Add('_All files_ (' +
151 <      IntToStr(FConnection.GetFilesCount) + ')');
152 <    exts := FConnection.GetExtensionsList;
153 <    for i := 0 to High(exts) do
151 >      IntToStr(ConManager.Connection[FConnectionID].GetFileCount) + ')');
152 >    exts := ConManager.Connection[FConnectionID].GetExtensionsList(EF_ExtCount);
153 >    for i := 0 to exts.Count - 1 do
154        if Length(FAllowedExts) > 0 then
155        begin
156 <        if Pos(MidStr(exts[i],1,4), FAllowedExts) > 0 then
157 <        begin
158 <          combo_extension.Items.Add(exts[i]);
159 <        end;
160 <      end else
194 <        combo_extension.Items.Add(exts[i]);
156 >        if Pos(MidStr(exts.Strings[i],1,4), FAllowedExts) > 0 then
157 >          combo_extension.Items.Add(exts.Strings[i]);
158 >      end
159 >      else
160 >        combo_extension.Items.Add(exts.Strings[i]);
161      combo_extension.ItemIndex := 0;
162      combo_extensionClick(Self);
163 +    exts.Free;
164    end;
165   end;
166  
# Line 205 | Line 172 | var
172    Extension: String;
173    no_zero_bytes: Boolean;
174    pattern: String;
175 <  files: TStringArray;
176 <  i: LongWord;
175 >  files: TStrings;
176 >  i: Integer;
177   begin
178 <  if FConnection <> nil then
178 >  if FConnectionID > -1 then
179    begin
180      Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4);
181      no_zero_bytes := not check_zerobyte.Checked;
# Line 221 | Line 188 | begin
188        else
189          Extension := '';
190  
191 <    files := FConnection.GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
191 >    files := ConManager.Connection[FConnectionID].GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
192  
193      filelist.Visible := False;
194      filelist.Items.Clear;
195 <    if Length(files) > 0 then
196 <      for i := 0 to High(files) do
230 <        filelist.Items.Add(files[i]);
195 >    if files.Count > 0 then
196 >      filelist.Items.AddStrings(files);
197      filelist.Visible := True;
198    end;
199   end;
# Line 238 | Line 204 | var
204    id: Integer;
205    ext: String;
206   begin
207 <  id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
207 >  id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
208    ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
209    exportd.Filter := 'Files of matching extension (*.' + ext + ')|*.' + ext + '|All files|*.*';
210    exportd.DefaultExt := ext;
211    if exportd.Execute then
212 <    ExportDatFile(FConnection, id, exportd.FileName);
212 >    ExportDatFile(FConnectionID, id, exportd.FileName);
213   end;
214  
215   procedure TForm_ToolTemplate.popup_importClick(Sender: TObject);
# Line 251 | Line 217 | var
217    id: Integer;
218    finfo: TFileInfo;
219    fs: TFileStream;
254  data: TData;
220   begin
221 <  id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
222 <  finfo := FConnection.GetFileInfo(id);
221 >  if CR_EditDat in ConManager.Connection[FConnectionID].ChangeRights then
222 >  begin
223 >    id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
224 >    finfo := ConManager.Connection[FConnectionID].GetFileInfo(id);
225  
226 <  importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' +
227 <        finfo.Extension + '|All files|*.*';
228 <  if importd.Execute then
229 <  begin
230 <    fs := TFileStream.Create(importd.FileName, fmOpenRead);
231 <    if fs.Size <> finfo.Size then
232 <      ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) +
233 <        ', file has to have same size as file in .dat.' + CrLf +
234 <        'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
235 <        'Size of chosen file: ' + FormatFileSize(fs.Size))
236 <    else begin
237 <      SetLength(data, fs.Size);
238 <      fs.Read(data[0], fs.Size);
239 <      FConnection.UpdateDatFile(id, data);
226 >    importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' +
227 >          finfo.Extension + '|All files|*.*';
228 >    if importd.Execute then
229 >    begin
230 >      fs := TFileStream.Create(importd.FileName, fmOpenRead);
231 >      if fs.Size <> finfo.Size then
232 >      begin
233 >        if not (CR_ResizeDat in ConManager.Connection[FConnectionID].ChangeRights) then
234 >        begin
235 >          ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) +
236 >            ', file has to have same size as file in .dat with this backend.' + CrLf +
237 >            'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
238 >            'Size of chosen file: ' + FormatFileSize(fs.Size));
239 >          Exit;
240 >        end else begin
241 >          if MessageBox(Self.Handle,
242 >              PChar('File has different size from the file in the .dat.' + CrLf +
243 >                    'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
244 >                    'Size of chosen file: ' + FormatFileSize(fs.Size) + CrLf +
245 >                    'Replace anyway?'), PChar('Different size'), MB_YESNO + MB_ICONWARNING) = ID_NO then
246 >          begin
247 >            Exit;
248 >          end;
249 >        end;
250 >      end;
251 >      ConManager.Connection[FConnectionID].UpdateDatFile(id, fs);
252        Self.listClick(Self);
253 +      fs.Free;
254      end;
255 <    fs.Free;
255 >  end else begin
256 >    ShowMessage('Editing .dat-contents not allowed with this backend.');
257    end;
258   end;
259  
# Line 282 | Line 263 | var
263    id: Integer;
264   begin
265    sender_name := TComponent(Sender).Name;
266 <  id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
266 >  id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
267    context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name));
268 <  Form_Main.open_child(context, FConnection, id);
268 >  Form_Main.open_child(context, FConnectionID, id);
269   end;
270  
271   procedure TForm_ToolTemplate.combo_connectionChange(Sender: TObject);
# Line 296 | Line 277 | begin
277    if combo_connection.ItemIndex >= 0 then
278    begin
279      name := combo_connection.Items.Strings[combo_connection.ItemIndex];
280 <    nstart := Pos(' ', name) + 1;
281 <    nend := Pos('(', name) - 1;
282 <    name := MidStr(name, nstart, nend - nstart);
283 <
284 <    for i := 0 to High(DataConnections) do
285 <    begin
286 <      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;
280 >    FConnectionID := StrToInt(MidStr(name, Pos('[', name) + 1, Pos(']', name) - Pos('[', name)  - 1));
281 >  end
282 >  else
283 >    FConnectionID := -1;
284 >  RecreateExtList;
285 >  if Assigned(FOnNewConnection) then
286 >    FOnNewConnection(FConnectionID);
287   end;
288  
289   procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject);
# Line 329 | Line 298 | var
298    item: TMenuItem;
299   begin
300    inherited;
301 <  RecreateList;
302 <  FSelectedFile := GetEmptyFileInfo;
301 >  Self.Width  := 260;
302 >  Self.Height := 300;
303 >  FAllowedExts := '';
304 >  FAllowMultiSelect := False;
305 >  FOnNewFileSelected := nil;
306 >  FOnNewConnection := nil;
307 >  FOnCheckCloseable := nil;
308 >  FConnectionID := -1;
309 >  FSelectedFile.ID := -1;
310 >  UpdateConList;
311    if Length(ToolList) > 0 then
312    begin
313      for i := 0 to High(ToolList) do
# Line 367 | Line 344 | end;
344   procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject);
345   begin
346    if btn_sort_id_asc.Down then
347 <    FSortBy := stIDAsc
347 >    FSortBy := ST_IDAsc
348    else if btn_sort_id_desc.Down then
349 <    FSortBy := stIDDesc
349 >    FSortBy := ST_IDDesc
350    else if btn_sort_name_asc.Down then
351 <    FSortBy := stNameAsc
351 >    FSortBy := ST_NameAsc
352    else if btn_sort_name_desc.Down then
353 <    FSortBy := stNameDesc
353 >    FSortBy := ST_NameDesc
354    else if btn_sort_ext_asc.Down then
355 <    FSortBy := stExtAsc
355 >    FSortBy := ST_ExtAsc
356    else if btn_sort_ext_desc.Down then
357 <    FSortBy := stExtDesc;
357 >    FSortBy := ST_ExtDesc;
358    LoadFileNames;
359   end;
360  
# Line 393 | Line 370 | var
370   begin
371    if filelist.ItemIndex > -1 then
372    begin
373 <    fileid := FConnection.ExtractFileID(
373 >    fileid := ConManager.Connection[FConnectionID].ExtractFileIDOfName(
374            filelist.Items.Strings[filelist.ItemIndex]);
375 <    FSelectedFile := FConnection.GetFileInfo(fileid);
375 >    FSelectedFile := ConManager.Connection[FConnectionID].GetFileInfo(fileid);
376      if Assigned(FOnNewFileSelected) then
377        FOnNewFileSelected(FSelectedFile);
378    end;
# Line 414 | Line 391 | end;
391  
392  
393  
394 < procedure TForm_ToolTemplate.SelectFileID(connection: TOniData; id: Integer);
418 < var
419 <  i: Integer;
420 <  name: String;
421 <  nstart, nend: Integer;
394 > procedure TForm_ToolTemplate.SelectConnection(ConnectionID: Integer);
395   begin
396 <  for i := 0 to combo_connection.Items.Count - 1 do
396 >  if FConnectionID <> ConnectionID then
397    begin
398 <    name := combo_connection.Items.Strings[i];
399 <    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;
398 >    combo_connection.ItemIndex := ConManager.ConnectionIndexByID[ConnectionID];
399 >    combo_connectionChange(Self);
400    end;
401 + end;
402 +
403 + procedure TForm_ToolTemplate.SelectFileID(ConnectionID, FileID: Integer);
404 + var
405 +  i: Integer;
406 + begin
407 +  if FConnectionID <> ConnectionID then
408 +    SelectConnection(ConnectionID);
409  
410    filelist.ItemIndex := -1;
411    if filelist.Items.Count > 0 then
412      for i := 0 to filelist.Items.Count - 1 do
413 <      if FConnection.ExtractFileID(filelist.Items.Strings[i]) = id then
413 >      if ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[i]) = FileID then
414        begin
415          filelist.ItemIndex := i;
416          Break;
# Line 445 | Line 418 | begin
418    Self.listClick(Self);
419   end;
420  
421 < procedure TForm_ToolTemplate.SelectFileName(connection: TOniData; filename: String);
421 > procedure TForm_ToolTemplate.SelectFileName(ConnectionID: Integer; filename: String);
422   var
423    i: Integer;
451  name: String;
452  nstart, nend: Integer;
424   begin
425 <  for i := 0 to combo_connection.Items.Count - 1 do
426 <  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;
425 >  if FConnectionID <> ConnectionID then
426 >    SelectConnection(ConnectionID);
427  
428    filelist.ItemIndex := -1;
429    if filelist.Items.Count > 0 then
# Line 508 | Line 468 | begin
468   end;
469  
470  
471 < 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);
471 > function TForm_ToolTemplate.GetToolCloseable: Boolean;
472   begin
473 <  Self.Width  := 260;
474 <  Self.Height := 300;
475 <  FOnNewFileSelected := nil;
476 <  FOnNewConnection := nil;
527 <  FAllowedExts := '';
528 <  FAllowMultiSelect := False;
473 >  if Assigned(FOnCheckCloseable) then
474 >    Result := FOnCheckCloseable
475 >  else
476 >    Result := True;
477   end;
478  
479   procedure TForm_ToolTemplate.FormActivate(Sender: TObject);

Diff Legend

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