private void GenerateGui() { var s = config.GetSections().ToList(); s.Sort(); foreach (var section in s) { var page = new TabPage(Utilities.GetPrettyName(section)); var panel = new TableLayoutPanel { Dock = DockStyle.Fill, ColumnCount = 2 }; var lastComment = ""; var i = 0; foreach (var option in config.EnumSection(section)) { if (!new[] { "#", ";", "//" }.Any(c => option.StartsWith(c))) { var l = new Label { Text = Utilities.GetPrettyName(option) + ":", TextAlign = ContentAlignment.MiddleLeft, //Width = tabControlMain.Width/4, AutoEllipsis = true }; panel.SetColumn(l, 0); panel.SetRow(l, i); panel.Controls.Add(l); Control t = null; var defaultValue = config.GetSetting(section, option); var tooltip = ""; // Check previous comment if (!String.IsNullOrEmpty(lastComment)) { var tmp = lastComment.Trim().Split(new[] { ']' }, 2); if (tmp.Length > 1) tooltip = tmp[1].Trim(); var m = Regex.Match(lastComment, @"\[(?<min>[-]?[\d.]{1,8}):(?<max>[-]?[\d.]{1,8})\]"); if (m.Success) { t = new NumericUpDown { Minimum = decimal.Parse(m.Groups["min"].Value), Maximum = decimal.Parse(m.Groups["max"].Value), Value = decimal.Parse(defaultValue) }; ((NumericUpDown)t).ValueChanged += (o, args) => { config.AddSetting(section, option, ((NumericUpDown)o).Value.ToString()); }; } else { m = Regex.Match(lastComment, @"\[(?<value>.+,.+)\]"); if (m.Success) { t = new ComboBox { DropDownStyle = ComboBoxStyle.DropDownList, Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right }; var items = new List<String>(); var description = new List<string>(); foreach (var v in m.Groups["value"].Value.Split(new[] { ',' })) { String name, value; if (v.Contains(':')) { var parts = v.Split(new[] { ':' }, 2); name = parts[1]; value = parts[0]; description.Add(String.Format(" {0} ({1})", name, value)); } else { name = v; value = v; } items.Add(value); ((ComboBox)t).Items.Add(name); if (defaultValue.Equals(value)) { ((ComboBox)t).SelectedIndex = ((ComboBox)t).Items.Count - 1; } } if (description.Count > 0) { if (!String.IsNullOrEmpty(tooltip)) tooltip += Environment.NewLine + Environment.NewLine; tooltip += "Available options:" + Environment.NewLine + String.Join(Environment.NewLine, description); } t.Tag = items; // No default value match if (((ComboBox)t).SelectedIndex < 0) { ((ComboBox)t).DropDownStyle = ComboBoxStyle.DropDown; t.Text = defaultValue; } ((ComboBox)t).SelectedValueChanged += (o, args) => { var c = o as ComboBox; if (c != null) { string v = null; if (c.Tag != null) { var list = c.Tag as List<string>; if (list != null) v = list[c.SelectedIndex]; } if(v == null) v = c.SelectedValue.ToString(); config.AddSetting(section, option, v); } }; } } } if (t == null) { t = new TextBox { Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right, Text = defaultValue /*, Width = (int) (tabControlMain.Width / 1.5)*/ }; t.TextChanged += (o, args) => { config.AddSetting(section, option, ((TextBox) o).Text); }; } panel.SetColumn(t, 1); panel.SetRow(l, i++); panel.Controls.Add(t); if (!String.IsNullOrEmpty(tooltip) && toolTipInfo != null) toolTipInfo.SetToolTip(t, tooltip); lastComment = ""; } else lastComment = option; } if (panel.Controls.Count > 0) { panel.AutoScroll = true; page.Controls.Add(panel); tabControlMain.Controls.Add(page); } else { page.Dispose(); panel.Dispose(); } } }
/// <summary> /// Clears the current TableLayoutPanel, removes it from the controls, /// adds new KeyButtons, then adds the TableLayoutPanel to the Controls. /// It resizes the client area to fit the size in the Configuration. /// </summary> protected void createTable() { if (keyDefs == null) { Utils.errMsg("No keys are defined"); return; } // Get the table size int rows = config.Rows; int cols = config.Cols; Debug.Print("createTable: rows=" + rows + " cols=" + cols); // Remove any existing tableLayoutPanel if (this.Controls.Contains(tableLayoutPanel)) { tableLayoutPanel.Controls.Clear(); tableLayoutPanel.RowStyles.Clear(); tableLayoutPanel.ColumnStyles.Clear(); Controls.Remove(tableLayoutPanel); tableLayoutPanel.Dispose(); } ClientSize = new Size(config.Size.Width, config.Size.Height); tableLayoutPanel = new System.Windows.Forms.TableLayoutPanel(); tableLayoutPanel.AutoSize = true; tableLayoutPanel.ColumnCount = cols; tableLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill; tableLayoutPanel.Location = new System.Drawing.Point(0, 0); tableLayoutPanel.Name = "tableLayoutPanel"; tableLayoutPanel.RowCount = rows; tableLayoutPanel.TabIndex = 0; tableLayoutPanel.Margin = new Padding(0); // Font string fontName = config.FontName; float fontSize = config.FontSize; FontStyle fontStyle = config.getFontStyle(); Font font = null; if (fontSize <= 0) { fontSize = defaultFontSize; } if (config.FontName == null || config.FontName.Length <= 0) { fontName = defaultFontName; // If the name is invalid, then the FontStyle is invalid fontStyle = defaultFontStyle; } try { font = new Font(fontName, fontSize, fontStyle); tableLayoutPanel.Font = font; config.FontName = font.Name; config.FontSize = font.SizeInPoints; if (!font.Name.Equals(fontName) || !font.Size.Equals(config.FontSize)) { Utils.errMsg("Error loading " + fontName + " " + config.FontSize + " pt" + LF + "Using " + font.Name + " " + font.SizeInPoints + " pt"); } } catch (Exception ex) { Utils.excMsg("Cannot create font " + fontName + " " + config.FontSize + " pt", ex); } #if false Debug.Print("tableLayoutPanel: margin=" + tableLayoutPanel.Margin); Debug.Print("tableLayoutPanel: padding=" + tableLayoutPanel.Padding); #endif // Set the rows and columns in the table tableLayoutPanel.RowCount = rows; tableLayoutPanel.ColumnCount = cols; tableLayoutPanel.AutoSize = true; tableLayoutPanel.Dock = DockStyle.Fill; float rowPercent = 100.0F / rows; float colPercent = 100.0F / cols; for (int row = 0; row < rows; row++) { this.tableLayoutPanel.RowStyles. Add(new RowStyle(SizeType.Percent, rowPercent)); } for (int col = 0; col < cols; col++) { tableLayoutPanel.ColumnStyles. Add(new System.Windows.Forms.ColumnStyle( System.Windows.Forms.SizeType.Percent, colPercent)); } // Set the key definitions into the table KeyButton keyButton; int buttonWidth = config.Size.Width / config.Cols; if (buttonWidth <= 0) { buttonWidth = 50; } int buttonHeight = config.Size.Height / config.Rows; if (buttonHeight <= 0) { buttonHeight = 50; } foreach (KeyDef keyDef in keyDefs) { keyButton = new KeyButton(keyDef); if (keyDef.Type == KeyDef.KeyType.UNUSED) { keyButton.Text = ""; //keyButton.BackColor = // Color.FromKnownColor(KnownColor.ControlLight); } else { keyButton.Text = keyDef.Name; }// Colors if (config.isValidFGColorString()) { keyButton.ForeColor = config.getFgColor(); } if (config.isValidBGColorString()) { keyButton.BackColor = config.getBgColor(); } keyButton.Dock = DockStyle.Fill; keyButton.Margin = new Padding(0); keyButton.Width = buttonWidth; keyButton.Height = buttonHeight; keyButton.AutoSize = true; keyButton.AutoSizeMode = AutoSizeMode.GrowAndShrink; tableLayoutPanel.Controls.Add(keyButton, keyDef.Col, keyDef.Row); #if False Debug.Print("keyButton: AutoSize=" + keyButton.AutoSize); Debug.Print("keyButton: MinimumSize=" + keyButton.MinimumSize); Debug.Print("keyButton: AutoSizeMode=" + keyButton.AutoSizeMode); #endif } Controls.Add(tableLayoutPanel); }