OSDN Git Service

Merge branch 'feature-videotype'
[opentween/open-tween.git] / OpenTween / OTPictureBox.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Runtime.InteropServices;
26 using System.Text;
27 using System.Windows.Forms;
28 using System.ComponentModel;
29 using System.Drawing;
30 using System.Threading.Tasks;
31 using System.Threading;
32 using System.Net;
33 using System.Net.Http;
34 using System.IO;
35 using OpenTween.Thumbnail;
36
37 namespace OpenTween
38 {
39     public class OTPictureBox : PictureBox
40     {
41         [Browsable(false)]
42         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
43         public new MemoryImage Image
44         {
45             get { return this.memoryImage; }
46             set
47             {
48                 this.memoryImage = value;
49                 base.Image = value != null ? value.Image : null;
50
51                 this.RestoreSizeMode();
52             }
53         }
54         private MemoryImage memoryImage;
55
56         [Localizable(true)]
57         [DefaultValue(PictureBoxSizeMode.Normal)]
58         public new PictureBoxSizeMode SizeMode
59         {
60             get { return this.currentSizeMode; }
61             set
62             {
63                 this.currentSizeMode = value;
64
65                 if (base.Image != base.InitialImage && base.Image != base.ErrorImage)
66                 {
67                     base.SizeMode = value;
68                 }
69             }
70         }
71
72         /// <summary>
73         /// InitialImage や ErrorImage の表示に SizeMode を一時的に変更するため、
74         /// 現在の SizeMode を記憶しておくためのフィールド
75         /// </summary>
76         private PictureBoxSizeMode currentSizeMode;
77
78         public void ShowInitialImage()
79         {
80             base.Image = base.InitialImage;
81
82             // InitialImage は SizeMode の値に依らず中央等倍に表示する必要がある
83             base.SizeMode = PictureBoxSizeMode.CenterImage;
84         }
85
86         public void ShowErrorImage()
87         {
88             base.Image = base.ErrorImage;
89
90             // ErrorImage は SizeMode の値に依らず中央等倍に表示する必要がある
91             base.SizeMode = PictureBoxSizeMode.CenterImage;
92         }
93
94         private void RestoreSizeMode()
95         {
96             base.SizeMode = this.currentSizeMode;
97         }
98
99         public async Task SetImageFromTask(Func<Task<MemoryImage>> imageTask)
100         {
101             try
102             {
103                 this.ShowInitialImage();
104                 this.Image = await imageTask();
105             }
106             catch (Exception)
107             {
108                 this.ShowErrorImage();
109                 try
110                 {
111                     throw;
112                 }
113                 catch (HttpRequestException) { }
114                 catch (InvalidImageException) { }
115                 catch (OperationCanceledException) { }
116                 catch (WebException) { }
117             }
118         }
119
120         protected override void OnPaint(PaintEventArgs pe)
121         {
122             try
123             {
124                 base.OnPaint(pe);
125
126                 // 動画なら再生ボタンを上から描画
127                 DrawPlayableMark(pe);
128             }
129             catch (ExternalException)
130             {
131                 // アニメーション GIF 再生中に発生するエラーの対策
132                 // 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=32894
133                 this.ShowErrorImage();
134             }
135         }
136
137         private void DrawPlayableMark(PaintEventArgs pe)
138         {
139             var thumb = this.Tag as ThumbnailInfo;
140             if (thumb == null || !thumb.IsPlayable) return;
141             if (base.Image == base.InitialImage || base.Image == base.ErrorImage) return;
142
143             var overlayImage = Properties.Resources.PlayableOverlayImage;
144
145             var overlaySize = Math.Min(this.Width, this.Height) / 4;
146             var destRect = new Rectangle(
147                 (this.Width - overlaySize) / 2,
148                 (this.Height - overlaySize) / 2,
149                 overlaySize,
150                 overlaySize);
151
152             pe.Graphics.DrawImage(overlayImage, destRect, 0, 0, overlayImage.Width, overlayImage.Height, GraphicsUnit.Pixel);
153         }
154
155         [Browsable(false)]
156         [EditorBrowsable(EditorBrowsableState.Never)]
157         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
158         public new string ImageLocation
159         {
160             get { throw new NotSupportedException(); }
161             set { throw new NotSupportedException(); }
162         }
163
164         [EditorBrowsable(EditorBrowsableState.Never)]
165         public new void Load(string url)
166         {
167             throw new NotSupportedException();
168         }
169     }
170 }