Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

761 lines
16 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. memdbint.c
  5. Abstract:
  6. operates the main child window of memdbe.exe.
  7. Author:
  8. Matthew Vanderzee (mvander) 13-Aug-1999
  9. Revision History:
  10. --*/
  11. #include "pch.h"
  12. #include "dbeditp.h"
  13. #include <commdlg.h>
  14. HINSTANCE g_hInst;
  15. HANDLE g_hHeap;
  16. extern HMENU g_hMenuMain;
  17. BOOL g_ParsePaths;
  18. OPENFILENAME g_Ofn;
  19. BOOL g_IsFileModified;
  20. CHAR g_FileName[_MAX_PATH];
  21. HTREEITEM g_hSelItem;
  22. BOOL
  23. WantProcess (
  24. UINT msg,
  25. WPARAM wParam,
  26. LPARAM lParam
  27. )
  28. {
  29. switch (msg) {
  30. case WM_COMMAND:
  31. switch (LOWORD(wParam)) {
  32. case ID_FILE_NEW:
  33. case ID_FILE_LOAD:
  34. case ID_FILE_SAVE:
  35. case ID_FILE_SAVEAS:
  36. case ID_FILE_REVERT:
  37. case ID_KEY_COPYNAME:
  38. case ID_KEY_CREATENEW:
  39. case ID_KEY_CREATECHILD:
  40. case ID_KEY_DELETE:
  41. case ID_KEY_FIND:
  42. case ID_KEY_FINDNEXT:
  43. case ID_KEY_PARSEPATHS:
  44. case ID_KEY_RENAME:
  45. return TRUE;
  46. }
  47. break;
  48. }
  49. return FALSE;
  50. }
  51. BOOL
  52. InitializeMemDb (
  53. HWND hWnd
  54. )
  55. {
  56. g_FileName[0] = '\0';
  57. g_Ofn.lStructSize = sizeof(OPENFILENAME);
  58. g_Ofn.hwndOwner = hWnd;
  59. g_Ofn.hInstance = NULL;
  60. g_Ofn.lpstrFilter = "DAT files\0*.dat\0All files\0*.*\0";
  61. g_Ofn.lpstrCustomFilter = NULL;
  62. g_Ofn.nMaxCustFilter = 0;
  63. g_Ofn.nFilterIndex = 1;
  64. g_Ofn.lpstrFile = g_FileName;
  65. g_Ofn.nMaxFile = _MAX_PATH;
  66. g_Ofn.lpstrFileTitle = NULL;
  67. g_Ofn.lpstrInitialDir = NULL;
  68. g_Ofn.lpstrTitle = NULL;
  69. g_Ofn.Flags = 0;
  70. g_Ofn.nFileOffset = 0;
  71. g_Ofn.nFileExtension = 0;
  72. g_Ofn.lpstrDefExt = "dat";
  73. g_Ofn.lCustData = 0L;
  74. g_Ofn.lpfnHook = NULL;
  75. g_Ofn.lpTemplateName = NULL;
  76. UtInitialize (NULL);
  77. MemDbInitialize ();
  78. return TRUE;
  79. }
  80. BOOL
  81. DestroyMemDb (
  82. VOID
  83. )
  84. {
  85. g_FileName[0] = '\0';
  86. MemDbTerminate();
  87. UtTerminate();
  88. return FALSE;
  89. }
  90. VOID
  91. pDisplayInit (
  92. HWND hdlg
  93. )
  94. {
  95. HMENU hMenu;
  96. SetDlgItemText (hdlg, IDC_STATIC_KEYNAME, "");
  97. if (hMenu = GetSubMenu (g_hMenuMain, MENUINDEX_MAIN_KEY)) {
  98. EnableMenuItem (hMenu, ID_KEY_FINDNEXT, MF_GRAYED);
  99. }
  100. }
  101. BOOL
  102. pUpdateFileStatus (
  103. HWND hdlg,
  104. BOOL FileModified
  105. )
  106. {
  107. HMENU hMenu;
  108. HWND hWnd;
  109. hWnd = GetParent (hdlg);
  110. g_IsFileModified = FileModified;
  111. if (hMenu = GetSubMenu (g_hMenuMain, MENUINDEX_MAIN_FILE)) {
  112. EnableMenuItem (hMenu, ID_FILE_REVERT, g_IsFileModified ? MF_ENABLED : MF_GRAYED);
  113. EnableMenuItem (hMenu, ID_FILE_SAVE, g_IsFileModified ? MF_ENABLED : MF_GRAYED);
  114. }
  115. if (hWnd) {
  116. SendMessage (
  117. hWnd,
  118. WM_FILE_UPDATE,
  119. (WPARAM)GetFileNameFromPathA(g_FileName),
  120. (LPARAM)g_IsFileModified
  121. );
  122. }
  123. return TRUE;
  124. }
  125. BOOL
  126. pRefreshView (
  127. HWND hdlg
  128. )
  129. {
  130. SetDlgItemText (hdlg, IDC_STATIC_KEYNAME, "");
  131. g_hSelItem = NULL;
  132. KeyTreeRefresh ();
  133. DataListRefresh ();
  134. return TRUE;
  135. }
  136. BOOL
  137. pLoadFile (
  138. LPSTR FileName
  139. )
  140. {
  141. BOOL b;
  142. if (FileName) {
  143. b = TRUE;
  144. if (FileName != g_FileName) {
  145. //
  146. // (FileName == g_FileName) when we are reloading the same database
  147. //
  148. StringCopyA (g_FileName, FileName);
  149. }
  150. } else {
  151. g_Ofn.Flags = OFN_HIDEREADONLY;
  152. b = GetOpenFileName (&g_Ofn);
  153. }
  154. KeyTreeClear ();
  155. DataListClear ();
  156. if (b) {
  157. if (!(b = MemDbLoadA (g_FileName))) {
  158. if (GetLastError () == ERROR_BAD_FORMAT) {
  159. MessageBox (NULL, "Invalid database file format", "Error", MB_OK|MB_ICONEXCLAMATION);
  160. }
  161. }
  162. }
  163. return b;
  164. }
  165. BOOL
  166. pSaveFile (
  167. BOOL UseCurrentName
  168. )
  169. {
  170. BOOL b = TRUE;
  171. if (!UseCurrentName || (g_FileName[0] == '\0')) {
  172. g_Ofn.Flags = OFN_OVERWRITEPROMPT;
  173. b = GetSaveFileName (&g_Ofn);
  174. }
  175. if (b) {
  176. b = MemDbSaveA (g_FileName);
  177. #ifdef DEBUG
  178. if (!b) {
  179. DEBUGMSG ((DBG_ERROR, "Could not save file \"%s\"!", g_FileName));
  180. }
  181. #endif
  182. }
  183. return b;
  184. }
  185. BOOL
  186. pVerifyClose (
  187. VOID
  188. )
  189. {
  190. int Temp;
  191. if (!g_IsFileModified) {
  192. return TRUE;
  193. }
  194. Temp = MessageBox (
  195. NULL,
  196. "Database is modified, save?",
  197. "MemDb Editor",
  198. MB_YESNOCANCEL | MB_ICONQUESTION
  199. );
  200. if (Temp == IDYES) {
  201. pSaveFile (TRUE);
  202. }
  203. return Temp != IDCANCEL;
  204. }
  205. BOOL
  206. pResetView (
  207. VOID
  208. )
  209. {
  210. KeyTreeSetFilterPattern (NULL);
  211. return TRUE;
  212. }
  213. VOID
  214. AlertBadNewItemName (
  215. HTREEITEM hItem,
  216. PSTR ErrorStr
  217. )
  218. {
  219. if (MessageBox (
  220. NULL,
  221. ErrorStr ? ErrorStr : "Error",
  222. "Error",
  223. MB_OKCANCEL | MB_ICONEXCLAMATION
  224. ) == IDOK)
  225. {
  226. KeyTreeForceEditLabel (hItem);
  227. } else {
  228. KeyTreeDeleteItem (hItem);
  229. }
  230. }
  231. BOOL
  232. pFindKey (
  233. HWND hdlg
  234. )
  235. {
  236. BOOL b;
  237. HMENU hMenu;
  238. b = KeyTreeFind (hdlg);
  239. if (b) {
  240. if (hMenu = GetSubMenu (g_hMenuMain, MENUINDEX_MAIN_KEY)) {
  241. EnableMenuItem (hMenu, ID_KEY_FINDNEXT, MF_ENABLED);
  242. }
  243. }
  244. return b;
  245. }
  246. BOOL
  247. pFindNextKey (
  248. HWND hdlg
  249. )
  250. {
  251. BOOL b;
  252. HMENU hMenu;
  253. b = KeyTreeFindNext ();
  254. if (!b) {
  255. if (hMenu = GetSubMenu (g_hMenuMain, MENUINDEX_MAIN_KEY)) {
  256. EnableMenuItem (hMenu, ID_KEY_FINDNEXT, MF_GRAYED);
  257. }
  258. }
  259. return b;
  260. }
  261. BOOL
  262. pToggleParsePaths (
  263. HWND hdlg
  264. )
  265. {
  266. HMENU hMenu;
  267. if (!(hMenu = GetSubMenu (g_hMenuMain, MENUINDEX_MAIN_KEY))) {
  268. return FALSE;
  269. }
  270. g_ParsePaths = !(GetMenuState (hMenu, ID_KEY_PARSEPATHS, MF_BYCOMMAND) & MF_CHECKED);
  271. CheckMenuItem (hMenu, ID_KEY_PARSEPATHS, MF_BYCOMMAND | (g_ParsePaths?MF_CHECKED:MF_UNCHECKED));
  272. pRefreshView (hdlg);
  273. return FALSE;
  274. }
  275. VOID
  276. pSetFilter (
  277. HWND hdlg,
  278. BOOL UseInput
  279. )
  280. {
  281. CHAR Filter[MEMDB_MAX];
  282. if (UseInput) {
  283. GetDlgItemText (hdlg, IDC_EDIT_FILTERPATTERN, Filter, MEMDB_MAX);
  284. } else {
  285. Filter[0] = '\0';
  286. }
  287. KeyTreeSetFilterPattern (Filter);
  288. pRefreshView (hdlg);
  289. }
  290. BOOL
  291. pCopyKeyNameToClipboard (
  292. HWND hdlg,
  293. HTREEITEM hItem
  294. )
  295. {
  296. CHAR KeyName[MEMDB_MAX];
  297. if (!KeyTreeGetNameOfItem (hItem, KeyName)) {
  298. DEBUGMSG ((DBG_ERROR, "Could not get name of item"));
  299. return FALSE;
  300. }
  301. SetDlgItemText (hdlg, IDC_EDIT_KEYNAMEHIDDEN, KeyName);
  302. SendMessage (GetDlgItem (hdlg, IDC_EDIT_KEYNAMEHIDDEN), EM_SETSEL, 0, -1);
  303. SendMessage (GetDlgItem (hdlg, IDC_EDIT_KEYNAMEHIDDEN), WM_COPY, 0, 0);
  304. return TRUE;
  305. }
  306. BOOL
  307. CALLBACK
  308. MainDlgProc (
  309. HWND hdlg,
  310. UINT uMsg,
  311. WPARAM wParam,
  312. LPARAM lParam
  313. )
  314. {
  315. static BOOL InDrag = FALSE;
  316. BOOL b = FALSE;
  317. switch (uMsg) {
  318. case WM_INITDIALOG:
  319. pDisplayInit (hdlg);
  320. KeyTreeInit (hdlg);
  321. DataListInit (hdlg);
  322. g_FileName[0] = '\0';
  323. pUpdateFileStatus (hdlg, FALSE);
  324. InDrag = FALSE;
  325. g_hSelItem = NULL;
  326. g_ParsePaths = TRUE;
  327. break;
  328. case WM_COMMAND:
  329. switch (LOWORD(wParam)) {
  330. case IDC_BUTTON_EXPANDALL:
  331. if (HIWORD(wParam) == BN_CLICKED) {
  332. KeyTreeExpandItem (NULL, TRUE, TRUE);
  333. }
  334. break;
  335. case IDC_BUTTON_COLLAPSEALL:
  336. if (HIWORD(wParam) == BN_CLICKED) {
  337. KeyTreeExpandItem (NULL, FALSE, TRUE);
  338. }
  339. break;
  340. case IDC_BUTTON_REFRESH:
  341. if (HIWORD(wParam) == BN_CLICKED) {
  342. pRefreshView (hdlg);
  343. }
  344. break;
  345. case IDC_BUTTON_APPLYFILTER:
  346. if (HIWORD(wParam) == BN_CLICKED) {
  347. pSetFilter (hdlg, TRUE);
  348. }
  349. break;
  350. case IDC_BUTTON_SHOWALL:
  351. if (HIWORD(wParam) == BN_CLICKED) {
  352. pSetFilter (hdlg, FALSE);
  353. }
  354. break;
  355. case ID_FILE_NEW:
  356. if (pVerifyClose ()) {
  357. g_FileName[0] = '\0';
  358. pUpdateFileStatus (hdlg, FALSE);
  359. //MemDbReset();
  360. pResetView ();
  361. pRefreshView (hdlg);
  362. }
  363. break;
  364. case ID_FILE_REVERT:
  365. if (MessageBox (
  366. NULL,
  367. "Revert to saved database?",
  368. "MemDb Editor",
  369. MB_YESNO | MB_ICONQUESTION
  370. ) == IDYES)
  371. {
  372. if (g_FileName[0]=='\0') {
  373. //MemDbReset();
  374. } else {
  375. pLoadFile (g_FileName);
  376. }
  377. pRefreshView (hdlg);
  378. pUpdateFileStatus (hdlg, FALSE);
  379. }
  380. break;
  381. case ID_FILE_LOAD:
  382. if (pVerifyClose () && pLoadFile (NULL)) {
  383. pResetView ();
  384. pRefreshView (hdlg);
  385. pUpdateFileStatus (hdlg, FALSE);
  386. }
  387. break;
  388. case ID_FILE_SAVE:
  389. case ID_FILE_SAVEAS:
  390. //
  391. // if command is saveas, we dont use same filename.
  392. //
  393. if (pSaveFile (LOWORD(wParam) == ID_FILE_SAVE)) {
  394. pUpdateFileStatus (hdlg, FALSE);
  395. }
  396. break;
  397. case ID_KEY_CREATENEW:
  398. KeyTreeCreateItem (hdlg);
  399. break;
  400. case ID_KEY_CREATECHILD:
  401. if (g_hSelItem) {
  402. KeyTreeCreateChildItem (hdlg, g_hSelItem);
  403. }
  404. break;
  405. case ID_KEY_DELETE:
  406. if (g_hSelItem && KeyTreeDeleteKey (g_hSelItem)) {
  407. pUpdateFileStatus (hdlg, TRUE);
  408. }
  409. break;
  410. case ID_KEY_CREATELINKAGE:
  411. if (g_hSelItem && KeyTreeCreateLinkage (hdlg, g_hSelItem, FALSE, 0)) {
  412. pUpdateFileStatus (hdlg, TRUE);
  413. }
  414. break;
  415. case ID_KEY_COPYNAME:
  416. if (g_hSelItem) {
  417. pCopyKeyNameToClipboard (hdlg, g_hSelItem);
  418. }
  419. break;
  420. case ID_KEY_RENAME:
  421. if (g_hSelItem) {
  422. KeyTreeForceEditLabel (g_hSelItem);
  423. KeyTreeSelectItem (g_hSelItem);
  424. }
  425. break;
  426. case ID_KEY_FIND:
  427. pFindKey (hdlg);
  428. break;
  429. case ID_KEY_FINDNEXT:
  430. pFindNextKey (hdlg);
  431. break;
  432. case ID_KEY_PARSEPATHS:
  433. pToggleParsePaths (hdlg);
  434. break;
  435. case ID_KEY_CLEARDATA:
  436. if (g_hSelItem && KeyTreeClearData (g_hSelItem)) {
  437. pUpdateFileStatus (hdlg, TRUE);
  438. }
  439. break;
  440. case ID_DATA_ADDVALUE:
  441. case ID_DATA_ADDFLAGS:
  442. if (g_hSelItem && KeyTreeAddShortData (
  443. hdlg,
  444. g_hSelItem,
  445. (BYTE)((LOWORD(wParam)==ID_DATA_ADDVALUE) ? DATAFLAG_VALUE : DATAFLAG_FLAGS)
  446. ))
  447. {
  448. pUpdateFileStatus (hdlg, TRUE);
  449. }
  450. break;
  451. default:
  452. return DefWindowProc(hdlg, uMsg, wParam, lParam);
  453. }
  454. break;
  455. case WM_NOTIFY:
  456. if (IsKeyTree(((LPNMHDR)lParam)->hwndFrom))
  457. {
  458. switch (((LPNMHDR)lParam)->code) {
  459. case TVN_SELCHANGED:
  460. g_hSelItem = KeyTreeSelChanged (hdlg, (LPNMTREEVIEW)lParam);
  461. break;
  462. case TVN_KEYDOWN:
  463. switch (((LPNMTVKEYDOWN)lParam)->wVKey) {
  464. case VK_DELETE:
  465. return SendMessage (hdlg, WM_COMMAND, MAKEWPARAM (ID_KEY_DELETE, 0), (LPARAM)NULL);
  466. case VK_INSERT:
  467. return SendMessage (hdlg, WM_COMMAND, MAKEWPARAM (ID_KEY_RENAME, 0), (LPARAM)NULL);
  468. default:
  469. return DefWindowProc(hdlg, uMsg, wParam, lParam);
  470. }
  471. break;
  472. case TVN_BEGINDRAG:
  473. if (InDrag) {
  474. KeyTreeEndDrag (FALSE, NULL);
  475. InDrag = FALSE;
  476. }
  477. if (KeyTreeBeginDrag (hdlg, (LPNMTREEVIEW)lParam)) {
  478. InDrag = TRUE;
  479. }
  480. break;
  481. case TVN_ENDLABELEDIT:
  482. b = (KeyTreeGetIndexOfItem (g_hSelItem) == INVALID_KEY_HANDLE);
  483. b = FALSE; // rename is not implemented
  484. if (((LPNMTVDISPINFO)lParam)->item.pszText) {
  485. if (KeyTreeRenameItem (
  486. ((LPNMTVDISPINFO)lParam)->item.hItem,
  487. ((LPNMTVDISPINFO)lParam)->item.pszText
  488. ))
  489. {
  490. pUpdateFileStatus (hdlg, TRUE);
  491. if (b) {
  492. if (!KeyTreeCreateEmptyKey (((LPNMTVDISPINFO)lParam)->item.hItem)) {
  493. KeyTreeDeleteItem (((LPNMTVDISPINFO)lParam)->item.hItem);
  494. }
  495. }
  496. }
  497. } else if (b) {
  498. //
  499. // for some reason the label edit failed, and this is a new
  500. // item (meaning we need a label)
  501. //
  502. AlertBadNewItemName (g_hSelItem, "New keys must have name");
  503. }
  504. break;
  505. case NM_RCLICK:
  506. KeyTreeSelectRClickItem ();
  507. KeyTreeRightClick (hdlg, g_hSelItem);
  508. break;
  509. default:
  510. return DefWindowProc(hdlg, uMsg, wParam, lParam);
  511. }
  512. } else if (IsDataList (((LPNMHDR)lParam)->hwndFrom)) {
  513. switch (((LPNMHDR)lParam)->code) {
  514. case NM_RCLICK:
  515. if (g_hSelItem) {
  516. DataListRightClick (hdlg, ((LPNMITEMACTIVATE)lParam)->ptAction);
  517. }
  518. break;
  519. case NM_DBLCLK:
  520. if (g_hSelItem) {
  521. DataListDblClick (
  522. hdlg,
  523. ((LPNMITEMACTIVATE)lParam)->iItem,
  524. ((LPNMITEMACTIVATE)lParam)->iSubItem
  525. );
  526. }
  527. break;
  528. default:
  529. return DefWindowProc(hdlg, uMsg, wParam, lParam);
  530. }
  531. }
  532. break;
  533. case WM_CAPTURECHANGED:
  534. if (InDrag) {
  535. //
  536. // something grabbed the capture away from us while
  537. // we were dragging, so stop dragging without taking
  538. // any action.
  539. //
  540. InDrag = FALSE;
  541. KeyTreeEndDrag (FALSE, NULL);
  542. }
  543. break;
  544. case WM_MOUSEMOVE:
  545. if (InDrag) {
  546. KeyTreeMoveDrag (MAKEPOINTS(lParam));
  547. }
  548. break;
  549. case WM_LBUTTONUP:
  550. if (InDrag) {
  551. InDrag = FALSE;
  552. if (KeyTreeEndDrag (TRUE, &MAKEPOINTS(lParam))) {
  553. pUpdateFileStatus (hdlg, TRUE);
  554. }
  555. }
  556. break;
  557. case WM_FILE_LOAD:
  558. if (pVerifyClose () && pLoadFile ((LPSTR)wParam)) {
  559. pRefreshView (hdlg);
  560. pUpdateFileStatus (hdlg, FALSE);
  561. }
  562. break;
  563. case WM_QUIT_CHECK:
  564. *((PBOOL)wParam) = pVerifyClose ();
  565. break;
  566. case WM_SELECT_KEY:
  567. KeyTreeSelectKey ((UINT)wParam);
  568. break;
  569. case WM_DESTROY:
  570. KeyTreeDestroy ();
  571. break;
  572. default:
  573. return DefWindowProc(hdlg, uMsg, wParam, lParam);
  574. }
  575. return FALSE;
  576. }