Release/: Updated readme.txt and added gpl-2.0.txt
[openwide.git] / owDLLUtil.c
blob14f0bc3a7629f922a05ab5563a28e49ae64e145a
1 #include <windows.h>
2 /**
3 * @author Luke Hudson
4 * @licence GPL2
5 */
7 #include <commctrl.h>
8 #include <shlwapi.h>
9 #include <shellapi.h>
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <dlgs.h>
13 #include "openwidedll.h"
14 #include "openwideres.h"
15 #include "owDllInc.h"
20 int focusDlgItem(HWND hwnd, int iFocus)
22 UINT uID = focusToCtlID(gOwShared.iFocus);
23 if( uID == CID_DIRLIST )
25 return SetFocus( GetDlgItem( GetDlgItem(hwnd, CID_DIRLISTPARENT) , uID) ) != NULL;
27 else
28 return SetFocus( GetDlgItem(hwnd, uID)) != NULL;
32 WORD focusToCtlID(int iFocus)
34 //BOOL bXP = isWinXP();
35 switch( iFocus )
37 case F_DIRLIST:
38 return CID_DIRLIST;
39 case F_FNAME:
40 return CID_FNAME;
41 case F_FTYPE:
42 return CID_FTYPE;
43 case F_PLACES:
44 return CID_PLACES;
45 case F_LOOKIN:
46 return CID_LOOKIN;
48 return CID_DIRLIST;
52 HWND getChildWinFromPt(HWND hwnd)
54 POINT pt;
55 GetCursorPos(&pt);
56 ScreenToClient(GetParent(hwnd), &pt);
58 EnableWindow(hwnd, FALSE);
59 HWND hw = ChildWindowFromPointEx(GetParent(hwnd), pt, CWP_SKIPDISABLED | CWP_SKIPINVISIBLE);
60 EnableWindow(hwnd, TRUE);
61 HWND hwSV = GetDlgItem(GetParent(hwnd), CID_DIRLISTPARENT);
62 if( hw == hwSV )
64 ClientToScreen(GetParent(hwnd), &pt);
65 ScreenToClient(hw, &pt);
66 hw = ChildWindowFromPointEx(hw, pt, CWP_SKIPDISABLED | CWP_SKIPINVISIBLE);
68 HWND hwEd = GetDlgItem(GetParent(hwnd), CID_FNAME);
69 if( hw == hwEd )
71 ClientToScreen(GetParent(hwnd), &pt);
72 ScreenToClient(hw, &pt);
73 hw = ChildWindowFromPointEx(hw, pt, CWP_SKIPDISABLED | CWP_SKIPINVISIBLE);
75 return hw;
80 int subclass(HWND hwnd, WNDPROC wpNew, LPARAM lpData)
82 if( GetProp(hwnd, OW_PROP_NAME) != NULL )
83 return 0;
84 POWSubClassData pow = (POWSubClassData)malloc(sizeof(OWSubClassData));
85 if(!pow)
86 return 0;
87 ZeroMemory(pow, sizeof(OWSubClassData));
88 if( !SetProp(hwnd, OW_PROP_NAME, pow) )
90 free(pow);
91 return 0;
93 pow->lpData = lpData;
94 pow->wpOrig = (WNDPROC)SetWindowLong(hwnd, GWL_WNDPROC, (LONG)wpNew);
95 return 1;
99 int unsubclass(HWND hwnd)
101 POWSubClassData pow = (POWSubClassData)GetProp(hwnd, OW_PROP_NAME);
102 if(pow)
104 SetWindowLong(hwnd, GWL_WNDPROC, (LONG)pow->wpOrig);
105 RemoveProp(hwnd, OW_PROP_NAME);
106 free(pow);
107 return 1;
109 return 0;
113 WORD viewToCmdID(int iView)
115 BOOL bXP = isWinXP();
116 switch( iView )
118 case V_DETAILS:
119 return CMD_2K_DETAILS;
120 case V_LIST:
121 return (bXP) ? CMD_XP_LIST : CMD_2K_LIST;
122 case V_SMICONS:
123 return (bXP) ? 0 : CMD_2K_SMICONS;
124 case V_LGICONS:
125 return (bXP) ? CMD_XP_LGICONS : CMD_2K_LGICONS;
126 case V_THUMBS:
127 return (bXP) ? CMD_XP_THUMBS : CMD_2K_THUMBS;
128 case V_TILES:
129 return (bXP) ? CMD_XP_TILES : 0;
131 return CMD_2K_LIST;
136 BOOL waitForMutex(void)
138 DWORD dwRes;
139 if( !ghMutex )
140 ghMutex = OpenMutex(SYNCHRONIZE, FALSE, OW_MUTEX_NAME);
141 if( ghMutex )
143 dwRes = WaitForSingleObject(ghMutex, INFINITE);
144 switch(dwRes)
146 case WAIT_OBJECT_0:
147 // okay, continue
148 break;
149 case WAIT_ABANDONED:
150 default:
151 return FALSE;
154 return TRUE;
157 void releaseMutex(void)
159 if( ghMutex )
161 ReleaseMutex(ghMutex);
162 ghMutex = NULL;
167 BOOL CALLBACK fpEnumChildren(HWND hwnd, LPARAM lParam)
169 static char buf[32];
170 if( GetClassName(hwnd, buf, 31) )
172 switch(gOwShared.iFocus)
174 case F_DIRLIST:
175 if( strcmp(buf, WC_LISTVIEWA) == 0 && GetDlgCtrlID(hwnd) == CID_DIRLIST)
176 SetFocus(hwnd);
177 break;
178 case F_FNAME:
179 if( strcmp(buf, WC_COMBOBOXEXA) == 0 && GetDlgCtrlID(hwnd) == CID_FNAME)
180 SetFocus(hwnd);
181 break;
182 case F_FTYPE:
183 if( strcmp(buf, WC_COMBOBOXA) == 0 && GetDlgCtrlID(hwnd) == CID_FTYPE)
184 SetFocus(hwnd);
185 break;
186 case F_PLACES:
187 if( IsWindowVisible(hwnd) && strcmp(buf, TOOLBARCLASSNAMEA) == 0 && GetDlgCtrlID(hwnd) == CID_PLACES)
188 SetFocus(hwnd);
189 break;
190 case F_LOOKIN:
191 if( strcmp(buf, WC_COMBOBOXA) == 0 && GetDlgCtrlID(hwnd) == CID_LOOKIN)
192 SetFocus(hwnd);
193 break;
196 /* if( GetDlgCtrlID(hwnd) == 0x47c )
198 SetWindowText(hwnd, "C:\\code\\");
199 SendMessage((HWND)lParam, WM_COMMAND, MAKEWPARAM(1, BN_CLICKED), (LPARAM)GetDlgItem((HWND)lParam, 1));
201 return 1;
205 static int CALLBACK WINAPI enumFindChildWindow(HWND hwnd, PFindChildData pData)
207 UINT uID = GetDlgCtrlID(hwnd);
208 static char buf[256];
209 if (uID == pData->uID)
211 //dbg("Found window %p with id %d", hwnd, uID);
212 if (GetClassName(hwnd, buf, 256) && strcmp(buf, pData->szClass) == 0)
214 pData->hwFound = hwnd;
215 return 0;
217 //else
218 //dbg("Window's class is %s - not what's wanted", buf);
220 return 1;
223 HWND findChildWindow(HWND hwParent, UINT uID, const char *szClass)
225 FindChildData fData;
227 fData.szClass = szClass;
228 fData.uID = uID;
229 fData.hwFound = NULL;
231 //dbg("findChildWindow: seeking \"%s\", %d", szClass, uID);
233 EnumChildWindows(hwParent, (WNDENUMPROC)enumFindChildWindow, (LPARAM) & fData);
235 return fData.hwFound;