comparison os2/dw.c @ 476:a84ee39e7aea

Initial folder browser code for OS/2.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 13 Oct 2003 09:13:40 +0000
parents 1547e8c327d9
children 923b46b0716c
comparison
equal deleted inserted replaced
475:1547e8c327d9 476:a84ee39e7aea
201 if(box > 0) 201 if(box > 0)
202 return lastbox; 202 return lastbox;
203 return handle; 203 return handle;
204 } 204 }
205 205
206
207 /* A "safe" WinSendMsg() that tries multiple times in case the
208 * queue is blocked for one reason or another.
209 */
210 MRESULT _dw_send_msg(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2, int failure)
211 {
212 MRESULT res;
213 int z = 0;
214
215 while((int)(res = WinSendMsg(hwnd, msg, mp1, mp2)) == failure)
216 {
217 z++;
218 if(z > 5000000)
219 return (MRESULT)failure;
220 dw_main_sleep(1);
221 }
222 return res;
223 }
206 224
207 /* Return the entryfield child of a window */ 225 /* Return the entryfield child of a window */
208 HWND _find_entryfield(HWND handle) 226 HWND _find_entryfield(HWND handle)
209 { 227 {
210 HENUM henum; 228 HENUM henum;
5951 5969
5952 cbExtra = sizeof(CNRITEM) - sizeof(MINIRECORDCORE); 5970 cbExtra = sizeof(CNRITEM) - sizeof(MINIRECORDCORE);
5953 5971
5954 /* Allocate memory for the parent record */ 5972 /* Allocate memory for the parent record */
5955 5973
5956 pci = WinSendMsg(handle, CM_ALLOCRECORD, MPFROMLONG(cbExtra), MPFROMSHORT(1)); 5974 if((pci = (PCNRITEM)_dw_send_msg(handle, CM_ALLOCRECORD, MPFROMLONG(cbExtra), MPFROMSHORT(1), 0)) == 0)
5975 return 0;
5957 5976
5958 /* Fill in the parent record data */ 5977 /* Fill in the parent record data */
5959 5978
5960 pci->rc.cb = sizeof(MINIRECORDCORE); 5979 pci->rc.cb = sizeof(MINIRECORDCORE);
5961 pci->rc.pszIcon = strdup(title); 5980 pci->rc.pszIcon = strdup(title);
6316 * handle: Handle to icon returned by dw_icon_load(). 6335 * handle: Handle to icon returned by dw_icon_load().
6317 */ 6336 */
6318 void API dw_icon_free(unsigned long handle) 6337 void API dw_icon_free(unsigned long handle)
6319 { 6338 {
6320 WinDestroyPointer(handle); 6339 WinDestroyPointer(handle);
6321 }
6322
6323 /* A "safe" WinSendMsg() that tries multiple times in case the
6324 * queue is blocked for one reason or another.
6325 */
6326 MRESULT _dw_send_msg(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2, int failure)
6327 {
6328 MRESULT res;
6329 int z = 0;
6330
6331 while((int)(res = WinSendMsg(hwnd, msg, mp1, mp2)) == failure)
6332 {
6333 z++;
6334 if(z > 5000000)
6335 return (MRESULT)failure;
6336 dw_main_sleep(1);
6337 }
6338 return res;
6339 } 6340 }
6340 6341
6341 /* 6342 /*
6342 * Allocates memory used to populate a container. 6343 * Allocates memory used to populate a container.
6343 * Parameters: 6344 * Parameters:
8025 env->DWMajorVersion = DW_MAJOR_VERSION; 8026 env->DWMajorVersion = DW_MAJOR_VERSION;
8026 env->DWMinorVersion = DW_MINOR_VERSION; 8027 env->DWMinorVersion = DW_MINOR_VERSION;
8027 env->DWSubVersion = DW_SUB_VERSION; 8028 env->DWSubVersion = DW_SUB_VERSION;
8028 } 8029 }
8029 8030
8031 /* The next few functions are support functions for the OS/2 folder browser */
8032 void _populate_directory(HMTX mtx, HWND *tree, HTREEITEM parent, char *path)
8033 {
8034 FILEFINDBUF3 ffbuf;
8035 HTREEITEM item;
8036 ULONG count;
8037 HDIR hdir = HDIR_CREATE;
8038 APIRET rc;
8039
8040 dw_mutex_lock(mtx);
8041 if(*tree)
8042 {
8043 if((rc = DosFindFirst(path, &hdir, MUST_HAVE_DIRECTORY | FILE_NORMAL,
8044 &ffbuf, sizeof(FILEFINDBUF3), &count, FIL_STANDARD)) == NO_ERROR)
8045 {
8046 while(DosFindNext(hdir, &ffbuf, sizeof(FILEFINDBUF3), &count) == NO_ERROR)
8047 {
8048 int len = strlen(path);
8049 char *folder = malloc(len + ffbuf.cchName + 2);
8050 strcpy(folder, path);
8051 strcpy(&folder[len-1], ffbuf.achName);
8052
8053 item = dw_tree_insert(*tree, ffbuf.achName, WinLoadFileIcon(folder, TRUE), parent, 0);
8054
8055 dw_mutex_unlock(mtx);
8056 strcat(folder, "\\*");
8057
8058 _populate_directory(mtx, tree, item, folder);
8059
8060 free(folder);
8061 dw_mutex_lock(mtx);
8062 if(!*tree)
8063 {
8064 dw_mutex_unlock(mtx);
8065 DosFindClose(hdir);
8066 return;
8067 }
8068 }
8069 DosFindClose(hdir);
8070 }
8071 }
8072 dw_mutex_unlock(mtx);
8073 }
8074
8075 void _populate_tree_thread(void *data)
8076 {
8077 HWND window = (HWND)data, *tree = (HWND *)dw_window_get_data(window, "_dw_tree");
8078 HMTX mtx = (HMTX)dw_window_get_data(window, "_dw_mutex");
8079 int drive;
8080 HTREEITEM items[26];
8081 FSINFO volinfo;
8082
8083 DosError(FERR_DISABLEHARDERR);
8084
8085 for(drive=0;drive<26;drive++)
8086 {
8087 if(DosQueryFSInfo(drive+1, FSIL_VOLSER,(PVOID)&volinfo, sizeof(FSINFO)) == NO_ERROR)
8088 {
8089 char folder[5] = "C:\\", name[9] = "Drive C:";
8090
8091 folder[0] = name[6] = 'A' + drive;
8092
8093 dw_mutex_lock(mtx);
8094 if(!*tree)
8095 {
8096 free(tree);
8097 dw_mutex_close(mtx);
8098 return;
8099 }
8100
8101 items[drive] = dw_tree_insert(*tree, name, WinLoadFileIcon(folder, TRUE), NULL, 0);
8102
8103 dw_mutex_unlock(mtx);
8104 }
8105 else
8106 items[drive] = 0;
8107 }
8108 DosError(FERR_ENABLEHARDERR);
8109
8110 for(drive=0;drive<26;drive++)
8111 {
8112 if(items[drive])
8113 {
8114 char folder[5] = "C:\\";
8115
8116 folder[0] = 'A' + drive;
8117
8118 strcat(folder, "*");
8119
8120 _populate_directory(mtx, tree, items[drive], folder);
8121 }
8122 }
8123
8124 dw_mutex_lock(mtx);
8125 if(!*tree)
8126 {
8127 free(tree);
8128 dw_mutex_close(mtx);
8129 }
8130 }
8131
8132 int DWSIGNAL _dw_ok_func(HWND window, void *data)
8133 {
8134 DWDialog *dwwait = (DWDialog *)data;
8135 HMTX mtx = (HMTX)dw_window_get_data(window, "_dw_mutex");
8136 void *treedata;
8137 HWND *tree;
8138
8139 if(!dwwait)
8140 return FALSE;
8141
8142 dw_mutex_lock(mtx);
8143 treedata = dw_window_get_data((HWND)dwwait->data, "_dw_tree_selected");
8144 if((tree = (HWND *)dw_window_get_data((HWND)dwwait->data, "_dw_tree")) != 0)
8145 *tree = 0;
8146 dw_mutex_unlock(mtx);
8147 dw_window_destroy((HWND)dwwait->data);
8148 dw_dialog_dismiss((DWDialog *)data, treedata);
8149 return FALSE;
8150 }
8151
8152 int DWSIGNAL _dw_cancel_func(HWND window, void *data)
8153 {
8154 DWDialog *dwwait = (DWDialog *)data;
8155 HMTX mtx = (HMTX)dw_window_get_data(window, "_dw_mutex");
8156 HWND *tree;
8157
8158 if(!dwwait)
8159 return FALSE;
8160
8161 dw_mutex_lock(mtx);
8162 if((tree = (HWND *)dw_window_get_data((HWND)dwwait->data, "_dw_tree")) != 0)
8163 *tree = 0;
8164 dw_mutex_unlock(mtx);
8165 dw_window_destroy((HWND)dwwait->data);
8166 dw_dialog_dismiss((DWDialog *)data, NULL);
8167 return FALSE;
8168 }
8169
8170 int DWSIGNAL _item_select(HWND window, HTREEITEM item, char *text, void *data, void *itemdata)
8171 {
8172 DWDialog *dwwait = (DWDialog *)data;
8173 char *treedata = (char *)dw_window_get_data((HWND)dwwait->data, "_dw_tree_selected");
8174
8175 if(treedata)
8176 free(treedata);
8177
8178 treedata = strdup(text);
8179 dw_window_set_data((HWND)dwwait->data, "_dw_tree_selected", (void *)treedata);
8180
8181 return FALSE;
8182 }
8183
8030 /* 8184 /*
8031 * Opens a file dialog and queries user selection. 8185 * Opens a file dialog and queries user selection.
8032 * Parameters: 8186 * Parameters:
8033 * title: Title bar text for dialog. 8187 * title: Title bar text for dialog.
8034 * defpath: The default path of the open dialog. 8188 * defpath: The default path of the open dialog.
8039 * the file path on success. 8193 * the file path on success.
8040 * 8194 *
8041 */ 8195 */
8042 char * API dw_file_browse(char *title, char *defpath, char *ext, int flags) 8196 char * API dw_file_browse(char *title, char *defpath, char *ext, int flags)
8043 { 8197 {
8044 FILEDLG fild; 8198 if(flags == DW_DIRECTORY_OPEN)
8045 HWND hwndFile; 8199 {
8046 int len; 8200 HWND window, hbox, vbox, tree, button, *ptr;
8047 8201 DWDialog *dwwait;
8048 if(defpath) 8202 HMTX mtx = dw_mutex_new();
8049 strcpy(fild.szFullFile, defpath); 8203
8204 window = dw_window_new( HWND_DESKTOP, title, FCF_SHELLPOSITION | FCF_TITLEBAR | FCF_SIZEBORDER | FCF_MINMAX);
8205
8206 vbox = dw_box_new(DW_VERT, 5);
8207
8208 dw_box_pack_start(window, vbox, 0, 0, TRUE, TRUE, 0);
8209
8210 tree = dw_tree_new(60);
8211
8212 dw_box_pack_start(vbox, tree, 1, 1, TRUE, TRUE, 0);
8213 ptr = malloc(sizeof(HWND));
8214 *ptr = tree;
8215 dw_window_set_data(window, "_dw_tree", (void *)ptr);
8216 dw_window_set_data(window, "_dw_mutex", (void *)mtx);
8217
8218 hbox = dw_box_new(DW_HORZ, 0);
8219
8220 dw_box_pack_start(vbox, hbox, 0, 0, TRUE, FALSE, 0);
8221
8222 dwwait = dw_dialog_new((void *)window);
8223
8224 dw_signal_connect(tree, DW_SIGNAL_ITEM_SELECT, DW_SIGNAL_FUNC(_item_select), (void *)dwwait);
8225
8226 button = dw_button_new("Ok", 1001L);
8227 dw_box_pack_start(hbox, button, 50, 30, TRUE, FALSE, 3);
8228 dw_signal_connect(button, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_dw_ok_func), (void *)dwwait);
8229
8230 button = dw_button_new("Cancel", 1002L);
8231 dw_box_pack_start(hbox, button, 50, 30, TRUE, FALSE, 3);
8232 dw_signal_connect(button, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(_dw_cancel_func), (void *)dwwait);
8233
8234 dw_window_set_usize(window, 225, 300);
8235 dw_window_show(window);
8236
8237 dw_thread_new((void *)_populate_tree_thread, (void *)window, 0xfff);
8238 return (char *)dw_dialog_wait(dwwait);
8239 }
8050 else 8240 else
8051 strcpy(fild.szFullFile, ""); 8241 {
8052 8242 FILEDLG fild;
8053 len = strlen(fild.szFullFile); 8243 HWND hwndFile;
8054 8244 int len;
8055 if(len) 8245
8056 { 8246 if(defpath)
8057 if(fild.szFullFile[len-1] != '\\') 8247 strcpy(fild.szFullFile, defpath);
8058 strcat(fild.szFullFile, "\\"); 8248 else
8059 } 8249 strcpy(fild.szFullFile, "");
8060 strcat(fild.szFullFile, "*"); 8250
8061 8251 len = strlen(fild.szFullFile);
8062 if(ext) 8252
8063 { 8253 if(len)
8064 strcat(fild.szFullFile, "."); 8254 {
8065 strcat(fild.szFullFile, ext); 8255 if(fild.szFullFile[len-1] != '\\')
8066 } 8256 strcat(fild.szFullFile, "\\");
8067 8257 }
8068 memset(&fild, 0, sizeof(FILEDLG)); 8258 strcat(fild.szFullFile, "*");
8069 fild.cbSize = sizeof(FILEDLG); 8259
8070 fild.fl = FDS_CENTER | FDS_OPEN_DIALOG; 8260 if(ext)
8071 fild.pszTitle = title; 8261 {
8072 fild.pszOKButton = ((flags & DW_FILE_SAVE) ? "Save" : "Open"); 8262 strcat(fild.szFullFile, ".");
8073 fild.pfnDlgProc = (PFNWP)WinDefFileDlgProc; 8263 strcat(fild.szFullFile, ext);
8074 8264 }
8075 hwndFile = WinFileDlg(HWND_DESKTOP, HWND_DESKTOP, &fild); 8265
8076 if(hwndFile) 8266 memset(&fild, 0, sizeof(FILEDLG));
8077 { 8267 fild.cbSize = sizeof(FILEDLG);
8078 switch(fild.lReturn) 8268 fild.fl = FDS_CENTER | FDS_OPEN_DIALOG;
8079 { 8269 fild.pszTitle = title;
8080 case DID_OK: 8270 fild.pszOKButton = ((flags & DW_FILE_SAVE) ? "Save" : "Open");
8081 return strdup(fild.szFullFile); 8271 fild.pfnDlgProc = (PFNWP)WinDefFileDlgProc;
8082 case DID_CANCEL: 8272
8083 return NULL; 8273 hwndFile = WinFileDlg(HWND_DESKTOP, HWND_DESKTOP, &fild);
8274 if(hwndFile)
8275 {
8276 switch(fild.lReturn)
8277 {
8278 case DID_OK:
8279 return strdup(fild.szFullFile);
8280 case DID_CANCEL:
8281 return NULL;
8282 }
8084 } 8283 }
8085 } 8284 }
8086 return NULL; 8285 return NULL;
8087 } 8286 }
8088 8287