home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / stddlg.cxx < prev    next >
C/C++ Source or Header  |  1995-04-04  |  15KB  |  567 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6. /*
  7.  *
  8.  *          Copyright (C) 1994, M. A. Sridhar
  9.  *  
  10.  *
  11.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  12.  *     to copy, modify or distribute this software  as you see fit,
  13.  *     and to use  it  for  any  purpose, provided   this copyright
  14.  *     notice and the following   disclaimer are included  with all
  15.  *     copies.
  16.  *
  17.  *                        DISCLAIMER
  18.  *
  19.  *     The author makes no warranties, either expressed or implied,
  20.  *     with respect  to  this  software, its  quality, performance,
  21.  *     merchantability, or fitness for any particular purpose. This
  22.  *     software is distributed  AS IS.  The  user of this  software
  23.  *     assumes all risks  as to its quality  and performance. In no
  24.  *     event shall the author be liable for any direct, indirect or
  25.  *     consequential damages, even if the  author has been  advised
  26.  *     as to the possibility of such damages.
  27.  *
  28.  */
  29.  
  30.  
  31.  
  32.  
  33. #if defined(__GNUC__)
  34. #pragma implementation
  35. #endif
  36.  
  37.  
  38. #if defined(__MS_WINDOWS__)
  39. #include <windows.h>
  40. #include <commdlg.h>
  41. #elif defined (__OS2__)
  42. #if defined(__BORLANDC__)
  43. #include <mem.h> // For memset
  44. #else
  45. #include <memory.h>
  46. #endif
  47. #elif defined (__X_MOTIF__)
  48. #include <Xm/MessageB.h>
  49. #include <Xm/FileSB.h>
  50. #include <Xm/Text.h>
  51. #endif
  52.  
  53. #include "ui/stddlg.h"
  54. #include "ui/applic.h"
  55. #include "ui/cntroler.h"
  56.  
  57. #if defined(__X_MOTIF__)
  58. class MessageBox {
  59.  
  60. public:
  61.     MessageBox (const char* message,
  62.                 UI_SimpleDialogStyle style = UIS_Ok,
  63.                 UI_SimpleDialogIconStyle icon = UIS_Information,
  64.                 UI_VisualObject* parent = NULL,
  65.                 const char* box_title = "");
  66.     ~MessageBox();
  67.     
  68.     short RunModal ();
  69.  
  70.     static void Callback (Widget w, void* client, void* call);
  71.  
  72. protected:
  73.     Widget     _widget;
  74.     CL_String  _callbackBtn;
  75.     short      _terminator;
  76.     CL_String  _msg;
  77.     CL_String  _title;
  78.     UI_SimpleDialogStyle _style;
  79.  
  80.     bool Filter (CL_Object&, long);
  81.  
  82.     bool Terminator (CL_Object&, long);
  83. };
  84.  
  85.  
  86. bool MessageBox::Filter (CL_Object&, long)
  87. {
  88.     return FALSE; // Do not dispatch any YACL events
  89. }
  90.  
  91. bool MessageBox::Terminator (CL_Object&, long)
  92. {
  93.     return _terminator != -1;
  94. }
  95.  
  96. #if defined(__GNUC__)
  97. template class CL_Binding<MessageBox>;
  98. #endif
  99.  
  100.  
  101. void MessageBox::Callback (Widget w, void* client, void* call)
  102. {
  103.     MessageBox* m = (MessageBox*) client;
  104.     XmAnyCallbackStruct* cb = (XmAnyCallbackStruct*) call;
  105.     switch (cb->reason) {
  106.     case XmCR_OK:
  107.         if (m->_style == UIS_YesNo || m->_style == UIS_YesNoCancel)
  108.             m->_terminator = UI_IDYES;
  109.         else
  110.             m->_terminator = UI_IDOK;
  111.         break;
  112.  
  113.     case XmCR_CANCEL:
  114.         if (m->_style == UIS_YesNo || m->_style == UIS_YesNoCancel)
  115.             m->_terminator = UI_IDNO;
  116.         else
  117.             m->_terminator = UI_IDCANCEL;
  118.         break;
  119.  
  120.     case XmCR_HELP:
  121.     default:
  122.         m->_terminator = UI_IDCANCEL; // For yes/no/cancel dialogs
  123.         break;
  124.     };
  125.     
  126. }
  127.  
  128. MessageBox::MessageBox (const char* message,
  129.                         UI_SimpleDialogStyle style,
  130.                         UI_SimpleDialogIconStyle icon,
  131.                         UI_VisualObject* parent,
  132.                         const char* box_title)
  133. {
  134.     _msg = message;
  135.     _title = box_title;
  136.     _style = style;
  137.     Widget pw = parent ? (Widget) parent->ViewHandle()
  138.         : _TheApplication->Controller().ShellWidget();
  139.     switch (icon) {
  140.     case UIS_Information:
  141.         _widget = XmCreateInformationDialog (pw, "infoDialog", NULL, 0);
  142.         break;
  143.         
  144.     case UIS_Question:
  145.         _widget = XmCreateQuestionDialog (pw, "questionDialog", NULL, 0);
  146.         break;
  147.         
  148.     case UIS_Error:
  149.         _widget = XmCreateErrorDialog (pw, "infoDialog", NULL, 0);
  150.         break;
  151.         
  152.     case UIS_Warning:
  153.     default:
  154.         _widget = XmCreateWarningDialog (pw, "warningDialog", NULL, 0);
  155.         break;
  156.     };
  157.         
  158.     _terminator = -1;
  159.  
  160.  
  161. }
  162.  
  163.  
  164. short MessageBox::RunModal ()
  165. {
  166.     Arg arg[6];
  167.     short argn = 0;
  168.     XmString msg = XmStringCreateLtoR ((char*) _msg.AsPtr(), 
  169.                                        XmSTRING_DEFAULT_CHARSET);
  170.     XtSetArg (arg[0], XmNmessageString,  msg);  argn++;
  171.     
  172.     XmString okTitle = XmStringCreateLtoR
  173.         ("Yes", XmSTRING_DEFAULT_CHARSET);
  174.     XmString cnTitle = XmStringCreateLtoR
  175.         ("No",  XmSTRING_DEFAULT_CHARSET);
  176.     XmString hlTitle = XmStringCreateLtoR
  177.         ("Cancel", XmSTRING_DEFAULT_CHARSET);
  178.  
  179.     switch (_style) {
  180.     case UIS_Ok:
  181.         XtUnmanageChild (XmMessageBoxGetChild
  182.                          (_widget, XmDIALOG_HELP_BUTTON));
  183.         XtUnmanageChild (XmMessageBoxGetChild
  184.                          (_widget, XmDIALOG_CANCEL_BUTTON));
  185.         break;
  186.         
  187.     case UIS_YesNo:
  188.         XtSetArg (arg[1], XmNcancelLabelString, cnTitle); argn++;
  189.         XtSetArg (arg[2], XmNokLabelString,     okTitle); argn++;
  190.         // Fall through...
  191.  
  192.     case UIS_OkCancel:
  193.         XtUnmanageChild (XmMessageBoxGetChild (_widget, XmDIALOG_HELP_BUTTON));
  194.         break;
  195.  
  196.     case UIS_YesNoCancel:
  197.         XtSetArg (arg[1], XmNcancelLabelString, cnTitle); argn++;
  198.         XtSetArg (arg[2], XmNokLabelString,     okTitle); argn++;
  199.         XtSetArg (arg[3], XmNhelpLabelString,   hlTitle); argn++;
  200.         break;
  201.  
  202.     default:
  203.         break;
  204.     };
  205.         
  206.     XtSetValues (_widget, arg, argn);
  207.     XmStringFree (okTitle);
  208.     XmStringFree (cnTitle);
  209.     XmStringFree (hlTitle);
  210.  
  211.     XtVaSetValues (XtParent (_widget), XmNdialogTitle, (char*)
  212.                    _title.AsPtr(), NULL);
  213.     XtVaSetValues (XtParent (_widget), XmNtitle, (char*)
  214.                    _title.AsPtr(), NULL);
  215.     
  216.  
  217.     XtAddCallback (_widget, XmNcancelCallback, &MessageBox::Callback,
  218.                    (XtPointer) this);
  219.     XtAddCallback (_widget, XmNokCallback,     &MessageBox::Callback,
  220.                    (XtPointer) this);
  221.     XtAddCallback (_widget, XmNhelpCallback,   &MessageBox::Callback,
  222.                    (XtPointer) this);
  223.     XtManageChild (_widget);
  224.     XtRealizeWidget (_widget);
  225.     XmStringFree (msg);
  226.     UI_Controller& controller = _TheApplication->Controller();
  227.     CL_Binding<MessageBox> fbind (this, MessageBox::Filter);
  228.     CL_Binding<MessageBox> tbind (this, MessageBox::Terminator);
  229.     while (_terminator == -1) {
  230.         controller.EventLoop (&tbind, &fbind);
  231.     }
  232.     return _terminator;
  233. }
  234.  
  235.  
  236. MessageBox::~MessageBox ()
  237. {
  238.     XtUnmanageChild (_widget);
  239.     XtDestroyWidget (_widget);
  240. }
  241.  
  242. #endif
  243.  
  244. UI_ViewID UI_SimpleDialog (const char* message,
  245.                            const char* box_title,
  246.                            UI_VisualObject* parent,
  247.                            UI_SimpleDialogStyle style,
  248.                            UI_SimpleDialogIconStyle icon)
  249. {
  250. #if defined (__MS_WINDOWS__)
  251.     long s;
  252.     switch (style) {
  253.     case UIS_Ok:
  254.         s = MB_OK;
  255.         break;
  256.         
  257.     case UIS_OkCancel:
  258.         s = MB_OKCANCEL;
  259.         break;
  260.         
  261.     case UIS_YesNo:
  262.         s = MB_YESNO;
  263.         break;
  264.         
  265.     case UIS_YesNoCancel:
  266.         s = MB_YESNOCANCEL;
  267.         break;
  268.  
  269.     default:
  270.         s = MB_OKCANCEL;
  271.         break;
  272.     }
  273.  
  274.     switch (icon) {
  275.     case UIS_Information:
  276.         s |= MB_ICONINFORMATION;
  277.         break;
  278.         
  279.     case UIS_Error:
  280.         s |= MB_ICONSTOP;
  281.         break;
  282.         
  283.     case UIS_Question:
  284.         s |= MB_ICONQUESTION;
  285.         break;
  286.         
  287.     case UIS_Warning:
  288.         s |= MB_ICONEXCLAMATION;
  289.         break;
  290.  
  291.     }
  292.     short ret_val = MessageBox (parent ? parent->ViewHandle() : NULL, message,
  293.                                 box_title, s);
  294.     switch (ret_val) {
  295.     case IDYES:
  296.         ret_val = UI_IDYES;
  297.         break;
  298.  
  299.     case IDNO:
  300.         ret_val = UI_IDNO;
  301.         break;
  302.  
  303.     default:
  304.         break;
  305.     }
  306.     return ret_val;
  307.             
  308. #elif defined (__OS2__)
  309.     ulong s = MB_MOVEABLE;
  310.     switch (style) {
  311.     case UIS_Ok:
  312.         s |= MB_OK;
  313.         break;
  314.         
  315.     case UIS_YesNo:
  316.         s |= MB_YESNO;
  317.         break;
  318.         
  319.     case UIS_YesNoCancel:
  320.         s |= MB_YESNOCANCEL;
  321.         break;
  322.  
  323.     case UIS_OkCancel:
  324.     default:
  325.         s |= MB_OKCANCEL;
  326.         break;
  327.     }
  328.  
  329.     switch (icon) {
  330.     case UIS_Information:
  331.         s |=  MB_INFORMATION;
  332.         break;
  333.         
  334.     case UIS_Error:
  335.         s |=  MB_ERROR;
  336.         break;
  337.         
  338.     case UIS_Question:
  339.         s |=  MB_QUERY;
  340.         break;
  341.         
  342.     case UIS_Warning:
  343.         s |=  MB_WARNING;
  344.         break;
  345.  
  346.     }
  347.     HWND handle = parent ? parent->ViewHandle() : HWND_DESKTOP;
  348. //     HWND handle = HWND_DESKTOP; // Seems like the message box is invisible
  349. //                                 // if an application window is specified.
  350. //                                 // Don't know why.
  351.     short ret_val = WinMessageBox
  352.         (HWND_DESKTOP, handle, message, box_title, 0, s);
  353.     switch (ret_val) {
  354.     case MBID_OK:
  355.         ret_val = UI_IDOK;
  356.         break;
  357.  
  358.     case MBID_CANCEL:
  359.         ret_val = UI_IDCANCEL;
  360.         break;
  361.  
  362.     case MBID_YES:
  363.         ret_val = UI_IDYES;
  364.         break;
  365.  
  366.     case MBID_NO:
  367.         ret_val = UI_IDNO;
  368.         break;
  369.  
  370.     default:
  371.         break;
  372.     }
  373.     return ret_val;
  374.             
  375. #endif
  376.  
  377. #if defined(__X_MOTIF__)
  378.     MessageBox theBox (message, style, icon, parent, box_title);
  379.     return theBox.RunModal ();
  380. #endif
  381. }
  382.  
  383.  
  384.  
  385.  
  386.  
  387. #if defined(__X_MOTIF__)
  388. class FileSelectBox {
  389.  
  390. public:
  391.     FileSelectBox (UI_VisualObject* parent,
  392.                    const char* box_title = "");
  393.     ~FileSelectBox();
  394.     
  395.     short RunModal ();
  396.  
  397.     CL_String FileName () {return _fileName;};
  398.     
  399.     static void Callback (Widget w, void* client, void* call);
  400.  
  401. protected:
  402.     Widget     _widget;
  403.     CL_String  _callbackBtn;
  404.     short      _terminator;
  405.     CL_String  _title;
  406.     CL_String  _fileName;
  407.  
  408.     bool Filter (CL_Object&, long);
  409.  
  410.     bool Terminator (CL_Object&, long);
  411.     
  412. };
  413.  
  414.  
  415.  
  416. bool FileSelectBox::Filter (CL_Object&, long)
  417. {
  418.     return FALSE; // Do not dispatch any YACL events
  419. }
  420.  
  421. bool FileSelectBox::Terminator (CL_Object&, long)
  422. {
  423.     return _terminator != -1;
  424. }
  425.  
  426. #if defined(__GNUC__)
  427. template class CL_Binding<FileSelectBox>;
  428. #endif
  429.  
  430.  
  431. void FileSelectBox::Callback (Widget w, void* client, void* call)
  432. {
  433.     FileSelectBox* m = (FileSelectBox*) client;
  434.     XmFileSelectionBoxCallbackStruct* cb =
  435.         (XmFileSelectionBoxCallbackStruct*) call;
  436.     switch (cb->reason) {
  437.     case XmCR_OK:
  438.         m->_terminator = UI_IDOK;
  439.         break;
  440.  
  441.     case XmCR_CANCEL:
  442.     default:
  443.         m->_fileName = "";
  444.         m->_terminator = UI_IDCANCEL;
  445.         break;
  446.     };
  447.     if (cb->reason != XmCR_OK)
  448.         return;
  449.     Widget box = XmFileSelectionBoxGetChild (m->_widget, XmDIALOG_TEXT);
  450.     char* p = XmTextGetString (box);
  451.     m->_fileName = p;
  452.     XtFree (p);
  453.     
  454. }
  455.  
  456. FileSelectBox::FileSelectBox (UI_VisualObject* parent,
  457.                               const char* box_title)
  458. {
  459.     _title = box_title;
  460.     Widget pw = parent ? (Widget) (parent->ViewHandle())
  461.         : _TheApplication->Controller().ShellWidget();
  462.     _widget = XmCreateFileSelectionDialog (pw, "fileSelectionBox", NULL, 0);
  463.     _terminator = -1;
  464. }
  465.  
  466.  
  467. short FileSelectBox::RunModal ()
  468. {
  469.     XtVaSetValues (XtParent (_widget), XmNtitle, (char*) _title.AsPtr(), NULL);
  470.     
  471.     XtAddCallback (_widget, XmNcancelCallback, &FileSelectBox::Callback,
  472.                    (XtPointer) this);
  473.     XtAddCallback (_widget, XmNokCallback,     &FileSelectBox::Callback,
  474.                    (XtPointer) this);
  475.     XtManageChild (_widget);
  476.     XtRealizeWidget (_widget);
  477.     UI_Controller& controller = _TheApplication->Controller();
  478.     CL_Binding<FileSelectBox> bind (this, FileSelectBox::Filter);
  479.     CL_Binding<FileSelectBox> tbind (this, FileSelectBox::Terminator);
  480.     while (_terminator == -1) {
  481.         controller.EventLoop (&tbind, &bind);
  482.     }
  483.     return _terminator;
  484. }
  485.  
  486.  
  487. FileSelectBox::~FileSelectBox ()
  488. {
  489.     XtUnmanageChild (_widget);
  490.     XtDestroyWidget (_widget);
  491. }
  492.  
  493. #endif
  494.  
  495. #if defined(__MS_WINDOWS__)
  496. static FAR char file_name[256]; // Windows doesn't like this object to be
  497.                                 // on the stack!
  498. #endif
  499.  
  500.  
  501. CL_String UI_FileSelectDialog (UI_VisualObject* parent, const char* box_title)
  502. {
  503. #if defined(__MS_WINDOWS__)
  504.     OPENFILENAME ofnTemp;
  505.     long ErrVal;    // Error value
  506.     char szTemp[] = "All files (*.*)\0*.*\0\0";
  507.     if (!parent)
  508.         return "";
  509.     long hWnd = parent->ViewHandle ();
  510.     
  511.     ofnTemp.lStructSize = sizeof (OPENFILENAME);
  512.     ofnTemp.hwndOwner = hWnd;    // An invalid hWnd causes non-modality
  513.     ofnTemp.hInstance = 0;
  514.     ofnTemp.lpstrFilter = (LPSTR)szTemp;  
  515.     ofnTemp.lpstrCustomFilter = NULL;
  516.     ofnTemp.nMaxCustFilter = 0;
  517.     ofnTemp.nFilterIndex = 1;
  518.     ofnTemp.lpstrFile = (LPSTR)file_name; // Stores the result in this variable
  519.     ofnTemp.nMaxFile = sizeof (file_name);
  520.     ofnTemp.lpstrFileTitle = NULL;
  521.     ofnTemp.nMaxFileTitle = 0;
  522.     ofnTemp.lpstrInitialDir = NULL;
  523.     ofnTemp.lpstrTitle = box_title;    // Title for dialog
  524.     ofnTemp.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
  525.     ofnTemp.nFileOffset = 0;
  526.     ofnTemp.nFileExtension = 0;
  527.     ofnTemp.lpstrDefExt = "*";
  528.     ofnTemp.lCustData = NULL;
  529.     ofnTemp.lpfnHook = NULL;
  530.     ofnTemp.lpTemplateName = NULL;
  531.     /*
  532.     If the call to GetOpenFileName() fails you can call CommDlgExtendedError()
  533.     to retrieve the type of error that occured.
  534.     */
  535.     if (!GetOpenFileName (&ofnTemp)) {
  536.         ErrVal=CommDlgExtendedError();
  537.         if (ErrVal) {    // 0 value means user selected Cancel
  538.             UI_SimpleDialog ("Error code: " + CL_String (ErrVal));
  539.         }
  540.         else
  541.             file_name[0] = '\0';
  542.  
  543.     }
  544.     InvalidateRect (hWnd, NULL, TRUE) ;    // Repaint to display the new name
  545.     return file_name;
  546.  
  547. #elif defined(__X_MOTIF__)
  548.     FileSelectBox box (parent, box_title);
  549.     box.RunModal ();
  550.     return box.FileName();
  551.  
  552. #elif defined(__OS2__)
  553.     FILEDLG fileDialog;
  554.     memset (&fileDialog, 0, sizeof fileDialog); // Clear it out
  555.     fileDialog.cbSize = sizeof fileDialog;
  556.     fileDialog.fl     = FDS_CENTER | FDS_OPEN_DIALOG;
  557.     fileDialog.pszTitle = (char*) box_title;
  558.     short b = WinFileDlg (HWND_DESKTOP,
  559.                          parent ? parent->ViewHandle() : HWND_DESKTOP,
  560.                          &fileDialog);
  561.     if (b != DID_OK || fileDialog.lReturn != DID_OK)
  562.         return "";
  563.     return fileDialog.szFullFile;
  564. #endif
  565. }
  566.  
  567.