2 * Openwide -- control Windows common dialog
4 * Copyright (c) 2000 Luke Hudson
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.
31 #include "openwidedll.h"
32 #include "openwideres.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
)
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
)
49 SetRect(&r
, 0,0,10,10);
50 MapDialogRect(hwnd
, &r
);
51 return (bHorz
) ? (pix
/ r
.right
) : (pix
/ r
.bottom
);
55 * Function source : C:\Code\lcc\Proto\util.c */
56 void Error(char *szError
, ...)
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
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
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
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
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
109 for(i
=0; pszFilter
[i
]!=0; i
++)
112 char *sp
= strchr(&pszFilter
[i
], '.');
113 if(sp
) ofn
.lpstrDefExt
= sp
+1;
115 if(!GetSaveFileName(&ofn
))
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
))
133 while (PeekMessage(&msg, hwOwner, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE));*/
139 TCHAR
*lbGetItemText(HWND hwLB
, int iItem
)
141 TCHAR
*szText
= NULL
;
143 len
= SendMessage(hwLB
, LB_GETTEXTLEN
, iItem
, 0) + 1;
146 szText
= malloc(len
+1 * sizeof(char));
147 if(!szText
) return NULL
;
148 len
= SendMessage(hwLB
, LB_GETTEXT
, iItem
, (LPARAM
)szText
);
155 static int AddRem_TrayIcon(HICON hIcon
, char *szTip
, HWND hwnd
, UINT uMsg
, DWORD dwState
, DWORD dwMode
){
159 memset(&nid
, 0, sizeof(NOTIFYICONDATA
));
160 nid
.cbSize
= sizeof(NOTIFYICONDATA
);
162 nid
.uFlags
= NIF_MESSAGE
| (hIcon
? NIF_ICON
: 0) | (szTip
? NIF_TIP
: 0);
164 nid
.uCallbackMessage
= uMsg
;
168 strncpy(nid
.szTip
, szTip
, 63);
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};
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
)
214 if (GetFileAttributes (path
) == 0xFFFFFFFF)
216 errCode
= GetLastError ();
217 if (errCode
== ERROR_FILE_NOT_FOUND
218 || errCode
== ERROR_PATH_NOT_FOUND
)
220 //dbg ("fileExists? getLastError gives %d", errCode);
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.
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
237 HRESULT
CreateLink(LPCSTR lpszPathObj
, LPCSTR lpszPathLink
, LPCSTR lpszDesc
)
242 // Get a pointer to the IShellLink interface.
243 hres
= CoCreateInstance(&CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
,
244 &IID_IShellLink
, (LPVOID
*)&psl
);
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
);
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
);
274 int cbAddString(HWND hwCB
, const char *szStr
, LPARAM lpData
)
276 int idx
= SendMessage(hwCB
, CB_ADDSTRING
, 0, (LPARAM
)szStr
);
279 int res
= SendMessage(hwCB
, CB_SETITEMDATA
, idx
, lpData
);
282 SendMessage(hwCB
, CB_DELETESTRING
, idx
, 0);
289 int getDlgItemRect(HWND hwnd
, UINT uID
, LPRECT pr
)
291 if(!IsWindow(hwnd
) || !pr
)
293 HWND hwCtl
= GetDlgItem(hwnd
, uID
);
296 if(!GetWindowRect(hwCtl
, pr
))
298 MapWindowPoints(NULL
, hwnd
, (LPPOINT
)&pr
->left
, 2);
304 BOOL
waitForMutex(void)
309 ghMutex
= OpenMutex(SYNCHRONIZE
, FALSE
, OW_MUTEX_NAME
);
313 dwRes
= WaitForSingleObject(ghMutex
, INFINITE
);
321 //dbg("Mutex wait failed");
328 void releaseMutex(void)
332 ReleaseMutex(ghMutex
);