home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / PROGMAN.PAK / PROGMANX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.8 KB  |  244 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1991, 1995 by Borland International, All Rights Reserved
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/dialog.h>
  7. #include <owl/framewin.h>
  8. #include <owl/applicat.h>
  9. #include <owl/listbox.h>
  10. #include <owl/inputdia.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <ddeml.h>
  14. #include "progmanx.h"     // Dialog item IDs
  15.  
  16.  
  17. //
  18. // TDDEProgTalk is the main window of the application.
  19. // It engages in a DDE conversation with the Program Manager
  20. // to create program groups with a
  21. // user specified list of program items.
  22. //
  23. // See DDEML example
  24.  
  25.  
  26. class TDDEProgTalk : public TDialog{
  27.   public:
  28.     //  Create a TListBox object to represent the
  29.     //  dialog's list box.
  30.     //
  31.     TDDEProgTalk(TWindow* parent, TResId resId) :
  32.       TDialog(parent, resId),
  33.       CallBackProc((FARPROC)CallBack) {
  34.       ListBox = new TListBox(this, ID_LISTBOX);
  35.     }
  36.     ~TDDEProgTalk();
  37.  
  38.   private:
  39.     void SetupWindow();
  40.     void CmAddItem();
  41.     void CmDeleteItem();
  42.     void CmClearItems();
  43.     void CmCreateGroup();
  44.     void EvDDEAck(HWND hWnd, LONG lParam);
  45.  
  46.     TListBox* ListBox;
  47.  
  48.     //DDEML
  49.     static HDDEDATA FAR PASCAL _export CallBack(WORD, WORD, HCONV, HSZ, HSZ,
  50.                                                 HDDEDATA, DWORD, DWORD);
  51.     DWORD   InstId;
  52.     HCONV   HConv;
  53.     HSZ     Service;
  54.     HSZ     Topic;
  55.     TProcInstance CallBackProc;
  56.  
  57.   DECLARE_RESPONSE_TABLE(TDDEProgTalk);
  58. };
  59.  
  60. static TDDEProgTalk* This = 0;
  61.  
  62. DEFINE_RESPONSE_TABLE1(TDDEProgTalk, TDialog)
  63.   EV_COMMAND(CM_ADDITEM, CmAddItem),
  64.   EV_COMMAND(CM_DELETEITEM, CmDeleteItem),
  65.   EV_COMMAND(CM_CLEARITEMS, CmClearItems),
  66.   EV_COMMAND(CM_CREATEGROUP, CmCreateGroup),
  67. END_RESPONSE_TABLE;
  68.  
  69. TDDEProgTalk::~TDDEProgTalk()
  70. {
  71.   // This clean up is required for those resources that were allocated during
  72.   // the DDEML conversation.
  73.   //
  74.   if (HConv)
  75.     DdeDisconnect(HConv);     // Let the other party know we are leaving
  76.  
  77.   if (InstId) {
  78.     DdeFreeStringHandle(InstId, Service);
  79.     DdeFreeStringHandle(InstId, Topic);
  80.     DdeUninitialize(InstId);
  81.   }
  82. }
  83.  
  84. //  SetupWindow is called right after the DDE window
  85. //  is created.  Initiate the DDE conversation.
  86. //
  87. void
  88. TDDEProgTalk::SetupWindow() {
  89.   InstId = 0;         // MUST be 0 the first time DdeInitialize() is called!
  90.   HConv = 0;
  91.   Service = Topic = 0;
  92.   This = this;
  93.  
  94.   TDialog::SetupWindow();
  95.  
  96.   // The code below sets up the DDEML call back function that is used by the
  97.   // DDE Management Library to carry out data transfers between
  98.   // applications.
  99.   //
  100.   if (CallBackProc) {
  101.     if (DdeInitialize(&InstId, (PFNCALLBACK)(FARPROC)CallBackProc, APPCMD_CLIENTONLY, 0) == DMLERR_NO_ERROR) {
  102.       Service = DdeCreateStringHandle(InstId, "PROGMAN", CP_WINANSI);
  103.       Topic = DdeCreateStringHandle(InstId, "PROGMAN", CP_WINANSI);
  104.       if (!Service || !Topic) {
  105.         MessageBox("Creation of strings failed.", Title, MB_ICONSTOP);
  106.         PostQuitMessage(0);
  107.       }
  108.     } else {
  109.       MessageBox("Initialization failed.", Title, MB_ICONSTOP);
  110.       PostQuitMessage(0);
  111.     }
  112.   } else {
  113.     MessageBox("Setup of callback failed.", Title, MB_ICONSTOP);
  114.     PostQuitMessage(0);
  115.   }
  116.  
  117. }
  118.  
  119. //
  120. // Add item button response method. Bring up the Add item dialog to
  121. // input a program item string, and add that item to the list box.
  122. //
  123. void
  124. TDDEProgTalk::CmAddItem()
  125. {
  126.   char name[64] = "";
  127.  
  128.   if (TInputDialog(this, "Add an Item to the Group", "Item &name:",
  129.                    name, sizeof(name)).Execute() != IDCANCEL)
  130.     ListBox->AddString(name);
  131. }
  132.  
  133. //
  134. // Delete item button response method. Delete the currently selected
  135. // item in the list box.
  136. //
  137. void
  138. TDDEProgTalk::CmDeleteItem()
  139. {
  140.   ListBox->DeleteString(ListBox->GetSelIndex());
  141. }
  142.  
  143. //
  144. // Clear items button response method. Clear the list box.
  145. //
  146. void
  147. TDDEProgTalk::CmClearItems()
  148. {
  149.   ListBox->ClearList();
  150. }
  151.  
  152. //
  153. // Create group button response method. Bring up the Create Group
  154. // dialog to input the program group name.
  155. //
  156. void
  157. TDDEProgTalk::CmCreateGroup()
  158. {
  159.   char* createGroup = "[CreateGroup(%s)]";
  160.   char* addItem = "[AddItem(%s)]";
  161.  
  162.   char name[64] = "";
  163.  
  164.   if (TInputDialog(this,
  165.                    "Create a new group", "&Name of new group:",
  166.                    name, sizeof(name)).Execute() != IDCANCEL) {
  167.     // Connect to the prog manager
  168.     HConv = DdeConnect(InstId, Service, Topic, 0);
  169.     if (!HConv) {
  170.       MessageBox("Can't start conversation.\nMake sure PROGMAN is running.", Title, MB_ICONSTOP);
  171.       return;
  172.       }
  173.  
  174.       // Subtract 2 for the '%s' in 'createGroup', plus 1 for null terminator.
  175.       //
  176.       int len = strlen(name) + strlen(createGroup) - 2 + 1;
  177.       int count = ListBox->GetCount();
  178.  
  179.       int i;
  180.       for (i = 0; i < count; i++)
  181.         // Subtract 2 for the '%s' in 'addItem'.
  182.         len += ListBox->GetStringLen(i) + strlen(addItem) - 2;
  183.  
  184.       char* commands = new char[len];
  185.       char* ptr=commands;
  186.       sprintf(ptr, createGroup, name);
  187.       for (i = 0; i < count; i++) {
  188.         ListBox->GetString(name, i);
  189.         ptr += strlen(ptr);
  190.         sprintf(ptr, addItem, name);
  191.       }
  192.  
  193.       //Send command to progman
  194.       if (DdeClientTransaction((LPBYTE)commands, len, HConv, 0L, CF_TEXT, XTYP_EXECUTE, 1000, 0))
  195.         ListBox->ClearList();
  196.  
  197.       delete[] commands;
  198.   }
  199. }
  200.  
  201. //
  202. // This call back function is the heart of interaction between this program
  203. // and DDEML.  Because Windows doesn't pass C++ 'this' pointers to call
  204. // back functions, a static 'this' pointer was used.  If you wanted to
  205. // create a Client that would allow for more than one conversation, using a
  206. // List of conversations and their associated 'this' pointers would be one
  207. // possible method to try.  The XTYP_ constants are described in detail in
  208. // the online help.
  209. //
  210. HDDEDATA FAR PASCAL _export
  211. TDDEProgTalk::CallBack(WORD type, WORD, HCONV /*hConv*/, HSZ, HSZ, HDDEDATA /*hData*/,
  212.                     DWORD, DWORD)
  213. {
  214.   switch (type) {
  215.     case XTYP_DISCONNECT:
  216.       This->MessageBox("Disconnected.", This->Title, MB_ICONINFORMATION);
  217.       This->HConv = 0;
  218.       break;
  219.  
  220.     case XTYP_ERROR:
  221.       This->MessageBox("A critical DDE error has occured.", This->Title, MB_ICONINFORMATION);
  222.   }
  223.   return 0;
  224. }
  225.  
  226. //----------------------------------------------------------------------------
  227.  
  228. // TDdeApp is the application object. It creates a main window of type
  229. // TDDEProgTalk.
  230. //
  231. class TDDEApp : public TApplication {
  232.   public:
  233.     TDDEApp() : TApplication() {}
  234.     void InitMainWindow() {
  235.       MainWindow = new TFrameWindow(0, "ProgTalk", new TDDEProgTalk(0, IDD_PROGTALK), TRUE);
  236.     }
  237. };
  238.  
  239. int
  240. OwlMain(int /*argc*/, char* /*argv*/ [])
  241. {
  242.   return TDDEApp().Run();
  243. }
  244.