1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
23 using System.Collections.Generic;
24 using System.ComponentModel;
25 using System.Diagnostics.CodeAnalysis;
30 using System.Net.Http;
32 using System.Windows.Forms;
33 using System.Text.RegularExpressions;
34 using System.Threading.Tasks;
35 using OpenTween.Thumbnail;
36 using System.Threading;
40 public partial class TweetThumbnail : UserControl
42 protected internal List<OTPictureBox> pictureBox = new List<OTPictureBox>();
44 public event EventHandler ThumbnailLoading;
45 public event EventHandler<ThumbnailDoubleClickEventArgs> ThumbnailDoubleClick;
46 public event EventHandler<ThumbnailImageSearchEventArgs> ThumbnailImageSearchClick;
48 public ThumbnailInfo Thumbnail
50 get { return this.pictureBox[this.scrollBar.Value].Tag as ThumbnailInfo; }
53 public TweetThumbnail()
55 InitializeComponent();
58 public Task ShowThumbnailAsync(PostClass post)
60 return this.ShowThumbnailAsync(post, CancellationToken.None);
63 public async Task ShowThumbnailAsync(PostClass post, CancellationToken cancelToken)
65 var loadTasks = new List<Task>();
67 this.scrollBar.Enabled = false;
69 if (post.Media.Count == 0 && post.PostGeo.Lat == 0 && post.PostGeo.Lng == 0)
71 this.SetThumbnailCount(0);
75 var thumbnails = (await this.GetThumbailInfoAsync(post, cancelToken))
78 cancelToken.ThrowIfCancellationRequested();
80 this.SetThumbnailCount(thumbnails.Length);
81 if (thumbnails.Length == 0)
84 for (int i = 0; i < thumbnails.Length; i++)
86 var thumb = thumbnails[i];
87 var picbox = this.pictureBox[i];
90 picbox.ContextMenuStrip = this.contextMenuStrip;
92 var loadTask = picbox.SetImageFromTask(() => thumb.LoadThumbnailImageAsync(cancelToken));
93 loadTasks.Add(loadTask);
95 var tooltipText = thumb.TooltipText;
96 if (!string.IsNullOrEmpty(tooltipText))
98 this.toolTip.SetToolTip(picbox, tooltipText);
101 cancelToken.ThrowIfCancellationRequested();
104 if (thumbnails.Length > 1)
105 this.scrollBar.Enabled = true;
107 if (this.ThumbnailLoading != null)
108 this.ThumbnailLoading(this, EventArgs.Empty);
110 await Task.WhenAll(loadTasks).ConfigureAwait(false);
113 private string GetImageSearchUri(string image_uri)
115 return @"https://www.google.com/searchbyimage?image_url=" + Uri.EscapeDataString(image_uri);
118 protected virtual Task<IEnumerable<ThumbnailInfo>> GetThumbailInfoAsync(PostClass post, CancellationToken token)
120 return ThumbnailGenerator.GetThumbnailsAsync(post, token);
126 /// <param name="count">表示するサムネイルの数</param>
127 protected void SetThumbnailCount(int count)
129 if (count == 0 && this.pictureBox.Count == 0)
132 using (ControlTransaction.Layout(this.panelPictureBox, false))
134 this.panelPictureBox.Controls.Clear();
135 foreach (var picbox in this.pictureBox)
137 var memoryImage = picbox.Image;
140 if (memoryImage != null)
141 memoryImage.Dispose();
143 // メモリリーク対策 (http://stackoverflow.com/questions/2792427#2793714)
144 picbox.ContextMenuStrip = null;
146 this.pictureBox.Clear();
148 this.scrollBar.Maximum = (count > 0) ? count - 1 : 0;
149 this.scrollBar.Value = 0;
151 for (int i = 0; i < count; i++)
153 var picbox = CreatePictureBox("pictureBox" + i);
154 picbox.Visible = (i == 0);
155 picbox.DoubleClick += this.pictureBox_DoubleClick;
157 this.panelPictureBox.Controls.Add(picbox);
158 this.pictureBox.Add(picbox);
163 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
164 protected virtual OTPictureBox CreatePictureBox(string name)
166 return new OTPictureBox()
169 SizeMode = PictureBoxSizeMode.Zoom,
171 Dock = DockStyle.Fill,
175 public void ScrollUp()
177 var newval = this.scrollBar.Value - this.scrollBar.SmallChange;
179 if (newval < this.scrollBar.Minimum)
180 newval = this.scrollBar.Minimum;
182 this.scrollBar.Value = newval;
185 public void ScrollDown()
187 var newval = this.scrollBar.Value + this.scrollBar.SmallChange;
189 if (newval > this.scrollBar.Maximum)
190 newval = this.scrollBar.Maximum;
192 this.scrollBar.Value = newval;
195 private void scrollBar_ValueChanged(object sender, EventArgs e)
197 using (ControlTransaction.Layout(this, false))
199 var value = this.scrollBar.Value;
200 for (var i = 0; i < this.pictureBox.Count; i++)
202 this.pictureBox[i].Visible = (i == value);
207 private void pictureBox_DoubleClick(object sender, EventArgs e)
209 var thumb = ((PictureBox)sender).Tag as ThumbnailInfo;
211 if (thumb == null) return;
213 if (this.ThumbnailDoubleClick != null)
215 this.ThumbnailDoubleClick(this, new ThumbnailDoubleClickEventArgs(thumb));
219 private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
221 var picbox = (OTPictureBox)this.contextMenuStrip.SourceControl;
222 var thumb = (ThumbnailInfo)picbox.Tag;
224 var searchTargetUri = thumb.FullSizeImageUrl ?? thumb.ThumbnailUrl ?? null;
225 if (searchTargetUri != null)
227 this.searchSimilarImageMenuItem.Enabled = true;
228 this.searchSimilarImageMenuItem.Tag = searchTargetUri;
232 this.searchSimilarImageMenuItem.Enabled = false;
236 private void searchSimilarImageMenuItem_Click(object sender, EventArgs e)
238 var searchTargetUri = (string)this.searchSimilarImageMenuItem.Tag;
239 var searchUri = this.GetImageSearchUri(searchTargetUri);
241 if (this.ThumbnailImageSearchClick != null)
242 this.ThumbnailImageSearchClick(this, new ThumbnailImageSearchEventArgs(searchUri));
246 public class ThumbnailDoubleClickEventArgs : EventArgs
248 public ThumbnailInfo Thumbnail { get; private set; }
250 public ThumbnailDoubleClickEventArgs(ThumbnailInfo thumbnail)
252 this.Thumbnail = thumbnail;
256 public class ThumbnailImageSearchEventArgs : EventArgs
258 public string ImageUrl { get; private set; }
260 public ThumbnailImageSearchEventArgs(string url)