home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / BCPPOWL3.ZIP / PROGTALK.ZIP / PROGTALK.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  5.6 KB  |  221 lines

  1. // OWL headers
  2. #include <owl.h>
  3. #include <dialog.h>
  4. #include <listbox.h>
  5. #include <inputdia.h>
  6.  
  7. #if defined(wsprintf)
  8. #undef wsprintf
  9. #define wsprintf wsprintf        // reverse windows.h
  10. extern "C" int FAR cdecl wsprintf(LPSTR,LPSTR,...);
  11. #endif
  12.  
  13. // Standard headers
  14. #include <string.h>
  15.  
  16. // Dialog item IDs
  17. #include "progtalk.h"
  18.  
  19. // Class definition of TDDEClient
  20. #include "ddeclien.h"
  21.  
  22. /*
  23.     TDDEProgTalk is the main window of the application.
  24.     It engages in a DDE conversation with the Program Manager
  25.     to create program groups with a
  26.     user specified list of program items.
  27. */
  28. class TDDEProgTalk : public TDDEClient
  29. {
  30.     private:
  31.         PTListBox ListBox;
  32.  
  33.     public:
  34.  
  35.         /*
  36.             TDDEProgTalk window constructor.
  37.             Create a TListBox object to represent the
  38.             dialog's list box.
  39.         */
  40.         TDDEProgTalk( PTWindowsObject Parent, LPSTR AName )
  41.             : TDDEClient( Parent, AName )
  42.         {
  43.             ListBox = new TListBox( this, ID_LISTBOX );
  44.         }
  45.         /*
  46.             SetupWindow is called right after the DDE window
  47.             is created.  Initiate the DDE conversation.
  48.         */
  49.         virtual void SetupWindow( void )
  50.         {
  51.                         TDialog::SetupWindow();
  52.             InitiateDDE( "PROGMAN", "PROGMAN" );
  53.         }
  54.         virtual void AddItem( TMessage& Msg )
  55.             = [ ID_FIRST + ID_ADDITEM ];
  56.         virtual void DeleteItem( TMessage& Msg )
  57.             = [ ID_FIRST + ID_DELETEITEM ];
  58.         virtual void ClearItems( TMessage& Msg )
  59.             = [ ID_FIRST + ID_CLEARITEMS ];
  60.         virtual void CreateGroup( TMessage& Msg )
  61.             = [ ID_FIRST + ID_CREATEGROUP ];
  62.         virtual void WMDDEAck( TMessage& Msg )
  63.             = [ WM_FIRST + WM_DDE_ACK ];
  64. };
  65.  
  66. /*
  67.     Add item button response method. Bring up the Add item dialog to
  68.     input a program item string, and add that item to the list box.
  69. */
  70.  
  71. void TDDEProgTalk::AddItem( TMessage& )
  72. {
  73.     char Name[ 64 ] = "";
  74.  
  75.     if (GetApplication()->ExecDialog( new TInputDialog( this,
  76.         "Add an Item to the Group",
  77.         "Item &name:",
  78.         Name,
  79.         sizeof( Name ) ) ) != IDCANCEL )
  80.             ListBox->AddString( Name );
  81. }
  82.  
  83. /*
  84.     Delete item button response method. Delete the currently selected
  85.     item in the list box.
  86. */
  87.  
  88. void TDDEProgTalk::DeleteItem( TMessage& )
  89. {
  90.     ListBox->DeleteString( ListBox->GetSelIndex() );
  91. }
  92.  
  93. // Clear items button response method. Clear the list box.
  94.  
  95. void TDDEProgTalk::ClearItems( TMessage& )
  96. {
  97.     ListBox->ClearList();
  98. }
  99.  
  100. /*
  101.     Create group button response method. Bring up the Create Group
  102.     dialog to input the program group name. Then, if a DDE link has
  103.     been established (ServerWindow != 0) and there is no DDE message
  104.     currently pending (PendingMessage == 0), build a list of Program
  105.     Manager commands, and submit the commands using a WM_DDE_EXECUTE
  106.     message. To build the command list, first calculate the total
  107.     length of the list, then allocate a global memory block of that
  108.     size, and finally store the command list as a null-terminated
  109.     string in the memory block.
  110. */
  111.  
  112. void TDDEProgTalk::CreateGroup( TMessage& )
  113. {
  114.     LPSTR lpCreateGroup = "[CreateGroup(%s)]";
  115.     LPSTR lpAddItem = "[AddItem(%s)]";
  116.  
  117.     BOOL Executed;
  118.     int i, len;
  119.     HANDLE HCommands;
  120.     LPSTR lpName, lpCommands;
  121.     char Name[ 64 ] = "";
  122.  
  123.     if (GetApplication()->ExecDialog( new TInputDialog( this,
  124.         "Create a new group",
  125.         "&Name of new group:",
  126.         Name,
  127.         sizeof( Name ) ) ) != IDCANCEL )
  128.     {
  129.         Executed = False;
  130.         if ( ( ServerWindow != 0 ) && ( PendingMessage == 0 ) )
  131.         {
  132.             // Subtract 2 for the '%s' in 'lpCreateGroup'
  133.             // plus 1 for null terminator.
  134.             len = strlen( Name ) + _fstrlen( lpCreateGroup ) - 2 + 1;
  135.             int count = ListBox->GetCount();
  136.             for ( i = 0; i < count; i++ )
  137.                 // Subtract 2 for the '%s' in 'lpAddItem'.
  138.                 len += ListBox->GetStringLen( i ) +
  139.                     _fstrlen( lpAddItem ) - 2;
  140.             HCommands = GlobalAlloc( GHND | GMEM_DDESHARE, len );
  141.             if ( HCommands != 0 )
  142.             {
  143.                 lpName = Name;
  144.                 lpCommands = GlobalLock( HCommands );
  145.                 wsprintf( lpCommands, lpCreateGroup,
  146.                     lpName );
  147.                 int count = ListBox->GetCount();
  148.                 for ( i = 0; i < count; i++ )
  149.                 {
  150.                     ListBox->GetString( Name, i );
  151.                     lpCommands += _fstrlen( lpCommands );
  152.                     wsprintf( lpCommands, lpAddItem,
  153.                         lpName );
  154.                 }
  155.                 GlobalUnlock( HCommands );
  156.                 if ( PostMessage( ServerWindow,
  157.                     WM_DDE_EXECUTE,
  158.                     HWindow,
  159.                     MAKELONG( 0, HCommands ) ) )
  160.                 {
  161.                     PendingMessage = WM_DDE_EXECUTE;
  162.                     Executed = True;
  163.                 }
  164.                 else
  165.                     GlobalFree( HCommands );
  166.             }
  167.         }
  168.         if ( ! Executed )
  169.             MessageBox( HWindow,
  170.                 "Program Manager DDE execute failed.",
  171.                 "Error",
  172.                 MB_ICONEXCLAMATION | MB_OK );
  173.     }
  174. }
  175.  
  176. /*
  177.     WM_DDE_ACK message response method.
  178.     If the current DDE message is a    WM_DDE_EXECUTE,
  179.     free the command string memory block and focus our
  180.     window (as usual), and then clear the list box.
  181. */
  182.  
  183. void TDDEProgTalk::WMDDEAck( TMessage& Msg )
  184. {
  185.     TDDEClient::WMDDEAck( Msg );
  186.     if ( PendingMessage == WM_DDE_EXECUTE )
  187.         ListBox->ClearList();
  188. }
  189.  
  190.  
  191. /*
  192.     TDDEApp is the application object. It creates a main window of type
  193.     TDDEProgTalk.
  194. */
  195.  
  196. class TDDEApp : public TApplication
  197. {
  198.     public:
  199.         TDDEApp(LPSTR AName, HANDLE hInstance, 
  200.             HANDLE hPrevInstance, LPSTR lpCmd,
  201.             int nCmdShow)
  202.             : TApplication(AName, hInstance, 
  203.                    hPrevInstance, lpCmd, nCmdShow) {};
  204.         virtual void InitMainWindow( void );
  205. };
  206.  
  207. // Create a DDE window as the application's main window.
  208. void TDDEApp::InitMainWindow( void )
  209. {
  210.     MainWindow = new TDDEProgTalk( 0, "ProgTalk" );
  211. }
  212.  
  213. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  214.            LPSTR lpCmd, int nCmdShow)
  215. {
  216.     TDDEApp DDEApp ( "ProgTalk", hInstance, hPrevInstance,
  217.         lpCmd, nCmdShow);
  218.     DDEApp.Run();
  219.     return ( DDEApp.Status );
  220. }
  221.