-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathowUtil.c
More file actions
340 lines (284 loc) · 8.3 KB
/
Copy pathowUtil.c
File metadata and controls
340 lines (284 loc) · 8.3 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Openwide -- control Windows common dialog
*
* Copyright (c) 2000 Luke Hudson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <windows.h>
/**
* @author Luke Hudson
* @licence GPL2
*/
#include <objbase.h>
#include <shobjidl.h>
#include <shlguid.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <shellapi.h>
#include <stdio.h>
#include "openwidedll.h"
#include "openwideres.h"
#include "owutil.h"
#include "owSharedUtil.h"
/* Attempt to convert from dialog units (measures used in .rc files) to pixels onscreen */
int dlgUnits2Pix(HWND hwnd, int units, BOOL bHorz)
{
RECT r;
SetRect(&r, 0,0,units,units);
MapDialogRect(hwnd, &r);
return (bHorz) ? r.right : r.bottom;
}
int pix2DlgUnits(HWND hwnd, int pix, BOOL bHorz)
{
RECT r;
SetRect(&r, 0,0,10,10);
MapDialogRect(hwnd, &r);
return (bHorz) ? (pix / r.right) : (pix / r.bottom);
}
/* Copied from Pro2
* Function source : C:\Code\lcc\Proto\util.c */
void Error(char *szError, ...)
{
char szBuff[256];
va_list vl;
va_start(vl, szError);
vsnprintf(szBuff, 256, szError, vl); // print error message to string
OutputDebugString(szBuff);
MessageBox(NULL, szBuff, "Error", MB_OK); // show message
va_end(vl);
exit(-1);
}
UINT APIENTRY OFNHookProcOldStyle(
HWND hdlg, // handle to the dialog box window
UINT uiMsg, // message identifier
WPARAM wParam, // message parameter
LPARAM lParam // message parameter
)
{
return FALSE;
}
char *Prompt_File_Name(int iFlags, HWND hwOwner, const char *pszFilter, const char *pszTitle)
{
static char szFileName[MAX_PATH]; // store selected file
char szDefFilter[] = "All Files\0*.*\0\0"; // default filter
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
szFileName[0] = 0; // mark the file name buffer as empty;
ofn.hwndOwner = hwOwner;
// If a filter is supplied, use it. Otherwise, use default.
ofn.lpstrFilter = (pszFilter) ? pszFilter : szDefFilter;
ofn.lpstrFile = szFileName; // where to store filename
ofn.nMaxFile = MAX_PATH; // length of filename buffer
ofn.lpstrTitle = pszTitle; // title if supplied
if(iFlags == 1)
{
ofn.Flags = OFN_EXPLORER // use new features
| OFN_CREATEPROMPT // create files if user wishes
| OFN_OVERWRITEPROMPT
| OFN_PATHMUSTEXIST // what it says
| OFN_HIDEREADONLY; // hide the read-only option
if(pszFilter)
{
int i;
for(i=0; pszFilter[i]!=0; i++)
;
i++;
char *sp= strchr(&pszFilter[i], '.');
if(sp) ofn.lpstrDefExt = sp+1;
}
if(!GetSaveFileName(&ofn))
return NULL;
return szFileName;
}
else
{
// ofn.lpfnHook = OFNHookProcOldStyle;
// ofn.Flags = OFN_ENABLEHOOK;
ofn.Flags = OFN_EXPLORER // use new features
| OFN_FILEMUSTEXIST // file names must be valid
| OFN_PATHMUSTEXIST // and paths too.
| OFN_HIDEREADONLY; // hide the read-only option
// dbg("Address of OFN data is %p, structsize is %lu", &ofn, ofn.lStructSize);
if(!GetOpenFileName(&ofn))
return NULL;
return szFileName;
}
/* MSG msg;
while (PeekMessage(&msg, hwOwner, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE));*/
}
TCHAR *lbGetItemText(HWND hwLB, int iItem)
{
TCHAR *szText = NULL;
int len;
len = SendMessage(hwLB, LB_GETTEXTLEN, iItem, 0) + 1;
if(len <= 1)
return NULL;
szText = malloc(len+1 * sizeof(char));
if(!szText) return NULL;
len = SendMessage(hwLB, LB_GETTEXT, iItem, (LPARAM)szText);
if( len <= 0 )
free(szText);
return szText;
}
static int AddRem_TrayIcon(HICON hIcon, char *szTip, HWND hwnd, UINT uMsg, DWORD dwState, DWORD dwMode){
NOTIFYICONDATA nid;
memset(&nid, 0, sizeof(NOTIFYICONDATA));
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hwnd;
nid.uFlags = NIF_MESSAGE | (hIcon ? NIF_ICON : 0) | (szTip ? NIF_TIP : 0);
nid.hIcon = hIcon;
nid.uCallbackMessage = uMsg;
if( szTip )
{
strncpy(nid.szTip, szTip, 63);
nid.szTip[63] = 0;
}
//nid.dwState = dwState;
return (Shell_NotifyIcon(dwMode, &nid) != 0);
}
int Add_TrayIcon(HICON hIcon, char *szTip, HWND hwnd, UINT uMsg, DWORD dwState){
return AddRem_TrayIcon(hIcon, szTip, hwnd, uMsg, dwState, NIM_ADD);
}
void EndTrayOperation(void)
{
NOTIFYICONDATA nid = {0};
nid.cbSize = sizeof(NOTIFYICONDATA);
Shell_NotifyIcon(NIM_SETFOCUS, &nid);
}
int Rem_TrayIcon(HWND hwnd, UINT uMsg, DWORD dwState){
return AddRem_TrayIcon(NULL, NULL, hwnd, uMsg, dwState, NIM_DELETE);
}
BOOL delFile(HWND hwnd, const char *szFile)
{
SHFILEOPSTRUCT shlOp = {0};
shlOp.hwnd = hwnd;
shlOp.wFunc = FO_DELETE;
shlOp.pFrom = szFile;
shlOp.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI | FOF_NO_CONNECTED_ELEMENTS | FOF_NORECURSION;
return (SHFileOperation(&shlOp) == S_OK);
}
BOOL fileExists (const char *path)
{
int errCode;
if (GetFileAttributes (path) == 0xFFFFFFFF)
{
errCode = GetLastError ();
if (errCode == ERROR_FILE_NOT_FOUND
|| errCode == ERROR_PATH_NOT_FOUND)
return FALSE;
//dbg ("fileExists? getLastError gives %d", errCode);
}
return TRUE;
}
// CreateLink - uses the Shell's IShellLink and IPersistFile interfaces
// to create and store a shortcut to the specified object.
//
// Returns the result of calling the member functions of the interfaces.
//
// Parameters:
// lpszPathObj - address of a buffer containing the path of the object.
// lpszPathLink - address of a buffer containing the path where the
// Shell link is to be stored.
// lpszDesc - address of a buffer containing the description of the
// Shell link.
HRESULT CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc)
{
HRESULT hres;
IShellLink* psl;
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
&IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
// Set the path to the shortcut target and add the description.
psl->lpVtbl->SetPath(psl, lpszPathObj);
psl->lpVtbl->SetDescription(psl, lpszDesc);
// Query IShellLink for the IPersistFile interface for saving the
// shortcut in persistent storage.
hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH];
// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
// TODO: Check return value from MultiByteWideChar to ensure success.
// Save the link by calling IPersistFile::Save.
hres = ppf->lpVtbl->Save(ppf, wsz, TRUE);
ppf->lpVtbl->Release(ppf);
}
psl->lpVtbl->Release(psl);
}
return hres;
}
int cbAddString(HWND hwCB, const char *szStr, LPARAM lpData)
{
int idx = SendMessage(hwCB, CB_ADDSTRING, 0, (LPARAM)szStr);
if( idx != CB_ERR )
{
int res = SendMessage(hwCB, CB_SETITEMDATA, idx, lpData);
if( res == CB_ERR )
{
SendMessage(hwCB, CB_DELETESTRING, idx, 0);
idx = CB_ERR;
}
}
return idx;
}
int getDlgItemRect(HWND hwnd, UINT uID, LPRECT pr)
{
if(!IsWindow(hwnd) || !pr)
return 0;
HWND hwCtl = GetDlgItem(hwnd, uID);
if(!hwCtl)
return 0;
if(!GetWindowRect(hwCtl, pr))
return 0;
MapWindowPoints(NULL, hwnd, (LPPOINT)&pr->left, 2);
return 1;
}
BOOL waitForMutex(void)
{
DWORD dwRes;
if( !ghMutex )
{
ghMutex = OpenMutex(SYNCHRONIZE, FALSE, OW_MUTEX_NAME);
}
if( ghMutex )
{
dwRes = WaitForSingleObject(ghMutex, INFINITE);
switch(dwRes)
{
case WAIT_OBJECT_0:
// okay, continue
break;
case WAIT_ABANDONED:
default:
//dbg("Mutex wait failed");
return FALSE;
}
}
return TRUE;
}
void releaseMutex(void)
{
if( ghMutex )
{
ReleaseMutex(ghMutex);
}
}