home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / c / wxwin140 / samples / form / form.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-18  |  4.8 KB  |  189 lines

  1. /*
  2.  * File:     form.cc
  3.  * Purpose:  Demo for wxWindows `forms' class
  4.  *
  5.  *                       wxWindows 1.40
  6.  * Copyright (c) 1993 Artificial Intelligence Applications Institute,
  7.  *                   The University of Edinburgh
  8.  *
  9.  *                     Author: Julian Smart
  10.  *                        Date: 18-4-93
  11.  *
  12.  * Permission to use, copy, modify, and distribute this software and its
  13.  * documentation for any purpose is hereby granted without fee, provided
  14.  * that the above copyright notice, author statement and this permission
  15.  * notice appear in all copies of this software and related documentation.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  18.  * IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  19.  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * IN NO EVENT SHALL THE ARTIFICIAL INTELLIGENCE APPLICATIONS INSTITUTE OR THE
  22.  * UNIVERSITY OF EDINBURGH BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR
  23.  * CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM
  24.  * LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF
  25.  * DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH
  26.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <windows.h>
  30. #include "wx.h"
  31. #include "form.h"
  32.  
  33. MyFrame *frame = NULL;
  34. MyForm *my_form = NULL;
  35. wxDialogBox *form_dialog = NULL;
  36. MyObject object_to_edit;
  37.  
  38. // This statement initialises the whole application
  39. MyApp myApp;
  40.  
  41. // The `main program' equivalent, creating the windows and returning the
  42. // main frame
  43. wxFrame *MyApp::OnInit(void)
  44. {
  45.   // Create the main frame window
  46.   frame = new MyFrame(NULL, "Form Demo", 0, 0, 400, 300);
  47.  
  48.   // Give it an icon
  49.   wxIcon *icon = new wxIcon("aiai_icn");
  50.   frame->SetIcon(icon);
  51.  
  52.   // Make a menubar
  53.   wxMenu *file_menu = new wxMenu;
  54.  
  55.   file_menu->Append(FORM_EDIT, "Edit a form");
  56.   file_menu->Append(FORM_QUIT, "Quit");
  57.  
  58.   wxMenuBar *menu_bar = new wxMenuBar;
  59.  
  60.   menu_bar->Append(file_menu, "File");
  61.  
  62.   // Associate the menu bar with the frame
  63.   frame->SetMenuBar(menu_bar);
  64.  
  65.   frame->Show(TRUE);
  66.  
  67.   // Return the main frame window
  68.   return frame;
  69. }
  70.  
  71. // Define my frame constructor
  72. MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
  73.   wxFrame(frame, title, x, y, w, h)
  74. {
  75. }
  76.  
  77. // Intercept menu commands
  78. void MyFrame::OnMenuCommand(int id)
  79. {
  80.   switch (id)
  81.   {
  82.     case FORM_QUIT:
  83.     {
  84.       OnClose();
  85.       delete this;
  86.       break;
  87.     }
  88.     case FORM_EDIT:
  89.     {
  90.       if (!form_dialog)
  91.       {
  92.         form_dialog = new wxDialogBox(frame, "Form", FALSE, 100, 100, 200, 300);
  93.         my_form = new MyForm;
  94.         my_form->EditForm(&object_to_edit, form_dialog);
  95.         form_dialog->Show(TRUE);
  96.       }
  97.       else form_dialog->Show(TRUE);
  98.       break;
  99.     }
  100.   }
  101. }
  102.  
  103. // Define the behaviour for the frame closing
  104. // - must delete all frames except for the main one.
  105. Bool MyFrame::OnClose(void)
  106. {
  107.   if (form_dialog)
  108.     delete form_dialog;
  109.   return TRUE;
  110. }
  111.  
  112. // Test button function
  113. void MyButtonProc(wxButton& but, wxEvent& event)
  114. {
  115.   wxMessageBox("Pressed a button", "Notification", wxOK);
  116. }
  117.  
  118. // A user-defined constraint
  119. Bool MyConstraint(int type, char *value, char *label, char *msg_buffer)
  120. {
  121.   if (value && (strlen(value) > 7))
  122.   {
  123.     sprintf(msg_buffer, "Value for %s should be 7 characters or less",
  124.             label);
  125.     return FALSE;
  126.   }
  127.   else return TRUE;
  128. }
  129.  
  130.  
  131. void MyForm::EditForm(MyObject *object, wxPanel *panel)
  132. {
  133.   Add(wxMakeFormString("string 1", &(object->string1), wxFORM_DEFAULT,
  134.                        new wxList(wxMakeConstraintFunction(MyConstraint), 0)));
  135.   Add(wxMakeFormNewLine());
  136.  
  137.   Add(wxMakeFormString("string 2", &(object->string2), wxFORM_DEFAULT,
  138.                        new wxList(wxMakeConstraintStrings("One", "Two", "Three", 0), 0)));
  139.   Add(wxMakeFormString("string 3", &(object->string3), wxFORM_CHOICE,
  140.                        new wxList(wxMakeConstraintStrings("Pig", "Cow",
  141.                                   "Aardvark", "Gorilla", 0), 0)));
  142.   Add(wxMakeFormNewLine());
  143.   Add(wxMakeFormShort("int 1", &(object->int1), wxFORM_DEFAULT,
  144.                        new wxList(wxMakeConstraintRange(0.0, 50.0), 0)));
  145.   Add(wxMakeFormNewLine());
  146.  
  147.   Add(wxMakeFormFloat("float 1", &(object->float1), wxFORM_DEFAULT,
  148.                        new wxList(wxMakeConstraintRange(-100.0, 100.0), 0)));
  149.   Add(wxMakeFormBool("bool 1", &(object->bool1)));
  150.   Add(wxMakeFormNewLine());
  151.  
  152.   Add(wxMakeFormButton("Test button", (wxFunction)MyButtonProc));
  153.  
  154.   AssociatePanel(panel);
  155. }
  156.  
  157. void MyForm::OnOk(void)
  158. {
  159.   form_dialog->Show(FALSE);
  160.  
  161.   delete my_form;
  162.   delete form_dialog;
  163.  
  164.   my_form = NULL;
  165.   form_dialog = NULL;
  166. }
  167.  
  168. void MyForm::OnCancel(void)
  169. {
  170.   form_dialog->Show(FALSE);
  171.  
  172.   delete my_form;
  173.   delete form_dialog;
  174.  
  175.   my_form = NULL;
  176.   form_dialog = NULL;
  177. }
  178.  
  179. MyObject::MyObject(void)
  180. {
  181.   string1 = NULL;
  182.   string2 = NULL;
  183.   string3 = NULL;
  184.   int1 = 20;
  185.   bool1 = 1;
  186.   float1 = 0.0;
  187. }
  188.  
  189.