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;
29 using System.Net.Http;
31 using System.Windows.Forms;
32 using System.Text.RegularExpressions;
33 using System.Threading.Tasks;
34 using OpenTween.Thumbnail;
35 using System.Threading;
39 public partial class TweetThumbnail : UserControl
41 protected internal List<OTPictureBox> pictureBox = new List<OTPictureBox>();
43 public event EventHandler ThumbnailLoading;
44 public event EventHandler<ThumbnailDoubleClickEventArgs> ThumbnailDoubleClick;
45 public event EventHandler<ThumbnailImageSearchEventArgs> ThumbnailImageSearchClick;
47 private object uiLockObj = new object();
49 public ThumbnailInfo Thumbnail
51 get { return this.pictureBox[this.scrollBar.Value].Tag as ThumbnailInfo; }
54 public TweetThumbnail()
56 InitializeComponent();
59 public Task ShowThumbnailAsync(PostClass post)
61 return this.ShowThumbnailAsync(post, CancellationToken.None);
64 public async Task ShowThumbnailAsync(PostClass post, CancellationToken cancelToken)
66 var loadTasks = new List<Task>();
68 this.scrollBar.Enabled = false;
70 var thumbnails = (await this.GetThumbailInfoAsync(post, cancelToken))
73 cancelToken.ThrowIfCancellationRequested();
77 this.SetThumbnailCount(thumbnails.Length);
78 if (thumbnails.Length == 0) return;
80 for (int i = 0; i < thumbnails.Length; i++)
82 var thumb = thumbnails[i];
83 var picbox = this.pictureBox[i];
86 picbox.ContextMenu = CreateContextMenu(thumb);
88 var loadTask = this.SetThumbnailImageAsync(picbox, thumb, cancelToken);
89 loadTasks.Add(loadTask);
91 var tooltipText = thumb.TooltipText;
92 if (!string.IsNullOrEmpty(tooltipText))
94 this.toolTip.SetToolTip(picbox, tooltipText);
97 cancelToken.ThrowIfCancellationRequested();
100 if (thumbnails.Length > 1)
101 this.scrollBar.Enabled = true;
104 if (this.ThumbnailLoading != null)
105 this.ThumbnailLoading(this, EventArgs.Empty);
107 await Task.WhenAll(loadTasks).ConfigureAwait(false);
110 private async Task SetThumbnailImageAsync(OTPictureBox picbox, ThumbnailInfo thumbInfo,
111 CancellationToken cancelToken)
115 picbox.ShowInitialImage();
116 picbox.Image = await thumbInfo.LoadThumbnailImageAsync(cancelToken);
120 picbox.ShowErrorImage();
125 catch (HttpRequestException) { }
126 catch (InvalidImageException) { }
127 catch (TaskCanceledException) { }
131 private ContextMenu CreateContextMenu(ThumbnailInfo thumb)
133 var contextMenu = new ContextMenu();
134 contextMenu.MenuItems.Add(CreateImageSearchMenuItem(thumb));
138 private MenuItem CreateImageSearchMenuItem(ThumbnailInfo thumb)
140 var item = new MenuItem();
141 item.Text = Properties.Resources.SearchSimilarImageText;
142 var search_targe_url = thumb.FullSizeImageUrl ?? thumb.ThumbnailUrl ?? null;
144 if (search_targe_url != null)
146 item.Click += (sender, e) =>
148 string uri = GetImageSearchUri(search_targe_url);
149 if (this.ThumbnailImageSearchClick != null)
151 this.ThumbnailImageSearchClick(this, new ThumbnailImageSearchEventArgs(uri));
157 item.Enabled = false;
163 private string GetImageSearchUri(string image_uri)
165 return @"https://www.google.com/searchbyimage?image_url=" + Uri.EscapeDataString(image_uri);
168 protected virtual Task<IEnumerable<ThumbnailInfo>> GetThumbailInfoAsync(PostClass post, CancellationToken token)
170 return ThumbnailGenerator.GetThumbnailsAsync(post, token);
176 /// <param name="count">表示するサムネイルの数</param>
177 protected void SetThumbnailCount(int count)
179 using (ControlTransaction.Layout(this, false))
181 this.panelPictureBox.Controls.Clear();
182 foreach (var picbox in this.pictureBox)
184 var memoryImage = picbox.Image;
185 var contextMenu = picbox.ContextMenu;
188 if (memoryImage != null)
189 memoryImage.Dispose();
190 if (contextMenu != null)
191 contextMenu.Dispose();
193 this.pictureBox.Clear();
195 this.scrollBar.Maximum = (count > 0) ? count - 1 : 0;
196 this.scrollBar.Value = 0;
198 for (int i = 0; i < count; i++)
200 var picbox = CreatePictureBox("pictureBox" + i);
201 picbox.Visible = (i == 0);
202 picbox.DoubleClick += this.pictureBox_DoubleClick;
204 this.panelPictureBox.Controls.Add(picbox);
205 this.pictureBox.Add(picbox);
210 protected virtual OTPictureBox CreatePictureBox(string name)
212 return new OTPictureBox()
215 SizeMode = PictureBoxSizeMode.Zoom,
217 Dock = DockStyle.Fill,
221 public void ScrollUp()
223 var newval = this.scrollBar.Value + this.scrollBar.SmallChange;
225 if (newval > this.scrollBar.Maximum)
226 newval = this.scrollBar.Maximum;
228 this.scrollBar.Value = newval;
231 public void ScrollDown()
233 var newval = this.scrollBar.Value - this.scrollBar.SmallChange;
235 if (newval < this.scrollBar.Minimum)
236 newval = this.scrollBar.Minimum;
238 this.scrollBar.Value = newval;
241 private void scrollBar_ValueChanged(object sender, EventArgs e)
243 using (ControlTransaction.Layout(this, false))
245 var value = this.scrollBar.Value;
246 for (var i = 0; i < this.pictureBox.Count; i++)
248 this.pictureBox[i].Visible = (i == value);
253 private void pictureBox_DoubleClick(object sender, EventArgs e)
255 var thumb = ((PictureBox)sender).Tag as ThumbnailInfo;
257 if (thumb == null) return;
259 if (this.ThumbnailDoubleClick != null)
261 this.ThumbnailDoubleClick(this, new ThumbnailDoubleClickEventArgs(thumb));
266 public class ThumbnailDoubleClickEventArgs : EventArgs
268 public ThumbnailInfo Thumbnail { get; private set; }
270 public ThumbnailDoubleClickEventArgs(ThumbnailInfo thumbnail)
272 this.Thumbnail = thumbnail;
276 public class ThumbnailImageSearchEventArgs : EventArgs
278 public string ImageUrl { get; private set; }
280 public ThumbnailImageSearchEventArgs(string url)