mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
InputText.dll: Updated to new API
This commit is contained in:
@ -1,63 +1,68 @@
|
||||
using System;
|
||||
/*
|
||||
Copyright (C) 2013 Rainmeter Team
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Rainmeter;
|
||||
|
||||
// The bulk of your plugin's code belongs in this file.
|
||||
namespace InputText
|
||||
{
|
||||
class PluginCode
|
||||
internal partial class Measure
|
||||
{
|
||||
// 'Update', 'Update2', and 'GetString' all return data back to Rainmeter, depending on
|
||||
// if the Rainmeter measure wants a numeric value or a string/text data.
|
||||
//
|
||||
// The 'Instance' member contains all of the data necessary to read the INI file
|
||||
// passed to your plugin when this instance was first initialized. Remember: your plugin
|
||||
// may be initialized multiple times. For example, a plugin that reads the free space
|
||||
// of a hard drive may be called multiple times -- once for each installed hard drive.
|
||||
|
||||
public UInt32 Update(Rainmeter.Settings.InstanceSettings Instance)
|
||||
private void ExecuteCommands(string Command)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
Command = Command.Trim();
|
||||
|
||||
//public double Update2(Rainmeter.Settings.InstanceSettings Instance)
|
||||
//{
|
||||
// return 0.0;
|
||||
//}
|
||||
// Get default options
|
||||
Dictionary<string, string> Options = new Dictionary<string, string>();
|
||||
ReadOption("DefaultValue", Options);
|
||||
ReadOption("X", Options);
|
||||
ReadOption("Y", Options);
|
||||
ReadOption("W", Options);
|
||||
ReadOption("H", Options);
|
||||
ReadOption("StringStyle", Options);
|
||||
ReadOption("StringAlign", Options);
|
||||
ReadOption("FocusDismiss", Options);
|
||||
ReadOption("FontColor", Options);
|
||||
ReadOption("FontFace", Options);
|
||||
ReadOption("FontSize", Options);
|
||||
ReadOption("SolidColor", Options);
|
||||
ReadOption("Password", Options);
|
||||
ReadOption("TopMost", Options);
|
||||
|
||||
public string GetString(Rainmeter.Settings.InstanceSettings Instance)
|
||||
{
|
||||
// This plugin is unique as it is one of the first to not be used to display data
|
||||
// back in Rainmeter, but to request user input and use that input during batch
|
||||
// operations (and other purposes).
|
||||
//
|
||||
// However, Rainmeter requires that data be sent back, so we'll return temporary data
|
||||
// In this case, the data is whatever the user last entered into an input textbox.
|
||||
return Instance.GetTempValue("LastInput", string.Empty).ToString();
|
||||
}
|
||||
|
||||
|
||||
// 'ExecuteBang' is a way of Rainmeter telling your plugin to do something *right now*.
|
||||
// What it wants to do can be defined by the 'Command' parameter.
|
||||
public void ExecuteBang(Rainmeter.Settings.InstanceSettings Instance, string Command)
|
||||
{
|
||||
#region Handle a single parameter
|
||||
|
||||
// If our parameter list only contains a single word, then open a textbox immediately
|
||||
// and set a value. This mode does not do any batching.
|
||||
if (!Command.Trim().Contains(" "))
|
||||
if (!Command.Contains(" "))
|
||||
{
|
||||
// Assume that the parameter is the name of the variable
|
||||
string sVariableName = Command.Trim();
|
||||
|
||||
// Ask for input
|
||||
string sInput = GetUserInput(Instance);
|
||||
string sInput = GetUserInput(Options);
|
||||
|
||||
// If the user cancelled out of the inputbox (ESC key, etc.), then abort
|
||||
if (sInput == null)
|
||||
return;
|
||||
|
||||
// Ask Rainmeter to set the variable using a bang (http://rainmeter.net/RainCMS/?q=Bangs)
|
||||
Rainmeter.Bang("!RainmeterSetVariable " + sVariableName + " \"" + sInput.Replace("\"", "\\\"") + "\"");
|
||||
API.Execute(rm.GetSkin(), "!SetVariable " + Command + " \"" + sInput + "\"");
|
||||
|
||||
// Note that the skin needs DynamicVariables=1 in the measure's settings or the above
|
||||
// code will have no effect.
|
||||
@ -68,7 +73,7 @@ namespace InputText
|
||||
#region Handle multiple parameters
|
||||
|
||||
// Our parameter list contains at least two words, so split them up
|
||||
string[] sParts = Command.Trim().Split(new string[] { " " }, StringSplitOptions.None);
|
||||
string[] sParts = Command.Split(new string[] { " " }, StringSplitOptions.None);
|
||||
|
||||
// If the first parameter is 'ExecuteBatch' (not case sensitive)...
|
||||
if (sParts[0].Trim().ToUpper() == "EXECUTEBATCH")
|
||||
@ -126,18 +131,25 @@ namespace InputText
|
||||
#region Parse commands in range
|
||||
// Parse each command in the range, aborting if any line returns 'false' or
|
||||
// the requested command line does not exist in the config for that measure.
|
||||
List<string> commands = new List<string>();
|
||||
|
||||
for (int i = iMin; i <= iMax; i++)
|
||||
{
|
||||
// Read this command's line
|
||||
string sCurrentLine = Instance.INI_Value("Command" + i.ToString());
|
||||
string sCurrentLine = rm.ReadString("Command" + i.ToString(), "", false);
|
||||
|
||||
// If empty/non-existent, abort
|
||||
if (string.IsNullOrEmpty(sCurrentLine))
|
||||
break;
|
||||
|
||||
commands.Add(sCurrentLine);
|
||||
}
|
||||
|
||||
foreach (string sCurrentLine in commands)
|
||||
{
|
||||
// Execute the line, but if there's a problem (error or they cancel the
|
||||
// input textbox), then abort
|
||||
if (!ExecuteLine(Instance, sCurrentLine))
|
||||
if (!ExecuteLine(sCurrentLine, Options))
|
||||
break;
|
||||
|
||||
// Continue to the next line, if there is any
|
||||
@ -147,7 +159,7 @@ namespace InputText
|
||||
}
|
||||
|
||||
// Unhandled command, log the message but otherwise do nothing
|
||||
Rainmeter.Log(Rainmeter.LogLevel.Debug, "InputText: Received command \"" + sParts[0].Trim() + "\", left unhandled");
|
||||
API.Log(API.LogType.Debug, "InputText: Received command \"" + sParts[0].Trim() + "\", left unhandled");
|
||||
|
||||
#endregion
|
||||
|
||||
@ -157,7 +169,7 @@ namespace InputText
|
||||
#region This is all code custom to this plugin
|
||||
|
||||
#region Parse the current command line
|
||||
private bool ExecuteLine(Rainmeter.Settings.InstanceSettings Instance, string sLine)
|
||||
private bool ExecuteLine(string sLine, Dictionary<string, string> Options)
|
||||
{
|
||||
// If this line contains a $UserInput$ token, then we need to do some extra
|
||||
// parsing
|
||||
@ -186,17 +198,17 @@ namespace InputText
|
||||
sLine = ScanAndReplace(sLine, "FocusDismiss", Overrides);
|
||||
sLine = ScanAndReplace(sLine, "FontColor", Overrides);
|
||||
sLine = ScanAndReplace(sLine, "FontFace", Overrides);
|
||||
sLine = ScanAndReplace(sLine, "FontSize", Overrides);
|
||||
sLine = ScanAndReplace(sLine, "SolidColor", Overrides);
|
||||
sLine = ScanAndReplace(sLine, "Password", Overrides);
|
||||
sLine = ScanAndReplace(sLine, "FontSize", Overrides);
|
||||
sLine = ScanAndReplace(sLine, "TopMost", Overrides);
|
||||
#endregion
|
||||
|
||||
// Get user input
|
||||
string sInput = GetUserInput(Instance, Overrides);
|
||||
string sInput = GetUserInput(Options, Overrides);
|
||||
if (sInput == null)
|
||||
{
|
||||
// Rainmeter.Log(Rainmeter.LogLevel.Debug, "InputText: Aborted, user cancelled text box");
|
||||
// API.Log(API.LogType.Debug, "InputText: Aborted, user cancelled text box");
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -206,168 +218,116 @@ namespace InputText
|
||||
catch (Exception ex)
|
||||
{
|
||||
// If there was an error doing any of the above, log it for debugging purposes.
|
||||
Rainmeter.Log(Rainmeter.LogLevel.Warning, "InputText: Exception " + ex.GetType().ToString() + ": " + ex.Message);
|
||||
API.Log(API.LogType.Warning, "InputText: Exception " + ex.GetType().ToString() + ": " + ex.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Execute the bang. This will either be the original line from CommandX= (sLine variable) or
|
||||
// an adjusted line based on special parsing above.
|
||||
// Rainmeter.Log(Rainmeter.LogLevel.Debug, "InputText: Executing bang: " + sLine);
|
||||
Rainmeter.Bang(sLine);
|
||||
// API.Log(API.LogType.Debug, "InputText: Executing bang: " + sLine);
|
||||
API.Execute(rm.GetSkin(), sLine);
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
#region GetUserInput() -- creates an input textbox and handles all INI and override settings
|
||||
|
||||
private string GetUserInput(Rainmeter.Settings.InstanceSettings Instance)
|
||||
delegate void ChangeSettingFromString(string value);
|
||||
delegate void ChangeInputBoxSetting(string option, ChangeSettingFromString method);
|
||||
|
||||
private string GetUserInput(Dictionary<string, string> Options)
|
||||
{
|
||||
// No INI overrides provided, so create an empty list
|
||||
return GetUserInput(Instance, new Dictionary<string,string>());
|
||||
return GetUserInput(Options, new Dictionary<string, string>());
|
||||
}
|
||||
private string GetUserInput(Rainmeter.Settings.InstanceSettings Instance, Dictionary<string,string> sOverrides)
|
||||
private string GetUserInput(Dictionary<string, string> Options, Dictionary<string, string> Overrides)
|
||||
{
|
||||
SkinWindow skin = new SkinWindow(rm);
|
||||
|
||||
// Create the form. 'InputBox' is a .NET form with a textbox and two button controls on it.
|
||||
InputBox input = new InputBox();
|
||||
input.Instance = Instance;
|
||||
input.ChangeX(Instance, "0");
|
||||
input.ChangeY(Instance, "0");
|
||||
InputBox input = new InputBox(skin);
|
||||
input.ChangeX("0");
|
||||
input.ChangeY("0");
|
||||
|
||||
// Change the styles of the InputBox form based on overrides or INI values
|
||||
#region Style and preference tweaks (INI and override settings)
|
||||
|
||||
if (sOverrides.ContainsKey("FontFace"))
|
||||
input.ChangeFontFace(sOverrides["FontFace"]);
|
||||
else if (!string.IsNullOrEmpty(Instance.INI_Value("FontFace")))
|
||||
input.ChangeFontFace(Instance.INI_Value("FontFace"));
|
||||
|
||||
if (sOverrides.ContainsKey("FontSize"))
|
||||
input.ChangeFontSize(sOverrides["FontSize"]);
|
||||
else if (!string.IsNullOrEmpty(Instance.INI_Value("FontSize")))
|
||||
input.ChangeFontSize(Instance.INI_Value("FontSize"));
|
||||
|
||||
if (sOverrides.ContainsKey("W"))
|
||||
input.ChangeW(sOverrides["W"]);
|
||||
else if (!string.IsNullOrEmpty(Instance.INI_Value("W")))
|
||||
input.ChangeW(Instance.INI_Value("W"));
|
||||
|
||||
if (sOverrides.ContainsKey("H"))
|
||||
input.ChangeH(sOverrides["H"]);
|
||||
else if (!string.IsNullOrEmpty(Instance.INI_Value("H")))
|
||||
input.ChangeH(Instance.INI_Value("H"));
|
||||
|
||||
if (sOverrides.ContainsKey("X"))
|
||||
input.ChangeX(Instance, sOverrides["X"]);
|
||||
else if (!string.IsNullOrEmpty(Instance.INI_Value("X")))
|
||||
input.ChangeX(Instance, Instance.INI_Value("X"));
|
||||
|
||||
if (sOverrides.ContainsKey("Y"))
|
||||
input.ChangeY(Instance, sOverrides["Y"]);
|
||||
else if (!string.IsNullOrEmpty(Instance.INI_Value("Y")))
|
||||
input.ChangeY(Instance, Instance.INI_Value("Y"));
|
||||
|
||||
if (sOverrides.ContainsKey("StringStyle"))
|
||||
input.ChangeFontStringStyle(sOverrides["StringStyle"]);
|
||||
else if (!string.IsNullOrEmpty(Instance.INI_Value("StringStyle")))
|
||||
input.ChangeFontStringStyle(Instance.INI_Value("StringStyle"));
|
||||
|
||||
if (sOverrides.ContainsKey("StringAlign"))
|
||||
input.ChangeStringAlign(sOverrides["StringAlign"]);
|
||||
else if (!string.IsNullOrEmpty(Instance.INI_Value("StringAlign")))
|
||||
input.ChangeStringAlign(Instance.INI_Value("StringAlign"));
|
||||
|
||||
bool bFocusDismiss = true;
|
||||
if (sOverrides.ContainsKey("FocusDismiss"))
|
||||
ChangeInputBoxSetting changeSetting = (opt, change) =>
|
||||
{
|
||||
input.MakeFocusDismiss(sOverrides["FocusDismiss"] == "1");
|
||||
bFocusDismiss = sOverrides["FocusDismiss"] == "1";
|
||||
}
|
||||
else
|
||||
if (Overrides.ContainsKey(opt))
|
||||
change(Overrides[opt]);
|
||||
else if (Options.ContainsKey(opt))
|
||||
change(Options[opt]);
|
||||
};
|
||||
|
||||
changeSetting("FontFace", input.ChangeFontFace);
|
||||
changeSetting("FontSize", input.ChangeFontSize);
|
||||
|
||||
changeSetting("W", input.ChangeW);
|
||||
changeSetting("H", input.ChangeH);
|
||||
changeSetting("X", input.ChangeX);
|
||||
changeSetting("Y", input.ChangeY);
|
||||
|
||||
changeSetting("StringStyle", input.ChangeFontStringStyle);
|
||||
changeSetting("StringAlign", input.ChangeStringAlign);
|
||||
|
||||
changeSetting("FontColor", input.ChangeFontColor);
|
||||
changeSetting("SolidColor", input.ChangeBackColor);
|
||||
|
||||
if (Overrides.ContainsKey("FocusDismiss"))
|
||||
input.MakeFocusDismiss(Overrides["FocusDismiss"] == "1");
|
||||
else if (Options.ContainsKey("FocusDismiss"))
|
||||
input.MakeFocusDismiss(Options["FocusDismiss"].Trim() == "1");
|
||||
|
||||
if (Overrides.ContainsKey("Password"))
|
||||
input.MakePassword(Overrides["Password"] == "1");
|
||||
else if (Options.ContainsKey("Password"))
|
||||
input.MakePassword(Options["Password"].Trim() == "1");
|
||||
|
||||
string topmost = null;
|
||||
if (Overrides.ContainsKey("TopMost"))
|
||||
topmost = Overrides["TopMost"];
|
||||
else if (Options.ContainsKey("TopMost"))
|
||||
topmost = Options["TopMost"].Trim();
|
||||
switch (topmost)
|
||||
{
|
||||
input.MakeFocusDismiss(!(Instance.INI_Value("FocusDismiss").Trim().ToUpper() != "1"));
|
||||
bFocusDismiss = !(Instance.INI_Value("FocusDismiss").Trim().ToUpper() != "1");
|
||||
}
|
||||
|
||||
if (sOverrides.ContainsKey("FontColor"))
|
||||
input.ChangeFontColor(sOverrides["FontColor"]);
|
||||
else if (!string.IsNullOrEmpty(Instance.INI_Value("FontColor")))
|
||||
input.ChangeFontColor(Instance.INI_Value("FontColor"));
|
||||
|
||||
if (sOverrides.ContainsKey("SolidColor"))
|
||||
input.ChangeBackColor(sOverrides["SolidColor"]);
|
||||
else if (!string.IsNullOrEmpty(Instance.INI_Value("SolidColor")))
|
||||
input.ChangeBackColor(Instance.INI_Value("SolidColor"));
|
||||
|
||||
if (sOverrides.ContainsKey("Passwords"))
|
||||
{
|
||||
if (sOverrides["DefaultValue"] == "1")
|
||||
input.MakePassword();
|
||||
}
|
||||
else if (Instance.INI_Value("Password").Trim().ToUpper() == "1")
|
||||
input.MakePassword();
|
||||
|
||||
bool bAutoTopMost = true;
|
||||
if (sOverrides.ContainsKey("TopMost"))
|
||||
{
|
||||
if (sOverrides["TopMost"] == "1")
|
||||
{
|
||||
case "1":
|
||||
input.MakeTopmost();
|
||||
bAutoTopMost = false;
|
||||
}
|
||||
else if (sOverrides["TopMost"] == "0")
|
||||
bAutoTopMost = false;
|
||||
break;
|
||||
case "0":
|
||||
break;
|
||||
default: // AUTO
|
||||
if (skin.IsTopmost)
|
||||
input.MakeTopmost();
|
||||
break;
|
||||
}
|
||||
else if (Instance.INI_Value("TopMost").Trim().ToUpper() == "1")
|
||||
{
|
||||
input.MakeTopmost();
|
||||
bAutoTopMost = false;
|
||||
}
|
||||
else if (Instance.INI_Value("TopMost").Trim().ToUpper() != "AUTO")
|
||||
if (!string.IsNullOrEmpty(Instance.INI_Value("TopMost").Trim()))
|
||||
bAutoTopMost = false;
|
||||
if (bAutoTopMost)
|
||||
if (Rainmeter.ParentIsTopmost(Instance))
|
||||
input.MakeTopmost();
|
||||
|
||||
if (sOverrides.ContainsKey("DefaultValue"))
|
||||
input.DefaultValue(sOverrides["DefaultValue"]);
|
||||
else if (!string.IsNullOrEmpty(Instance.INI_Value("DefaultValue")))
|
||||
input.DefaultValue(Instance.INI_Value("DefaultValue").Trim());
|
||||
changeSetting("DefaultValue", input.DefaultValue);
|
||||
|
||||
#endregion
|
||||
|
||||
if (bFocusDismiss)
|
||||
{
|
||||
input.Show(new WindowWrapper(Rainmeter.GetConfigWindow(Instance)));
|
||||
if (!input.ShowInputBox())
|
||||
return null;
|
||||
|
||||
for (; ; )
|
||||
{
|
||||
if (input.DialogResult != System.Windows.Forms.DialogResult.None || input.drBackup != System.Windows.Forms.DialogResult.None)
|
||||
break;
|
||||
System.Windows.Forms.Application.DoEvents();
|
||||
System.Threading.Thread.Sleep(44);
|
||||
}
|
||||
}
|
||||
else
|
||||
lock (this.locker)
|
||||
{
|
||||
input.ShowDialog(new WindowWrapper(Rainmeter.GetConfigWindow(Instance)));
|
||||
this.LastInput = input.TextValue;
|
||||
}
|
||||
|
||||
if (input.drBackup != System.Windows.Forms.DialogResult.None)
|
||||
{
|
||||
if (input.drBackup != System.Windows.Forms.DialogResult.OK)
|
||||
return null;
|
||||
}
|
||||
else if (input.DialogResult != System.Windows.Forms.DialogResult.None)
|
||||
{
|
||||
if (input.DialogResult != System.Windows.Forms.DialogResult.OK)
|
||||
return null;
|
||||
}
|
||||
|
||||
Instance.SetTempValue("LastInput", input.sTextValue);
|
||||
return input.sTextValue;
|
||||
return input.TextValue;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ReadOption() -- reads given option's value from Rainmeter
|
||||
private void ReadOption(string optionName, Dictionary<string, string> Options)
|
||||
{
|
||||
string value = rm.ReadString(optionName, "");
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
{
|
||||
Options.Add(optionName, value);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Replace() -- case-insensitive string replacement
|
||||
private static string Replace(string sIn, string sFind, string sReplace)
|
||||
{
|
||||
@ -458,7 +418,7 @@ namespace InputText
|
||||
if (FindTag(sLine, sTagName))
|
||||
{
|
||||
string sTagData = TagData(sLine, sTagName);
|
||||
// Rainmeter.Log(Rainmeter.LogLevel.Debug, "InputText: Overriding " + sTagName + " with " + sTagData);
|
||||
// API.Log(API.LogType.Debug, "InputText: Overriding " + sTagName + " with " + sTagData);
|
||||
if (sTagData.StartsWith("\""))
|
||||
Overrides.Add(sTagName, sTagData.Substring(1, sTagData.Length - 2));
|
||||
else
|
||||
|
Reference in New Issue
Block a user