program.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Program Manager
  3. *
  4. * Copyright 1996 Ulrich Schmid
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #define WIN32_LEAN_AND_MEAN
  21. #include <string.h>
  22. #include "windows.h"
  23. #include "progman.h"
  24. /***********************************************************************
  25. *
  26. * PROGRAM_ProgramWndProc
  27. */
  28. static LRESULT CALLBACK PROGRAM_ProgramWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  29. {
  30. switch (msg)
  31. {
  32. case WM_NCLBUTTONDOWN:
  33. {
  34. HLOCAL hProgram = (HLOCAL) GetWindowLongPtrW(hWnd, 0);
  35. PROGRAM *program = LocalLock(hProgram);
  36. PROGGROUP *group = LocalLock(program->hGroup);
  37. group->hActiveProgram = hProgram;
  38. EnableMenuItem(Globals.hFileMenu, PM_MOVE , MF_ENABLED);
  39. EnableMenuItem(Globals.hFileMenu, PM_COPY , MF_ENABLED);
  40. break;
  41. }
  42. case WM_NCLBUTTONDBLCLK:
  43. {
  44. PROGRAM_ExecuteProgram((HLOCAL) GetWindowLongPtrW(hWnd, 0));
  45. return(0);
  46. }
  47. case WM_PAINTICON:
  48. case WM_NCPAINT:
  49. {
  50. PROGRAM *program;
  51. PAINTSTRUCT ps;
  52. HDC hdc;
  53. hdc = BeginPaint(hWnd,&ps);
  54. program = LocalLock((HLOCAL) GetWindowLongPtrW(hWnd, 0));
  55. if (program->hIcon)
  56. DrawIcon(hdc, 0, 0, program->hIcon);
  57. EndPaint(hWnd,&ps);
  58. break;
  59. }
  60. }
  61. return DefWindowProcW(hWnd, msg, wParam, lParam);
  62. }
  63. /***********************************************************************
  64. *
  65. * PROGRAM_RegisterProgramWinClass
  66. */
  67. ATOM PROGRAM_RegisterProgramWinClass(void)
  68. {
  69. WNDCLASSW class;
  70. class.style = CS_HREDRAW | CS_VREDRAW;
  71. class.lpfnWndProc = PROGRAM_ProgramWndProc;
  72. class.cbClsExtra = 0;
  73. class.cbWndExtra = sizeof(LONG_PTR);
  74. class.hInstance = Globals.hInstance;
  75. class.hIcon = 0;
  76. class.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
  77. class.hbrBackground = GetStockObject (WHITE_BRUSH);
  78. class.lpszMenuName = 0;
  79. class.lpszClassName = STRING_PROGRAM_WIN_CLASS_NAME;
  80. return RegisterClassW(&class);
  81. }
  82. /***********************************************************************
  83. *
  84. * PROGRAM_NewProgram
  85. */
  86. VOID PROGRAM_NewProgram(HLOCAL hGroup)
  87. {
  88. INT nCmdShow = SW_SHOWNORMAL;
  89. INT nHotKey = 0;
  90. INT nIconIndex = 0;
  91. CHAR szName[MAX_PATHNAME_LEN] = "";
  92. CHAR szCmdLine[MAX_PATHNAME_LEN] = "";
  93. CHAR szIconFile[MAX_PATHNAME_LEN] = "";
  94. CHAR szWorkDir[MAX_PATHNAME_LEN] = "";
  95. HICON hIcon = 0;
  96. if (!DIALOG_ProgramAttributes(szName, szCmdLine, szWorkDir, szIconFile,
  97. &hIcon, &nIconIndex, &nHotKey,
  98. &nCmdShow, MAX_PATHNAME_LEN))
  99. return;
  100. if (!hIcon) hIcon = LoadIconW(0, (LPWSTR)IDI_WINLOGO);
  101. if (!PROGRAM_AddProgram(hGroup, hIcon, szName, 0, 0, szCmdLine, szIconFile,
  102. nIconIndex, szWorkDir, nHotKey, nCmdShow))
  103. return;
  104. GRPFILE_WriteGroupFile(hGroup);
  105. }
  106. /***********************************************************************
  107. *
  108. * PROGRAM_ModifyProgram
  109. */
  110. VOID PROGRAM_ModifyProgram(HLOCAL hProgram)
  111. {
  112. PROGRAM *program = LocalLock(hProgram);
  113. CHAR szName[MAX_PATHNAME_LEN];
  114. CHAR szCmdLine[MAX_PATHNAME_LEN];
  115. CHAR szIconFile[MAX_PATHNAME_LEN];
  116. CHAR szWorkDir[MAX_PATHNAME_LEN];
  117. lstrcpynA(szName, LocalLock(program->hName), MAX_PATHNAME_LEN);
  118. lstrcpynA(szCmdLine, LocalLock(program->hCmdLine), MAX_PATHNAME_LEN);
  119. lstrcpynA(szIconFile, LocalLock(program->hIconFile), MAX_PATHNAME_LEN);
  120. lstrcpynA(szWorkDir, LocalLock(program->hWorkDir), MAX_PATHNAME_LEN);
  121. if (!DIALOG_ProgramAttributes(szName, szCmdLine, szWorkDir, szIconFile,
  122. &program->hIcon, &program->nIconIndex,
  123. &program->nHotKey, &program->nCmdShow,
  124. MAX_PATHNAME_LEN))
  125. return;
  126. MAIN_ReplaceString(&program->hName, szName);
  127. MAIN_ReplaceString(&program->hCmdLine, szCmdLine);
  128. MAIN_ReplaceString(&program->hIconFile, szIconFile);
  129. MAIN_ReplaceString(&program->hWorkDir, szWorkDir);
  130. SetWindowTextA(program->hWnd, szName);
  131. UpdateWindow(program->hWnd);
  132. GRPFILE_WriteGroupFile(program->hGroup);
  133. return;
  134. }
  135. /***********************************************************************
  136. *
  137. * PROGRAM_AddProgram
  138. */
  139. HLOCAL PROGRAM_AddProgram(HLOCAL hGroup, HICON hIcon, LPCSTR lpszName,
  140. INT x, INT y, LPCSTR lpszCmdLine,
  141. LPCSTR lpszIconFile, INT nIconIndex,
  142. LPCSTR lpszWorkDir, INT nHotKey, INT nCmdShow)
  143. {
  144. PROGGROUP *group = LocalLock(hGroup);
  145. PROGRAM *program;
  146. HLOCAL hPrior, *p;
  147. HLOCAL hProgram = LocalAlloc(LMEM_FIXED, sizeof(PROGRAM));
  148. HLOCAL hName = LocalAlloc(LMEM_FIXED, 1 + strlen(lpszName));
  149. HLOCAL hCmdLine = LocalAlloc(LMEM_FIXED, 1 + strlen(lpszCmdLine));
  150. HLOCAL hIconFile = LocalAlloc(LMEM_FIXED, 1 + strlen(lpszIconFile));
  151. HLOCAL hWorkDir = LocalAlloc(LMEM_FIXED, 1 + strlen(lpszWorkDir));
  152. if (!hProgram || !hName || !hCmdLine || !hIconFile || !hWorkDir)
  153. {
  154. MAIN_MessageBoxIDS(IDS_OUT_OF_MEMORY, IDS_ERROR, MB_OK);
  155. if (hProgram) LocalFree(hProgram);
  156. if (hName) LocalFree(hName);
  157. if (hCmdLine) LocalFree(hCmdLine);
  158. if (hIconFile) LocalFree(hIconFile);
  159. if (hWorkDir) LocalFree(hWorkDir);
  160. return(0);
  161. }
  162. memcpy(LocalLock(hName), lpszName, 1 + strlen(lpszName));
  163. memcpy(LocalLock(hCmdLine), lpszCmdLine, 1 + strlen(lpszCmdLine));
  164. memcpy(LocalLock(hIconFile), lpszIconFile, 1 + strlen(lpszIconFile));
  165. memcpy(LocalLock(hWorkDir), lpszWorkDir, 1 + strlen(lpszWorkDir));
  166. group->hActiveProgram = hProgram;
  167. hPrior = 0;
  168. p = &group->hPrograms;
  169. while (*p)
  170. {
  171. hPrior = *p;
  172. p = &((PROGRAM*)LocalLock(hPrior))->hNext;
  173. }
  174. *p = hProgram;
  175. program = LocalLock(hProgram);
  176. program->hGroup = hGroup;
  177. program->hPrior = hPrior;
  178. program->hNext = 0;
  179. program->hName = hName;
  180. program->hCmdLine = hCmdLine;
  181. program->hIconFile = hIconFile;
  182. program->nIconIndex = nIconIndex;
  183. program->hWorkDir = hWorkDir;
  184. program->hIcon = hIcon;
  185. program->nCmdShow = nCmdShow;
  186. program->nHotKey = nHotKey;
  187. program->hWnd =
  188. CreateWindowW(STRING_PROGRAM_WIN_CLASS_NAME, NULL,
  189. WS_CHILD | WS_CAPTION,
  190. x, y, CW_USEDEFAULT, CW_USEDEFAULT,
  191. group->hWnd, 0, Globals.hInstance, 0);
  192. SetWindowTextA(program->hWnd, lpszName);
  193. SetWindowLongPtrW(program->hWnd, 0, (LONG_PTR) hProgram);
  194. ShowWindow (program->hWnd, SW_SHOWMINIMIZED);
  195. SetWindowPos (program->hWnd, 0, x, y, 0, 0, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE);
  196. UpdateWindow (program->hWnd);
  197. return hProgram;
  198. }
  199. /***********************************************************************
  200. *
  201. * PROGRAM_CopyMoveProgram
  202. */
  203. VOID PROGRAM_CopyMoveProgram(HLOCAL hProgram, BOOL bMove)
  204. {
  205. PROGRAM *program = LocalLock(hProgram);
  206. PROGGROUP *fromgroup = LocalLock(program->hGroup);
  207. HLOCAL hGroup = DIALOG_CopyMove(LocalLock(program->hName),
  208. LocalLock(fromgroup->hName), bMove);
  209. if (!hGroup) return;
  210. /* FIXME shouldn't be necessary */
  211. OpenIcon(((PROGGROUP*)LocalLock(hGroup))->hWnd);
  212. if (!PROGRAM_AddProgram(hGroup,
  213. #if 0
  214. CopyIcon(program->hIcon),
  215. #else
  216. program->hIcon,
  217. #endif
  218. LocalLock(program->hName),
  219. program->x, program->y,
  220. LocalLock(program->hCmdLine),
  221. LocalLock(program->hIconFile),
  222. program->nIconIndex,
  223. LocalLock(program->hWorkDir),
  224. program->nHotKey, program->nCmdShow)) return;
  225. GRPFILE_WriteGroupFile(hGroup);
  226. if (bMove) PROGRAM_DeleteProgram(hProgram, TRUE);
  227. }
  228. /***********************************************************************
  229. *
  230. * PROGRAM_ExecuteProgram
  231. */
  232. VOID PROGRAM_ExecuteProgram(HLOCAL hProgram)
  233. {
  234. PROGRAM *program = LocalLock(hProgram);
  235. LPSTR lpszCmdLine = LocalLock(program->hCmdLine);
  236. /* FIXME set working directory from program->hWorkDir */
  237. WinExec(lpszCmdLine, program->nCmdShow);
  238. if (Globals.bMinOnRun) CloseWindow(Globals.hMainWnd);
  239. }
  240. /***********************************************************************
  241. *
  242. * PROGRAM_DeleteProgram
  243. */
  244. VOID PROGRAM_DeleteProgram(HLOCAL hProgram, BOOL bUpdateGrpFile)
  245. {
  246. PROGRAM *program = LocalLock(hProgram);
  247. PROGGROUP *group = LocalLock(program->hGroup);
  248. group->hActiveProgram = 0;
  249. if (program->hPrior)
  250. ((PROGRAM*)LocalLock(program->hPrior))->hNext = program->hNext;
  251. else
  252. ((PROGGROUP*)LocalLock(program->hGroup))->hPrograms = program->hNext;
  253. if (program->hNext)
  254. ((PROGRAM*)LocalLock(program->hNext))->hPrior = program->hPrior;
  255. if (bUpdateGrpFile)
  256. GRPFILE_WriteGroupFile(program->hGroup);
  257. DestroyWindow(program->hWnd);
  258. #if 0
  259. if (program->hIcon)
  260. DestroyIcon(program->hIcon);
  261. #endif
  262. LocalFree(program->hName);
  263. LocalFree(program->hCmdLine);
  264. LocalFree(program->hIconFile);
  265. LocalFree(program->hWorkDir);
  266. LocalFree(hProgram);
  267. }
  268. /***********************************************************************
  269. *
  270. * PROGRAM_FirstProgram
  271. */
  272. HLOCAL PROGRAM_FirstProgram(HLOCAL hGroup)
  273. {
  274. PROGGROUP *group;
  275. if (!hGroup) return(0);
  276. group = LocalLock(hGroup);
  277. return(group->hPrograms);
  278. }
  279. /***********************************************************************
  280. *
  281. * PROGRAM_NextProgram
  282. */
  283. HLOCAL PROGRAM_NextProgram(HLOCAL hProgram)
  284. {
  285. PROGRAM *program;
  286. if (!hProgram) return(0);
  287. program = LocalLock(hProgram);
  288. return(program->hNext);
  289. }
  290. /***********************************************************************
  291. *
  292. * PROGRAM_ActiveProgram
  293. */
  294. HLOCAL PROGRAM_ActiveProgram(HLOCAL hGroup)
  295. {
  296. PROGGROUP *group;
  297. if (!hGroup) return(0);
  298. group = LocalLock(hGroup);
  299. if (IsIconic(group->hWnd)) return(0);
  300. return(group->hActiveProgram);
  301. }
  302. /***********************************************************************
  303. *
  304. * PROGRAM_ProgramName
  305. */
  306. LPCSTR PROGRAM_ProgramName(HLOCAL hProgram)
  307. {
  308. PROGRAM *program;
  309. if (!hProgram) return(0);
  310. program = LocalLock(hProgram);
  311. return(LocalLock(program->hName));
  312. }