Check-in [e56988475d]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Moved bindings between UI and preview engines from main form to TPreviewEngineUIManager class.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | feature/windows-preview-host
Files: files | file ages | folders
SHA1:e56988475da9f6a12b5e40270c1d8a84aa2dc540
User & Date: tinus 2015-02-10 17:18:54
Context
2015-02-10
18:12
Moved the display of shell images to their own form, as a preview engine. check-in: 49db1181bc user: tinus tags: feature/windows-preview-host
17:18
Moved bindings between UI and preview engines from main form to TPreviewEngineUIManager class. check-in: e56988475d user: tinus tags: feature/windows-preview-host
15:30
Added automatic selection of engine/renderer. check-in: 44b1a2692d user: tinus tags: feature/windows-preview-host
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ZPreview/src/Delphi/F_Main.dfm.

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
...
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    Top = 26
    Width = 816
    Height = 41
    Align = alTop
    TabOrder = 1
    Visible = False
    OnDblClick = pnlErrorDblClick
    ExplicitTop = 464
    object lblError: TLabel
      AlignWithMargins = True
      Left = 4
      Top = 4
      Width = 3
      Height = 13
      Align = alClient
      Alignment = taCenter
      ShowAccelChar = False
      WordWrap = True


    end
  end
  object acttbMain: TActionToolBar
    Left = 0
    Top = 0
    Width = 816
    Height = 26
................................................................................
    Width = 4
    Height = 438
    Align = alRight
    AutoSize = True
    Constraints.MaxWidth = 4
    DockSite = True
    TabOrder = 3
    ExplicitTop = 26
  end
  object ActionManager: TActionManager
    ActionBars = <
      item
        Items = <
          item
            Action = actRefresh







<




|
|




>
>







 







<







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
...
120
121
122
123
124
125
126

127
128
129
130
131
132
133
    Top = 26
    Width = 816
    Height = 41
    Align = alTop
    TabOrder = 1
    Visible = False
    OnDblClick = pnlErrorDblClick

    object lblError: TLabel
      AlignWithMargins = True
      Left = 4
      Top = 4
      Width = 808
      Height = 33
      Align = alClient
      Alignment = taCenter
      ShowAccelChar = False
      WordWrap = True
      ExplicitWidth = 3
      ExplicitHeight = 13
    end
  end
  object acttbMain: TActionToolBar
    Left = 0
    Top = 0
    Width = 816
    Height = 26
................................................................................
    Width = 4
    Height = 438
    Align = alRight
    AutoSize = True
    Constraints.MaxWidth = 4
    DockSite = True
    TabOrder = 3

  end
  object ActionManager: TActionManager
    ActionBars = <
      item
        Items = <
          item
            Action = actRefresh

Changes to ZPreview/src/Delphi/F_Main.pas.

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
..
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
...
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
...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
...
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
...
500
501
502
503
504
505
506

507
508
509
510
511
512
513
...
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  System.Generics.Collections,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.ComCtrls,
  Vcl.StdCtrls, Vcl.ToolWin, Vcl.ImgList, System.Actions, Vcl.ActnList,
  Vcl.ActnMan, Vcl.ActnCtrls, Vcl.PlatformDefaultStyleActnCtrls, Vcl.StdActns,
  {$IFDEF WIN32}FreeBitmap,{$ENDIF} WlxPluginWrapper, uHostPreview,
  U_ZaapSentinel2, U_PreviewEngine,
  M_Main;

type
  TfrmMain = class(TForm)
    tbcPreview: TTabControl;
    pbxPicture: TPaintBox;
    acttbMain: TActionToolBar;
................................................................................
    procedure actViewFullScreenExecute(Sender: TObject);
    procedure pnlErrorDblClick(Sender: TObject);
    procedure actFileLoadPluginAccept(Sender: TObject);
    procedure actViewTopmostUpdate(Sender: TObject);
    procedure actViewTopmostExecute(Sender: TObject);
    procedure actWindowMinimizeExecute(Sender: TObject);
    procedure actPreviewEngineExecute(Sender: TObject);
    procedure actSelectEngineExecute(Sender: TObject);
    procedure actSelectRendererExecute(Sender: TObject);
    procedure actSelectRendererUpdate(Sender: TObject);
    procedure actEnableEngineExecute(Sender: TObject);
    procedure actEngineAutoselectExecute(Sender: TObject);
  private
    { Private declarations }
//    FWatcher: TThread;
    FInitialized: Boolean;
    FEngineForms: TObjectList<TCustomPreviewEngineForm>;
    FEngineSelectors: TDictionary<TCustomPreviewEngineForm,TAction>;
    FRendererSelectors: TDictionary<TRenderer,TAction>;
    FEngineEnablers: TDictionary<TCustomPreviewEngineForm,TAction>;

    {$IFDEF WIN32}FFreeBitmap: TFreeWinBitmap;{$ENDIF}
    FWlxPlugin: TWlxPluginWrapper;
    FPreviewHost: THostPreviewHandler;
    FGraphic: TGraphic;

    procedure LoadSentinel;
................................................................................
{$R *.dfm}

