private void deleteButton_Click(object sender, EventArgs e)
        {
            if (currentlySelectedNode != null)
            {
                //
                // Fetch parent node
                //
                TreeNode parentNode = currentlySelectedNode.Parent;

                if (parentNode == globalNode)
                {
                    globalActions.Remove(currentlySelectedNode.Tag);
                }
                else if (parentNode == windowsNode)
                {
                    windowActions.Remove(currentlySelectedNode.Tag);
                }
                else if (parentNode.Tag is ActionWindow)
                {
                    ActionWindow window = parentNode.Tag as ActionWindow;

                    window.Actions.Remove(currentlySelectedNode.Tag);
                }

                parentNode.Nodes.Remove(currentlySelectedNode);
            }
        }
        private void idComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (currentlySelectedNode != null)
            {
                //
                // Update the actual node
                //
                if (currentlySelectedNode.Tag is ActionWindow)
                {
                    ActionWindow window = currentlySelectedNode.Tag as ActionWindow;
                    //          window.Id = Convert.ToInt32(idTextBox.Text);
                    //
                    //          currentlySelectedNode.Text = window.Description + " (" + window.Id + ")";
                }
                else if (currentlySelectedNode.Tag is KeyAction)
                {
                    if (idComboBox.Text != null)
                    {
                        KeyAction action = currentlySelectedNode.Tag as KeyAction;

                        try
                        {
                            Action.ActionType actionType = (Action.ActionType)Enum.Parse(typeof(Action.ActionType), idComboBox.Text);

                            action.Id = (int)actionType;

                            currentlySelectedNode.Text          = action.Description + " (" + action.Id + ")";
                            currentlySelectedNode.Nodes[0].Text = String.Format("Action = " + GetActionName(action.Id));
                        }
                        catch { }
                    }
                }
            }
        }
        private void descriptionTextBox_TextChanged(object sender, EventArgs e)
        {
            if (currentlySelectedNode != null)
            {
                int id = 0;

                //
                // Update the actual node
                //
                if (currentlySelectedNode.Tag is ActionWindow)
                {
                    ActionWindow window = currentlySelectedNode.Tag as ActionWindow;
                    window.Description = descriptionTextBox.Text;
                    id = window.Id;
                }
                else if (currentlySelectedNode.Tag is KeyAction)
                {
                    KeyAction action = currentlySelectedNode.Tag as KeyAction;
                    action.Description = descriptionTextBox.Text;
                    id = action.Id;
                }

                //
                // Update visible stuff
                //
                currentlySelectedNode.Text = String.Format("{0} ({1})", descriptionTextBox.Text, id);
            }
        }
        private void idTextBox_TextChanged(object sender, EventArgs e)
        {
            if (currentlySelectedNode != null)
            {
                //
                // Update the actual node
                //
                if (currentlySelectedNode.Tag is ActionWindow)
                {
                    ActionWindow window = currentlySelectedNode.Tag as ActionWindow;

                    window.Id = idTextBox.Text.Length > 0 ? Convert.ToInt32(idTextBox.Text) : 0;

                    currentlySelectedNode.Text = window.Description + " (" + window.Id + ")";
                }
            }
        }
    private void addButton_Click(object sender, EventArgs e)
    {
      if (currentlySelectedNode != null)
      {
        if (currentlySelectedNode == globalNode)
        {
          //
          // Add an action to the global node
          //
          KeyAction action = new KeyAction(0, "", "<New Action>", "");
          globalActions.Add(action);

          TreeNode actionNode = new TreeNode(action.Description + " (" + action.Id + ")");

          actionNode.Nodes.Add("Action = " + GetActionName(action.Id));
          actionNode.Nodes.Add("Key = " + action.Key);
          actionNode.Nodes.Add("Sound = " + action.Sound);

          actionNode.Tag = action;

          globalNode.Nodes.Add(actionNode);

          //
          // Make node visible and select it
          //
          actionNode.EnsureVisible();
          keyTreeView.SelectedNode = actionNode;
        }
        else if (currentlySelectedNode == windowsNode)
        {
          //
          // Add window
          //
          ActionWindow window = new ActionWindow();
          windowActions.Add(window);

          window.Id = 0;
          window.Description = "<New Window>";

          TreeNode windowNode = new TreeNode(window.Description + " (" + window.Id + ")");

          windowNode.Tag = window;

          windowsNode.Nodes.Add(windowNode);

          //
          // Make node visible and select it
          //
          windowNode.EnsureVisible();
          keyTreeView.SelectedNode = windowNode;
        }
        else if (currentlySelectedNode.Tag is ActionWindow)
        {
          ActionWindow window = currentlySelectedNode.Tag as ActionWindow;

          //
          // Add an action to the selected window
          //
          KeyAction action = new KeyAction(0, "", "<New Action>", "");
          window.Actions.Add(action);

          TreeNode actionNode = new TreeNode(action.Description + " (" + action.Id + ")");

          actionNode.Nodes.Add("Action = " + GetActionName(action.Id));
          actionNode.Nodes.Add("Key = " + action.Key);
          actionNode.Nodes.Add("Sound = " + action.Sound);

          actionNode.Tag = action;

          currentlySelectedNode.Nodes.Add(actionNode);

          //
          // Make node visible and select it
          //
          actionNode.EnsureVisible();
          keyTreeView.SelectedNode = actionNode;
        }
      }
    }
    private void LoadKeys()
    {
      XmlDocument document = new XmlDocument();

      try
      {
        // Load the xml document
        string keyConfigFile = Config.GetFile(Config.Dir.Config, "keymap.xml");
        if (!File.Exists(keyConfigFile))
        {
          keyConfigFile = Config.GetFile(Config.Dir.Base, "keymap.xml");
        }

        if (File.Exists(keyConfigFile))
        {
          document.Load(keyConfigFile);
        }
        else
        {
          Log.Error(
            "GeneralKeys: Error loading Keymap.xml - make sure you've got a valid file in your base or config directory!");
          return;
        }

        XmlElement rootElement = document.DocumentElement;
        // Make sure we're loading a keymap
        if (rootElement != null && rootElement.Name.Equals("keymap"))
        {
          // Fetch global actions
          XmlNodeList nodeList = rootElement.SelectNodes("/keymap/global/action");

          foreach (XmlNode node in nodeList)
          {
            AddActionNode(ref globalActions, node);
          }
          // Fetch all windows
          nodeList = rootElement.SelectNodes("/keymap/window");

          foreach (XmlNode node in nodeList)
          {
            // Allocate new window
            ActionWindow window = new ActionWindow();
            // Fetch description and id for this window
            XmlNode idNode = node.SelectSingleNode("id");
            XmlNode descriptionNode = node.SelectSingleNode("description");

            window.Description = descriptionNode.InnerText;
            window.Id = Convert.ToInt32(idNode.InnerText.Length > 0 ? idNode.InnerText : "0");

            XmlNodeList actionNodeList = node.SelectNodes("action");

            foreach (XmlNode actionNode in actionNodeList)
            {
              AddActionNode(ref window.Actions, actionNode);
            }
            // Add to the window list
            windowActions.Add(window);
          }
        }
      }
      catch (Exception e)
      {
        Debug.WriteLine(e.Message);
      }
    }
        private void addButton_Click(object sender, EventArgs e)
        {
            if (currentlySelectedNode != null)
            {
                if (currentlySelectedNode == globalNode)
                {
                    //
                    // Add an action to the global node
                    //
                    KeyAction action = new KeyAction(0, "", "<New Action>", "");
                    globalActions.Add(action);

                    TreeNode actionNode = new TreeNode(action.Description + " (" + action.Id + ")");

                    actionNode.Nodes.Add("Action = " + GetActionName(action.Id));
                    actionNode.Nodes.Add("Key = " + action.Key);
                    actionNode.Nodes.Add("Sound = " + action.Sound);

                    actionNode.Tag = action;

                    globalNode.Nodes.Add(actionNode);

                    //
                    // Make node visible and select it
                    //
                    actionNode.EnsureVisible();
                    keyTreeView.SelectedNode = actionNode;
                }
                else if (currentlySelectedNode == windowsNode)
                {
                    //
                    // Add window
                    //
                    ActionWindow window = new ActionWindow();
                    windowActions.Add(window);

                    window.Id          = 0;
                    window.Description = "<New Window>";

                    TreeNode windowNode = new TreeNode(window.Description + " (" + window.Id + ")");

                    windowNode.Tag = window;

                    windowsNode.Nodes.Add(windowNode);

                    //
                    // Make node visible and select it
                    //
                    windowNode.EnsureVisible();
                    keyTreeView.SelectedNode = windowNode;
                }
                else if (currentlySelectedNode.Tag is ActionWindow)
                {
                    ActionWindow window = currentlySelectedNode.Tag as ActionWindow;

                    //
                    // Add an action to the selected window
                    //
                    KeyAction action = new KeyAction(0, "", "<New Action>", "");
                    window.Actions.Add(action);

                    TreeNode actionNode = new TreeNode(action.Description + " (" + action.Id + ")");

                    actionNode.Nodes.Add("Action = " + GetActionName(action.Id));
                    actionNode.Nodes.Add("Key = " + action.Key);
                    actionNode.Nodes.Add("Sound = " + action.Sound);

                    actionNode.Tag = action;

                    currentlySelectedNode.Nodes.Add(actionNode);

                    //
                    // Make node visible and select it
                    //
                    actionNode.EnsureVisible();
                    keyTreeView.SelectedNode = actionNode;
                }
            }
        }
        private void keyTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            currentlySelectedNode = e.Node;

            addButton.Enabled = currentlySelectedNode != null &&
                                (!(currentlySelectedNode.Tag is KeyAction) && currentlySelectedNode.Tag != null);

            if (addButton.Enabled == false)
            {
                addButton.Enabled = currentlySelectedNode == globalNode || currentlySelectedNode == windowsNode;
            }

            //
            // Enable/Disable controls
            //
            deleteButton.Enabled              =
                keyTextBox.Enabled            =
                    fileNameButton.Enabled    =
                        idComboBox.Enabled    =
                            idTextBox.Enabled = soundTextBox.Enabled = descriptionTextBox.Enabled = e.Node.Tag is KeyAction;

            if (deleteButton.Enabled == false)
            {
                deleteButton.Enabled = currentlySelectedNode.Tag is ActionWindow;
            }

            if (e.Node.Tag is ActionWindow)
            {
                //
                // Enable correct controls
                //
                idTextBox.Visible  = true;
                idComboBox.Visible = false;

                idTextBox.Enabled = true;

                ActionWindow window = e.Node.Tag as ActionWindow;

                descriptionTextBox.Text = window.Description;
                idTextBox.Text          = window.Id.ToString();

                keyTextBox.Text   = string.Empty;
                soundTextBox.Text = string.Empty;

                descriptionTextBox.Enabled = idComboBox.Enabled = true;
            }
            else if (e.Node.Tag is KeyAction)
            {
                //
                // Enable correct controls
                //
                idTextBox.Visible  = false;
                idComboBox.Visible = true;

                KeyAction action = e.Node.Tag as KeyAction;

                descriptionTextBox.Text = action.Description;
                idComboBox.Text         = GetActionName(action.Id);
                keyTextBox.Text         = action.Key;
                soundTextBox.Text       = action.Sound;
            }
            else
            {
                //
                // None of the above
                //
            }
        }
        private void LoadKeys()
        {
            XmlDocument document = new XmlDocument();

            try
            {
                // Load the xml document
                string keyConfigFile = Config.GetFile(Config.Dir.Config, "keymap.xml");
                if (!File.Exists(keyConfigFile))
                {
                    keyConfigFile = Config.GetFile(Config.Dir.Base, "keymap.xml");
                }

                if (File.Exists(keyConfigFile))
                {
                    document.Load(keyConfigFile);
                }
                else
                {
                    Log.Error(
                        "GeneralKeys: Error loading Keymap.xml - make sure you've got a valid file in your base or config directory!");
                    return;
                }

                XmlElement rootElement = document.DocumentElement;
                // Make sure we're loading a keymap
                if (rootElement != null && rootElement.Name.Equals("keymap"))
                {
                    // Fetch global actions
                    XmlNodeList nodeList = rootElement.SelectNodes("/keymap/global/action");

                    foreach (XmlNode node in nodeList)
                    {
                        AddActionNode(ref globalActions, node);
                    }
                    // Fetch all windows
                    nodeList = rootElement.SelectNodes("/keymap/window");

                    foreach (XmlNode node in nodeList)
                    {
                        // Allocate new window
                        ActionWindow window = new ActionWindow();
                        // Fetch description and id for this window
                        XmlNode idNode          = node.SelectSingleNode("id");
                        XmlNode descriptionNode = node.SelectSingleNode("description");

                        window.Description = descriptionNode.InnerText;
                        window.Id          = Convert.ToInt32(idNode.InnerText.Length > 0 ? idNode.InnerText : "0");

                        XmlNodeList actionNodeList = node.SelectNodes("action");

                        foreach (XmlNode actionNode in actionNodeList)
                        {
                            AddActionNode(ref window.Actions, actionNode);
                        }
                        // Add to the window list
                        windowActions.Add(window);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }