home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / wps / textfldr / textfldr.c next >
C/C++ Source or Header  |  1999-05-11  |  16KB  |  446 lines

  1.  
  2. /*
  3.  *
  4.  *
  5.  *   Module Name: TEXTFLDR
  6.  *
  7.  *   OS/2 Work Place Shell Sample Program
  8.  *
  9.  *   Copyright (C) 1993 IBM Corporation
  10.  *
  11.  *       DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  12.  *       sample code created by IBM Corporation. This sample code is not
  13.  *       part of any standard or IBM product and is provided to you solely
  14.  *       for  the purpose of assisting you in the development of your
  15.  *       applications.  The code is provided "AS IS", without
  16.  *       warranty of any kind.  IBM shall not be liable for any damages
  17.  *       arising out of your use of the sample code, even if they have been
  18.  *       advised of the possibility of such damages.
  19.  *
  20.  */
  21.  
  22.  
  23. #define TextFolder_Class_Source
  24. #include "textfldr.ih"
  25.  
  26. /*
  27.  *
  28.  *  METHOD: IsTextFile                                     (X) PRIVATE
  29.  *                                                         ( ) PUBLIC
  30.  *  DESCRIPTION:
  31.  *
  32.  *    This method checks the first 'n' characters of a file to determine
  33.  *    if it is or is not text. This method is invoked by the
  34.  *    ValidateDragAndDrop method. It is invoked only if no type is specified
  35.  *    for the object being manipulated.  A file is considered to be text if
  36.  *    the first 'n' bytes are in the range of ASCII 32 (20 Hex) to ASCII 126
  37.  *    (7E Hex), or is a carriage return (ASCII 10, Hex 0A), line feed
  38.  *    (ASCII 13, Hex 0D), tab (ASCII 8, HEX 08), or end of file marker
  39.  *    (ASCII 26, Hex 1A).
  40.  *
  41.  *    NOTE: The value used for 'n' is stored in the variable ulSampleSize
  42.  *    which is local to the IsTextFile method.
  43.  *
  44.  *  RETURN:
  45.  *
  46.  *    TRUE  - the object is a text file.
  47.  *    FALSE - the object is not a text file.
  48.  *
  49.  *  HOW TO OVERRIDE:
  50.  *
  51.  *    n/a
  52.  *
  53.  */
  54.  
  55. SOM_Scope BOOL   SOMLINK TextFolderwps_IsTextFile(TextFolder *somSelf,
  56.                 PSZ pszFileName)
  57. {
  58.     ULONG   Action;              /* The action performed on the file */
  59.     HFILE   hFile;               /* The file handle */
  60.     ULONG   ulSampleSize = 500L; /* Number of bytes to test */
  61.     ULONG   ulBytesRead;         /* Number of sample bytes read */
  62.     ULONG   ulCount;             /* Counter variable */
  63.     PSZ     pszSample = NULL;    /* The sample buffer */
  64.     CHAR    cChar;               /* Current character from sample buffer */
  65.     APIRET  aRC;                 /* Return code from API calls */
  66.     BOOL    bRC = FALSE;         /* True - it is a text file */
  67.                                  /* False - it isn't not a text file */
  68.  
  69.     /* TextFolderData *somThis = TextFolderGetData(somSelf); */
  70.     TextFolderMethodDebug("TextFolder","TextFolderwps_IsTextFile");
  71.  
  72.  
  73.     /* Open the file that contains the object */
  74.     if( NO_ERROR != ( aRC=DosOpen( pszFileName,        /* File path name */
  75.                              &hFile,                   /* File handle */
  76.                              &Action,                  /* Action taken */
  77.                              0L,                       /* File size */
  78.                              FILE_NORMAL,              /* File attribute */
  79.                              OPEN_ACTION_FAIL_IF_NEW | /* Open function type */
  80.                              OPEN_ACTION_OPEN_IF_EXISTS,
  81.                              OPEN_FLAGS_FAIL_ON_ERROR | OPEN_SHARE_DENYNONE |
  82.                              OPEN_ACCESS_READONLY, 0L)))
  83.        somPrintf( "Unable to open file %s (%ld)\r\n", pszFileName, aRC);
  84.  
  85.     else{
  86.  
  87.        /* Allocate buffer for reading a sample of the object into. */
  88.        if( NO_ERROR != (aRC = DosAllocMem( (PPVOID)&pszSample, ulSampleSize,
  89.                                            PAG_COMMIT | PAG_READ | PAG_WRITE)))
  90.           somPrintf( "Unable to allocate buffer of %ld bytes (%ld)\r\n",
  91.                                                             ulSampleSize, aRC);
  92.        else{
  93.  
  94.           if( NO_ERROR != ( aRC = DosRead( hFile, pszSample, ulSampleSize,
  95.                                                                &ulBytesRead)))
  96.              somPrintf( "Unable to read %ld bytes from file %s (%ld)\r\n",
  97.                                                ulSampleSize, pszFileName, aRC);
  98.           else{
  99.  
  100.              /* Check all the characters read in the sample buffer */
  101.              for( ulCount=0, bRC=TRUE; (ulCount<ulBytesRead) && (bRC == TRUE);
  102.                                                                     ulCount++){
  103.  
  104.                 cChar = pszSample[ ulCount];
  105.  
  106.                 /* Make sure the characters are text characters,   */
  107.                 /* carriage return, line feed, tab, or end of file */
  108.  
  109.                 if( ((cChar < FIRST_TEXT_CHAR) || (cChar > LAST_TEXT_CHAR)) &&
  110.                    (cChar != CHAR_CARRIAGE_RETURN) && (cChar != CHAR_LINE_FEED)
  111.                         && (cChar != CHAR_TAB) && (cChar != CHAR_END_OF_FILE)){
  112.                     bRC = FALSE;
  113.                 }
  114.              }
  115.           }
  116.  
  117.           /* Clean up */
  118.           DosFreeMem( pszSample);
  119.           DosClose( hFile);
  120.        }
  121.     }
  122.  
  123.     return( bRC);
  124. }
  125.  
  126. /*
  127.  *
  128.  *  METHOD: ValidateDragAndDrop                            (X) PRIVATE
  129.  *                                                         ( ) PUBLIC
  130.  *  DESCRIPTION:
  131.  *
  132.  *    This method determines if an object can be dragged/dropped on the
  133.  *    TextFolder. It is invoked by the wpDrag and wpDragOver methods.
  134.  *
  135.  *    The ValidateDragAndDrop method first checks the type of the object
  136.  *    being manipulated. If a type is specified, and it is Plain Text,
  137.  *    the operation is considered to be valid. If a type is specified and
  138.  *    it is not Plain Text the operation is considered invalid. If no
  139.  *    type is specified the IsTextFile() method is invoked to determine
  140.  *    if the object is or is not a text object.
  141.  *
  142.  *  RETURN:
  143.  *
  144.  *    TRUE  - The object can be dragged/dropped on the TextFolder.
  145.  *    FALSE - The object can not be dragged/dropped on the TextFolder.
  146.  *
  147.  *  HOW TO OVERRIDE:
  148.  *
  149.  *    n/a
  150.  *
  151.  */
  152.  
  153. /*
  154.  *
  155.  * METHOD: ValidateDragAndDrop                            (X) PRIVATE
  156.  *                                                        ( ) PUBLIC
  157.  * DESCRIPTION:
  158.  *
  159.  *   This method determines if an object can be dragged/dropped on the
  160.  *   TextFolder. It is invoked by the wpDrag and wpDragOver methods.
  161.  *
  162.  *   The ValidateDragAndDrop method first checks the type of the object
  163.  *   being manipulated. If a type is specified, and it is Plain Text,
  164.  *   the operation is considered to be valid. If a type is specified and
  165.  *   it is not Plain Text the operation is considered invalid. If no
  166.  *   type is specified the IsTextFile() method is invoked to determine
  167.  *   if the object is or is not a text object.
  168.  *
  169.  * RETURN:
  170.  *
  171.  *   TRUE  - The object can be dragged/dropped on the TextFolder.
  172.  *   FALSE - The object can not be dragged/dropped on the TextFolder.
  173.  *
  174.  * HOW TO OVERRIDE:
  175.  *
  176.  *   n/a
  177.  *
  178.  *
  179.  *   Methods from the WPFileSystem class
  180.  *
  181.  */
  182.  
  183. SOM_Scope BOOL   SOMLINK TextFolderwps_ValidateDragAndDrop(TextFolder *somSelf,
  184.                 PDRAGINFO pdrgInfo)
  185. {
  186.     CHAR    pszBuffer[ 255];     // Buffer for file system object name
  187.     ULONG   ulNumberOfObjects;   // Number of object draged over object
  188.     ULONG   ulCurrentObject;     // Used to increment list of objects
  189.     BOOL    bObjectType;         // Type of object
  190.  
  191.     PDRAGITEM pDragItem;
  192.  
  193.     /* TextFolderData *somThis = TextFolderGetData(somSelf); */
  194.     TextFolderMethodDebug("TextFolder","TextFolderwps_ValidateDragAndDrop");
  195.  
  196.     /* Get the number of objects being dragged */
  197.     ulNumberOfObjects = DrgQueryDragitemCount( pdrgInfo);
  198.  
  199.     /* Test each object */
  200.     for( ulCurrentObject = 0; ulCurrentObject<ulNumberOfObjects;
  201.                                                             ulCurrentObject++){
  202.         pDragItem = DrgQueryDragitemPtr( pdrgInfo, ulCurrentObject);
  203.  
  204.         /* If the object isn't Plain Text check to see if any type is */
  205.         /* associated with the object. If no type is associated then  */
  206.         /* check the contents of the file to see if it is text only.  */
  207.         if( FALSE == DrgVerifyType( pDragItem, DRT_TEXT)){
  208.  
  209.             /* If the associated type is known, this isn't a text      */
  210.             /* object. If it isn't known, check the object's contents. */
  211.             bObjectType = DrgVerifyType( pDragItem, "Reserved");
  212.  
  213.             /* Get the path */
  214.             DrgQueryStrName( pDragItem->hstrContainerName, sizeof( pszBuffer),
  215.                                                                     pszBuffer);
  216.             /* Append the name of the object to the path */
  217.             DrgQueryStrName( pDragItem->hstrSourceName,
  218.                                        sizeof( pszBuffer) - strlen( pszBuffer),
  219.                                               &pszBuffer[ strlen( pszBuffer)]);
  220.  
  221.             /* If a type was specified, it isn't Plain Text. */
  222.             /* If no type was specified check if it is a text file. */
  223.             if( bObjectType == FALSE || ( bObjectType == TRUE &&
  224.                                  FALSE == _IsTextFile( somSelf, pszBuffer)) ){
  225.  
  226.                 somPrintf( "This is not a Plain Text object. ");
  227.                 DrgQueryStrName( pDragItem->hstrType, sizeof( pszBuffer),
  228.                                                                     pszBuffer);
  229.                 somPrintf( "The types for this object are: '%s'\n", pszBuffer);
  230.  
  231.                 return( FALSE);
  232.             }
  233.         }
  234.     }
  235.  
  236.     return( TRUE);
  237. }
  238.  
  239. /*
  240.  *
  241.  *   METHOD: wpFilterPopupMenu                              ( ) PRIVATE
  242.  *                                                          (X) PUBLIC
  243.  *   DESCRIPTION:
  244.  *
  245.  *     Removes the Tree view option from the Open popup menu.
  246.  *
  247.  */
  248.  
  249. SOM_Scope ULONG   SOMLINK TextFolderwps_wpFilterPopupMenu(TextFolder *somSelf,
  250.                 ULONG ulFlags,
  251.                 HWND hwndCnr,
  252.                 BOOL fMultiSelect)
  253. {
  254.     /* TextFolderData *somThis = TextFolderGetData(somSelf); */
  255.     TextFolderMethodDebug("TextFolder","TextFolderwps_wpFilterPopupMenu");
  256.  
  257.     ulFlags = parent_wpFilterPopupMenu(somSelf,ulFlags,hwndCnr,fMultiSelect);
  258.  
  259.     /* Remove the tree view menu option and make sure delete is available */
  260.     return ( ( ulFlags | CTXT_DELETE) & ~CTXT_TREE);
  261. }
  262.  
  263. /*
  264.  *
  265.  *   METHOD: wpAddFolderView2Page                           ( ) PRIVATE
  266.  *                                                          (X) PUBLIC
  267.  *   DESCRIPTION:
  268.  *
  269.  *     Removes the tree view settings page for the TextFolder.
  270.  *
  271.  */
  272.  
  273. SOM_Scope ULONG   SOMLINK TextFolderwps_wpAddFolderView2Page(TextFolder *somSelf,
  274.                 HWND hwndNotebook)
  275. {
  276.     /* TextFolderData *somThis = TextFolderGetData(somSelf); */
  277.     TextFolderMethodDebug("TextFolder","TextFolderwps_wpAddFolderView2Page");
  278.  
  279.     return( SETTINGS_PAGE_REMOVED);
  280. }
  281.  
  282. /*
  283.  *
  284.  *   METHOD: wpAddFolderIncludePage                         ( ) PRIVATE
  285.  *                                                          (X) PUBLIC
  286.  *   DESCRIPTION:
  287.  *
  288.  *     Removes the Include Page from the TextFolder's settings.
  289.  *
  290.  */
  291.  
  292. SOM_Scope ULONG   SOMLINK TextFolderwps_wpAddFolderIncludePage(TextFolder *somSelf,
  293.                 HWND hwndNotebook)
  294. {
  295.     /* TextFolderData *somThis = TextFolderGetData(somSelf); */
  296.     TextFolderMethodDebug("TextFolder","TextFolderwps_wpAddFolderIncludePage");
  297.  
  298.     return ( SETTINGS_PAGE_REMOVED);
  299. }
  300.  
  301. /*
  302.  *
  303.  *   METHOD: wpDragOver                                     ( ) PRIVATE
  304.  *                                                          (X) PUBLIC
  305.  *   DESCRIPTION:
  306.  *
  307.  *     Allows or disallows an object to be dragged over the TextFolder.
  308.  *
  309.  */
  310.  
  311. SOM_Scope MRESULT   SOMLINK TextFolderwps_wpDragOver(TextFolder *somSelf,
  312.                 HWND hwndCnr,
  313.                 PDRAGINFO pdrgInfo)
  314. {
  315.     MRESULT mResult;
  316.  
  317.     /* TextFolderData *somThis = TextFolderGetData(somSelf); */
  318.     TextFolderMethodDebug("TextFolder","TextFolderwps_wpDragOver");
  319.  
  320.     mResult = parent_wpDragOver(somSelf,hwndCnr,pdrgInfo);
  321.  
  322.     /* If the parent said it is okay to drop then we will check it also. */
  323.     if( DOR_NEVERDROP != SHORT1FROMMR( mResult)){
  324.  
  325.         /* If the object is acceptable, return the mResult from the parent */
  326.         if( FALSE == _ValidateDragAndDrop( somSelf, pdrgInfo))
  327.             mResult = MRFROM2SHORT( DOR_NEVERDROP, DO_UNKNOWN);
  328.     }
  329.  
  330.     return ( mResult);
  331. }
  332.  
  333. /*
  334.  *
  335.  *   METHOD: wpDrop                                         ( ) PRIVATE
  336.  *                                                          (X) PUBLIC
  337.  *   DESCRIPTION:
  338.  *
  339.  *     Allows or disallows an object to be dropped on the TextFolder.
  340.  *
  341.  */
  342.  
  343. SOM_Scope MRESULT   SOMLINK TextFolderwps_wpDrop(TextFolder *somSelf,
  344.                 HWND hwndCnr,
  345.                 PDRAGINFO pdrgInfo,
  346.                 PDRAGITEM pdrgItem)
  347. {
  348.     MRESULT mResult;
  349.  
  350.     /* TextFolderData *somThis = TextFolderGetData(somSelf); */
  351.     TextFolderMethodDebug("TextFolder","TextFolderwps_wpDrop");
  352.  
  353.     mResult = parent_wpDrop(somSelf,hwndCnr,pdrgInfo,pdrgItem);
  354.  
  355.     /* If the parent said it is okay to drop then we will check it also */
  356.     if( DOR_NEVERDROP != SHORT1FROMMR( mResult)){
  357.  
  358.  
  359.         /* Validate the objects */
  360.         if( FALSE == _ValidateDragAndDrop( somSelf, pdrgInfo))
  361.             mResult = MRFROM2SHORT( DOR_NEVERDROP, DO_UNKNOWN);
  362.     }
  363.  
  364.     return ( mResult);
  365. }
  366.  
  367. #undef SOM_CurrentClass
  368. #define SOM_CurrentClass SOMMeta
  369. /*
  370.  *
  371.  *  METHOD: wpclsQueryTitle
  372.  *
  373.  *  DESCRIPTION:
  374.  *
  375.  *    Provides a default name of Text Folder for TextFolder.
  376.  *
  377.  */
  378.  
  379. SOM_Scope PSZ   SOMLINK TextFolderM_wpclsQueryTitle(M_TextFolder *somSelf)
  380. {
  381.     /* M_TextFolderData *somThis = M_TextFolderGetData(somSelf); */
  382.     M_TextFolderMethodDebug("M_TextFolder","TextFolderM_wpclsQueryTitle");
  383.  
  384.     return ( "Text Folder");
  385. }
  386.  
  387. /*
  388.  *
  389.  *  METHOD: wpclsQueryIconData
  390.  *
  391.  *  DESCRIPTION:
  392.  *
  393.  *    Provides an icon for the TextFolder.
  394.  *
  395.  */
  396.  
  397. SOM_Scope ULONG   SOMLINK TextFolderM_wpclsQueryIconData(M_TextFolder *somSelf,
  398.                 PICONINFO pIconInfo)
  399. {
  400.     static HMODULE hModule = NULLHANDLE;
  401.     PSZ            pszLibraryName;
  402.  
  403.     /* M_TextFolderData *somThis = M_TextFolderGetData(somSelf); */
  404.     M_TextFolderMethodDebug("M_TextFolder","TextFolderM_wpclsQueryIconData");
  405.  
  406.     /* Only get the handle once */
  407.     if( (hModule == NULLHANDLE) && pIconInfo){
  408.  
  409.        /* Get the name of the library so we can get it's handle. */
  410.        if( NULL != ( pszLibraryName = _somLocateClassFile( SOMClassMgrObject,
  411.                       SOM_IdFromString( "TextFolder"), TextFolder_MajorVersion,
  412.                                                      TextFolder_MinorVersion)))
  413.  
  414.            /* Get the handle for the library. */
  415.            DosQueryModuleHandle( pszLibraryName, &hModule);
  416.     }
  417.  
  418.     if( pIconInfo){
  419.  
  420.        pIconInfo->fFormat = ICON_RESOURCE;
  421.        pIconInfo->resid   = ID_ICON;
  422.        pIconInfo->hmod    = hModule;
  423.     }
  424.  
  425.     return ( sizeof( ICONINFO));
  426. }
  427.  
  428. SOM_Scope PSZ  SOMLINK TextFolderwps_wpclsQueryTitle(M_TextFolder *somSelf)
  429. {
  430.     /* M_TextFolderData *somThis = M_TextFolderGetData(somSelf); */
  431.     M_TextFolderMethodDebug("M_TextFolder","TextFolderwps_wpclsQueryTitle");
  432.  
  433.     return (M_TextFolder_parent_M_WPFolder_wpclsQueryTitle(somSelf));
  434. }
  435.  
  436. SOM_Scope ULONG  SOMLINK TextFolderwps_wpclsQueryIconData(M_TextFolder *somSelf, 
  437.                                                           PICONINFO pIconInfo)
  438. {
  439.     /* M_TextFolderData *somThis = M_TextFolderGetData(somSelf); */
  440.     M_TextFolderMethodDebug("M_TextFolder","TextFolderwps_wpclsQueryIconData");
  441.  
  442.     return (M_TextFolder_parent_M_WPFolder_wpclsQueryIconData(somSelf, 
  443.                                                               pIconInfo));
  444. }
  445.  
  446.