home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 January / Chip_1999-01_cd.bin / sharewar / cppc / CPPC.EXE / Programmers only / ext_date / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-17  |  4.1 KB  |  167 lines

  1. /*
  2.     file: main.cpp
  3. */
  4.  
  5.  
  6. #include <tchar.h>
  7. #include "..\ext_h\CP_EXT.h"
  8. #include "main.h"
  9.  
  10.  
  11. //    global variables
  12.  
  13. HINSTANCE    gDLLInstance = NULL;
  14.  
  15.  
  16.  
  17. //    dll main entry point
  18. //    we need this to initialize above global instance
  19.  
  20. BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
  21. {
  22.     if ( DLL_PROCESS_ATTACH == fdwReason )
  23.         gDLLInstance = hDLLInst;
  24.  
  25.     return TRUE;
  26. }
  27.  
  28.  
  29. //    externals main entry point
  30.  
  31. LRESULT CALLBACK CP_ENTRYPOINT(    SHORT            option,
  32.                                 CPEntryProcPtr    entryPoint, 
  33.                                 LPARAM            *lparam,
  34.                                 HANDLE            *globals )
  35. {
  36.     CPParamBlock    pb;                //    used for callback to CopyPaste
  37.     LRESULT            result = 0L;    //    result of call. 0 == no error
  38.  
  39.  
  40.     //    if option < 0 then this is a notification of CopyPaste
  41.     //    if option > 0 then this is a menu select call
  42.     switch ( option )
  43.     {
  44.     case eInitExternal:
  45.         {
  46.             //    load our menu and give it to CP to install
  47.             HMENU hmenu = LoadMenu ( gDLLInstance, "DATE_UTILITIES" );
  48.             *lparam = (LPARAM) hmenu;
  49.             result = (NULL == hmenu) ? GetLastError() : 0L;
  50.         }
  51.         break;
  52.  
  53.     case eDeinitExternal:
  54.         {
  55.             //    we are about to exit. receive our HMENU to destroy
  56.             if ( ! DestroyMenu ( (HMENU) *lparam ) )
  57.                 result = GetLastError();
  58.         }
  59.         break;
  60.  
  61.         //    this is our first menu entry ( see ext.date.rc )
  62.     case eFirstOption:        //    insert current date ( short )
  63.         {
  64.             HGLOBAL    hglobal = GetDateTimeHandle ( 
  65.                             DATE_SHORTDATE, GetDateFormat );
  66.             if ( hglobal )
  67.             {
  68.                 pb.wparam1 = getCurrentClipboard(entryPoint);    
  69.                 pb.wparam2 = CF_TEXT;    //    clipboard format
  70.                 pb.handle1 = hglobal;    //    data handle
  71.                 result = CallCP ( eSetClipboardItem, &pb );
  72.             }
  73.             //    if everything works fine, the global handle
  74.             //    belongs to the clipboard.
  75.             //    On fail, you are responsible to free memory.
  76.             if ( result )
  77.                 GlobalFree ( hglobal );
  78.         }
  79.         break;
  80.  
  81.         //    this is our second menu entry ( see ext.date.rc )
  82.     case 2:                    //    insert current date ( long )
  83.         {
  84.             HGLOBAL    hglobal = GetDateTimeHandle ( 
  85.                             DATE_LONGDATE, GetDateFormat );
  86.             if ( hglobal )
  87.             {
  88.                 pb.wparam1 = getCurrentClipboard(entryPoint);    
  89.                 pb.wparam2 = CF_TEXT;    //    clipboard format
  90.                 pb.handle1 = hglobal;    //    data handle
  91.                 result = CallCP ( eSetClipboardItem, &pb );
  92.             }
  93.             if ( result )
  94.                 GlobalFree ( hglobal );
  95.         }
  96.         break;
  97.  
  98.         //    this is our third menu entry ( see ext.date.rc )
  99.     case 3:                    //    insert current time
  100.         {
  101.             HGLOBAL    hglobal = GetDateTimeHandle ( 
  102.                             TIME_FORCE24HOURFORMAT, GetTimeFormat );
  103.             if ( hglobal )
  104.             {
  105.                 pb.wparam1 = getCurrentClipboard(entryPoint);    
  106.                 pb.wparam2 = CF_TEXT;    //    clipboard format
  107.                 pb.handle1 = hglobal;    //    data handle
  108.                 result = CallCP ( eSetClipboardItem, &pb );
  109.             }
  110.             if ( result )
  111.                 GlobalFree ( hglobal );
  112.         }
  113.         break;
  114.     }
  115.     return result;
  116. }
  117.  
  118.  
  119. //    this one returns the currently selected clipboard
  120. int getCurrentClipboard (CPEntryProcPtr    entryPoint)
  121. {
  122.     CPParamBlock    pb;
  123.     int                result;
  124.  
  125.     ZeroMemory ( &pb, sizeof(CPParamBlock) );
  126.     result = CallCP ( eGetCurrentClipboard, &pb );
  127.     return ( ! result ) ? pb.wparam1 : 0;
  128. }
  129.  
  130.  
  131. //    this routine returns a formatted date/time in a globals handle
  132.  
  133. HGLOBAL GetDateTimeHandle ( DWORD flags, DateTimeFormatFP dtfp )
  134. {
  135.     SYSTEMTIME    systemTime;
  136.     TCHAR        szBuffer[128];
  137.     int            numBytes;
  138.     HGLOBAL        hglobal = NULL;
  139.  
  140.     GetSystemTime(&systemTime);
  141.  
  142.     //    first call to GetDateFormat() with cch == 0
  143.     //    to get the needed size
  144.     numBytes = (*dtfp)( LOCALE_USER_DEFAULT,    flags, 
  145.                 &systemTime, NULL, (LPTSTR) szBuffer, 0 );
  146.  
  147.     //    next call with cch == sizeof(szBuffer) to get
  148.     //    the real data.
  149.     if ( numBytes > 0 )
  150.         numBytes = (*dtfp)( LOCALE_USER_DEFAULT, flags,
  151.                 &systemTime, NULL, (LPTSTR) szBuffer, sizeof(szBuffer) );
  152.  
  153.     //    if there are still bytes in our stack buffer, allocate
  154.     //    global memory and return as function result.
  155.     if ( numBytes > 0 )
  156.     {            
  157.         numBytes += sizeof(TCHAR);
  158.         hglobal = GlobalAlloc (  GMEM_MOVEABLE | GMEM_DDESHARE, numBytes );
  159.         if ( NULL != hglobal )
  160.         {
  161.             CopyMemory ( GlobalLock ( hglobal ), szBuffer, numBytes );
  162.             GlobalUnlock ( hglobal );
  163.         }
  164.     }
  165.     return hglobal;
  166. }
  167.