home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Graphics / sKulpt / skulpt-src / ResLoad.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-26  |  13.5 KB  |  517 lines

  1. #define STRICT
  2.  
  3. // Includes standard Windows
  4. #include <windows.h>
  5. #include <windowsx.h>
  6. #include <time.h>
  7. #include <stdlib.h>
  8. #include <malloc.h>
  9. #include <memory.h>
  10. #include <stdio.h>
  11.  
  12. // Includes D3D
  13. #define  D3D_OVERLOADS
  14. #include <ddraw.h>
  15. #include <d3d.h>
  16. #include <d3dx.h>
  17.  
  18. // Includes utilitaires D3D
  19. #include "d3dmath.h"
  20. #include "d3dutil.h"
  21. #include "D3DEnum.h"
  22.  
  23. // Ids Resources
  24. #include "resource.h"
  25.  
  26. // Constantes
  27. #include "const.h"
  28.  
  29. // Types
  30. #include "types.h"
  31.  
  32. // Variables globales projet
  33. #include "vars.h"
  34.  
  35. // Prototypes fonctions autres modules
  36. #include "proto.h"
  37.  
  38. // Macros
  39. #include "macros.h"
  40.  
  41. #define RT_CURSOR       1
  42. #define RT_BITMAP       2
  43. #define RT_ICON         3
  44. #define RT_MENU         4
  45. #define RT_DIALOG       5
  46. #define RT_STRING       6
  47. #define RT_FONTDIR      7
  48. #define RT_FONT         8
  49. #define RT_ACCELERATOR  9
  50. #define RT_RCDATA       10
  51. #define RT_MESSAGETABLE 11
  52. #define RT_GROUP_CURSOR 12
  53. #define RT_GROUP_ICON   14
  54. #define RT_VERSION      16
  55. #define RT_DLGINCLUDE   17
  56. #define RT_PLUGPLAY     19
  57. #define RT_VXD          20
  58. #define RT_ANICURSOR    21
  59. #define RT_ANIICON      22
  60. #define RT_HTML         23
  61.  
  62. static const XDC_IS_ORDINAL = 0;
  63. static const XDC_IS_NAME    = 1;
  64. #define XDC_C_FACTOR  3/2
  65.  
  66. typedef union 
  67. {
  68.     UWORD     wOrd;
  69.     char    szName[80];
  70. } OrdinalOrName;
  71.  
  72. typedef struct
  73. {
  74.     DWORD dwDataSize;
  75.     DWORD dwHeaderSize;
  76.     DWORD dwDataVersion;
  77.     UWORD wMemoryFlags;
  78.     UWORD wLanguageId;
  79.     DWORD dwVersion;  
  80.     DWORD dwCharacteristics; 
  81.     LPCTSTR lpType;
  82.     LPCTSTR lpName;
  83.     UINT  uResTypeTag;
  84.     OrdinalOrName ResType;
  85.     UINT  uResNameTag;
  86.     OrdinalOrName ResName;
  87.     void    *Data;
  88. }  ResEntry;
  89.  
  90. // Globales
  91. static unsigned char *amsResBasePtr;   // Base des ressources
  92. static unsigned long amsResSize;       // Tailles des ressources
  93.  
  94. Window *CreateWindow(ULONG x, ULONG y, ULONG largeur, ULONG hauteur, char *titre)
  95. {
  96.     Gadget *gadtemp, **gadlist = &gadtemp;
  97.  
  98.     CreateContext(gadlist);
  99.  
  100.     return OpenWindowTags(NULL,
  101.                 WA_Left,        x,
  102.                 WA_Top,         y,
  103.                 WA_Width,       largeur * XDC_C_FACTOR + hInst->WBorLeft + hInst->WBorRight,
  104.                 WA_Height,      hauteur * XDC_C_FACTOR + hInst->WBorTop + hInst->BarHeight + hInst->WBorBottom,
  105.                 WA_Title,       titre,
  106.                 WA_Gadgets,     gadtemp,
  107.                 WA_CustomScreen,(ULONG) hInst,
  108.                 WA_DepthGadget, TRUE,
  109.                 WA_CloseGadget, TRUE,
  110.                 WA_DragBar,     TRUE,
  111.                 WA_IDCMP,       IDCMP_CLOSEWINDOW);
  112. }
  113.  
  114. void AddGad(Window *win, ULONG type, ULONG x, ULONG y, ULONG largeur, ULONG hauteur, char *label)
  115. {
  116.     struct Gadget *gad, *temp;
  117.     static struct NewGadget gadtemp;
  118.     char *text = (char *)malloc(strlen(label) + 1);
  119.     memcpy(text, label, strlen(label) + 1);
  120.  
  121.     gadtemp.ng_LeftEdge = x * XDC_C_FACTOR + win->BorderLeft;
  122.     gadtemp.ng_TopEdge = y * XDC_C_FACTOR + win->WScreen->BarHeight;
  123.     gadtemp.ng_Width = largeur * XDC_C_FACTOR;
  124.     gadtemp.ng_Height = (type == TEXT_KIND) ? 16 : hauteur * XDC_C_FACTOR;
  125.     gadtemp.ng_GadgetText = ((type == CHECKBOX_KIND) /*|| (type == TEXT_KIND)*/) ? NULL : text;
  126.     gadtemp.ng_TextAttr = NULL;
  127.     gadtemp.ng_GadgetID = 0;
  128.     gadtemp.ng_Flags = PLACETEXT_IN;
  129.     gadtemp.ng_VisualInfo = GetVisualInfoA(win->WScreen,NULL);
  130.     gadtemp.ng_UserData = 0;
  131.  
  132.     temp = win->FirstGadget;
  133.     while(temp->NextGadget)
  134.         temp = temp->NextGadget;
  135.  
  136.     switch(type)
  137.     {
  138.         case BUTTON_KIND:
  139.             gad = CreateGadget(type, temp, &gadtemp, TAG_DONE);
  140.             break;
  141.         case TEXT_KIND:
  142.             gad = CreateGadget(type, temp, &gadtemp,/* GTTX_Text, text, GTTX_Border, FALSE, GTTX_Justification, GTJ_LEFT, GTTX_Clipped, TRUE,*/ TAG_DONE);
  143.             break;
  144.         case STRING_KIND:
  145.             gad = CreateGadget(type, temp, &gadtemp, GTST_String, label, TAG_DONE);
  146.             break;
  147.         case SCROLLER_KIND:
  148.             gad = CreateGadget(type, temp, &gadtemp, GTSC_Total, 200, TAG_DONE);
  149.             break;
  150.         case CHECKBOX_KIND:
  151.             gad = CreateGadget(type, temp, &gadtemp, GTCB_Scaled, TRUE, TAG_DONE);
  152.             break;
  153.  
  154.         default:
  155.             gad = CreateGadget(type, temp, &gadtemp, TAG_DONE);
  156.     }
  157. }
  158.  
  159. BOOL DLGInit(char *sResFileName)
  160. {
  161.     BPTR hFileLock = Lock(sResFileName, SHARED_LOCK);
  162.  
  163.     if(hFileLock != NULL)
  164.     {
  165.         struct FileInfoBlock sFib;
  166.         BOOL bOk = FALSE;
  167.  
  168.         if(Examine(hFileLock,&sFib) && sFib.fib_DirEntryType < 0 && sFib.fib_Size > 0)
  169.         {
  170.             amsResSize = sFib.fib_Size;
  171.             amsResBasePtr = (unsigned char *) AllocVec(amsResSize,MEMF_ANY|MEMF_PUBLIC);
  172.             if(amsResBasePtr != NULL)
  173.             {
  174.                 BPTR hFileHandle;
  175.  
  176.                 hFileHandle = Open(sResFileName,MODE_OLDFILE);
  177.                 if(hFileHandle != NULL)
  178.                 {
  179.                     if(Read(hFileHandle,amsResBasePtr, amsResSize) == amsResSize)
  180.                         bOk = TRUE;
  181.  
  182.                     Close(hFileHandle);
  183.                 }
  184.             }
  185.         }
  186.         UnLock(hFileLock);
  187.  
  188.         if(!bOk)
  189.         {
  190.             FreeVec(amsResBasePtr);
  191.             amsResBasePtr = NULL;
  192.             amsResSize = 0;
  193.             return FALSE;
  194.         }
  195.         return TRUE;
  196.     }
  197.     else
  198.         return FALSE;
  199. }
  200.  
  201. void DLGClose(void)
  202. {
  203.     if (amsResBasePtr)
  204.     {
  205.         FreeVec( amsResBasePtr );
  206.         amsResBasePtr = NULL;
  207.     }
  208. }
  209.  
  210. static BOOL IsIntel(void)
  211. {
  212.     static long IsIntel = 1;
  213.  
  214.     if( *((char *)&IsIntel) == 1 )    // Intel
  215.         return TRUE;
  216.     return FALSE;                    // Motorola
  217. }
  218.  
  219. static UWORD IntelWord(UWORD *addr)
  220. {
  221.     unsigned char B1, B2;
  222.  
  223.     if( IsIntel() )
  224.         return *addr;
  225.  
  226.     B1 = *((unsigned char *) addr );
  227.     B2 = *(((unsigned char *) addr ) + 1);
  228.     return (UWORD) B1 | ((UWORD) B2 << 8);
  229. }
  230. static DWORD IntelLong(DWORD *addr)
  231. {
  232.     UWORD W1, W2;
  233.  
  234.     if( IsIntel() )
  235.     {
  236.         return *addr;
  237.     }
  238.     else
  239.     {
  240.         W1 = IntelWord( (UWORD*) addr );
  241.         W2 = IntelWord( ((UWORD*) addr) + 1 );
  242.         return (ULONG) W1 | ((ULONG) W2 << 16);
  243.     }
  244. }
  245.  
  246. // Lit un objet encodé sous la forme name ou ordinal
  247. static int ReadNameOrOrdinal( unsigned char **pos, UWORD *wOrdinal, char *Name )
  248. {
  249.     unsigned char *carPtr;
  250.  
  251.     carPtr = *pos;
  252.  
  253.     if( (carPtr[0] == 0xFF) && ( carPtr[1] == 0xFF) )
  254.     {
  255.         // C'est un ordinal (UWORD suivant 0xFFFF)
  256.         *wOrdinal = IntelWord( (UWORD*)&carPtr[2] );
  257.         *pos = *pos + 4;
  258.         return XDC_IS_ORDINAL;
  259.     }
  260.     else
  261.     {
  262.         while( *Name++ = *carPtr ) carPtr += 2;
  263.         
  264.         *pos = carPtr + 2;
  265.         return XDC_IS_NAME;
  266.     }
  267. }
  268.  
  269. static unsigned char *ReadResEntry(unsigned char *pos, ResEntry *res)
  270. {
  271.     DWORD dwTaille;
  272.  
  273.     dwTaille = res->dwDataSize = IntelLong( ((DWORD*) pos)++  );
  274.     res->dwHeaderSize = IntelLong( ((DWORD*) pos)++  );
  275.     res->uResTypeTag = ReadNameOrOrdinal( &pos, &(res->ResType.wOrd), res->ResType.szName  );
  276.     res->uResNameTag = ReadNameOrOrdinal( &pos, &(res->ResName.wOrd), res->ResName.szName );
  277.  
  278.     if( res->uResTypeTag == 0) 
  279.         res->lpType = MAKEINTRESOURCE(res->ResType.wOrd);
  280.     else
  281.         res->lpType = (LPCTSTR)res->ResType.szName;
  282.  
  283.     if( res->uResNameTag == 0) 
  284.         res->lpName = MAKEINTRESOURCE(res->ResName.wOrd);
  285.     else
  286.         res->lpName  = (LPCTSTR)res->ResName.szName;
  287.  
  288.     res->dwDataVersion     = IntelLong( ((DWORD*) pos)++ );
  289.     res->wMemoryFlags      = IntelWord( ((UWORD*) pos)++ );
  290.     res->wLanguageId       = IntelWord( ((UWORD*) pos)++ );
  291.     res->dwVersion         = IntelLong( ((DWORD*) pos)++ );
  292.     res->dwCharacteristics = IntelLong( ((DWORD*) pos)++ );
  293.     res->Data = pos;
  294.  
  295.     // Skip DataSize multiple de 4
  296.     if ((dwTaille & 0x3) != 0) dwTaille = (dwTaille & 0xFFFFFFFC) + 4;
  297.     return pos + dwTaille;
  298. }
  299.  
  300. // Compare deux LPCTSTR
  301. static int CheckLPCT( LPCTSTR lp1, LPCTSTR lp2 )
  302. {
  303.     // On ne peut comparer que des types compatibles
  304.     if( (((long)lp1 & 0xFFFF0000) == 0) && (((long)lp2 & 0xFFFF0000) == 0))
  305.     {
  306.         // Ordinal
  307.         if( (long)lp1 == (long)lp2 ) return -1;
  308.         return 0;
  309.     }
  310.  
  311.     if( (((long)lp1 & 0xFFFF0000) != 0) && (((long)lp2 & 0xFFFF0000) != 0))
  312.     {
  313.         if( strcmp( (char*)lp1, (char*)lp2 ) == 0 ) return -1;
  314.         return 0;
  315.     }
  316.  
  317.     return 0;
  318. }
  319.  
  320. // Récupère une WSTRING dans la ressource et en fait une string non unicode
  321. static unsigned char *myGetString( unsigned char *ucChar, char *szString )
  322. {
  323.     while( *szString++ = *ucChar  ) ucChar += 2;
  324.  
  325.     ucChar += 2;
  326.  
  327.     return ucChar;
  328. }
  329.  
  330. // Appelé en boucle pour chaque item de la structure de la dialog pour ajouter
  331. // l'item à la fenêtre
  332. static unsigned char *myLoadDialogItem(HWND hWnd, unsigned char *ucData )
  333. {
  334.     DWORD    dwHelpID;
  335.     DWORD    dwStyle;
  336.     DWORD    dwExtendedStyle; 
  337.     UWORD     wID;
  338.     short    x, y, cx, cy; 
  339.     UWORD     wExtraBytes;
  340.     UWORD     wTagNom, wTagClasse;
  341.     OrdinalOrName leNom;
  342.     OrdinalOrName laClasse;
  343.  
  344.     // DialogItem est alignée sur un DWORD
  345.     if( (long)ucData & 3 ) 
  346.         ucData = (unsigned char*) (((long)ucData & 0xFFFFFFFC) + 4);
  347.  
  348.     // Lecture des champs
  349.     dwHelpID        = IntelLong( ((DWORD*) ucData)++ );
  350.     dwExtendedStyle = IntelLong( ((DWORD*) ucData)++ );
  351.     dwStyle            = IntelLong( ((DWORD*) ucData)++ );
  352.     x                = IntelWord( ((UWORD*)  ucData)++ );
  353.     y                = IntelWord( ((UWORD*)  ucData)++ );
  354.     cx                = IntelWord( ((UWORD*)  ucData)++ );
  355.     cy                = IntelWord( ((UWORD*)  ucData)++ );
  356.     wID             = IntelWord( ((UWORD*)  ucData)++ );
  357.  
  358.     ucData+=2;
  359.  
  360.     wTagClasse = ReadNameOrOrdinal( &ucData, &(laClasse.wOrd), laClasse.szName  );
  361.     wTagNom = ReadNameOrOrdinal( &ucData, &(leNom.wOrd), leNom.szName  );
  362.     wExtraBytes = IntelWord( ((UWORD*)ucData)++ );
  363.  
  364.     ULONG aType;
  365.     switch( laClasse.wOrd )
  366.     {
  367.         case 0x0080:
  368.             switch(dwStyle & 0xF)
  369.             {
  370.                 case BS_CHECKBOX:
  371.                 case BS_AUTOCHECKBOX:
  372.                     aType = CHECKBOX_KIND;  break;
  373.                 case BS_RADIOBUTTON:
  374.                 case BS_AUTORADIOBUTTON:
  375.                     aType = MX_KIND;        break;
  376.                 case BS_GROUPBOX:
  377.                     aType = TEXT_KIND;      break;
  378.                 default:
  379.                     aType = BUTTON_KIND;    break;
  380.             }
  381.             break;
  382.         case 0x0081: aType = STRING_KIND;   break;
  383.         case 0x0082: aType = TEXT_KIND;     break;
  384.         case 0x0083: aType = LISTVIEW_KIND; break;
  385.         case 0x0084: aType = SCROLLER_KIND; break;
  386.         case 0x0085: aType = CYCLE_KIND;    break;
  387.         default : aType = ~0; break;
  388.     }
  389.     if (aType != ~0)
  390.         AddGad(hWnd, aType, x, y, cx, cy, leNom.szName);
  391.  
  392.     return ucData + wExtraBytes;
  393. }
  394.  
  395. // Charge le dialogbox à partir du pointeur sur les données de la dialog dans les ressources
  396. // (qui a été renseigné par myFindRessource())
  397. void myLoadDialog( unsigned char *ucData )
  398. {
  399.     DWORD    dwStyle;
  400.     DWORD    dwExtendedStyle; 
  401.     UWORD     wNbItem;
  402.     short    x, y, cx, cy; 
  403.     BOOL    bExtended;
  404.     char    szTitle[128], szFont[80];
  405.     UWORD     wTagMenu, wTagClasse;
  406.     OrdinalOrName leMenu;
  407.     OrdinalOrName laClasse;
  408.     HWND hWnd;
  409.  
  410.     // Lire la signature
  411.     if (IntelWord( ((UWORD*)  ucData) + 1 ) == 0xFFFF)
  412.         bExtended = TRUE;
  413.     else
  414.         bExtended = FALSE;
  415.  
  416.     if( bExtended )
  417.     {
  418.         ucData += 8;
  419.         dwExtendedStyle = IntelLong( ((DWORD*) ucData)++ );
  420.         dwStyle            = IntelLong( ((DWORD*) ucData)++ );
  421.     }
  422.     else
  423.     {
  424.         dwStyle            = IntelLong( ((DWORD*) ucData)++ );
  425.         dwExtendedStyle = IntelLong( ((DWORD*) ucData)++ );
  426.     }
  427.  
  428.     wNbItem            = IntelWord( ((UWORD*)  ucData)++ );
  429.     x                = IntelWord( ((UWORD*)  ucData)++ );
  430.     y                = IntelWord( ((UWORD*)  ucData)++ );
  431.     cx                = IntelWord( ((UWORD*)  ucData)++ );
  432.     cy                = IntelWord( ((UWORD*)  ucData)++ );
  433.  
  434.     // Le menu & la Classe
  435.     if( ( wTagMenu = IntelWord( ((UWORD*)ucData)++ )) != 0x0000 )     // Il y a un menu
  436.     {
  437.         if( wTagMenu == 0xFFFF ) leMenu.wOrd = IntelWord( ((UWORD*)ucData)++ );
  438.         else ucData = myGetString( ucData, leMenu.szName );
  439.     }
  440.  
  441.     if( ( wTagClasse = IntelWord( ((UWORD*)ucData)++ )) != 0x0000 )     // Il y a une classe
  442.     {
  443.         if( wTagClasse == 0xFFFF ) leMenu.wOrd = IntelWord( ((UWORD*)ucData)++ );
  444.         else ucData = myGetString( ucData, laClasse.szName );
  445.     }
  446.  
  447.     // Le nom
  448.     ucData = myGetString( ucData, szTitle );
  449.  
  450.     if( (dwStyle & DS_SETFONT) == DS_SETFONT )
  451.     {
  452.         ucData += 6;
  453.         ucData = myGetString( ucData, szFont );
  454.     }
  455.  
  456.     vTrace("Dialog box '%s' @(%d,%d), taille (%d,%d), %d contrôles",szTitle, x, y,cx,cy, wNbItem);
  457.  
  458.     hWnd = CreateWindow(x, y, cx, cy, szTitle);
  459.  
  460.     // Lecture des Items
  461.     while( wNbItem-- )
  462.         ucData = myLoadDialogItem(hWnd, ucData );
  463.  
  464.     // Rafraîchir les gadgets
  465.     RefreshGadgets(hWnd->FirstGadget, hWnd, NULL);
  466.  
  467.     // Attendre un message sur la fenêtre
  468.     while(((IntuiMessage *)WaitPort(hWnd->UserPort))->Class != IDCMP_CLOSEWINDOW)
  469.         GT_ReplyIMsg(GT_GetIMsg(hWnd->UserPort));
  470.  
  471.     // Fermer la fenêtre
  472.     CloseWindow(hWnd);
  473. }
  474.  
  475. // Trouve une ressource dans le fichier de ressource en fonction de son nom et de son type
  476. static BOOL myFindResource( LPCTSTR lpTemplate, LPCTSTR lpType, ResEntry *res )
  477. {
  478.     unsigned char *ucBasePtr, *ucFin;
  479.     
  480.     ucBasePtr = amsResBasePtr;
  481.     ucFin = ucBasePtr + amsResSize;
  482.  
  483.     while(ucBasePtr < ucFin)
  484.     {
  485.         ucBasePtr = ReadResEntry( ucBasePtr, res );
  486.  
  487.         // Est-ce le bon type de ressource et la bonne ressource
  488.         if( CheckLPCT( lpType, res->lpType ) &&
  489.                 CheckLPCT( lpTemplate, res->lpName ) ) 
  490.                     return TRUE;
  491.     }
  492.  
  493.     return FALSE;
  494. }
  495.  
  496. int DialogBox(HINSTANCE hInst, LPCTSTR sRes, HWND hWnd, DLGPROC vVoid)
  497. {
  498.     ResEntry res;
  499.     vTrace("Chargement dialog %d", sRes);
  500.     if( myFindResource( sRes , (LPCTSTR) RT_DIALOG, &res) )
  501.         myLoadDialog( (unsigned char *) res.Data );
  502.  
  503.     return NULL;
  504. }
  505.  
  506. int DialogBoxParam(HINSTANCE hInst, LPCTSTR sRes, HWND hWnd, DLGPROC vVoid, LPARAM lParam)
  507. {
  508.     ResEntry res;
  509.     vTrace("Chargement dialog %d (param. %ld)", sRes, lParam);
  510.     if( myFindResource( sRes , (LPCTSTR) RT_DIALOG, &res) )
  511.         myLoadDialog( (unsigned char *) res.Data );
  512.  
  513.     return NULL;
  514. }
  515.  
  516.  
  517.