summaryrefslogtreecommitdiff
path: root/docview/src/SettingsUnit.pas
blob: 903e9fe7707e2691d2bda6ee8d15fad078099662 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
unit SettingsUnit;

{$mode objfpc}{$H+}

interface

// Defines settings (options) in a record and contains functions
// for loading and saving them to ini file.

Uses
  Classes
  ,contnrs
  ,fpg_base
  ,fpg_main
  ,CanvasFontManager
  ;

Const
  ContentsBackgroundColorIndex          = 0;
  ContentsTextColorIndex                = 1;
  ContentsLinesColorIndex               = 2;
  IndexBackgroundColorIndex             = 3;
  IndexTextColorIndex                   = 4;
  SearchBackgroundColorIndex            = 5;
  SearchTextColorIndex                  = 6;
  NotesListBackgroundColorIndex         = 7;
  NotesListTextColorIndex               = 8;
  TopicBackgroundColorIndex             = 9;
  NotesTextColorIndex                   = 10;
  SearchHighlightTextColorIndex         = 11;
  NumColorSettings                      = 12;

  // already defined, but these values are slightly different
  //clLightYellow = $ffffc0;
  //clLightBlue = $e0e0ff;
  //clLightCyan = $c0ffff;
  //clLightGreen = $e0ffe0;

  VGADefaultColors: array[ 0 .. NumColorSettings - 1 ] of TfpgColor
   = ( clBoxColor,
       clText1,
       clText1,
       clBoxColor,
       clText1,
       clBoxColor,
       clText1,
       clBoxColor,
       clText1,
       clBoxColor,
       clGreen,
       clYellow );

  DefaultColors: array[ 0 .. NumColorSettings - 1 ] of TfpgColor
   = ( clLightCyan,
       clBlack,
       clBlue,
       clLightGreen,
       clBlack,
       clLightBlue,
       clBlack,
       clWhite,
       clBlack,
       clWhite,
       clGreen,
       clYellow );

  ApplicationFontIndex = 0;
  NumFontSettings = 1;

  DefaultTopicFont = DefaultTopicFont + '-' + DefaultTopicFontSize;
  DefaultTopicFixedFont = DefaultTopicFixedFont + '-10' + DefaultTopicFixedFontSize;


Type
  TIndexStyle = ( isAlphabetical, isFileOnly, isFull );
  TToolbarStyle = ( tsNone, tsImages, tsText, tsImagesAndText );
  TGlobalSearchLocation = ( gsHelpPaths, gsFixedDrives, gsSelectedHelpPaths, gsCustom );


  TMRUItem = class(TObject)
  public
    Title: string;
    Filenames: TStringList;
    constructor Create;
    destructor  Destroy; override;
  end;


  TSettings = record
    MRUList: TObjectList;
    LastOpenDirectory: string;
    LastSaveDirectory: string;
    StartupHelp: boolean;
    LeftPanelWidth: longint;
    ShowLeftPanel: boolean;
    FileDialogSplit: Double;
    Colors: array[ 0..NumColorSettings - 1 ] of TfpgColor;
    NormalFont: TfpgFont;
    FixedFont: TfpgFont;
    Fonts: array[ 0..NumFontSettings - 1 ] of TfpgFont;
    FixedFontSubstitution: boolean;
    FixedFontSubstitutes: string;
    IndexStyle: TIndexStyle;
    SmoothScrolling: boolean;
    UseOriginalDialogs: boolean;
    OpenWithExpandedContents: boolean;
    ToolbarBackgroundImageFilename: string;
    ToolbarStyle: TToolbarStyle;
    ConfirmWinHelp: boolean;
    GlobalSearchLocation: TGlobalSearchLocation;
    SearchDirectories: TStringList;
    IPFTopicSaveAsEscaped: boolean;
  end;


// global procs
procedure LoadSettings;
procedure SaveSettings;
procedure writeSettingsDetailsTo(aStrings : TStrings);
procedure AddToMRUList( const Title: string; Filenames: TStrings );

