summaryrefslogtreecommitdiffstats
path: root/chrome_frame/ole_document_impl.h
blob: f18db1635ede5e96563bbac4526e8057d54e044f (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
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// TODO(slightlyoff): Add any required LICENSE block changes for MSFT code
// inclusion.

// ole_document_impl.h : IOleDocument implementation
//
// This file is a modified version of the OleDocument.h file, which is
// part of the ActiveDoc MSDN sample. The modifications are largely
// conversions to Google coding guidelines. Below if the original header
// from the file.

// This is a part of the Active Template Library.
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// This source code is only intended as a supplement to the
// Active Template Library Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Active Template Library product.

#ifndef CHROME_FRAME_OLE_DOCUMENT_IMPL_H_
#define CHROME_FRAME_OLE_DOCUMENT_IMPL_H_

// TODO(sanjeevr): Revisit this impl file and cleanup dependencies
#include <atlbase.h>
#include <docobj.h>

#include "base/logging.h"

//////////////////////////////////////////////////////////////////////////////
// IOleDocumentImpl
template <class T>
class ATL_NO_VTABLE IOleDocumentImpl : public IOleDocument {
 public:
  STDMETHOD(CreateView)(IOleInPlaceSite* in_place_site,
                        IStream* stream,
                        DWORD reserved ,
                        IOleDocumentView** new_view) {
    DVLOG(1) << __FUNCTION__;
    if (new_view == NULL)
      return E_POINTER;
    T* t = static_cast<T*>(this);
    // If we've already created a view then we can't create another as we
    // currently only support the ability to create one view
    if (t->m_spInPlaceSite)
      return E_FAIL;
    IOleDocumentView* view;
    t->GetUnknown()->QueryInterface(IID_IOleDocumentView,
                                    reinterpret_cast<void**>(&view));
    // If we support IOleDocument we should support IOleDocumentView
    ATLENSURE(view != NULL);
    // If they've given us a site then use it
    if (in_place_site != NULL)
      view->SetInPlaceSite(in_place_site);
    // If they have given us an IStream pointer then use it to
    // initialize the view
    if (stream != NULL)
      view->ApplyViewState(stream);
    // Return the view
    *new_view = view;
    return S_OK;
  }

  STDMETHOD(GetDocMiscStatus)(DWORD* status) {
    DVLOG(1) << __FUNCTION__;
    if (NULL == status)
      return E_POINTER;
    *status = DOCMISC_NOFILESUPPORT;
    return S_OK;
  }

  STDMETHOD(EnumViews)(IEnumOleDocumentViews** enum_views,
                       IOleDocumentView** view) {
    DVLOG(1) << __FUNCTION__;
    if (view == NULL)
      return E_POINTER;
    T* t = static_cast<T*>(this);
    // We only support one view
    return t->_InternalQueryInterface(IID_IOleDocumentView,
                                      reinterpret_cast<void**>(view));
  }
};

//////////////////////////////////////////////////////////////////////////////
// IOleDocumentViewImpl

template <class T>
class ATL_NO_VTABLE IOleDocumentViewImpl : public IOleDocumentView {
 public:
  STDMETHOD(SetInPlaceSite)(IOleInPlaceSite* in_place_site) {
    DVLOG(1) << __FUNCTION__;
    T* t = static_cast<T*>(this);
    if (t->m_spInPlaceSite) {
      // If we already have a site get rid of it
      UIActivate(FALSE);
      HRESULT hr = t->InPlaceDeactivate();
      if (FAILED(hr))
        return hr;
      DCHECK(!t->m_bInPlaceActive);
    }
    if (in_place_site != NULL) {
      t->m_spInPlaceSite.Release();
      in_place_site->QueryInterface(
          IID_IOleInPlaceSiteWindowless,
          reinterpret_cast<void **>(&t->m_spInPlaceSite));
      if (!t->m_spInPlaceSite) {
        // TODO(sanjeevr): This is a super-hack because m_spInPlaceSite
        // is an IOleInPlaceSiteWindowless pointer and we are setting
        // an IOleInPlaceSite pointer into it. The problem is that ATL
        // (CComControlBase) uses this in a schizophrenic manner based
        // on the m_bWndLess flag. Ouch, ouch, ouch! Find a way to clean
        // this up.
        // Disclaimer: I did not invent this hack, it exists in the MSDN
        // sample from where this code has been derived and it also exists
        // in ATL itself (look at atlctl.h line 938).
        t->m_spInPlaceSite =
            reinterpret_cast<IOleInPlaceSiteWindowless*>(in_place_site);
      }
    }
    return S_OK;
  }

  STDMETHOD(GetInPlaceSite)(IOleInPlaceSite** in_place_site) {
    DVLOG(1) << __FUNCTION__;
    if (in_place_site == NULL)
      return E_POINTER;
    T* t = static_cast<T*>(this);
    return t->m_spInPlaceSite->QueryInterface(
        IID_IOleInPlaceSite,
        reinterpret_cast<LPVOID *>(in_place_site));
  }

  STDMETHOD(GetDocument)(IUnknown** document) {
    DVLOG(1) << __FUNCTION__;
    if (document == NULL)
      return E_POINTER;
    T* t = static_cast<T*>(this);
    *document = t->GetUnknown();
    (*document)->AddRef();
    return S_OK;
  }

  STDMETHOD(SetRect)(LPRECT view_rect) {
    static bool is_resizing = false;
    if (is_resizing)
      return S_OK;
    is_resizing = true;
    DVLOG(1) << __FUNCTION__ << " " << view_rect->left << ","
             << view_rect->top << "," << view_rect->right << ","
             << view_rect->bottom;
    T* t = static_cast<T*>(this);
    t->SetObjectRects(view_rect, view_rect);
    is_resizing = false;
    return S_OK;
  }

  STDMETHOD(GetRect)(LPRECT view_rect) {
    DVLOG(1) << __FUNCTION__;
    if (view_rect == NULL)
      return E_POINTER;
    T* t = static_cast<T*>(this);
    *view_rect = t->m_rcPos;
    return S_OK;
  }

  STDMETHOD(SetRectComplex)(LPRECT view_rect,
                            LPRECT hscroll_rect,
                            LPRECT vscroll_rect,
                            LPRECT size_box_rect) {
    DVLOG(1) << __FUNCTION__ << " not implemented";
    return E_NOTIMPL;
  }

  STDMETHOD(Show)(BOOL show) {
    DVLOG(1) << __FUNCTION__;
    T* t = static_cast<T*>(this);
    HRESULT hr = S_OK;
    if (show) {
      if (!t->m_bUIActive)
        hr = t->ActiveXDocActivate(OLEIVERB_INPLACEACTIVATE);
    } else {
      hr = t->UIActivate(FALSE);
      ::ShowWindow(t->m_hWnd, SW_HIDE);
    }
    return hr;
  }

  STDMETHOD(UIActivate)(BOOL ui_activate) {
    DVLOG(1) << __FUNCTION__;
    T* t = static_cast<T*>(this);
    HRESULT hr = S_OK;
    if (ui_activate) {
      // We must know the client site first
      if (t->m_spInPlaceSite == NULL)
        return E_UNEXPECTED;
      if (!t->m_bUIActive)
        hr = t->ActiveXDocActivate(OLEIVERB_UIACTIVATE);
    } else {
      // Menu integration is still not complete, so do not destroy
      // IE's menus.  If we call InPlaceMenuDestroy here, menu items such
      // as Print etc will be disabled and we will not get calls to QueryStatus
      // for those commands.
      // t->InPlaceMenuDestroy();
      // t->DestroyToolbar();
      hr = t->UIDeactivate();
    }
    return hr;
  }

  STDMETHOD(Open)() {
    DVLOG(1) << __FUNCTION__ << " not implemented";
    return E_NOTIMPL;
  }

  STDMETHOD(CloseView)(DWORD reserved) {
    DVLOG(1) << __FUNCTION__;
    T* t = static_cast<T*>(this);
    t->Show(FALSE);
    t->SetInPlaceSite(NULL);
    return S_OK;
  }

  STDMETHOD(SaveViewState)(LPSTREAM stream) {
    DVLOG(1) << __FUNCTION__ << " not implemented";
    return E_NOTIMPL;
  }

  STDMETHOD(ApplyViewState)(LPSTREAM stream) {
    DVLOG(1) << __FUNCTION__ << " not implemented";
    return E_NOTIMPL;
  }

  STDMETHOD(Clone)(IOleInPlaceSite* new_in_place_site,
                   IOleDocumentView** new_view) {
    DVLOG(1) << __FUNCTION__ << " not implemented";
    return E_NOTIMPL;
  }

  HRESULT ActiveXDocActivate(LONG verb) {
    return E_NOTIMPL;
  }
};

#endif  // CHROME_FRAME_OLE_DOCUMENT_IMPL_H_