aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Package/Editors/Editor.EditorPane.cs
blob: faf62ec391b64d32381235012b41780d670a3ff1 (plain)
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
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace QtVsTools.Package.Editors
{
    using Core;

    public abstract partial class Editor
    {
        internal class EditorPane : WindowPane, IVsPersistDocData
        {
            private Editor Editor { get; }
            private string QtToolsPath { get; }

            private TableLayoutPanel EditorContainer { get; set; }
            private Panel EditorControl { get; }
            public override IWin32Window Window => EditorContainer;

            private Process EditorProcess { get; set; }
            private IntPtr EditorWindow { get; set; }
            private int EditorWindowStyle { get; set; }
            private int EditorWindowStyleExt { get; set; }
            private IntPtr EditorIcon { get; set; }

            private NotifyDetach NotifyDetach { get; set; }

            public EditorPane(Editor editor, string qtToolsPath)
            {
                Editor = editor;
                QtToolsPath = qtToolsPath;

                EditorControl = new Panel
                {
                    BackColor = SystemColors.Window,
                    Dock = DockStyle.Fill
                };
                EditorControl.Margin = Padding.Empty;
                EditorControl.Resize += EditorControl_Resize;

                EditorContainer = new TableLayoutPanel
                {
                    ColumnCount = 1,
                    RowCount = 1
                };
                EditorContainer.ColumnStyles.Add(new ColumnStyle());
                EditorContainer.RowStyles.Add(new RowStyle());
                EditorContainer.Controls.Add(EditorControl, 0, 0);
            }

            protected override void Dispose(bool disposing)
            {
                try {
                    if (disposing) {
                        EditorContainer?.Dispose();
                        EditorContainer = null;
                        GC.SuppressFinalize(this);
                    }
                } finally {
                    base.Dispose(disposing);
                }
            }

            int IVsPersistDocData.GetGuidEditorType(out Guid pClassID)
            {
                pClassID = Editor.Guid;
                return VSConstants.S_OK;
            }

            int IVsPersistDocData.LoadDocData(string pszMkDocument)
            {
                EditorProcess = Editor.Start(pszMkDocument, QtToolsPath,
                    hideWindow: !Editor.Detached);
                if (EditorProcess == null)
                    return VSConstants.E_FAIL;

                EditorProcess.EnableRaisingEvents = true;

                if (Editor.Detached) {
                    Editor.OnStart(EditorProcess);
                    CloseParentFrame();
                    return VSConstants.S_OK;
                }

                if (Editor.ShowDetachNotification)
                    ShowDetachBar();

                EditorProcess.WaitForInputIdle();
                EditorProcess.Exited += EditorProcess_Exited;

                var t = Stopwatch.StartNew();
                while (EditorWindow == IntPtr.Zero && t.ElapsedMilliseconds < 5000) {
                    var windows = new Dictionary<IntPtr, string>();
                    foreach (ProcessThread thread in EditorProcess.Threads) {
                        NativeAPI.EnumThreadWindows(
                            dwThreadId: (uint)thread.Id,
                            lParam: IntPtr.Zero,
                            lpfn: (hWnd, _) =>
                            {
                                windows.Add(hWnd, NativeAPI.GetWindowCaption(hWnd));
                                return true;
                            });
                    }

                    EditorWindow = windows
                        .Where(w => Editor.WindowFilter(w.Value))
                        .Select(w => w.Key)
                        .FirstOrDefault();

                    if (EditorWindow == IntPtr.Zero)
                        Thread.Sleep(100);
                }

                if (EditorWindow == IntPtr.Zero) {
                    EditorProcess.Kill();
                    EditorProcess = null;
                    return VSConstants.E_FAIL;
                }

                // Save editor window styles and icon
                EditorWindowStyle = NativeAPI.GetWindowLong(
                    EditorWindow, NativeAPI.GWL_STYLE);
                EditorWindowStyleExt = NativeAPI.GetWindowLong(
                    EditorWindow, NativeAPI.GWL_EXSTYLE);
                EditorIcon = NativeAPI.SendMessage(
                    EditorWindow, NativeAPI.WM_GETICON, NativeAPI.ICON_SMALL, 0);
                if (EditorIcon == IntPtr.Zero)
                    EditorIcon = (IntPtr)NativeAPI.GetClassLong(EditorWindow, NativeAPI.GCL_HICON);
                if (EditorIcon == IntPtr.Zero)
                    EditorIcon = (IntPtr)NativeAPI.GetClassLong(EditorWindow, NativeAPI.GCL_HICONSM);

                // Move editor window inside VS
                if (NativeAPI.SetParent(
                        EditorWindow, EditorControl.Handle) == IntPtr.Zero) {
                    EditorWindow = IntPtr.Zero;
                    EditorProcess.Kill();
                    EditorProcess = null;
                    return VSConstants.E_FAIL;
                }

                if (NativeAPI.SetWindowLong(
                    EditorWindow, NativeAPI.GWL_STYLE, NativeAPI.WS_VISIBLE) == 0) {
                    EditorWindow = IntPtr.Zero;
                    EditorProcess.Kill();
                    EditorProcess = null;
                    return VSConstants.E_FAIL;
                }

                if (!NativeAPI.MoveWindow(
                    EditorWindow, 0, 0, EditorControl.Width, EditorControl.Height, true)) {
                    EditorWindow = IntPtr.Zero;
                    EditorProcess.Kill();
                    EditorProcess = null;
                    return VSConstants.E_FAIL;
                }

                Editor.OnStart(EditorProcess);
                return VSConstants.S_OK;
            }

            void CloseParentFrame()
            {
                EditorProcess = null;
                EditorWindow = IntPtr.Zero;
                ThreadHelper.JoinableTaskFactory.Run(async () =>
                {
                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                    var parentFrame = GetService(typeof(SVsWindowFrame)) as IVsWindowFrame;
                    parentFrame?.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_NoSave);
                });
            }

            private void EditorProcess_Exited(object sender, EventArgs e)
            {
                CloseParentFrame();
                Editor.OnExit(EditorProcess);
            }

            void EditorControl_Resize(object sender, EventArgs e)
            {
                if (EditorControl != null && EditorWindow != IntPtr.Zero) {
                    NativeAPI.MoveWindow(
                        EditorWindow, 0, 0, EditorControl.Width, EditorControl.Height, true);
                }
            }

            private void ShowDetachBar()
            {
                ThreadHelper.JoinableTaskFactory.Run(async () =>
                {
                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                    if (GetService(typeof(SVsWindowFrame)) is not IVsWindowFrame parentFrame)
                        return;

                    var result = parentFrame.GetProperty((int)__VSFPROPID7.VSFPROPID_InfoBarHost,
                        out var value);
                    if (ErrorHandler.Failed(result) || value is not IVsInfoBarHost infoBarHost)
                        return;

                    NotifyDetach?.Close();
                    NotifyDetach = new NotifyDetach(Detach, Editor.DisableDetachNotification,
                        infoBarHost);
                    NotifyDetach.Show();
                });
            }

            public void Detach()
            {
                if (EditorProcess != null) {
                    var editorWindow = EditorWindow;
                    DetachEditorWindow();
                    CloseParentFrame();
                    NativeAPI.ShowWindow(editorWindow, NativeAPI.SW_RESTORE);
                    NativeAPI.SetForegroundWindow(editorWindow);
                }
            }

            public void DetachEditorWindow()
            {
                NativeAPI.ShowWindow(EditorWindow,
                    NativeAPI.SW_HIDE);
                NativeAPI.SetParent(
                    EditorWindow, IntPtr.Zero);
                NativeAPI.SetWindowLong(
                    EditorWindow, NativeAPI.GWL_STYLE, EditorWindowStyle);
                NativeAPI.SetWindowLong(
                    EditorWindow, NativeAPI.GWL_EXSTYLE, EditorWindowStyleExt);
                NativeAPI.SendMessage(
                    EditorWindow, NativeAPI.WM_SETICON, NativeAPI.ICON_SMALL, EditorIcon);
                NativeAPI.MoveWindow(
                    EditorWindow, 0, 0, EditorControl.Width, EditorControl.Height, true);
                NativeAPI.ShowWindow(EditorWindow,
                    NativeAPI.SW_SHOWMINNOACTIVE);
            }

            int IVsPersistDocData.Close()
            {
                if (EditorProcess != null) {
                    DetachEditorWindow();
                    var editorProcess = EditorProcess;
                    EditorProcess = null;
                    var editorWindow = EditorWindow;
                    EditorWindow = IntPtr.Zero;

                    // Close editor window
                    _ = System.Threading.Tasks.Task.Run(() =>
                    {
                        NativeAPI.SendMessage(editorWindow, NativeAPI.WM_CLOSE, 0, 0);
                        if (!editorProcess.WaitForExit(500)) {
                            NativeAPI.ShowWindow(editorWindow,
                                NativeAPI.SW_RESTORE);
                            NativeAPI.SetForegroundWindow(editorWindow);
                        }
                    });
                }

                if (EditorContainer == null) {
                    if (EditorContainer != null) {
                        EditorContainer.Dispose();
                        EditorContainer = null;
                    }
                }

                return VSConstants.S_OK;
            }

            int IVsPersistDocData.RenameDocData(uint grfAttribs,
                IVsHierarchy pHierNew,
                uint itemidNew,
                string pszMkDocumentNew)
            {
                return VSConstants.E_NOTIMPL;
            }

            int IVsPersistDocData.IsDocDataDirty(out int pfDirty)
            {
                pfDirty = 0;
                return VSConstants.S_OK;
            }

            int IVsPersistDocData.SetUntitledDocPath(string pszDocDataPath)
            {
                return VSConstants.S_OK;
            }

            int IVsPersistDocData.SaveDocData(VSSAVEFLAGS dwSave,
                out string pbstrMkDocumentNew,
                out int pfSaveCanceled)
            {
                pbstrMkDocumentNew = string.Empty;
                pfSaveCanceled = 0;
                return VSConstants.E_NOTIMPL;
            }

            int IVsPersistDocData.OnRegisterDocData(uint docCookie,
                IVsHierarchy pHierNew,
                uint itemidNew)
            {
                return VSConstants.S_OK;
            }

            int IVsPersistDocData.IsDocDataReloadable(out int pfReloadable)
            {
                pfReloadable = 0;
                return VSConstants.S_OK;
            }

            int IVsPersistDocData.ReloadDocData(uint grfFlags)
            {
                return VSConstants.E_NOTIMPL;
            }
        }
    }
}