using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using TradeIdeas.ServerConnection; using TradeIdeas.TIProData; namespace TradeIdeas.TIProGUI.CloudLayout { public partial class SaveToCloud : Form { public static readonly WindowIconCache WindowIconCache = new WindowIconCache("SAVE_TO_CLOUD"); private ListViewItem _specialLayoutItems; WatermarkTextBox shortDescriptionTextBox = new WatermarkTextBox(); WatermarkTextBox longDescriptionTextBox = new WatermarkTextBox(); public SaveToCloud() { InitializeComponent(); WindowIconCache.SetIcon(this); //set the anchoring to the two watermark textboxes... shortDescriptionTextBox.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; longDescriptionTextBox.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; //modify and add the WatermarkTextBox shortDescriptionTextBox.Top = 32; shortDescriptionTextBox.Left = 3; shortDescriptionTextBox.Size = new System.Drawing.Size(633, 20); shortDescriptionTextBox.WatermarkText = "Title"; splitContainer1.Panel2.Controls.Add(shortDescriptionTextBox); //modify and add the WatermarkTextBox longDescriptionTextBox.Multiline = true; longDescriptionTextBox.AcceptsReturn = true; longDescriptionTextBox.ScrollBars = ScrollBars.Vertical; longDescriptionTextBox.Top = 58; longDescriptionTextBox.Left = 3; longDescriptionTextBox.Size = new System.Drawing.Size(633, 77); longDescriptionTextBox.WatermarkText = "Description"; splitContainer1.Panel2.Controls.Add(longDescriptionTextBox); foreach (Form form in Application.OpenForms) { ISaveLayout saveable = form as ISaveLayout; if (null != saveable) { ListViewItem item = new ListViewItem(form.Text); item.SubItems.Add(form.Location.ToString()); item.SubItems.Add(form.Size.ToString()); item.ImageIndex = GetIconId(saveable.WindowIconCache); item.Tag = saveable; listView1.Items.Add(item); } } _specialLayoutItems = new ListViewItem("Special layout items", GetIconId(GuiEnvironment.DefaultIcon)); listView1.Items.Add(_specialLayoutItems); //Set default list view listView1.View = View.Details; detailsToolStripMenuItem.Checked = true; UpdateControls(); Font = GuiEnvironment.FontSettings; } private int GetIconId(WindowIconCache source) { Icon icon = source.Icon; try { // This is required to get a high quality version of the icon. If you // add the icon directly to the image list, the icon will be resized // automatically, but it won't look as good. using (Icon smallIcon = new Icon(icon, smallImageList.ImageSize)) { smallImageList.Images.Add(smallIcon); } using (Icon largeIcon = new Icon(icon, largeImageList.ImageSize)) { largeImageList.Images.Add(largeIcon); } } catch { // Is it possible that the image was added to one list but not the other? while (smallImageList.Images.Count > largeImageList.Images.Count) smallImageList.Images.RemoveAt(largeImageList.Images.Count); return -1; } return smallImageList.Images.Count - 1; } public static void DoIt(SendManager sendManager) { using (SaveToCloud dialog = new SaveToCloud()) { dialog.ShowDialog(); if (null != dialog.MessageToServer) { using (SaveToCloudResponse responseCloud = new SaveToCloudResponse(dialog.MessageToServer, sendManager)) { responseCloud.ShowDialog(); string code = responseCloud._code; if (code != null) { try { Clipboard.SetText(code); } catch { } CollaborateForm form = new CollaborateForm(); form.ConfigString = code; form.Text = "Save to Cloud"; form.SetCloudOptions(); form.ShowDialog(); form.Dispose(); } } } } } private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e) { if ((shortDescriptionTextBox.Text == "") && (e.Item.Checked)) shortDescriptionTextBox.Text = e.Item.Text; //Clear textbox text if nothing checked if ((listView1.CheckedItems.Count == 0) && (longDescriptionTextBox.IsTextBoxEmpty)) { shortDescriptionTextBox.Text = ""; longDescriptionTextBox.Text = ""; } UpdateControls(); } private void UpdateControls() { saveButton.Enabled = listView1.CheckedItems.Count > 0; } private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) { if (e.IsSelected) // Do not allow any selection. That would be confusing. You can check things, so you don't // need to select things. listView1.SelectedItems.Clear(); } private void selectAllButton_Click(object sender, EventArgs e) { if (shortDescriptionTextBox.Text == "") // This isn't very exciting. The real goal was to copy the name of a single strategy // when the user checks a box. But if the user hits the select all button, then that // code will always copy the name of the first window. This seems more reasonable. shortDescriptionTextBox.Text = "Layout"; foreach (ListViewItem item in listView1.Items) item.Checked = true; clearPreviousLayoutCheckBox.Checked = true; } private void selectNoneButton_Click(object sender, EventArgs e) { foreach (ListViewItem item in listView1.Items) item.Checked = false; clearPreviousLayoutCheckBox.Checked = false; shortDescriptionTextBox.Text = ""; longDescriptionTextBox.Text = ""; } private void SaveNow() { HashSet windowTypes = new HashSet(); List windows = new List(); int windowCount = 0; foreach (ListViewItem item in listView1.CheckedItems) { ISaveLayout window = item.Tag as ISaveLayout; if (null != window) { windowCount++; windows.Add(window); windowTypes.Add(window.WindowIconCache.WindowName); } } string layout = LayoutManager.Instance().SaveSome(_specialLayoutItems.Checked, windows).OuterXml; bool clearPreviousLayout = clearPreviousLayoutCheckBox.Checked; string icon = ""; if (windowTypes.Count == 1) icon = windowTypes.First(); MessageToServer = TalkWithServer.CreateMessage( "command", "save_to_cloud", "layout", layout, "icon", icon, "window_count", windowCount, "clear_previous_layout", clearPreviousLayoutCheckBox.Checked?"1":"0", "short_description", shortDescriptionTextBox.Text, "long_description", longDescriptionTextBox.Text); // TODO send this message to the server and report the result. } /// /// This is null if the user cancels. It is a valid message to the server if the user hits okay. /// public Dictionary MessageToServer { get; private set; } private void saveButton_Click(object sender, EventArgs e) { SaveNow(); } private void largeIconToolStripMenuItem_Click(object sender, EventArgs e) { listView1.View = View.LargeIcon; largeIconToolStripMenuItem.Checked = true; detailsToolStripMenuItem.Checked = smallIconToolStripMenuItem.Checked = listToolStripMenuItem.Checked = false; } private void detailsToolStripMenuItem_Click(object sender, EventArgs e) { listView1.View = View.Details; detailsToolStripMenuItem.Checked = true; largeIconToolStripMenuItem.Checked = smallIconToolStripMenuItem.Checked = listToolStripMenuItem.Checked = false; } private void smallIconToolStripMenuItem_Click(object sender, EventArgs e) { listView1.View = View.SmallIcon; smallIconToolStripMenuItem.Checked = true; largeIconToolStripMenuItem.Checked = detailsToolStripMenuItem.Checked = listToolStripMenuItem.Checked = false; } private void listToolStripMenuItem_Click(object sender, EventArgs e) { listView1.View = View.List; listToolStripMenuItem.Checked = true; largeIconToolStripMenuItem.Checked = detailsToolStripMenuItem.Checked = smallIconToolStripMenuItem.Checked = false; } } /// /// inspired by this forum entry: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/10f75954-6d14-4926-a02d-98649653b9c8/ /// and http://stackoverflow.com/questions/8566764/textbox-in-c-sharp-windows-form /// Watermark TextBox in winform /// public class WatermarkTextBox : TextBox { private string watermarkText; private Color watermarkColor; private Color foreColor; private bool isTextBoxEmpty; public WatermarkTextBox() { this.WatermarkText = "Watermark Text..."; this.WatermarkColor = Color.FromKnownColor(KnownColor.Silver); this.Enter += onEnter; this.Leave += onLeave; } [Browsable(true)] public bool IsTextBoxEmpty { get { return this.isTextBoxEmpty; } } [Browsable(true)] public new Color ForeColor { get { return this.foreColor; } set { if (value == this.foreColor) { return; } this.foreColor = value; if (!this.isTextBoxEmpty) { base.ForeColor = value; } } } [Browsable(true)] public Color WatermarkColor { get { return this.watermarkColor; } set { if (value == this.watermarkColor) { return; } this.watermarkColor = value; if (this.isTextBoxEmpty) { base.ForeColor = this.watermarkColor; } } } [Browsable(true)] public string WatermarkText { get { return this.watermarkText; } set { if (value == this.watermarkText) { return; } this.watermarkText = value; if ((base.Text.Length == 0) || (isTextBoxEmpty)) { this.isTextBoxEmpty = true; base.Text = this.watermarkText; base.ForeColor = this.watermarkColor; } this.Invalidate(); } } public override string Text { get { return this.isTextBoxEmpty ? string.Empty : base.Text; } set { if (string.IsNullOrEmpty(value)) { this.isTextBoxEmpty = true; base.ForeColor = this.watermarkColor; base.Text = this.watermarkText; } else { this.isTextBoxEmpty = false; base.ForeColor = this.foreColor; base.Text = value; } } } private void onEnter(object sender, EventArgs e) { if (this.isTextBoxEmpty) { this.isTextBoxEmpty = false; base.ForeColor = this.foreColor; base.Text = string.Empty; } } private void onLeave(object sender, EventArgs e) { if (string.IsNullOrEmpty(base.Text)) { this.isTextBoxEmpty = true; base.ForeColor = this.watermarkColor; base.Text = this.watermarkText; } else { this.isTextBoxEmpty = false; } } } }