home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / winnt / dlgedit / reslist.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  8KB  |  323 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. /****************************** Module Header *******************************
  13. * Module Name: reslist.c
  14. *
  15. * Contains routines to manage the linked list of resources.
  16. *
  17. * Functions:
  18. *   FindDialog()
  19. *   AllocResLink()
  20. *   RestoreDialog()
  21. *   FreeRes()
  22. *   FreeResList()
  23. *   FreeResLink()
  24. *   DeleteDialogResource()
  25. *
  26. * Comments:
  27. *
  28. ****************************************************************************/
  29.  
  30. #include "dlgedit.h"
  31. #include "dlgfuncs.h"
  32. #include "dlgextrn.h"
  33.  
  34. #include <stdlib.h>
  35. #include <string.h>
  36.  
  37.  
  38.  
  39. /************************************************************************
  40. * FindDialog
  41. *
  42. * This function steps through the linked list of resources looking
  43. * for the dialog resource with the given name.  This name can be either
  44. * a string or an ordinal.  Strings are compared without regard to case.
  45. *
  46. * When looking at the dialog names in the resources, if the dialog
  47. * resource is the current one, it will compare the current name for the
  48. * dialog instead of the name that is stored in the resource.  This is
  49. * because the name in the resource could be out of date with respect
  50. * to the current name of the dialog being edited.  This happens when
  51. * the user changes the dialog's name, but has not yet done an action
  52. * that causes the resource to be synched, such as a File/Save, for
  53. * instance.
  54. *
  55. * Arguments:
  56. *   LPTSTR pszDlgName    - Name or ordinal of the dialog to find.
  57. *
  58. * Returns:
  59. *   TRUE if a dialog with that name is found, FALSE if not.
  60. *
  61. ************************************************************************/
  62.  
  63. BOOL FindDialog(
  64.     LPTSTR pszDlgName)
  65. {
  66.     PRESLINK prl;
  67.  
  68.     if (gfEditingDlg && NameOrdCmp(gcd.pszDlgName, pszDlgName) == 0)
  69.         return TRUE;
  70.  
  71.     for (prl = gprlHead; prl; prl = prl->prlNext) {
  72.         /*
  73.          * Is this a dialog resource and do they compare?
  74.          */
  75.         if (prl->fDlgResource && prl != gcd.prl &&
  76.                 NameOrdCmp(prl->pszName, pszDlgName) == 0)
  77.             break;
  78.     }
  79.  
  80.     return prl ? TRUE : FALSE;
  81. }
  82.  
  83.  
  84.  
  85. /************************************************************************
  86. * AllocResLink
  87. *
  88. * This function allocates a new RESLINK structure for the linked list
  89. * of resources.  It allocates local memory for the link, allocates
  90. * and fills global memory with the given resource data and initializes
  91. * the fields of the structure.  The link is not added to the list,
  92. * however.
  93. *
  94. * Returns:
  95. *   A pointer to the newly allocated RESLINK structure, or NULL if
  96. *   an error occurs.
  97. *
  98. ************************************************************************/
  99.  
  100. PRESLINK AllocResLink(
  101.     PRES pRes)
  102. {
  103.     PRESLINK prl;
  104.     PRES pResNew;
  105.     PRES2 pRes2;
  106.     LPTSTR pszName;
  107.     INT cbName;
  108.     LPTSTR pszType;
  109.  
  110.     if (!(prl = (PRESLINK)MyAlloc(sizeof(RESLINK))))
  111.         return NULL;
  112.  
  113.     prl->prlNext = NULL;
  114.  
  115.     prl->cbRes = ResourceSize(pRes);
  116.     if (!(prl->hRes = GlobalAlloc(GMEM_MOVEABLE, prl->cbRes))) {
  117.         MyFree(prl);
  118.         Message(MSG_OUTOFMEMORY);
  119.         return NULL;
  120.     }
  121.  
  122.     pResNew = (PRES)GlobalLock(prl->hRes);
  123.     memcpy(pResNew, pRes, prl->cbRes);
  124.     GlobalUnlock(prl->hRes);
  125.  
  126.     pszType = ResourceType(pRes);
  127.     if (IsOrd(pszType) && OrdID(pszType) == ORDID_RT_DIALOG) {
  128.         prl->fDlgResource = TRUE;
  129.         pszName = ResourceName(pRes);
  130.         cbName = NameOrdLen(pszName);
  131.  
  132.         if (!(prl->pszName = MyAlloc(cbName))) {
  133.             GlobalFree(prl->hRes);
  134.             MyFree(prl);
  135.             return NULL;
  136.         }
  137.  
  138.         NameOrdCpy(prl->pszName, pszName);
  139.  
  140.         pRes2 = ResourcePart2(pRes);
  141.         prl->wLanguage = pRes2->LanguageId;
  142.     }
  143.     else {
  144.         prl->fDlgResource = FALSE;
  145.         prl->pszName = NULL;
  146.         prl->wLanguage = 0;
  147.     }
  148.  
  149.     return prl;
  150. }
  151.  
  152.  
  153.  
  154. /****************************************************************************
  155. * RestoreDialog
  156. *
  157. * This function is used to restore the current dialog to the condition
  158. * that it was in just before it was last chosen to edit.
  159. *
  160. ****************************************************************************/
  161.  
  162. VOID RestoreDialog(VOID)
  163. {
  164.     PRESLINK prlSave;
  165.  
  166.     if (Message(MSG_RESTOREDIALOG) == IDYES) {
  167.         prlSave = gcd.prl;
  168.         DeleteDialog(FALSE);
  169.  
  170.         ResLinkToDialog(prlSave);
  171.     }
  172. }
  173.  
  174.  
  175.  
  176. /****************************************************************************
  177. * FreeRes
  178. *
  179. * This frees the entire list of resources and deletes the dialog box
  180. * being edited.
  181. *
  182. ****************************************************************************/
  183.  
  184. VOID FreeRes(VOID)
  185. {
  186.     CancelSelection(TRUE);
  187.  
  188.     if (gfEditingDlg)
  189.         DeleteDialog(FALSE);
  190.  
  191.     FreeResList();
  192.  
  193.     pszResFile = NULL;
  194.     gfResChged = FALSE;
  195. }
  196.  
  197.  
  198.  
  199. /****************************************************************************
  200. * FreeResList
  201. *
  202. * This function frees the entire resource list.
  203. *
  204. ****************************************************************************/
  205.  
  206. VOID FreeResList(VOID)
  207. {
  208.     PRESLINK prl;
  209.     PRESLINK prlNext;
  210.  
  211.     for (prl = gprlHead; prl; prl = prlNext) {
  212.         prlNext = prl->prlNext;
  213.         FreeResLink(prl);
  214.     }
  215.  
  216.     gprlHead = NULL;
  217. }
  218.  
  219.  
  220.  
  221. /****************************************************************************
  222. * FreeResLink
  223. *
  224. * This frees a linked resource structure and everything that it
  225. * contains.  It does not close up the linked list, however.
  226. *
  227. * Arguments:
  228. *   PRESLINK prl - Points to the resource link to free.
  229. *
  230. ****************************************************************************/
  231.  
  232. VOID FreeResLink(
  233.     PRESLINK prl)
  234. {
  235.     if (prl->pszName)
  236.         MyFree(prl->pszName);
  237.  
  238.     if (prl->hRes)
  239.         GlobalFree(prl->hRes);
  240.  
  241.     MyFree(prl);
  242. }
  243.  
  244.  
  245.  
  246. /************************************************************************
  247. * DeleteDialogResource
  248. *
  249. * This function deletes the current dialog from the linked list of
  250. * resources.  It handles the case where the current dialog is not
  251. * yet in the list.
  252. *
  253. ************************************************************************/
  254.  
  255. VOID DeleteDialogResource(VOID)
  256. {
  257.     PRESLINK prl;
  258.     PRESLINK prlPrev;
  259.  
  260.     /*
  261.      * Does a link for the current dialog exist?
  262.      */
  263.     if (gcd.prl) {
  264.         /*
  265.          * Find the existing link and get it's previous link.
  266.          */
  267.         for (prl = gprlHead, prlPrev = NULL; prl && prl != gcd.prl;
  268.                 prlPrev = prl, prl = prl->prlNext)
  269.             ;
  270.  
  271.         /*
  272.          * Close up the linked list.
  273.          */
  274.         if (prlPrev)
  275.             prlPrev->prlNext = gcd.prl->prlNext;
  276.         else
  277.             gprlHead = gcd.prl->prlNext;
  278.  
  279.         /*
  280.          * Delete the link.
  281.          */
  282.         FreeResLink(gcd.prl);
  283.         gcd.prl = NULL;
  284.     }
  285. }
  286.  
  287.  
  288.  
  289. /****************************************************************************
  290. *
  291. * Stubs for the C runtime international calls that are not implemented yet.
  292. * These should be removed once the C runtime international library
  293. * is dropped to NT.
  294. *
  295. ****************************************************************************/
  296.  
  297. LPWSTR itoaw(
  298.     INT value,
  299.     LPWSTR string,
  300.     INT radix)
  301. {
  302.     CHAR szAnsi[17];
  303.  
  304.     itoa(value, szAnsi, radix);
  305.  
  306.     MultiByteToWideChar(CP_ACP, 0, szAnsi, -1, string, 17);
  307.  
  308.     return string;
  309. }
  310.  
  311.  
  312.  
  313. INT awtoi(
  314.     LPWSTR string)
  315. {
  316.     CHAR szAnsi[17];
  317.     BOOL fDefCharUsed;
  318.  
  319.     WideCharToMultiByte(CP_ACP, 0, string, -1, szAnsi, 17, NULL, &fDefCharUsed);
  320.  
  321.     return atoi(szAnsi);
  322. }
  323.