type
  THostPreviewHandlerClass = class(THostPreviewHandler);

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmMain.FormCreate(Sender: TObject);
var
  FormClass: TCustomPreviewEngineFormClass;
  cltPreviewEngine: TActionClientItem;
  Form: TCustomPreviewEngineForm;
  actSelectEngine, actEnableEngine: TAction;
  EngineIndex, ImageIndex: integer;
  Item: TActionClientItem;
  Renderer: TRenderer;
  actSelectRenderer: TAction;
  ItemOffset: integer;
begin
  Application.OnException := ApplicationException;

  FEngineSelectors := TDictionary<TCustomPreviewEngineForm,TAction>.Create;
  FRendererSelectors := TDictionary<TRenderer,TAction>.Create;
  FEngineEnablers := TDictionary<TCustomPreviewEngineForm,TAction>.Create;

  FEngineForms := TObjectList<TCustomPreviewEngineForm>.Create(TComparer<TCustomPreviewEngineForm>.Construct(
                      function(const Left, Right: TCustomPreviewEngineForm): integer
                      begin
                        {$MESSAGE HINT 'TODO: sort engine forms -- MCO 2015-02-09'}
                        // TODO: look 'em up in some list, then base their order on that list.
                        // TODO: if they don't occur in the list, move them to the end
                        Result := 0;
                      end));
  for FormClass in TCustomPreviewEngineFormClass.RegisteredForms do begin
    Form := FormClass.Create(Self);
    FEngineForms.Add(Form);

    Form.Parent := tbcPreview;
    Form.Align := alClient;
    Form.BorderStyle := bsNone;
    Form.Enabled := True; // TODO: read from ini or something
  end;
  if FEngineForms.Count > 0 then begin
    FEngineForms.Sort;

    cltPreviewEngine := ActionManager.FindItemByAction(actPreviewEngine);
    ItemOffset := ActionManager.FindItemByAction(actEngineAutoselect).Index + 1;

    EngineIndex := 0;
    for Form in FEngineForms do begin
      if (Form.Icon = nil) or Form.Icon.Empty then
        ImageIndex := -1
      else
        ImageIndex := modMain.imlMain.AddIcon(Form.Icon);

      actSelectEngine := TAction.Create(Self);
      actSelectEngine.Caption := Form.Caption;
      actSelectEngine.Enabled := Form.Enabled; // TODO: dynamically follow when the form is enabled
      actSelectEngine.Hint := Form.Caption;
      actSelectEngine.ImageIndex := ImageIndex;
      if EngineIndex < 10 then
        actSelectEngine.ShortCut := scCtrl or scShift or VkKeyScan(IntToStr((EngineIndex + 1) mod 10)[1]);
      actSelectEngine.Tag := NativeInt(Form);
      actSelectEngine.Visible := True;
      actSelectEngine.OnExecute := actSelectEngineExecute;
      FEngineSelectors.Add(Form, actSelectEngine);

      Item := cltPreviewEngine.Items.Insert(ItemOffset + EngineIndex) as TActionClientItem;
      Item.Action := actSelectEngine;

      // Fill the list of renderers
      if Length(Form.Renderers) > 1 then begin
        with Item.Items.Add do begin
          Action := actSelectEngine;
          Caption := '(autoselect)';
          ImageIndex := -1;
        end;
        for Renderer in Form.Renderers do begin
          actSelectRenderer := TAction.Create(Self);
          actSelectRenderer.Caption := Renderer.DisplayName;
          actSelectRenderer.Enabled := Form.Enabled; // TODO: dynamically follow when the form is enabled
          actSelectRenderer.Tag := NativeInt(Renderer);
          actSelectRenderer.OnExecute := actSelectRendererExecute;
          actSelectRenderer.OnUpdate := actSelectRendererUpdate;
          FRendererSelectors.Add(Renderer, actSelectRenderer);
          with Item.Items.Add do begin
            Action := actSelectRenderer;
          end;
        end;
      end {for};


      {--- MCO 10-02-2015: Create a toolbar button to enable/disable this engine ---}
      actEnableEngine := TAction.Create(Self);
      actEnableEngine.AutoCheck := True;
      actEnableEngine.Caption := Form.Caption;
      actEnableEngine.Checked := Form.Enabled;
      actEnableEngine.Hint := 'Use ' + Form.Caption + '|Toggle the use of "' + Form.Caption + '" for previewing files';
      actEnableEngine.ImageIndex := ImageIndex;
      if EngineIndex < 10 then
        actEnableEngine.ShortCut := scCtrl or VkKeyScan(IntToStr((EngineIndex + 1) mod 10)[1]);
      actEnableEngine.Tag := NativeInt(Form);
      actEnableEngine.Visible := True;
      actEnableEngine.OnExecute := actEnableEngineExecute;

      Item := ActionManager.ActionBars[0].Items.Add;
      Item.Action := actEnableEngine;
      Item.ImageIndex := ImageIndex;
      Item.ShowGlyph := True;
      Item.ShowCaption := False;
      Item.Visible := True;

      Inc(EngineIndex);
    end;
  end;
