private void Selection_Changed(object sender, EventArgs e)
        {
            TreeIter selectedRow = GetSelectedRow();

            if (!IsSelectionValid(selectedRow))
            {
                return;
            }

            string selectedName = (string)treeViewMenu.Model.GetValue(selectedRow, 2);

            enteredAccelKey = KeyShortcuts.LookupEntry(selectedName);
        }
        private static void AddCustomShortcut(TreeIter parent, TreeStore treeStore, string path, string name, string type)
        {
            // only menu shortcuts supported on the mac because of the GTK "cmd is alt" bug
            if (PlatformHelper.Platform == PlatformTypes.MacOSX &&
                (path.Contains("btn") || path == KeyShortcuts.CHOOSE_KEY || path == KeyShortcuts.HELP_KEY))
            {
                return;
            }

            string   parentPath       = (string)treeStore.GetValue(parent, 2);
            int      indexOfSeparator = path.LastIndexOf('/');
            TreeIter finalParent      = parent;

            if (indexOfSeparator >= 0)
            {
                string parentNode = path.Substring(0, indexOfSeparator);
                if (parentNode != parentPath)
                {
                    for (int i = 0; i < treeStore.IterNChildren(parent); i++)
                    {
                        TreeIter row;
                        treeStore.IterNthChild(out row, parent, i);
                        string child = (string)treeStore.GetValue(row, 2);
                        if (child.Substring(child.LastIndexOf('/') + 1) != parentNode)
                        {
                            continue;
                        }

                        finalParent = row;
                        break;
                    }
                    name = name.Substring(name.LastIndexOf('/') + 1);
                }
            }
            AccelKey key = KeyShortcuts.LookupEntry(path);

            // set "menu" as the type because these custom bindings cannot be the same as some real menu bindings
            if (finalParent.Equals(TreeIter.Zero))
            {
                treeStore.AppendValues(name, KeyShortcuts.KeyToString(key), path, type);
            }
            else
            {
                treeStore.AppendValues(finalParent, name, KeyShortcuts.KeyToString(key), path, type);
            }
        }
        private bool UsedInMenuOrSameScreen(AccelKey newKey)
        {
            TreeIter selectedRow  = GetSelectedRow();
            string   selectedName = (string)treeViewMenu.Model.GetValue(selectedRow, 2);
            string   selectedType = (string)treeViewMenu.Model.GetValue(selectedRow, 3);
            TreeIter ownerRow     = TreeIter.Zero;

            treeViewMenu.Model.Foreach((model, path, row) =>
            {
                string name = (string)model.GetValue(row, 2);
                if (selectedName == name)
                {
                    return(false);
                }

                string type  = (string)model.GetValue(row, 3);
                AccelKey key = KeyShortcuts.LookupEntry(name);
                // when comparing with menus, ignore paths containing a '/' because menus do not have such (in incomplete paths)
                if (selectedType == type && (selectedType != KeyShortcuts.MENU_NEIGHBOURING_SHORTCUT || !name.Contains("/")) &&
                    KeyShortcuts.KeyEqual((uint)newKey.Key, (uint)key.Key) && newKey.AccelMods == key.AccelMods)
                {
                    ownerRow = row;
                    return(true);
                }
                return(false);
            });

            if (!ownerRow.Equals(TreeIter.Zero))
            {
                string translation = Translator.GetString("The selected shortcut is already used for the \"{0}\" menu item. " +
                                                          "Do you want to remove the shortcut for \"{0}\" to reassign it?");
                string message = string.Format(translation, treeViewMenu.Model.GetValue(ownerRow, 0));
                if (Message.ShowDialog(Translator.GetString("Warning!"), string.Empty,
                                       message, "Icons.Question32.png",
                                       MessageButtons.YesNo) == ResponseType.Yes)
                {
                    string ownerItem = (string)treeViewMenu.Model.GetValue(ownerRow, 2);
                    ApplyShortcut(ownerItem, new AccelKey(Key.VoidSymbol, ModifierType.None, AccelFlags.Visible));
                    treeViewMenu.Model.SetValue(ownerRow, 1, KeyShortcuts.KeyToString(AccelKey.Zero));
                    return(false);
                }
                return(true);
            }
            return(false);
        }
        private void ApplyShortcut(string menuItemName, AccelKey accelKey)
        {
            string accelPath = KeyShortcuts.GetAccelPath(menuItemName);

            changedShortcuts.Add(new KeyValuePair <string, AccelKey> (accelPath,
                                                                      new AccelKey(accelKey.Key, KeyShortcuts.GetAllowedModifier(accelKey.AccelMods), AccelFlags.Visible)));
            MenuItemWrapper menuItemWrapper = menu.FindMenuItem(menuItemName);

            if (menuItemWrapper == null)
            {
                return;
            }
            if (accelKey.Key != Key.VoidSymbol)
            {
                return;
            }

            AccelKey key = KeyShortcuts.LookupEntry(menuItemName);

            menuItemWrapper.Item.RemoveAccelerator(accelGroup, (uint)key.Key, key.AccelMods);
        }
