private void FindInUseKeyboardAccelerators(IEnumerable controls) { const char amp = '&'; foreach (var control in controls) { if (!(control is Control ctrl)) { if (control is ToolStripItem menuItem) { var index = menuItem.Text.IndexOf(amp); if (index >= 0) { InUseKeyboardAccelerators.Add(Char.ToLower(menuItem.Text[index + 1])); } } continue; } if (!(ctrl is TextBox)) // Don't mess with the user edit area { var index = ctrl.Text.IndexOf(amp); if (index >= 0) { InUseKeyboardAccelerators.Add(Char.ToLower(ctrl.Text[index + 1])); } } // Special controls if (control is GroupBox gb) { FindInUseKeyboardAccelerators(gb.Controls); // Handle controls inside the groupbox } else if (control is SplitContainer sc) { FindInUseKeyboardAccelerators(sc.Controls); // Handle controls inside the splits } else if (control is SplitterPanel sp) { FindInUseKeyboardAccelerators(sp.Controls); // Handle controls inside the splits } } }
public string TranslateString(string text) // Public for test purposes { if (ModeUI == SrmDocument.DOCUMENT_TYPE.proteomic || string.IsNullOrEmpty(text)) { return(text); } var noAmp = text.Replace(@"&", String.Empty); if (TRANSLATION_TABLE.Any(kvp => noAmp.Contains(kvp.Value))) // Avoid "p&eptides are a kind of molecule" => "mol&ecules are a kind of molecule" { return(text); } foreach (var kvp in TRANSLATION_TABLE) { // Replace each occurrence, but be careful not to change &Peptide to &Molecule for (var i = 0; ;) { i = text.IndexOf(kvp.Key, i, StringComparison.Ordinal); if (i >= 0) // Found something to replace { if (!(i > 0 && text[i - 1] == '&')) // Watch for & before match - if we wanted to match it we already would have since table is sorted long to short { text = text.Substring(0, i) + kvp.Value + text.Substring(i + kvp.Key.Length); i += kvp.Value.Length; } else { i += kvp.Key.Length; } } else { break; } } } // Did we get tripped up by & keyboard accelerators? noAmp = text.Replace(@"&", String.Empty); if (InUseKeyboardAccelerators != null && TRANSLATION_TABLE.Any(kvp => noAmp.Contains(kvp.Key))) { // See if the proposed replacement has any letters that aren't in use as accelerators elsewhere noAmp = TranslateString(noAmp); var accel = noAmp.FirstOrDefault(c => Char.IsLetterOrDigit(c) && !InUseKeyboardAccelerators.Contains(Char.ToLower(c))); var index = noAmp.IndexOf(accel); if (index >= 0) { var indexU = noAmp.IndexOf(Char.ToUpper(accel)); // Prefer upper case text = noAmp.Insert((indexU >= 0) ? indexU : index, @"&"); InUseKeyboardAccelerators.Add(Char.ToLower(accel)); } } // If no keyboard accelerator available, proceed without one if (TRANSLATION_TABLE.Any(kvp => text.Contains(kvp.Key))) { return(noAmp); } return(text); }