using System.Drawing;
using System.Windows.Forms;
namespace FastReport.Utils
{
///
/// Storage service for form's controls.
///
public class ControlStorageService : StorageService
{
private Control control;
///
/// Use device independent pixels (measured in 96dpi mode).
///
public bool UseDips { get; set; }
private int ToDip(int value) => UseDips ? (int)(value / control.DpiMultiplier()) : value;
private int ToPix(int value) => UseDips ? (int)(value * control.DpiMultiplier()) : value;
///
/// Gets value in device-independent pixels (dips) and converts it to pixels. Takes property into account.
///
/// The key.
/// Default value.
/// Minimum value.
/// Maximum value.
/// Value in dips.
public int GetDip(string key, int defaultValue = 0, int minValue = int.MinValue, int maxValue = int.MaxValue)
{
int result = ToPix(GetInt(key, defaultValue));
if (result < minValue)
result = minValue;
if (result > maxValue)
result = maxValue;
return result;
}
///
/// Stores value in device-independent pixels (dips). Takes property into account.
///
/// The key.
/// Value.
public void SetDip(string key, int value) => SetInt(key, ToDip(value));
///
/// Gets font from font storage.
///
/// Path to font storage.
/// Default value.
/// The font object.
public Font GetFont(string path, Font defaultFont)
{
var fs = new StorageService("Designer,Fonts," + path);
string fontName = fs.GetStr("font-name", defaultFont.Name);
float fontSize = fs.GetFloat("font-size", defaultFont.Size);
bool bold = fs.GetBool("font-bold");
bool italic = fs.GetBool("font-italic");
return new Font(fontName, fontSize, (italic ? FontStyle.Italic : 0) | (bold ? FontStyle.Bold : 0));
}
///
/// Initializes a new instance of storage class.
///
/// The control which dpi setting is used to save/restore dips.
/// The root path.
public ControlStorageService(Control control, string path) : base(path)
{
this.control = control;
#if (WPF || AVALONIA)
UseDips = true;
#endif
}
}
}