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#
Raw Normal View History

using bzit.bomg.Models;
2017-07-30 11:59:34 +00:00
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
2017-07-30 11:59:34 +00:00
using System.Windows.Forms;
namespace bzit.bomg
{
public partial class BookmarkTreeView : RyzStudio.Windows.Forms.MovableTreeView
{
protected IconDatabase iconDatabase = null;
2017-07-30 11:59:34 +00:00
public BookmarkTreeView()
{
InitializeComponent();
if (this.ImageList == null)
{
this.ImageList = new ImageList();
}
2017-07-30 11:59:34 +00:00
this.ImageList.ColorDepth = ColorDepth.Depth16Bit;
this.ImageList.ImageSize = new Size(16, 16);
this.ImageList.TransparentColor = Color.Transparent;
2017-07-30 11:59:34 +00:00
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);
2017-07-30 11:59:34 +00:00
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new ImageList ImageList { get => base.ImageList; set => base.ImageList = value; }
2017-07-30 11:59:34 +00:00
protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
2017-07-30 11:59:34 +00:00
{
base.OnPreviewKeyDown(e);
2017-07-30 11:59:34 +00:00
TreeNode tn = this.SelectedNode;
if (tn == null)
{
return;
}
2017-07-30 11:59:34 +00:00
switch (e.KeyCode)
{
case Keys.Insert:
if (e.Modifiers == Keys.Shift)
{
// do nothing
}
else
{
MessageBox.Show("!");
//AddBookmarkPage();
}
2017-07-30 11:59:34 +00:00
break;
2017-07-30 11:59:34 +00:00
default: break;
}
}
2017-07-30 11:59:34 +00:00
public bool InitialiseIconDatabase(out string message, string filename)
2017-07-30 11:59:34 +00:00
{
message = string.Empty;
2017-07-30 11:59:34 +00:00
if (string.IsNullOrWhiteSpace(filename))
{
return false;
}
2017-07-30 11:59:34 +00:00
if (iconDatabase == null)
2017-07-30 11:59:34 +00:00
{
iconDatabase = new IconDatabase();
2017-07-30 11:59:34 +00:00
}
bool rv = false;
if (File.Exists(filename))
2017-07-30 11:59:34 +00:00
{
rv = iconDatabase.LoadFile(filename);
if (!rv)
2017-07-30 11:59:34 +00:00
{
rv = iconDatabase.Create(filename, true, null, true);
if (!rv)
2017-07-30 11:59:34 +00:00
{
message = iconDatabase.LastError;
return false;
2017-07-30 11:59:34 +00:00
}
}
}
else
{
rv = iconDatabase.Create(filename, true, null, true);
if (!rv)
2017-07-30 11:59:34 +00:00
{
message = iconDatabase.LastError;
return false;
2017-07-30 11:59:34 +00:00
}
}
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);
2017-07-30 11:59:34 +00:00
}
public void AddItem_OnSelectedNode(BookmarkItemViewModel viewModel)
2017-07-30 11:59:34 +00:00
{
if (this.SelectedNode == null)
2017-07-30 11:59:34 +00:00
{
return;
2017-07-30 11:59:34 +00:00
}
if (this.SelectedNode.Tag != null)
2017-07-30 11:59:34 +00:00
{
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)
2017-07-30 11:59:34 +00:00
{
return (int)IconSet.Default;
}
else
{
this.ImageList.Images.Add(model.SiteAddress, rs);
return this.ImageList.Images.IndexOfKey(model.SiteAddress);
2017-07-30 11:59:34 +00:00
}
}
Bitmap bmp = model.RetrieveFavicon();
if (bmp == null)
2017-07-30 11:59:34 +00:00
{
return (int)IconSet.Default;
2017-07-30 11:59:34 +00:00
}
this.ImageList.Images.Add(model.SiteAddress, bmp);
return this.ImageList.Images.IndexOfKey(model.SiteAddress);
2017-07-30 11:59:34 +00:00
}
protected TreeNode addFolderPath(string path)
{
TreeNode tn = this.Nodes[0];
if (tn == null)
2017-07-30 11:59:34 +00:00
{
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];
2017-07-30 11:59:34 +00:00
}
return tn;
2017-07-30 11:59:34 +00:00
}
}
}