[TRANSLATIONS] Turkish translation update by Erdem Ersoy. CORE-10513
[reactos.git] / reactos / base / applications / clipbrd / winutils.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Clipboard Viewer
4 * FILE: base/applications/clipbrd/winutils.c
5 * PURPOSE: Miscellaneous helper functions.
6 * PROGRAMMERS: Ricardo Hanke
7 */
8
9 #include "precomp.h"
10
11 void ShowLastWin32Error(HWND hwndParent)
12 {
13 DWORD dwError;
14 LPWSTR lpMsgBuf = NULL;
15
16 dwError = GetLastError();
17 if (dwError == NO_ERROR)
18 return;
19
20 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
21 NULL, dwError, 0, (LPWSTR)&lpMsgBuf, 0, NULL);
22 MessageBoxW(hwndParent, lpMsgBuf, NULL, MB_OK | MB_ICONERROR);
23 LocalFree(lpMsgBuf);
24 }
25
26 void BringWindowToFront(HWND hWnd)
27 {
28 if (IsIconic(hWnd))
29 {
30 ShowWindow(hWnd, SW_RESTORE);
31 SetForegroundWindow(hWnd);
32 }
33 else
34 {
35 SetForegroundWindow(hWnd);
36 }
37 }
38
39 int DrawTextFromResource(HINSTANCE hInstance, UINT uID, HDC hDC, LPRECT lpRect, UINT uFormat)
40 {
41 LPWSTR lpBuffer;
42 int nCount;
43
44 nCount = LoadStringW(hInstance, uID, (LPWSTR)&lpBuffer, 0);
45 if (nCount)
46 {
47 return DrawTextW(hDC, lpBuffer, nCount, lpRect, uFormat);
48 }
49 else
50 {
51 return 0;
52 }
53 }
54
55 int MessageBoxRes(HWND hWnd, HINSTANCE hInstance, UINT uText, UINT uCaption, UINT uType)
56 {
57 MSGBOXPARAMSW mb;
58
59 ZeroMemory(&mb, sizeof(mb));
60 mb.cbSize = sizeof(mb);
61 mb.hwndOwner = hWnd;
62 mb.hInstance = hInstance;
63 mb.lpszText = MAKEINTRESOURCEW(uText);
64 mb.lpszCaption = MAKEINTRESOURCEW(uCaption);
65 mb.dwStyle = uType;
66 mb.dwLanguageId = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
67
68 return MessageBoxIndirectW(&mb);
69 }
70
71 void DrawTextFromClipboard(HDC hDC, LPRECT lpRect, UINT uFormat)
72 {
73 HGLOBAL hGlobal;
74 LPWSTR lpchText;
75
76 if (!OpenClipboard(NULL))
77 return;
78
79 hGlobal = GetClipboardData(CF_UNICODETEXT);
80 if (!hGlobal)
81 return;
82
83 lpchText = GlobalLock(hGlobal);
84 if (!lpchText)
85 return;
86
87 DrawTextW(hDC, lpchText, -1, lpRect, uFormat);
88 GlobalUnlock(hGlobal);
89 CloseClipboard();
90 }
91
92 void BitBltFromClipboard(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, int nXSrc, int nYSrc, DWORD dwRop)
93 {
94 HDC hdcMem;
95 HBITMAP hbm;
96
97 if (!OpenClipboard(NULL))
98 return;
99
100 hdcMem = CreateCompatibleDC(hdcDest);
101 if (hdcMem)
102 {
103 hbm = (HBITMAP)GetClipboardData(CF_BITMAP);
104 SelectObject(hdcMem, hbm);
105 BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcMem, nXSrc, nYSrc, dwRop);
106 DeleteDC(hdcMem);
107 }
108 CloseClipboard();
109 }
110
111 void SetDIBitsToDeviceFromClipboard(UINT uFormat, HDC hdc, int XDest, int YDest, int XSrc, int YSrc, UINT uStartScan, UINT fuColorUse)
112 {
113 LPBITMAPINFOHEADER lpInfoHeader;
114 LPBYTE lpBits;
115 HGLOBAL hGlobal;
116 INT iPalSize;
117
118 if (!OpenClipboard(NULL))
119 return;
120
121 hGlobal = GetClipboardData(uFormat);
122 if (!hGlobal)
123 return;
124
125 lpInfoHeader = GlobalLock(hGlobal);
126 if (!lpInfoHeader)
127 return;
128
129 if (lpInfoHeader->biBitCount < 16)
130 {
131 iPalSize = (1 << lpInfoHeader->biBitCount) * 4;
132 }
133 else
134 {
135 iPalSize = 0;
136 }
137
138 lpBits = (LPBYTE)lpInfoHeader + lpInfoHeader->biSize + iPalSize;
139
140 SetDIBitsToDevice(hdc, XDest, YDest, lpInfoHeader->biWidth, lpInfoHeader->biHeight, XSrc, YSrc, uStartScan, lpInfoHeader->biHeight, lpBits, (LPBITMAPINFO)lpInfoHeader, fuColorUse);
141
142 GlobalUnlock(hGlobal);
143 CloseClipboard();
144 }
145
146 void PlayMetaFileFromClipboard(HDC hdc, const RECT *lpRect)
147 {
148 LPMETAFILEPICT mp;
149 HGLOBAL hGlobal;
150
151 if (!OpenClipboard(NULL))
152 return;
153
154 hGlobal = GetClipboardData(CF_METAFILEPICT);
155 if (!hGlobal)
156 return;
157
158 mp = (LPMETAFILEPICT)GlobalLock(hGlobal);
159 if (!mp)
160 return;
161
162 SetMapMode(hdc, mp->mm);
163 SetViewportExtEx(hdc, lpRect->right, lpRect->bottom, NULL);
164 SetViewportOrgEx(hdc, lpRect->left, lpRect->top, NULL);
165 PlayMetaFile(hdc, mp->hMF);
166 GlobalUnlock(hGlobal);
167 CloseClipboard();
168 }
169
170 void PlayEnhMetaFileFromClipboard(HDC hdc, const RECT *lpRect)
171 {
172 HENHMETAFILE hEmf;
173
174 if (!OpenClipboard(NULL))
175 return;
176
177 hEmf = GetClipboardData(CF_ENHMETAFILE);
178 PlayEnhMetaFile(hdc, hEmf, lpRect);
179 CloseClipboard();
180 }