mirror of
https://github.com/xcp-ng/xenadmin.git
synced 2024-12-22 08:26:03 +01:00
8c15bcc6d9
Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
126 lines
4.5 KiB
C#
126 lines
4.5 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.Drawing;
|
|
using System.Windows.Forms;
|
|
|
|
namespace XenAdmin.Controls
|
|
{
|
|
/// <summary>
|
|
/// A combobox that automatically resizes the dropdown's width to be as big
|
|
/// as the longest string in the items list
|
|
/// </summary>
|
|
public class LongStringComboBox : ComboBox
|
|
{
|
|
private readonly ToolTip toolTip = new ToolTip();
|
|
|
|
private bool disposed;
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
if (!disposed)
|
|
{
|
|
toolTip.Dispose();
|
|
}
|
|
disposed = true;
|
|
}
|
|
base.Dispose(disposing);
|
|
}
|
|
|
|
protected override void OnDropDown(EventArgs e)
|
|
{
|
|
int totalWidth = MeasureLongestItem + VerticalScrollBarWidth;
|
|
DropDownWidth = totalWidth > Width ? totalWidth : Width;
|
|
base.OnDropDown(e);
|
|
}
|
|
|
|
protected override void OnCreateControl()
|
|
{
|
|
// Some strings can be very long, so we will show the value as a mouse over tooltip.
|
|
// Initialize the tooltip here.
|
|
toolTip.Active = true;
|
|
toolTip.AutomaticDelay = 0;
|
|
toolTip.AutoPopDelay = 50000;
|
|
toolTip.InitialDelay = 50;
|
|
toolTip.ReshowDelay = 50;
|
|
toolTip.ShowAlways = true;
|
|
|
|
base.OnCreateControl();
|
|
}
|
|
|
|
private int MeasureLongestItem
|
|
{
|
|
get
|
|
{
|
|
using (Graphics graphics = CreateGraphics())
|
|
{
|
|
int longest = 0;
|
|
foreach (var item in Items)
|
|
{
|
|
int stringLength = (int) graphics.MeasureString(item.ToString(), Font).Width;
|
|
if (stringLength > longest)
|
|
longest = stringLength;
|
|
}
|
|
return longest;
|
|
}
|
|
}
|
|
}
|
|
|
|
private int MeasureTextWidth(string text)
|
|
{
|
|
using (Graphics graphics = CreateGraphics())
|
|
{
|
|
return (int) graphics.MeasureString(text, Font).Width;
|
|
}
|
|
}
|
|
|
|
private int VerticalScrollBarWidth
|
|
{
|
|
get { return Items.Count > MaxDropDownItems ? SystemInformation.VerticalScrollBarWidth : 0; }
|
|
}
|
|
|
|
private const int DropDownButtonWidth = 10;
|
|
|
|
protected override void OnSelectedIndexChanged(EventArgs e)
|
|
{
|
|
string itemText = SelectedItem != null ? SelectedItem.ToString() : string.Empty;
|
|
int selectedItemTextWidth = !string.IsNullOrEmpty(itemText)
|
|
? MeasureTextWidth(itemText) + DropDownButtonWidth
|
|
: 0;
|
|
toolTip.SetToolTip(this, selectedItemTextWidth > ClientRectangle.Width ? itemText : string.Empty);
|
|
|
|
base.OnSelectedIndexChanged(e);
|
|
}
|
|
}
|
|
}
|