home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWL1.PAK / DIALOG.CPP < prev    next >
Text File  |  1995-08-29  |  10KB  |  341 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. /* ------------------------------------------------------------
  4.   Defines type TDialog.  This defines the basic behavior
  5.   of all dialogs.
  6.   ------------------------------------------------------------ */
  7.  
  8. #include "dialog.h"
  9. #include <string.h>
  10. #include <alloc.h>
  11.  
  12. extern PTWindowsObject DlgCreationWindow;
  13.  
  14. /* Constructor for a TDialog object. Takes an LPSTR for its template name.
  15.    TWindowsObject's constructor creates an instance thunk for the TDialog. */
  16. TDialog::TDialog(PTWindowsObject AParent, LPSTR AName, PTModule AModule)
  17.                                 : TWindowsObject(AParent, AModule)
  18. {
  19.   DisableAutoCreate();
  20.   Title = (LPSTR)MAKEINTRESOURCE(-1);
  21.   if ( HIWORD(AName) )   // Not NULL and not an int in disguise
  22.     Attr.Name = _fstrdup(AName);
  23.   else
  24.     Attr.Name = AName;
  25.   Attr.Param = 0L;
  26.   IsModal = FALSE;
  27. }
  28.  
  29. /* Constructor for a TDialog object. Takes an int for its template name.
  30.    TWindowsObject's constructor creates an instance thunk for the TDialog. */
  31. TDialog::TDialog(PTWindowsObject AParent, int ResourceId, PTModule AModule)
  32.                                 : TWindowsObject(AParent, AModule)
  33. {
  34.   DisableAutoCreate();
  35.   Title = (LPSTR)MAKEINTRESOURCE(-1);
  36.   Attr.Name = (LPSTR)MAKEINTRESOURCE(ResourceId);
  37.   Attr.Param = 0L;
  38.   IsModal = FALSE;
  39. }
  40.  
  41. /* Destructor for a TDialog. ~TWindowsObject frees the instance
  42.    thunk. */
  43. TDialog::~TDialog()
  44. {
  45.   if ( HIWORD(Attr.Name) )
  46.     farfree(Attr.Name);
  47. }
  48.  
  49. static BOOL RegisterFails(void *AWindowsObject, void *)
  50. {
  51.   return !((PTWindowsObject)AWindowsObject)->Register();
  52. }
  53.  
  54. /* Creates an MS-Windows modeless dialog, and associates the
  55.    modeless dialog interface element with the TDialog.  Creation
  56.    and association are not attempted if the Status data member
  57.    is non-zero. */
  58. BOOL TDialog::Create()
  59. {
  60.   HWND HParent;
  61.  
  62.   IsModal = FALSE; // GetClassName called from Register needs to know this.
  63.   if ( Status == 0 && Register() )
  64.   {
  65.     DisableAutoCreate();
  66.     EnableKBHandler();
  67.     if ( !Parent )
  68.       HParent = 0;
  69.     else
  70.       HParent = Parent->HWindow;
  71.     DlgCreationWindow = this;
  72.     /* Register all the dialog's child objects (for custom control
  73.        support) */
  74.     if ( FirstThat(RegisterFails, NULL) == NULL )
  75.     {
  76.       HWindow = CreateDialogParam(GetModule()->hInstance,
  77.                Attr.Name, HParent, (DLGPROC)GetInstance(), Attr.Param);
  78.       if ( !HWindow )
  79.         Status = EM_INVALIDWINDOW;
  80.     }
  81.     else
  82.       Status = EM_INVALIDCHILD;
  83.     DlgCreationWindow = NULL;
  84.   }
  85.   return (Status == 0);
  86. }
  87.  
  88. /* Creates an MS-Windows modal dialog.  Associates the modal dialog
  89.   interface element with the TDialog.  Creation and association is not
  90.   attempted if the Status data member is non-zero. */
  91. int TDialog::Execute()
  92. {
  93.   HWND HParent;
  94.   int ReturnValue = -1;
  95.   PTWindowsObject OldKBHandler;
  96.  
  97.   IsModal = TRUE;
  98.   if ( Status == 0  && Register() )
  99.   {
  100.     DisableAutoCreate();
  101.  
  102.     /* Enable the keyboard handler. Although modal dialogs do
  103.         their own keyboard handling, we use the WB_KBHANDLER
  104.         flag for WM_COMMAND processing. */
  105.     EnableKBHandler();
  106.     if ( GetApplication() )
  107.       OldKBHandler = GetApplication()->KBHandlerWnd;
  108.  
  109.     if ( !Parent )
  110.       HParent = 0;
  111.     else
  112.       HParent = Parent->HWindow;
  113.     DlgCreationWindow = this;
  114.     /* Register all the dialog's child objects (for custom control
  115.        support) */
  116.     if ( FirstThat(RegisterFails, NULL) == NULL )
  117.     {
  118.       ReturnValue = DialogBoxParam(GetModule()->hInstance, Attr.Name,
  119.                                 HParent, (DLGPROC)GetInstance(), Attr.Param);
  120.       // -1 if the function cannot create the dialog box
  121.       if ( ReturnValue == -1)
  122.         Status = EM_INVALIDWINDOW;
  123.     }
  124.     else
  125.       Status = EM_INVALIDCHILD;
  126.     DlgCreationWindow = NULL;
  127.     if ( GetApplication() )
  128.       GetApplication()->SetKBHandler(OldKBHandler);
  129.   }
  130.   if ( Status == 0 )
  131.     delete this;
  132.   else
  133.       if (ReturnValue != -1)
  134.           ReturnValue = BAD_DIALOG_STATUS;  // dialog ran, but status != 0
  135.   return ReturnValue;
  136. }
  137.  
  138. /* Responds to an incoming WM_INITDIALOG message.  This message is
  139.   sent after an MS-Windows dialog is created and before the dialog
  140.   is displayed. Calls SetupWindow to perform set up for the dialog. */
  141. void TDialog::WMInitDialog(TMessage&)
  142. {
  143.   SetupWindow();
  144. }
  145.  
  146. /* Sets up the dialog box by calling SetCaption and then
  147.    TWindowsObject::SetupWindow. Calling SetCaption here allows one
  148.    to override the dialog caption (specified in the dialog resource)
  149.    by setting Title prior to this point. */
  150. void TDialog::SetupWindow()
  151. {
  152.   SetCaption(Title);
  153.   TWindowsObject::SetupWindow();
  154. }
  155.  
  156. /*Respond to Windows attempt to close down. Note: a dialog needs
  157.   to invert the test because Windows expects the opposite response from
  158.   that of a normal window. */
  159. void TDialog::WMQueryEndSession(TMessage& Msg)
  160. {
  161.   if ( GetApplication() && this == GetApplication()->MainWindow )
  162.     Msg.Result = (long)!(GetApplication()->CanClose());
  163.   else
  164.     Msg.Result = (long)!CanClose();
  165. }
  166.  
  167. /* Conditionally shuts down the dialog box. If this is a modal
  168.    dialog calls CanClose and, if CanClose returns true, transfers
  169.    its data and shuts down, passing ARetValue. Calls
  170.    TWindowsObject::CloseWindow if this is a modeless dialog. */
  171. void TDialog::CloseWindow(int ARetValue)
  172. {
  173.   if ( IsModal )
  174.   {
  175.     if ( CanClose() )
  176.     {
  177.       TransferData(TF_GETDATA);
  178.       ShutDownWindow(ARetValue);
  179.     }
  180.   }
  181.   else   // !IsModal
  182.     TWindowsObject::CloseWindow();
  183. }
  184.  
  185. /* Conditionally shuts down the dialog box. Calls CloseWindow
  186.    passing IDCANCEL if this is a modal dialog. If this is a
  187.    modeless dialog calls TWindowsObject::CloseWindow. */
  188. void TDialog::CloseWindow()
  189. {
  190.   if ( IsModal )
  191.     CloseWindow(IDCANCEL);
  192.   else   // !IsModal
  193.     TWindowsObject::CloseWindow();
  194. }
  195.  
  196. /* Unconditionally shuts down a dialog box, returning ARetValue if this
  197.    is a modal dialog. */
  198. void TDialog::ShutDownWindow(int ARetValue)
  199. {
  200.   if ( IsModal )
  201.     // Note that we can't delete a modal dialog here because we're still in
  202.     // its Execute function.
  203.     Destroy(ARetValue);
  204.   else
  205.     TWindowsObject::ShutDownWindow();
  206. }
  207.  
  208. /* Unconditionally shuts down a dialog box, returning IDCANCEL if this
  209.    is a modal dialog. */
  210. void TDialog::ShutDownWindow()
  211. {
  212.   if ( IsModal )
  213.     // Note that we can't delete a modal dialog here because we're still in
  214.     // its Execute function.
  215.     Destroy(IDCANCEL);
  216.   else
  217.   TWindowsObject::ShutDownWindow();
  218. }
  219.  
  220. static void DoEnableAutoCreate(void *AWindowsObject, void *)
  221. {
  222.   if ( ((PTWindowsObject)AWindowsObject)->HWindow )
  223.     ((PTWindowsObject)AWindowsObject)->EnableAutoCreate();
  224. }
  225.  
  226. /* Destroys the MS-Windows dialog associated with the TDialog. */
  227. void TDialog::Destroy(int ARetValue)
  228. {
  229.   if ( IsModal && HWindow)
  230.   {
  231.     ForEach(DoEnableAutoCreate, NULL);
  232.     EndDialog(HWindow, ARetValue);
  233.   }
  234.   else
  235.     TWindowsObject::Destroy();
  236. }
  237.  
  238. /* Destroys the MS-Windows dialog associated with the TDialog. */
  239. void TDialog::Destroy()
  240. {
  241.   if ( IsModal )
  242.     Destroy(IDCANCEL);
  243.   else
  244.     TWindowsObject::Destroy();
  245. }
  246.  
  247. /* Set Dialog's Title data member and Caption */
  248. void TDialog::SetCaption(LPSTR ATitle)
  249. {
  250.   if ( (int)ATitle != -1 )
  251.     TWindowsObject::SetCaption(ATitle);
  252. }
  253.  
  254. /* Responds to an incoming notification message from a button with
  255.    an Id equal to IDOK.  Calls CanClose.  If the call returns True,
  256.    calls TransferData and then ends the dialog, returning IDOK. */
  257. void TDialog::Ok(TMessage&)
  258. {
  259.   CloseWindow(IDOK);
  260. }
  261.  
  262. /* Responds to an incoming notification message from a button with
  263.    an Id equal to IDCANCEL.  Ends the dialog, returning IDCANCEL
  264.    if it's a modal dialog. */
  265. void TDialog::Cancel(TMessage&)
  266. {
  267.   ShutDownWindow();
  268. }
  269.  
  270. /* Ends the dialog, returning IDCANCEL if it's a modal dialog. */
  271. void TDialog::WMClose(TMessage&)
  272. {
  273.   ShutDownWindow();
  274. }
  275.  
  276. LPSTR TDialog::GetClassName()
  277. {
  278.   if ( IsModal )
  279.     return (LPSTR)32770L;
  280.   else
  281.     return "OWLDialog31";
  282. }
  283.  
  284. /* Specifies registration attributes for the MS-Windows window
  285.    class of the TDialog, allowing instances of TDialog to
  286.    be registered.  Sets the fields of the passed WNDCLASS
  287.    parameter to the default attributes appropriate for a
  288.    TDialog. */
  289. void TDialog::GetWindowClass(WNDCLASS& AWndClass)
  290. {
  291.   AWndClass.style = CS_HREDRAW | CS_VREDRAW;
  292.   AWndClass.lpfnWndProc = DefDlgProc;
  293.   AWndClass.cbClsExtra = 0;
  294.   AWndClass.cbWndExtra = DLGWINDOWEXTRA;
  295.   AWndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  296.   AWndClass.lpszMenuName = NULL;
  297.   AWndClass.hInstance = GetModule()->hInstance;
  298.   AWndClass.hIcon = LoadIcon(0, IDI_APPLICATION);
  299.   AWndClass.hCursor = LoadCursor(0, IDC_ARROW);
  300.   AWndClass.lpszClassName = GetClassName();
  301. }
  302.  
  303. /* Reads an instance of TDialog from the passed ipstream. */
  304. void *TDialog::read(ipstream& is)
  305. {
  306.   BOOL NameIsNumeric;
  307.  
  308.   TWindowsObject::read(is);
  309.  
  310.   is >> NameIsNumeric;
  311.   if ( NameIsNumeric )
  312.     is >> (long)(Attr.Name);
  313.   else
  314.     Attr.Name = is.freadString();
  315.  
  316.   is >> IsModal;
  317.   return this;
  318. }
  319.  
  320. /* Writes the TDialog to the passed opstream. */
  321. void TDialog::write(opstream& os)
  322. {
  323.   BOOL NameIsNumeric;
  324.  
  325.   TWindowsObject::write(os);
  326.   NameIsNumeric = HIWORD(Attr.Name) == NULL;
  327.   os << NameIsNumeric;
  328.   if ( NameIsNumeric )
  329.     os << (long)(Attr.Name);
  330.   else
  331.     os.fwriteString(Attr.Name);
  332.   os << IsModal;
  333. }
  334.  
  335. TStreamable *TDialog::build()
  336. {
  337.   return new TDialog(streamableInit);
  338. }
  339.  
  340. TStreamableClass RegDialog("TDialog", TDialog::build, __DELTA(TDialog));
  341.