-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathBookmarksViewer.cs
161 lines (146 loc) · 4.9 KB
/
BookmarksViewer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace Patagames.Pdf.Net.Controls.Wpf
{
/// <summary>
/// Represents the BookmarksViewer control for displaying bookmarks contained in PDF document.
/// </summary>
public class BookmarksViewer : TreeView
{
#region Dependency properties
/// <summary>
/// DependencyProperty as the backing store for <see cref="PdfViewer"/>
/// </summary>
public static readonly DependencyProperty PdfViewerProperty =
DependencyProperty.Register("PdfViewer", typeof(PdfViewer), typeof(BookmarksViewer),
new PropertyMetadata(null,
(o, e) =>
{
var bookmarksViewer = o as BookmarksViewer;
var oldValue = e.OldValue as PdfViewer;
var newValue = e.NewValue as PdfViewer;
if (oldValue != newValue)
bookmarksViewer.OnPdfViewerChanging(oldValue, newValue);
}));
#endregion
#region Public Properties
/// <summary>
/// Gets or sets PdfViewer control associated with this BookmarksViewer control
/// </summary>
/// <remarks>It's a dependency property. Please find more details here: <see cref="BookmarksViewer.PdfViewerProperty"/></remarks>
public PdfViewer PdfViewer
{
get { return (PdfViewer)GetValue(PdfViewerProperty); }
set { SetValue(PdfViewerProperty, value); }
}
#endregion
#region Constructors and initialization
/// <summary>
/// Initializes a new instance of the <see cref="BookmarksViewer"/> class.
/// </summary>
public BookmarksViewer()
{
string dataTemplateString =
@"<HierarchicalDataTemplate ItemsSource=""{Binding Path=Childs}"">" +
@"<TextBlock Text=""{Binding Title}"" />" +
@"</HierarchicalDataTemplate>";
ParserContext parserContext = new ParserContext();
parserContext.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
DataTemplate template = (DataTemplate)XamlReader.Parse(dataTemplateString, parserContext);
ItemTemplate = template;
}
#endregion
#region Overrides
/// <summary>
/// Raises the System.Windows.Controls.TreeView.SelectedItemChanged event when the
/// System.Windows.Controls.TreeView.SelectedItem property value changes.
/// </summary>
/// <param name="e">Provides the item that was previously selected and the item that is currently
/// selected for the System.Windows.Controls.TreeView.SelectedItemChanged event.</param>
protected override void OnSelectedItemChanged(RoutedPropertyChangedEventArgs<object> e)
{
var bookmark = e.NewValue as PdfBookmark;
if (bookmark == null)
return;
if (bookmark.Action != null)
ProcessAction(bookmark.Action);
else if (bookmark.Destination != null)
ProcessDestination(bookmark.Destination);
base.OnSelectedItemChanged(e);
}
#endregion
#region Protected methods
/// <summary>
/// Called when the current PdfViewer control associated with the ToolStrip is changing.
/// </summary>
/// <param name="oldValue">PdfViewer control of which was associated with the ToolStrip.</param>
/// <param name="newValue">PdfViewer control of which will be associated with the ToolStrip.</param>
protected virtual void OnPdfViewerChanging(PdfViewer oldValue, PdfViewer newValue)
{
if (oldValue != null)
{
oldValue.AfterDocumentChanged -= pdfViewer_DocumentChanged;
oldValue.DocumentClosed -= pdfViewer_DocumentClosed;
oldValue.DocumentLoaded -= pdfViewer_DocumentLoaded;
}
if (newValue != null)
{
newValue.AfterDocumentChanged += pdfViewer_DocumentChanged;
newValue.DocumentClosed += pdfViewer_DocumentClosed;
newValue.DocumentLoaded += pdfViewer_DocumentLoaded;
}
RebuildTree();
}
/// <summary>
/// Process the <see cref="PdfAction"/>.
/// </summary>
/// <param name="pdfAction">PdfAction to be performed.</param>
protected virtual void ProcessAction(PdfAction pdfAction)
{
if (PdfViewer == null)
return;
PdfViewer.ProcessAction(pdfAction);
}
/// <summary>
/// Process the <see cref="PdfDestination"/>.
/// </summary>
/// <param name="pdfDestination">PdfDestination to be performed.</param>
protected virtual void ProcessDestination(PdfDestination pdfDestination)
{
if (PdfViewer == null)
return;
PdfViewer.ProcessDestination(pdfDestination); ;
}
#endregion
#region Private event handlers
private void pdfViewer_DocumentChanged(object sender, EventArgs e)
{
RebuildTree();
}
private void pdfViewer_DocumentLoaded(object sender, EventArgs e)
{
RebuildTree();
}
private void pdfViewer_DocumentClosed(object sender, EventArgs e)
{
RebuildTree();
}
#endregion
#region Public methods
/// <summary>
/// Constructs the tree of bookmarks
/// </summary>
public void RebuildTree()
{
if (PdfViewer == null || PdfViewer.Document == null || PdfViewer.Document.Bookmarks == null)
this.ItemsSource = null;
else
{
this.ItemsSource = PdfViewer.Document.Bookmarks;
}
}
#endregion
}
}