end {TfrmMain.FormCreate};

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  if Assigned(FGraphic) then
    FGraphic.Free;
................................................................................
    FWlxPlugin.Free;
{$IFDEF WIN32}
  if Assigned(FFreeBitmap) then
    FFreeBitmap.Free;
{$ENDIF}
  FSentinel.Free;

  FEngineSelectors.Free;
  FRendererSelectors.Free;
  FEngineEnablers.Free;
  FEngineForms.Free;
end {TfrmMain.FormDestroy};

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  actAutorefresh.Checked := False;
  Application.OnException := nil;
................................................................................
          Close;
        end;
        'R': begin
          FSentinel.Reset;
          FInitialized := False;
        end;
        else begin
          Shortcut := scCtrl + scShift + Ord(FSentinel.Pamphlet.CommandChar);
          for i := 0 to ActionManager.ActionCount - 1 do begin
            if (ActionManager.Actions[i].ShortCut = Shortcut) or (ActionManager.Actions[i].SecondaryShortCuts.IndexOfShortCut(Shortcut) > -1) then begin
              ActionManager.Actions[i].Execute;
              // DIRTY HACK: a HelpContext of -1 means we shouldn't process the pamphlet after having processed this action
              if ActionManager.Actions[i].HelpContext = -1 then
                Exit
              else
................................................................................
      end {case CommandChar};
    end {if};
  end {if};

  tbcPreview.Tabs[tbcPreview.TabIndex] := ExtractFileName(FSentinel.ThisPath);

  {$MESSAGE WARN 'TODO: Rewrite this, harness the new preview engine structure — MCO 10-02-2015'}


  pnlError.Hide;
  try
{$IFDEF WIN32}
    if Assigned(FFreeBitmap) then
      FreeAndNil(FFreeBitmap);
{$ENDIF}
................................................................................
  finally
//    actAutoRefresh.Checked := Assigned(FWatcher);
//    actRefresh.Enabled := not actAutoRefresh.Checked;
  end;
end {TfrmMain.chkAutoRefreshClick};

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmMain.actSelectEngineExecute(Sender: TObject);
var
  actSelectEngine: TAction;
  Form: TCustomPreviewEngineForm;
  Renderer: TRenderer;
  actSelectRenderer: TAction;
begin
  actSelectEngine := Sender as TAction;
  Form := TObject(actSelectEngine.Tag) as TCustomPreviewEngineForm;

  if Form.TryShowFile(FSentinel.ThisPath, Renderer) then begin
    Form.Show;

    {$MESSAGE HINT 'TODO: UIEngineManager.SetSelectedRenderer — MCO 10-02-2015'}
    // Update the menu item for the engine
    actPreviewEngine.ImageIndex := actSelectEngine.ImageIndex;
    ActionManager.FindItemByAction(actSelectEngine).Default := True;
    // Update the menu item for the renderer
    actSelectRenderer := FRendererSelectors.Items[Renderer];
    ActionManager.FindItemByAction(actSelectRenderer).Default := True;
  end else begin
    Form.Hide;

    {$MESSAGE HINT 'TODO: Select another one — MCO 10-02-2015'}

    actPreviewEngine.ImageIndex := actPreviewEngine.Tag;
  end;
end {TfrmMain.actSelectEngineExecute};

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmMain.actSelectRendererExecute(Sender: TObject);
var
  actSelectRenderer: TAction;
  Renderer: TRenderer;
  Form: TCustomPreviewEngineForm;
  actSelectEngine: TAction;
begin
  actSelectRenderer := Sender as TAction;
  Renderer := TObject(actSelectRenderer.Tag) as TRenderer;
  Form := Renderer.Form;

  if Renderer.TryShowFile(FSentinel.ThisPath) then begin
    Form.Show;

    // Mark its menu item as default (bold)
    {$MESSAGE HINT 'TODO: UIEngineManager.SetSelectedRenderer — MCO 10-02-2015'}
    ActionManager.FindItemByAction(actSelectRenderer).Default := True;

    // Mark the engine's menu item as default (bold) as well
    actSelectEngine := FEngineSelectors.Items[Form];
    actPreviewEngine.ImageIndex := actSelectEngine.ImageIndex;
    ActionManager.FindItemByAction(actSelectEngine).Default := True;
  end else begin
    {$MESSAGE HINT 'TODO: What to do when the renderer couldn’t render? — MCO 10-02-2015'}
    // TODO:
  end;
end {TfrmMain.actSelectRendererExecute};

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmMain.actSelectRendererUpdate(Sender: TObject);
var
  actSelectRenderer: TAction;
  Renderer: TRenderer;
  Form: TCustomPreviewEngineForm;
begin
  actSelectRenderer := Sender as TAction;
  Renderer := TObject(actSelectRenderer.Tag) as TRenderer;
  Form := Renderer.Form;

  actSelectRenderer.Enabled := Form.Enabled;
  if actSelectRenderer.Enabled then begin
    actSelectRenderer.Checked := Renderer.CouldShowFile(FSentinel.ThisPath);
  end;