Exemple #5
0
        private bool UsedInMenu(int row)
        {
            bool result = true;

            AccelMap.Foreach(dlgEditQuickItems.Handle,
                             (data, accelPath, accelKey, accelMods, changed) =>
            {
                if (changedMenus.Contains(accelPath) || currentKey.Key == 0)
                {
                    return;
                }
                string name = accelPath.Substring(accelPath.IndexOf('/') + 1);
                MenuItemWrapper menuItem = mainMenu.FindMenuItem(name);
                if (menuItem != null && (uint)currentKey.Key == accelKey && currentKey.AccelMods == accelMods)
                {
                    string title       = Translator.GetString("Warning!");
                    string translation = Translator.GetString("The selected shortcut is already used for menu item \"{0}\". " +
                                                              "Do you want to remove the shortcut for \"{0}\" and assign it to \"{1}\"?");
                    string message = string.Format(translation, menuItem.Text, itemShortcuts [row].ItemName);
                    if (Message.ShowDialog(title, string.Empty, message, "Icons.Question32.png",
                                           MessageButtons.YesNo) == ResponseType.Yes)
                    {
                        AccelMap.ChangeEntry(accelPath, (uint)Key.VoidSymbol, 0, true);
                        changedMenus.Add(accelPath);
                        AccelKey key = KeyShortcuts.LookupEntry(name);
                        menuItem.Item.RemoveAccelerator(accelGroup, (uint)key.Key, key.AccelMods);
                    }
                    else
                    {
                        result = false;
                    }
                }
            });
            if (!result)
            {
                return(false);
            }
            return(true);
        }
        private void AddNodes(TreeStore treeStore, IEnumerable <Widget> menuItems, TreeIter parent)
        {
            // TODO: this method, and some of its callees, use too many hard-coded strings, improve this
            string parentName = string.Empty;

            foreach (MenuItem menuItem in menuItems)
            {
                Label label = menuItem.Child as Label;
                if (label == null)
                {
                    continue;
                }

                if (BusinessDomain.RestrictionTree.GetRestriction(menuItem.Name) != UserRestrictionState.Allowed)
                {
                    continue;
                }

                AccelKey key = KeyShortcuts.LookupEntry(menuItem.Name);
                parentName = menuItem.Parent.Name;
                TreeIter row = parent.Equals(TreeIter.Zero) ?
                               treeStore.AppendValues(label.Text, KeyShortcuts.KeyToString(key), menuItem.Name, KeyShortcuts.MENU_NEIGHBOURING_SHORTCUT) :
                               treeStore.AppendValues(parent, label.Text, KeyShortcuts.KeyToString(key), menuItem.Name, KeyShortcuts.MENU_NEIGHBOURING_SHORTCUT);

                Container container = menuItem.Submenu as Container;
                if (container != null)
                {
                    AddNodes(treeStore, (container).Children, row);
                }

                switch (menuItem.Name)
                {
                case "mnuEditPartners":
                case "mnuEditGoods":
                case "mnuEditUsers":
                case "mnuEditObjects":
                case "mnuEditVATGroups":
                case "mnuEditDevices":
                case "mnuEditAdminPriceRules":
                    AddEditDialogShortcuts(row, treeStore, menuItem.Name);
                    break;

                case "mnuOperTradeObject":
                    AddPOSShortcuts(row, treeStore, menuItem.Name);
                    break;
                }

                MenuItem item = menuItem;
                foreach (ICustomKeyShortcut shortcut in
                         from customKeyShortcut in KeyShortcuts.CustomKeyShortcuts
                         where customKeyShortcut.Parent == item.Name
                         orderby customKeyShortcut.Ordinal
                         select customKeyShortcut)
                {
                    AddCustomShortcut(row, treeStore, shortcut.Path, shortcut.Label, shortcut.Type);
                }
            }

            if (parentName == menu.Menu.Name)
            {
                AddCustomShortcut(parent, treeStore, KeyShortcuts.CHOOSE_KEY, Translator.GetString("Select"), KeyShortcuts.MENU_NEIGHBOURING_SHORTCUT);
            }

            if (parentName == "mnuOperations_menu")
            {
                AddCustomShortcut(parent, treeStore, "txtPartner", Translator.GetString("Partner"), KeyShortcuts.MENU_NEIGHBOURING_SHORTCUT);
                AddCustomShortcut(parent, treeStore, "btnSave", Translator.GetString("Save Operation"), KeyShortcuts.MENU_NEIGHBOURING_SHORTCUT);
                AddCustomShortcut(parent, treeStore, "btnClear", Translator.GetString("Clear Operation"), KeyShortcuts.MENU_NEIGHBOURING_SHORTCUT);
            }

            if (parentName == "mnuHelp_menu")
            {
                AddCustomShortcut(parent, treeStore, KeyShortcuts.HELP_KEY, Translator.GetString("Show Help"), KeyShortcuts.MENU_NEIGHBOURING_SHORTCUT);
            }
        }