home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / mdi1.cpp < prev    next >
C/C++ Source or Header  |  1997-07-29  |  4KB  |  189 lines

  1. /*
  2.   Program to illustrate simple MDI windows
  3. */
  4. #include <owl\mdi.rh>
  5. #include <owl\applicat.h>
  6. #include <owl\framewin.h>
  7. #include <owl\mdi.h>
  8. #include <owl\static.h>
  9. #include <owl\edit.h>
  10. #include <owl\scroller.h>
  11. #include "mdi1.h"
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. const MaxWords = 100;
  16. const WordsPerLine = 12;
  17. const NumWords = 10;
  18. char* Words[NumWords] = { "The ", "friend ", "saw ", "the ",
  19.               "girl ", "drink ", "milk ", "boy ",
  20.               "cake ", "bread " };
  21.  
  22. BOOL ExpressClose = FALSE;
  23. int NumMDIChild = 0;
  24. int HighMDIindex = 0;
  25.  
  26. class TWinApp : public TApplication
  27. {
  28. public:
  29.   TWinApp() : TApplication() {}
  30.  
  31. protected:
  32.   virtual void InitMainWindow();
  33. };
  34.  
  35. class TAppMDIChild : public TMDIChild
  36. {
  37. public:
  38.   // pointer to the edit box control
  39.   TEdit* TextBox;
  40.   TStatic* TextTxt;
  41.  
  42.   TAppMDIChild(TMDIClient& parent, int ChildNum);
  43.  
  44. protected:
  45.  
  46.   // handle closing the MDI child window
  47.   virtual BOOL CanClose();
  48. };
  49.  
  50. class TAppMDIClient : public TMDIClient
  51. {
  52. public:
  53.  
  54.   TAppMDIClient() : TMDIClient() {}
  55.  
  56.  protected:
  57.  
  58.   // create a new child
  59.   virtual TMDIChild* InitChild();
  60.  
  61.   // close all MDI children
  62.   virtual BOOL CloseChildren();
  63.  
  64.   // handle the command for counting the MDI children
  65.   void CMCountChildren();
  66.  
  67.   // handle closing the MDI frame window
  68.   virtual BOOL CanClose();
  69.  
  70.   // declare response table
  71.   DECLARE_RESPONSE_TABLE(TAppMDIClient);
  72. };
  73.  
  74. DEFINE_RESPONSE_TABLE1(TAppMDIClient, TMDIClient)
  75.   EV_COMMAND(CM_COUNTCHILDREN, CMCountChildren),
  76. END_RESPONSE_TABLE;
  77.  
  78. TAppMDIChild::TAppMDIChild(TMDIClient& parent, int ChildNum)
  79.   : TMDIChild(parent),
  80.      TFrameWindow(&parent),
  81.      TWindow(&parent)
  82. {
  83.   char s[1024];
  84.  
  85.   // set the scrollers in the window
  86.   Attr.Style |= WS_VSCROLL | WS_HSCROLL;
  87.   // create the TScroller instance
  88.   Scroller = new TScroller(this, 200, 15, 10, 50);
  89.  
  90.   // set MDI child window title
  91.   sprintf(s, "%s%i", "MDI Child #", ChildNum);
  92.   Title = _fstrdup(s);
  93.  
  94.   // randomize the seed for the random-number generator
  95.   randomize();
  96.  
  97.   // assign a null string to the variable s
  98.   strcpy(s, "");
  99.   // build the list of random words
  100.   for (int i = 0; i < MaxWords; i++) {
  101.      if (i > 0 && i % WordsPerLine == 0)
  102.         strcat(s, "\r\n");
  103.      strcat(s, Words[random(NumWords)]);
  104.   }
  105.   // create a static text object in the child window if the
  106.   // ChildNum variable stores an odd number.  Otherwise,
  107.   // create an edit box control
  108.   if (ChildNum % 2 == 0) {
  109.      // create the edit box
  110.      TextBox = new TEdit(this, ID_TEXT_EDIT, s,
  111.             10, 10, 300, 400, 0, TRUE);
  112.      // remove borders and scroll bars
  113.      TextBox->Attr.Style &= ~WS_BORDER;
  114.      TextBox->Attr.Style &= ~WS_VSCROLL;
  115.      TextBox->Attr.Style &= ~WS_HSCROLL;
  116.   }
  117.   else
  118.      // create static text
  119.      TextTxt = new TStatic(this, -1, s, 10, 10, 300, 400, strlen(s));
  120. }
  121.  
  122. BOOL TAppMDIChild::CanClose()
  123. {
  124.   // return TRUE if the ExpressClose member of the
  125.   // parent MDI frame window is TRUE
  126.   if (ExpressClose == TRUE) {
  127.     NumMDIChild--;
  128.     return TRUE;
  129.   }
  130.   else
  131.     // prompt the user and return the prompt result
  132.      if (MessageBox("Close this MDI window?",
  133.              "Query", MB_YESNO | MB_ICONQUESTION) == IDYES) {
  134.         NumMDIChild--;
  135.       return TRUE;
  136.     }
  137.     else
  138.       return FALSE;
  139. }
  140.  
  141. TMDIChild* TAppMDIClient::InitChild()
  142. {
  143.   ++NumMDIChild;
  144.   return new TAppMDIChild(*this, ++HighMDIindex);
  145. }
  146.  
  147. BOOL TAppMDIClient::CloseChildren()
  148. {
  149.   BOOL result;
  150.   // set the ExpressClose flag
  151.   ExpressClose = TRUE;
  152.   // invoke the parent class CloseChildren() member function
  153.   result = TMDIClient::CloseChildren();
  154.   // clear the ExpressClose flag
  155.   ExpressClose = FALSE;
  156.   NumMDIChild = 0;
  157.   HighMDIindex = 0;
  158.   return result;
  159. }
  160.  
  161. //  display a message box that shows the number of children
  162. void TAppMDIClient::CMCountChildren()
  163. {
  164.   char msgStr[81];
  165.  
  166.   sprintf(msgStr, "There are %i MDI child windows", NumMDIChild);
  167.   MessageBox(msgStr, "Information", MB_OK | MB_ICONINFORMATION);
  168. }
  169.  
  170. BOOL TAppMDIClient::CanClose()
  171. {
  172.   return MessageBox("Close this application?",
  173.              "Query", MB_YESNO | MB_ICONQUESTION) == IDYES;
  174. }
  175.  
  176. void TWinApp::InitMainWindow()
  177. {
  178.   MainWindow = new TMDIFrame("Simple MDI Text Viewer",
  179.                   TResId(IDM_COMMANDS),
  180.                   *new TAppMDIClient);
  181. }
  182.  
  183. int OwlMain(int /* argc */, char** /*argv[] */)
  184. {
  185.   TWinApp app;
  186.   return app.Run();
  187. }
  188.  
  189.