1 /*
2  *  "GEDKeeper", the personal genealogical database editor.
3  *  Copyright (C) 2009-2021 by Sergey V. Zhdanovskih.
4  *
5  *  This file is part of "GEDKeeper".
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 using System;
22 using System.ComponentModel;
23 using System.IO;
24 using BSLib.Design.Graphics;
25 using Eto.Drawing;
26 using Eto.Forms;
27 using GDModel;
28 using GKCore;
29 using GKCore.Controllers;
30 using GKCore.Interfaces;
31 using GKCore.MVP.Views;
32 using GKUI.Components;
33 
34 namespace GKUI.Forms
35 {
36     public partial class MediaViewerWin : CommonWindow, IMediaViewerWin
37     {
38         private readonly MediaViewerController fController;
39         private ITimer fTimer;
40         private Control fViewer;
41 
42         public GDMFileReferenceWithTitle FileRef
43         {
44             get { return fController.FileRef; }
45             set { fController.FileRef = value; }
46         }
47 
48         public GDMMultimediaRecord Multimedia
49         {
50             get { return fController.Multimedia; }
51             set { fController.Multimedia = value; }
52         }
53 
SetViewText(string text)54         public void SetViewText(string text)
55         {
56             try {
57                 TextArea txtBox = new TextArea();
58                 txtBox.ReadOnly = true;
59                 // FIXME: fix encoding! and test other!!!
60                 txtBox.Text = text;
61 
62                 SetViewControl(txtBox);
63             } catch (Exception ex) {
64                 Logger.WriteError("MediaViewerWin.SetViewText()", ex);
65             }
66         }
67 
SetViewRTF(string text)68         public void SetViewRTF(string text)
69         {
70             try {
71                 RichTextArea rtfBox = new RichTextArea();
72                 rtfBox.ReadOnly = true;
73                 rtfBox.Text = text;
74 
75                 SetViewControl(rtfBox);
76             } catch (Exception ex) {
77                 Logger.WriteError("MediaViewerWin.SetViewRTF()", ex);
78             }
79         }
80 
SetViewHTML(Stream stm)81         public void SetViewHTML(Stream stm)
82         {
83             try {
84                 var browser = new WebView();
85                 browser.LoadHtml(stm);
86 
87                 SetViewControl(browser);
88             } catch (Exception ex) {
89                 Logger.WriteError("MediaViewerWin.SetViewHTML()", ex);
90             }
91         }
92 
SetViewMedia(string mediaFile)93         public void SetViewMedia(string mediaFile)
94         {
95             var mediaPlayer = new MediaPlayer();
96             mediaPlayer.MediaFile = mediaFile;
97 
98             SetViewControl(mediaPlayer);
99         }
100 
SetViewImage(IImage img, GDMFileReferenceWithTitle fileRef)101         public void SetViewImage(IImage img, GDMFileReferenceWithTitle fileRef)
102         {
103             var imageCtl = new GKUI.Components.ImageView();
104             imageCtl.OpenImage(img);
105 
106             fController.ProcessPortraits(imageCtl, fileRef);
107 
108             fTimer = AppHost.Instance.CreateTimer(100.0f, InitViewer_Tick);
109             fTimer.Start();
110 
111             SetViewControl(imageCtl);
112         }
113 
DisposeViewControl()114         public void DisposeViewControl()
115         {
116             if (fViewer != null) fViewer.Dispose();
117         }
118 
SetViewControl(Control ctl)119         private void SetViewControl(Control ctl)
120         {
121             if (ctl == null) return;
122             fViewer = ctl;
123             fViewer.Size = new Size(1000, 600);
124             SetLocale();
125 
126             SuspendLayout();
127             Content = fViewer;
128             ResumeLayout();
129         }
130 
MediaViewerWin(IBaseWindow baseWin)131         public MediaViewerWin(IBaseWindow baseWin)
132         {
133             InitializeComponent();
134 
135             SetLocale();
136 
137             fController = new MediaViewerController(this);
138             fController.Init(baseWin);
139         }
140 
SetLocale()141         public override void SetLocale()
142         {
143             var localizable = fViewer as ILocalizable;
144             if (localizable != null) localizable.SetLocale();
145         }
146 
OnLoad(EventArgs e)147         protected override void OnLoad(EventArgs e)
148         {
149             base.OnLoad(e);
150 
151             if (fViewer != null) {
152                 fViewer.Focus();
153                 fViewer.Invalidate();
154 
155                 /*var imageViewer = fViewer as GKUI.Components.ImageView;
156                 if (imageViewer != null) {
157                     imageViewer.ZoomToFit();
158                 }*/
159             }
160         }
161 
162         // dirty temporary hack
InitViewer_Tick(object sender, EventArgs e)163         private void InitViewer_Tick(object sender, EventArgs e)
164         {
165             var imageViewer = fViewer as GKUI.Components.ImageView;
166             if (imageViewer != null && !imageViewer.Viewport.Size.IsEmpty) {
167                 imageViewer.ZoomToFit();
168                 fTimer.Stop();
169             }
170         }
171 
MediaViewerWin_KeyDown(object sender, KeyEventArgs e)172         private void MediaViewerWin_KeyDown(object sender, KeyEventArgs e)
173         {
174             if (e.Key == Keys.Escape) Close();
175         }
176 
MediaViewerWin_FormClosing(object sender, CancelEventArgs e)177         private void MediaViewerWin_FormClosing(object sender, CancelEventArgs e)
178         {
179             var mediaPlayer = fViewer as MediaPlayer;
180             if (mediaPlayer != null) {
181                 mediaPlayer.btnStop_Click(null, null);
182             }
183         }
184     }
185 }
186