Removed +x perm
[openwide.git] / owUtil.c
blob6395ea820d6ab4d26b3e65b775c7e1fbb8be4de8
1 #include <windows.h>
2 /**
3 * @author Luke Hudson
4 * @licence GPL2
5 */
7 #include <objbase.h>
8 #include <shobjidl.h>
9 #include <shlguid.h>
10 #include <shlobj.h>
11 #include <shlwapi.h>
12 #include <shellapi.h>
13 #include <stdio.h>
14 #include "openwidedll.h"
15 #include "openwideres.h"
16 #include "owutil.h"
17 #include "owSharedUtil.h"
19 int dlgUnits2Pix(HWND hwnd, int units, BOOL bHorz)
21 RECT r;
22 SetRect(&r, 0,0,units,units);
23 MapDialogRect(hwnd, &r);
24 return (bHorz) ? r.right : r.bottom;
27 int pix2DlgUnits(HWND hwnd, int pix, BOOL bHorz)
29 RECT r;
30 SetRect(&r, 0,0,10,10);
31 MapDialogRect(hwnd, &r);
32 return (bHorz) ? (pix / r.right) : (pix / r.bottom);
35 /* Copied from Pro2
36 * Function source : C:\Code\lcc\Proto\util.c */
37 void Error(char *szError, ...)
39 char szBuff[256];
40 va_list vl;
41 va_start(vl, szError);
42 _vsnprintf(szBuff, 256, szError, vl); // print error message to string
43 OutputDebugString(szBuff);
44 MessageBox(NULL, szBuff, "Error", MB_OK); // show message
45 va_end(vl);
46 exit(-1);
49 UINT APIENTRY OFNHookProcOldStyle(
50 HWND hdlg, // handle to the dialog box window
51 UINT uiMsg, // message identifier
52 WPARAM wParam, // message parameter
53 LPARAM lParam // message parameter
56 return FALSE;
62 char *Prompt_File_Name(int iFlags, HWND hwOwner, const char *pszFilter, const char *pszTitle)
64 static char szFileName[MAX_PATH]; // store selected file
65 char szDefFilter[] = "All Files\0*.*\0\0"; // default filter
66 OPENFILENAME ofn;
68 ZeroMemory(&ofn, sizeof(ofn));
69 ofn.lStructSize = sizeof(ofn);
71 szFileName[0] = 0; // mark the file name buffer as empty;
73 ofn.hwndOwner = hwOwner;
74 // If a filter is supplied, use it. Otherwise, use default.
75 ofn.lpstrFilter = (pszFilter) ? pszFilter : szDefFilter;
76 ofn.lpstrFile = szFileName; // where to store filename
77 ofn.nMaxFile = MAX_PATH; // length of filename buffer
78 ofn.lpstrTitle = pszTitle; // title if supplied
80 if(iFlags == 1)
82 ofn.Flags = OFN_EXPLORER // use new features
83 | OFN_CREATEPROMPT // create files if user wishes
84 | OFN_OVERWRITEPROMPT
85 | OFN_PATHMUSTEXIST // what it says
86 | OFN_HIDEREADONLY; // hide the read-only option
87 if(pszFilter)
89 int i;
90 for(i=0; pszFilter[i]!=0; i++)
92 i++;
93 char *sp= strchr(&pszFilter[i], '.');
94 if(sp) ofn.lpstrDefExt = sp+1;
96 if(!GetSaveFileName(&ofn))
97 return NULL;
98 return szFileName;
100 else
102 // ofn.lpfnHook = OFNHookProcOldStyle;
103 // ofn.Flags = OFN_ENABLEHOOK;
104 ofn.Flags = OFN_EXPLORER // use new features
105 | OFN_FILEMUSTEXIST // file names must be valid
106 | OFN_PATHMUSTEXIST // and paths too.
107 | OFN_HIDEREADONLY; // hide the read-only option
108 // dbg("Address of OFN data is %p, structsize is %lu", &ofn, ofn.lStructSize);
109 if(!GetOpenFileName(&ofn))
110 return NULL;
111 return szFileName;
113 /* MSG msg;
114 while (PeekMessage(&msg, hwOwner, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE));*/
120 TCHAR *lbGetItemText(HWND hwLB, int iItem)
122 TCHAR *szText = NULL;
123 int len;
124 len = SendMessage(hwLB, LB_GETTEXTLEN, iItem, 0) + 1;
125 if(len <= 1)
126 return NULL;
127 szText = malloc(len+1 * sizeof(char));
128 if(!szText) return NULL;
129 len = SendMessage(hwLB, LB_GETTEXT, iItem, (LPARAM)szText);
130 if( len <= 0 )
131 free(szText);
132 return szText;
136 static int AddRem_TrayIcon(HICON hIcon, char *szTip, HWND hwnd, UINT uMsg, DWORD dwState, DWORD dwMode){
138 NOTIFYICONDATA nid;
140 memset(&nid, 0, sizeof(NOTIFYICONDATA));
141 nid.cbSize = sizeof(NOTIFYICONDATA);
142 nid.hWnd = hwnd;
143 nid.uFlags = NIF_MESSAGE | (hIcon ? NIF_ICON : 0) | (szTip ? NIF_TIP : 0);
144 nid.hIcon = hIcon;
145 nid.uCallbackMessage = uMsg;
147 if( szTip )
149 strncpy(nid.szTip, szTip, 63);
150 nid.szTip[63] = 0;
153 //nid.dwState = dwState;
154 return (Shell_NotifyIcon(dwMode, &nid) != 0);
159 int Add_TrayIcon(HICON hIcon, char *szTip, HWND hwnd, UINT uMsg, DWORD dwState){
160 return AddRem_TrayIcon(hIcon, szTip, hwnd, uMsg, dwState, NIM_ADD);
164 void EndTrayOperation(void)
166 NOTIFYICONDATA nid = {0};
167 nid.cbSize = sizeof(NOTIFYICONDATA);
168 Shell_NotifyIcon(NIM_SETFOCUS, &nid);
172 int Rem_TrayIcon(HWND hwnd, UINT uMsg, DWORD dwState){
173 return AddRem_TrayIcon(NULL, NULL, hwnd, uMsg, dwState, NIM_DELETE);
177 BOOL delFile(HWND hwnd, const char *szFile)
179 SHFILEOPSTRUCT shlOp = {0};
180 shlOp.hwnd = hwnd;
181 shlOp.wFunc = FO_DELETE;
182 shlOp.pFrom = szFile;
183 shlOp.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI | FOF_NO_CONNECTED_ELEMENTS | FOF_NORECURSION;
184 return (SHFileOperation(&shlOp) == S_OK);
191 BOOL fileExists (const char *path)
193 int errCode;
195 if (GetFileAttributes (path) == 0xFFFFFFFF)
197 errCode = GetLastError ();
198 if (errCode == ERROR_FILE_NOT_FOUND
199 || errCode == ERROR_PATH_NOT_FOUND)
200 return FALSE;
201 //dbg ("fileExists? getLastError gives %d", errCode);
203 return TRUE;
206 // CreateLink - uses the Shell's IShellLink and IPersistFile interfaces
207 // to create and store a shortcut to the specified object.
209 // Returns the result of calling the member functions of the interfaces.
211 // Parameters:
212 // lpszPathObj - address of a buffer containing the path of the object.
213 // lpszPathLink - address of a buffer containing the path where the
214 // Shell link is to be stored.
215 // lpszDesc - address of a buffer containing the description of the
216 // Shell link.
218 HRESULT CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc)
220 HRESULT hres;
221 IShellLink* psl;
223 // Get a pointer to the IShellLink interface.
224 hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
225 &IID_IShellLink, (LPVOID*)&psl);
226 if (SUCCEEDED(hres))
228 IPersistFile* ppf;
230 // Set the path to the shortcut target and add the description.
231 psl->lpVtbl->SetPath(psl, lpszPathObj);
232 psl->lpVtbl->SetDescription(psl, lpszDesc);
234 // Query IShellLink for the IPersistFile interface for saving the
235 // shortcut in persistent storage.
236 hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
238 if (SUCCEEDED(hres))
240 WCHAR wsz[MAX_PATH];
242 // Ensure that the string is Unicode.
243 MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
245 // TODO: Check return value from MultiByteWideChar to ensure success.
246 // Save the link by calling IPersistFile::Save.
247 hres = ppf->lpVtbl->Save(ppf, wsz, TRUE);
248 ppf->lpVtbl->Release(ppf);
250 psl->lpVtbl->Release(psl);
252 return hres;
255 int cbAddString(HWND hwCB, const char *szStr, LPARAM lpData)
257 int idx = SendMessage(hwCB, CB_ADDSTRING, 0, (LPARAM)szStr);
258 if( idx != CB_ERR )
260 int res = SendMessage(hwCB, CB_SETITEMDATA, idx, lpData);
261 if( res == CB_ERR )
263 SendMessage(hwCB, CB_DELETESTRING, idx, 0);
264 idx = CB_ERR;
267 return idx;
270 int getDlgItemRect(HWND hwnd, UINT uID, LPRECT pr)
272 if(!IsWindow(hwnd) || !pr)
273 return 0;
274 HWND hwCtl = GetDlgItem(hwnd, uID);
275 if(!hwCtl)
276 return 0;
277 if(!GetWindowRect(hwCtl, pr))
278 return 0;
279 MapWindowPoints(NULL, hwnd, (LPPOINT)&pr->left, 2);
280 return 1;
285 BOOL waitForMutex(void)
287 DWORD dwRes;
288 if( !ghMutex )
290 ghMutex = OpenMutex(SYNCHRONIZE, FALSE, OW_MUTEX_NAME);
292 if( ghMutex )
294 dwRes = WaitForSingleObject(ghMutex, INFINITE);
295 switch(dwRes)
297 case WAIT_OBJECT_0:
298 // okay, continue
299 break;
300 case WAIT_ABANDONED:
301 default:
302 //dbg("Mutex wait failed");
303 return FALSE;
306 return TRUE;
309 void releaseMutex(void)
311 if( ghMutex )
313 ReleaseMutex(ghMutex);