home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CNRDTL.ZIP / CNRDTL.C < prev    next >
C/C++ Source or Header  |  1992-09-18  |  12KB  |  302 lines

  1. // cnrdtl.c -- Sample program to demonstrate container detail
  2.  
  3. //--------------------------------------------------------------
  4. //  cnrdtl.c
  5. //
  6. //      Sample program to demonstrate container detail view
  7. //
  8. //  By: Guy Scharf                      (415) 948-9186
  9. //      Software Architects, Inc.       FAX: (415) 948-1620
  10. //      2163 Jardin Drive               
  11. //      Mountain View, CA   94040       
  12. //      CompuServe: 76702,557
  13. //      Internet: 76702.557@compuserve.com
  14. //  (c) Copyright 1992 Software Architects, Inc.  
  15. //
  16. //      All Rights Reserved.
  17. //
  18. //  Software Architects, Inc. develops software products for 
  19. //  independent software vendors using OS/2 and Presentation 
  20. //  Manager.
  21. //--------------------------------------------------------------
  22.  
  23. #define INCL_PM                         // Basic OS/2 PM
  24. #define INCL_BASE                       // components
  25. #include <OS2.H>
  26.  
  27. #include <stdlib.h>                     // C runtime library
  28. #include <stddef.h>
  29. #include <string.h>
  30.  
  31. #include "cnrdtl.h"                     // Container demo defs
  32. #include "cnfunc.h"                     // Container utilities
  33.  
  34.  
  35. //  Sample data
  36.  
  37. typedef struct
  38. {
  39.     SHORT   mm, dd, yy;                 // General date storage
  40. } OURDATE, *POURDATE;
  41.  
  42. typedef struct
  43. {
  44.     PSZ     pszState;                   // Name of state
  45.     PSZ     pszCapital;                 // Name of city
  46.     ULONG   ulPopulation;               // '80 census population
  47.     OURDATE ourdateAdm;                 // Date admitted
  48. }  STATEREC, *PSTATEREC;
  49.  
  50. STATEREC statedata[] =
  51. {
  52.     {"Alabama",     "Montgomery",   3893888, {12, 14, 1819}},
  53.     {"Alaska",      "Juneau",        401851, { 1,  3, 1959}},
  54.     {"Arizona",     "Phoenix",      2718425, { 2,  4, 1912}},
  55.     {"Arkansas",    "Little Rock",  2286435, { 6, 15, 1836}},
  56.     {"California",  "Sacramento",  23667565, { 9,  9, 1850}},
  57.     {"Colorado",    "Denver",       2889735, { 8,  1, 1876}},
  58.     {"Connecticut", "Hartford",     3107576, { 1,  9, 1788}},
  59.     {"Delaware",    "Dover",         594317, {12,  7, 1787}},
  60.     {"Florida",     "Tallahassee",  9746342, { 3,  3, 1845}},
  61.     {"Georgia",     "Atlanta",      5463105, { 1,  2, 1788}},
  62.     {"Hawaii",      "Honolulu",      964691, { 8, 21, 1959}}
  63. };
  64.  
  65. typedef struct                          // Container data record
  66. {
  67.     MINIRECORDCORE  RecordCore;         // MINIRECORDCORE structure
  68.     PSZ         pszCapital;             // Capital city
  69.     ULONG       ulPopulation;           // Population
  70.     CDATE       cdateAdmitted;          // Date admitted
  71. } RECORD, *PRECORD;
  72.  
  73. COLDESC cdState[] =
  74. {
  75.   {offsetof(RECORD, RecordCore.pszIcon), CFA_STRING | CFA_FIREADONLY |
  76.      CFA_HORZSEPARATOR | CFA_LEFT, IDS_HEAD_STATE, 
  77.      CFA_LEFT, 0, NULL},
  78.   {offsetof(RECORD, pszCapital), CFA_STRING | CFA_FIREADONLY |
  79.      CFA_HORZSEPARATOR | CFA_LEFT, IDS_HEAD_CAP, 
  80.      CFA_LEFT, 0, NULL},
  81.   {offsetof(RECORD, ulPopulation), CFA_ULONG | CFA_FIREADONLY | 
  82.      CFA_HORZSEPARATOR | CFA_RIGHT, IDS_HEAD_POP, 
  83.      CFA_RIGHT, 0, NULL},
  84.   {offsetof(RECORD, cdateAdmitted), CFA_DATE | CFA_FIREADONLY | 
  85.      CFA_HORZSEPARATOR | CFA_RIGHT, IDS_HEAD_ADM, 
  86.      CFA_RIGHT, 0, NULL}
  87. };
  88.  
  89. //  Prototypes of procedures
  90.  
  91. static MRESULT EXPENTRY ExampleDlgProc (HWND, MSGID, MPARAM,
  92.                                         MPARAM);
  93.                                         
  94. static MRESULT InitContainer (          // Initialize slider
  95. HWND    hwndDlg,                        // I - Dialog window
  96. USHORT  idContainer,                    // I - Container id
  97. USHORT  cStates,                        // I - Number of states
  98. PSTATEREC pastate);                     // I - State data array
  99.  
  100. //--------------------------------------------------------------
  101. //                                                                          
  102. //  Main program to drive container example
  103. //                                                                          
  104. //--------------------------------------------------------------
  105.  
  106. int main (void)
  107. {
  108.     HAB     hab;                        // Handle to anchor blk
  109.     HMQ     hmqMsgQueue;                // Handle to msg queue
  110. #ifndef OS220
  111.     HMODULE hmodContainer;              // Handle to cntr module
  112. #endif
  113.  
  114.     hab = WinInitialize (0);            // Initialize PM
  115.  
  116.     hmqMsgQueue = WinCreateMsgQueue (hab, 0);// Create msg queue
  117.  
  118. #ifndef OS220
  119.     if (DosLoadModule (NULL, 0, CCL_CONTAINER_DLL, 
  120.                        &hmodContainer))
  121.         return FALSE;
  122. #endif
  123.  
  124.     WinDlgBox (HWND_DESKTOP, HWND_DESKTOP, ExampleDlgProc, 0,
  125.                IDLG_EXAMPLE, NULL);
  126.  
  127. #ifndef OS220
  128.     DosFreeModule (hmodContainer);
  129. #endif
  130.  
  131.     WinDestroyMsgQueue (hmqMsgQueue);   // Shutdown
  132.     WinTerminate       (hab);
  133.     return 0;
  134. }
  135.  
  136. //--------------------------------------------------------------
  137. //                                                                          
  138. //  ExampleDlgProc() -- Show state info
  139. //                                                                          
  140. //--------------------------------------------------------------
  141.  
  142. static MRESULT EXPENTRY ExampleDlgProc (
  143. HWND    hwndDlg,
  144. MSGID   msg,
  145. MPARAM  mp1,
  146. MPARAM  mp2)
  147. {
  148.     switch(msg)
  149.     {
  150.         //------------------------------------------------------
  151.         //  Initialize dialog by defining the details view
  152.         //  in the container, loading records, etc.
  153.         //------------------------------------------------------
  154.         case WM_INITDLG:
  155.  
  156.             CnCreateDetailsView (WinWindowFromID (hwndDlg, 
  157.                                                 IDCN_STATEINFO),
  158.                               sizeof cdState / sizeof (COLDESC),
  159.                               cdState, 
  160.                               0,
  161.                               33,
  162.                               IDS_TITLE,
  163.                               0);
  164.                                                                
  165.             //--------------------------------------------------
  166.             //  Load the container
  167.             //--------------------------------------------------
  168.             InitContainer (hwndDlg, IDCN_STATEINFO,
  169.                            sizeof statedata / sizeof (STATEREC),
  170.                            statedata);
  171.             return 0;
  172.  
  173.         //------------------------------------------------------
  174.         //  Process pushbuttons.  They both just quit dialog.
  175.         //------------------------------------------------------
  176.         case WM_COMMAND:
  177.             switch (SHORT1FROMMP(mp1))
  178.             {
  179.                                         // Cancel pressed
  180.                                         // Dismiss dialog
  181.                 case DID_CANCEL:
  182.                     WinDismissDlg (hwndDlg, FALSE);
  183.                     return 0;
  184.  
  185.                                         // OK button pressed
  186.                 case DID_OK:            // We're done
  187.                     WinDismissDlg (hwndDlg, TRUE);
  188.                     return 0;
  189.             }
  190.             return 0;
  191.         
  192.         //------------------------------------------------------
  193.         //  Recover memory allocated for the container
  194.         //------------------------------------------------------
  195.         case WM_DESTROY:
  196.             CnDestroyDetailsView (WinWindowFromID (hwndDlg, 
  197.                                                 IDCN_STATEINFO),
  198.                               sizeof cdState / sizeof (COLDESC),
  199.                               cdState);
  200.             return 0;
  201.                                    
  202.         //------------------------------------------------------
  203.         //  All other messages go to default window procedure
  204.         //------------------------------------------------------
  205.         default:
  206.             return (WinDefDlgProc (hwndDlg, msg, mp1, mp2));
  207.     }
  208.     return FALSE;
  209. }
  210.  
  211.  
  212.  
  213. //--------------------------------------------------------------
  214. // Function: InitContainer
  215. // Outputs:  none                                               
  216. //                                                              
  217. // This function loads the container with all of the state data.
  218. // See the article for a complete discussion of this function.
  219. //--------------------------------------------------------------
  220.  
  221. static MRESULT InitContainer (          // Initialize slider
  222. HWND    hwndDlg,                        // I - Dialog window
  223. USHORT  idContainer,                    // I - Container id
  224. USHORT  cStates,                        // I - Number of states
  225. PSTATEREC pastate)                      // I - State data array
  226. {
  227.     USHORT      i;                      // Loop counter
  228.     ULONG       cbExtraBytes;           // Extra bytes in record structure */
  229.  
  230.     PRECORD     precord;                // -> container records
  231.     PRECORD     precordFirst;           // -> first container record
  232.  
  233.     RECORDINSERT recordInsert;          // Record insertion control
  234.  
  235.     //----------------------------------------------------------
  236.     //  Allocate memory for all user records
  237.     //----------------------------------------------------------
  238.  
  239.     cbExtraBytes = (ULONG)(sizeof(RECORD) - 
  240.                                          sizeof(MINIRECORDCORE));
  241.     precord = (PRECORD) WinSendDlgItemMsg (hwndDlg, idContainer,
  242.                                   CM_ALLOCRECORD,
  243.                                   MPFROMLONG (cbExtraBytes),
  244.                                   MPFROMSHORT (cStates));
  245.     precordFirst = precord;
  246.                                       
  247.     //----------------------------------------------------------
  248.     //  Initialize all records
  249.     //----------------------------------------------------------
  250.  
  251.     for (i = 0; i < cStates; i++, pastate++)
  252.     {
  253.         //------------------------------------------------------
  254.         //   Initialize the container record control structure
  255.         //------------------------------------------------------
  256.         precord->RecordCore.cb        = sizeof (MINIRECORDCORE);
  257.  
  258.         //------------------------------------------------------
  259.         //  Copy our data to the container record control struct
  260.         //------------------------------------------------------
  261.         precord->RecordCore.pszIcon  = pastate->pszState;
  262.         precord->pszCapital          = pastate->pszCapital;
  263.         precord->ulPopulation        = pastate->ulPopulation;
  264.         precord->cdateAdmitted.month = pastate->ourdateAdm.mm;
  265.         precord->cdateAdmitted.day   = pastate->ourdateAdm.dd;
  266.         precord->cdateAdmitted.year  = pastate->ourdateAdm.yy;
  267.  
  268.                                         // Move to next record
  269.         precord = (PRECORD) precord->RecordCore.preccNextRecord;
  270.     }
  271.  
  272.     //----------------------------------------------------------
  273.     //  Construct record insertion control structure
  274.     //----------------------------------------------------------
  275.  
  276.     memset (&recordInsert, 0, sizeof (RECORDINSERT));
  277.     recordInsert.cb                 = sizeof (RECORDINSERT);
  278.     recordInsert.pRecordParent      = NULL;
  279.     recordInsert.pRecordOrder       = (PRECORDCORE)((ULONG)
  280.                                           MPFROMSHORT(CMA_END));
  281.     recordInsert.zOrder             = (USHORT) CMA_TOP;
  282.     recordInsert.cRecordsInsert     = cStates;
  283.     recordInsert.fInvalidateRecord  = TRUE;
  284.  
  285.     //----------------------------------------------------------
  286.     //  Insert the records
  287.     //----------------------------------------------------------
  288.  
  289.     WinSendDlgItemMsg (hwndDlg, idContainer,
  290.                        CM_INSERTRECORD,
  291.                        MPFROMP (precordFirst),
  292.                        MPFROMP (&recordInsert));
  293.  
  294.     //----------------------------------------------------------
  295.     //  Return to caller
  296.     //----------------------------------------------------------
  297.  
  298.     return 0;
  299. }
  300.  
  301.  
  302.