OSDN Git Service

TweenMain.ListTab.SelectedTabへの参照をCurrentTabPageに置き換える
[opentween/open-tween.git] / OpenTween / OpenURL.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
9 // 
10 // This file is part of OpenTween.
11 // 
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 // 
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 // for more details. 
21 // 
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 using System;
28 using System.Collections.Generic;
29 using System.ComponentModel;
30 using System.Data;
31 using System.Drawing;
32 using System.Linq;
33 using System.Text;
34 using System.Windows.Forms;
35
36 namespace OpenTween
37 {
38     public partial class OpenURL : OTBaseForm
39     {
40         private string _selUrl;
41
42         public OpenURL()
43             => this.InitializeComponent();
44
45         private void OK_Button_Click(object sender, EventArgs e)
46         {
47             if (UrlList.SelectedItems.Count == 0)
48             {
49                 this.DialogResult = DialogResult.Cancel;
50             }
51             else
52             {
53                 _selUrl = UrlList.SelectedItem.ToString();
54                 this.DialogResult = DialogResult.OK;
55             }
56             this.Close();
57         }
58
59         private void Cancel_Button_Click(object sender, EventArgs e)
60         {
61             this.DialogResult = DialogResult.Cancel;
62             this.Close();
63         }
64
65         public void ClearUrl()
66             => this.UrlList.Items.Clear();
67
68         public void AddUrl(OpenUrlItem openUrlItem)
69             => this.UrlList.Items.Add(openUrlItem);
70
71         public string SelectedUrl
72         {
73             get
74             {
75                 if (UrlList.SelectedItems.Count == 1)
76                     return _selUrl;
77                 else
78                     return "";
79             }
80         }
81
82         private void OpenURL_Shown(object sender, EventArgs e)
83         {
84             UrlList.Focus();
85             if (UrlList.Items.Count > 0)
86                 UrlList.SelectedIndex = 0;
87         }
88
89         private void UrlList_DoubleClick(object sender, EventArgs e)
90         {
91             if (UrlList.SelectedItem == null)
92                 return;
93
94             if (UrlList.IndexFromPoint(UrlList.PointToClient(Control.MousePosition)) == ListBox.NoMatches)
95                 return;
96
97             if (UrlList.Items[UrlList.IndexFromPoint(UrlList.PointToClient(Control.MousePosition))] == null)
98                 return;
99             OK_Button_Click(sender, e);
100         }
101
102         private void UrlList_KeyDown(object sender, KeyEventArgs e)
103         {
104             if (e.KeyCode == Keys.J && UrlList.SelectedIndex < UrlList.Items.Count - 1)
105             {
106                 e.SuppressKeyPress = true;
107                 UrlList.SelectedIndex += 1;
108             }
109             if (e.KeyCode == Keys.K && UrlList.SelectedIndex > 0)
110             {
111                 e.SuppressKeyPress = true;
112                 UrlList.SelectedIndex -= 1;
113             }
114             if (e.Control && e.KeyCode == Keys.Oem4)
115             {
116                 e.SuppressKeyPress = true;
117                 Cancel_Button_Click(null, null);
118             }
119         }
120     }
121
122     public class OpenUrlItem
123     {
124         private string _linkText;
125
126         public OpenUrlItem(string linkText, string url, string href)
127         {
128             this._linkText = linkText;
129             this.Url = url;
130             this.Href = href;
131         }
132
133         public string Text
134         {
135             get
136             {
137                 if (this._linkText.StartsWith("@", StringComparison.Ordinal)
138                     || this._linkText.StartsWith("@", StringComparison.Ordinal)
139                     || this._linkText.StartsWith("#", StringComparison.Ordinal)
140                     || this._linkText.StartsWith("#", StringComparison.Ordinal))
141                     return this._linkText;
142                 if (this._linkText.TrimEnd('/') == this.Url.TrimEnd('/'))
143                     return this.Url;
144                 else
145                     return this._linkText + "  >>>  " + this.Url;
146             }
147         }
148
149         public override string ToString()
150             => this.Href;
151
152         public string Url { get; }
153         public string Href { get; }
154     }
155 }