home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / dde / ddeclnt / aclient.cpp next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  18.1 KB  |  431 lines

  1. /******************************************************************************
  2. * .FILE:         aclient.cpp                                                  *
  3. *                                                                             *
  4. * .DESCRIPTION:  Dynamic Data Exchange Client Program: Class Implementation   *
  5. *                                                                             *
  6. * .CLASSES:      AClientWindow                                                *
  7. *                AConversation                                                *
  8. *                                                                             *
  9. * .COPYRIGHT:                                                                 *
  10. *    Licensed Material - Program-Property of IBM                              *
  11. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  12. *                                                                             *
  13. * .DISCLAIMER:                                                                *
  14. *   The following [enclosed] code is sample code created by IBM               *
  15. *   Corporation.  This sample code is not part of any standard IBM product    *
  16. *   and is provided to you solely for the purpose of assisting you in the     *
  17. *   development of your applications.  The code is provided 'AS IS',          *
  18. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  19. *   arising out of your use of the sample code, even if they have been        *
  20. *   advised of the possibility of such damages.                               *
  21. *                                                                             *
  22. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  23. *                                                                             *
  24. ******************************************************************************/
  25. #ifndef _IBASE_                         //Make sure ibase.hpp is included
  26.   #include <ibase.hpp>                  //  since that is where IC_<environ>
  27. #endif                                  //  is defined.
  28. #include <imenubar.hpp>                 //IMenuBar Class
  29. #include <imle.hpp>                     //IMultiLineEdit (MLE) Class
  30. #include <istattxt.hpp>                 //IStatic Text
  31. #include <iapp.hpp>                     //IApplication Classes
  32. #include <istring.hpp>                  //IString Class
  33. #include <iddeevt.hpp>                  //IDDEEvent Classes
  34. #include <iinfoa.hpp>                   //IInfoArea Class
  35. #include <isplitcv.hpp>                 //ISplitCanvas Class
  36. #include <ifont.hpp>                    //IFont Class
  37. #include <imsgbox.hpp>                  //IMessageBox Class
  38. #include <icolor.hpp>                   //IColor
  39. #include <icoordsy.hpp>
  40. #include "aclient.h"
  41. #include "aclient.hpp"
  42.  
  43. //*************************************************************************
  44. // main  - Application entry point
  45. //*************************************************************************
  46. int main(int argc,char **argv)
  47. {
  48.   ICoordinateSystem::setApplicationOrientation(
  49.           ICoordinateSystem::originLowerLeft );
  50.  
  51.    AClientWindow clientWindow(WND_CLIENT);//Create main window
  52.  
  53.    IApplication::current().run();       //Display main window & start events
  54.  
  55.    return 0;
  56. } /* end main */
  57.  
  58. //**************************************************************************
  59. // AClientWindow :: AClientWindow - Constructor for our main window
  60. //**************************************************************************
  61. AClientWindow :: AClientWindow(unsigned long windowId)
  62.   : IFrameWindow (windowId),            //Call IFrameWindow constructor
  63.     menuBar(WND_CLIENT, this),
  64.     statusLine(WND_STATUS, this, this), //Create status line
  65.     todoStatus(WND_TODOSTA, &statusLine, //Create todo status line
  66.       &statusLine),
  67.     workStatus(WND_WORKSTA, &statusLine,//Create work status line
  68.       &statusLine),
  69.     doneStatus(WND_DONESTA, &statusLine,//Create done status line
  70.       &statusLine),
  71.     cArea(WND_CAREA, this, this),       //Create Client Area
  72.     workWindow(WND_WORKW, &cArea, this),//Create work window
  73.     mle(WND_MLE, &cArea, &cArea),       //Create MLE to display DDE activities
  74.     infoArea(this),                     //Create Information Area
  75.     resLib()
  76. {
  77.   handleEventsFor(this);                //Set self as command event handler
  78.   conversation = 0;
  79.  
  80.   cArea.setOrientation(                 //Set the orientation
  81.     ISplitCanvas::horizontalSplit);     //  to horizontal
  82.   setIcon( id() );                      //Set the icon
  83.   setClient(&cArea);                    //Set Client to cArea
  84.  
  85.   addExtension(&statusLine,             //Add Status Line above the client
  86.     IFrameWindow::aboveClient,          //  and specify the height
  87.     IFont(&statusLine).maxCharHeight());//  and specify height
  88.  
  89.   setExtensionSize(&infoArea,           //
  90.          IFont(&infoArea).maxCharHeight());//and specify height
  91.   infoArea.setInactiveText(STR_HOTNO);  //Set information area text from RC
  92.  
  93.   todoCount=0;
  94.   workCount=0;
  95.   doneCount=0;
  96.  
  97.   menuBar.disableItem(MI_END_CONV);     //Disable End Conversation
  98.   menuBar.disableItem(MI_REQUEST_DATA); //Disable request data
  99.   menuBar.disableItem(MI_POKE_DATA);    //Disable poke data
  100.   menuBar.disableItem(MI_BEGIN_HOTLINK);//Disable begin hot link
  101.   menuBar.disableItem(MI_END_HOTLINK);  //Disable end hot link
  102.  
  103.   workWindow.setAlignment(              //Set Alignment to Center in both
  104.     IStaticText::centerCenter);         //  directions
  105.   IFont tempFont("Helv", 16);           //Create Temp. Font
  106.   workWindow.setFont(tempFont);         //Set Font for Work Item Text
  107.  
  108.   sizeTo(ISize(400,300));               //Set the size of main window
  109.   updateStatus();                       //Update status line
  110.   setFocus();                           //Set Focus to main window
  111.   show();
  112.  
  113. } /* end AClientWindow :: AClientWindow(...) */
  114.  
  115. //**************************************************************************
  116. // AClientWindow :: AClientWindow - Destructor for our main window
  117. //**************************************************************************
  118. AClientWindow :: ~AClientWindow()
  119. {
  120.   if (conversation)
  121.   {
  122.     try
  123.     {
  124.       conversation->endHotLinks();
  125.     }
  126.     catch (IException& exc)             //Catch Error
  127.     {
  128.       delete conversation;
  129.     }
  130.     delete conversation;
  131.   } /* endif */
  132. } /* end AClientWindow :: ~AClientWindow() */
  133.  
  134. //**************************************************************************
  135. // AClientWindow :: command(..)
  136. //**************************************************************************
  137. IBase::Boolean AClientWindow::command(ICommandEvent& cmdevt)
  138. {
  139.   IResourceLibrary resLib;
  140.   switch(cmdevt.commandId())
  141.   {
  142.     case MI_START_CONV:
  143.     {
  144.       conversation=new AConversation(this);
  145.       log( MI_BEGIN_SENT );
  146.       conversation->begin(
  147.         resLib.loadString( MI_APP ),
  148.         resLib.loadString( MI_TOPIC ) );
  149.       menuBar.disableItem(MI_START_CONV);   //Disable Start Conversation
  150.       menuBar.enableItem(MI_END_CONV);      //Enable End Conversation
  151.       menuBar.enableItem(MI_REQUEST_DATA);  //Enable request data
  152.       menuBar.enableItem(MI_POKE_DATA);     //Enable poke data
  153.       menuBar.enableItem(MI_BEGIN_HOTLINK); //Enable begin hot link
  154.       break;
  155.     }
  156.     case MI_END_CONV:
  157.     {
  158.       menuBar.enableItem(MI_START_CONV);    //Enable Start Conversation
  159.       menuBar.disableItem(MI_END_CONV);     //Disable End Conversation
  160.       menuBar.disableItem(MI_REQUEST_DATA); //Enable request data
  161.       menuBar.disableItem(MI_POKE_DATA);    //Enable poke data
  162.       if (!menuBar.isItemEnabled(MI_BEGIN_HOTLINK))
  163.       {
  164.         log( MI_END_HOT_SENT );
  165.         conversation->endHotLinks();
  166.       }
  167.       log( MI_END_SENT );
  168.       conversation->end();
  169.       menuBar.disableItem(MI_BEGIN_HOTLINK); //Disable begin hot link
  170.       menuBar.disableItem(MI_END_HOTLINK);   //Disable end hot link
  171.       infoArea.setInactiveText(STR_HOTNO);   //Set information area text from RC
  172.       todoStatus.setForegroundColor(         //Set todo status color
  173.         IColor(IColor::blue));
  174.       workStatus.setForegroundColor(         //Set work status color
  175.         IColor(IColor::blue));
  176.       doneStatus.setForegroundColor(         //Set done status color
  177.         IColor(IColor::blue));
  178.       break;
  179.     }
  180.     case MI_REQUEST_DATA:
  181.     {
  182.       workItemDone();                       //Set work item done
  183.       log( MI_REQDATA_SENT );
  184.       try
  185.       {
  186.         conversation->requestData( resLib.loadString( MI_NEXTTODO ));
  187.       }
  188.       catch (IException& exc)           //Catch Error
  189.       {
  190.         IMessageBox mbox(this);         //Create Message Box
  191.         mbox.show(exc);
  192.         return false;                   //Return error
  193.       }
  194.       break;
  195.     }
  196.     case MI_POKE_DATA:
  197.     {
  198.       workItemDone();                   //Set work item done
  199.       break;
  200.     }
  201.     case MI_BEGIN_HOTLINK:
  202.     {
  203.       log( MI_BEGIN_HOTLINK_HOTTODO_SENT );
  204.       conversation->beginHotLink( resLib.loadString( MI_HOTTODO ) );
  205.       log( MI_BEGIN_HOTLINK_HOTWORK_SENT );
  206.       conversation->beginHotLink( resLib.loadString( MI_HOTWORK ) );
  207.       log( MI_BEGIN_HOTLINK_HOTDONE_SENT );
  208.       conversation->beginHotLink( resLib.loadString( MI_HOTDONE ) );
  209.       menuBar.disableItem(MI_BEGIN_HOTLINK);//Disable begin hot link
  210.       menuBar.enableItem(MI_END_HOTLINK);   //Enable end hot link
  211.       infoArea.setInactiveText(STR_HOTYES); //Set information area text from RC
  212.       todoStatus.setForegroundColor(        //Set todo status color
  213.         IColor(IColor::red));
  214.       workStatus.setForegroundColor(        //Set work status color
  215.         IColor(IColor::red));
  216.       doneStatus.setForegroundColor(        //Set done status color
  217.         IColor(IColor::red));
  218.       conversation->requestData( resLib.loadString( MI_HOTTODO ) );
  219.       conversation->requestData( resLib.loadString( MI_HOTWORK ) );
  220.       conversation->requestData( resLib.loadString( MI_HOTDONE ) );
  221.       break;
  222.     }
  223.     case MI_END_HOTLINK:
  224.     {
  225.       log( MI_END_HOT_SENT );
  226.       conversation->endHotLinks();
  227.       menuBar.enableItem(MI_BEGIN_HOTLINK); //Enable begin hot link
  228.       menuBar.disableItem(MI_END_HOTLINK);  //Disable end hot link
  229.       infoArea.setInactiveText(STR_HOTNO);  //Set information area text from RC
  230.       todoStatus.setForegroundColor(        //Set todo status color
  231.         IColor(IColor::blue));
  232.       workStatus.setForegroundColor(        //Set work status color
  233.         IColor(IColor::blue));
  234.       doneStatus.setForegroundColor(        //Set done status color
  235.         IColor(IColor::blue));
  236.       break;
  237.     }
  238.     default:
  239.     {
  240.       return false;
  241.     }
  242.   }
  243.   return true;
  244. } /* end AClientWindow :: command(..) */
  245.  
  246. //**************************************************************************
  247. // AClientWindow :: log(..)
  248. //**************************************************************************
  249. IBase::Boolean AClientWindow::log( IResourceId resId )
  250. {
  251.   IResourceLibrary resLib;
  252.   mle.addLineAsLast(resLib.loadString( resId ));    //Add to mle message log
  253.  
  254.   return true;
  255. } /* end AClientWindow :: log(..) */
  256.  
  257. //**************************************************************************
  258. // AClientWindow :: log(..)
  259. //**************************************************************************
  260. IBase::Boolean AClientWindow::log( const char * message )
  261. {
  262.   mle.addLineAsLast( message );                     //Add to mle message log
  263.  
  264.   return true;
  265. } /* end AClientWindow :: log(..) */
  266.  
  267. //**************************************************************************
  268. // AClientWindow :: workItemDone(..)
  269. //**************************************************************************
  270. IBase::Boolean AClientWindow::workItemDone()
  271. {
  272.   IResourceLibrary resLib;
  273.   IString workItem(workWindow.text());
  274.   if (workItem.size() > 1)
  275.   {
  276.     log( MI_POKEDATA_SENT );
  277.     conversation->pokeData(
  278.       resLib.loadString( MI_DONEPOKE ), (char*)workItem,
  279.        workItem.size());
  280.     setWorkItem("");
  281.   } /* endif */
  282.   return true;
  283. } /* end AClientWindow :: workItemDone(..) */
  284.  
  285. //**************************************************************************
  286. // AClientWindow :: setWorkItem(..)
  287. //**************************************************************************
  288. IBase::Boolean AClientWindow::setWorkItem(char * message)
  289. {
  290.   workWindow.setText(message);          //Update new work item
  291.   return true;
  292. } /* end AClientWindow :: setWorkItem(..) */
  293.  
  294. //**************************************************************************
  295. // AClientWindow :: workItem(..)
  296. //**************************************************************************
  297. IString AClientWindow::workItem()
  298. {
  299.   return IString(workWindow.text());
  300. } /* end AClientWindow :: workItem(..) */
  301.  
  302. //**************************************************************************
  303. // AClientWindow :: updateStatus()
  304. //**************************************************************************
  305. IBase::Boolean AClientWindow::updateStatus()
  306. {
  307.   todoStatus.setText(                   //Set the status to todo count
  308.     IString(resLib.loadString( MI_TODO_COUNT ) )+
  309.     IString(todoCount));
  310.   workStatus.setText(                   //Set the status to work count
  311.     IString(resLib.loadString( MI_WORK_COUNT ) )+
  312.     IString(workCount));
  313.   doneStatus.setText(                   //Set the status to done count
  314.     IString(resLib.loadString( MI_DONE_COUNT ) )+
  315.     IString(doneCount));
  316.   return true;
  317. } /* end AClientWindow :: updateStatus() */
  318.  
  319. //**************************************************************************
  320. // AClientWindow :: updateTodo(..)
  321. //**************************************************************************
  322. IBase::Boolean AClientWindow::updateTodo(char * newCount)
  323. {
  324.   todoStatus.setText(                   //Set the status to todo count
  325.     IString(resLib.loadString( MI_TODO_COUNT ) )+
  326.     IString(newCount));
  327.   return true;
  328. } /* end AClientWindow :: updateTodo(..) */
  329.  
  330. //**************************************************************************
  331. // AClientWindow :: updateWork(..)
  332. //**************************************************************************
  333. IBase::Boolean AClientWindow::updateWork(char * newCount)
  334. {
  335.   workStatus.setText(                   //Set the status to work count
  336.     IString(resLib.loadString( MI_WORK_COUNT ) )+
  337.     IString(newCount));
  338.   return true;
  339. } /* end AClientWindow :: updateWork(..) */
  340.  
  341. //**************************************************************************
  342. // AClientWindow :: updateDone(..)
  343. //**************************************************************************
  344. IBase::Boolean AClientWindow::updateDone(char * newCount)
  345. {
  346.   doneStatus.setText(                   //Set the status to done count
  347.     IString(resLib.loadString( MI_DONE_COUNT ) )+
  348.     IString(newCount));
  349.   return true;
  350. } /* end AClientWindow :: updateDone(..) */
  351.  
  352. //**************************************************************************
  353. // AConversation :: AConversation - Constructor for our conversation
  354. //**************************************************************************
  355. AConversation::AConversation(AClientWindow* frameWin)
  356.               : IDDEClientConversation(false),
  357.                 clientWindow(frameWin),
  358.                 resLib()
  359. {
  360. }
  361.  
  362. //**************************************************************************
  363. // AConversation :: data(..)
  364. //**************************************************************************
  365. IBase::Boolean AConversation :: data(IDDEDataEvent& event)
  366. {
  367.   IString newWorkItem;
  368.   IString newData(event.data());
  369.   IString item(event.item());
  370.  
  371.   if (event.isDataFromHotLink())        //Is Data from hot link?
  372.   {
  373.     mainWindow()->log(IString(resLib.loadString( MI_DATA_HOTLINK ) )
  374.       + item + IString("):")
  375.       + newData
  376.       + IString(resLib.loadString( MI_RECEIVED )));
  377.   }
  378.   else                                  //New work item from server
  379.   {
  380.     mainWindow()->log(IString(resLib.loadString( MI_DATA ))
  381.       + item + IString("):")
  382.       + newData
  383.       + IString(resLib.loadString( MI_RECEIVED )));
  384.   } /* endif */
  385.  
  386.   if (item == IString(resLib.loadString( MI_NEXTTODO )))    //Is item the Next Todo?
  387.   {
  388.     mainWindow()->setWorkItem(newData);
  389.   } /* endif */
  390.   if (item == IString(resLib.loadString( MI_HOTTODO )))     //Is item the todo counter?
  391.   {
  392.     mainWindow()->updateTodo(newData);
  393.   } /* endif */
  394.   if (item == IString(resLib.loadString( MI_HOTWORK )))     //Is item the work counter?
  395.   {
  396.     mainWindow()->updateWork(newData);
  397.   } /* endif */
  398.   if (item == IString(resLib.loadString( MI_HOTDONE )))     //Is item the done counter?
  399.   {
  400.     mainWindow()->updateDone(newData);
  401.   } /* endif */
  402.   return true;
  403. } /* end AConversation :: data(..) */
  404.  
  405. void AConversation :: conversationEnded(IDDEClientEndEvent& endEvt)
  406. /**********************************************************/
  407. /* Do any cleanup on close of conversation by server      */
  408. /**********************************************************/
  409. {
  410.    if (endEvt.sourceOfEnd() == IDDEEndEvent::server)
  411.      mainWindow()->log(IString(resLib.loadString( MI_TOPIC_END )) +
  412.        IString(endEvt.topic()) + IString(resLib.loadString( MI_IN_APP )) +
  413.        IString(endEvt.application()) + IString(resLib.loadString( MI_BY_SERVER )));
  414.    else if (endEvt.sourceOfEnd() == IDDEEndEvent::client)
  415.      mainWindow()->log(IString(resLib.loadString( MI_TOPIC_END )) +
  416.        IString(endEvt.topic()) + IString(resLib.loadString( MI_IN_APP )) +
  417.        IString(endEvt.application()) + IString(resLib.loadString( MI_BY_CLIENT )));
  418.    else
  419.      mainWindow()->log(IString(resLib.loadString( MI_TOPIC_END )) +
  420.        IString(endEvt.topic()) + IString(resLib.loadString( MI_IN_APP )) +
  421.        IString(endEvt.application()) + IString(resLib.loadString( MI_BY_ERROR )));
  422. }
  423.  
  424. /**********************************************************/
  425. /* Handle Ack Events                                      */
  426. /**********************************************************/
  427. void AConversation :: acknowledged(IDDEClientAcknowledgeEvent& event)
  428. {
  429.    mainWindow()->log( MI_ACK_RECEIVED );
  430. }
  431.