end {TfrmMain.actSelectRendererUpdate};

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmMain.actEnableEngineExecute(Sender: TObject);
var
  actEnableEngine: TAction;
  Form: TCustomPreviewEngineForm;
  Renderer: TRenderer;
  actSelectEngine, actSelectRenderer: TAction;
begin
  actEnableEngine := Sender as TAction;
  Form := TObject(actEnableEngine.Tag) as TCustomPreviewEngineForm;

  Form.Enabled := actEnableEngine.Checked;
  if Form.Enabled and (Length(Form.CouldShowFile(FSentinel.ThisPath)) > 0) and Form.TryShowFile(FSentinel.ThisPath, Renderer) then begin
    Form.Show;

    {$MESSAGE HINT 'TODO: UIEngineManager.SetSelectedRenderer — MCO 10-02-2015'}
    actSelectEngine := FEngineSelectors.Items[Form];
    with ActionManager.FindItemByAction(actSelectEngine) do begin
      actPreviewEngine.ImageIndex := ImageIndex;
      Default := True;
    end;
    actSelectRenderer := FRendererSelectors.Items[Renderer];
    actSelectRenderer.Enabled := Form.Enabled;
    ActionManager.FindItemByAction(actSelectRenderer).Default := True;
  end else if not Form.Enabled then begin
    Form.Hide;
    // automatically select the (next) engine to display the current file
    actEngineAutoselect.Execute;

  end;
end {TfrmMain.actEnableEngineExecute};

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmMain.actEngineAutoselectExecute(Sender: TObject);
var
  Form: TCustomPreviewEngineForm;
  Renderer: TRenderer;
  actSelectEngine: TAction;
begin
  actPreviewEngine.ImageIndex := actPreviewEngine.Tag;
  actPreviewEngine.Hint := '';

  for Form in FEngineForms do begin
    if Form.Enabled and Form.TryShowFile(FSentinel.ThisPath, Renderer) then begin
      Form.Show;
      {$MESSAGE HINT 'TODO: UIEngineManager.SetSelectedRenderer — MCO 10-02-2015'}
      actSelectEngine := FEngineSelectors.Items[Form];
      actPreviewEngine.ImageIndex := actSelectEngine.ImageIndex;
      actPreviewEngine.Hint := Form.Caption + '/' + Renderer.DisplayName;
      ActionManager.FindItemByAction(actPreviewEngine).Action := actPreviewEngine;
      ActionManager.FindItemByAction(actSelectEngine).Default := True;
      ActionManager.FindItemByAction(FRendererSelectors.Items[Renderer]).Default := True;
      Break;
    end else begin
      Form.Hide;
    end;
  end {for};
end {TfrmMain.actEngineAutoselectExecute};

end.







|







 







<
<
<
<





|
<
<
<







 







<
<
<
<
<
<
<
<
<
<



<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>
|
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







<
<
<
|







 







|







 







>







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

<
<
<
<

<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
..
57
58
59
60
61
62
63




64
65
66
67
68
69



70
71
72
73
74
75
76
..
95
96
97
98
99
100
101










102
103
104















105
106







107





































































108
109
110
111
112
113
114
...
118
119
120
121
122
123
124



125
126
127
128
129
130
131
132
...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
...
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
...
620
621
622
623
624
625
626












































































































627




628




629













630
631
632
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  System.Generics.Collections,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.ComCtrls,
  Vcl.StdCtrls, Vcl.ToolWin, Vcl.ImgList, System.Actions, Vcl.ActnList,
  Vcl.ActnMan, Vcl.ActnCtrls, Vcl.PlatformDefaultStyleActnCtrls, Vcl.StdActns,
  {$IFDEF WIN32}FreeBitmap,{$ENDIF} WlxPluginWrapper, uHostPreview,
  U_ZaapSentinel2, U_PreviewEngine, U_PreviewEngineUIManager,
  M_Main;

type
  TfrmMain = class(TForm)
    tbcPreview: TTabControl;
    pbxPicture: TPaintBox;
    acttbMain: TActionToolBar;
................................................................................
    procedure actViewFullScreenExecute(Sender: TObject);
    procedure pnlErrorDblClick(Sender: TObject);
    procedure actFileLoadPluginAccept(Sender: TObject);
    procedure actViewTopmostUpdate(Sender: TObject);
    procedure actViewTopmostExecute(Sender: TObject);
    procedure actWindowMinimizeExecute(Sender: TObject);
    procedure actPreviewEngineExecute(Sender: TObject);




    procedure actEngineAutoselectExecute(Sender: TObject);
  private
    { Private declarations }
//    FWatcher: TThread;
    FInitialized: Boolean;
    FEngineManager: TPreviewEngineUIManager;




    {$IFDEF WIN32}FFreeBitmap: TFreeWinBitmap;{$ENDIF}
    FWlxPlugin: TWlxPluginWrapper;
    FPreviewHost: THostPreviewHandler;
    FGraphic: TGraphic;

    procedure LoadSentinel;
................................................................................
{$R *.dfm}

type
  THostPreviewHandlerClass = class(THostPreviewHandler);

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmMain.FormCreate(Sender: TObject);










