home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 v2.4 Fix / W95-v2.4fix.iso / ACADWIN / ADS / CPP / DDE / DDEMISC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  12.0 KB  |  383 lines

  1. /* 
  2.     DDEMISC.CPP -
  3.     
  4.     This file:
  5.  
  6.         Defines miscellaneous functions that are used in ADS 
  7.         DDE C++ classes.
  8.         
  9.  
  10.     (C) Copyright 1988-1994 by Autodesk, Inc.
  11.  
  12.     This program is copyrighted by Autodesk, Inc. and is  licensed
  13.     to you under the following conditions.  You may not distribute
  14.     or  publish the source code of this program in any form.   You
  15.     may  incorporate this code in object form in derivative  works
  16.     provided  such  derivative  works  are  (i.) are  designed and
  17.     intended  to  work  solely  with  Autodesk, Inc. products, and
  18.     (ii.)  contain  Autodesk's  copyright  notice  "(C)  Copyright
  19.     1988-1994 by Autodesk, Inc."
  20.  
  21.     AUTODESK  PROVIDES THIS PROGRAM "AS IS" AND WITH  ALL  FAULTS.
  22.     AUTODESK  SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF  MER-
  23.     CHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK,  INC.
  24.     DOES  NOT  WARRANT THAT THE OPERATION OF THE PROGRAM  WILL  BE
  25.     UNINTERRUPTED OR ERROR FREE.
  26.  
  27. */
  28. #include "ddeinc.h"
  29.  
  30. //-----------------------------------------------------------------------------
  31. #ifdef DDEADS_DBG
  32. static char     dbg_string[256];
  33. #endif
  34.  
  35. /******************************************************************************
  36. *                                                                             *
  37. *                        DDE_HCONV member functions                           *
  38. *                                                                             *
  39. ******************************************************************************/
  40. //-----------------------------------------------------------------------------
  41. DDE_HCONV::DDE_HCONV( DDE_CONNECTION* _conx
  42.                     , SERVER_INFO *_server 
  43.                     , PCONVCONTEXT pcc)
  44.                     : ADS_OBJ(), conx( _conx ), server( _server )
  45. {
  46.     cur_conv = DdeConnect( DDE_GLOBAL::GetInstId()
  47.                         , conx->DDEService()
  48.                         , conx->DDETopic()
  49.                         , pcc );
  50.  
  51.     if ( cur_conv == 0 )
  52.     {
  53.         MessageBox( GetFocus()
  54.                     , "DDE Connection failed."
  55.                     , "DDE_HCONV Info", MB_OK );
  56.     }
  57. #ifdef DDEADS_DBG
  58.     wsprintf( dbg_string
  59.             , " DdeConnect: this = %lx cur_conv = %lx"
  60.             , this
  61.             , cur_conv );
  62.     OutputDebugString( dbg_string );
  63. #endif
  64. }
  65.  
  66. //-----------------------------------------------------------------------------
  67. BOOL DDE_HCONV::DisConnect()
  68. {
  69.     if ( cur_conv )
  70.     {
  71. #ifdef DDEADS_DBG
  72.     wsprintf( dbg_string
  73.             , " DdeDisconnect: this = %lx cur_conv = %lx"
  74.             , this
  75.             , cur_conv );
  76.     OutputDebugString( dbg_string );
  77. #endif
  78.         if ( DdeDisconnect( cur_conv ) )
  79.         {
  80.             cur_conv = 0;
  81.             return TRUE;
  82.         }
  83.         else
  84.         {
  85.             DWORD   err = GetLastError();
  86.  
  87.             ::MessageBox( GetFocus()
  88.                         , "Fail to disconnect conversation"
  89.                         , "DDE_HCONV"
  90.                         , MB_OK );
  91.             return FALSE;
  92.         }
  93.     }
  94.     return TRUE;
  95. }
  96.  
  97. //-----------------------------------------------------------------------------
  98. DDE_HCONV::~DDE_HCONV()
  99. {
  100.     if ( cur_conv )
  101.     {
  102.         conx->RemoveConnection();
  103.     }
  104. }
  105.  
  106. //-----------------------------------------------------------------------------
  107. // 
  108. // New conversation can be set up by providing a new DDE_CONNECTION
  109. // and a SERVER_INFO, and PCONVCONTEXT.  Unless DDE_CONNECTION's 
  110. // topic or service name change, no new conversation is initiated.
  111. //
  112. HCONV DDE_HCONV::NewConv( DDE_CONNECTION* _conx
  113.                             , SERVER_INFO *_server 
  114.                             , PCONVCONTEXT pcc)
  115. {
  116.     if ( Valid() )
  117.     {
  118.         if ( !DisConnect() )
  119.         {
  120.             return NULL;
  121.         }
  122.     }
  123.     
  124.     ASSERT( _conx != NULL );
  125.     ASSERT( _server != NULL );
  126.     
  127.     conx = _conx;
  128.     server = _server;
  129.  
  130.     ASSERT( conx->DDEService() );
  131.     ASSERT( conx->DDETopic() );
  132.  
  133.    cur_conv = DdeConnect( DDE_GLOBAL::GetInstId()
  134.                         , conx->DDEService()
  135.                         , conx->DDETopic()
  136.                         , pcc );
  137. #ifdef DDEADS_DBG
  138.     wsprintf( dbg_string
  139.             , " DdeConnect: this = %lx cur_conv = %lx"
  140.             , this
  141.             , cur_conv );
  142.     OutputDebugString( dbg_string );
  143. #endif
  144.     if ( !cur_conv )
  145.     {
  146.         // try to start the application then
  147.         server->Start();
  148.     
  149.         cur_conv = DdeConnect( DDE_GLOBAL::GetInstId()
  150.                             , conx->DDEService()
  151.                             , conx->DDETopic()
  152.                             , pcc );
  153.         if ( cur_conv == 0 )
  154.         {
  155.             MessageBox( GetFocus()
  156.                         , "DDE Connection failed."
  157.                         , "DDE_HCONV Info", MB_OK );
  158.         }
  159.     }
  160.     return ( cur_conv );
  161. }
  162.  
  163. /******************************************************************************
  164. *                                                                             *
  165. *                 DDE_STRING_HANDLE member functions                          *
  166. *                                                                             *
  167. ******************************************************************************/
  168. //-----------------------------------------------------------------------------
  169. DDE_STRING_HANDLE::DDE_STRING_HANDLE( DDE_CONNECTION* _conx )
  170. {
  171.     handle = NULL; 
  172.     conx = _conx; 
  173. }
  174.  
  175. //-----------------------------------------------------------------------------
  176. DDE_STRING_HANDLE::~DDE_STRING_HANDLE() 
  177.     if ( handle )
  178.     {
  179.         DdeFreeStringHandle( DDE_GLOBAL::GetInstId(), handle );
  180.     }
  181. }
  182.  
  183. //-----------------------------------------------------------------------------
  184. HSZ DDE_STRING_HANDLE::NewHandle( ADS_STRING& dde_string_name )
  185. {
  186.     if ( conx && conx->linking )
  187.     {
  188.         conx->Unlink();
  189.     }
  190.  
  191.     if ( handle )
  192.     {
  193.         DdeFreeStringHandle( DDE_GLOBAL::GetInstId(), handle );
  194.         handle = NULL;
  195.     }
  196.     if ( dde_string_name.Valid() )
  197.     {
  198.         string = dde_string_name;
  199.         char temp_buf[ 256 ];
  200.         strcpy( temp_buf, string.CString() );
  201.         handle = DdeCreateStringHandle( DDE_GLOBAL::GetInstId()
  202.                                         , temp_buf
  203.                                         , CP_WINANSI );
  204.     }
  205.     return handle;
  206. }
  207.  
  208. /******************************************************************************
  209. *                                                                             *
  210. *                               DDE_ITEM                                      *
  211. *                                                                             *
  212. ******************************************************************************/
  213. //-----------------------------------------------------------------------------
  214. // Default value for DDE_ITEM 
  215. //
  216. HSZ DDE_ITEM::ConstructDDEString( ADS_STRING& string )
  217. {
  218.     Reset();
  219.     return NewHandle( string );
  220. }
  221.  
  222. /******************************************************************************
  223. *                                                                             *
  224. *                              EXCEL_ITEM                                     *
  225. *                                                                             *
  226. ******************************************************************************/
  227. //-----------------------------------------------------------------------------
  228. ADS_STRING EXCEL_ITEM::init_string = "R1C1";
  229.  
  230. //-----------------------------------------------------------------------------
  231. HSZ  EXCEL_ITEM::ConstructDDEString( ADS_STRING& send_string )
  232. {
  233.  
  234.     ASSERT( send_string.Valid() );
  235.     
  236.     int     startRow    = (DDE_GLOBAL::excel_server_info)->GetCurRow();
  237.     int     startCol    = (DDE_GLOBAL::excel_server_info)->GetCurCol();
  238.     int     nRow        = (DDE_GLOBAL::excel_server_info)->GetMaxRow();
  239.     int     nCol        = (DDE_GLOBAL::excel_server_info)->GetMaxCol();
  240.     int     endRow, endCol;
  241.  
  242.     //
  243.     // Check to see if we have enough row and col to accommendate
  244.     // the string
  245.     //
  246.     // The dumb way...
  247.     int num_row_needed = 1;
  248.     int num_col_needed = 1;
  249.     int current_col_needed = 1;
  250.  
  251.     //
  252.     // Num of col = max width of the spreadsheet, if 
  253.     // (DDE_GLOBAL::excel_server_info)->row_seperator is seen, check the num col needed 
  254.     // for this row.
  255.     //
  256.  
  257.     //
  258.     // kludge before I have time to use ADS_STRING functions...
  259.     //
  260.     char *temp_ptr = ( char * )send_string.CString();
  261.     while ( *temp_ptr != EOS 
  262.         && ( temp_ptr = strpbrk( temp_ptr
  263.                                 , (DDE_GLOBAL::excel_server_info)->seperators.CString() ) ) 
  264.            != NULL )
  265.     {
  266.         if ( strncmp( (DDE_GLOBAL::excel_server_info)->row_seperator.CString()
  267.                     , temp_ptr
  268.                     , (DDE_GLOBAL::excel_server_info)->row_seperator.Length() ) == 0 )
  269.         {
  270.             num_row_needed++;
  271.             if ( current_col_needed > num_col_needed )
  272.             {
  273.                 num_col_needed = current_col_needed;
  274.             }
  275.             current_col_needed = 1;
  276.         }
  277.         else if ( strncmp( (DDE_GLOBAL::excel_server_info)->col_seperator.CString()
  278.                         , temp_ptr
  279.                         , (DDE_GLOBAL::excel_server_info)->col_seperator.Length() ) == 0 )
  280.         {
  281.             current_col_needed++;
  282.         }
  283.         temp_ptr++;
  284.     }
  285.  
  286.     if ( ( nRow - startRow ) < num_row_needed 
  287.         || ( nCol - startCol ) < num_col_needed )
  288.     {
  289.         ::MessageBox( GetFocus()
  290.                     , "Hit the spreadsheet limit, item name not build, transfer incomplete"
  291.                     , "ADS Dde transfer information"
  292.                     , MB_OK );
  293.         Reset();
  294.         return NULL;
  295.     }
  296.  
  297.     char    NumStr[20];
  298.  
  299.     endRow = num_row_needed + startRow - 1;
  300.     endCol = num_col_needed + startCol - 1;
  301.  
  302.     if ( endCol > max_range_col )
  303.     {
  304.         max_range_col = endCol;
  305.     }
  306.  
  307.     if ( endRow > max_range_row )
  308.     {
  309.         max_range_row = endRow;
  310.     }
  311.  
  312.     string = (DDE_GLOBAL::excel_server_info)->ssrow;
  313.     _itoa( startRow, NumStr, 10 );
  314.     string += NumStr;
  315.     string += (DDE_GLOBAL::excel_server_info)->sscol;
  316.     _itoa( startCol, NumStr, 10);
  317.     string += NumStr;
  318.  
  319.     if ( nRow == 1 && nCol == 1 )
  320.     {
  321.         return Handle();
  322.     }
  323.     string += (DDE_GLOBAL::excel_server_info)->ssdelim;
  324.  
  325.     string += (DDE_GLOBAL::excel_server_info)->ssrow;
  326.     _itoa( endRow, NumStr, 10 );
  327.     string += NumStr;
  328.  
  329.     string += (DDE_GLOBAL::excel_server_info)->sscol;
  330.     _itoa( endCol, NumStr, 10 );
  331.     string += NumStr;
  332.  
  333.     //
  334.     // Update the spread sheet server_info here, set current row and col
  335.     //
  336.     (DDE_GLOBAL::excel_server_info)->SetCurRow( endRow + 1 );
  337.     
  338.     return NewHandle( string );    
  339. }
  340.  
  341. //-----------------------------------------------------------------------------
  342. HSZ EXCEL_ITEM::Range()
  343. {
  344.     char    NumStr[20];
  345.  
  346.     range_string = (DDE_GLOBAL::excel_server_info)->ssrow;
  347.     range_string += "1";
  348.     range_string += (DDE_GLOBAL::excel_server_info)->sscol;
  349.     range_string += "1";
  350.  
  351.     range_string += (DDE_GLOBAL::excel_server_info)->ssdelim;
  352.  
  353.     range_string += (DDE_GLOBAL::excel_server_info)->ssrow;
  354.     _itoa( max_range_row, NumStr, 10 );
  355.     range_string += NumStr;
  356.  
  357.     range_string += (DDE_GLOBAL::excel_server_info)->sscol;
  358.     _itoa( max_range_col, NumStr, 10 );
  359.     range_string += NumStr;
  360.  
  361.     if ( hrange != 0 )
  362.     {
  363.         DdeFreeStringHandle( DDE_GLOBAL::GetInstId(), hrange );
  364.     }
  365.     hrange = DdeCreateStringHandle( DDE_GLOBAL::GetInstId()
  366.                                     , ( char* ) range_string.CString()
  367.                                     , CP_WINANSI );
  368.     return hrange;    
  369.     
  370. }
  371.  
  372. //-----------------------------------------------------------------------------
  373. EXCEL_ITEM::~EXCEL_ITEM()
  374. {
  375.     if ( hrange != 0 )
  376.     {
  377.         DdeFreeStringHandle( DDE_GLOBAL::GetInstId(), hrange );
  378.         hrange = 0;
  379.     }
  380. }
  381.  
  382.