Skip to content

Commit

Permalink
Improve KeeTheme integration & context menu
Browse files Browse the repository at this point in the history
Resize KeeTheme container object to accomodate for non-default font sizes
Use KeePass' context menu instead of own context menu
  • Loading branch information
Rookiestyle committed Sep 11, 2021
1 parent 9fa3f09 commit d31106f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/ColoredPassword.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<LangVersion>5</LangVersion>
</PropertyGroup>
<PropertyGroup>
<PlgxKeePassVersion>2.39</PlgxKeePassVersion>
<PlgxKeePassVersion>2.40</PlgxKeePassVersion>
<PlgXOS>
</PlgXOS>
</PropertyGroup>
Expand Down
93 changes: 25 additions & 68 deletions src/ColoredSecureTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ColoredSecureTextBox()
LocationChanged += ColoredSecureTextBox_LocationChanged;
SizeChanged += ColoredSecureTextBox_SizeChanged;
//Focus change is no longer triggered since KeePass 2.49
Enter += OnFocusChangeRequired;
Enter += OnFocusChangeRequired;
}

private void M_text_ParentChanged(object sender, EventArgs e)
Expand All @@ -67,6 +67,7 @@ private void M_text_ParentChanged(object sender, EventArgs e)
private void ColoredSecureTextBox_SizeChanged(object sender, EventArgs e)
{
m_text.Size = Size;
if (m_bKeeTheme) m_text.Parent.Size = Size;
}

private void ColoredSecureTextBox_LocationChanged(object sender, EventArgs e)
Expand Down Expand Up @@ -150,11 +151,12 @@ protected override void OnParentChanged(EventArgs e)
Parent.PerformLayout();
}

public override void EnableProtection(bool bEnable)
public override void EnableProtection(bool bEnable)
{
PluginDebug.AddInfo(Name + " Protect password display: " + bEnable.ToString());
m_text.TextChanged -= ColorTextChanged;
base.EnableProtection(bEnable);
m_text.Name = Name + "_RTB";
if (bEnable)
{
Visible = true;
Expand All @@ -179,9 +181,11 @@ public override void EnableProtection(bool bEnable)
m_text.Size = Size;
AdjustLocation();
m_text.Font = Font;
m_text.Visible = true;
m_text.TabStop = true;
if (m_bKeeTheme) m_text.Parent.Visible = true;
//If password repeat is off and KeeTheme is active => Hide RichTextBox, will not be shown properly otherwise
bool bVisible = Enabled || !(Name.Contains("Repeat") && m_bKeeTheme);
if (m_bKeeTheme) m_text.Parent.Visible = bVisible;
m_text.Visible = bVisible;
m_text.TabStop = bVisible;
m_text.Text = Text;
m_text.ReadOnly = ReadOnly;
Visible = false;
Expand All @@ -206,8 +210,8 @@ public override void EnableProtection(bool bEnable)
}
}

private void AdjustLocation()
{
private void AdjustLocation()
{
//If KeeTheme is active, the ColoredTextBox is contained in
//a RichTextBoxDecorator

Expand Down Expand Up @@ -236,58 +240,37 @@ private Form GetForm(Control c)
}
}

public class ColorTextBox : RichTextBox
public class ColorTextBox : CustomRichTextBoxEx
{
private bool m_bColorBackground = true;
private bool? m_bKeeTheme = null;

private RichTextBoxContextMenu m_ctx = new RichTextBoxContextMenu();

public bool ColorBackground
{
get { return GetColorBackground(); }
{
get { return GetColorBackground(); }
set { m_bColorBackground = value; }
}
}

private bool GetColorBackground()
{
if (!m_bKeeTheme.HasValue && Parent != null) m_bKeeTheme= Parent.GetType().FullName.Contains("KeeTheme");
private bool GetColorBackground()
{
if (!m_bKeeTheme.HasValue && Parent != null) m_bKeeTheme = Parent.GetType().FullName.Contains("KeeTheme");
if (!m_bKeeTheme.HasValue) return m_bColorBackground;
return !m_bKeeTheme.Value && m_bColorBackground;
}

protected override void Dispose(bool disposing)
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing && (ContextMenuStrip != null) && !ContextMenuStrip.IsDisposed)
{
ContextMenuStrip.Opening -= ContextMenuStrip_Opening;
ContextMenuStrip.Dispose();
ContextMenuStrip = null;
}
m_ctx.Detach();
}