var
  Settings: TSettings;

Implementation

Uses
  SysUtils
  ,fpg_iniutils
  ,ACLStringUtility
  ,nvUtilities
  ;

const
  GeneralSection = 'General';
  FontsSection = 'Fonts';
  ColoursSection = 'Colours';
  MRUSection = 'RecentFiles';
  MRUItemBaseSection = 'RecentFile';
  SearchSection = 'Search';

  DefaultWidth = 620;
  DefaultHeight = 460;

  MaxMRUListEntries = 9;

constructor TMRUItem.Create;
begin
  Title := '';
  Filenames := TStringList.Create;
end;

destructor TMRUItem.Destroy;
begin
  Filenames.Free;
  inherited Destroy;
end;

Procedure LoadSettings;
var
  ColorIndex: longint;
  DefaultColor: TfpgColor;
  FontName: string;
  SettingString: string;
  MRUItem: TMRUItem;
  MRUItemSection: string;
  MRUItemFileCount: longint;
  MRUItemFileIndex: longint;
  i: longint;
  Count: longint;
  MRUFilename: string;
//  MRUFileTitle: string;
begin
  LogEvent(LogSettings, 'LoadSettings' );
  with gINI do
  begin
    EraseSection( 'Windows' );
    with Settings do
    begin
      LastOpenDirectory := ReadString( GeneralSection, 'LastOpenDirectory', '' );
      LastSaveDirectory := ReadString( GeneralSection, 'LastSaveDirectory', '' );

      // Read split points, as units of 0.1%
      LeftPanelWidth := ReadInteger( GeneralSection, 'LeftPanelWidth', 225 );
      FileDialogSplit := ReadInteger( GeneralSection, 'FileDialogSplit', 500 ) / 1000;

      ShowLeftPanel := ReadBool( GeneralSection, 'ShowLeftPanel', true );

      // Colours
      for ColorIndex := 0 to High( Colors ) do
      begin
        //if GetScreenColorDepth > 8 then
           DefaultColor := DefaultColors[ ColorIndex ];
        //else
        //   DefaultColor := VGADefaultColors[ ColorIndex ];
        Colors[ ColorIndex ] := ReadInteger( ColoursSection,
                                             'Color' + IntToStr( ColorIndex ),
                                             DefaultColor );
      end;

      // Most Recently Used files list...
      Count := ReadInteger( MRUSection, 'Count', 0 );
      for i := 0 to Count - 1 do
      begin
        MRUItemSection := MRUItemBaseSection + IntToStr( i );
        MRUItem := TMRUItem.Create;
        MRUItem.Title := ReadString( MRUItemSection, 'Title', '' );
        MRUItemFileCount := ReadInteger( MRUItemSection, 'FileCount', 0 );
        for MRUItemFileIndex := 0 to MRUItemFileCount - 1 do
        begin
          MRUFilename := ReadString( MRUItemSection,
                                     'File' + IntToStr( MRUItemFileIndex ),
                                     '' );
          if MRUFilename <> '' then
            MRUItem.Filenames.Add( MRUFilename );
        end;
        if ( MRUItem.Title <> '' ) and ( MRUItem.Filenames.Count > 0 ) then
          // valid MRU item
          MRUList.Add( MRUItem )
        else
        begin
          // not valid
          MRUItem.Free;
          MRUItem := nil;
        end;
      end;

      // Fonts
      NormalFont := fpgGetFont(ReadString(FontsSection, 'NormalFont', DefaultTopicFont));
      if NormalFont = nil then
        NormalFont := fpgStyle.DefaultFont;

      FixedFont := fpgGetFont(ReadString(FontsSection, 'FixedFont', DefaultTopicFixedFont));
      if FixedFont = nil then
        FixedFont := fpgStyle.FixedFont;

      for i := 0 to NumFontSettings - 1 do
      begin
        FontName := 'Font' + IntToStr( i );
        Fonts[ i ] := nil;
        if ReadBool( FontsSection, FontName + 'Customised', false ) then
          Fonts[ i ] := fpgGetFont(ReadString(FontsSection, FontName + 'Desc', DefaultTopicFont));
      end;

      FixedFontSubstitution := ReadBool( FontsSection, 'FixedFontSubstitution', true );
      FixedFontSubstitutes := ReadString( FontsSection, 'FixedFontSubstitutes', 'Mono-10' );

      // Index style
      SettingString := ReadString( GeneralSection, 'IndexStyle', 'Full' );
      if SameText( SettingString, 'FileOnly' ) then
        IndexStyle := isFileOnly
      else if Sametext( SettingString, 'Alphabetical' ) then
        IndexStyle := isAlphabetical
      else
        IndexStyle := isFull;

      StartupHelp := ReadBool( GeneralSection, 'StartupHelp', true );

      SmoothScrolling := ReadBool( GeneralSection, 'SmoothScrolling', true );
