home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / memsz331.zip / Source.zip / DDE.H < prev    next >
Text File  |  1995-08-03  |  13KB  |  376 lines

  1. /********************************************************************** DDE.H
  2.  *                                                                          *
  3.  *                      Dynamic Data Exchange Support                       *
  4.  *                                                                          *
  5.  ****************************************************************************/
  6.  
  7. #ifndef DDE_H
  8. #define DDE_H
  9.  
  10. #ifndef OS2_INCLUDED
  11.    #define INCL_BASE
  12.    #define INCL_PM
  13.    #include <os2.h>
  14. #endif
  15.  
  16. #include "Debug.h"
  17.  
  18. #define MAX_DDE_LINKS (50)
  19. #define MAX_DDE_CLIENTS (50)
  20.  
  21. class Dde_Item {
  22.  
  23.    private:
  24.       char *Name ;
  25.       int Format ;
  26.       PVOID Data ;
  27.       int Size ;
  28.       Dde_Item *Prev, *Next ;
  29.       HWND HotLinks [MAX_DDE_LINKS] [2] ;       // [0] Server, [1] Client.
  30.       HWND WarmLinks [MAX_DDE_LINKS] [2] ;      // [0] Server, [1] Client.
  31.  
  32.    public:
  33.       Dde_Item ( char *name, int format, PVOID data, int size ) :
  34.          Format(format), Size(size), Prev(0), Next(0) {
  35.          Name = (char*) malloc ( strlen(name)+1 ) ;
  36.          strcpy ( Name, name ) ;
  37.          Data = malloc ( Size ) ;
  38.          memcpy ( Data, data, Size ) ;
  39.          memset ( HotLinks, 0, sizeof(HotLinks) ) ;
  40.          memset ( WarmLinks, 0, sizeof(WarmLinks) ) ;
  41.       }
  42.  
  43.       ~Dde_Item ( ) {
  44.          free ( Name ) ;
  45.          free ( Data ) ;
  46.       }
  47.  
  48.       char *QueryName ( ) { return ( Name ) ; }
  49.       void *QueryData ( ) { return ( Data ) ; }
  50.       int QuerySize ( ) { return ( Size ) ; }
  51.  
  52.       Dde_Item *QueryPrev ( ) { return ( Prev ) ; }
  53.       Dde_Item *QueryNext ( ) { return ( Next ) ; }
  54.       void SetPrev ( Dde_Item *Item ) { Prev = Item ; }
  55.       void SetNext ( Dde_Item *Item ) { Next = Item ; }
  56.  
  57.       void Update ( int format, PVOID data, int size ) {
  58.          Format = format ;
  59.          if ( ( Size < size ) || ( Size > size*3 ) ) {
  60.             Size = size ;
  61.             free ( Data ) ;
  62.             Data = malloc ( Size ) ;
  63.          } /* endif */
  64.          memcpy ( Data, data, Size ) ;
  65.          BroadcastUpdate ( ) ;
  66.       }
  67.  
  68.       void Query ( int &format, PVOID &data, int &size ) {
  69.          format = Format ;
  70.          data = Data ;
  71.          size = Size ;
  72.       }
  73.  
  74.       void Request   ( HWND Server, HWND Client ) ;
  75.       void Poke      ( HWND Server, HWND Client, int Format, PVOID Data, int Size ) ;
  76.       void Advise    ( HWND Server, HWND Client, BOOL Hot ) ;
  77.       void Unadvise  ( HWND Server, HWND Client ) ;
  78.       void Execute   ( HWND Server, HWND Client, int Format, PVOID Data, int Size ) ;
  79.       void Terminate ( HWND Server, HWND Client ) ;
  80.  
  81.       void BroadcastUpdate ( ) ;
  82. } ;
  83.  
  84. class Dde_Topic {
  85.  
  86.    private:
  87.       char *Name ;
  88.       char *ItemListName ;
  89.       Dde_Item *First, *Last ;
  90.       Dde_Topic *Prev, *Next ;
  91.       HWND Servers [MAX_DDE_CLIENTS] ;
  92.  
  93.    public:
  94.       Dde_Topic ( char *name, char *itemlistname ) : First(0), Last(0), Prev(0), Next(0) {
  95.          Name = (char*) malloc ( strlen(name)+1 ) ;
  96.          strcpy ( Name, name ) ;
  97.          ItemListName = (char*) malloc ( strlen(itemlistname)+1 ) ;
  98.          strcpy ( ItemListName, itemlistname ) ;
  99.          memset ( Servers, 0, sizeof(Servers) ) ;
  100.          AddItem ( ItemListName, DDEFMT_TEXT, "", 1 ) ;
  101.       }
  102.  
  103.       ~Dde_Topic ( ) {
  104.          free ( Name ) ;
  105.          free ( ItemListName ) ;
  106.          while ( First ) {
  107.             Dde_Item *Item = First ;
  108.             First = First->QueryNext() ;
  109.             delete Item ;
  110.          } /* endwhile */
  111.          for ( int i=0; i<sizeof(Servers)/sizeof(Servers[0]); i++ ) 
  112.             if ( Servers[i] ) 
  113.                WinDestroyWindow ( Servers[i] ) ;
  114.       }
  115.  
  116.       char *QueryName ( ) { return ( Name ) ; }
  117.       Dde_Topic *QueryPrev ( ) { return ( Prev ) ; }
  118.       Dde_Topic *QueryNext ( ) { return ( Next ) ; }
  119.  
  120.       void SetPrev ( Dde_Topic *Topic ) { Prev = Topic ; }
  121.       void SetNext ( Dde_Topic *Topic ) { Next = Topic ; }
  122.  
  123.       void AddItem ( Dde_Item *Item ) {
  124.          if ( First ) {
  125.             Last->SetNext ( Item ) ;
  126.             Item->SetPrev ( Last ) ;
  127.          } else {
  128.             First = Item ;
  129.             Item->SetPrev ( 0 ) ;
  130.          } /* endif */
  131.          Last = Item ;
  132.          Item->SetNext ( 0 ) ;
  133.       }
  134.  
  135.       void RemoveItem ( Dde_Item *Item ) {
  136.          if ( Item->QueryPrev() ) 
  137.             Item->QueryPrev()->SetNext ( Item->QueryNext() ) ;
  138.          else
  139.             First = Item->QueryNext() ;
  140.          if ( Item->QueryNext() )
  141.             Item->QueryNext()->SetPrev ( Item->QueryPrev() ) ;
  142.          else
  143.             Last = Item->QueryPrev() ;
  144.          delete Item ;
  145.       }
  146.  
  147.       Dde_Item *FindItem ( char *ItemName ) {
  148.          Dde_Item *Item = First ;
  149.          while ( Item ) {
  150.             if ( !strcmpi ( ItemName, Item->QueryName() ) ) 
  151.                return ( Item ) ;
  152.             Item = Item->QueryNext() ;
  153.          } /* endwhile */
  154.          return ( 0 ) ;
  155.       }
  156.  
  157.       BOOL AddItem ( char *ItemName, int Format, PVOID Data, int Size ) {
  158.          Dde_Item *Item = FindItem ( ItemName ) ;
  159.          if ( Item ) return ( FALSE ) ;
  160.          Item = new Dde_Item ( ItemName, Format, Data, Size ) ;
  161.          AddItem ( Item ) ;
  162.          UpdateItemList ( ) ;
  163.          return ( TRUE ) ;
  164.       }
  165.  
  166.       BOOL UpdateItem ( char *ItemName, int Format, PVOID Data, int Size ) {
  167.          Dde_Item *Item = FindItem ( ItemName ) ;
  168.          if ( Item == 0 ) return ( FALSE ) ;
  169.          Item->Update ( Format, Data, Size ) ;
  170.          return ( TRUE ) ;
  171.       }
  172.  
  173.       BOOL RemoveItem ( char *ItemName ) {
  174.          Dde_Item *Item = FindItem ( ItemName ) ;
  175.          if ( Item == 0 ) return ( FALSE ) ;
  176.          RemoveItem ( Item ) ;
  177.          UpdateItemList ( ) ;
  178.          return ( TRUE ) ;
  179.       }
  180.  
  181.       BOOL QueryItem ( char *ItemName, int &Format, PVOID &Data, int &Size ) {
  182.          Dde_Item *Item = FindItem ( ItemName ) ;
  183.          if ( Item == 0 ) return ( FALSE ) ;
  184.          Item->Query ( Format, Data, Size ) ;
  185.          return ( TRUE ) ;
  186.       }
  187.  
  188.       void UpdateItemList ( ) {
  189.          // Determine how large the item list is.
  190.          int ListSize = 0 ;
  191.          Dde_Item *Item = First ;
  192.          while ( Item ) {
  193.             ListSize += strlen(Item->QueryName()) + 1 ;
  194.             Item = Item->QueryNext() ;
  195.          } /* endwhile */
  196.          // Allocate memory for the list.
  197.          char *ItemList = (char*) malloc ( ListSize ) ;
  198.          // Fill the item list.
  199.          ItemList[0] = 0 ;
  200.          Item = First ;
  201.          while ( Item ) {
  202.             strcat ( ItemList, Item->QueryName() ) ;
  203.             if ( Item->QueryNext() ) 
  204.                strcat ( ItemList, "\t" ) ;
  205.             Item = Item->QueryNext() ;
  206.          } /* endwhile */
  207.          // Update the item list item.
  208.          Item = First ;
  209.          while ( Item ) {
  210.             if ( !strcmpi ( ItemListName, Item->QueryName() ) ) {
  211.                Item->Update ( DDEFMT_TEXT, ItemList, strlen(ItemList)+1 ) ;
  212.                break ;
  213.             } /* endif */
  214.             Item = Item->QueryNext() ;
  215.          } /* endwhile */
  216.       }
  217.  
  218.       void Initiate  ( HWND Owner,  HWND Client, PDDEINIT pDdeInit, CONVCONTEXT &Conv, char *AppName ) ;
  219.       void Request   ( HWND Server, HWND Client, char *ItemName ) ;
  220.       void Poke      ( HWND Server, HWND Client, char *ItemName, int Format, PVOID Data, int Size ) ;
  221.       void Advise    ( HWND Server, HWND Client, char *ItemName, BOOL Hot ) ;
  222.       void Unadvise  ( HWND Server, HWND Client, char *ItemName ) ;
  223.       void Execute   ( HWND Server, HWND Client, char *ItemName, int Format, PVOID Data, int Size ) ;
  224.       void Terminate ( HWND Server, HWND Client, BOOL DestroyServer ) ;
  225. } ;
  226.  
  227. class Dde_Application {
  228.  
  229.    private:
  230.       char *Name ;
  231.       Dde_Topic *First, *Last ;
  232.  
  233.    public:
  234.       Dde_Application ( char *name ) : First(0), Last(0) {
  235.          Name = (char*) malloc ( strlen(name)+1 ) ;
  236.          strcpy ( Name, name ) ;
  237.       }
  238.  
  239.       ~Dde_Application ( ) {
  240.          free ( Name ) ;
  241.          while ( First ) {
  242.             Dde_Topic *Topic = First ;
  243.             First = First->QueryNext() ;
  244.             delete Topic ;
  245.          } /* endwhile */
  246.       }
  247.  
  248.       char *QueryName ( ) { return ( Name ) ; }
  249.  
  250.       void AddTopic ( Dde_Topic *Topic ) {
  251.          if ( First ) {
  252.             Last->SetNext ( Topic ) ;
  253.             Topic->SetPrev ( Last ) ;
  254.          } else {
  255.             First = Topic ;
  256.             Topic->SetPrev ( 0 ) ;
  257.          } /* endif */
  258.          Last = Topic ;
  259.          Topic->SetNext ( 0 ) ;
  260.       }
  261.  
  262.       void RemoveTopic ( Dde_Topic *Topic ) {
  263.          if ( Topic->QueryPrev() ) 
  264.             Topic->QueryPrev()->SetNext ( Topic->QueryNext() ) ;
  265.          else
  266.             First = Topic->QueryNext() ;
  267.          if ( Topic->QueryNext() )
  268.             Topic->QueryNext()->SetPrev ( Topic->QueryPrev() ) ;
  269.          else
  270.             Last = Topic->QueryPrev() ;
  271.          delete Topic ;
  272.       }
  273.  
  274.       Dde_Topic *FindTopic ( char *TopicName ) {
  275.          Dde_Topic *Topic = First ;
  276.          while ( Topic ) {
  277.             if ( !strcmpi ( TopicName, Topic->QueryName() ) ) 
  278.                return ( Topic ) ;
  279.             Topic = Topic->QueryNext() ;
  280.          } /* endwhile */
  281.          return ( 0 ) ;
  282.       }
  283.  
  284.       BOOL AddTopic ( char *TopicName, char *ItemListName ) {
  285.          Dde_Topic *Topic = FindTopic ( TopicName ) ;
  286.          if ( Topic ) return ( FALSE ) ;
  287.          Topic = new Dde_Topic ( TopicName, ItemListName ) ;
  288.          AddTopic ( Topic ) ;
  289.          return ( TRUE ) ;
  290.       }
  291.  
  292.       BOOL RemoveTopic ( char *TopicName ) {
  293.          Dde_Topic *Topic = FindTopic ( TopicName ) ;
  294.          if ( Topic == 0 ) return ( FALSE ) ;
  295.          RemoveTopic ( Topic ) ;
  296.          return ( TRUE ) ;
  297.       }
  298.  
  299.       Dde_Item *FindItem ( char *TopicName, char *ItemName ) {
  300.          Dde_Topic *Topic = FindTopic ( TopicName ) ;
  301.          return ( Topic ? Topic->FindItem ( ItemName ) : 0 ) ;
  302.       }
  303.  
  304.       BOOL AddItem ( char *TopicName, char *ItemName, int Format, PVOID Data, int Size ) {
  305.          Dde_Topic *Topic = FindTopic ( TopicName ) ;
  306.          return ( Topic ? Topic->AddItem ( ItemName, Format, Data, Size ) : FALSE ) ;
  307.       }
  308.  
  309.       BOOL UpdateItem ( char *TopicName, char *ItemName, int Format, PVOID Data, int Size ) {
  310.          Dde_Topic *Topic = FindTopic ( TopicName ) ;
  311.          return ( Topic ? Topic->UpdateItem ( ItemName, Format, Data, Size ) : FALSE ) ;
  312.       }
  313.  
  314.       BOOL RemoveItem ( char *TopicName, char *ItemName ) {
  315.          Dde_Topic *Topic = FindTopic ( TopicName ) ;
  316.          return ( Topic ? Topic->RemoveItem ( ItemName ) : FALSE ) ;
  317.       }
  318.  
  319.       BOOL QueryItem ( char *TopicName, char *ItemName, int &Format, PVOID &Data, int &Size ) {
  320.          Dde_Topic *Topic = FindTopic ( TopicName ) ;
  321.          return ( Topic ? Topic->QueryItem ( ItemName, Format, Data, Size ) : FALSE ) ;
  322.       }
  323.  
  324.       void Initiate ( HWND Owner, HWND Client, PDDEINIT pDdeInit, CONVCONTEXT &Conv ) ;
  325. } ;
  326.  
  327. class Dde_Server {
  328.  
  329.    private:
  330.       BOOL Ready ;
  331.       Dde_Application Application ;
  332.  
  333.    public:
  334.       Dde_Server ( HAB Anchor, HMODULE Library, HWND Owner, char *AppName ) ;
  335.  
  336.       ~Dde_Server ( ) { }
  337.  
  338.       void Initiate ( HWND Owner, HWND Client, PDDEINIT pDdeInit ) ;
  339.  
  340.       BOOL IsReady ( ) { return ( Ready ) ; }
  341.  
  342.       BOOL AddTopic ( char *TopicName, char *ItemListName ) {
  343.          return ( Application.AddTopic ( TopicName, ItemListName ) ) ;
  344.       }
  345.  
  346.       BOOL RemoveTopic ( char *TopicName ) {
  347.          return ( Application.RemoveTopic ( TopicName ) ) ;
  348.       }
  349.  
  350.       BOOL AddItem ( char *TopicName, char *ItemName, int Format, PVOID Data, int Size ) {
  351.          return ( Application.AddItem ( TopicName, ItemName, Format, Data, Size ) ) ;
  352.       }
  353.  
  354.       BOOL UpdateItem ( char *TopicName, char *ItemName, int Format, PVOID Data, int Size ) {
  355.          return ( Application.UpdateItem ( TopicName, ItemName, Format, Data, Size ) ) ;
  356.       }
  357.  
  358.       BOOL RemoveItem ( char *TopicName, char *ItemName ) {
  359.          return ( Application.RemoveItem ( TopicName, ItemName ) ) ;
  360.       }
  361.  
  362.       BOOL QueryItem ( char *TopicName, char *ItemName, int &Format, PVOID &Data, int &Size ) {
  363.          return ( Application.QueryItem ( TopicName, ItemName, Format, Data, Size ) ) ;
  364.       }
  365.  
  366.       Dde_Topic *FindTopic ( char *TopicName ) {
  367.          return ( Application.FindTopic ( TopicName ) ) ;
  368.       }
  369.  
  370.       Dde_Item *FindItem ( char *TopicName, char *ItemName ) {
  371.          return ( Application.FindItem ( TopicName, ItemName ) ) ;
  372.       }
  373. } ;
  374.  
  375. #endif
  376.