begin
  Application.OnException := ApplicationException;
















  FEngineManager := TPreviewEngineUIManager.Create(ActionManager,
                                                   tbcPreview,







                                                   actPreviewEngine);





































































end {TfrmMain.FormCreate};

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  if Assigned(FGraphic) then
    FGraphic.Free;
................................................................................
    FWlxPlugin.Free;
{$IFDEF WIN32}
  if Assigned(FFreeBitmap) then
    FFreeBitmap.Free;
{$ENDIF}
  FSentinel.Free;




  FEngineManager.Free;
end {TfrmMain.FormDestroy};

{ ------------------------------------------------------------------------------------------------ }
procedure TfrmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  actAutorefresh.Checked := False;
  Application.OnException := nil;
................................................................................
          Close;
        end;
        'R': begin
          FSentinel.Reset;
          FInitialized := False;
        end;
        else begin
          Shortcut := scCtrl + scShift + VkKeyScanA(FSentinel.Pamphlet.CommandChar);
          for i := 0 to ActionManager.ActionCount - 1 do begin
            if (ActionManager.Actions[i].ShortCut = Shortcut) or (ActionManager.Actions[i].SecondaryShortCuts.IndexOfShortCut(Shortcut) > -1) then begin
              ActionManager.Actions[i].Execute;
              // DIRTY HACK: a HelpContext of -1 means we shouldn't process the pamphlet after having processed this action
              if ActionManager.Actions[i].HelpContext = -1 then
                Exit
              else
................................................................................
      end {case CommandChar};
    end {if};
  end {if};

  tbcPreview.Tabs[tbcPreview.TabIndex] := ExtractFileName(FSentinel.ThisPath);

  {$MESSAGE WARN 'TODO: Rewrite this, harness the new preview engine structure — MCO 10-02-2015'}
  FEngineManager.TryShowFile(FSentinel.ThisPath);

  pnlError.Hide;
  try
{$IFDEF WIN32}
    if Assigned(FFreeBitmap) then
      FreeAndNil(FFreeBitmap);
{$ENDIF}
................................................................................
  finally
//    actAutoRefresh.Checked := Assigned(FWatcher);
//    actRefresh.Enabled := not actAutoRefresh.Checked;
  end;
end {TfrmMain.chkAutoRefreshClick};

{ ------------------------------------------------------------------------------------------------ }












































































































procedure TfrmMain.actEngineAutoselectExecute(Sender: TObject);




begin




  FEngineManager.TryShowFile(FSentinel.ThisPath);













end {TfrmMain.actEngineAutoselectExecute};

end.

Added ZPreview/src/Delphi/U_PreviewEngineUIManager.pas.

































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
unit U_PreviewEngineUIManager;

interface

uses
  System.Generics.Collections,
  Vcl.Controls, Vcl.ActnMan, Vcl.ActnList,
  U_PreviewEngine;

type
  TPreviewEngineUIManager = class
  private
    FActionMgr: TActionManager;
    FContainer: TWinControl;
    FActionPreviewEngine: TAction;

    FEngineForms: TObjectList<TCustomPreviewEngineForm>;
    FEngineSelectors: TDictionary<TCustomPreviewEngineForm,TAction>;
    FRendererSelectors: TDictionary<TRenderer,TAction>;
    FEngineEnablers: TDictionary<TCustomPreviewEngineForm,TAction>;

    FFileName: string;
    FSelectedRenderer: TRenderer;

    procedure InitializeEngines;
    procedure InitializeUI;

    procedure actSelectEngineExecute(Sender: TObject);
    procedure actSelectRendererExecute(Sender: TObject);
    procedure actSelectRendererUpdate(Sender: TObject);
    procedure actEnableEngineExecute(Sender: TObject);

    procedure SetSelectedRenderer(const Renderer: TRenderer);
  public
    constructor Create(const ActionManager: TActionManager;
                       const Container: TWinControl;
                       const ActionPreviewEngine: TAction);
    destructor  Destroy; override;

    function TryShowFile(const FileName: string): TRenderer;

    property ActionManager: TActionManager  read FActionMgr;
    property Container: TWinControl         read FContainer;

    property Filename: string               read FFileName;
  end;

implementation

uses
  System.SysUtils, System.Classes, System.Generics.Defaults,
  Winapi.Windows,
  Vcl.Forms;

{ ================================================================================================ }
{ TUIPreviewEngineManager }

{ ------------------------------------------------------------------------------------------------ }
constructor TPreviewEngineUIManager.Create(const ActionManager: TActionManager;
                                           const Container: TWinControl;
                                           const ActionPreviewEngine: TAction);
begin
  inherited Create;
  FEngineForms := TObjectList<TCustomPreviewEngineForm>.Create;

  FActionMgr := ActionManager;
  FActionPreviewEngine := ActionPreviewEngine;
  FContainer := Container;

  FEngineSelectors := TDictionary<TCustomPreviewEngineForm,TAction>.Create;
  FRendererSelectors := TDictionary<TRenderer,TAction>.Create;
  FEngineEnablers := TDictionary<TCustomPreviewEngineForm,TAction>.Create;

  InitializeEngines;
  InitializeUI;
