OSDN Git Service

1ff43a68d9fa5d1f960297b7153c9b2df8a24935
[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 => this.memoryImage;
46             set
47             {
48                 this.memoryImage = value;
49                 base.Image = value?.Image;
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 => 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             => base.SizeMode = this.currentSizeMode;
96
97         /// <summary>
98         /// SetImageFromTask メソッドを連続で呼び出した際に設定される画像が前後するのを防ぐため、
99         /// 現在進行中の Task を表す Id を記憶しておくためのフィールド
100         /// </summary>
101         private int currentImageTaskId = 0;
102
103         public async Task SetImageFromTask(Func<Task<MemoryImage>> imageTask)
104         {
105             var id = Interlocked.Increment(ref this.currentImageTaskId);
106
107             try
108             {
109                 this.ShowInitialImage();
110
111                 var image = await imageTask();
112
113                 if (id == this.currentImageTaskId)
114                     this.Image = image;
115             }
116             catch (Exception)
117             {
118                 if (id == this.currentImageTaskId)
119                     this.ShowErrorImage();
120                 try
121                 {
122                     throw;
123                 }
124                 catch (HttpRequestException) { }
125                 catch (InvalidImageException) { }
126                 catch (OperationCanceledException) { }
127                 catch (ObjectDisposedException) { }
128                 catch (WebException) { }
129                 catch (IOException) { }
130             }
131         }
132
133         protected override void OnPaint(PaintEventArgs pe)
134         {
135             try
136             {
137                 base.OnPaint(pe);
138
139                 // 動画なら再生ボタンを上から描画
140                 DrawPlayableMark(pe);
141             }
142             catch (ExternalException)
143             {
144                 // アニメーション GIF 再生中に発生するエラーの対策
145                 // 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=32894
146                 this.ShowErrorImage();
147             }
148         }
149
150         private void DrawPlayableMark(PaintEventArgs pe)
151         {
152             if (!(this.Tag is ThumbnailInfo thumb && thumb.IsPlayable)) return;
153             if (base.Image == base.InitialImage || base.Image == base.ErrorImage) return;
154
155             var overlayImage = Properties.Resources.PlayableOverlayImage;
156
157             var overlaySize = Math.Min(this.Width, this.Height) / 4;
158             var destRect = new Rectangle(
159                 (this.Width - overlaySize) / 2,
160                 (this.Height - overlaySize) / 2,
161                 overlaySize,
162                 overlaySize);
163
164             pe.Graphics.DrawImage(overlayImage, destRect, 0, 0, overlayImage.Width, overlayImage.Height, GraphicsUnit.Pixel);
165         }
166
167         [Browsable(false)]
168         [EditorBrowsable(EditorBrowsableState.Never)]
169         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
170         public new string ImageLocation
171         {
172             get => null;
173             set { }
174         }
175
176         [EditorBrowsable(EditorBrowsableState.Never)]
177         public new void Load(string url)
178             => throw new NotSupportedException();
179     }
180 }