1 using Mesen.GUI.Config; 2 using System; 3 using System.Collections.Generic; 4 using System.ComponentModel; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace Mesen.GUI.Forms 12 { 13 public class BaseForm : Form 14 { ProcessCmdKeyHandler(Keys keyData, ref bool processed)15 public delegate void ProcessCmdKeyHandler(Keys keyData, ref bool processed); 16 public event ProcessCmdKeyHandler OnProcessCmdKey; 17 18 protected ToolTip toolTip; 19 private System.ComponentModel.IContainer components; 20 private bool _iconSet = false; 21 protected int _inMenu = 0; 22 private static Timer _tmrUpdateBackground; 23 private static bool _needResume = false; 24 BaseForm()25 public BaseForm() 26 { 27 InitializeComponent(); 28 } 29 30 protected virtual bool IsConfigForm { get { return false; } } 31 StartBackgroundTimer()32 public static void StartBackgroundTimer() 33 { 34 _tmrUpdateBackground = new Timer(); 35 _tmrUpdateBackground.Start(); 36 _tmrUpdateBackground.Tick += tmrUpdateBackground_Tick; 37 } 38 StopBackgroundTimer()39 public static void StopBackgroundTimer() 40 { 41 _tmrUpdateBackground?.Stop(); 42 } 43 tmrUpdateBackground_Tick(object sender, EventArgs e)44 private static void tmrUpdateBackground_Tick(object sender, EventArgs e) 45 { 46 Form focusedForm = null; 47 foreach(Form form in Application.OpenForms) { 48 if(form.ContainsFocus) { 49 focusedForm = form; 50 break; 51 } 52 } 53 54 bool needPause = focusedForm == null && ConfigManager.Config.PreferenceInfo.PauseWhenInBackground; 55 if(focusedForm != null) { 56 needPause |= ConfigManager.Config.PreferenceInfo.PauseWhenInMenusAndConfig && focusedForm is BaseForm && (((BaseForm)focusedForm)._inMenu > 0 || ((BaseForm)focusedForm).IsConfigForm); 57 needPause |= ConfigManager.Config.PreferenceInfo.PauseWhenInMenusAndConfig && !(focusedForm is BaseInputForm) && !focusedForm.GetType().FullName.Contains("Debugger"); 58 needPause |= ConfigManager.Config.PreferenceInfo.PauseWhenInDebuggingTools && focusedForm.GetType().FullName.Contains("Debugger"); 59 } 60 61 if(needPause) { 62 if(!InteropEmu.IsPaused(InteropEmu.ConsoleId.Master)) { 63 _needResume = true; 64 InteropEmu.Pause(InteropEmu.ConsoleId.Master); 65 } 66 } else if(_needResume) { 67 InteropEmu.Resume(InteropEmu.ConsoleId.Master); 68 _needResume = false; 69 } 70 71 InteropEmu.SetFlag(EmulationFlags.InBackground, focusedForm == null); 72 } 73 ProcessCmdKey(ref Message msg, Keys keyData)74 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 75 { 76 bool processed = false; 77 OnProcessCmdKey?.Invoke(keyData, ref processed); 78 return processed || base.ProcessCmdKey(ref msg, keyData); 79 } 80 Show(object sender, IWin32Window owner = null)81 public void Show(object sender, IWin32Window owner = null) 82 { 83 if(sender is ToolStripMenuItem) { 84 ToolStripItem menuItem = (ToolStripMenuItem)sender; 85 if(menuItem.Image == null) { 86 menuItem = menuItem.OwnerItem; 87 } 88 this.Icon = menuItem.Image; 89 } 90 91 CenterOnParent(owner); 92 base.Show(); 93 } 94 CenterOnParent(IWin32Window owner)95 private void CenterOnParent(IWin32Window owner) 96 { 97 Form parent = (Form)owner; 98 Point point = parent.PointToScreen(new Point(parent.Width / 2, parent.Height / 2)); 99 100 this.StartPosition = FormStartPosition.Manual; 101 this.Top = point.Y - this.Height / 2; 102 this.Left = point.X - this.Width / 2; 103 } 104 ShowDialog(object sender, IWin32Window owner = null)105 public DialogResult ShowDialog(object sender, IWin32Window owner = null) 106 { 107 if(sender is ToolStripMenuItem) { 108 ToolStripItem menuItem = (ToolStripMenuItem)sender; 109 if(menuItem.Image == null) { 110 menuItem = menuItem.OwnerItem; 111 } 112 this.Icon = menuItem.Image; 113 } 114 return base.ShowDialog(owner); 115 } 116 OnLoad(EventArgs e)117 protected override void OnLoad(EventArgs e) 118 { 119 base.OnLoad(e); 120 121 if(!DesignMode) { 122 if(!_iconSet) { 123 base.Icon = Properties.Resources.MesenIcon; 124 } 125 } 126 127 int tabIndex = 0; 128 ThemeHelper.FixMonoColors(this); 129 InitializeTabIndexes(this, ref tabIndex); 130 ResourceHelper.ApplyResources(this); 131 } 132 InitializeTabIndexes(TableLayoutPanel tlp, ref int tabIndex)133 private void InitializeTabIndexes(TableLayoutPanel tlp, ref int tabIndex) 134 { 135 tlp.TabIndex = tabIndex; 136 tabIndex++; 137 138 for(int i = 0; i < tlp.RowCount; i++) { 139 for(int j = 0; j < tlp.ColumnCount; j++) { 140 Control ctrl = tlp.GetControlFromPosition(j, i); 141 if(ctrl != null) { 142 if(ctrl is TableLayoutPanel) { 143 InitializeTabIndexes(((TableLayoutPanel)ctrl), ref tabIndex); 144 } else { 145 InitializeTabIndexes(ctrl, ref tabIndex); 146 } 147 } 148 } 149 } 150 } 151 InitializeTabIndexes(Control container, ref int tabIndex)152 private void InitializeTabIndexes(Control container, ref int tabIndex) 153 { 154 container.TabIndex = tabIndex; 155 tabIndex++; 156 157 foreach(Control ctrl in container.Controls) { 158 if(ctrl is TableLayoutPanel) { 159 InitializeTabIndexes(((TableLayoutPanel)ctrl), ref tabIndex); 160 } else { 161 InitializeTabIndexes(ctrl, ref tabIndex); 162 } 163 } 164 } 165 166 public new Image Icon 167 { 168 set 169 { 170 if(value != null) { 171 Bitmap b = new Bitmap(value); 172 Icon i = System.Drawing.Icon.FromHandle(b.GetHicon()); 173 base.Icon = i; 174 i.Dispose(); 175 176 _iconSet = true; 177 } 178 } 179 } 180 181 public new SizeF AutoScaleDimensions 182 { 183 set 184 { 185 if(!Program.IsMono) { 186 base.AutoScaleDimensions = value; 187 } 188 } 189 } 190 191 public new AutoScaleMode AutoScaleMode 192 { 193 set { 194 if(Program.IsMono) { 195 base.AutoScaleMode = AutoScaleMode.None; 196 } else { 197 base.AutoScaleMode = value; 198 } 199 } 200 } 201 InitializeComponent()202 private void InitializeComponent() 203 { 204 this.components = new System.ComponentModel.Container(); 205 this.toolTip = new System.Windows.Forms.ToolTip(this.components); 206 this.SuspendLayout(); 207 // 208 // toolTip 209 // 210 this.toolTip.AutomaticDelay = 0; 211 this.toolTip.AutoPopDelay = 32700; 212 this.toolTip.InitialDelay = 10; 213 this.toolTip.ReshowDelay = 10; 214 // 215 // BaseForm 216 // 217 this.Name = "BaseForm"; 218 this.ResumeLayout(false); 219 220 } 221 } 222 } 223