OSDN Git Service

OpenTween v1.3.9 リリース
[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         {
44             InitializeComponent();
45         }
46
47         private void OK_Button_Click(object sender, EventArgs e)
48         {
49             if (UrlList.SelectedItems.Count == 0)
50             {
51                 this.DialogResult = DialogResult.Cancel;
52             }
53             else
54             {
55                 _selUrl = UrlList.SelectedItem.ToString();
56                 this.DialogResult = DialogResult.OK;
57             }
58             this.Close();
59         }
60
61         private void Cancel_Button_Click(object sender, EventArgs e)
62         {
63             this.DialogResult = DialogResult.Cancel;
64             this.Close();
65         }
66
67         public void ClearUrl()
68         {
69             UrlList.Items.Clear();
70         }
71
72         public void AddUrl(OpenUrlItem openUrlItem)
73         {
74             UrlList.Items.Add(openUrlItem);
75         }
76
77         public string SelectedUrl
78         {
79             get
80             {
81                 if (UrlList.SelectedItems.Count == 1)
82                     return _selUrl;
83                 else
84                     return "";
85             }
86         }
87
88         private void OpenURL_Shown(object sender, EventArgs e)
89         {
90             UrlList.Focus();
91             if (UrlList.Items.Count > 0)
92                 UrlList.SelectedIndex = 0;
93         }
94
95         private void UrlList_DoubleClick(object sender, EventArgs e)
96         {
97             if (UrlList.SelectedItem == null)
98                 return;
99
100             if (UrlList.IndexFromPoint(UrlList.PointToClient(Control.MousePosition)) == ListBox.NoMatches)
101                 return;
102
103             if (UrlList.Items[UrlList.IndexFromPoint(UrlList.PointToClient(Control.MousePosition))] == null)
104                 return;
105             OK_Button_Click(sender, e);
106         }
107
108         private void UrlList_KeyDown(object sender, KeyEventArgs e)
109         {
110             if (e.KeyCode == Keys.J && UrlList.SelectedIndex < UrlList.Items.Count - 1)
111             {
112                 e.SuppressKeyPress = true;
113                 UrlList.SelectedIndex += 1;
114             }
115             if (e.KeyCode == Keys.K && UrlList.SelectedIndex > 0)
116             {
117                 e.SuppressKeyPress = true;
118                 UrlList.SelectedIndex -= 1;
119             }
120             if (e.Control && e.KeyCode == Keys.Oem4)
121             {
122                 e.SuppressKeyPress = true;
123                 Cancel_Button_Click(null, null);
124             }
125         }
126     }
127
128     public class OpenUrlItem
129     {
130         private string _linkText;
131
132         public OpenUrlItem(string linkText, string url, string href)
133         {
134             this._linkText = linkText;
135             this.Url = url;
136             this.Href = href;
137         }
138
139         public string Text
140         {
141             get
142             {
143                 if (this._linkText.StartsWith("@", StringComparison.Ordinal)
144                     || this._linkText.StartsWith("@", StringComparison.Ordinal)
145                     || this._linkText.StartsWith("#", StringComparison.Ordinal)
146                     || this._linkText.StartsWith("#", StringComparison.Ordinal))
147                     return this._linkText;
148                 if (this._linkText.TrimEnd('/') == this.Url.TrimEnd('/'))
149                     return this.Url;
150                 else
151                     return this._linkText + "  >>>  " + this.Url;
152             }
153         }
154
155         public override string ToString()
156         {
157             return this.Href;
158         }
159
160         public string Url { get; }
161         public string Href { get; }
162     }
163 }