simpdlg.cxx
上传用户:hzhsqp
上传日期:2007-01-06
资源大小:1600k
文件大小:16k
- /*
- * simpdlg.cxx
- *
- * Simple dialogs.
- *
- * Portable Windows Library
- *
- * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
- *
- * The contents of this file are subject to the Mozilla Public License
- * Version 1.0 (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
- * the License for the specific language governing rights and limitations
- * under the License.
- *
- * The Original Code is Portable Windows Library.
- *
- * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
- *
- * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
- * All Rights Reserved.
- *
- * Contributor(s): ______________________________________.
- *
- * $Log: simpdlg.cxx,v $
- * Revision 1.25 1999/08/07 07:13:23 robertj
- * Fixed problems with "balloon help" text popup.
- *
- * Revision 1.24 1998/12/01 13:01:16 robertj
- * new directory structure.
- * Unix version
- *
- * Revision 1.23 1998/09/23 06:29:56 robertj
- * Added open source copyright license.
- *
- * Revision 1.22 1996/04/30 12:34:00 robertj
- * Changed "inPixels" boolean to enum for three coordinate systems.
- *
- * Revision 1.21 1995/06/04 12:43:48 robertj
- * Fixed bug with fonts being inherited from parent.
- * Fixed bug with Abort Retry Ignore returning incorrect code.
- *
- * Revision 1.20 1995/04/25 11:28:38 robertj
- * Fixed Borland compiler warnings.
- *
- * Revision 1.19 1995/01/16 09:42:07 robertj
- * Documentation.
- *
- * Revision 1.18 1995/01/15 04:53:10 robertj
- * Fixed problems with stdarg and PString parameter on GNU compiler.
- *
- * Revision 1.17 1995/01/10 11:44:17 robertj
- * Removed PString parameter in stdarg function for GNU C++ compatibility.
- *
- * Revision 1.16 1994/11/19 00:18:35 robertj
- * Changed PInteger to be INT, ie standard type like BOOL/WORD etc.
- *
- * Revision 1.15 1994/10/30 11:47:16 robertj
- * Changed mechanism for doing notification callback functions.
- *
- * Revision 1.14 1994/08/21 23:43:02 robertj
- * Changed parameter before variable argument list to NOT be a reference.
- *
- * Revision 1.13 1994/08/01 03:41:24 robertj
- * Use of PNEW instead of new for heap debugging. Need undef for Unix end.
- *
- * Revision 1.12 1994/07/27 05:58:07 robertj
- * Synchronisation.
- *
- * Revision 1.11 1994/06/25 11:55:15 robertj
- * Unix version synchronisation.
- *
- * Revision 1.10 1994/04/11 12:07:09 robertj
- * Fixed bug in icon dimensions.
- *
- * Revision 1.9 1994/04/03 08:34:18 robertj
- * Added help and focus functionality.
- *
- * Revision 1.8 1994/03/07 07:47:00 robertj
- * Major upgrade
- *
- * Revision 1.7 1993/12/16 06:20:57 robertj
- * Changes to callback function definition due to GCC.
- *
- * Revision 1.6 1993/12/15 10:17:51 robertj
- * Moved simple dialog static functions from inline to cxx file.
- *
- * Revision 1.5 1993/12/06 22:36:38 robertj
- * Fixed problem with unimplemented icons.
- *
- * Revision 1.4 1993/10/16 20:56:30 robertj
- * Changed ButtonPressed for X Windows compatibility.
- * Added static icon protection against non-implementation.
- *
- * Revision 1.3 1993/08/24 00:27:59 robertj
- * Rearrangement of pushbutton classes, new class added.
- *
- * Revision 1.2 1993/08/21 04:40:19 robertj
- * Fixed icons using wrong constructor.
- *
- * Revision 1.3 1993/07/14 12:41:52 robertj
- * Fixed comment leader.
- *
- * Revision 1.2 1993/07/14 02:08:18 robertj
- * Fixed header comment for RCS logs.
- */
- #ifdef __GNUC__
- #pragma implementation "simpdlg.h"
- #endif
- #include <pwlib.h>
- #define new PNEW
- ///////////////////////////////////////////////////////////////////////////////
- // PSimpleDialog
- #if defined(_PSIMPLEDIALOG)
- PSimpleDialog::PSimpleDialog(PInteractor * parent,
- const PString & str, ButtonsToHave btns, IconToShow icn)
- : PModalDialog(parent)
- {
- SetFont(owner->GetSystemFont());
- PInteractor * topWnd = parent;
- while (topWnd->GetParent() != NULL)
- topWnd = topWnd->GetParent();
- PString title(((PTitledWindow*)topWnd)->GetTitle());
- SetTitle(title);
- PResourceString okStr (PSTD_ID_STR_OK, "OK");
- PResourceString cancelStr(PSTD_ID_STR_CANCEL, "Cancel");
- PResourceString yesStr (PSTD_ID_STR_YES, "Yes");
- PResourceString noStr (PSTD_ID_STR_NO, "No");
- PResourceString abortStr (PSTD_ID_STR_ABORT, "Abort");
- PResourceString retryStr (PSTD_ID_STR_RETRY, "Retry");
- PResourceString ignoreStr(PSTD_ID_STR_IGNORE, "Ignore");
- switch (btns) {
- case OkCancelBtn :
- btn1 = new PTextButton(this, okStr, PPushButton::DefaultButton);
- btn2 = new PTextButton(this, cancelStr);
- btn3 = NULL;
- btn1val = WasOk;
- btn2val = WasCancel;
- break;
- case YesNoBtn :
- btn1 = new PTextButton(this, yesStr, PPushButton::DefaultButton);
- btn2 = new PTextButton(this, noStr);
- btn3 = NULL;
- btn1val = WasYes;
- btn2val = WasNo;
- break;
- case YesNoCancelBtn :
- btn1 = new PTextButton(this, yesStr, PPushButton::DefaultButton);
- btn2 = new PTextButton(this, noStr);
- btn3 = new PTextButton(this, cancelStr);
- btn1val = WasYes;
- btn2val = WasNo;
- btn3val = WasCancel;
- break;
- case RetryCancelBtn :
- btn1 = new PTextButton(this, retryStr, PPushButton::DefaultButton);
- btn2 = new PTextButton(this, cancelStr);
- btn3 = NULL;
- btn1val = WasRetry;
- btn2val = WasCancel;
- break;
- case AbortRetryIgnoreBtn :
- btn1 = new PTextButton(this, abortStr);
- btn2 = new PTextButton(this, retryStr, PPushButton::DefaultButton);
- btn3 = new PTextButton(this, ignoreStr);
- btn1val = WasAbort;
- btn2val = WasRetry;
- btn3val = WasIgnore;
- break;
- default :
- btn1 = new PTextButton(this, okStr, PPushButton::DefaultButton);
- btn2 = btn3 = NULL;
- btn1val = WasOk;
- }
- switch (icn) {
- #ifdef _PSTATICICON
- case InformationIcon :
- icon = new PStaticIcon(this, PIcon(PSTD_ID_ICON_INFORMATION));
- break;
- case QuestionIcon :
- icon = new PStaticIcon(this, PIcon(PSTD_ID_ICON_QUESTION));
- break;
- case ExclamationIcon :
- icon = new PStaticIcon(this, PIcon(PSTD_ID_ICON_EXCLAMATION));
- break;
- case StopSignIcon :
- icon = new PStaticIcon(this, PIcon(PSTD_ID_ICON_STOPSIGN));
- break;
- #endif
- default :
- icon = NULL;
- }
- text = new PStaticText(this, str);
- PDim tdim(text->GetDimensions(LocalCoords));
- PDim dlgdim(tdim); // Start with size of text
- PDim idim;
- if (icon != NULL) {
- idim = icon->GetDimensions(LocalCoords);
- idim.SetWidth(idim.Width()*3/2);
- }
- if (dlgdim.Height() < idim.Height()) // allow enough vertical space for the icon
- dlgdim.SetHeight(idim.Height());
- dlgdim.SetWidth(dlgdim.Width() + idim.Width()); // allow horizontal space plus half icon width gap
- PDim bdim(btn1->GetDimensions(LocalCoords));
- if (btn2 != NULL) {
- PDim b2dim(btn2->GetDimensions(LocalCoords));
- if (bdim.Height() < b2dim.Height())
- bdim.SetHeight(b2dim.Height());
- if (bdim.Width() < b2dim.Width())
- bdim.SetWidth(b2dim.Width());
- }
- if (btn3 != NULL) {
- PDim b3dim(btn3->GetDimensions(LocalCoords));
- if (bdim.Height() < b3dim.Height())
- bdim.SetHeight(b3dim.Height());
- if (bdim.Width() < b3dim.Width())
- bdim.SetWidth(b3dim.Width());
- }
- btn1->SetDimensions(bdim, LocalCoords); // Set buttons to all the same size
- if (btn2 != NULL)
- btn2->SetDimensions(bdim, LocalCoords);
- if (btn3 != NULL)
- btn3->SetDimensions(bdim, LocalCoords);
- PDim bzonedim(bdim);
- if (btn2 != NULL && btn3 != NULL)
- bzonedim.SetWidth(bdim.Width()*5); // Total buttons width, 3 buttons + 4 half width gaps
- else if (btn2 != NULL)
- bzonedim.SetWidth(bdim.Width()*7/2); // Total buttons width, 2 buttons + 3 half width gaps
- bzonedim.SetHeight(bzonedim.Height() * 2);
-
- if (dlgdim.Width() < bzonedim.Width()) // Allow enough horizontal space for buttons
- dlgdim.SetWidth(bzonedim.Width());
- dlgdim.SetHeight(dlgdim.Height() + bzonedim.Height()); // Allow vertical space for buttons with half a button
- // height above and below
- dlgdim += PDim(20,10); // Left, right and top white space all 10
- // Assure it is a minimum width for title
- PDim titledim(PDrawCanvas(this).MeasureString(title));
- titledim.SetWidth(titledim.Width() + 40);
- if (dlgdim.Width() < titledim.Width())
- dlgdim.SetWidth(titledim.Width());
- // Now clip to the screen size
- PDim sdim(FromPixels(owner->GetScreenRect().Dimensions()));
- if (dlgdim.Width() > sdim.Width()) {
- dlgdim.SetWidth(sdim.Width());
- tdim.SetWidth(dlgdim.Width() - 20 - idim.Width());
- }
- if (dlgdim.Height() > sdim.Height())
- dlgdim.SetHeight(sdim.Height());
- // Make sure it doesn't get smaller than 1/4 screen width either.
- sdim.SetWidth(sdim.Width()/4);
- if (dlgdim.Width() < sdim.Width())
- dlgdim.SetWidth(sdim.Width());
- // Size and position the dialog
- SetDimensions(dlgdim, LocalCoords);
- SetPosition(0, 0, CentreScreen, CentreScreen);
-
- // Now position all the child windows
- if (icon != NULL) {
- PDIMENSION ty = dlgdim.Height() - bzonedim.Height() - 10;
- icon->SetPosition(10, 10 + (ty - idim.Height())/2);
- text->SetPosition(10 + idim.Width(), 10);
- text->SetDimensions(dlgdim.Width()-20-idim.Width(), ty, LocalCoords);
- }
- else {
- text->SetPosition(10, 10);
- text->SetDimensions(dlgdim.Width()-20,
- dlgdim.Height() - bzonedim.Height() - 10,
- LocalCoords);
- }
-
- PORDINATE bpos = dlgdim.Height() - bzonedim.Height() + bdim.Height()/2;
- if (btn2 != NULL && btn3 != NULL) {
- PORDINATE gap = (dlgdim.Width() - bdim.Width()*3)/4;
- btn1->SetPosition(gap, bpos);
- btn2->SetPosition(gap*2 + bdim.Width(), bpos);
- btn3->SetPosition(gap*3 + bdim.Width()*2, bpos);
- }
- else if (btn2 != NULL) {
- PORDINATE gap = (dlgdim.Width() - bdim.Width()*2)/3;
- btn1->SetPosition(gap, bpos);
- btn2->SetPosition(gap*2 + bdim.Width(), bpos);
- }
- else
- btn1->SetPosition((dlgdim.Width()-bdim.Width())/2, bpos);
- // Show all the the children
- text->Show();
- btn1->SetNotifier(PCREATE_NOTIFIER(DoButtonPress));
- btn1->Show();
- if (btn2 != NULL) {
- btn2->Show();
- btn2->SetNotifier(btn1->GetNotifier());
- }
- if (btn3 != NULL) {
- btn3->Show();
- btn3->SetNotifier(btn1->GetNotifier());
- }
- if (icon != NULL)
- icon->Show();
- }
- void PSimpleDialog::DoButtonPress(PPushButton & button, INT)
- {
- if (&button == btn1)
- EndModal(btn1val);
- else if (&button == btn2)
- EndModal(btn2val);
- else if (&button == btn3)
- EndModal(btn3val);
- }
- PSimpleDialog::ButtonType PSimpleDialog::DoSimple(PInteractor * parent,
- PRESOURCE_ID resID, ButtonsToHave btns, IconToShow icn, va_list args)
- {
- PResourceString str(resID);
- return DoSimple(parent, str, btns, icn, args);
- }
- void PSimpleDialog::Info(PInteractor * parent, PRESOURCE_ID resID, ...)
- {
- va_list args;
- va_start(args, resID);
- DoSimple(parent, resID, OkBtn, InformationIcon, args);
- }
- void PSimpleDialog::Error(PInteractor * parent, PRESOURCE_ID resID, ...)
- {
- va_list args;
- va_start(args, resID);
- DoSimple(parent, resID, OkBtn, ExclamationIcon, args);
- }
- void PSimpleDialog::FatalError(PInteractor * parent, PRESOURCE_ID resID, ...)
- {
- va_list args;
- va_start(args, resID);
- DoSimple(parent, resID, OkBtn, StopSignIcon, args);
- }
- BOOL PSimpleDialog::OkCancel(PInteractor * parent, PRESOURCE_ID resID, ...)
- {
- va_list args;
- va_start(args, resID);
- return DoSimple(parent, resID, OkCancelBtn, QuestionIcon, args) == WasOk;
- }
- BOOL PSimpleDialog::YesNo(PInteractor * parent, PRESOURCE_ID resID, ...)
- {
- va_list args;
- va_start(args, resID);
- return DoSimple(parent, resID, YesNoBtn, QuestionIcon, args) == WasYes;
- }
- PSimpleDialog::ButtonType
- PSimpleDialog::YesNoCancel(PInteractor * parent, PRESOURCE_ID resID, ...)
- {
- va_list args;
- va_start(args, resID);
- return DoSimple(parent, resID, YesNoCancelBtn, QuestionIcon, args);
- }
- PSimpleDialog::ButtonType PSimpleDialog::AbortRetryIgnore(
- PInteractor * parent, PRESOURCE_ID resID, ...)
- {
- va_list args;
- va_start(args, resID);
- return DoSimple(parent, resID, AbortRetryIgnoreBtn, ExclamationIcon, args);
- }
- PSimpleDialog::ButtonType PSimpleDialog::DoSimple(PInteractor * parent,
- const char * fmt, ButtonsToHave btns, IconToShow icn, va_list args)
- {
- PString str;
- str.vsprintf(fmt, args);
- return DoSimple(parent, str, btns, icn);
- }
- void PSimpleDialog::Info(PInteractor * parent, const char * str, ...)
- {
- va_list args;
- va_start(args, str);
- DoSimple(parent, str, OkBtn, InformationIcon, args);
- }
- void PSimpleDialog::Error(PInteractor * parent, const char * str, ...)
- {
- va_list args;
- va_start(args, str);
- DoSimple(parent, str, OkBtn, ExclamationIcon, args);
- }
- void PSimpleDialog::FatalError(PInteractor * parent, const char * str, ...)
- {
- va_list args;
- va_start(args, str);
- DoSimple(parent, str, OkBtn, StopSignIcon, args);
- }
- BOOL PSimpleDialog::OkCancel(PInteractor * parent, const char * str, ...)
- {
- va_list args;
- va_start(args, str);
- return DoSimple(parent, str, OkCancelBtn, QuestionIcon, args) == WasOk;
- }
- BOOL PSimpleDialog::YesNo(PInteractor * parent, const char * str, ...)
- {
- va_list args;
- va_start(args, str);
- return DoSimple(parent, str, YesNoBtn, QuestionIcon, args) == WasYes;
- }
- PSimpleDialog::ButtonType
- PSimpleDialog::YesNoCancel(PInteractor * parent, const char * str, ...)
- {
- va_list args;
- va_start(args, str);
- return DoSimple(parent, str, YesNoCancelBtn, QuestionIcon, args);
- }
- PSimpleDialog::ButtonType PSimpleDialog::AbortRetryIgnore(
- PInteractor * parent, const char * str, ...)
- {
- va_list args;
- va_start(args, str);
- return DoSimple(parent, str, AbortRetryIgnoreBtn, ExclamationIcon, args);
- }
- PSimpleDialog::ButtonType PSimpleDialog::DoSimple(PInteractor * parent,
- const PString & str, ButtonsToHave btns, IconToShow icn)
- {
- PSimpleDialog dlg(parent, str, btns, icn);
- return (ButtonType)dlg.RunModal();
- }
- void PSimpleDialog::Info(PInteractor * parent, const PString & str)
- {
- DoSimple(parent, str, OkBtn, InformationIcon);
- }
- void PSimpleDialog::Error(PInteractor * parent, const PString & str)
- {
- DoSimple(parent, str, OkBtn, ExclamationIcon);
- }
- void PSimpleDialog::FatalError(PInteractor * parent, const PString & str)
- {
- DoSimple(parent, str, OkBtn, StopSignIcon);
- }
- BOOL PSimpleDialog::OkCancel(PInteractor * parent, const PString & str)
- {
- return DoSimple(parent, str, OkCancelBtn, QuestionIcon) == WasOk;
- }
- BOOL PSimpleDialog::YesNo(PInteractor * parent, const PString & str)
- {
- return DoSimple(parent, str, YesNoBtn, QuestionIcon) == WasYes;
- }
- PSimpleDialog::ButtonType
- PSimpleDialog::YesNoCancel(PInteractor * parent, const PString & str)
- {
- return DoSimple(parent, str, YesNoCancelBtn, QuestionIcon);
- }
- PSimpleDialog::ButtonType PSimpleDialog::AbortRetryIgnore(
- PInteractor * parent, const PString & str)
- {
- return DoSimple(parent, str, AbortRetryIgnoreBtn, ExclamationIcon);
- }
- #endif
- #undef new
- // End Of File ///////////////////////////////////////////////////////////////