This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
PartViewHelper.cs
345 lines (293 loc) · 11.7 KB
/
PartViewHelper.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/****************************************************************************
PartViewHelper.cs
View helper object for the Part XML Editor windows.
------------------------------------------------------------- LICENSE BEGINS HERE--------------------------------------------------------------------------------------
Copyright (c) Microsoft Corporation
All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
-------------------------------------------------------------- LICENSE ENDS HERE -----------------------------------------------------------------------------------------
****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using System.Runtime.InteropServices;
namespace Microsoft.OpenXMLEditor
{
class PartViewHelper : IVsWindowFrameNotify3,
IVsTextLinesEvents
{
#region Members
private IVsWindowFrame windowFrame;
private PackageEditorPane editor;
private IVsTextLines textLines;
private uint textLinesEventsCookie;
// private uint finalTextChangeCommitEventsCookie; // Is this used now?
private bool justOpened;
private bool starred;
#endregion Members
#region Constructors
public PartViewHelper(PackageEditorPane editor, IVsWindowFrame windowFrame)
{
this.windowFrame = windowFrame;
this.editor = editor;
justOpened = true;
object data;
ErrorHandler.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out data));
textLines = (IVsTextLines)data;
AdviseTextLinesEvents(true);
}
#endregion Constructors
#region Properties
private string BufferContents
{
get
{
int lineCount;
int lengthOfLast;
string bufferText = string.Empty;
ErrorHandler.ThrowOnFailure(
this.textLines.GetLineCount(out lineCount)
);
if (lineCount > 0)
{
ErrorHandler.ThrowOnFailure(
this.textLines.GetLengthOfLine(lineCount - 1, out lengthOfLast)
);
ErrorHandler.ThrowOnFailure(
this.textLines.GetLineText(0, 0, lineCount - 1, lengthOfLast, out bufferText)
);
}
return bufferText;
}
set
{
int lastLine;
int lastColumn;
Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(
textLines.GetLastLineIndex(out lastLine, out lastColumn));
// Lock the buffer before changing its content.
Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(
textLines.LockBuffer());
try
{
GCHandle handle = GCHandle.Alloc(value, GCHandleType.Pinned);
try
{
TextSpan[] span = new TextSpan[1];
Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(
textLines.ReplaceLines(0, 0, lastLine, lastColumn, handle.AddrOfPinnedObject(), value.Length, span));
}
finally
{
handle.Free();
}
}
finally
{
Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(
textLines.UnlockBuffer());
}
}
}
public bool IsModified
{
get
{
uint flags;
ErrorHandler.ThrowOnFailure(this.textLines.GetStateFlags(out flags));
return (flags & (uint)BUFFERSTATEFLAGS.BSF_MODIFIED) != 0;
}
set
{
uint flags = 0;
ErrorHandler.ThrowOnFailure(textLines.GetStateFlags(out flags));
bool fStar = (flags & (uint)BUFFERSTATEFLAGS.BSF_MODIFIED) != 0;
if (fStar != value)
{
if (value)
flags |= (uint)BUFFERSTATEFLAGS.BSF_MODIFIED;
else
flags &= ~(uint)BUFFERSTATEFLAGS.BSF_MODIFIED;
ErrorHandler.ThrowOnFailure(textLines.SetStateFlags(flags));
UpdateStar(value);
}
}
}
private static bool HasToSave(uint pgrfSaveOptions)
{
return pgrfSaveOptions == (uint)__FRAMECLOSE.FRAMECLOSE_SaveIfDirty || pgrfSaveOptions == (uint)__FRAMECLOSE.FRAMECLOSE_PromptSave;
}
public string PhysicalView
{
get
{
object obj;
if (ErrorHandler.Succeeded(this.windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_pszPhysicalView, out obj)))
return obj as string;
return null;
}
}
public PackageEditorPane Editor
{
get
{
return editor;
}
}
#endregion Properties
#region Methods
public void CloseWindow()
{
this.windowFrame.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_SaveIfDirty);
}
public void ReloadView()
{
if (!this.editor.PartExits(PhysicalView))
{
this.IsModified = false;
CloseWindow();
return;
}
BufferContents = this.editor.GetXml(PhysicalView);
this.IsModified = false;
}
public void FlushChanges()
{
if (!this.IsModified)
return;
this.editor.UpdateXml(PhysicalView, BufferContents);
}
public void Show()
{
ErrorHandler.ThrowOnFailure(
windowFrame.Show()
);
}
private void Cleanup()
{
AdviseTextLinesEvents(false);
}
private void AdviseTextLinesEvents(bool subscribe)
{
IConnectionPointContainer conPtCont;
conPtCont = (IConnectionPointContainer)textLines;
if (conPtCont != null)
{
IConnectionPoint conPt;
Guid iidConPt = typeof(IVsTextLinesEvents).GUID;
conPtCont.FindConnectionPoint(ref iidConPt, out conPt);
if (subscribe)
{
conPt.Advise((IVsTextLinesEvents)this, out textLinesEventsCookie);
}
else
{
// If the goal is to unsubscribe, but there is no subscription active, exit.
if (textLinesEventsCookie == VSConstants.VSCOOKIE_NIL)
return;
conPt.Unadvise(textLinesEventsCookie);
textLinesEventsCookie = VSConstants.VSCOOKIE_NIL;
}
}
}
private void UpdateStar(bool fStar)
{
object editorCaptionObject;
ErrorHandler.ThrowOnFailure(
this.windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_EditorCaption, out editorCaptionObject)
);
string editorCaption = (string)editorCaptionObject;
if (fStar)
{
// add dirty star, if not already there.
if (editorCaption.Length > 0 && '*' != editorCaption[editorCaption.Length - 1])
{
editorCaption += "*";
ErrorHandler.ThrowOnFailure(this.windowFrame.SetProperty((int)__VSFPROPID.VSFPROPID_EditorCaption, editorCaption));
}
// also dirty the parent editor
editor.IsModified = true;
}
else
{
// remove dirty star if it is present
if (editorCaption.Length > 0 && '*' == editorCaption[editorCaption.Length - 1])
{
editorCaption = editorCaption.Remove(editorCaption.Length - 1);
ErrorHandler.ThrowOnFailure(this.windowFrame.SetProperty((int)__VSFPROPID.VSFPROPID_EditorCaption, editorCaption));
}
}
starred = fStar;
}
#endregion Methods
#region IVsWindowFrameNotify3
public int OnDockableChange(int fDockable, int x, int y, int w, int h)
{
return VSConstants.S_OK;
}
public int OnMove(int x, int y, int w, int h)
{
return VSConstants.S_OK;
}
public int OnShow(int fShow)
{
return VSConstants.S_OK;
}
public int OnSize(int x, int y, int w, int h)
{
return VSConstants.S_OK;
}
public int OnClose(ref uint pgrfSaveOptions)
{
//if (HasToSave(pgrfSaveOptions))
this.FlushChanges();
this.Cleanup();
return VSConstants.S_OK;
}
#endregion IVsWindowFrameNotify3
#region IVsTextLinesEvents
void IVsTextLinesEvents.OnChangeLineAttributes(int iFirstLine, int iLastLine)
{
}
void IVsTextLinesEvents.OnChangeLineText(Microsoft.VisualStudio.TextManager.Interop.TextLineChange[] pTextLineChange, int fLast)
{
if (justOpened)
{
justOpened = false;
string buffer;
if (pTextLineChange.Length == 1 && ErrorHandler.Succeeded(this.textLines.GetLineText(
pTextLineChange[0].iStartLine, pTextLineChange[0].iStartIndex,
pTextLineChange[0].iNewEndLine, pTextLineChange[0].iNewEndIndex, out buffer)))
{
// Just opening the editor dirties the buffer and triggers this event *if* the
// <?xml encoding=""?> attribute doesn't match the actual encoding of the buffer
// (van vary only by case such as "UTF-8" versus "utf-8").
try
{
// test to see if the buffer change was the encoding string
Encoding dummy = Encoding.GetEncoding(buffer);
// succeeded, so clear the dirty flag
IsModified = false;
return;
}
catch
{
// buffer wasn't an encoding string, so ignore it.
}
}
}
bool starred = IsModified;
if (starred != this.starred)
UpdateStar(starred);
// [This code is present in .NET Reflector version from shipping binary]
this.FlushChanges();
}
#endregion
} // end class
} // end namespace