home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / wxwin140 / samples / mdi / mdi.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  6.8 KB  |  281 lines

  1. /*
  2.  * File:     mdi.cc
  3.  * Purpose:  MDI demo for wxWindows class library
  4.  *           Run with no arguments for SDI style, with -mdi argument
  5.  *           for MDI style windows.
  6.  *
  7.  *                       wxWindows 1.40
  8.  * Copyright (c) 1993 Artificial Intelligence Applications Institute,
  9.  *                   The University of Edinburgh
  10.  *
  11.  *                     Author: Julian Smart
  12.  *                        Date: 18-4-93
  13.  *
  14.  * Permission to use, copy, modify, and distribute this software and its
  15.  * documentation for any purpose is hereby granted without fee, provided
  16.  * that the above copyright notice, author statement and this permission
  17.  * notice appear in all copies of this software and related documentation.
  18.  *
  19.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  20.  * IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  21.  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  22.  *
  23.  * IN NO EVENT SHALL THE ARTIFICIAL INTELLIGENCE APPLICATIONS INSTITUTE OR THE
  24.  * UNIVERSITY OF EDINBURGH BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR
  25.  * CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM
  26.  * LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF
  27.  * DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH
  28.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  29.  */
  30.  
  31. #include <windows.h>
  32. #include "wx.h"
  33. #include "mdi.h"
  34.  
  35. MyFrame *frame = NULL;
  36. wxList my_children;
  37. Bool isMDI = FALSE;
  38.  
  39. // This statement initialises the whole application
  40. MyApp myApp;
  41.  
  42. // For drawing lines in a canvas
  43. float xpos = -1;
  44. float ypos = -1;
  45.  
  46. // Initialise this in OnInit, not statically
  47. wxPen *red_pen;
  48.  
  49. int top_frame_type = wxSDI;
  50. int child_frame_type = wxSDI;
  51.  
  52. wxFont *small_font;
  53.  
  54. // The `main program' equivalent, creating the windows and returning the
  55. // main frame
  56. wxFrame *MyApp::OnInit(void)
  57. {
  58.   // Find out if we're SDI or MDI
  59.   if (argc > 1 && (strcmp(argv[1], "-mdi") == 0))
  60.   {
  61.     isMDI = TRUE;
  62.     top_frame_type = wxMDI_PARENT;
  63.     child_frame_type = wxMDI_CHILD;
  64.   }
  65.  
  66.   // Create a red pen
  67.   red_pen = new wxPen("RED", 3, wxSOLID);
  68.  
  69.   // Create a small font
  70.   small_font = new wxFont(10, wxSWISS, wxNORMAL, wxNORMAL);
  71.  
  72.   // Create the main frame window
  73.   frame = new MyFrame(NULL, "SDI/MDI Demo", 0, 0, 400, 500, top_frame_type);
  74.  
  75.   // Give it an icon (this is ignored in MDI mode: uses resources)
  76.   wxIcon *icon = new wxIcon("aiai_icn");
  77.   frame->SetIcon(icon);
  78.  
  79.   // Make a menubar
  80.   wxMenu *file_menu = new wxMenu;
  81.  
  82.   file_menu->Append(MDI_NEW_WINDOW, "&New window");
  83.   file_menu->Append(MDI_QUIT, "&Exit");
  84.  
  85.   wxMenu *help_menu = new wxMenu;
  86.   help_menu->Append(MDI_ABOUT, "&About");
  87.  
  88.   wxMenuBar *menu_bar = new wxMenuBar;
  89.  
  90.   menu_bar->Append(file_menu, "&File");
  91.   menu_bar->Append(help_menu, "&Help");
  92.  
  93.   // Associate the menu bar with the frame
  94.   frame->SetMenuBar(menu_bar);
  95.  
  96.   frame->Show(TRUE);
  97.  
  98.   // Essential - return the main frame window
  99.   return frame;
  100. }
  101.  
  102. // Define my frame constructor
  103. MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h, int type):
  104.   wxFrame(frame, title, x, y, w, h, type)
  105. {
  106. }
  107.  
  108. MyChild::MyChild(wxFrame *frame, char *title, int x, int y, int w, int h, int type):
  109.   wxFrame(frame, title, x, y, w, h, type)
  110. {
  111.   my_children.Append(this);
  112. }
  113.  
  114. MyChild::~MyChild(void)
  115. {
  116.   my_children.DeleteObject(this);
  117. }
  118.  
  119. // Intercept menu commands
  120. void MyFrame::OnMenuCommand(int id)
  121. {
  122.   switch (id)
  123.   {
  124.     case MDI_QUIT:
  125.     {
  126.       OnClose();
  127.       delete this;
  128.       break;
  129.     }
  130.     case MDI_ABOUT:
  131.     {
  132.       (void)wxMessageBox("wxWindows 1.30 MDI Demo\nAuthor: Julian Smart J.Smart@ed.ac.uk\nAIAI (c) 1993\nUsage: mdi.exe [-mdi]", "About MDI Demo");
  133.       break;
  134.     }
  135.     case MDI_NEW_WINDOW:
  136.     {
  137.       // Make another frame, containing a canvas
  138.       MyChild *subframe = new MyChild(frame, "Canvas Frame", 50, 50, 400, 300,
  139.                              child_frame_type);
  140.  
  141.       // Give it an icon (this is ignored in MDI mode: uses resources)
  142.       wxIcon *icon = new wxIcon("chrt_icn");
  143.       subframe->SetIcon(icon);
  144.  
  145.       // Give it a status line
  146.       subframe->CreateStatusLine();
  147.  
  148.       // Make a menubar
  149.       wxMenu *file_menu = new wxMenu;
  150.  
  151.       if (isMDI)
  152.         file_menu->Append(MDI_NEW_WINDOW, "&New window");
  153.  
  154.       file_menu->Append(MDI_CHILD_QUIT, "&Close child");
  155.  
  156.       if (isMDI)
  157.         file_menu->Append(MDI_QUIT, "&Exit");
  158.  
  159.       wxMenu *option_menu = new wxMenu;
  160.  
  161.       // Dummy option
  162.       option_menu->Append(MDI_REFRESH, "&Refresh picture");
  163.  
  164.       wxMenu *help_menu = new wxMenu;
  165.       help_menu->Append(MDI_ABOUT, "&About");
  166.  
  167.       wxMenuBar *menu_bar = new wxMenuBar;
  168.  
  169.       menu_bar->Append(file_menu, "&File");
  170.       menu_bar->Append(option_menu, "&Options");
  171.       menu_bar->Append(help_menu, "&Help");
  172.  
  173.       // Associate the menu bar with the frame
  174.       subframe->SetMenuBar(menu_bar);
  175.  
  176.       int width, height;
  177.       subframe->GetClientSize(&width, &height);
  178.       MyCanvas *canvas = new MyCanvas(subframe, 0, 0, width, height);
  179.       wxCursor *cursor = new wxCursor(wxCURSOR_PENCIL);
  180.       canvas->SetCursor(cursor);
  181.  
  182.       // Give it scrollbars
  183.       canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
  184.       canvas->SetPen(red_pen);
  185.  
  186.       subframe->Show(TRUE);
  187.       break;
  188.     }
  189.   }
  190. }
  191.  
  192. // Define a constructor for my canvas
  193. MyCanvas::MyCanvas(wxFrame *frame, int x, int y, int w, int h):
  194.  wxCanvas(frame, x, y, w, h)
  195. {
  196. }
  197.  
  198. // Define the repainting behaviour
  199. void MyCanvas::OnPaint(void)
  200. {
  201.   SetFont(small_font);
  202.   SetPen(wxGREEN_PEN);
  203.   DrawLine(0, 0, 200, 200);
  204.   DrawLine(200, 0, 0, 200);
  205.  
  206.   SetBrush(wxCYAN_BRUSH);
  207.   SetPen(wxRED_PEN);
  208.   DrawRectangle(100, 100, 100, 50);
  209.   DrawRoundedRectangle(150, 150, 100, 50);
  210.  
  211.   DrawEllipse(250, 250, 100, 50);
  212.   DrawSpline(50, 200, 50, 100, 200, 10);
  213.   DrawLine(50, 230, 200, 230);
  214.   DrawText("This is a test string", 50, 230);
  215. }
  216.  
  217. // This implements a tiny doodling program! Drag the mouse using
  218. // the left button.
  219. void MyCanvas::OnEvent(wxEvent& event)
  220. {
  221.   SetPen(wxBLACK_PEN);
  222.   float x, y;
  223.   event.Position(&x, &y);
  224.   if (xpos > -1 && ypos > -1 && event.Dragging())
  225.   {
  226.     DrawLine(xpos, ypos, x, y);
  227.   }
  228.   xpos = x;
  229.   ypos = y;
  230. }
  231.  
  232. // Define the behaviour for the frame closing
  233. // - must delete all frames except for the main one.
  234. Bool MyFrame::OnClose(void)
  235. {
  236.   // Must delete children
  237.   wxNode *node = my_children.First();
  238.   while (node)
  239.   {
  240.     MyChild *child = (MyChild *)node->Data();
  241.     wxNode *next = node->Next();
  242.     child->OnClose();
  243.     delete child;
  244.     node = next;
  245.   }
  246.   return TRUE;
  247. }
  248.  
  249.  
  250. // Intercept menu commands
  251. void MyChild::OnMenuCommand(int id)
  252. {
  253.   switch (id)
  254.   {
  255.     case MDI_CHILD_QUIT:
  256.     {
  257.       OnClose();
  258.       delete this;
  259.       break;
  260.     }
  261.     default:
  262.     {
  263.       frame->OnMenuCommand(id);
  264.       break;
  265.     }
  266.   }
  267. }
  268.  
  269. Bool MyChild::OnClose(void)
  270. {
  271.   return TRUE;
  272. }
  273.  
  274.  
  275. void GenericOk(wxButton& but, wxEvent& event)
  276. {
  277.   wxDialogBox *dialog = (wxDialogBox *)but.GetParent();
  278.  
  279.   dialog->Show(FALSE);
  280. }
  281.