-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathFormBase.cs
More file actions
149 lines (127 loc) · 4.66 KB
/
FormBase.cs
File metadata and controls
149 lines (127 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#nullable enable
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using BizHawk.Bizware.Graphics;
using BizHawk.Client.Common;
using BizHawk.Common;
namespace BizHawk.Client.EmuHawk
{
public class FormBase : Form
{
private const string PLACEHOLDER_TITLE = "(will take value from WindowTitle/WindowTitleStatic)";
/// <summary>removes transparency from an image by combining it with a solid background</summary>
public static Image FillImageBackground(Image img, Color c)
{
var result = BitmapBuffer.CreateBitmapObject(img.Size);
using var g = Graphics.FromImage(result);
g.Clear(c);
g.DrawImage(img, x: 0, y: 0, width: img.Width, height: img.Height);
return result;
}
/// <summary>
/// Under Mono, <see cref="SystemColors.Control">SystemColors.Control</see> returns an ugly beige.<br/>
/// This method recursively replaces the <see cref="Control.BackColor"/> of the given <paramref name="control"/> (can be a <see cref="Form"/>) with <see cref="Color.WhiteSmoke"/>
/// iff they have the default of <see cref="SystemColors.Control">SystemColors.Control</see>.<br/>
/// (Also adds a custom <see cref="ToolStrip.Renderer"/> to <see cref="ToolStrip">ToolStrips</see> to change their colors.)
/// </summary>
public static void FixBackColorOnControls(Control control)
{
if (control.BackColor == SystemColors.Control) control.BackColor = Color.WhiteSmoke;
foreach (Control c1 in control.Controls)
{
if (c1 is ToolStrip ts) ts.Renderer = GlobalToolStripRenderer;
else FixBackColorOnControls(c1);
}
}
public static readonly ToolStripSystemRenderer GlobalToolStripRenderer = new();
private string? _windowTitleStatic;
public virtual bool BlocksInputWhenFocused
=> true;
public bool MenuIsOpen { get; private set; }
public Config? Config { get; set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override string Text
{
get => base.Text;
set
{
if (DesignMode) return;
throw new InvalidOperationException("window title can only be changed by calling " + nameof(UpdateWindowTitle) + " (which calls " + nameof(WindowTitle) + " getter)");
}
}
protected virtual string WindowTitle => WindowTitleStatic;
/// <remarks>To enforce the "static title" semantics for implementations, this getter will be called once and cached.</remarks>
protected virtual string WindowTitleStatic
{
get
{
if (DesignMode) return PLACEHOLDER_TITLE;
#pragma warning disable CA1065 // yes, really throw
throw new NotImplementedException("you have to implement this; the Designer prevents this from being an abstract method");
#pragma warning restore CA1065
}
}
public FormBase() => base.Text = PLACEHOLDER_TITLE;
protected override void OnLoad(EventArgs e)
{
try
{
base.OnLoad(e);
}
catch (Exception ex)
{
using ExceptionBox box = new(ex);
box.ShowDialog(owner: this);
Close();
return;
}
if (OSTailoredCode.IsUnixHost) FixBackColorOnControls(this);
UpdateWindowTitle();
if (MainMenuStrip != null)
{
MainMenuStrip.MenuActivate += (_, _) => MenuIsOpen = true;
MainMenuStrip.MenuDeactivate += (_, _) => MenuIsOpen = false;
}
}
public void UpdateWindowTitle()
=> base.Text = Config?.UseStaticWindowTitles == true
? (_windowTitleStatic ??= WindowTitleStatic)
: WindowTitle;
// Alt key hacks. We need this in order for hotkey bindings with alt to work.
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_KEYMENU = 0xF100;
internal void SendAltCombination(char character)
{
var m = new Message { WParam = new IntPtr(SC_KEYMENU), LParam = new IntPtr(character), Msg = WM_SYSCOMMAND, HWnd = Handle };
if (character == ' ') base.WndProc(ref m);
else if (character >= 'a' && character <= 'z') base.ProcessDialogChar(character);
}
internal void FocusToolStipMenu()
{
var m = new Message { WParam = new IntPtr(SC_KEYMENU), LParam = new IntPtr(0), Msg = WM_SYSCOMMAND, HWnd = Handle };
base.WndProc(ref m);
}
protected override void WndProc(ref Message m)
{
if (!BlocksInputWhenFocused)
{
// this is necessary to trap plain alt keypresses so that only our hotkey system gets them
if (m.Msg == WM_SYSCOMMAND)
{
if (m.WParam.ToInt32() == SC_KEYMENU && m.LParam == IntPtr.Zero)
{
return;
}
}
}
base.WndProc(ref m);
}
protected override bool ProcessDialogChar(char charCode)
{
if (BlocksInputWhenFocused) return base.ProcessDialogChar(charCode);
// this is necessary to trap alt+char combinations so that only our hotkey system gets them
return (ModifierKeys & Keys.Alt) != 0 || base.ProcessDialogChar(charCode);
}
}
}