//      UseOriginalDialogs := ReadBool( GeneralSection, 'UseOriginalDialogs', false );
      OpenWithExpandedContents := ReadBool( GeneralSection, 'OpenWithExpandedContents', false );

//      ToolBarBackgroundImageFilename := ReadString( GeneralSection, 'ToolbarBackground', '' );
      SettingString := ReadString( GeneralSection, 'ToolbarStyle', 'ImagesAndText' );

      if SameText( SettingString, 'None' ) then
        ToolbarStyle := tsNone
      else if SameText( SettingString, 'Images' ) then
        ToolbarStyle := tsImages
      else if SameText( SettingString, 'Text' ) then
        ToolbarStyle := tsText
      else
        ToolbarStyle := tsImagesAndText;

      ConfirmWinHelp := ReadBool( GeneralSection, 'ConfirmWinHelp', true );
      IPFTopicSaveAsEscaped := ReadBool(GeneralSection, 'IPFTopicSaveAsEscaped', true);

      Count := ReadInteger( SearchSection, 'CustomDirCount', 0 );

      SearchDirectories.Clear;
      for i := 0 to Count - 1 do
      begin
        SettingString := ReadString( SearchSection,
                                     'CustomDir' + IntToStr( i ),
                                     '' );
        if trim( SettingString ) <> '' then
          SearchDirectories.Add( SettingString );
      end;
      SettingString := ReadString( SearchSection,
                                   'Location',
                                   'HelpPaths' );
      if SameText( SettingString, 'HelpPaths' ) then
        GlobalSearchLocation := gsHelpPaths
      else if SameText( SettingString, 'FixedDrives' ) then
        GlobalSearchLocation := gsFixedDrives
      else if SameText( SettingString, 'SelectedHelpPaths' ) then
        GlobalSearchLocation := gsSelectedHelpPaths
      else
        GlobalSearchLocation := gsCustom;

    end;
  end;
  LogEvent(LogSettings, ' Done' );
end;

procedure SaveSettings;
var
  ColorIndex: longint;
  FontIndex: longint;
  FontName: string;
  i: longint;
  MRUItemFileIndex: longint;
  SettingString: string;
  MRUItem: TMRUItem;
  MRUItemSection: string;
