Removed Pro2 comments
[openwide.git] / owUtil.c
blobcc6397bbaca5653aac709f9df29b230d4af303bc
1 /*
2 * Openwide -- control Windows common dialog
3 *
4 * Copyright (c) 2000 Luke Hudson
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <windows.h>
24 #include <objbase.h>
25 #include <shobjidl.h>
26 #include <shlguid.h>
27 #include <shlobj.h>
28 #include <shlwapi.h>
29 #include <shellapi.h>
30 #include <stdio.h>
31 #include "openwidedll.h"
32 #include "openwideres.h"
33 #include "owutil.h"
34 #include "owSharedUtil.h"
37 /* Attempt to convert from dialog units (measures used in .rc files) to pixels onscreen */
38 int dlgUnits2Pix(HWND hwnd, int units, BOOL bHorz)
40 RECT r;
41 SetRect(&r, 0,0,units,units);
42 MapDialogRect(hwnd, &r);
43 return (bHorz) ? r.right : r.bottom;
46 int pix2DlgUnits(HWND hwnd, int pix, BOOL bHorz)
48 RECT r;
49 SetRect(&r, 0,0,10,10);
50 MapDialogRect(hwnd, &r);
51 return (bHorz) ? (pix / r.right) : (pix / r.bottom);
54 /* Copied from Pro2
55 * Function source : C:\Code\lcc\Proto\util.c */
56 void Error(char *szError, ...)
58 char szBuff[256];
59 va_list vl;
60 va_start(vl, szError);
61 vsnprintf(szBuff, 256, szError, vl); // print error message to string
62 OutputDebugString(szBuff);
63 MessageBox(NULL, szBuff, "Error", MB_OK); // show message
64 va_end(vl);
65 exit(-1);
68 UINT APIENTRY OFNHookProcOldStyle(
69 HWND hdlg, // handle to the dialog box window
70 UINT uiMsg, // message identifier
71 WPARAM wParam, // message parameter
72 LPARAM lParam // message parameter
75 return FALSE;
81 char *Prompt_File_Name(int iFlags, HWND hwOwner, const char *pszFilter, const char *pszTitle)
83 static char szFileName[MAX_PATH]; // store selected file
84 char szDefFilter[] = "All Files\0*.*\0\0"; // default filter
85 OPENFILENAME ofn;
87 ZeroMemory(&ofn, sizeof(ofn));
88 ofn.lStructSize = sizeof(ofn);
90 szFileName[0] = 0; // mark the file name buffer as empty;
92 ofn.hwndOwner = hwOwner;
93 // If a filter is supplied, use it. Otherwise, use default.
94 ofn.lpstrFilter = (pszFilter) ? pszFilter : szDefFilter;
95 ofn.lpstrFile = szFileName; // where to store filename
96 ofn.nMaxFile = MAX_PATH; // length of filename buffer
97 ofn.lpstrTitle = pszTitle; // title if supplied
99 if(iFlags == 1)
101 ofn.Flags = OFN_EXPLORER // use new features
102 | OFN_CREATEPROMPT // create files if user wishes
103 | OFN_OVERWRITEPROMPT
104 | OFN_PATHMUSTEXIST // what it says
105 | OFN_HIDEREADONLY; // hide the read-only option
106 if(pszFilter)
108 int i;
109 for(i=0; pszFilter[i]!=0; i++)
111 i++;
112 char *sp= strchr(&pszFilter[i], '.');
113 if(sp) ofn.lpstrDefExt = sp+1;
115 if(!GetSaveFileName(&ofn))
116 return NULL;
117 return szFileName;
119 else
121 // ofn.lpfnHook = OFNHookProcOldStyle;
122 // ofn.Flags = OFN_ENABLEHOOK;
123 ofn.Flags = OFN_EXPLORER // use new features
124 | OFN_FILEMUSTEXIST // file names must be valid
125 | OFN_PATHMUSTEXIST // and paths too.
126 | OFN_HIDEREADONLY; // hide the read-only option
127 // dbg("Address of OFN data is %p, structsize is %lu", &ofn, ofn.lStructSize);
128 if(!GetOpenFileName(&ofn))
129 return NULL;
130 return szFileName;
132 /* MSG msg;
133 while (PeekMessage(&msg, hwOwner, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE));*/
139 TCHAR *lbGetItemText(HWND hwLB, int iItem)
141 TCHAR *szText = NULL;
142 int len;
143 len = SendMessage(hwLB, LB_GETTEXTLEN, iItem, 0) + 1;
144 if(len <= 1)
145 return NULL;
146 szText = malloc(len+1 * sizeof(char));
147 if(!szText) return NULL;
148 len = SendMessage(hwLB, LB_GETTEXT, iItem, (LPARAM)szText);
149 if( len <= 0 )
150 free(szText);
151 return szText;
155 static int AddRem_TrayIcon(HICON hIcon, char *szTip, HWND hwnd, UINT uMsg, DWORD dwState, DWORD dwMode){
157 NOTIFYICONDATA nid;
159 memset(&nid, 0, sizeof(NOTIFYICONDATA));
160 nid.cbSize = sizeof(NOTIFYICONDATA);
161 nid.hWnd = hwnd;
162 nid.uFlags = NIF_MESSAGE | (hIcon ? NIF_ICON : 0) | (szTip ? NIF_TIP : 0);
163 nid.hIcon = hIcon;
164 nid.uCallbackMessage = uMsg;
166 if( szTip )
168 strncpy(nid.szTip, szTip, 63);
169 nid.szTip[63] = 0;
172 //nid.dwState = dwState;
173 return (Shell_NotifyIcon(dwMode, &nid) != 0);
178 int Add_TrayIcon(HICON hIcon, char *szTip, HWND hwnd, UINT uMsg, DWORD dwState){
179 return AddRem_TrayIcon(hIcon, szTip, hwnd, uMsg, dwState, NIM_ADD);
183 void EndTrayOperation(void)
185 NOTIFYICONDATA nid = {0};
186 nid.cbSize = sizeof(NOTIFYICONDATA);
187 Shell_NotifyIcon(NIM_SETFOCUS, &nid);
191 int Rem_TrayIcon(HWND hwnd, UINT uMsg, DWORD dwState){
192 return AddRem_TrayIcon(NULL, NULL, hwnd, uMsg, dwState, NIM_DELETE);
196 BOOL delFile(HWND hwnd, const char *szFile)
198 SHFILEOPSTRUCT shlOp = {0};
199 shlOp.hwnd = hwnd;
200 shlOp.wFunc = FO_DELETE;
201 shlOp.pFrom = szFile;
202 shlOp.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI | FOF_NO_CONNECTED_ELEMENTS | FOF_NORECURSION;
203 return (SHFileOperation(&shlOp) == S_OK);
210 BOOL fileExists (const char *path)
212 int errCode;
214 if (GetFileAttributes (path) == 0xFFFFFFFF)
216 errCode = GetLastError ();
217 if (errCode == ERROR_FILE_NOT_FOUND
218 || errCode == ERROR_PATH_NOT_FOUND)
219 return FALSE;
220 //dbg ("fileExists? getLastError gives %d", errCode);
222 return TRUE;
225 // CreateLink - uses the Shell's IShellLink and IPersistFile interfaces
226 // to create and store a shortcut to the specified object.
228 // Returns the result of calling the member functions of the interfaces.
230 // Parameters:
231 // lpszPathObj - address of a buffer containing the path of the object.
232 // lpszPathLink - address of a buffer containing the path where the
233 // Shell link is to be stored.
234 // lpszDesc - address of a buffer containing the description of the
235 // Shell link.
237 HRESULT CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc)
239 HRESULT hres;
240 IShellLink* psl;
242 // Get a pointer to the IShellLink interface.
243 hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
244 &IID_IShellLink, (LPVOID*)&psl);
245 if (SUCCEEDED(hres))
247 IPersistFile* ppf;
249 // Set the path to the shortcut target and add the description.
250 psl->lpVtbl->SetPath(psl, lpszPathObj);
251 psl->lpVtbl->SetDescription(psl, lpszDesc);
253 // Query IShellLink for the IPersistFile interface for saving the
254 // shortcut in persistent storage.
255 hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
257 if (SUCCEEDED(hres))
259 WCHAR wsz[MAX_PATH];
261 // Ensure that the string is Unicode.
262 MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
264 // TODO: Check return value from MultiByteWideChar to ensure success.
265 // Save the link by calling IPersistFile::Save.
266 hres = ppf->lpVtbl->Save(ppf, wsz, TRUE);
267 ppf->lpVtbl->Release(ppf);
269 psl->lpVtbl->Release(psl);
271 return hres;
274 int cbAddString(HWND hwCB, const char *szStr, LPARAM lpData)
276 int idx = SendMessage(hwCB, CB_ADDSTRING, 0, (LPARAM)szStr);
277 if( idx != CB_ERR )
279 int res = SendMessage(hwCB, CB_SETITEMDATA, idx, lpData);
280 if( res == CB_ERR )
282 SendMessage(hwCB, CB_DELETESTRING, idx, 0);
283 idx = CB_ERR;
286 return idx;
289 int getDlgItemRect(HWND hwnd, UINT uID, LPRECT pr)
291 if(!IsWindow(hwnd) || !pr)
292 return 0;
293 HWND hwCtl = GetDlgItem(hwnd, uID);
294 if(!hwCtl)
295 return 0;
296 if(!GetWindowRect(hwCtl, pr))
297 return 0;
298 MapWindowPoints(NULL, hwnd, (LPPOINT)&pr->left, 2);
299 return 1;
304 BOOL waitForMutex(void)
306 DWORD dwRes;
307 if( !ghMutex )
309 ghMutex = OpenMutex(SYNCHRONIZE, FALSE, OW_MUTEX_NAME);
311 if( ghMutex )
313 dwRes = WaitForSingleObject(ghMutex, INFINITE);
314 switch(dwRes)
316 case WAIT_OBJECT_0:
317 // okay, continue
318 break;
319 case WAIT_ABANDONED:
320 default:
321 //dbg("Mutex wait failed");
322 return FALSE;
325 return TRUE;
328 void releaseMutex(void)
330 if( ghMutex )
332 ReleaseMutex(ghMutex);