Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Web.Mobile / UI / MobileControls / Design / Util / MSHTMLHost.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="MSHTMLHost.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 //------------------------------------------------------------------------------
6
7 // MSHTMLHost.cs
8 //
9 // 12/17/98: Created: [....]
10 //
11
12 namespace System.Web.UI.Design.MobileControls.Util {
13     using System.Runtime.Serialization.Formatters;
14     
15     using System.Runtime.InteropServices;
16     using System.ComponentModel;
17
18     using System.Diagnostics;
19
20     using System;
21     
22     using Microsoft.Win32;    
23     using System.Windows.Forms;
24
25     /// <include file='doc\MSHTMLHost.uex' path='docs/doc[@for="MSHTMLHost"]/*' />
26     /// <devdoc>
27     ///    Control that hosts a Trident DocObject.
28     /// </devdoc>
29     /// <internalonly/>
30     // 
31
32
33     [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
34     [Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
35     internal sealed class MSHTMLHost : Control {
36         private TridentSite tridentSite;
37
38         internal MSHTMLHost() : base() {
39         }
40
41         public NativeMethods.IHTMLDocument2 GetDocument() {
42             Debug.Assert(tridentSite != null,
43                          "Cannot call getDocument before calling createTrident");
44
45             return tridentSite.GetDocument();
46         }
47
48         protected override CreateParams CreateParams {
49              get {
50                 CreateParams cp = base.CreateParams;
51
52                 cp.ExStyle |= NativeMethods.WS_EX_STATICEDGE;
53                 return cp;
54             }
55         }
56
57         internal bool CreateTrident() {
58             Debug.Assert(Handle != IntPtr.Zero,
59                          "MSHTMLHost must first be created before createTrident is called");
60
61             try {
62                 tridentSite = new TridentSite(this);
63             }
64             catch (Exception e) {
65                 Debug.WriteLine("Exception caught in MSHTMLHost::CreateTrident\n\t" + e.ToString());
66                 return false;
67             }
68             return true;
69         }
70
71         internal void ActivateTrident() {
72             Debug.Assert(tridentSite != null,
73                          "cannot call activateTrident before calling createTrident");
74
75             tridentSite.Activate();
76         }
77     }
78
79
80     /// <include file='doc\MSHTMLHost.uex' path='docs/doc[@for="TridentSite"]/*' />
81     /// <devdoc>
82     ///    Implements the client site for Trident DocObject
83     /// </devdoc>
84     [ClassInterface(ClassInterfaceType.None)]
85     [Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
86     internal class TridentSite : NativeMethods.IOleClientSite, NativeMethods.IOleDocumentSite, NativeMethods.IOleInPlaceSite, NativeMethods.IOleInPlaceFrame, NativeMethods.IDocHostUIHandler {
87
88         protected Control parentControl;
89         protected NativeMethods.IOleDocumentView tridentView;
90         protected NativeMethods.IOleObject tridentOleObject;
91         protected NativeMethods.IHTMLDocument2 tridentDocument;
92
93         protected EventHandler resizeHandler;
94
95         internal TridentSite(Control parent) {
96             Debug.Assert((parent != null) && (parent.Handle != IntPtr.Zero),
97                          "Invalid control passed in as parent of Trident window");
98
99             parentControl = parent;
100             resizeHandler = new EventHandler(this.OnParentResize);
101             parentControl.Resize += resizeHandler;
102
103             CreateDocument();
104         }
105
106         public NativeMethods.IHTMLDocument2 GetDocument() {
107             return tridentDocument;
108         }
109
110         internal void Activate() {
111             ActivateDocument();
112         }
113
114         protected virtual void OnParentResize(object src, EventArgs e) {
115             if (tridentView != null) {
116                 NativeMethods.COMRECT r = new NativeMethods.COMRECT();
117
118                 NativeMethods.GetClientRect(parentControl.Handle, r);
119                 tridentView.SetRect(r);
120             }
121         }
122
123
124         ///////////////////////////////////////////////////////////////////////////
125         // IOleClientSite Implementation
126
127         public virtual void SaveObject() {
128         }
129
130         public virtual object GetMoniker(int dwAssign, int dwWhichMoniker) {
131             throw new COMException(String.Empty, NativeMethods.E_NOTIMPL);
132         }
133
134         public virtual int GetContainer(out NativeMethods.IOleContainer ppContainer) {
135             ppContainer = null;
136             return NativeMethods.E_NOINTERFACE;
137         }
138
139         public virtual void ShowObject() {
140         }
141
142         public virtual void OnShowWindow(int fShow) {
143         }
144
145         public virtual void RequestNewObjectLayout() {
146         }
147
148
149         ///////////////////////////////////////////////////////////////////////////
150         // IOleDocumentSite Implementation
151
152         public virtual int ActivateMe(NativeMethods.IOleDocumentView pViewToActivate) {
153             Debug.Assert(pViewToActivate != null,
154                          "Expected the view to be non-null");
155             if (pViewToActivate == null)
156                 return NativeMethods.E_INVALIDARG;
157
158             NativeMethods.COMRECT r = new NativeMethods.COMRECT();
159
160             NativeMethods.GetClientRect(parentControl.Handle, r);
161
162             tridentView = pViewToActivate;
163             tridentView.SetInPlaceSite((NativeMethods.IOleInPlaceSite)this);
164             tridentView.UIActivate(1);
165             tridentView.SetRect(r);
166             tridentView.Show(1);
167
168             return NativeMethods.S_OK;
169         }
170
171
172         ///////////////////////////////////////////////////////////////////////////
173         // IOleInPlaceSite Implementation
174
175         public virtual IntPtr GetWindow() {
176             return parentControl.Handle;
177         }
178
179         public virtual void ContextSensitiveHelp(int fEnterMode) {
180             throw new COMException(String.Empty, NativeMethods.E_NOTIMPL);
181         }
182
183         public virtual int CanInPlaceActivate() {
184             return NativeMethods.S_OK;
185         }
186
187         public virtual void OnInPlaceActivate() {
188         }
189
190         public virtual void OnUIActivate() {
191         }
192
193         public virtual void GetWindowContext(out NativeMethods.IOleInPlaceFrame ppFrame, out NativeMethods.IOleInPlaceUIWindow ppDoc, NativeMethods.COMRECT lprcPosRect, NativeMethods.COMRECT lprcClipRect, NativeMethods.tagOIFI lpFrameInfo) {
194
195             ppFrame = (NativeMethods.IOleInPlaceFrame)this;
196             ppDoc = null;
197
198             NativeMethods.GetClientRect(parentControl.Handle, lprcPosRect);
199             NativeMethods.GetClientRect(parentControl.Handle, lprcClipRect);
200
201             lpFrameInfo.cb = System.Runtime.InteropServices.Marshal.SizeOf(typeof(NativeMethods.tagOIFI));
202             lpFrameInfo.fMDIApp = 0;
203             lpFrameInfo.hwndFrame = parentControl.Handle;
204             lpFrameInfo.hAccel = IntPtr.Zero;
205             lpFrameInfo.cAccelEntries = 0;
206         }
207
208         public virtual int Scroll(NativeMethods.tagSIZE scrollExtant) {
209             return(NativeMethods.E_NOTIMPL);
210         }
211
212         public virtual void OnUIDeactivate(int fUndoable) {
213             // NOTE, [....], 7/99: Don't return E_NOTIMPL. Somehow doing nothing and returning S_OK
214             //    fixes trident hosting in Win2000.
215         }
216
217         public virtual void OnInPlaceDeactivate() {
218         }
219
220         public virtual void DiscardUndoState() {
221             throw new COMException(SR.GetString(SR.MSHTMLHost_Not_Implemented), NativeMethods.E_NOTIMPL);
222         }
223
224         public virtual void DeactivateAndUndo() {
225         }
226
227         public virtual int OnPosRectChange(NativeMethods.COMRECT lprcPosRect) {
228             return NativeMethods.S_OK;
229         }
230
231
232         ///////////////////////////////////////////////////////////////////////////
233         // IOleInPlaceFrame Implementation
234
235         public virtual void GetBorder(NativeMethods.COMRECT lprectBorder) {
236             throw new COMException(String.Empty, NativeMethods.E_NOTIMPL);
237         }
238
239         public virtual void RequestBorderSpace(NativeMethods.COMRECT pborderwidths) {
240             throw new COMException(String.Empty, NativeMethods.E_NOTIMPL);
241         }
242
243         public virtual void SetBorderSpace(NativeMethods.COMRECT pborderwidths) {
244             throw new COMException(String.Empty, NativeMethods.E_NOTIMPL);
245         }
246
247         public virtual void SetActiveObject(NativeMethods.IOleInPlaceActiveObject pActiveObject, string pszObjName) {
248             // NOTE, [....], 7/99: Don't return E_NOTIMPL. Somehow doing nothing and returning S_OK
249             //    fixes trident hosting in Win2000.
250             // throw new COMException(String.Empty, NativeMethods.E_NOTIMPL);
251         }
252
253         public virtual void InsertMenus(IntPtr hmenuShared, object lpMenuWidths) {
254             throw new COMException(String.Empty, NativeMethods.E_NOTIMPL);
255         }
256
257         public virtual void SetMenu(IntPtr hmenuShared, IntPtr holemenu, IntPtr hwndActiveObject) {
258             throw new COMException(String.Empty, NativeMethods.E_NOTIMPL);
259         }
260
261         public virtual void RemoveMenus(IntPtr hmenuShared) {
262             throw new COMException(String.Empty, NativeMethods.E_NOTIMPL);
263         }
264
265         public virtual void SetStatusText(string pszStatusText) {
266         }
267
268         public virtual void EnableModeless(int fEnable) {
269         }
270
271         public virtual int TranslateAccelerator(ref NativeMethods.MSG lpmsg, short wID) {
272             return NativeMethods.S_FALSE;
273         }
274
275
276         ///////////////////////////////////////////////////////////////////////////
277         // IDocHostUIHandler Implementation
278
279         public virtual int ShowContextMenu(int dwID, NativeMethods.POINT pt, object pcmdtReserved, object pdispReserved) {
280             return NativeMethods.S_OK;
281         }
282
283         public virtual int GetHostInfo(NativeMethods.DOCHOSTUIINFO info) {
284             info.dwDoubleClick = NativeMethods.DOCHOSTUIDBLCLICK.DEFAULT;
285             info.dwFlags = NativeMethods.DOCHOSTUIFLAG.FLAT_SCROLLBAR |
286                            NativeMethods.DOCHOSTUIFLAG.NO3DBORDER |
287                            NativeMethods.DOCHOSTUIFLAG.DIALOG |
288                            NativeMethods.DOCHOSTUIFLAG.DISABLE_SCRIPT_INACTIVE;
289
290             return NativeMethods.S_OK;
291         }
292
293         public virtual int EnableModeless(bool fEnable) {
294             return NativeMethods.S_OK;
295         }
296
297         public virtual int ShowUI(int dwID, NativeMethods.IOleInPlaceActiveObject activeObject, NativeMethods.IOleCommandTarget commandTarget, NativeMethods.IOleInPlaceFrame frame, NativeMethods.IOleInPlaceUIWindow doc) {
298             return NativeMethods.S_OK;
299         }
300
301         public virtual int HideUI() {
302             return NativeMethods.S_OK;
303         }
304
305         public virtual int UpdateUI() {
306             return NativeMethods.S_OK;
307         }
308
309         public virtual int OnDocWindowActivate(bool fActivate) {
310             return NativeMethods.E_NOTIMPL;
311         }
312
313         public virtual int OnFrameWindowActivate(bool fActivate) {
314             return NativeMethods.E_NOTIMPL;
315         }
316
317         public virtual int ResizeBorder(NativeMethods.COMRECT rect, NativeMethods.IOleInPlaceUIWindow doc, bool fFrameWindow) {
318             return NativeMethods.E_NOTIMPL;
319         }
320
321         public virtual int GetOptionKeyPath(string[] pbstrKey, int dw) {
322             pbstrKey[0] = null;
323             return NativeMethods.S_OK;
324         }
325
326         public virtual int GetDropTarget(NativeMethods.IOleDropTarget pDropTarget, out NativeMethods.IOleDropTarget ppDropTarget) {
327             ppDropTarget = null;
328             return NativeMethods.S_FALSE;
329         }
330
331         public virtual int GetExternal(out object ppDispatch) {
332             ppDispatch = null;
333             return NativeMethods.S_OK;
334         }
335
336         public virtual int TranslateAccelerator(ref NativeMethods.MSG msg, ref Guid group, int nCmdID) {
337             return NativeMethods.S_OK;
338         }
339
340         public virtual int TranslateUrl(int dwTranslate, string strUrlIn, out string pstrUrlOut) {
341             pstrUrlOut = null;
342             return NativeMethods.E_NOTIMPL;
343         }
344
345         public virtual int FilterDataObject(NativeMethods.IOleDataObject pDO, out NativeMethods.IOleDataObject ppDORet) {
346             ppDORet = null;
347             return NativeMethods.S_OK;
348         }
349
350
351         ///////////////////////////////////////////////////////////////////////////
352         // Implementation
353
354         /// <include file='doc\MSHTMLHost.uex' path='docs/doc[@for="TridentSite.CreateDocument"]/*' />
355         /// <devdoc>
356         ///     Creates a new instance of mshtml and initializes it as a new document
357         ///     using its IPersistStreamInit.
358         /// </devdoc>
359         protected void CreateDocument() {
360
361             try {
362                 // Create an instance of Trident
363                 tridentDocument = (NativeMethods.IHTMLDocument2)new NativeMethods.HTMLDocument();
364                 tridentOleObject = (NativeMethods.IOleObject)tridentDocument;
365
366                 // Initialize its client site
367                 tridentOleObject.SetClientSite((NativeMethods.IOleClientSite)this);
368
369                 // Initialize it
370                 NativeMethods.IPersistStreamInit psi = (NativeMethods.IPersistStreamInit)tridentDocument;
371                 psi.InitNew();
372             }
373             catch (Exception e) {
374                 Debug.Fail(e.ToString());
375                 throw e;
376             }
377         }
378
379         /// <include file='doc\MSHTMLHost.uex' path='docs/doc[@for="TridentSite.ActivateDocument"]/*' />
380         /// <devdoc>
381         ///     Activates the mshtml instance
382         /// </devdoc>
383         protected void ActivateDocument() {
384             Debug.Assert(tridentOleObject != null,
385                          "How'd we get here when trident is null!");
386
387             try {
388                 NativeMethods.COMRECT r = new NativeMethods.COMRECT();
389
390                 NativeMethods.GetClientRect(parentControl.Handle, r);
391                 tridentOleObject.DoVerb(NativeMethods.OLEIVERB_UIACTIVATE, IntPtr.Zero, (NativeMethods.IOleClientSite)this, 0, parentControl.Handle, r);
392             }
393             catch (Exception e) {
394                 Debug.Fail(e.ToString());
395             }
396         }
397     }
398 }