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 TibisMathematicsSuite.KeyboardIO; namespace TibisMathematicsSuite { public partial class MainWindow : Form { private Dictionary moduleShortcuts = new Dictionary(); bool allowExit = false, allowShow = false; #region Constructor public MainWindow() { InitializeComponent(); PopulateModules(); PopulateNotificationList(); UpdateGlobalHotkeys(); // Hotkey Hotkey.HotkeyPressed += new EventHandler(Hotkey_HotkeyPressed); // Show baloon message notify.ShowBalloonTip(2, "Tibi's Mathematics Suite", "The suite is running. Right click this icon to open an application.", ToolTipIcon.None); } #endregion #region Rendering protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); DynamicLink.Controls.BackgroundGradient.Paint(e.Graphics, new Rectangle(0, 0, this.Width, this.Height)); } protected override void OnResize(EventArgs e) { base.OnResize(e); this.Invalidate(); } #endregion #region Update routines private void PopulateModules() { // Collect data if (DynamicLink.Modules.Items.Count == 0) DynamicLink.Modules.CollectData(); // Get the already made dictionary var dict = TibisMathematicsSuite.Properties.Settings.Default.ModuleKeys; if (dict == null) dict = new System.Collections.Specialized.StringDictionary(); foreach (var i in DynamicLink.Modules.Items) { if (dict.ContainsKey(i.Name)) moduleShortcuts.Add(i.Name, MyKey.Parse(dict[i.Name])); else moduleShortcuts.Add(i.Name, null); } // Now that our dictionary is complete, add it to list box UpdateModules(); } private void PopulateNotificationList() { // Collect data if (DynamicLink.Modules.Items.Count == 0) DynamicLink.Modules.CollectData(); // Add items int j = 0; foreach (var i in DynamicLink.Modules.Items.OrderBy(x => x.Name)) { ToolStripMenuItem item = new ToolStripMenuItem(i.Name); item.Click += new EventHandler(menuContextModule_Click); item.Image = i.Icon.ToBitmap(); item.ToolTipText = i.Description; notifyContext.Items.Insert(j++, item); } } private void UpdateModules() { listModules.Items.Clear(); foreach (var i in moduleShortcuts) if (i.Value == null) listModules.Items.Add(new ListViewItem (new string[] { i.Key, "" })); else listModules.Items.Add(new ListViewItem(new string[] { i.Key, i.Value.ToString() })); } private void UpdateModule(string name) { foreach (ListViewItem i in listModules.Items) if (i.Text == name) { if (moduleShortcuts[name] == null) i.SubItems[1].Text = ""; else i.SubItems[1].Text = moduleShortcuts[name].ToString(); return; } } private void UpdateGlobalHotkeys() { KeyboardIO.Hotkey.UnregisterAll(); foreach (var i in moduleShortcuts) if (i.Value != null) KeyboardIO.Hotkey.Register(i.Value); } #endregion #region Buttons private void buttonClear_Click(object sender, EventArgs e) { if (listModules.SelectedItems.Count != 1) return; string name = listModules.SelectedItems[0].Text; moduleShortcuts[name] = null; UpdateModule(name); } private void buttonSet_Click(object sender, EventArgs e) { if (listModules.SelectedItems.Count != 1) return; string name = listModules.SelectedItems[0].Text; moduleShortcuts[name] = keyShortcutBox.Key; UpdateModule(name); } private void buttonAccept_Click(object sender, EventArgs e) { // Update hotkey list UpdateGlobalHotkeys(); // Save settings if (TibisMathematicsSuite.Properties.Settings.Default.ModuleKeys == null) TibisMathematicsSuite.Properties.Settings.Default.ModuleKeys = new System.Collections.Specialized.StringDictionary(); TibisMathematicsSuite.Properties.Settings.Default.ModuleKeys.Clear(); foreach (var i in moduleShortcuts) if (i.Value != null) TibisMathematicsSuite.Properties.Settings.Default.ModuleKeys.Add(i.Key, i.Value.ToString()); // Hide window this.Hide(); } private void buttonCancel_Click(object sender, EventArgs e) { this.Close(); } void menuContextModule_Click(object sender, EventArgs e) { ToolStripMenuItem menu = sender as ToolStripMenuItem; if (menu != null) DynamicLink.Launcher.StartModule(menu.Text); } #endregion #region Context menu private void notifyContextAbout_Click(object sender, EventArgs e) { DynamicLink.Launcher.About(); } private void notifyContextSettings_Click(object sender, EventArgs e) { allowShow = true; this.Show(); } private void notifyContextExit_Click(object sender, EventArgs e) { allowExit = true; Application.Exit(); } #endregion #region Other event handlers void Hotkey_HotkeyPressed(object sender, MyKeyEventArgs e) { foreach (var i in moduleShortcuts) if (e.Key == i.Value) { DynamicLink.Launcher.StartModule(i.Key); return; } } private void listModules_SelectedIndexChanged(object sender, EventArgs e) { if (listModules.SelectedItems.Count == 1) keyShortcutBox.Enabled = true; else { keyShortcutBox.Reset(); keyShortcutBox.Enabled = false; } } private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing && !allowExit) { e.Cancel = true; this.Hide(); } else { notify.Visible = false; TibisMathematicsSuite.Properties.Settings.Default.Save(); } } protected override void SetVisibleCore(bool value) { base.SetVisibleCore((allowShow) ? value : false); // We need to reinitialize some settings if (allowShow && value) { moduleShortcuts.Clear(); listModules.Items.Clear(); PopulateModules(); } } #endregion } }