OSDN Git Service

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