summaryrefslogtreecommitdiff
path: root/cd/src/win32/cdwnative.c
blob: 3534c6e30e6f272a4f17c92799c04f96649f8250 (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
/** \file
 * \brief Windows Native Window Driver
 *
 * See Copyright Notice in cd.h
 */

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

#include "cdwin.h"
#include "cdnative.h"


int cdGetScreenColorPlanes(void)
{
  int bpp;
  HDC ScreenDC = GetDC(NULL);
  bpp = GetDeviceCaps(ScreenDC, BITSPIXEL);
  ReleaseDC(NULL, ScreenDC);
  return bpp;
}

void cdGetScreenSize(int *width, int *height, double *width_mm, double *height_mm)
{
  HDC ScreenDC = GetDC(NULL);
  if (width) *width = GetDeviceCaps(ScreenDC, HORZRES);
  if (height) *height = GetDeviceCaps(ScreenDC, VERTRES);
  if (width_mm) *width_mm = ((GetDeviceCaps(ScreenDC, HORZRES) * 25.4) / GetDeviceCaps(ScreenDC, LOGPIXELSX));
  if (height_mm) *height_mm = ((GetDeviceCaps(ScreenDC, VERTRES) * 25.4) / GetDeviceCaps(ScreenDC, LOGPIXELSY));
  ReleaseDC(NULL, ScreenDC);
}

static void cdwReleaseDC(cdCtxCanvas *ctxcanvas)
{
  SelectObject(ctxcanvas->hDC, ctxcanvas->hOldBrush);     
  SelectObject(ctxcanvas->hDC, ctxcanvas->hOldPen);
  SelectObject(ctxcanvas->hDC, ctxcanvas->hOldFont);
  ReleaseDC(ctxcanvas->hWnd, ctxcanvas->hDC);
  ctxcanvas->hDC = NULL;
}

static int cdactivate(cdCtxCanvas *ctxcanvas)
{
  if (ctxcanvas->hWnd)
  {
    RECT rect;
    GetClientRect(ctxcanvas->hWnd, &rect);
    ctxcanvas->canvas->w = rect.right - rect.left;
    ctxcanvas->canvas->h = rect.bottom - rect.top;
  
    ctxcanvas->canvas->w_mm = ((double)ctxcanvas->canvas->w) / ctxcanvas->canvas->xres;
    ctxcanvas->canvas->h_mm = ((double)ctxcanvas->canvas->h) / ctxcanvas->canvas->yres;
  
    ctxcanvas->canvas->bpp = cdGetScreenColorPlanes();
  }
  
  /* Se nao e' ownwer, tem que restaurar o contexto */
  if (!ctxcanvas->isOwnedDC)
  {
    if (ctxcanvas->hDC) /* deactivate not called */
      cdwReleaseDC(ctxcanvas);

    ctxcanvas->hDC = GetDC(ctxcanvas->hWnd);
    cdwRestoreDC(ctxcanvas);
  }

  if (ctxcanvas->canvas->use_matrix)
    ctxcanvas->canvas->cxTransform(ctxcanvas, ctxcanvas->canvas->matrix);

  return CD_OK;
}

static void cddeactivate(cdCtxCanvas *ctxcanvas)
{
  /* Se nao e' ownwer, tem que liberar o contexto */
  if (!ctxcanvas->isOwnedDC && ctxcanvas->hDC)
    cdwReleaseDC(ctxcanvas);
}

static void cdkillcanvas(cdCtxCanvas *ctxcanvas)
{
  /* se nao e' owner e nao esta' ativo, simula ativacao */
  if (!ctxcanvas->isOwnedDC && !ctxcanvas->hDC)
  {
    ctxcanvas->hDC = GetDC(ctxcanvas->hWnd);
    cdwRestoreDC(ctxcanvas);
  }
  
  cdwKillCanvas(ctxcanvas);

  if (ctxcanvas->release_dc)
    ReleaseDC(ctxcanvas->hWnd, ctxcanvas->hDC);
  
  memset(ctxcanvas, 0, sizeof(cdCtxCanvas));
  free(ctxcanvas);
}

static void cdcreatecanvas(cdCanvas* canvas, void *data)
{
  cdCtxCanvas* ctxcanvas;
  HWND hWnd = NULL;
  HDC hDC, ScreenDC;
  int release_dc = 0;

  ScreenDC = GetDC(NULL);
  canvas->bpp = GetDeviceCaps(ScreenDC, BITSPIXEL);
  canvas->xres = (float)(((double)GetDeviceCaps(ScreenDC, LOGPIXELSX)) / 25.4);
  canvas->yres = (float)(((double)GetDeviceCaps(ScreenDC, LOGPIXELSY)) / 25.4);
  ReleaseDC(NULL, ScreenDC);

  if (!data)
  {
    hDC = GetDC(NULL);
    release_dc = 1;
    canvas->w = GetDeviceCaps(hDC, HORZRES);
    canvas->h = GetDeviceCaps(hDC, VERTRES);
  }
  else if (IsWindow((HWND)data)) 
  {
    RECT rect;
    hWnd = (HWND)data;

    hDC = GetDC(hWnd);
    release_dc = 1;
  
    GetClientRect(hWnd, &rect);
    canvas->w = rect.right - rect.left;
    canvas->h = rect.bottom - rect.top;
  }
  else  /* can be a HDC or a string */
  {
    DWORD objtype = GetObjectType((HGDIOBJ)data);
    if (objtype == OBJ_DC || objtype == OBJ_MEMDC || 
        objtype == OBJ_ENHMETADC || objtype == OBJ_METADC)   
    {
      hDC = (HDC)data;
      canvas->w = GetDeviceCaps(hDC, HORZRES);
      canvas->h = GetDeviceCaps(hDC, VERTRES);
    }
    else
    {
      hDC = NULL;
      canvas->w = 0;
      canvas->h = 0;
      sscanf((char*)data,"%p %dx%d", &hDC, &canvas->w, &canvas->h); 

      if (!hDC || !canvas->w || !canvas->h)
        return;
    }
    release_dc = 0;
  }
  
  canvas->w_mm = ((double)canvas->w) / canvas->xres;
  canvas->h_mm = ((double)canvas->h) / canvas->yres;

  /* Inicializa driver WIN32 */
  ctxcanvas = cdwCreateCanvas(canvas, hWnd, hDC, CDW_WIN);
  
  ctxcanvas->release_dc = release_dc;
  ctxcanvas->clip_pnt[2].x = ctxcanvas->clip_pnt[1].x = canvas->w - 1;
  ctxcanvas->clip_pnt[3].y = ctxcanvas->clip_pnt[2].y = canvas->h - 1;

  if (hWnd)
  {
    LONG style = GetClassLong(hWnd, GCL_STYLE);
    ctxcanvas->isOwnedDC = (int) ((style & CS_OWNDC) || (style & CS_CLASSDC));
  }
  else
    ctxcanvas->isOwnedDC = 1;

  /* Se nao e' ownwer, tem que liberar o contexto */
  if (!ctxcanvas->isOwnedDC)
    cdwReleaseDC(ctxcanvas);
}

static void cdinittable(cdCanvas* canvas)
{
  cdwInitTable(canvas);

  canvas->cxKillCanvas = cdkillcanvas;
  canvas->cxActivate = cdactivate;
  canvas->cxDeactivate = cddeactivate;
}

static cdContext cdNativeContext =
{
  CD_CAP_ALL & ~(CD_CAP_PLAY | CD_CAP_YAXIS | CD_CAP_FPRIMTIVES ),
  0,
  cdcreatecanvas,
  cdinittable,
  NULL,              
  NULL,
};

cdContext* cdContextNativeWindow(void)
{
  if (cdUseContextPlus(CD_QUERY))
  {
    cdContext* ctx = cdGetContextPlus(CD_CTX_NATIVEWINDOW);
    if (ctx != NULL)
      return ctx;
  }

  return &cdNativeContext;
}