1 /*
2   KeePass Password Safe - The Open-Source Password Manager
3   Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de>
4 
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.
9 
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.
14 
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
18 */
19 
20 using System;
21 using System.Collections.Generic;
22 using System.Diagnostics;
23 using System.Drawing;
24 using System.Text;
25 using System.Windows.Forms;
26 
27 using KeePass.Native;
28 using KeePass.Util;
29 
30 namespace KeePass.UI
31 {
32 	public sealed class CustomToolStripEx : ToolStrip
33 	{
34 		private CriticalSectionEx m_csSizeAuto = new CriticalSectionEx();
35 		private int m_iLockedHeight = 0;
36 
CustomToolStripEx()37 		public CustomToolStripEx() : base()
38 		{
39 			// ThemeToolStripRenderer.AttachTo(this);
40 
41 			UIUtil.Configure(this);
42 		}
43 
44 #if DEBUG
~CustomToolStripEx()45 		~CustomToolStripEx()
46 		{
47 			if(m_csSizeAuto.TryEnter()) m_csSizeAuto.Exit();
48 			else { Debug.Assert(false); } // Should have been unlocked
49 		}
50 #endif
51 
WndProc(ref Message m)52 		protected override void WndProc(ref Message m)
53 		{
54 			base.WndProc(ref m);
55 
56 			// Enable 'click through' behavior
57 			if((m.Msg == NativeMethods.WM_MOUSEACTIVATE) &&
58 				(m.Result == (IntPtr)NativeMethods.MA_ACTIVATEANDEAT))
59 			{
60 				m.Result = (IntPtr)NativeMethods.MA_ACTIVATE;
61 			}
62 		}
63 
LockHeight(bool bLock)64 		public void LockHeight(bool bLock)
65 		{
66 			Debug.Assert(this.Height > 0);
67 			m_iLockedHeight = (bLock ? this.Height : 0);
68 		}
69 
OnSizeChanged(EventArgs e)70 		protected override void OnSizeChanged(EventArgs e)
71 		{
72 			if(m_csSizeAuto.TryEnter())
73 			{
74 				try
75 				{
76 					Size sz = this.Size;
77 					// Ignore zero-size events (which can occur e.g. when
78 					// the ToolStrip is being hidden)
79 					if((sz.Width > 0) && (sz.Height > 0))
80 					{
81 						if((m_iLockedHeight > 0) && (sz.Height != m_iLockedHeight))
82 						{
83 							base.OnSizeChanged(e);
84 							this.Height = m_iLockedHeight;
85 							Debug.Assert(this.Size.Height == m_iLockedHeight);
86 							return;
87 						}
88 					}
89 				}
90 				catch(Exception) { Debug.Assert(false); }
91 				finally { m_csSizeAuto.Exit(); }
92 			}
93 
94 			base.OnSizeChanged(e);
95 		}
96 	}
97 }
98