home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 21.ddi / TRANSFER.PAK / TRANSFER.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  11.1 KB  |  425 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //----------------------------------------------------------------------------
  4. #include <owl\owlpch.h>
  5. #include <owl\applicat.h>
  6. #include <owl\framewin.h>
  7. #include <owl\dialog.h>
  8. #include <owl\radiobut.h>
  9. #include <owl\edit.h>
  10. #include <owl\listbox.h>
  11. #include <owl\combobox.h>
  12. #include <owl\scrollba.h>
  13. #include <string.h>
  14. #include <cstring.h>
  15.  
  16. #include "transfer.h"
  17.  
  18. const MAXNAMELEN    = 26;
  19. const MAXADDRLEN    = 47;
  20. const MAXCITYSTLEN  = 27;
  21. const MAXCOUNTRYLEN = 27;
  22. const MAXLBDATALEN  = 30;
  23. const MAXCBDATALEN  = 30;
  24.  
  25. const WM_DIALOG_CLOSED = WM_USER + 100;
  26.  
  27. struct TTransferStruct {
  28.   TTransferStruct();
  29.  
  30.   BOOL            MrTitle;
  31.   BOOL            MsTitle;
  32.   BOOL            DrTitle;
  33.   char            NameEdit[MAXNAMELEN];
  34.   char            Addr1Edit[MAXADDRLEN];
  35.   char            Addr2Edit[MAXADDRLEN];
  36.   char            CityStEdit[MAXCITYSTLEN];
  37.   char            CountryEdit[MAXCOUNTRYLEN];
  38.   BOOL            CheckBox1;
  39.   BOOL            CheckBox2;
  40.   BOOL            CheckBox3;
  41.   TListBoxData    ListBoxData;
  42.   char            LBDataEdit[MAXLBDATALEN];
  43.   TComboBoxData   ComboBoxData;
  44.   char            CBDataEdit[MAXCBDATALEN];
  45.   TScrollBarData  ScrollBarData;
  46. };
  47.  
  48. TTransferStruct::TTransferStruct()
  49. {
  50.   MrTitle = MsTitle = DrTitle = FALSE;
  51.   NameEdit[0] = 0;
  52.   Addr1Edit[0] = 0;
  53.   Addr2Edit[0] = 0;
  54.   CityStEdit[0] = 0;
  55.   CountryEdit[0] = 0;
  56.  
  57.   CheckBox1 = CheckBox2 = CheckBox3 = 0;
  58.  
  59.   //
  60.   // Pre-fill the listbox. It may be sorted, and/or multiselect
  61.   //
  62.   ListBoxData.AddString("Zebra");
  63.   ListBoxData.AddString("Aardvark");
  64.   ListBoxData.AddString("Ocelot");
  65.   ListBoxData.AddString("Beaver");
  66.   ListBoxData.AddString("Emu");
  67.   ListBoxData.Select(2);    // Ocelot
  68.   ListBoxData.Select(3);    // Beaver
  69.   LBDataEdit[0] = 0;
  70.  
  71.   //
  72.   // Pre-fill the combobox
  73.   //
  74.   ComboBoxData.AddString("Red");
  75.   ComboBoxData.AddString("Pink");
  76.   ComboBoxData.AddString("Blue");
  77.   ComboBoxData.AddString("Green");
  78.   ComboBoxData.AddString("Yellow");
  79.   ComboBoxData.Select(2);    // Blue
  80.   CBDataEdit[0] = 0;
  81.  
  82.   ScrollBarData.LowValue = 0;
  83.   ScrollBarData.HighValue = 100;
  84.   ScrollBarData.Position = 50;
  85. }
  86.  
  87.  
  88.  
  89. //----------------------------------------------------------------------------
  90.  
  91. class TTransferDialog : public TDialog {
  92.   public:
  93.     TTransferDialog(TWindow* parent, int resId, TTransferStruct& ts);
  94.     void CmAddListBox();
  95.     void CmAddComboBox();
  96.     void EvDestroy();
  97.  
  98.     void CloseWindow(int ret);
  99.  
  100.   private:
  101.     TListBox*         ListBox;         // cache for transfer access.
  102.     TComboBox*        ComboBox;
  103.     TEdit*            ListBoxData;
  104.     TEdit*            ComboBoxData;
  105.     TScrollBar*       ScrollBar;
  106.  
  107.   DECLARE_RESPONSE_TABLE(TTransferDialog);
  108.  
  109. };
  110.  
  111. DEFINE_RESPONSE_TABLE1(TTransferDialog, TDialog)
  112.   EV_COMMAND(CM_ADDLISTBOX, CmAddListBox),
  113.   EV_COMMAND(CM_ADDCOMBOBOX, CmAddComboBox),
  114.   EV_WM_DESTROY,
  115. END_RESPONSE_TABLE;
  116.  
  117. TTransferDialog::TTransferDialog(TWindow* parent, int resId, TTransferStruct& ts)
  118.   : TDialog(parent, resId)
  119. {
  120.   new TRadioButton(this, ID_MRBUTTON, 0);
  121.   new TRadioButton(this, ID_MSBUTTON, 0);
  122.   new TRadioButton(this, ID_DRBUTTON, 0);
  123.   new TEdit(this, ID_NAMEEDIT, sizeof(ts.NameEdit));
  124.   new TEdit(this, ID_ADDR1EDIT, sizeof(ts.Addr1Edit));
  125.   new TEdit(this, ID_ADDR2EDIT, sizeof(ts.Addr2Edit));
  126.   new TEdit(this, ID_CITYSTEDIT, sizeof(ts.CityStEdit));
  127.   new TEdit(this, ID_COUNTRYEDIT, sizeof(ts.CountryEdit));
  128.   new TCheckBox(this, ID_CHECKBOX1);
  129.   new TCheckBox(this, ID_CHECKBOX2);
  130.   new TCheckBox(this, ID_CHECKBOX3);
  131.   ListBox = new TListBox(this, ID_LISTBOX);
  132.   ListBoxData = new TEdit(this, ID_LISTBOXDATA, sizeof(ts.LBDataEdit));
  133.   ComboBox = new TComboBox(this, ID_COMBOBOX, MAXCBDATALEN);
  134.   ComboBoxData = new TEdit(this, ID_COMBOBOXDATA, sizeof(ts.CBDataEdit));
  135.   ScrollBar = new TScrollBar(this, ID_SCROLLBAR);
  136.  
  137.   SetTransferBuffer(&ts);
  138. }
  139.  
  140. //
  141. // Lets transfer on close even though we are modeless
  142. //
  143. void
  144. TTransferDialog::CloseWindow(int ret)
  145. {
  146.   TransferData(tdGetData);
  147.   TDialog::CloseWindow(ret);
  148. }
  149.  
  150.  
  151. //
  152. // Add a string to the list box.
  153. //
  154. void
  155. TTransferDialog::CmAddListBox()
  156. {
  157.   char   buf[MAXLBDATALEN] = "";
  158.  
  159.   ListBoxData->GetLine(buf, MAXLBDATALEN - 1, 0);
  160.   if (buf[0] != '\0')
  161.     ListBox->AddString(buf);
  162.   ListBoxData->DeleteLine(0);
  163. }
  164.  
  165. //
  166. // Add a string to the combo box.
  167. //
  168. void
  169. TTransferDialog::CmAddComboBox()
  170. {
  171.   char   buf[MAXCBDATALEN] = "";
  172.  
  173.   ComboBoxData->GetLine(buf, MAXCBDATALEN - 1, 0);
  174.   if (buf[0] != '\0')
  175.     ComboBox->AddString(buf);
  176.   ComboBoxData->DeleteLine(0);
  177. }
  178.  
  179. void
  180. TTransferDialog::EvDestroy()
  181. {
  182.   // tell application that dialog is closing.
  183.   Parent->PostMessage(WM_DIALOG_CLOSED);
  184.   TDialog::EvDestroy();
  185. }
  186.  
  187. //----------------------------------------------------------------------------
  188.  
  189. class TTransferInfoDialog : public TDialog {
  190.   public:
  191.     TTransferInfoDialog(TWindow* parent, int resId, string& info);
  192.     void SetupWindow();
  193.  
  194.   private:
  195.     TEdit*  DisplayInfo;
  196.     string& Info;
  197. };
  198.  
  199. TTransferInfoDialog::TTransferInfoDialog(TWindow* parent, int resId, string& info)
  200.   : TDialog(parent, resId), Info(info)
  201. {
  202.   DisplayInfo = new TEdit(this, ID_INFO, 2000);
  203. }
  204.  
  205. void
  206. TTransferInfoDialog::SetupWindow()
  207. {
  208.   TDialog::SetupWindow();
  209.   DisplayInfo->Insert(Info.c_str());
  210. }
  211.  
  212. //----------------------------------------------------------------------------
  213.  
  214. class TTransferWindow : public TFrameWindow {
  215.   public:
  216.     TTransferWindow(TWindow* parent, const char* title);
  217.    ~TTransferWindow() {
  218.       delete[] Label;
  219.     }
  220.  
  221.     void CmDialog();
  222.     void CmTransfer();
  223.     void CmDialogEnable(TCommandEnabler& commandHandler);
  224.     LRESULT EvDialogClosed(WPARAM wParam, LPARAM lParam);
  225.  
  226.   private:
  227.     TTransferStruct   TransferStruct;
  228.     TTransferDialog*  TransferDialog;
  229.     char*             Label;     // string to print on transfer.
  230.  
  231.   DECLARE_RESPONSE_TABLE(TTransferWindow);
  232. };
  233.  
  234. DEFINE_RESPONSE_TABLE1(TTransferWindow, TFrameWindow)
  235.   EV_COMMAND(CM_DIALOG, CmDialog),
  236.   EV_COMMAND(CM_TRANSFER, CmTransfer),
  237.   EV_COMMAND_ENABLE(CM_DIALOG, CmDialogEnable),
  238.   EV_MESSAGE(WM_DIALOG_CLOSED, EvDialogClosed),
  239. END_RESPONSE_TABLE;
  240.  
  241.  
  242. TTransferWindow::TTransferWindow(TWindow* parent, const char* title)
  243.   : TFrameWindow(parent, title),
  244.     TWindow(parent, title)
  245. {
  246.   AssignMenu(ID_MENU);
  247.   Label = new char[1024];
  248.   Label[0] = 0;
  249.   TransferDialog = 0;
  250. }
  251.  
  252. //
  253. // Create modeless dialog.
  254. //
  255. void
  256. TTransferWindow::CmDialog()
  257. {
  258.   TransferDialog = new TTransferDialog(this, IDD_DIALOG, TransferStruct);
  259.   TransferDialog->Create();
  260.   TransferDialog->ShowWindow(SW_SHOW);
  261. }
  262.  
  263. //
  264. // Transfer data and display.
  265. //
  266. void
  267. TTransferWindow::CmTransfer()
  268. {
  269.   if (TransferDialog)
  270.     TransferDialog->Transfer(&TransferStruct, tdGetData);
  271.  
  272.   TStringArray&   lbStrArray = TransferStruct.ListBoxData.GetStrings();
  273.   TStringArray&   cbStrArray = TransferStruct.ComboBoxData.GetStrings();
  274.   unsigned        lbNItems = lbStrArray.GetItemsInContainer();
  275.   unsigned        cbNItems = cbStrArray.GetItemsInContainer();
  276.   unsigned        i;
  277.   char            buf[10];
  278.   string          displayInfo;
  279.  
  280.   // mailing info.
  281.   //
  282.   displayInfo += "Mailing Label Entered:";
  283.   displayInfo += "\r\n";
  284.   if (TransferStruct.MrTitle)
  285.     displayInfo += "Mr. ";
  286.   else if (TransferStruct.MsTitle)
  287.     displayInfo +=  "Ms. ";
  288.   else if (TransferStruct.DrTitle)
  289.     displayInfo += "Dr. ";
  290.   else
  291.     displayInfo += "??. ";
  292.   displayInfo += TransferStruct.NameEdit;
  293.   displayInfo += "\r\n";
  294.   displayInfo += TransferStruct.Addr1Edit;
  295.   displayInfo += "\r\n";
  296.   if (strcmp(TransferStruct.Addr2Edit, "") != 0) {
  297.     displayInfo += TransferStruct.Addr2Edit;
  298.     displayInfo += "\r\n";
  299.   }
  300.   displayInfo += TransferStruct.CityStEdit;
  301.   displayInfo += "\r\n";
  302.   displayInfo += TransferStruct.CountryEdit;
  303.   displayInfo += "\r\n";
  304.   displayInfo += "\r\n";
  305.  
  306.   // check boxes.
  307.   //
  308.   if (TransferStruct.CheckBox1)
  309.     displayInfo += "CheckBox #1 checked.\r\n";
  310.   else
  311.     displayInfo += "CheckBox #1 not checked.\r\n";
  312.  
  313.   if (TransferStruct.CheckBox2)
  314.     displayInfo += "CheckBox #2 checked.\r\n";
  315.   else
  316.     displayInfo += "CheckBox #2 not checked.\r\n";
  317.  
  318.   if (TransferStruct.CheckBox3)
  319.     displayInfo += "CheckBox #3 checked.\r\n\r\n";
  320.   else
  321.     displayInfo += "CheckBox #3 not checked.\r\n\r\n";
  322.  
  323.   // list box strings.
  324.   //
  325.   if (lbNItems != 0) {
  326.     TStringArrayIterator iter(lbStrArray);
  327.     displayInfo += "First 5 ListBox strings:\r\n";
  328.     for (i = 0; i < lbNItems && i < 5; i++, iter++) {
  329.       displayInfo += "    ";
  330.       displayInfo += iter.Current();
  331.       displayInfo += "\r\n";
  332.     }
  333.     displayInfo += "\r\n";
  334.  
  335.     displayInfo += "Listbox selection\r\n    ";
  336.     string sel;
  337.     TransferStruct.ListBoxData.GetSelString(sel);
  338.     displayInfo += sel;
  339.     displayInfo += "\r\n";
  340.   }
  341.  
  342.   // strings from edit control for list box data.
  343.   //
  344.   if (TransferStruct.LBDataEdit[0] != '\0') {
  345.     displayInfo += "String from Edit control for ListBox data:\r\n    ";
  346.     displayInfo += TransferStruct.LBDataEdit;
  347.     displayInfo += "\r\n";
  348.   }
  349.  
  350.   // combo box strings.
  351.   //
  352.   if (cbNItems != 0) {
  353.     displayInfo += "First 5 ComboBox strings:\r\n";
  354.     for (i = 0; i < cbNItems && i < 5; i++) {
  355.       displayInfo += "    ";
  356.       displayInfo += cbStrArray[i];
  357.       displayInfo += "\r\n";
  358.     }
  359.     displayInfo += "\r\n";
  360.  
  361.     displayInfo += "Combobox selection\r\n    ";
  362.     displayInfo += TransferStruct.ComboBoxData.GetSelection();
  363.     displayInfo += "\r\n";
  364.   }
  365.  
  366.   // strings from edit control for combo box data.
  367.   //
  368.   if (TransferStruct.CBDataEdit[0] != '\0') {
  369.     displayInfo += "String from Edit control for ComboBox data:\r\n    ";
  370.     displayInfo += TransferStruct.CBDataEdit;
  371.   }
  372.  
  373.   // scroll bar info.
  374.   //
  375.   displayInfo += "\r\nScrollBar Info:\r\n    Low value: ";
  376.   itoa(TransferStruct.ScrollBarData.LowValue, buf, 10);
  377.   displayInfo += buf;
  378.   displayInfo += "  High value: ";
  379.   itoa(TransferStruct.ScrollBarData.HighValue, buf, 10);
  380.   displayInfo += buf;
  381.   displayInfo += "  Current value: ";
  382.   itoa(TransferStruct.ScrollBarData.Position, buf, 10);
  383.   displayInfo += buf;
  384.  
  385.   // display info.
  386.   //
  387.   TTransferInfoDialog infoDialog(this, ID_DISPLAY_INFO, displayInfo);
  388.   infoDialog.Execute();
  389. }
  390.  
  391. //
  392. // Disable the 'App|Dialog' menu item if dialog is already active.
  393. //
  394. void
  395. TTransferWindow::CmDialogEnable(TCommandEnabler& commandHandler)
  396. {
  397.   commandHandler.Enable(!TransferDialog);
  398. }
  399.  
  400. //
  401. // The dialog has closed, notified by dialog.
  402. //
  403. LRESULT
  404. TTransferWindow::EvDialogClosed(WPARAM, LPARAM)
  405. {
  406.   TransferDialog = 0;
  407.   return 1;
  408. }
  409.  
  410. //----------------------------------------------------------------------------
  411.  
  412. class TTransferApp : public TApplication {
  413.   public:
  414.     TTransferApp() : TApplication("TransferTest") {}
  415.  
  416.     void InitMainWindow()
  417.        {MainWindow = new TTransferWindow(0, "Test Dialog Transfer");}
  418. };
  419.  
  420. int
  421. OwlMain(int /*argc*/, char* /*argv*/ [])
  422. {
  423.   return TTransferApp().Run();
  424. }
  425.