2
KeePass Password Safe - The Open-Source Password Manager
3
Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
using System.Collections.Generic;
23
using System.Windows.Forms;
24
using System.ComponentModel;
25
using System.Reflection;
26
using System.Diagnostics;
28
using KeePassLib.Native;
30
namespace KeePassLib.Utility
32
public static class MonoWorkarounds
34
private static bool? m_bReq = null;
35
public static bool IsRequired()
37
if(!m_bReq.HasValue) m_bReq = NativeLib.IsUnix();
42
// Key events not raised while Alt is down, and nav keys out of order.
43
// https://sourceforge.net/p/keepass/bugs/1245/
45
// NumericUpDown bug: text is drawn below up/down buttons.
46
// https://sourceforge.net/p/keepass/bugs/1254/
48
// Text in input field is incomplete.
49
// https://bugzilla.xamarin.com/show_bug.cgi?id=5795
50
// https://sourceforge.net/p/keepass/discussion/329220/thread/d23dc88b/
52
// WebRequest GetResponse call missing, breaks WebDAV due to no PUT.
53
// https://bugzilla.xamarin.com/show_bug.cgi?id=10163
54
// https://sourceforge.net/p/keepass/bugs/1117/
55
// https://sourceforge.net/p/keepass/discussion/329221/thread/9422258c/
56
// https://github.com/mono/mono/commit/8e67b8c2fc7cb66bff7816ebf7c1039fb8cfc43b
57
// https://bugzilla.xamarin.com/show_bug.cgi?id=1512
58
// https://sourceforge.net/p/keepass/patches/89/
60
// PictureBox not rendered when bitmap height >= control height.
61
// https://bugzilla.xamarin.com/show_bug.cgi?id=12525
62
// https://sourceforge.net/p/keepass/discussion/329220/thread/54f61e9a/
64
// RichTextBox doesn't handle Unicode string correctly.
65
// https://bugzilla.novell.com/show_bug.cgi?id=586901
67
// ListView column headers not drawn.
68
// https://bugzilla.novell.com/show_bug.cgi?id=620618
70
// Calling Control.Hide doesn't remove the application from taskbar.
71
// https://bugzilla.novell.com/show_bug.cgi?id=649266
73
// Minimum sizes must be enforced.
74
// http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=686017
76
// Mono recreates the main window incorrectly.
77
// https://bugs.launchpad.net/ubuntu/+source/keepass2/+bug/801414
79
// Increase tab control height, otherwise Mono throws exceptions.
80
// https://sourceforge.net/projects/keepass/forums/forum/329221/topic/4519750
81
// https://bugs.launchpad.net/ubuntu/+source/keepass2/+bug/891029
83
// ListView group header selection unsupported.
84
// https://sourceforge.net/p/keepass/discussion/329221/thread/31dae0f0/
86
// Problems with minimizing windows, no content rendered.
87
// https://sourceforge.net/p/keepass/discussion/329220/thread/d50a79d6/
88
public static bool IsRequired(uint uBugID)
90
if(!MonoWorkarounds.IsRequired()) return false;
92
ulong v = NativeLib.MonoVersion;
96
return (v >= 0x0002000B00000000UL); // >= 2.11
102
public static void ApplyTo(Form f)
104
if(!MonoWorkarounds.IsRequired()) return;
105
if(f == null) { Debug.Assert(false); return; }
107
#if (!KeePassLibSD && !KeePassRT)
108
f.HandleCreated += MonoWorkarounds.OnFormHandleCreated;
111
ApplyToControlsRec(f.Controls, f, MonoWorkarounds.ApplyToControl);
115
public static void Release(Form f)
117
if(!MonoWorkarounds.IsRequired()) return;
118
if(f == null) { Debug.Assert(false); return; }
120
#if (!KeePassLibSD && !KeePassRT)
121
f.HandleCreated -= MonoWorkarounds.OnFormHandleCreated;
123
ApplyToControlsRec(f.Controls, f, MonoWorkarounds.ReleaseControl);
127
#if (!KeePassLibSD && !KeePassRT)
128
private delegate void MwaControlHandler(Control c, Form fContext);
130
private static void ApplyToControlsRec(Control.ControlCollection cc,
131
Form fContext, MwaControlHandler fn)
133
if(cc == null) { Debug.Assert(false); return; }
135
foreach(Control c in cc)
138
ApplyToControlsRec(c.Controls, fContext, fn);
142
private static void ApplyToControl(Control c, Form fContext)
144
Button btn = (c as Button);
145
if(btn != null) ApplyToButton(btn, fContext);
147
NumericUpDown nc = (c as NumericUpDown);
148
if((nc != null) && MonoWorkarounds.IsRequired(1254))
150
if(nc.TextAlign == HorizontalAlignment.Right)
151
nc.TextAlign = HorizontalAlignment.Left;
155
private sealed class MwaHandlerInfo
157
private readonly Delegate m_fnOrg; // May be null
158
public Delegate FunctionOriginal
160
get { return m_fnOrg; }
163
private readonly Delegate m_fnOvr;
164
public Delegate FunctionOverride
166
get { return m_fnOvr; }
169
private readonly DialogResult m_dr;
170
public DialogResult Result
175
private readonly Form m_fContext;
176
public Form FormContext
178
get { return m_fContext; }
181
public MwaHandlerInfo(Delegate fnOrg, Delegate fnOvr, DialogResult dr,
187
m_fContext = fContext;
191
private static EventHandlerList GetEventHandlers(Component c,
192
out object objClickEvent)
194
FieldInfo fi = typeof(Control).GetField("ClickEvent", // Mono
195
BindingFlags.Static | BindingFlags.NonPublic);
197
fi = typeof(Control).GetField("EventClick", // .NET
198
BindingFlags.Static | BindingFlags.NonPublic);
199
if(fi == null) { Debug.Assert(false); objClickEvent = null; return null; }
201
objClickEvent = fi.GetValue(null);
202
if(objClickEvent == null) { Debug.Assert(false); return null; }
204
PropertyInfo pi = typeof(Component).GetProperty("Events",
205
BindingFlags.Instance | BindingFlags.NonPublic);
206
return (pi.GetValue(c, null) as EventHandlerList);
209
private static Dictionary<object, MwaHandlerInfo> m_dictHandlers =
210
new Dictionary<object, MwaHandlerInfo>();
211
private static void ApplyToButton(Button btn, Form fContext)
213
DialogResult dr = btn.DialogResult;
214
if(dr == DialogResult.None) return; // No workaround required
216
object objClickEvent;
217
EventHandlerList ehl = GetEventHandlers(btn, out objClickEvent);
218
if(ehl == null) { Debug.Assert(false); return; }
219
Delegate fnClick = ehl[objClickEvent]; // May be null
221
EventHandler fnOvr = new EventHandler(MonoWorkarounds.OnButtonClick);
222
m_dictHandlers[btn] = new MwaHandlerInfo(fnClick, fnOvr, dr, fContext);
224
btn.DialogResult = DialogResult.None;
225
if(fnClick != null) ehl.RemoveHandler(objClickEvent, fnClick);
226
ehl[objClickEvent] = fnOvr;
229
private static void ReleaseControl(Control c, Form fContext)
231
Button btn = (c as Button);
232
if(btn != null) ReleaseButton(btn, fContext);
235
private static void ReleaseButton(Button btn, Form fContext)
238
m_dictHandlers.TryGetValue(btn, out hi);
239
if(hi == null) return;
241
object objClickEvent;
242
EventHandlerList ehl = GetEventHandlers(btn, out objClickEvent);
243
if(ehl == null) { Debug.Assert(false); return; }
245
ehl.RemoveHandler(objClickEvent, hi.FunctionOverride);
246
if(hi.FunctionOriginal != null)
247
ehl[objClickEvent] = hi.FunctionOriginal;
249
btn.DialogResult = hi.Result;
250
m_dictHandlers.Remove(btn);
253
private static void OnButtonClick(object sender, EventArgs e)
255
Button btn = (sender as Button);
256
if(btn == null) { Debug.Assert(false); return; }
259
m_dictHandlers.TryGetValue(btn, out hi);
260
if(hi == null) { Debug.Assert(false); return; }
262
Form f = hi.FormContext;
264
// Set current dialog result by setting the form's private
265
// variable; the DialogResult property can't be used,
266
// because it raises close events
267
FieldInfo fiRes = typeof(Form).GetField("dialog_result",
268
BindingFlags.Instance | BindingFlags.NonPublic);
269
if(fiRes == null) { Debug.Assert(false); return; }
270
if(f != null) fiRes.SetValue(f, hi.Result);
272
if(hi.FunctionOriginal != null)
273
hi.FunctionOriginal.Method.Invoke(hi.FunctionOriginal.Target,
274
new object[] { btn, e });
276
// Raise close events, if the click event handler hasn't
277
// reset the dialog result
278
if((f != null) && (f.DialogResult == hi.Result))
279
f.DialogResult = hi.Result; // Raises close events
282
private static void SetWmClass(Form f)
284
NativeMethods.SetWmClass(f, PwDefs.UnixName, PwDefs.ResClass);
287
private static void OnFormHandleCreated(object sender, EventArgs e)
289
Form f = (sender as Form);
290
if(f == null) { Debug.Assert(false); return; }
292
if(!f.IsHandleCreated) return; // Prevent infinite loop
298
/// Set the value of the private <c>shown_raised</c> member
299
/// variable of a form.
301
/// <returns>Previous <c>shown_raised</c> value.</returns>
302
internal static bool ExchangeFormShownRaised(Form f, bool bNewValue)
304
if(f == null) { Debug.Assert(false); return true; }
308
FieldInfo fi = typeof(Form).GetField("shown_raised",
309
BindingFlags.Instance | BindingFlags.NonPublic);
310
if(fi == null) { Debug.Assert(false); return true; }
312
bool bPrevious = (bool)fi.GetValue(f);
314
fi.SetValue(f, bNewValue);
318
catch(Exception) { Debug.Assert(false); }