OSDN Git Service

ブロックの括弧を独立した行に書く (SA1500, SA1501, SA1502)
[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 #nullable enable
23
24 using System;
25 using System.Collections.Generic;
26 using System.ComponentModel;
27 using System.Drawing;
28 using System.IO;
29 using System.Linq;
30 using System.Net;
31 using System.Net.Http;
32 using System.Runtime.InteropServices;
33 using System.Text;
34 using System.Threading;
35 using System.Threading.Tasks;
36 using System.Windows.Forms;
37 using OpenTween.Thumbnail;
38
39 namespace OpenTween
40 {
41     public class OTPictureBox : PictureBox
42     {
43         [Browsable(false)]
44         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
45         public new MemoryImage? Image
46         {
47             get => this.memoryImage;
48             set
49             {
50                 this.memoryImage = value;
51                 base.Image = value?.Image;
52
53                 this.RestoreSizeMode();
54             }
55         }
56         private MemoryImage? memoryImage;
57
58         [Localizable(true)]
59         [DefaultValue(PictureBoxSizeMode.Normal)]
60         public new PictureBoxSizeMode SizeMode
61         {
62             get => this.currentSizeMode;
63             set
64             {
65                 this.currentSizeMode = value;
66
67                 if (base.Image != base.InitialImage && base.Image != base.ErrorImage)
68                 {
69                     base.SizeMode = value;
70                 }
71             }
72         }
73
74         /// <summary>
75         /// InitialImage や ErrorImage の表示に SizeMode を一時的に変更するため、
76         /// 現在の SizeMode を記憶しておくためのフィールド
77         /// </summary>
78         private PictureBoxSizeMode currentSizeMode;
79
80         public void ShowInitialImage()
81         {
82             base.Image = base.InitialImage;
83
84             // InitialImage は SizeMode の値に依らず中央等倍に表示する必要がある
85             base.SizeMode = PictureBoxSizeMode.CenterImage;
86         }
87
88         public void ShowErrorImage()
89         {
90             base.Image = base.ErrorImage;
91
92             // ErrorImage は SizeMode の値に依らず中央等倍に表示する必要がある
93             base.SizeMode = PictureBoxSizeMode.CenterImage;
94         }
95
96         private void RestoreSizeMode()
97             => base.SizeMode = this.currentSizeMode;
98
99         /// <summary>
100         /// SetImageFromTask メソッドを連続で呼び出した際に設定される画像が前後するのを防ぐため、
101         /// 現在進行中の Task を表す Id を記憶しておくためのフィールド
102         /// </summary>
103         private int currentImageTaskId = 0;
104
105         public async Task SetImageFromTask(Func<Task<MemoryImage>> imageTask)
106         {
107             var id = Interlocked.Increment(ref this.currentImageTaskId);
108
109             try
110             {
111                 this.ShowInitialImage();
112
113                 var image = await imageTask();
114
115                 if (id == this.currentImageTaskId)
116                     this.Image = image;
117             }
118             catch (Exception)
119             {
120                 if (id == this.currentImageTaskId)
121                     this.ShowErrorImage();
122                 try
123                 {
124                     throw;
125                 }
126                 catch (HttpRequestException)
127                 {
128                 }
129                 catch (InvalidImageException)
130                 {
131                 }
132                 catch (OperationCanceledException)
133                 {
134                 }
135                 catch (ObjectDisposedException)
136                 {
137                 }
138                 catch (WebException)
139                 {
140                 }
141                 catch (IOException)
142                 {
143                 }
144             }
145         }
146
147         protected override void OnPaint(PaintEventArgs pe)
148         {
149             try
150             {
151                 base.OnPaint(pe);
152
153                 // 動画なら再生ボタンを上から描画
154                 this.DrawPlayableMark(pe);
155             }
156             catch (ExternalException)
157             {
158                 // アニメーション GIF 再生中に発生するエラーの対策
159                 // 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=32894
160                 this.ShowErrorImage();
161             }
162         }
163
164         private void DrawPlayableMark(PaintEventArgs pe)
165         {
166             if (!(this.Tag is ThumbnailInfo thumb && thumb.IsPlayable)) return;
167             if (base.Image == base.InitialImage || base.Image == base.ErrorImage) return;
168
169             var overlayImage = Properties.Resources.PlayableOverlayImage;
170
171             var overlaySize = Math.Min(this.Width, this.Height) / 4;
172             var destRect = new Rectangle(
173                 (this.Width - overlaySize) / 2,
174                 (this.Height - overlaySize) / 2,
175                 overlaySize,
176                 overlaySize);
177
178             pe.Graphics.DrawImage(overlayImage, destRect, 0, 0, overlayImage.Width, overlayImage.Height, GraphicsUnit.Pixel);
179         }
180
181         [Browsable(false)]
182         [EditorBrowsable(EditorBrowsableState.Never)]
183         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
184         public new string? ImageLocation
185         {
186             get => null;
187             set { }
188         }
189
190         [EditorBrowsable(EditorBrowsableState.Never)]
191         public new void Load(string url)
192             => throw new NotSupportedException();
193     }
194 }