home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 8.ddi / PROGTALK.ZIP / PROGTALK.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  5.5 KB  |  217 lines

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