home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / c / bsscdemo / listdem2 / listdem2.c next >
Encoding:
C/C++ Source or Header  |  1993-05-26  |  22.7 KB  |  548 lines

  1. /*
  2.    Copyright (c) 1993 by Barking Spider Software, Inc.  All rights reserved.
  3.                                                                            
  4.    Filename...:  listdem2.c
  5.    
  6.    Version,,,,:  1.0
  7.    
  8.    Language...:  Microsoft C/C++ 7.0
  9.    
  10.    Model......:  Small
  11.    
  12.    Environment:  Microsoft Windows 3.1
  13.                                                                            
  14.    Description:  This source module demonstrates the BSS List Control as part 
  15.                  of a dialog template.  The dialog box template in listdem2.dlg
  16.                  was created with the Dialog Editor.   The Dialog Editor
  17.                  is shipped with the Windows 3.1 SDK.  Since the BSS List
  18.                  Control has custom control support built in, all that was
  19.                  required to use the tree control in the Dialog Editor was to
  20.                  select the File menu item, then select Open Custom, type in
  21.                  the path for treectrl.dll and hit return.  Select New under
  22.                  the File menu item to get an empty dialog box frame.
  23.                  The Dialog Editor Toolbox will then contain a button
  24.                  representing the tree/list control.  Select this button, drag
  25.                  the mouse over the new dialog box frame and stretch the 
  26.                  custom tree/list control into the desired place.
  27.                  
  28.                  In the sample code below, a simple top level overlapped
  29.                  window is created with a couple of menu items.  From these
  30.                  menu items, the user can create a modal dialog box or the
  31.                  user can exit the application.  If the user selects the
  32.                  menu item List Dialog, a modal dialog box will appear that
  33.                  contains a BSS List Control.
  34.                  
  35.                  The List Control is populated with list items that
  36.                  display an icon and text.  The icon is the infamous BSS
  37.                  Spider icon. 
  38.                  
  39.                  When the user decides to quit the dialog box, hitting the 
  40.                  Enter key, Esc key, or clicking on push button will exit the
  41.                  dialog box.
  42.    Notes......:             
  43.                                                                            
  44.    History....:  
  45.                                                                               
  46.    Author.....:  Peter J. Kaufman
  47. */    
  48.  
  49. #include <windows.h>
  50. #include "listdem2.h"     // Listdem2 function prototypes. 
  51. #include "..\bscore.h"    // BSS List Control structures, notification messages,
  52.                           // error codes...
  53. #include "..\bslist.h"    // BSS List Control function prototypes for the
  54.                           // exported APIs.
  55. #include <string.h>
  56. #include <stdlib.h>
  57. #include "spider.h"       // Static array of strings used to create list items.
  58.  
  59. static char szAppName[] = "ListDem2";
  60.  
  61. HANDLE hInst ;
  62.  
  63.  
  64. /*****************************************************************************
  65. int PASCAL WinMain ( HANDLE hInstance,
  66.                      HANDLE hPrevInstance,
  67.                      LPSTR lpszCmdLine,
  68.                      int nCmdShow);
  69.                      
  70. *****************************************************************************/
  71.  
  72. int PASCAL WinMain ( HANDLE hInstance,
  73.                      HANDLE hPrevInstance,
  74.                      LPSTR lpszCmd,
  75.                      int nCmdShow)
  76. {
  77.    HWND       hwnd ;
  78.    MSG        msg ;
  79.    WNDCLASS   wndclass ;
  80.  
  81.    hInst = hInstance ;
  82.  
  83.    if (!hPrevInstance)
  84.    {
  85.       wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  86.       wndclass.lpfnWndProc   = ListDemo2WndProc ;
  87.       wndclass.cbClsExtra    = 0 ;
  88.       wndclass.cbWndExtra    = 0 ;
  89.       wndclass.hInstance     = hInst;
  90.       wndclass.hIcon         = LoadIcon (hInst,
  91.                                          MAKEINTRESOURCE(IDR_LISTDEMO_ICON)) ;
  92.       wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  93.       wndclass.hbrBackground = COLOR_APPWORKSPACE + 1;
  94.       wndclass.lpszMenuName  = szAppName;
  95.       wndclass.lpszClassName = szAppName;
  96.  
  97.       RegisterClass (&wndclass) ;
  98.    }
  99.  
  100.    hwnd = CreateWindow (szAppName,
  101.                         "Barking Spider Software, Inc. List Demo 2 - Dialog",
  102.                         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  103.                         CW_USEDEFAULT,
  104.                         CW_USEDEFAULT,
  105.                         CW_USEDEFAULT,
  106.                         CW_USEDEFAULT,
  107.                         NULL,
  108.                         NULL,
  109.                         hInstance,
  110.                         NULL);
  111.  
  112.    ShowWindow (hwnd, nCmdShow) ;
  113.    UpdateWindow (hwnd) ;
  114.  
  115.    while (GetMessage (&msg, NULL, 0, 0))
  116.    {
  117.       TranslateMessage (&msg) ;
  118.       DispatchMessage (&msg) ;
  119.    }
  120.    return msg.wParam ;
  121. }
  122.  
  123.  
  124.  
  125. /*****************************************************************************
  126. long FAR PASCAL _export ListDemo2WndProc ( HWND hwnd,
  127.                                            UINT message,
  128.                                            UINT wParam,
  129.                                            LONG lParam)
  130.                      
  131.  This is the window procedure for the top level window of this application.
  132.  When the user selects the menu item List Dialog, a modal dialog box based on
  133.  the dialog box template in listdem2.dlg will be created. 
  134. *****************************************************************************/
  135.  
  136.  
  137. long FAR PASCAL _export ListDemo2WndProc ( HWND hwnd,
  138.                                            UINT message,
  139.                                            UINT wParam,
  140.                                            LONG lParam)
  141. {                                                          
  142.    static FARPROC lpfnListDlgProc ;
  143.    static HANDLE  hInstance ;
  144.  
  145.    switch (message)
  146.    {
  147.       case WM_CREATE:
  148.          hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  149.          lpfnListDlgProc = MakeProcInstance ((FARPROC) ListDlgProc,
  150.                                                        hInstance) ;
  151.          return 0 ;
  152.  
  153.       case WM_COMMAND:
  154.          switch (wParam)
  155.          {
  156.             case IDM_LIST_DIALOG:
  157.                DialogBox (hInstance, 
  158.                           MAKEINTRESOURCE(IDD_DIALOG),
  159.                           hwnd,
  160.                           lpfnListDlgProc);
  161.                return 0;
  162.                
  163.             case IDM_EXIT:
  164.                SendMessage (hwnd, WM_CLOSE, 0, 0L) ;
  165.                return 0;
  166.          }
  167.          break ;
  168.  
  169.       case WM_DESTROY :
  170.          PostQuitMessage (0) ;
  171.          return 0 ;
  172.    }
  173.    return DefWindowProc (hwnd, message, wParam, lParam) ;
  174. }
  175.     
  176.  
  177.  
  178.  
  179. /*****************************************************************************
  180. BOOL FAR PASCAL _export ListDlgProc ( HWND hwnd,
  181.                                       UINT message,
  182.                                       UINT wParam,
  183.                                       LONG lParam);
  184.                                       
  185.  
  186.   This is the dialog box procedure for the dialog box template defined in 
  187.   listdem2.dlg.  It manages the tree control, the edit controls, and the
  188.   push button.  The dialog boxes two primary functions are:
  189.   
  190.     1) Demonstrate the list control in a dialog box.
  191.     2) Allow the user to "play" with the list control.
  192.   
  193.   In WM_INITDIALOG, the BSS Spider icon is loaded, the bitmap spaces are
  194.   defined, the space between the text and the bitmap is defined, and the
  195.   list items are added to the list.
  196.   
  197.   When the user is tire of playing with the list control, pressing the push
  198.   button will end the fun.
  199. ******************************************************************************/
  200.  
  201. HWND    hwndList;    // Tree control handle
  202. HICON   hSpiderIcon;
  203.  
  204. #define MAX_LIST_ITEMS (sizeof(szSpider)/sizeof(char *))
  205.  
  206. #define SPACE_BEFORE_TEXT       4
  207.  
  208.                     
  209. BOOL FAR PASCAL _export ListDlgProc ( HWND hwnd,
  210.                                       UINT message,
  211.                                       UINT wParam,
  212.                                       LONG lParam)
  213. {
  214.    LP_TREE_NODE_DEF   lpTreeNodeDef;   // Pointer to list node definition
  215.    LP_TREE_NODE_DEF   lpTreeNodeDef2;
  216.    LP_SELECT_NOTIF    lpSelectNotif;   // Pointer to the notification struct.
  217.    WORD               i;
  218.    WORD               wErrCode;
  219.    WORD               wMaxListItems;
  220.  
  221.    switch (message)
  222.    {
  223.       case WM_INITDIALOG:
  224.       
  225.          // Get the handle of the list control
  226.          hwndList = GetDlgItem (hwnd, IDC_LIST);
  227.          
  228.          
  229.          BSL_SetXSpaceBeforeText( hwndList, SPACE_BEFORE_TEXT );
  230.          
  231.          // Bitmaps spaces are regions where bitmaps are displayed for the
  232.          // list item.  Bitmap spaces encompass the bitmaps or icons.
  233.          // The bitmap spaces are placed left of the text.  
  234.          
  235.          // From left to right the left most bitmap space has an index
  236.          // value of 0, the next bitmap space has an index value of 1,
  237.          // and so on.
  238.          // When a bitmap space is defined, it is defined for the entire
  239.          // list, for all list items.  For example, if the left most bitmap
  240.          // space is defined to have a width of 30 pixels and a height of
  241.          // 20 pixels, then all the bitmaps placed in this bitmap space
  242.          // for all the list items will have to fit into these dimensions.
  243.          // The reasoning behind this, is that it forces column alignment
  244.          // of the bitmaps for a given bitmap space and offers a
  245.          // consistency in hit testing.  Hit testing is performed on the
  246.          // bitmap space, not just on the bitmap.
  247.          
  248.          // Below, the left most bitmap space is defined.  The max width
  249.          // and height are defined.  The first argument is the BSS List
  250.          // Control handle, the second is the bitmap space identifier or
  251.          // index.  The third and fourth arguments are the width and
  252.          // height respectively.  The last argument signals to the BSS List
  253.          // Control whether to center or left justify the currently
  254.          // assigned bitmap in this bitmap space.  TRUE means center,
  255.          // FALSE means left justify.
  256.          
  257.          // Instead of hard code values for the width and height, another
  258.          // could be taken.  If the item to be assigned to the below
  259.          // bitmap space is a bitmap, then a call to
  260.          // GetObject (hBitmap, sizeof(BITMAP), (LPSTR) &bm);
  261.          // could be called and the dimensions of the bitmap could be
  262.          // used along with some padding to define the width and height
  263.          // of the bitmap space.
  264.          
  265.          // If the item to be assigned to the below bitmap space is an
  266.          // icon, then the values from the calls to
  267.          // GetSystemMetrics (SM_CXICON) and  GetSystemMetrics (SM_CYICON) 
  268.          // could be used along with some padding to define the width
  269.          // and height of the bitmap space.  Since the Windows GDI
  270.          // call DrawIcon ( ) is used to draw the icon by the tree
  271.          // control, if a bitmap space is to contain an icon then the
  272.          // width and height must be defined by the described method above.
  273.          // The required height and width are a result of the Windows API,
  274.          // DrawIcon(), displaying the whole ICON and not offering any 
  275.          // clipping functionality.  The application can determine
  276.          // the minimum height and width bitmap space needed by the List
  277.          // Control, by calling the Windows API, GetSystemMetrics().
  278.          // Calling GetSystemMetrics() with SM_CXICON will return the width
  279.          // of an ICON and calling GetSystemMetrics() with SM_CYICON will
  280.          // return the height of an ICON.  After this is accomplished, the
  281.          // application can place additional margins on the height and
  282.          // width of the bitmap space for visuals.
  283.       
  284.          // The advantages of using ICONs instead of bitmaps are:
  285.          //  1) Icon editors are readily available and offer flexibility to
  286.          //     the presentation of the image.
  287.          //  2) MORICONS.DLL offer an array of icons to use as templates
  288.          //     for original images.  MORICONS.DLL is shipped with Windows 3.1.
  289.          //  3) DrawIcon()s will respect the image created by the developer in
  290.          //     the sense of background masking, etc.
  291.          //  One major disadvantage is that the size of the ICON is fixed
  292.          //  where bitmaps can be any size and clipped to fit in a smaller
  293.          //  bitmap space.
  294.           
  295.          BSL_SetBitmapSpace( hwndList, 
  296.                              0,
  297.                              GetSystemMetrics (SM_CXICON) + 4,
  298.                              GetSystemMetrics (SM_CYICON) + 4,
  299.                              TRUE);
  300.           
  301.          hSpiderIcon = LoadIcon ( hInst, MAKEINTRESOURCE(IDR_LISTDEMO_ICON));
  302.  
  303.          // Now, the fun part...  Defining list items.
  304.             
  305.          // In this example, all of the list items are defined in one
  306.          // call to BSL_AddListItems ( ), even though several calls to 
  307.          // BSL_AddListItems ( ) could have been made instead.  Making
  308.          // several calls instead of one offer the application 
  309.          // multitasking and low memory usage.  Since the list we are
  310.          // creating is not very large, one call will be enough.
  311.          
  312.          // The first thing, allocate MAX_LIST_ITEMS worth of 
  313.          // TREE_NODE_DEF structures, one for each list item to be added
  314.          // to the BSS List Control.  It is best if this memory is
  315.          // initialize to zero.  Any members of the TREE_NODE_DEF structure
  316.          // that are not defined must be initialized with zero.  The 
  317.          // definition structure, TREE_NODE_DEF, is the vehicle that the
  318.          // application uses to define list items to the BSS List Control.
  319.          // This memory is the property of the application.  It's contents
  320.          // are copied to the list control.  Once the node is defined, this
  321.          // memory can be freed.
  322.    
  323.          // Note.  GlobalAlloc ( ) returns the segment and not the handle
  324.          // of the allocated memory since the GMEM_FIXED flag is applied 
  325.          // which requests that the memory block is fixed in memory.  Also,
  326.          // since the GMEM_ZEROINIT flag is applied, the memory is zeroed.
  327.          // MAKEP ( ) macro converts this segment into a long
  328.          // pointer (segment:0).  The GMEM_SHARE flag is used since
  329.          // the pointer is passed to the list control in the process
  330.          // of defining new list items.
  331.          
  332.          wMaxListItems = MAX_LIST_ITEMS;
  333.             
  334.          lpTreeNodeDef = (LP_TREE_NODE_DEF) MAKEP( GlobalAlloc ( 
  335.                                       GMEM_FIXED|GMEM_ZEROINIT|GMEM_SHARE,
  336.                                          MAKELONG(sizeof(TREE_NODE_DEF)* 
  337.                                                  wMaxListItems ,0)),0);
  338.                                          
  339.          if( lpTreeNodeDef )  // If the above memory allocation is success...
  340.          {
  341.             lpTreeNodeDef2 = lpTreeNodeDef; 
  342.             for (i = 0; 
  343.                  i < wMaxListItems;
  344.                  i++, lpTreeNodeDef2++)
  345.             {
  346.                // Assign the text that will be displayed for the list item.
  347.             
  348.                // Normally, the list control will allocate wTextLength + 1 
  349.                // worth of memory to store a copy of the string pointed to
  350.                // by lpszText when the list item is created since the
  351.                // supplied pointer can become invalid if the string it
  352.                // points to is moved or deleted in the future.                  
  353.                     
  354.                lpTreeNodeDef2->lpszText = szSpider [i];
  355.                
  356.                // Assign the length of the above text to the wTextLength
  357.                // member.  This tells the list control, how many characters
  358.                // to display.
  359.                
  360.                // Note. If the sign bit of the wTextLength member is set to
  361.                // 1 by the application then this signals the list control
  362.                // NOT to allocate memory for the string pointed to by
  363.                // lpszText, above, but just store and use the supplied
  364.                // string pointer directly to display the tree node's text.
  365.                // This would work in the case of the string which was 
  366.                // assigned to the lpszText member, above, because it is a
  367.                // constant and is guaranteed to be in the application's
  368.                // data segment for the life of the application.  It will
  369.                // not be applied here though, since this is suppose to be
  370.                // a simple demo.  The advantage of this technique is that
  371.                // no allocation of memory for the string is needed thus
  372.                // conserving resources and increasing speed.
  373.  
  374.                lpTreeNodeDef2->wTextLength 
  375.                                        = lstrlen(lpTreeNodeDef2->lpszText);
  376.                
  377.                // Now it's time to assign bitmap handles created earlier to
  378.                // the first bitmap space which was defined earlier as well.
  379.                // Currently, there are three bitmap spaces allowed to be
  380.                // defined per item.  If more are needed, then the source
  381.                // can be purchased and the number of bitmap spaces can be
  382.                // increased.  In most case only two bitmaps are good
  383.                // enough.
  384.                
  385.                // If any of the bitmap spaces are not used or the 
  386.                // corresponding bitmap/icon handle will be defined later,
  387.                // then make sure that all unused hBitmap[] and
  388.                // hActiveBitmap[] array elements are set to NULL.  The
  389.                // tree control does not check the validity of a
  390.                // bitmap/icon handle.  
  391.             
  392.                // Bitmap/icon handles can be assigned to bitmap spaces, at
  393.                // a later time by calling BSL_SetBitmap ( ) and/or 
  394.                // BSL_SetIcon ( ).               
  395.           
  396.                lpTreeNodeDef2->hBitmap[0] = hSpiderIcon;
  397.                lpTreeNodeDef2->chBitmapTypeBits = 0x0001;
  398.                lpTreeNodeDef2->hActiveBitmap[0] = NULL;
  399.   
  400.                // Notice that the hActiveBitmap[] counterparts are not
  401.                // set to NULL.  The tree control will NOT reference
  402.                // their associated hActiveBitmap[] if hBitmap[] is
  403.                // set to NULL.
  404.                
  405.                lpTreeNodeDef2->hBitmap[1] = NULL;
  406.                lpTreeNodeDef2->hBitmap[2] = NULL;
  407.               
  408.                // No user-defined data associated with the node.
  409.                lpTreeNodeDef2->lpUserData = NULL;
  410.                lpTreeNodeDef2->wUserDataSize = 0;
  411.             }          
  412.                 
  413.             // Add the list items to the list.  Now the list is complete
  414.             // and the user will now be able to play, play, play.
  415.            
  416.             wErrCode  = BSL_AddListItems( hwndList,
  417.                                           wMaxListItems,
  418.                                           lpTreeNodeDef);
  419.         
  420.             HandleAddChildrenError( hwnd, wErrCode);
  421.                      
  422.             FREEP(lpTreeNodeDef);
  423.             
  424.             // Set focus to the tree control, show it, and let 
  425.             // the user knock herself or himself out.
  426.                  
  427.             SetFocus (hwndList);
  428.             ShowWindow (hwndList, SW_SHOW);
  429.          }
  430.          else
  431.          {
  432.             MessageBox (hwnd, "Out of memory.", "WM_CREATE",MB_OK);
  433.          }
  434.          SetFocus (hwndList);
  435.          return FALSE;
  436.  
  437.  
  438.       // This is not used in the example but I thought you would like to 
  439.       // have a starting piece of code to handle double clicks on the
  440.       // list items.
  441.  
  442.       case WM_BST_SELECT_NOTIF_DBLCLK:
  443.       
  444.          // WM_BST_SELECT_NOTIF_DBLCLK is a notification message sent to
  445.          // the application when the user:
  446.          // 1)  double clicks on a list item,
  447.          // 2)  hits the carriage return (acts as if the currently highlighted
  448.          //     list item was double clicked on.),
  449.          // lParam is a pointer to a SELECT_NOTIF notification structure which
  450.          // is owned by the list control.  DO NOT FREE THIS MEMORY OR ALL
  451.          // HELL WILL BREAK LOOSE.  The members of the SELECT_NOTIF structure
  452.          // consist of the pointer to the list item that was selected and a
  453.          // flags member that describes what region of the list item which
  454.          // was clicked.
  455.                
  456.          lpSelectNotif = (LP_SELECT_NOTIF) lParam;
  457.  
  458.          return 0;
  459.  
  460.  
  461.       case WM_SYSCOLORCHANGE:
  462.  
  463.          // Sad but true...  The WM_SYSCOLORCHANGE notification has be sent
  464.          // to the list control since only top level windows get this
  465.          // notification. 
  466.  
  467.          if(hwndList)
  468.             SendMessage (hwndList, message, wParam, lParam);
  469.          return TRUE;       
  470.  
  471.       case WM_COMMAND:
  472.          switch (wParam)
  473.          {
  474.             case IDOK:
  475.                if(GetFocus () != GetDlgItem (hwnd, IDOK))
  476.                   break;
  477.                   
  478.             case IDCANCEL:
  479.                // Delete all list items in the list and free icon resources.
  480.                BSL_ResetList( hwndList);
  481.                DestroyIcon(hSpiderIcon);
  482.                EndDialog (hwnd, TRUE) ;
  483.                return TRUE ;
  484.          }
  485.          break ;
  486.    }
  487.    return FALSE ;
  488. }
  489.  
  490.  
  491.  
  492. char * npszAddChildrenErrors [] =
  493. {
  494. "Memory allocation failure.",
  495. "Level limit exceeded.",
  496. "The number of nodes supported has been exeeded.",
  497. "Only on root per tree.",
  498. "Given parent node is invalid.",
  499. "Invalid error code"
  500. };                     
  501.  
  502. WORD HandleAddChildrenError( HWND hwnd, WORD wErrCode)
  503. {
  504.    WORD i;
  505.      
  506.    switch (wErrCode)
  507.    {
  508.       case BST_NO_ERROR:
  509.          i = 0;
  510.          break;
  511.  
  512.       case BST_ERR_MEMORY_ALLOC_FAILED:
  513.          i = 1;
  514.          break;
  515.                      
  516.       case BST_ERR_LEVEL_LIMIT_EXCEEDED:
  517.          i = 2;
  518.          break;
  519.                      
  520.       case BST_ERR_TOO_MANY_NODES:
  521.          i = 3;
  522.          break;
  523.                  
  524.       case BST_ERR_ONLY_ONE_ROOT_ALLOWED: 
  525.          i = 4;
  526.          break;
  527.                      
  528.       case BST_ERR_INVALID_PARENT_FOR_INSERTION:
  529.          i = 5;
  530.          break;
  531.                
  532.       default:
  533.         i = 6;
  534.         break;
  535.    }
  536.    if ( i != 0)
  537.    {
  538.       MessageBox ( hwnd, 
  539.                    npszAddChildrenErrors[i-1],
  540.                    "BST_AddChildrenToParent",
  541.                     MB_OK);
  542.    }
  543.    return i;   
  544. }
  545.  
  546. /*--------------------------------- EOF -----------------------------------*/
  547.  
  548.