OSDN Git Service

Merge branch 'cleanup-code-analyzer-errors'
[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
36 namespace OpenTween
37 {
38     public class OTPictureBox : PictureBox
39     {
40         [Browsable(false)]
41         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
42         public new MemoryImage Image
43         {
44             get { return this.memoryImage; }
45             set
46             {
47                 this.memoryImage = value;
48                 base.Image = value != null ? value.Image : null;
49
50                 this.RestoreSizeMode();
51             }
52         }
53         private MemoryImage memoryImage;
54
55         [Localizable(true)]
56         [DefaultValue(PictureBoxSizeMode.Normal)]
57         public new PictureBoxSizeMode SizeMode
58         {
59             get { return this.currentSizeMode; }
60             set
61             {
62                 this.currentSizeMode = value;
63
64                 if (base.Image != base.InitialImage && base.Image != base.ErrorImage)
65                 {
66                     base.SizeMode = value;
67                 }
68             }
69         }
70
71         /// <summary>
72         /// InitialImage や ErrorImage の表示に SizeMode を一時的に変更するため、
73         /// 現在の SizeMode を記憶しておくためのフィールド
74         /// </summary>
75         private PictureBoxSizeMode currentSizeMode;
76
77         public void ShowInitialImage()
78         {
79             base.Image = base.InitialImage;
80
81             // InitialImage は SizeMode の値に依らず中央等倍に表示する必要がある
82             base.SizeMode = PictureBoxSizeMode.CenterImage;
83         }
84
85         public void ShowErrorImage()
86         {
87             base.Image = base.ErrorImage;
88
89             // ErrorImage は SizeMode の値に依らず中央等倍に表示する必要がある
90             base.SizeMode = PictureBoxSizeMode.CenterImage;
91         }
92
93         private void RestoreSizeMode()
94         {
95             base.SizeMode = this.currentSizeMode;
96         }
97
98         public async Task SetImageFromTask(Func<Task<MemoryImage>> imageTask)
99         {
100             try
101             {
102                 this.ShowInitialImage();
103                 this.Image = await imageTask();
104             }
105             catch (Exception)
106             {
107                 this.ShowErrorImage();
108                 try
109                 {
110                     throw;
111                 }
112                 catch (HttpRequestException) { }
113                 catch (InvalidImageException) { }
114                 catch (TaskCanceledException) { }
115                 catch (WebException) { }
116             }
117         }
118
119         protected override void OnPaint(PaintEventArgs pe)
120         {
121             try
122             {
123                 base.OnPaint(pe);
124             }
125             catch (ExternalException)
126             {
127                 // アニメーション GIF 再生中に発生するエラーの対策
128                 // 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=32894
129                 this.ShowErrorImage();
130             }
131         }
132
133         [Browsable(false)]
134         [EditorBrowsable(EditorBrowsableState.Never)]
135         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
136         public new string ImageLocation
137         {
138             get { throw new NotSupportedException(); }
139             set { throw new NotSupportedException(); }
140         }
141
142         [EditorBrowsable(EditorBrowsableState.Never)]
143         public new void Load(string url)
144         {
145             throw new NotSupportedException();
146         }
147     }
148 }