end {TUIPreviewEngineManager.Create};
{ ------------------------------------------------------------------------------------------------ }
destructor TPreviewEngineUIManager.Destroy;
begin
  FEngineEnablers.Free;
  FRendererSelectors.Free;
  FEngineSelectors.Free;
  FEngineForms.Free;

  inherited;
end {TUIPreviewEngineManager.Destroy};

{ ------------------------------------------------------------------------------------------------ }
procedure TPreviewEngineUIManager.InitializeEngines;
var
  FormClass: TCustomPreviewEngineFormClass;
  Form: TCustomPreviewEngineForm;
begin
  FEngineForms := TObjectList<TCustomPreviewEngineForm>.Create(TComparer<TCustomPreviewEngineForm>.Construct(
                      function(const Left, Right: TCustomPreviewEngineForm): integer
                      begin
                        {$MESSAGE HINT 'TODO: sort engine forms -- MCO 2015-02-09'}
                        // TODO: look 'em up in some list, then base their order on that list.
                        // TODO: if they don't occur in the list, move them to the end
                        Result := 0;
                      end));
  for FormClass in TCustomPreviewEngineFormClass.RegisteredForms do begin
    Form := FormClass.Create(nil);
    Form.Parent := FContainer;
    Form.Align := alClient;
    Form.BorderStyle := bsNone;
    Form.Enabled := True; // TODO: read from ini or something
    FEngineForms.Add(Form);
  end;
end {TUIPreviewEngineManager.InitializeEngines};

{ ------------------------------------------------------------------------------------------------ }
procedure TPreviewEngineUIManager.InitializeUI;
var
  ItemPreviewEngine: TActionClientItem;
  Form: TCustomPreviewEngineForm;
  actSelectEngine, actEnableEngine: TAction;
  EngineIndex, ImageIndex: integer;
  Item: TActionClientItem;
  Renderer: TRenderer;
  actSelectRenderer: TAction;
  ItemOffset: integer;
begin
  if FEngineForms.Count <= 0 then
    Exit;

  ItemPreviewEngine := ActionManager.FindItemByAction(FActionPreviewEngine);
  ItemOffset := 1;

  EngineIndex := 0;
  for Form in FEngineForms do begin
    if (Form.Icon = nil) or Form.Icon.Empty then
      ImageIndex := -1
    else
      ImageIndex := ActionManager.Images.AddIcon(Form.Icon);

    actSelectEngine := TAction.Create(ActionManager);
    actSelectEngine.Caption := Form.Caption;
    actSelectEngine.Enabled := Form.Enabled; // TODO: dynamically follow when the form is enabled
    actSelectEngine.Hint := Form.Caption;
    actSelectEngine.ImageIndex := ImageIndex;
    if EngineIndex < 10 then
      actSelectEngine.ShortCut := scCtrl or scShift or VkKeyScan(IntToStr((EngineIndex + 1) mod 10)[1]);
    actSelectEngine.Tag := NativeInt(Form);
    actSelectEngine.Visible := True;
    actSelectEngine.OnExecute := actSelectEngineExecute;
    FEngineSelectors.Add(Form, actSelectEngine);

    Item := ItemPreviewEngine.Items.Insert(ItemOffset + EngineIndex) as TActionClientItem;
    Item.Action := actSelectEngine;

    // Fill the list of renderers
    if Length(Form.Renderers) > 1 then begin
      with Item.Items.Add do begin
        Action := actSelectEngine;
        Caption := '(autoselect)';
        ImageIndex := -1;
      end;
      for Renderer in Form.Renderers do begin
        actSelectRenderer := TAction.Create(ActionManager);
        actSelectRenderer.Caption := Renderer.DisplayName;
        actSelectRenderer.Enabled := Form.Enabled; // TODO: dynamically follow when the form is enabled
        actSelectRenderer.Tag := NativeInt(Renderer);
        actSelectRenderer.OnExecute := actSelectRendererExecute;
        actSelectRenderer.OnUpdate := actSelectRendererUpdate;
        FRendererSelectors.Add(Renderer, actSelectRenderer);
        with Item.Items.Add do begin
          Action := actSelectRenderer;
        end;
      end;
    end {for};

    {--- MCO 10-02-2015: Create a toolbar button to enable/disable this engine ---}
    actEnableEngine := TAction.Create(ActionManager);
    actEnableEngine.AutoCheck := True;
    actEnableEngine.Caption := Form.Caption;
    actEnableEngine.Checked := Form.Enabled;
    actEnableEngine.Hint := Format('Use %0:s|Toggle the use of %0:s for previewing files', [Form.Caption]);
    actEnableEngine.ImageIndex := ImageIndex;
    if EngineIndex < 10 then
      actEnableEngine.ShortCut := scCtrl or VkKeyScan(IntToStr((EngineIndex + 1) mod 10)[1]);
    actEnableEngine.Tag := NativeInt(Form);
    actEnableEngine.Visible := True;
    actEnableEngine.OnExecute := actEnableEngineExecute;

    Item := ActionManager.ActionBars[0].Items.Add;
    Item.Action := actEnableEngine;
    Item.ImageIndex := ImageIndex;
    Item.ShowGlyph := True;
    Item.ShowCaption := False;
    Item.Visible := True;

    Inc(EngineIndex);
  end {for};