begin
  LogEvent(LogSettings, 'SaveSettings' );
  with gINI do
  begin
    with Settings do
    begin
      WriteString( GeneralSection, 'LastOpenDirectory', LastOpenDirectory );
      WriteString( GeneralSection, 'LastSaveDirectory', LastSaveDirectory );

      WriteInteger( GeneralSection, 'LeftPanelWidth', LeftPanelWidth );
      // Write split points, as units of 0.1%
      WriteInteger( GeneralSection, 'FileDialogSplit', Round( FileDialogSplit * 1000 ) );

      WriteBool( GeneralSection, 'ShowLeftPanel', ShowLeftPanel);

      // Colours
      for ColorIndex := 0 to High( Colors ) do
        WriteInteger( ColoursSection,
                      'Color' + IntToStr( ColorIndex ),
                      Colors[ ColorIndex ] );

      // MRU files
      WriteInteger( MRUSection, 'Count', MRUList.Count );
      for i := 0 to MRUList.Count - 1 do
      begin
        MRUItem := TMRUItem(MRUList[ i ]);
        MRUItemSection := MRUItemBaseSection + IntToStr( i );
        EraseSection( MRUItemSection );
        WriteString( MRUItemSection, 'Title', MRUItem.Title );
        WriteInteger( MRUItemSection, 'FileCount', MRUItem.Filenames.Count );
        for MRUItemFileIndex := 0 to MRUItem.Filenames.Count - 1 do
          WriteString( MRUItemSection,
                       'File' + IntToStr( MRUItemFileIndex ),
                       MRUItem.Filenames[ MRUItemFileIndex ] );
      end;

      // clear unused sections
      for i := MRUList.Count to MaxMRUListEntries do
      begin
        MRUItemSection := MRUItemBaseSection + IntToStr( i );
        EraseSection( MRUItemSection );
      end;

      // Fonts
      WriteString( FontsSection, 'NormalFont', NormalFont.FontDesc );
      WriteString( FontsSection, 'FixedFont', FixedFont.FontDesc );
      for FontIndex := 0 to NumFontSettings - 1 do
      begin
        FontName := 'Font' + IntToStr( FontIndex );
        WriteBool( FontsSection, FontName + 'Customised', Fonts[ FontIndex ] <> nil );
        if Fonts[ FontIndex ] <> nil then
          WriteString( FontsSection, FontName + 'Desc', Fonts[ FontIndex ].FontDesc );
      end;

      WriteBool( FontsSection, 'FixedFontSubstitution', FixedFontSubstitution );
      WriteString( FontsSection, 'FixedFontSubstitutes', FixedFontSubstitutes );

      case IndexStyle of
        isFileOnly:
          SettingString := 'FileOnly';
        isAlphabetical:
          SettingString := 'Alphabetical';
        isFull:
          SettingString := 'Full';
      end;
      WriteString( GeneralSection, 'IndexStyle', SettingString );

      WriteBool( GeneralSection, 'StartupHelp', StartupHelp );
      WriteBool( GeneralSection, 'SmoothScrolling', SmoothScrolling );
//      WriteBool( GeneralSection, 'UseOriginalDialogs', UseOriginalDialogs );
      WriteBool( GeneralSection, 'OpenWithExpandedContents', OpenWithExpandedContents );
//      WriteString( GeneralSection, 'ToolbarBackground', ToolbarBackgroundImageFilename );

      case ToolbarStyle of
        tsNone:
          SettingString := 'None';
        tsImages:
          SettingString := 'Images';
        tsText:
          SettingString := 'Text';
        tsImagesAndText:
          SettingString := 'ImagesAndText';
      end;

      WriteString( GeneralSection, 'ToolbarStyle', SettingString );
      WriteBool( GeneralSection, 'ConfirmWinHelp', ConfirmWinHelp );
      WriteBool( GeneralSection, 'IPFTopicSaveAsEscaped', IPFTopicSaveAsEscaped);
      WriteInteger( SearchSection, 'CustomDirCount', SearchDirectories.Count );

      SearchDirectories.Sorted := true;
      SearchDirectories.CaseSensitive := True;
      SearchDirectories.Duplicates := dupIgnore;

      for i := 0 to SearchDirectories.Count - 1 do
      begin
        WriteString( SearchSection,
                     'CustomDir' + IntToStr( i ),
                     SearchDirectories[ i ] );
      end;

      case GlobalSearchLocation of
        gsHelpPaths:
          SettingString := 'HelpPaths';

        gsFixedDrives:
          SettingString := 'FixedDrives';

        gsSelectedHelpPaths:
          SettingString := 'SelectedHelpPaths';

        gsCustom:
          SettingString := 'Custom';
      end;

      WriteString( SearchSection, 'Location', SettingString );
    end;
  end;
  LogEvent(LogSettings, ' Done' );
End;

