2 * FreeRDP: A Remote Desktop Protocol Client
3 * GDI Device Context Functions
5 * Copyright 2010-2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 /* Device Context Functions: http://msdn.microsoft.com/en-us/library/dd183554 */
25 #include <freerdp/freerdp.h>
26 #include <freerdp/gdi/gdi.h>
27 #include <freerdp/codec/color.h>
29 #include <freerdp/gdi/region.h>
31 #include <freerdp/gdi/dc.h>
34 * Get the current device context (a new one is created each time).\n
36 * @return current device context
41 HGDI_DC hDC = (HGDI_DC) malloc(sizeof(GDI_DC));
42 hDC->bytesPerPixel = 4;
43 hDC->bitsPerPixel = 32;
44 hDC->drawMode = GDI_R2_BLACK;
45 hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0);
52 * Create a device context.\n
54 * @return new device context
57 HGDI_DC gdi_CreateDC(HCLRCONV clrconv, int bpp)
59 HGDI_DC hDC = (HGDI_DC) malloc(sizeof(GDI_DC));
61 hDC->drawMode = GDI_R2_BLACK;
62 hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0);
66 hDC->bitsPerPixel = bpp;
67 hDC->bytesPerPixel = bpp / 8;
69 hDC->alpha = clrconv->alpha;
70 hDC->invert = clrconv->invert;
71 hDC->rgb555 = clrconv->rgb555;
73 hDC->hwnd = (HGDI_WND) malloc(sizeof(GDI_WND));
74 hDC->hwnd->invalid = gdi_CreateRectRgn(0, 0, 0, 0);
75 hDC->hwnd->invalid->null = 1;
77 hDC->hwnd->count = 32;
78 hDC->hwnd->cinvalid = (HGDI_RGN) malloc(sizeof(GDI_RGN) * hDC->hwnd->count);
79 hDC->hwnd->ninvalid = 0;
85 * Create a new device context compatible with the given device context.\n
87 * @param hdc device context
88 * @return new compatible device context
91 HGDI_DC gdi_CreateCompatibleDC(HGDI_DC hdc)
93 HGDI_DC hDC = (HGDI_DC) malloc(sizeof(GDI_DC));
94 hDC->bytesPerPixel = hdc->bytesPerPixel;
95 hDC->bitsPerPixel = hdc->bitsPerPixel;
96 hDC->drawMode = hdc->drawMode;
97 hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0);
100 hDC->alpha = hdc->alpha;
101 hDC->invert = hdc->invert;
102 hDC->rgb555 = hdc->rgb555;
107 * Select a GDI object in the current device context.\n
109 * @param hdc device context
110 * @param hgdiobject new selected GDI object
111 * @return previous selected GDI object
114 HGDIOBJECT gdi_SelectObject(HGDI_DC hdc, HGDIOBJECT hgdiobject)
116 HGDIOBJECT previousSelectedObject = hdc->selectedObject;
118 if (hgdiobject == NULL)
121 if (hgdiobject->objectType == GDIOBJECT_BITMAP)
123 hdc->selectedObject = hgdiobject;
125 else if (hgdiobject->objectType == GDIOBJECT_PEN)
127 previousSelectedObject = (HGDIOBJECT) hdc->pen;
128 hdc->pen = (HGDI_PEN) hgdiobject;
130 else if (hgdiobject->objectType == GDIOBJECT_BRUSH)
132 previousSelectedObject = (HGDIOBJECT) hdc->brush;
133 hdc->brush = (HGDI_BRUSH) hgdiobject;
135 else if (hgdiobject->objectType == GDIOBJECT_REGION)
137 hdc->selectedObject = hgdiobject;
139 else if (hgdiobject->objectType == GDIOBJECT_RECT)
141 hdc->selectedObject = hgdiobject;
145 /* Unknown GDI Object Type */
149 return previousSelectedObject;
153 * Delete a GDI object.\n
155 * @param hgdiobject GDI object
156 * @return 1 if successful, 0 otherwise
159 int gdi_DeleteObject(HGDIOBJECT hgdiobject)
161 if (hgdiobject == NULL)
164 if (hgdiobject->objectType == GDIOBJECT_BITMAP)
166 HGDI_BITMAP hBitmap = (HGDI_BITMAP) hgdiobject;
168 if (hBitmap->data != NULL)
173 else if (hgdiobject->objectType == GDIOBJECT_PEN)
175 HGDI_PEN hPen = (HGDI_PEN) hgdiobject;
178 else if (hgdiobject->objectType == GDIOBJECT_BRUSH)
180 HGDI_BRUSH hBrush = (HGDI_BRUSH) hgdiobject;
182 if(hBrush->style == GDI_BS_PATTERN)
184 if (hBrush->pattern != NULL)
185 gdi_DeleteObject((HGDIOBJECT) hBrush->pattern);
190 else if (hgdiobject->objectType == GDIOBJECT_REGION)
194 else if (hgdiobject->objectType == GDIOBJECT_RECT)
200 /* Unknown GDI Object Type */
209 * Delete device context.\n
211 * @param hdc device context
212 * @return 1 if successful, 0 otherwise
215 int gdi_DeleteDC(HGDI_DC hdc)
219 if (hdc->hwnd->cinvalid != NULL)
220 free(hdc->hwnd->cinvalid);
222 if (hdc->hwnd->invalid != NULL)
223 free(hdc->hwnd->invalid);