end {TUIPreviewEngineManager.InitializeUI};

{ ------------------------------------------------------------------------------------------------ }
function TPreviewEngineUIManager.TryShowFile(const FileName: string): TRenderer;
var
  Form: TCustomPreviewEngineForm;
begin
  FFileName := FileName;

  FActionPreviewEngine.ImageIndex := FActionPreviewEngine.Tag;
  FActionPreviewEngine.Hint := '';

  Result := nil;
  for Form in FEngineForms do begin
    if (Result = nil) and Form.Enabled and Form.TryShowFile(Filename, Result) then begin
      Form.Show;
      SetSelectedRenderer(Result);
    end else begin
      Form.Hide;
    end;
  end {for};
end {TUIPreviewEngineManager.TryShowFile};

{ ------------------------------------------------------------------------------------------------ }
procedure TPreviewEngineUIManager.SetSelectedRenderer(const Renderer: TRenderer);
var
  actSelectEngine, actSelectRenderer: TAction;
begin
  FSelectedRenderer := Renderer;

  // Update the menu item for the engine
  actSelectEngine := FEngineSelectors.Items[Renderer.Form];
  ActionManager.FindItemByAction(actSelectEngine).Default := True;
  // Update the menu item for the renderer
  actSelectRenderer := FRendererSelectors.Items[Renderer];
  ActionManager.FindItemByAction(actSelectRenderer).Default := True;

  FActionPreviewEngine.Hint := Format('%s/%s', [Renderer.Form.Caption, Renderer.DisplayName]);
  FActionPreviewEngine.ImageIndex := actSelectEngine.ImageIndex;
end {TUIPreviewEngineManager.SetSelectedRenderer};

{ ------------------------------------------------------------------------------------------------ }
procedure TPreviewEngineUIManager.actEnableEngineExecute(Sender: TObject);
var
  actEnableEngine: TAction;
  Form: TCustomPreviewEngineForm;
  Renderer: TRenderer;
begin
  actEnableEngine := Sender as TAction;
  Form := TObject(actEnableEngine.Tag) as TCustomPreviewEngineForm;

  Form.Enabled := actEnableEngine.Checked;
  if Form.Enabled
      and (Length(Form.CouldShowFile(FFileName)) > 0)
      and Form.TryShowFile(FFileName, Renderer)
  then begin
    Form.Show;
    SetSelectedRenderer(Renderer);
  end else if not Form.Enabled then begin
    Form.Hide;
    // automatically select the (next) engine to display the current file
    TryShowFile(FFileName);
  end;
end {TUIPreviewEngineManager.actEnableEngineExecute};

{ ------------------------------------------------------------------------------------------------ }
procedure TPreviewEngineUIManager.actSelectEngineExecute(Sender: TObject);
var
  actSelectEngine: TAction;
  Form: TCustomPreviewEngineForm;
  Renderer: TRenderer;
  actSelectRenderer: TAction;
begin
  actSelectEngine := Sender as TAction;
  Form := TObject(actSelectEngine.Tag) as TCustomPreviewEngineForm;

  if Form.TryShowFile(FFileName, Renderer) then begin
    Form.Show;
    SetSelectedRenderer(Renderer);
  end else begin
    Form.Hide;
    {$MESSAGE HINT 'TODO: What to do when the engine can’t render this file? — MCO 10-02-2015'}
    TryShowFile(FFileName);
  end;
end {TUIPreviewEngineManager.actSelectEngineExecute};

{ ------------------------------------------------------------------------------------------------ }
procedure TPreviewEngineUIManager.actSelectRendererExecute(Sender: TObject);
var
  actSelectRenderer: TAction;
  Renderer: TRenderer;
  Form: TCustomPreviewEngineForm;
  actSelectEngine: TAction;
begin
  actSelectRenderer := Sender as TAction;
  Renderer := TObject(actSelectRenderer.Tag) as TRenderer;
  Form := Renderer.Form;

  if Renderer.TryShowFile(FFileName) then begin
    Form.Show;
    SetSelectedRenderer(Renderer);
  end else begin
    {$MESSAGE HINT 'TODO: What to do when the renderer couldn’t render? — MCO 10-02-2015'}
  end;
end {TUIPreviewEngineManager.actSelectRendererExecute};

{ ------------------------------------------------------------------------------------------------ }
procedure TPreviewEngineUIManager.actSelectRendererUpdate(Sender: TObject);
var
  actSelectRenderer: TAction;
  Renderer: TRenderer;
  Form: TCustomPreviewEngineForm;
begin
  actSelectRenderer := Sender as TAction;
  Renderer := TObject(actSelectRenderer.Tag) as TRenderer;
  Form := Renderer.Form;

  actSelectRenderer.Enabled := (FFileName <> '') and Form.Enabled;
  if actSelectRenderer.Enabled then begin
    actSelectRenderer.Checked := Renderer.CouldShowFile(FFileName);
  end;
end {TUIPreviewEngineManager.actSelectRendererUpdate};



