Skip to content

Commit

Permalink
Merge pull request #39 from mitchcapper/239_support
Browse files Browse the repository at this point in the history
KP 2.39 support, rich textbox support, better password confirmation support, redid high dpi support and resize support completely.
  • Loading branch information
mitchcapper authored Jun 8, 2019
2 parents 00cfa75 + 1eb55b7 commit cf67fd9
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 220 deletions.
313 changes: 192 additions & 121 deletions EntryTemplateManager.ChildViewer.cs

Large diffs are not rendered by default.

93 changes: 50 additions & 43 deletions EntryTemplateManager.TemplateEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using KeePass.UI;
using KeePassLib;

namespace KPEntryTemplates {
Expand All @@ -23,6 +24,7 @@ private void InitializeGridView(TabPage page) {
return;
}

page.HorizontalScroll.Enabled = false;
page.SuspendLayout();
dataGridView = new DataGridView();
colTitle = new DataGridViewTextBoxColumn();
Expand All @@ -44,15 +46,18 @@ private void InitializeGridView(TabPage page) {
dataGridView.DefaultValuesNeeded += dataGridView_DefaultValuesNeeded;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Columns.AddRange(new DataGridViewColumn[] {
colTitle,
colTitle,
colField,
colFieldName,
colType,
colFieldName,
colType,
colOpt,colOptionValue});
dataGridView.Location = new Point(0, 0);
dataGridView.Name = "dataGridView";
dataGridView.Size = new System.Drawing.Size(TAB_WIDTH, TAB_HEIGHT);
dataGridView.TabIndex = 0;
dataGridView.TabIndex = 0;
SetBaseSizes(page);
var template = new EntryTemplate(null, null, "DataGridView", 0, null);//used just for the conrol sizing
SetControlSizing(template, dataGridView);
dataGridView.ScrollBars = ScrollBars.None;
dataGridView.DragDrop += dataGridView_DragDrop;
dataGridView.DragOver += dataGridView_DragOver;
dataGridView.MouseDown += dataGridView_MouseDown;
Expand All @@ -77,17 +82,17 @@ private void InitializeGridView(TabPage page) {
//
colField.HeaderText = "Field";
colField.Items.AddRange(new object[] {
"Custom",
"Title",
"Username",
"Password",
"Custom",
"Title",
"Username",
"Password",
"Password Confirmation",
"URL",
"Notes",
"Override URL",
"Expiry Date"});
colField.Name = "colField";
colField.Width = 105;
colField.Width = DpiUtil.ScaleIntX(105);
colField.DropDownWidth = 180;


Expand All @@ -98,56 +103,59 @@ private void InitializeGridView(TabPage page) {
colFieldName.HeaderText = "Field Name";
colFieldName.Name = "colFieldName";
colFieldName.SortMode = DataGridViewColumnSortMode.NotSortable;
colFieldName.Width = 75;
colFieldName.Width = DpiUtil.ScaleIntX(75);
//
// colType
//
colType.HeaderText = "Type";
colType.Name = "colType";
colType.Width = 100;
colType.Width = DpiUtil.ScaleIntX(100);
colType.Items.AddRange(new object[] {
"Inline",
"Inline",
"Inline URL",
"Popout",
"Protected Inline",
"Protected Popout",
"Popout",
"Protected Inline",
"Protected Popout",
"Date",
"Time",
"Date Time",
"Checkbox",
"Divider",
"Listbox"
"Listbox",
"RichTextbox"
});
colType.DropDownWidth = 150;
colOpt.HeaderText = "Opt";
colOpt.HeaderText = "Npt";
colOpt.Name = "colOpt";
colOpt.Width = 40;
colOpt.Width = DpiUtil.ScaleIntX(40);
colOpt.UseColumnTextForButtonValue = true;
colOpt.Text = "Opt";
colOpt.ToolTipText = "Option";
colOptionValue.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
colOptionValue.Visible = false;
colOptionValue.Name = "colOptionValue";
colOptionValue.Width = 0;


page.Controls.Add(dataGridView);
remove_as_template_button = new Button();
remove_as_template_button.Text = "Remove As Template";
remove_as_template_button.Width = TAB_WIDTH - 140 - 45;
remove_as_template_button.Left = 115;
remove_as_template_button.Height = BUTTON_HEIGHT;
remove_as_template_button.Width = PAGE_WIDTH - DpiUtil.ScaleIntX(140) - DpiUtil.ScaleIntX(45);
remove_as_template_button.Left = DpiUtil.ScaleIntX(85);
//remove_as_template_button.Height = 28;
remove_as_template_button.UseVisualStyleBackColor = true;
dataGridView.Size = new Size(TAB_WIDTH, TAB_HEIGHT - remove_as_template_button.Height - 10); //have to set its size before setting the buttons size
remove_as_template_button.Top = dataGridView.Height + 5;

dataGridView.Tag = template;

remove_as_template_button.Tag = template;
remove_as_template_button.Click += remove_as_template_button_Click;
SetControlSizing(template, remove_as_template_button);
page.Controls.Add(remove_as_template_button);
form.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
form.AutoScaleMode = AutoScaleMode.Font;
page.ResumeLayout();

}
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e){
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e) {
if (e.ColumnIndex != dataGridView.Columns["colOpt"].Index)
return;
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
Expand All @@ -157,14 +165,15 @@ private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e){
String msg = "";
switch (type) {
case "Inline":
case "RichTextbox"://CustomRichTextBoxEx
case "Protected Inline":
msg = "How many lines to show for the textbox(1-100)?";
break;
case "Listbox":
msg = "Listbox Items, seperate with each with a comma";
break;
}
String ret = OptionsForm.GetOption(msg, (string) row.Cells["colOptionValue"].Value);
String ret = OptionsForm.GetOption(msg, (string)row.Cells["colOptionValue"].Value);
if (ret != null)
row.Cells["colOptionValue"].Value = ret;
}
Expand All @@ -190,7 +199,7 @@ void dataGridView_EditingControlShowing(object sender, DataGridViewEditingContro
else if (cell.OwningColumn.Name == "colType") {
box.SelectedIndexChanged += col_type_box_SelectedIndexChanged;
}
if (box != null){
if (box != null) {
box.DropDown += box_DropDown;
box.SelectedIndexChanged += box_SelectedIndexChanged;
}
Expand All @@ -204,19 +213,20 @@ void box_SelectedIndexChanged(object sender, EventArgs e) {
void box_DropDown(object sender, EventArgs e) {
((DataGridViewComboBoxEditingControl)sender).BackColor = Color.White;
}
private void SetRowOptionEnabled(DataGridViewRow row, String type){
private void SetRowOptionEnabled(DataGridViewRow row, String type) {
bool opt_enabled = false;
switch (type) {
case "Inline":
case "Protected Inline":
case "RichTextbox":
case "Listbox":
opt_enabled = true;
break;
}

(row.Cells["colOpt"] as DataGridViewDisableButtonCell).Enabled = opt_enabled;
}
private void col_type_box_SelectedIndexChanged(object sender, EventArgs e){
private void col_type_box_SelectedIndexChanged(object sender, EventArgs e) {
DataGridViewCell cell = dataGridView.CurrentCell;
String type = cell.EditedFormattedValue.ToString();
SetRowOptionEnabled(cell.OwningRow, type);
Expand Down Expand Up @@ -260,7 +270,7 @@ void col_field_box_SelectedIndexChanged(object sender, EventArgs e) {
break;
case "Notes":
fieldName = PwDefs.NotesField;
type = conf.ProtectNotes ? "Protected Inline" : "Inline";
type = conf.ProtectNotes ? "Protected Inline" : "RichTextbox";
break;
default:
type = "";
Expand All @@ -274,13 +284,13 @@ void col_field_box_SelectedIndexChanged(object sender, EventArgs e) {
return;
}
row.Cells["colType"].ReadOnly = row.Cells["colFieldName"].ReadOnly = read_only;
if (type != ""){
if (type != "") {
row.Cells["colType"].Value = type;
SetRowOptionEnabled(row, type);
}
if (fieldName != "")
row.Cells["colFieldName"].Value = fieldName;


}

Expand All @@ -293,7 +303,7 @@ void dataGridView_SelectionChanged(object sender, EventArgs e) {
to_del = null;

}
private void RemoveToDel(){
private void RemoveToDel() {
if (to_del != null) {
if (dataGridView.Rows.Contains(to_del))
dataGridView.Rows.Remove(to_del);
Expand Down Expand Up @@ -369,8 +379,7 @@ void dataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e
if (res != DialogResult.Retry) {
dataGridView.CancelEdit();
to_del = row;
}
else {
} else {
e.Cancel = true;
row.ErrorText = old_row_err;
}
Expand Down Expand Up @@ -448,8 +457,7 @@ private void init_data_table() {
row.Cells["colOptionValue"].Value = t.options;
}

}
catch (Exception e) {
} catch (Exception e) {
MessageBox.Show(e.Message);
}
}
Expand Down Expand Up @@ -481,8 +489,7 @@ private void dataGridView_MouseDown(object sender, MouseEventArgs e) {
Size dragSize = SystemInformation.DragSize;
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)),
dragSize);
}
else
} else
dragBoxFromMouseDown = Rectangle.Empty;
}

Expand Down
8 changes: 3 additions & 5 deletions EntryTemplateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
using KeePassLib.Utility;
using KeePass.UI;
using KeePass.Resources;
using System.Threading.Tasks;

namespace KPEntryTemplates {
partial class EntryTemplateManager {
private const int TAB_WIDTH = 465;
private const int TAB_HEIGHT = 350;

private PwEntryForm form;
private IPluginHost m_host;
Expand Down Expand Up @@ -43,7 +42,7 @@ public EntryTemplateManager(IPluginHost host, PwEntryForm form) {
form.EntrySaving += form_EntrySaving;
our_page.UseVisualStyleBackColor = true;
form_tab_control.TabPages.Insert(0, our_page);
if (entry_is_child || entry_is_template)
if (entry_is_child || entry_is_template && !KeePassLib.Native.NativeLib.IsUnix())
form_tab_control.SelectTab(0);
}

Expand Down Expand Up @@ -102,8 +101,7 @@ private void child_view_show() {
entry_is_child = false;
buttons_show();
return;
}

}
init_child_vals();
}
public static bool entry_is_in_template_group(IPluginHost m_host, PwGroup group) {
Expand Down
78 changes: 34 additions & 44 deletions KPEntryTemplates.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using KeePass.Forms;
using KeePass.Plugins;
using KeePass.Resources;
using KeePass.UI;
using KeePass.Util;
using KeePassLib;
Expand Down Expand Up @@ -48,50 +50,38 @@ void GlobalWindowManager_WindowAdded(object sender, GwmWindowEventArgs e) {
if (form == null)
return;
form.Shown += form_Shown;
form.Resize += form_Resize;
form.Resize += form_Resize;
}

void form_Shown(object sender, EventArgs e)
{
PwEntryForm form = sender as PwEntryForm;
new EntryTemplateManager(m_host, form);
}

void form_Resize(object sender, EventArgs e)
{
// on form resize, change edits and bottom button widths;
// also reposition right side buttons

PwEntryForm form = sender as PwEntryForm;

TabControl tabControl = null;
foreach (Control c in form.Controls) {
if (c is TabControl) {
tabControl = c as TabControl;
break;
}
}
if (tabControl == null) return;

TabPage tmplPage = tabControl.TabPages[0];
if (tmplPage.Text != "Template") return;

foreach (Control c in tmplPage.Controls) {
if (!(c is Label)) {
if (c is CheckBox) {
c.Left = tmplPage.Width - ((c.Width + 55) / 2);
} else if (c is Button) {
if ((c as Button).Text == "Remove As Template Child") {
c.Width = tmplPage.Width - c.Left - 55;
} else {
c.Left = tmplPage.Width - ((c.Width + 55) / 2);
}
} else {
c.Width = tmplPage.Width - c.Left - 55;
}
}
}
}
private void form_Shown(object sender, EventArgs e) {
PwEntryForm form = sender as PwEntryForm;
new EntryTemplateManager(m_host, form);
//form_Resize(sender, e);

}

void form_Resize(object sender, EventArgs e) {


PwEntryForm form = sender as PwEntryForm;

TabControl tabControl = null;
foreach (Control c in form.Controls) {
if (c is TabControl) {
tabControl = c as TabControl;
break;
}
}
if (tabControl == null) return;

TabPage tmplPage = tabControl.TabPages[0];
if (tmplPage.Text != "Template")
return;
EntryTemplateManager.SetBaseSizes(tmplPage);
foreach (Control c in tmplPage.Controls)
EntryTemplateManager.UpdateControlSize(c);

}

void EntryTemplates_EntryCreating(object sender, TemplateEntryEventArgs e) {
EntryTemplateManager.InitChildEntry(e.TemplateEntry, e.Entry);
Expand All @@ -115,7 +105,7 @@ void m_tsmi_set_template_parent_Click(object sender, EventArgs e) {
return;
PwEntry[] entries = m_host.MainWindow.GetSelectedEntries();
foreach (PwEntry entry in entries) {
EntryTemplateManager.set_entry_template_parent(m_host.Database,entry, parent);
EntryTemplateManager.set_entry_template_parent(m_host.Database, entry, parent);
}
m_host.MainWindow.UpdateUI(false, null, false, m_host.MainWindow.GetSelectedGroup(), false, null, true);
}
Expand All @@ -137,7 +127,7 @@ void EntryContextMenu_Opening(object sender, System.ComponentModel.CancelEventAr
}
public override string UpdateUrl {
get {
return "http://mitchcapper.com/keepass_versions.txt?KPET";
return "http://mitchcapper.com/keepass_versions.txt?KPET&KPV=" + PwDefs.VersionString;
}
}
public override void Terminate() {
Expand Down
Loading

0 comments on commit cf67fd9

Please sign in to comment.