1 /*
2   KeePass Password Safe - The Open-Source Password Manager
3   Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de>
4 
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19 
20 using System;
21 using System.Collections.Generic;
22 using System.ComponentModel;
23 using System.Diagnostics;
24 using System.Drawing;
25 using System.Text;
26 using System.Windows.Forms;
27 
28 using KeePass.App;
29 using KeePass.UI;
30 
31 namespace KeePass.Forms
32 {
33 	public partial class SingleLineEditForm : Form
34 	{
35 		private string m_strTitle = string.Empty;
36 		private string m_strDesc = string.Empty;
37 		private string m_strLongDesc = string.Empty;
38 		private Image m_imgIcon = null;
39 		private string m_strDefaultText = string.Empty;
40 		private string[] m_vSelectable = null;
41 
42 		private string m_strResultString = string.Empty;
43 
44 		public string ResultString
45 		{
46 			get { return m_strResultString; }
47 		}
48 
SingleLineEditForm()49 		public SingleLineEditForm()
50 		{
51 			InitializeComponent();
52 			GlobalWindowManager.InitializeForm(this);
53 		}
54 
InitEx(string strTitle, string strDesc, string strLongDesc, Image imgIcon, string strDefaultText, string[] vSelectable)55 		public void InitEx(string strTitle, string strDesc, string strLongDesc,
56 			Image imgIcon, string strDefaultText, string[] vSelectable)
57 		{
58 			m_strTitle = strTitle;
59 			m_strDesc = strDesc;
60 			m_strLongDesc = strLongDesc;
61 			m_imgIcon = imgIcon;
62 			m_strDefaultText = strDefaultText;
63 			m_vSelectable = vSelectable;
64 		}
65 
OnFormLoad(object sender, EventArgs e)66 		private void OnFormLoad(object sender, EventArgs e)
67 		{
68 			if(m_strTitle == null) throw new InvalidOperationException();
69 			if(m_strDesc == null) throw new InvalidOperationException();
70 			if(m_strLongDesc == null) throw new InvalidOperationException();
71 			if(m_imgIcon == null) throw new InvalidOperationException();
72 			if(m_strDefaultText == null) throw new InvalidOperationException();
73 
74 			GlobalWindowManager.AddWindow(this);
75 
76 			BannerFactory.CreateBannerEx(this, m_bannerImage,
77 				m_imgIcon, m_strTitle, m_strDesc);
78 			this.Icon = AppIcons.Default;
79 
80 			this.Text = m_strTitle;
81 
82 			Debug.Assert(!m_lblLongDesc.AutoSize); // For RTL support
83 			m_lblLongDesc.Text = m_strLongDesc;
84 
85 			Control cFocus = null;
86 			if((m_vSelectable == null) || (m_vSelectable.Length == 0))
87 			{
88 				m_cmbEdit.Enabled = false;
89 				m_cmbEdit.Visible = false;
90 
91 				cFocus = m_tbEdit;
92 			}
93 			else // With selectable values
94 			{
95 				m_tbEdit.Enabled = false;
96 				m_tbEdit.Visible = false;
97 
98 				cFocus = m_cmbEdit;
99 
100 				foreach(string strPreDef in m_vSelectable)
101 					m_cmbEdit.Items.Add(strPreDef);
102 
103 				UIUtil.EnableAutoCompletion(m_cmbEdit, false);
104 			}
105 
106 			cFocus.Text = m_strDefaultText;
107 
108 			Invalidate();
109 			UIUtil.SetFocus(cFocus, this);
110 		}
111 
OnBtnOK(object sender, EventArgs e)112 		private void OnBtnOK(object sender, EventArgs e)
113 		{
114 			if((m_vSelectable == null) || (m_vSelectable.Length == 0))
115 				m_strResultString = m_tbEdit.Text;
116 			else
117 				m_strResultString = m_cmbEdit.Text;
118 		}
119 
OnBtnCancel(object sender, EventArgs e)120 		private void OnBtnCancel(object sender, EventArgs e)
121 		{
122 		}
123 
OnFormClosed(object sender, FormClosedEventArgs e)124 		private void OnFormClosed(object sender, FormClosedEventArgs e)
125 		{
126 			GlobalWindowManager.RemoveWindow(this);
127 		}
128 	}
129 }
130