This repository has been archived on 2022-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
bookmark-manager/BookmarkTreeView.cs

219 lines
6.0 KiB
C#

using bzit.bomg.Models;
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace bzit.bomg
{
public partial class BookmarkTreeView : RyzStudio.Windows.Forms.MovableTreeView
{
protected IconDatabase iconDatabase = null;
public BookmarkTreeView()
{
InitializeComponent();
if (this.ImageList == null)
{
this.ImageList = new ImageList();
}
this.ImageList.ColorDepth = ColorDepth.Depth16Bit;
this.ImageList.ImageSize = new Size(16, 16);
this.ImageList.TransparentColor = Color.Transparent;
this.ImageList.Images.Clear();
this.ImageList.Images.Add(Properties.Resources.transmit_blue);
this.ImageList.Images.Add(Properties.Resources.folder);
this.ImageList.Images.Add(Properties.Resources.folder_explore);
this.ImageList.Images.Add(Properties.Resources.page_white_world_bw);
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new ImageList ImageList { get => base.ImageList; set => base.ImageList = value; }
protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
{
base.OnPreviewKeyDown(e);
TreeNode tn = this.SelectedNode;
if (tn == null)
{
return;
}
switch (e.KeyCode)
{
case Keys.Insert:
if (e.Modifiers == Keys.Shift)
{
// do nothing
}
else
{
MessageBox.Show("!");
//AddBookmarkPage();
}
break;
default: break;
}
}
public bool InitialiseIconDatabase(out string message, string filename)
{
message = string.Empty;
if (string.IsNullOrWhiteSpace(filename))
{
return false;
}
if (iconDatabase == null)
{
iconDatabase = new IconDatabase();
}
bool rv = false;
if (File.Exists(filename))
{
rv = iconDatabase.LoadFile(filename);
if (!rv)
{
rv = iconDatabase.Create(filename, true, null, true);
if (!rv)
{
message = iconDatabase.LastError;
return false;
}
}
}
else
{
rv = iconDatabase.Create(filename, true, null, true);
if (!rv)
{
message = iconDatabase.LastError;
return false;
}
}
return true;
}
public void AddItem(BookmarkItemViewModel viewModel)
{
int iconIndex = addIcon(viewModel);
TreeNode tn = new TreeNode(viewModel.SiteName, iconIndex, iconIndex);
tn.Tag = viewModel;
tn.ToolTipText = viewModel.ToString();
TreeNode tn2 = addFolderPath(viewModel.TreeviewPath);
tn2.Nodes.Add(tn);
}
public void AddItem_OnSelectedNode(BookmarkItemViewModel viewModel)
{
if (this.SelectedNode == null)
{
return;
}
if (this.SelectedNode.Tag != null)
{
return;
}
int iconIndex = addIcon(viewModel);
TreeNode tn = new TreeNode(viewModel.SiteName, iconIndex, iconIndex);
tn.Tag = viewModel;
tn.ToolTipText = viewModel.ToString();
int n = this.SelectedNode.Nodes.Add(tn);
this.SelectedNode = this.SelectedNode.Nodes[n];
}
protected int addIcon(BookmarkItemViewModel viewModel) => addIcon(viewModel.ToModel());
protected int addIcon(BookmarkItemModel model)
{
if (this.ImageList.Images.ContainsKey(model.SiteAddress))
{
return this.ImageList.Images.IndexOfKey(model.SiteAddress);
}
if (iconDatabase.HasIcon(model.SiteAddress))
{
Image rs = iconDatabase.GetIcon(model.SiteAddress);
if (rs == null)
{
return (int)IconSet.Default;
}
else
{
this.ImageList.Images.Add(model.SiteAddress, rs);
return this.ImageList.Images.IndexOfKey(model.SiteAddress);
}
}
Bitmap bmp = model.RetrieveFavicon();
if (bmp == null)
{
return (int)IconSet.Default;
}
this.ImageList.Images.Add(model.SiteAddress, bmp);
return this.ImageList.Images.IndexOfKey(model.SiteAddress);
}
protected TreeNode addFolderPath(string path)
{
TreeNode tn = this.Nodes[0];
if (tn == null)
{
return tn;
}
if (string.IsNullOrWhiteSpace(path))
{
return tn;
}
if (string.IsNullOrWhiteSpace(path.Trim('\\')))
{
return tn;
}
string[] folderList = path.Trim('\\').Split('\\');
if (folderList.Length <= 0)
{
return tn;
}
foreach (string item in folderList)
{
if (tn.Nodes.ContainsKey(item))
{
// do nothing
}
else
{
tn.Nodes.Add(item, item, (int)IconSet.Folder1, (int)IconSet.Folder2);
}
tn = tn.Nodes[item];
}
return tn;
}
}
}