source: trunk/NewView/SettingsUnit.pas@ 113

Last change on this file since 113 was 113, checked in by RBRi, 18 years ago

changed to use StringUtilsUnit

  • Property svn:eol-style set to native
File size: 18.2 KB
Line 
1Unit SettingsUnit;
2
3// NewView - a new OS/2 Help Viewer
4// Copyright 2003 Aaron Lawrence (aaronl at consultant dot com)
5// This software is released under the Gnu Public License - see readme.txt
6
7Interface
8
9// Defines settings (options) in a record and contains functions
10// for loading and saving them to ini file.
11
12Uses
13 Forms,
14 Classes;
15
16Const
17 ContentsBackgroundColorIndex = 0;
18 ContentsTextColorIndex = 1;
19 ContentsLinesColorIndex = 2;
20 IndexBackgroundColorIndex = 3;
21 IndexTextColorIndex = 4;
22 SearchBackgroundColorIndex = 5;
23 SearchTextColorIndex = 6;
24 NotesListBackgroundColorIndex = 7;
25 NotesListTextColorIndex = 8;
26 TopicBackgroundColorIndex = 9;
27 NotesTextColorIndex = 10;
28 SearchHighlightTextColorIndex = 11;
29 NumColorSettings = 12;
30
31 clLightYellow = $ffffc0;
32 clLightBlue = $e0e0ff;
33 clLightCyan = $c0ffff;
34 clLightGreen = $e0ffe0;
35
36 VGADefaultColors: array[ 0 .. NumColorSettings - 1 ] of TColor
37 = ( clWindow,
38 clWindowText,
39 clWindowText,
40 clWindow,
41 clWindowText,
42 clWindow,
43 clWindowText,
44 clWindow,
45 clWindowText,
46 clWindow,
47 clGreen,
48 clYellow );
49
50 DefaultColors: array[ 0 .. NumColorSettings - 1 ] of TColor
51 = ( clLightCyan,
52 clBlack,
53 clBlue,
54 clLightGreen,
55 clBlack,
56 clLightBlue,
57 clBlack,
58 clWhite,
59 clBlack,
60 clWhite,
61 clGreen,
62 clYellow );
63
64 ApplicationFontIndex = 0;
65 NumFontSettings = 1;
66
67 DefaultTopicFontName = 'Helv';
68 DefaultTopicFontSize = 8;
69
70 DefaultTopicFixedFontName = 'Courier';
71 DefaultTopicFixedFontSize = 8;
72
73Type
74 TIndexStyle = ( isFileOnly, isAlphabetical, isFull );
75 TToolbarStyle = ( tsNone, tsImages, tsText, tsImagesAndText );
76 TGlobalSearchLocation = ( gsHelpPaths, gsFixedDrives, gsSelectedHelpPaths, gsCustom );
77
78 TMRUItem = class
79 Title: string;
80 Filenames: TStringList;
81 constructor Create;
82 destructor Destroy; override;
83 end;
84
85 TSettings = record
86 MRUList: TList;
87
88 LastOpenDirectory: string;
89 LastSaveDirectory: string;
90
91 StartupHelp: boolean;
92
93 LeftPanelWidth: longint;
94 ShowLeftPanel_Help: boolean;
95 ShowLeftPanel_Standalone: boolean;
96
97 FileDialogSplit: real;
98
99 // Colours
100 Colors: array[ 0..NumColorSettings - 1 ] of TColor;
101
102 NormalFont: TFont;
103 FixedFont: TFont;
104 Fonts: array[ 0..NumFontSettings - 1 ] of TFont;
105 FixedFontSubstitution: boolean;
106 FixedFontSubstitutes: string;
107
108 IndexStyle: TIndexStyle;
109
110 SmoothScrolling: boolean;
111 UseOriginalDialogs: boolean;
112 OpenWithExpandedContents: boolean;
113
114 ToolbarBackgroundImageFilename: string;
115 ToolbarStyle: TToolbarStyle;
116
117 ConfirmWinHelp: boolean;
118
119 GlobalSearchLocation: TGlobalSearchLocation;
120
121 SearchDirectories: TStringList;
122 end;
123
124 Procedure WriteWindowPos( Form: TForm );
125 Procedure ReadWindowPos( Form: TForm );
126 Procedure LoadSettings;
127 Procedure SaveSettings;
128
129Procedure AddToMRUList( const Title: string;
130 Filenames: TStrings );
131
132Var
133 Settings: TSettings;
134
135Implementation
136
137Uses
138 SysUtils,
139 DebugUnit,
140 Dos,
141 FileUtilsUnit,
142 ACLUtility,
143 StringUtilsUnit,
144 ControlsUtility;
145
146Const
147 IniFileName = 'NewView.ini';
148 GeneralSection = 'General';
149 FontsSection = 'Fonts';
150 ColoursSection = 'Colours';
151 MRUSection = 'RecentFiles';
152 MRUItemBaseSection = 'RecentFile';
153 SearchSection = 'Search';
154
155 DefaultWidth = 620;
156 DefaultHeight = 460;
157
158 MaxMRUListEntries = 9;
159
160constructor TMRUItem.Create;
161begin
162 Title := '';
163 Filenames := TStringList.Create;
164end;
165
166destructor TMRUItem.Destroy;
167begin
168 Filenames.Destroy;
169end;
170
171Function IniFilePath: string;
172Var
173 Dir: string;
174 Name, Ext: string;
175 UserIniFile: string;
176Begin
177 // Put it in the same place as the application
178 UserIniFile := GetApplicationDir;
179 // Note: do NOT combine these two steps. Caused an obscure bug!
180 FSplit( UserIniFile, Dir, Name, Ext );
181 Result := AddDirectorySeparator( Dir ) + IniFileName;
182End;
183
184// Note: since Sibyl SPCC 2.1.8.3
185// the Ascii ini file constructor does not throw an exception
186procedure GetIniFile( Var IniFile: TMyIniFile );
187begin
188 IniFile := TMyIniFile.Create( IniFilePath );
189end;
190
191procedure CloseIniFile( Var IniFIle: TMyIniFile );
192begin
193 try
194 LogEvent(LogSettings, 'Calling IniFile.Destroy');
195 IniFile.Destroy;
196 except
197 on E: Exception do
198 begin
199 // don't care if we can't save settings.
200 LogEvent(LogSettings, ' Exception: ' + E.Message );
201 end;
202 end;
203end;
204
205Procedure LoadSettings;
206Var
207 IniFile: TMyIniFile;
208 ColorIndex: longint;
209 DefaultColor: TColor;
210 FontName: string;
211 SettingString: string;
212
213 MRUItem: TMRUItem;
214 MRUItemSection: string;
215 MRUItemFileCount: longint;
216 MRUItemFileIndex: longint;
217 i: longint;
218 Count: longint;
219 MRUFilename: string;
220 MRUFileTitle: string;
221Begin
222 GetIniFile( IniFile );
223 with IniFile do
224 begin
225 IniFile.EraseSection( 'Windows' );
226 with Settings do
227 begin
228 LastOpenDirectory := ReadString( GeneralSection, 'LastOpenDirectory', '' );
229 LastSaveDirectory := ReadString( GeneralSection, 'LastSaveDirectory', '' );
230
231 // Read split points, as units of 0.1%
232 LeftPanelWidth := ReadInteger( GeneralSection, 'LeftPanelWidth', 225 );
233 FileDialogSplit := ReadInteger( GeneralSection, 'FileDialogSplit', 500 ) / 1000;
234
235 ShowLeftPanel_Help := ReadBool( GeneralSection, 'ShowLeftPanel_Help', false );
236 ShowLeftPanel_Standalone := ReadBool( GeneralSection, 'ShowLeftPanel_Standalone', true );
237
238 // Colours
239 for ColorIndex := 0 to High( Colors ) do
240 begin
241 if GetScreenColorDepth > 8 then
242 DefaultColor := DefaultColors[ ColorIndex ]
243 else
244 DefaultColor := VGADefaultColors[ ColorIndex ];
245 Colors[ ColorIndex ] := ReadInteger( ColoursSection,
246 'Color' + IntToStr( ColorIndex ),
247 DefaultColor );
248 end;
249
250 // Most Recently Used files list...
251
252 Count := ReadInteger( MRUSection,
253 'Count',
254 0 );
255
256 // Old style MRU list
257 for i := 0 to Count - 1 do
258 begin
259 MRUFilename := ReadString( MRUSection,
260 'File' + IntToStr( i ),
261 '' );
262 MRUFileTitle := ReadString( MRUSection,
263 'Title' + IntToStr( i ),
264 '' );
265 if MRUFilename <> '' then
266 begin
267 MRUItem := TMRUItem.Create;
268 MRUItem.Title := MRUFileTitle;
269
270 StrExtractStrings(MRUItem.Filenames, MRUFilename, ['+'], #0);
271 MRUList.Add( MRUItem );
272 end;
273 end;
274
275 // New style
276 for i := 0 to Count - 1 do
277 begin
278 MRUItemSection := MRUItemBaseSection + IntToStr( i );
279
280 MRUItem := TMRUItem.Create;
281
282 MRUItem.Title := ReadString( MRUItemSection,
283 'Title',
284 '' );
285
286 MRUItemFileCount := ReadInteger( MRUItemSection,
287 'FileCount',
288 0 );
289 for MRUItemFileIndex := 0 to MRUItemFileCount - 1 do
290 begin
291 MRUFilename := ReadString( MRUItemSection,
292 'File' + IntToStr( MRUItemFileIndex ),
293 '' );
294 if MRUFilename <> '' then
295 begin
296 MRUItem.Filenames.Add( MRUFilename );
297 end;
298 end;
299
300 if ( MRUItem.Title <> '' )
301 and ( MRUItem.Filenames.Count > 0 ) then
302 begin
303 // valid MRU item
304 MRUList.Add( MRUItem );
305 end
306 else
307 begin
308 // not valid
309 MRUItem.Destroy;
310 MRUItem := nil;
311 end;
312
313 end;
314
315 // Fonts
316 NormalFont := Screen.GetFontFromPointSize(
317 ReadString( FontsSection, 'NormalFont', DefaultTopicFontName ),
318 ReadInteger( FontsSection, 'NormalFontSize', DefaultTopicFontSize ) );
319 if NormalFont = nil then
320 NormalFont := Screen.DefaultFont;
321
322 FixedFont := Screen.GetFontFromPointSize(
323 ReadString( FontsSection, 'FixedFont', DefaultTopicFixedFontName ),
324 ReadInteger( FontsSection, 'FixedFontSize', DefaultTopicFixedFontSize ) );
325 if FixedFont = nil then
326 FixedFont := Screen.DefaultFont;
327
328 for i := 0 to NumFontSettings - 1 do
329 begin
330 FontName := 'Font' + IntToStr( i );
331 Fonts[ i ] := nil;
332 if ReadBool( FontsSection,
333 FontName
334 + 'Customised',
335 false ) then
336 Fonts[ i ] := Screen.GetFontFromPointSize(
337 ReadString( FontsSection, FontName + 'Face', DefaultTopicFontName ),
338 ReadInteger( FontsSection, FontName + 'Size', 10 ) );
339 end;
340
341 FixedFontSubstitution := ReadBool( FontsSection, 'FixedFontSubstitution', true );
342 FixedFontSubstitutes := ReadString( FontsSection, 'FixedFontSubstitutes', 'Courier 18x12' );
343
344 // Index style
345 SettingString := ReadString( GeneralSection, 'IndexStyle', 'Full' );
346 if StrEqualIgnoringCase( SettingString, 'FileOnly' ) then
347 IndexStyle := isFileOnly
348 else if StrEqualIgnoringCase( SettingString, 'Alphabetical' ) then
349 IndexStyle := isAlphabetical
350 else
351 IndexStyle := isFull;
352
353 StartupHelp := ReadBool( GeneralSection, 'StartupHelp', true );
354
355 SmoothScrolling := ReadBool( GeneralSection, 'SmoothScrolling', true );
356 UseOriginalDialogs := ReadBool( GeneralSection, 'UseOriginalDialogs', false );
357 OpenWithExpandedContents := ReadBool( GeneralSection, 'OpenWithExpandedContents', false );
358
359 ToolBarBackgroundImageFilename := ReadString( GeneralSection, 'ToolbarBackground', '' );
360 SettingString := ReadString( GeneralSection, 'ToolbarStyle', 'ImagesAndText' );
361
362 if StrEqualIgnoringCase( SettingString, 'None' ) then
363 ToolbarStyle := tsNone
364 else if StrEqualIgnoringCase( SettingString, 'Images' ) then
365 ToolbarStyle := tsImages
366 else if StrEqualIgnoringCase( SettingString, 'Text' ) then
367 ToolbarStyle := tsText
368 else
369 ToolbarStyle := tsImagesAndText;
370
371 ConfirmWinHelp := ReadBool( GeneralSection, 'ConfirmWinHelp', true );
372
373 Count := ReadInteger( SearchSection,
374 'CustomDirCount',
375 0 );
376
377 SearchDirectories.Clear;
378 for i := 0 to Count - 1 do
379 begin
380 SettingString := ReadString( SearchSection,
381 'CustomDir' + IntToStr( i ),
382 '' );
383 if trim( SettingString ) <> '' then
384 SearchDirectories.Add( SettingString );
385 end;
386
387 SettingString := ReadString( SearchSection,
388 'Location',
389 'HelpPaths' );
390 if StrEqualIgnoringCase( SettingString, 'HelpPaths' ) then
391 GlobalSearchLocation := gsHelpPaths
392 else if StrEqualIgnoringCase( SettingString, 'FixedDrives' ) then
393 GlobalSearchLocation := gsFixedDrives
394 else if StrEqualIgnoringCase( SettingString, 'SelectedHelpPaths' ) then
395 GlobalSearchLocation := gsSelectedHelpPaths
396 else
397 GlobalSearchLocation := gsCustom;
398
399 end;
400 end;
401 CloseIniFile( IniFile );
402End;
403
404Procedure SaveSettings;
405Var
406 IniFile: TMyIniFile;
407 ColorIndex: longint;
408 FontIndex: longint;
409 FontName: string;
410 i: longint;
411 MRUItemFileIndex: longint;
412 SettingString: string;
413 MRUItem: TMRUItem;
414 MRUItemSection: string;
415
416Begin
417 LogEvent(LogSettings, 'SaveSettings' );
418 GetIniFile( IniFile );
419 with IniFile do
420 begin
421 with Settings do
422 begin
423 WriteString( GeneralSection, 'LastOpenDirectory', LastOpenDirectory );
424 WriteString( GeneralSection, 'LastSaveDirectory', LastSaveDirectory );
425
426 WriteInteger( GeneralSection, 'LeftPanelWidth', LeftPanelWidth );
427 // Write split points, as units of 0.1%
428 WriteInteger( GeneralSection, 'FileDialogSplit', Round( FileDialogSplit * 1000 ) );
429
430 WriteBool( GeneralSection, 'ShowLeftPanel_Help', ShowLeftPanel_Help );
431 WriteBool( GeneralSection, 'ShowLeftPanel_Standalone', ShowLeftPanel_Standalone );
432
433 // COlours
434 for ColorIndex := 0 to High( Colors ) do
435 WriteInteger( ColoursSection,
436 'Color' + IntToStr( ColorIndex ),
437 Colors[ ColorIndex ] );
438
439 EraseSection( MRUSection ); // get rid of old style MRU
440 WriteInteger( MRUSection,
441 'Count',
442 MRUList.Count );
443
444 for i := 0 to MRUList.Count - 1 do
445 begin
446 MRUItem := MRUList[ i ];
447
448 MRUItemSection := MRUItemBaseSection + IntToStr( i );
449 EraseSection( MRUItemSection );
450
451 WriteString( MRUItemSection,
452 'Title',
453 MRUItem.Title );
454
455 WriteInteger( MRUItemSection,
456 'FileCount',
457 MRUItem.Filenames.Count );
458
459 for MRUItemFileIndex := 0 to MRUItem.Filenames.Count - 1 do
460 WriteString( MRUItemSection,
461 'File' + IntToStr( MRUItemFileIndex ),
462 MRUItem.Filenames[ MRUItemFileIndex ] );
463 end;
464
465 // clear unused sections
466 for i := MRUList.Count to MaxMRUListEntries do
467 begin
468 MRUItemSection := MRUItemBaseSection + IntToStr( i );
469 EraseSection( MRUItemSection );
470 end;
471
472 WriteString( FontsSection, 'NormalFont', NormalFont.FaceName );
473 WriteInteger( FontsSection, 'NormalFontSize', NormalFont.PointSize );
474 WriteString( FontsSection, 'FixedFont', FixedFont.FaceName );
475 WriteInteger( FontsSection, 'FixedFontSize', FixedFont.PointSize );
476
477 for FontIndex := 0 to NumFontSettings - 1 do
478 begin
479 FontName := 'Font' + IntToStr( FontIndex );
480
481 WriteBool( FontsSection, FontName + 'Customised', Fonts[ FontIndex ] <> nil );
482
483 if Fonts[ FontIndex ] <> nil then
484 begin
485 WriteString( FontsSection, FontName + 'Face', Fonts[ FontIndex ].FaceName );
486 WriteInteger( FontsSection, FontName + 'Size', Fonts[ FontIndex ].PointSize );
487 end;
488
489 end;
490
491 WriteBool( FontsSection, 'FixedFontSubstitution', FixedFontSubstitution );
492 WriteString( FontsSection, 'FixedFontSubstitutes', FixedFontSubstitutes );
493
494 case IndexStyle of
495 isFileOnly:
496 SettingString := 'FileOnly';
497 isAlphabetical:
498 SettingString := 'Alphabetical';
499 isFull:
500 SettingString := 'Full';
501 end;
502
503 WriteString( GeneralSection, 'IndexStyle', SettingString );
504
505 WriteBool( GeneralSection, 'StartupHelp', StartupHelp );
506
507 WriteBool( GeneralSection, 'SmoothScrolling', SmoothScrolling );
508 WriteBool( GeneralSection, 'UseOriginalDialogs', UseOriginalDialogs );
509 WriteBool( GeneralSection, 'OpenWithExpandedContents', OpenWithExpandedContents );
510
511 WriteString( GeneralSection, 'ToolbarBackground', ToolbarBackgroundImageFilename );
512
513 case ToolbarStyle of
514 tsNone:
515 SettingString := 'None';
516 tsImages:
517 SettingString := 'Images';
518 tsText:
519 SettingString := 'Text';
520 tsImagesAndText:
521 SettingString := 'ImagesAndText';
522 end;
523
524 WriteString( GeneralSection, 'ToolbarStyle', SettingString );
525
526 WriteBool( GeneralSection, 'ConfirmWinHelp', ConfirmWinHelp );
527
528 WriteInteger( SearchSection,
529 'CustomDirCount',
530 SearchDirectories.Count );
531
532 SearchDirectories.Sorted := true;
533 SearchDirectories.CaseSensitive := false;
534 SearchDirectories.Duplicates := dupIgnore;
535
536 for i := 0 to SearchDirectories.Count - 1 do
537 begin
538 WriteString( SearchSection,
539 'CustomDir' + IntToStr( i ),
540 SearchDirectories[ i ] );
541 end;
542
543 case GlobalSearchLocation of
544 gsHelpPaths:
545 SettingString := 'HelpPaths';
546
547 gsFixedDrives:
548 SettingString := 'FixedDrives';
549
550 gsSelectedHelpPaths:
551 SettingString := 'SelectedHelpPaths';
552
553 gsCustom:
554 SettingString := 'Custom';
555 end;
556
557 WriteString( SearchSection,
558 'Location',
559 SettingString );
560
561 end;
562 end;
563 CloseIniFile( IniFile );
564 LogEvent(LogSettings, ' Done' );
565
566End;
567
568Procedure AddToMRUList( const Title: string;
569 Filenames: TStrings );
570var
571 MRUIndex: longint;
572 PreviousMRUIndex: longint;
573 MRUItem: TMRUItem;
574begin
575 PreviousMRUIndex := -1;
576 for MRUIndex := 0 to Settings.MRUList.Count - 1 do
577 begin
578 MRUItem := Settings.MRUList[ MRUIndex ];
579
580 if ( MRUItem.Title = Title )
581 and ( MRUItem.Filenames.Equals( Filenames ) ) then
582 begin
583 // found identical entry in the list already.
584 PreviousMRUIndex := MRUIndex;
585 break;
586 end;
587 end;
588
589 if PreviousMRUIndex > -1 then
590 begin
591 // is already in list, move to top of list
592 MRUItem := Settings.MRUList[ PreviousMRUIndex ];
593 Settings.MRUList.Delete( PreviousMRUIndex );
594 end
595 else
596 begin
597 // not yet in list. Create new
598 MRUItem := TMRUItem.Create;
599 MRUItem.Title := Title;
600 MRUItem.Filenames.Assign( Filenames );
601 end;
602
603 Settings.MRUList.Insert( 0, MRUItem );
604
605 while Settings.MRUList.Count > MaxMRUListEntries do
606 begin
607 MRUItem := Settings.MRUList[ MaxMRUListEntries ];
608 Settings.MRUList.Delete( MaxMRUListEntries );
609 MRUItem.Destroy;
610 end;
611end;
612
613Procedure WriteWindowPos( Form: TForm );
614Var
615 IniFile: TMyIniFile;
616Begin
617 GetIniFile( IniFile );
618 SaveFormSizePosition( Form, IniFile );
619 CloseIniFile( IniFile );
620End;
621
622Procedure ReadWindowPos( Form: TForm );
623Var
624 IniFile: TMyIniFile;
625Begin
626 GetIniFile( IniFile );
627 LoadFormSizePosition( Form, IniFile );
628 CloseIniFile( IniFile );
629End;
630
631Initialization
632 Settings.NormalFont := Screen.GetFontFromPointSize( 'Helv',
633 8 );
634 Settings.FixedFont := Screen.GetFontFromPointSize( 'Courier',
635 8 );
636 Settings.SearchDirectories := TStringList.Create;
637
638Finalization
639 Settings.NormalFont.Destroy;
640 Settings.FixedFont.Destroy;
641 Settings.SearchDirectories.Destroy;
642End.
Note: See TracBrowser for help on using the repository browser.