public ColorTextBox() : base()
{
Multiline = false;

//Keep ContextMenuStrip_Opening in sync with this
ContextMenuStrip = new ContextMenuStrip();
ToolStripMenuItem m = new ToolStripMenuItem() { Text = KeePass.Resources.KPRes.Cut, Name = "CM_Cut" };
m.Click += (o, e) => Cut();
ContextMenuStrip.Items.Add(m);
m = new ToolStripMenuItem() { Text = KeePass.Resources.KPRes.Copy, Name = "CM_Copy" };
m.Click += (o, e) => Copy();
ContextMenuStrip.Items.Add(m);
m = new ToolStripMenuItem() { Text = KeePass.Resources.KPRes.Paste, Name = "CM_Paste" };
m.Click += (o, e) => Paste(Clipboard.GetText());
ContextMenuStrip.Items.Add(m);
m = new ToolStripMenuItem() { Text = KeePass.Resources.KPRes.Delete, Name = "CM_Delete" };
m.Click += (o, e) =>
{ SelectedText = string.Empty; };
ContextMenuStrip.Items.Add(m);
ContextMenuStrip.Items.Add(new ToolStripSeparator());
m = new ToolStripMenuItem() { Text = KeePass.Resources.KPRes.SelectAll, Name = "CM_SelectAll" };
m.Click += (o, e) => { Select(0, Text.Length); };
ContextMenuStrip.Items.Add(m);
ContextMenuStrip.Opening += ContextMenuStrip_Opening;
SimpleTextOnly = true;
m_ctx.Attach(this, null);
}

protected override void OnTextChanged(EventArgs e)
Expand All @@ -296,19 +279,6 @@ protected override void OnTextChanged(EventArgs e)
if (!DesignMode) ColorText();
}

protected void Paste(string strData)
{
if (this.SelectionLength > 0)
{
this.SelectedText = strData;
return;
}
int nCursorPos = this.SelectionStart;
string strPre = this.Text.Substring(0, nCursorPos);
string strPost = this.Text.Substring(nCursorPos);
this.Text = strPre + strData + strPost;
}

public static List<CharRange> GetRanges(string s)
{
List<CharRange> lCR = new List<CharRange>();
Expand Down Expand Up @@ -381,18 +351,5 @@ public void ColorText()

Select(nCursorPos, 0); //restore cursor position
}

private void ContextMenuStrip_Opening(object sender, EventArgs e)
{
//Mono does not suppport 'searchAllChildren' for ToolStripMenuItem
ContextMenuStrip.Items[0].Enabled = !string.IsNullOrEmpty(SelectedText); //Cut
ContextMenuStrip.Items[1].Enabled = !string.IsNullOrEmpty(SelectedText); //Copy
ContextMenuStrip.Items[2].Enabled = Clipboard.ContainsText(); //Paste
ContextMenuStrip.Items[3].Enabled = !string.IsNullOrEmpty(SelectedText); //Delete

//ContextMenuStrip.Items[4] = Separator

ContextMenuStrip.Items[5].Enabled = !string.IsNullOrEmpty(SelectedText); //Select All
}
}
}
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.12.1")]
[assembly: AssemblyFileVersion("0.12.1")]
[assembly: AssemblyVersion("0.13")]
[assembly: AssemblyFileVersion("0.13")]
2 changes: 1 addition & 1 deletion src/Utilities/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static PluginDebug()
AskOpen = KeePass.Program.CommandLineArgs["debugsaveonly"] == null;

DebugMode = KeePass.Program.CommandLineArgs[KeePass.App.AppDefs.CommandLineOptions.Debug] != null;
if (!DebugMode)
if (!DebugMode && (KeePass.Program.CommandLineArgs["debugplugin"] != null))
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion version.info
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
:
ColoredPassword:0.12.1
ColoredPassword:0.13
ColoredPassword!de:5
ColoredPassword!pl:2
ColoredPassword!pt:1
Expand Down

0 comments on commit d31106f

Please sign in to comment.