end.

Changes to ZPreview/src/Delphi/ZPreview.dpr.

2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
17
18
19
20
21
22

uses
  Vcl.Forms,
  F_Main in 'F_Main.pas' {frmMain},
  F_Sentinel in 'F_Sentinel.pas' {frmInfoSentinel},
  F_Info in 'F_Info.pas' {frmInfo},
  M_Main in 'M_Main.pas' {modMain: TDataModule},
  U_PreviewEngine in 'U_PreviewEngine.pas';


{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.Title := 'ZPreview';
  Application.CreateForm(TmodMain, modMain);
  Application.CreateForm(TfrmMain, frmMain);
  Application.CreateForm(TfrmInfoSentinel, frmInfoSentinel);
  Application.CreateForm(TfrmInfo, frmInfo);
  Application.Run;
end.







|
>













2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

uses
  Vcl.Forms,
  F_Main in 'F_Main.pas' {frmMain},
  F_Sentinel in 'F_Sentinel.pas' {frmInfoSentinel},
  F_Info in 'F_Info.pas' {frmInfo},
  M_Main in 'M_Main.pas' {modMain: TDataModule},
  U_PreviewEngine in 'U_PreviewEngine.pas',
  U_PreviewEngineUIManager in 'U_PreviewEngineUIManager.pas';

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.Title := 'ZPreview';
  Application.CreateForm(TmodMain, modMain);
  Application.CreateForm(TfrmMain, frmMain);
  Application.CreateForm(TfrmInfoSentinel, frmInfoSentinel);
  Application.CreateForm(TfrmInfo, frmInfo);
  Application.Run;
end.

Changes to ZPreview/src/Delphi/ZPreview.dproj.

101
102
103
104
105
106
107

108
109
110
111
112
113
114
...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
            <Form>frmInfo</Form>
        </DCCReference>
        <DCCReference Include="M_Main.pas">
            <Form>modMain</Form>
            <DesignClass>TDataModule</DesignClass>
        </DCCReference>
        <DCCReference Include="U_PreviewEngine.pas"/>

        <BuildConfiguration Include="Release">
            <Key>Cfg_2</Key>
            <CfgParent>Base</CfgParent>
        </BuildConfiguration>
        <BuildConfiguration Include="Base">
            <Key>Base</Key>
        </BuildConfiguration>
................................................................................
                    <VersionInfoKeys Name="restoreAnyVersion"/>
                    <VersionInfoKeys Name="installLocation"/>
                    <VersionInfoKeys Name="largeHeap"/>
                    <VersionInfoKeys Name="theme"/>
                </VersionInfoKeys>
            </Delphi.Personality>
            <Deployment>
                <DeployFile LocalName="Win32\Debug\ZPreview.32.exe" Configuration="Debug" Class="ProjectOutput">
                    <Platform Name="Win32">
                        <RemoteName>ZPreview.exe</RemoteName>
                        <Overwrite>true</Overwrite>
                    </Platform>
                </DeployFile>
                <DeployFile LocalName="Win32\Debug\ZPreview.exe" Configuration="Debug" Class="ProjectOutput">
                    <Platform Name="Win32">
                        <RemoteName>ZPreview.exe</RemoteName>
                        <Overwrite>true</Overwrite>
                    </Platform>
                </DeployFile>
                <DeployClass Required="true" Name="DependencyPackage">
                    <Platform Name="iOSDevice">







>







 







|





|







101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
            <Form>frmInfo</Form>
        </DCCReference>
        <DCCReference Include="M_Main.pas">
            <Form>modMain</Form>
            <DesignClass>TDataModule</DesignClass>
        </DCCReference>
        <DCCReference Include="U_PreviewEngine.pas"/>
        <DCCReference Include="U_PreviewEngineUIManager.pas"/>
        <BuildConfiguration Include="Release">
            <Key>Cfg_2</Key>
            <CfgParent>Base</CfgParent>
        </BuildConfiguration>
        <BuildConfiguration Include="Base">
            <Key>Base</Key>
        </BuildConfiguration>
................................................................................
                    <VersionInfoKeys Name="restoreAnyVersion"/>
                    <VersionInfoKeys Name="installLocation"/>
                    <VersionInfoKeys Name="largeHeap"/>
                    <VersionInfoKeys Name="theme"/>
                </VersionInfoKeys>
            </Delphi.Personality>
            <Deployment>
                <DeployFile LocalName="Win32\Debug\ZPreview.exe" Configuration="Debug" Class="ProjectOutput">
                    <Platform Name="Win32">
                        <RemoteName>ZPreview.exe</RemoteName>
                        <Overwrite>true</Overwrite>
                    </Platform>
                </DeployFile>
                <DeployFile LocalName="Win32\Debug\ZPreview.32.exe" Configuration="Debug" Class="ProjectOutput">
                    <Platform Name="Win32">
                        <RemoteName>ZPreview.exe</RemoteName>
                        <Overwrite>true</Overwrite>
                    </Platform>
                </DeployFile>
                <DeployClass Required="true" Name="DependencyPackage">
                    <Platform Name="iOSDevice">