Procedure AddToMRUList( const Title: string; Filenames: TStrings );
var
  MRUIndex: longint;
  PreviousMRUIndex: longint;
  MRUItem: TMRUItem;
begin
  PreviousMRUIndex := -1;
  for MRUIndex := 0 to Settings.MRUList.Count - 1 do
  begin
    MRUItem := TMRUItem(Settings.MRUList[ MRUIndex ]);
    if     ( MRUItem.Title = Title )
       and ( MRUItem.Filenames.Equals( Filenames ) ) then
    begin
      // found identical entry in the list already.
      PreviousMRUIndex := MRUIndex;
      break;
    end;
  end;

  if PreviousMRUIndex > -1 then
  begin
    // is already in list. Get instance so we can move it to the top of list
    MRUItem := TMRUItem(Settings.MRUList[PreviousMRUIndex]);
    Settings.MRUList.Extract(MRUItem);
  end
  else
  begin
    // not yet in list. Create new
    MRUItem := TMRUItem.Create;
    MRUItem.Title := Title;
    MRUItem.Filenames.Assign( Filenames );
  end;

  Settings.MRUList.Insert( 0, MRUItem );
  while Settings.MRUList.Count > MaxMRUListEntries do
  begin
    Settings.MRUList.Remove(Settings.MRUList.Last);
  end;
end;

procedure writeSettingsDetailsTo(aStrings : TStrings);
Begin
  aStrings.Add('');
  aStrings.Add('---- Settings ----');
  aStrings.Add('info: Screenwidth=' + IntToStr(fpgApplication.ScreenWidth));
  aStrings.Add('info: Screenheight=' + IntToStr(fpgApplication.ScreenHeight));
  aStrings.Add('info: dpi=' + IntToStr(fpgApplication.Screen_dpi));

  aStrings.Add('LastOpenDirectory: ' + Settings.LastOpenDirectory);
  aStrings.Add('LastSaveDirectory: ' + Settings.LastSaveDirectory);
  aStrings.Add('StartupHelp:       ' + boolToStr(Settings.StartupHelp));
  // LeftPanelWidth: longint;
  aStrings.Add('ShowLeftPanel: ' + boolToStr(Settings.ShowLeftPanel));
  // FileDialogSplit: real;
  // Colors: array[ 0..NumColorSettings - 1 ] of TColor;
  // NormalFont: TFont;
  // FixedFont: TFont;
  // Fonts: array[ 0..NumFontSettings - 1 ] of TFont;
  aStrings.Add('FixedFontSubstitution: ' + boolToStr(Settings.FixedFontSubstitution));
  aStrings.Add('FixedFontSubstitutes: ' + Settings.FixedFontSubstitutes);
  // IndexStyle: TIndexStyle;
  aStrings.Add('SmoothScrolling: ' + boolToStr(Settings.SmoothScrolling));
//  aStrings.Add('UseOriginalDialogs: ' + boolToStr(Settings.UseOriginalDialogs));
  aStrings.Add('OpenWithExpandedContents: ' + boolToStr(Settings.OpenWithExpandedContents));
//  aStrings.Add('ToolbarBackgroundImageFilename: ' + Settings.ToolbarBackgroundImageFilename);
  // ToolbarStyle: TToolbarStyle;
  aStrings.Add('ConfirmWinHelp: ' + boolToStr(Settings.ConfirmWinHelp));
  aStrings.Add('IPFTopicSaveAsEscaped: ' + BoolToStr(Settings.IPFTopicSaveAsEscaped));
  // GlobalSearchLocation: TGlobalSearchLocation;
  // SearchDirectories: TStringList;
end;


Initialization
  Settings.MRUList := TObjectList.Create;

  //Settings.NormalFont := fpgStyle.DefaultFont;
  //Settings.FixedFont := fpgStyle.FixedFont;
  //Settings.SearchDirectories := TStringList.Create;

Finalization
  Settings.NormalFont.Free;
  Settings.FixedFont.Free;
  Settings.SearchDirectories.Free;
  Settings.MRUList.Free;

End.