summaryrefslogtreecommitdiff
path: root/iup/srcole/iup_olecontrol.cpp
blob: 20431c5244fcc71eb7db64af750cea2347a44a66 (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
/*
 * iupolectl.cpp
 *
 *   CPI que implementa um container OLE
 */

#include "tOleHandler.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "iup.h"
#include "iupole.h"

#include "iup_object.h"
#include "iup_register.h"
#include "iup_attrib.h"
#include "iup_stdcontrols.h"
#include "iup_str.h"
#include "iup_layout.h"


struct _IcontrolData
{
  iupCanvas canvas;  /* from IupCanvas (must reserve it) */

  tOleHandler* olehandler;
};

static char* iOleControlGetDesignModeAttrib(Ihandle* ih)
{
  if (ih->data->olehandler->m_ambientProp.getDesignMode())
    return "YES";
  else
    return "NO";
}

static int iOleControlSetDesignModeAttrib(Ihandle* ih, const char* value)
{
  if (iupStrBoolean(value))
    ih->data->olehandler->m_ambientProp.setDesignMode(true, true);
  else
    ih->data->olehandler->m_ambientProp.setDesignMode(false, true);
  return 1;
}

static int iOleControlSetDesignModeDontNotifyAttrib(Ihandle* ih, const char* value)
{
  if (iupStrBoolean(value))
    ih->data->olehandler->m_ambientProp.setDesignMode(true, false);
  else
    ih->data->olehandler->m_ambientProp.setDesignMode(false, false);
  return 1;
}

static char* iOleControlGetIUnknownAttrib(Ihandle* ih)
{
  IUnknown *punk = NULL;
  ih->data->olehandler->ObjectGet(&punk);
  return (char*)punk;
}

static int iOleControlResize_CB(Ihandle *ih)
{
  if (!ih->data->olehandler->m_hWnd)
    return IUP_DEFAULT;

  ih->data->olehandler->OnShow();

  return IUP_DEFAULT;
}

static void iOleControlComputeNaturalSizeMethod(Ihandle* ih, int *w, int *h, int *expand)
{
  long natural_w = 0, natural_h = 0;
  (void)expand; /* unset if not a container */

  ih->data->olehandler->GetNaturalSize(&natural_w, &natural_h);

  *w = natural_w;
  *h = natural_h;
}

static void iOleControlLayoutUpdateMethod(Ihandle* ih)
{
  SIZEL szl;
  szl.cx = ih->currentwidth;
  szl.cy = ih->currentheight;
  ih->data->olehandler->SizeSet(&szl, TRUE, TRUE);
  ih->data->olehandler->UpdateInPlaceObjectRects(NULL, TRUE);
}

static int iOleControlMapMethod(Ihandle* ih)
{
  /* reset the canvas BGCOLOR */
  IupSetAttribute(ih, "BACKGROUND", NULL); 

  ih->data->olehandler->m_hWnd = ih->handle;

  return IUP_NOERROR;
}

static int iOleControlCreateMethod(Ihandle* ih, void **params)
{
  CLSID clsid;

  if (!params || !(params[0]))
    return IUP_ERROR;

  char *progID = (char*)params[0];

  /* free the data alocated by IupCanvas */
  if (ih->data) free(ih->data);
  ih->data = iupALLOCCTRLDATA();

  /* change the IupCanvas default values */
  iupAttribSetStr(ih, "BORDER", "NO");

  /* IupCanvas callbacks */
  IupSetCallback(ih,"RESIZE_CB",(Icallback)iOleControlResize_CB);

  size_t len = strlen(progID)+1;
  wchar_t* wcProgId = (wchar_t*) malloc(len * sizeof(wchar_t));
  mbstowcs(wcProgId, progID, len);
  HRESULT hr = CLSIDFromProgID(wcProgId, &clsid);
  free(wcProgId);
  if(FAILED(hr))
    return IUP_ERROR;

  ih->data->olehandler = new tOleHandler();
  if (ih->data->olehandler->Create(&clsid) == CREATE_FAILED)
    return IUP_ERROR;

  return IUP_NOERROR;
}

static void iOleControlDestroyMethod(Ihandle* ih)
{
  ih->data->olehandler->Close(true);
  delete ih->data->olehandler;
}

static void iOleControlRelease(Iclass* ic)
{
  (void)ic;
  OleUninitialize();
}

static Iclass* iOleControlGetClass(void)
{
  Iclass* ic = iupClassNew(iupCanvasGetClass());

  ic->name = "olecontrol";
  ic->format = "s"; /* one string */
  ic->nativetype = IUP_TYPECANVAS;
  ic->childtype = IUP_CHILDNONE;
  ic->is_interactive = 1;

  /* Class functions */
  ic->Create = iOleControlCreateMethod;
  ic->Destroy = iOleControlDestroyMethod;
  ic->Release = iOleControlRelease;
  ic->Map = iOleControlMapMethod;
  ic->LayoutUpdate = iOleControlLayoutUpdateMethod;
  ic->ComputeNaturalSize = iOleControlComputeNaturalSizeMethod;

  iupClassRegisterAttribute(ic, "DESIGNMODE", iOleControlGetDesignModeAttrib, iOleControlSetDesignModeAttrib, NULL, NULL, IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "DESIGNMODE_DONT_NOTIFY", iOleControlGetDesignModeAttrib, iOleControlSetDesignModeDontNotifyAttrib, NULL, NULL, IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "IUNKNOWN", iOleControlGetIUnknownAttrib, NULL, NULL, NULL, IUPAF_READONLY|IUPAF_NOT_MAPPED|IUPAF_NO_INHERIT|IUPAF_NO_STRING);

  /* Overwrite the canvas implementation */
  iupClassRegisterAttribute(ic, "BGCOLOR", NULL, NULL, NULL, NULL, IUPAF_NOT_MAPPED);

  return ic;
}

Ihandle *IupOleControl(const char *ProgID)
{
  void *params[2];
  params[0] = (void*)ProgID;
  params[1] = NULL;
  return IupCreatev("olecontrol", params);
}

int IupOleControlOpen(void)
{
  if (IupGetGlobal("_IUP_OLECONTROL_OPEN"))
    return IUP_OPENED;

  HRESULT retval = OleInitialize(NULL);
  if (retval != S_OK && retval != S_FALSE)
    return IUP_ERROR;

  iupRegisterClass(iOleControlGetClass());

  IupSetGlobal("_IUP_OLECONTROL_OPEN", "1");
  return IUP_NOERROR;
}