xenadmin/XenAdmin/Controls/DecentGroupBox.cs
Konstantina Chremmou 3283ca4737 CP-6317: Importing from branch clearwater-ln.
# HG changeset patch
# User Konstantina Chremmou <Konstantina.Chremmou@citrix.com>
# Date 1377010165 -3600
# Node ID 46350c8ef9df37a0c41d7a7245438948d261504b
# Parent  a1c6bcf2b4d3119cff54d87fb97bd2491c9d8f67
CA-112226: Ellipsization should use TextRenderer.MeasureText (GDI) rather than
Graphics.MeasureString (GDI+) since the controls use the default UseCompatibleTextRendering
value (false).

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
2013-09-26 14:35:57 +01:00

150 lines
4.6 KiB
C#

/* Copyright (c) Citrix Systems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using XenAdmin.Core;
namespace XenAdmin.Controls
{
/// <summary>
/// A System.Windows.Forms.GroupBox with the additional property 'UseMnemonic'.
/// </summary>
public class DecentGroupBox : GroupBox
{
private bool useMnemonic;
/// <summary>
/// If set to true, any ampersands in the Text property are interpreted as keyboard shortcuts. Otherwise the ampersands are rendered correctly.
/// </summary>
[DefaultValue(false)]
public bool UseMnemonic
{
get
{
return useMnemonic;
}
set
{
useMnemonic = value;
RefreshText();
}
}
private string rawText;
public new string Text
{
get
{
return rawText;
}
set
{
rawText = value;
RefreshText();
}
}
private bool autoEllipsis = true;
[DefaultValue(true)]
public bool AutoEllipsis
{
get
{
return autoEllipsis;
}
set
{
autoEllipsis = value;
RefreshText();
}
}
/// <summary>
/// The text that has been passed to the base System.Windows.Forms.GroupBox class, escaped as necessary.
/// </summary>
protected String EscapedText
{
get
{
return base.Text;
}
}
private const int FUDGE = 15;
private void RefreshText()
{
string newText = rawText;
if (!useMnemonic)
newText = rawText.EscapeAmpersands();
int maxWidth = Width - FUDGE;
if (maxWidth <= 0)
{
// Cope with excessively narrow groupboxes
base.Text = String.Empty;
return;
}
if (autoEllipsis && newText != null)
{
using (Graphics g = CreateGraphics())
{
newText = newText.Ellipsise(new Rectangle(0, 0, maxWidth, Height), Font);
if (newText == ".") //If ellipsise shortens the string to less than the ellipsis
{
base.Text = String.Empty;
return;
}
}
//Avoid sending &... to the base class, so ensure we have an even number of ampersands on the end of the string.
if (!useMnemonic && newText.Count(c => c == '&') % 2 > 0)
newText = newText.Remove(newText.LastIndexOf('&'), 1);
}
base.Text = newText;
}
protected override void OnSizeChanged(EventArgs e)
{
RefreshText();
base.OnSizeChanged(e);
}
}
}