dialogs.cxx
上传用户:hzhsqp
上传日期:2007-01-06
资源大小:1600k
文件大小:38k
- /*
- * dialogs.cxx
- *
- * Dialog classes implementation.
- *
- * 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: dialogs.cxx,v $
- * Revision 1.66 2000/04/04 22:26:20 robertj
- * Fixed bug in PDialog when close button closes regardless of OnCancel() virtual.
- *
- * Revision 1.65 2000/03/17 20:32:22 robertj
- * Fixed race conditions in mult-threaded windows (dialogs in particular)
- *
- * Revision 1.64 2000/03/04 13:08:35 robertj
- * Moved common function in open file dialog to common source file.
- *
- * Revision 1.63 2000/03/04 10:09:38 robertj
- * Fixed bugs in open file dialog (showed up in Win2K).
- *
- * Revision 1.62 1999/11/16 06:51:42 robertj
- * Created PCustomListBox to allow motif native code PStringListBox implementation
- *
- * Revision 1.61 1999/11/01 00:07:44 robertj
- * Changed semantics of OnClose() to be the same as PTitledWindow
- *
- * Revision 1.60 1999/10/24 11:52:05 robertj
- * Fixed problems with creating/deleting dialogs from background threads.
- *
- * Revision 1.59 1999/08/25 05:13:33 robertj
- * Fixed problem with calling modal dialog from a background thread.
- *
- * Revision 1.58 1999/08/07 07:13:23 robertj
- * Fixed problems with "balloon help" text popup.
- *
- * Revision 1.57 1999/02/16 08:08:07 robertj
- * MSVC 6.0 compatibility changes.
- *
- * Revision 1.56 1998/12/12 00:43:51 robertj
- * Added functions for user request on printing selection only.
- * Fixed transfer of start and end pages from PPrintDialog.
- *
- * Revision 1.55 1998/12/02 03:50:12 robertj
- * Unix compatibility changes
- *
- * Revision 1.54 1998/09/24 03:42:39 robertj
- * Added open software license.
- *
- * Revision 1.53 1998/09/24 01:55:32 robertj
- * Fixed bug that caused modal dialog not to go away when closed from another thread.
- *
- * Revision 1.52 1998/09/22 15:09:45 robertj
- * Fixed confusion in Font Dialog, variable at different levels with same name.
- * Fixed Font Dialog not returning font that was selected.
- *
- * Revision 1.51 1998/09/17 04:25:56 robertj
- * Fixed race condition if dialog is closed by a different thread to the PProcess thrad.
- *
- * Revision 1.50 1998/09/14 13:14:20 robertj
- * Fixed memory leak on delete of PStringList, not deleting all its contents.
- * Fixed failure to update control values on non-modal dialog box initialisation.
- *
- * Revision 1.49 1998/09/04 07:01:06 robertj
- * Added a lot of DEVMODE infor for win95 to propagate from PrintDlg to PPrintCanvas.
- *
- * Revision 1.48 1998/03/20 03:12:44 robertj
- * Fixed bug in standard file dialog getting filename and folder.
- *
- * Revision 1.47 1998/03/09 07:14:31 robertj
- * Explorer based OpenDirDialog.
- * More implementation of file type lists.
- *
- * Revision 1.46 1998/03/09 01:46:45 robertj
- * Enhanced file types for standard file dialog.
- *
- * Revision 1.44 1996/11/04 03:32:25 robertj
- * Fixed bug in background colour of dialogs.
- *
- * Revision 1.43 1996/10/31 12:39:56 robertj
- * Added RCS keywords.
- *
- */
- #include <pwlib.h>
- class PDummyListBox : public PListBox
- {
- PCLASSINFO(PDummyListBox, PListBox)
- public:
- PDummyListBox(PInteractorLayout * layout, PRESOURCE_ID ctrlID)
- : PListBox(layout, ctrlID, PNotifier(), NULL) { }
- virtual void OnDrawEntry(PINDEX, PObject &, PCanvas &, const PRect &, BOOL, BOOL)
- { }
- virtual PDim OnMeasureEntry(PINDEX, PObject &, PCanvas &)
- { return PDim(); }
- virtual BOOL IsOwnerDraw() const
- { return FALSE; }
- };
- #define new PNEW
- ///////////////////////////////////////////////////////////////////////////////
- // PInteractorLayout
- PInteractorLayout::PInteractorLayout(PInteractor * parent)
- : PInteractor(parent)
- {
- focusInteractor = this;
- pResourceID = (PRESOURCE_ID)-1;
- }
- PInteractorLayout::PInteractorLayout(PInteractor * parent, PRESOURCE_ID resID)
- : PInteractor(parent)
- {
- focusInteractor = this;
- pResourceID = resID;
- }
- PInteractorLayout::~PInteractorLayout()
- {
- if (_hWnd != NULL && PThread::Current() != owner) {
- if (_hWnd != P_DEAD_WINDOW)
- PostMessage(_hWnd, WM_CLOSE, 0, 0);
- handledDestroySignal.Wait();
- }
- owner->RemoveDialog(this);
- }
- BOOL PEXPORTED EnumDlgChildren(HWND hWnd, PInteractorLayout * layout)
- {
- if (GetParent(hWnd) != layout->GetHWND())
- return TRUE;
- PRESOURCE_ID ctrlID = (PRESOURCE_ID)GetDlgCtrlID(hWnd);
- if (ctrlID == 0xffff)
- return TRUE;
- if (layout->GetControl(ctrlID) != NULL)
- return TRUE;
- PControl * child = NULL;
- char className[20];
- GetClassName(hWnd, className, sizeof(className));
- DWORD style = GetWindowLong(hWnd, GWL_STYLE);
- if (lstrcmpi(className, "EDIT") == 0) {
- if ((style&ES_MULTILINE) != 0)
- child = new PMultiLineEditBox(layout, ctrlID, PNotifier(), NULL);
- else {
- char str[30];
- GetWindowText(hWnd, str, sizeof(str));
- SetWindowText(hWnd, "");
- if (lstrcmpi(str, "INTEDIT") == 0)
- child = new PIntegerEditBox(layout, ctrlID, PNotifier(), NULL);
- else if (lstrcmpi(str, "REALEDIT") == 0)
- child = new PFloatEditBox(layout, ctrlID, PNotifier(), NULL);
- else
- child = new PEditBox(layout, ctrlID, PNotifier(), NULL);
- }
- }
- else if (lstrcmpi(className, "STATIC") == 0) {
- switch (style&15) {
- case SS_LEFT :
- case SS_SIMPLE :
- case SS_LEFTNOWORDWRAP :
- case SS_CENTER :
- case SS_RIGHT :
- child = new PStaticText(layout, ctrlID, PNotifier(), NULL);
- break;
- case SS_ICON :
- child = new PStaticIcon(layout, ctrlID, PNotifier(), NULL);
- break;
- }
- }
- else if (lstrcmpi(className, "BUTTON") == 0) {
- switch (style&15) {
- case BS_PUSHBUTTON :
- case BS_DEFPUSHBUTTON :
- child = new PTextButton(layout, ctrlID, PNotifier(), NULL);
- break;
- case BS_AUTOCHECKBOX :
- case BS_CHECKBOX :
- child = new PCheckBox(layout, ctrlID, PNotifier(), NULL);
- break;
- case BS_AUTO3STATE :
- case BS_3STATE :
- child = new PCheck3WayBox(layout, ctrlID, PNotifier(), NULL);
- break;
- case BS_AUTORADIOBUTTON :
- case BS_RADIOBUTTON :
- child = new PRadioButton(layout, ctrlID, PNotifier(), NULL);
- break;
- case BS_GROUPBOX :
- child = new PStaticBox(layout, ctrlID, PNotifier(), NULL);
- break;
- }
- }
- else if (lstrcmpi(className, "LISTBOX") == 0) {
- if ((style&LBS_OWNERDRAWVARIABLE) == 0)
- child = new PDummyListBox(layout, ctrlID);
- else
- child = new PStringListBox(layout, ctrlID, PNotifier(), NULL);
- }
- else if (lstrcmpi(className, "COMBOBOX") == 0) {
- switch (style&15) {
- case CBS_DROPDOWN :
- child = new PChoiceBox(layout, ctrlID, PNotifier(), NULL);
- break;
- case CBS_DROPDOWNLIST :
- child = new PComboBox(layout, ctrlID, PNotifier(), NULL);
- break;
- }
- }
- else if (lstrcmpi(className, "SCROLLBAR") == 0) {
- if ((style&1) != 0)
- child = new PVerticalScrollBar(layout, ctrlID, PNotifier(), NULL);
- else
- child = new PHorizontalScrollBar(layout, ctrlID, PNotifier(), NULL);
- }
- return TRUE;
- }
- void PInteractorLayout::ConstructEnd(PRESOURCE_ID resID)
- {
- EnumChildWindows(GetHWND(), (WNDENUMPROC)EnumDlgChildren, (LPARAM)this);
- PResourceData dialog("#5", resID);
- PResourceData strings("DIALOGSTRINGS", resID);
- #if defined(_WIN32)
- PINDEX numControls = ((const DLGTEMPLATE *)(const BYTE *)dialog)->cdit;
- const WORD * strptr = (const WORD *)(const BYTE *)strings;
- const WORD * strbase;
- #else
- PINDEX numControls = dialog[4];
- const char * strptr = (const char *)(const BYTE *)strings;
- const char * strbase;
- #endif
- for (PINDEX i = 0; i < numControls; i++) {
- PControl & child = (PControl &)children[i];
- strbase = strptr;
- PINDEX len = 0;
- while (*strptr++ != 0)
- len++;
- if (len != 0)
- child.SetBalloonHelp(PString(strbase, len));
- while (*strptr != 0) {
- strbase = strptr;
- len = 0;
- while (*strptr++ != 0)
- len++;
- if (child.IsDescendant(PChoiceBox::Class()))
- ((PChoiceBox&)child).AddString(PString(strbase, len));
- else if (child.IsDescendant(PStringListBox::Class()))
- ((PStringListBox&)child).AddString(PString(strbase, len));
- else if (child.IsDescendant(PComboBox::Class()))
- ((PComboBox&)child).AddString(PString(strbase, len));
- }
- strptr++;
- }
- for (i = 1; i < GetNumChildren(); i++) {
- if (children[i].IsDescendant(PRadioButton::Class()) &&
- children[i-1].IsDescendant(PRadioButton::Class()))
- ((PRadioButton &)children[i]).AddToGroup((PRadioButton &)children[i-1]);
- }
- UpdateControls();
- }
- void PInteractorLayout::GetCreateWinInfo(WNDCLASS & wndClass)
- {
- PInteractor::GetCreateWinInfo(wndClass);
- wndClass.lpszClassName = "Dialog";
- }
- void PInteractorLayout::CreateHWND()
- {
- if (pResourceID != -1) {
- _hWnd = CreateDialog(owner->GetInstance(),
- MAKEINTRESOURCE(pResourceID),
- parent->GetHWND(),
- (DLGPROC)owner->GetWndProcPtr(PApplication::DlgProcType));
- PAssert(_hWnd != NULL, PInvalidWindow);
- PPoint pos = GetPosition(ScreenCoords);
- if (pos.X() < 0 && pos.Y() < 0)
- SetPosition(0,0,CentreParent,CentreParent);
- HFONT hFont = (HFONT)(UINT)SendMessage(_hWnd, WM_GETFONT, 0, 0L);
- if (hFont != NULL)
- font = PRealFont(hFont);
- else
- SetFont(parent->GetFont(), TRUE);
- }
- else {
- WNDCLASS dummyWndClass;
- #if defined(_WIN32)
- struct {
- DLGTEMPLATE header;
- WORD szMenuName[1], szClassName[1], szCaption[1];
- WORD wPointSize;
- WORD szFaceName[1];
- } emptyDlg;
- memset(&emptyDlg, 0, sizeof(emptyDlg));
- GetCreateWinInfo(dummyWndClass);
- emptyDlg.header.dwExtendedStyle = _exStyleBits;
- #else
- struct {
- struct {
- DWORD style;
- BYTE bNumberOfItems;
- WORD x, y, cx, cy;
- } header;
- char szMenuName[1], szClassName[1], szCaption[1];
- WORD wPointSize;
- char szFaceName[1];
- } emptyDlg;
- memset(&emptyDlg, 0, sizeof(emptyDlg));
- GetCreateWinInfo(dummyWndClass);
- #endif
- emptyDlg.header.style = _styleBits;
- _hWnd = CreateDialogIndirect(owner->GetInstance(),
- &emptyDlg.header,
- parent->GetHWND(),
- (DLGPROC)owner->GetWndProcPtr(PApplication::DlgProcType));
- PAssert(_hWnd != NULL, PInvalidWindow);
- }
- _styleBits = GetWindowLong(_hWnd, GWL_STYLE);
- _exStyleBits = GetWindowLong(_hWnd, GWL_EXSTYLE);
- owner->AddWindowHandle(_hWnd, this);
- owner->AddDialog(this);
- }
- void PInteractorLayout::WndProc()
- {
- PInteractor::WndProc();
- switch (_msg->event) {
- case WM_CLOSE :
- DestroyWindow(_hWnd);
- break;
- case WM_DESTROY :
- owner->RemoveDialog(this);
- handledDestroySignal.Signal();
- break;
- }
- handledMessageSignal.Signal();
- }
- void PInteractorLayout::DefWndProc()
- {
- _msg->processed = FALSE;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // PDialog
- PDialog::PDialog(PInteractor * parent)
- : PInteractorLayout(parent)
- {
- backgroundColour = owner->GetButtonBkColour();
- }
- PDialog::PDialog(PInteractor * parent, PRESOURCE_ID resID)
- : PInteractorLayout(parent, resID)
- {
- backgroundColour = owner->GetButtonBkColour();
- }
- void PDialog::WndProc()
- {
- if (_msg->event == WM_CLOSE)
- OnCancel();
- else
- PInteractor::WndProc();
- }
- void PDialog::GetCreateWinInfo(WNDCLASS & wndClass)
- {
- PInteractorLayout::GetCreateWinInfo(wndClass);
- _styleBits = WS_POPUP|WS_CLIPCHILDREN|WS_CAPTION|WS_SYSMENU|WS_BORDER;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // PFloatingDialog
- void PFloatingDialog::Construct()
- {
- titleFont = PRealFont(PFont("MS Sans Serif", 1)); // Smallest font possible
- borderWidth = owner->GetBorderSize().Width();
- borderHeight = owner->GetBorderSize().Height();
- titleHeight = titleFont.GetHeight(TRUE) + borderHeight*2;
- }
- void PFloatingDialog::WndProc()
- {
- switch (_msg->event) {
- case WM_NCCALCSIZE :
- NCCalcSize((LPNCCALCSIZE_PARAMS)_msg->lParam);
- return;
-
- case WM_NCHITTEST :
- _msg->lResult = NCHitTest(_msg->lParam);
- return;
- case WM_NCPAINT :
- NCPaint();
- return;
- }
- PDialog::WndProc();
- }
- void PFloatingDialog::NCCalcSize(LPNCCALCSIZE_PARAMS cs)
- {
- PDim bdr = owner->GetBorderSize();
- cs->rgrc[0].top += titleHeight + borderHeight;
- cs->rgrc[0].left += borderWidth;
- cs->rgrc[0].bottom -= borderHeight;
- cs->rgrc[0].right -= borderWidth;
- }
- LRESULT PFloatingDialog::NCHitTest(const PPoint & pt)
- {
- PRect rect;
- GetWindowRect(_hWnd, rect);
- rect.Inflate(-(PORDINATE)borderWidth, -(PORDINATE)borderHeight);
- if (!rect.ContainsPoint(pt))
- return HTBORDER;
- if (pt.Y() > rect.Y() + (PORDINATE)titleHeight)
- return HTCLIENT;
- if (pt.X() < rect.X() + (PORDINATE)titleHeight)
- return HTSYSMENU;
- return HTCAPTION;
- }
- void PFloatingDialog::NCPaint()
- {
- PDrawCanvas canvas(this, GetWindowDC(_hWnd), TRUE, TRUE);
- PRect rect;
- GetWindowRect(_hWnd, rect);
- rect.SetOrigin(0, 0);
- PColour bdrcol;
- bdrcol.FromSYSCOLOR(COLOR_WINDOWFRAME);
- canvas.SetPenFgColour(bdrcol);
- canvas.SetFillFgColour(PColour::Clear);
- canvas.DrawRect(rect);
- rect.SetHeight(titleHeight+borderHeight);
- canvas.SetFillFgColour(owner->GetActiveTitleBkColour());
- canvas.FillRect(rect);
- PString title = GetTitle();
- canvas.SetTextFgColour(owner->GetActiveTitleFgColour());
- canvas.SetTextBkColour(owner->GetActiveTitleBkColour());
- canvas.SetFont(titleFont);
- canvas.DrawTextLine(rect.X() + titleHeight,
- rect.Y() + borderHeight, title, NULL, title.GetLength());
- rect.SetWidth(titleHeight);
- rect.Inflate(-(int)borderWidth*2, -(int)borderHeight*2);
- canvas.SetFillFgColour(owner->GetButtonBkColour());
- canvas.DrawBevelledRect(rect, TRUE, FALSE);
- }
- void PFloatingDialog::GetCreateWinInfo(WNDCLASS & wndClass)
- {
- PDialog::GetCreateWinInfo(wndClass);
- _styleBits = WS_POPUP|WS_CLIPCHILDREN|WS_BORDER|WS_SYSMENU;
- _exStyleBits |= WS_EX_TOPMOST;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // PModalDialog
- PModalDialog::~PModalDialog()
- {
- }
- void PModalDialog::Construct()
- {
- ok = cancel = NULL;
- runState = Initialising;
- }
- int PModalDialog::RunModal()
- {
- runState = Running;
- owner->GetWindow()->Disable();
- OnInit();
- Show();
- if (PThread::Current() == owner) {
- while (runState == Running && owner->ProcessMessage())
- ;
- }
- else {
- while (runState == Running)
- handledMessageSignal.Wait();
- }
- return returnValue;
- }
- void PModalDialog::EndModal(int retVal)
- {
- owner->GetWindow()->Enable();
- Hide();
- returnValue = retVal;
- runState = Ended;
- PostMessage(_hWnd, WM_COMMAND, 0, 0);
- }
- void PModalDialog::GetCreateWinInfo(WNDCLASS & wndClass)
- {
- PDialog::GetCreateWinInfo(wndClass);
- _styleBits |= DS_MODALFRAME;
- _exStyleBits |= WS_EX_DLGMODALFRAME;
- }
- void PModalDialog::InitStandardDialog()
- {
- // EnumChildWindows(_hWnd, (WNDENUMPROC)EnumDlgChildren, (LPARAM)this);
- font = PRealFont((HFONT)(UINT)SendMessage(_hWnd, WM_GETFONT, 0, 0L));
- HDC hDC = GetDC(_hWnd);
- HBRUSH hBrush = (HBRUSH)(UINT)SendMessage(_hWnd,
- #ifdef _WIN32
- WM_CTLCOLORDLG, (WPARAM)hDC, (LPARAM)_hWnd);
- #else
- WM_CTLCOLOR, (WPARAM)hDC, MAKELONG(_hWnd, CTLCOLOR_DLG));
- #endif
- ReleaseDC(_hWnd, hDC);
- if (hBrush == NULL)
- hBrush = (HBRUSH)PGET_CLASS_VALUE(_hWnd, HBRBACKGROUND);
- if (hBrush != NULL) {
- LOGBRUSH brush;
- GetObject(hBrush, sizeof(brush), (LPSTR)&brush);
- backgroundColour.FromCOLORREF(brush.lbColor);
- }
- runState = Running;
- _styleBits = GetWindowLong(_hWnd, GWL_STYLE) | WS_CLIPSIBLINGS;
- SetWindowLong(_hWnd, GWL_STYLE, _styleBits);
- _exStyleBits = GetWindowLong(_hWnd, GWL_EXSTYLE);
- OnInit();
- }
- ///////////////////////////////////////////////////////////////////////////////
- // PFileDialog
- #if !defined(_WIN32)
- typedef UINT (CALLBACK * LPOFNHOOKPROC)(HWND, UINT, WPARAM, LPARAM);
- #endif
- PFileDialog::PFileDialog(PInteractor * parent, PRESOURCE_ID resID)
- : PModalDialog(parent, resID)
- {
- selChangeMessage = RegisterWindowMessage(LBSELCHSTRING);
- memset(&fileDlgInfo, 0, sizeof(fileDlgInfo));
- fileDlgInfo.lStructSize = sizeof(fileDlgInfo);
- fileDlgInfo.hwndOwner = parent->GetHWND();
- fileDlgInfo.hInstance = owner->GetInstance();
- fileBuffer[0] = ' ';
- fileDlgInfo.lpstrFile = fileBuffer;
- strcpy(customFilter, "All Files");
- memcpy(&customFilter[strlen(customFilter)+1], "*.* ", 5);
- fileDlgInfo.lpstrCustomFilter = customFilter;
- fileDlgInfo.nMaxCustFilter = sizeof(customFilter);
- fileDlgInfo.nMaxFile = sizeof(fileBuffer);
- #ifdef OFN_EXPLORER
- fileDlgInfo.Flags = OFN_ENABLEHOOK|OFN_HIDEREADONLY|OFN_NOVALIDATE|OFN_EXPLORER;
- #else
- fileDlgInfo.Flags = OFN_ENABLEHOOK|OFN_HIDEREADONLY|OFN_NOVALIDATE;
- #endif
- fileDlgInfo.lCustData = (DWORD)this;
- fileDlgInfo.lpfnHook =
- (LPOFNHOOKPROC)owner->GetWndProcPtr(PApplication::FileDlgProcType);
- }
- PFileDialog::~PFileDialog()
- {
- if (_hWnd != P_DEAD_WINDOW)
- DestroyWindow(_hWnd);
- }
- void PFileDialog::SetTitle(const PString & title)
- {
- if (_hWnd != NULL)
- PModalDialog::SetTitle(title);
- else {
- dlgTitle = title;
- fileDlgInfo.lpstrTitle = dlgTitle;
- }
- }
- void PFileDialog::WndProc()
- {
- switch (_msg->event) {
- case WM_INITDIALOG :
- InitStandardDialog();
- return;
- #ifndef OFN_EXPLORER
- case WM_COMMAND :
- switch (LOWORD(_msg->wParam)) {
- case IDOK : {
- directory = PDirectory();
- GetDlgItemText(_hWnd, 1152, fileBuffer, sizeof(fileBuffer));
- file = PFilePath(fileBuffer);
- OnOk();
- return;
- }
- case IDCANCEL :
- OnCancel();
- return;
- }
- break;
- #endif
- case WM_NOTIFY:
- {
- OFNOTIFY * ofNotify = (OFNOTIFY *)_msg->lParam;
- HWND hDlg = ::GetParent(_hWnd);
- switch (ofNotify->hdr.code) {
- case CDN_FILEOK :
- SendMessage(hDlg, CDM_GETFOLDERPATH, sizeof(fileBuffer), (LPARAM)fileBuffer);
- directory = fileBuffer;
- SendMessage(hDlg, CDM_GETFILEPATH, sizeof(fileBuffer), (LPARAM)fileBuffer);
- file = PFilePath(fileBuffer);
- OnOk();
- return;
- }
- }
- break;
- default:
- if (_msg->event == selChangeMessage)
- OnListSelection(_msg->wParam, LOWORD(_msg->lParam), HIWORD(_msg->lParam));
- }
- PModalDialog::WndProc();
- }
- void PFileDialog::OnOk()
- {
- DefWndProc();
- }
- void PFileDialog::OnCancel()
- {
- DefWndProc();
- }
- void PFileDialog::OnListSelection(UINT, UINT, UINT)
- {
- }
- void PFileDialog::CreateHWND()
- {
- PAssertAlways("Illegal operation on PFileDialog");
- }
- ///////////////////////////////////////////////////////////////////////////////
- // POpenFileDialog
- POpenFileDialog::POpenFileDialog(PInteractor * parent, PRESOURCE_ID resID)
- : PFileDialog(parent, resID)
- {
- fileDlgInfo.Flags |= OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT;
- if (resID != PSTD_ID_DIALOG_OPEN_FILE) {
- fileDlgInfo.Flags |= OFN_ENABLETEMPLATE;
- fileDlgInfo.lpTemplateName = MAKEINTRESOURCE(resID);
- }
- }
- int POpenFileDialog::RunModal()
- {
- fileDlgInfo.lpstrInitialDir = directory;
- lstrcpy(fileBuffer, defaultFilename);
- PString fileTypeStr;
- if (fileTypes.GetSize() != 0) {
- for (PINDEX i = 0; i < fileTypes.GetSize(); i++) {
- PString s = fileTypes[i];
- if (s.Find('377') != P_MAX_INDEX)
- fileTypeStr += s + "377";
- else {
- PString tmp("*");
- tmp += s + "377";
- fileTypeStr += tmp + tmp;
- }
- }
- fileTypeStr += "377";
- fileDlgInfo.lpstrFilter = fileTypeStr;
- for (LPSTR p = (LPSTR)fileDlgInfo.lpstrFilter; *p != ' '; p++) {
- while (*p == '377')
- *p++ = ' ';
- }
- fileDlgInfo.nFilterIndex = 1;
- }
- if (GetOpenFileName(&fileDlgInfo)) {
- runState = Ended;
- return TRUE;
- }
- DWORD err = CommDlgExtendedError();
- #if defined(_WIN32)
- SetLastError(err);
- #endif
- PAssertOS(err == 0);
- return FALSE;
- }
- void POpenFileDialog::SetFileTypes(const PStringArray & array)
- {
- fileTypes = array;
- SetInitialFileType(GetFinalFileType());
- }
- void POpenFileDialog::SetInitialFileType(PINDEX i)
- {
- if (i != P_MAX_INDEX)
- fileDlgInfo.nFilterIndex = i+1;
- else
- fileDlgInfo.nFilterIndex = 0;
- }
- void POpenFileDialog::SetInitialFileType(const PString & str)
- {
- memset(customFilter, 0, sizeof(customFilter));
- strncpy(customFilter, str, sizeof(customFilter)-1);
- fileDlgInfo.nFilterIndex = 0;
- }
- PINDEX POpenFileDialog::GetFinalFileType() const
- {
- if (fileDlgInfo.nFilterIndex > 0)
- return fileDlgInfo.nFilterIndex-1;
- return P_MAX_INDEX;
- }
- BOOL POpenFileDialog::GetFinalFileType(PString & str) const
- {
- if ((PINDEX)fileDlgInfo.nFilterIndex > fileTypes.GetSize())
- return FALSE;
- if (fileDlgInfo.nFilterIndex > 0)
- str = fileTypes[fileDlgInfo.nFilterIndex-1];
- else
- str = &customFilter[strlen(customFilter)+1];
- return !str;
- }
- void POpenFileDialog::FilterFilesFromList()
- {
- PINDEX i = 0;
- while (i < (PINDEX)SendDlgItemMessage(_hWnd, 1120, LB_GETCOUNT, 0, 0)) {
- char str[P_MAX_PATH];
- SendDlgItemMessage(_hWnd, 1120, LB_GETTEXT, i, (DWORD)str);
- if (OnFilterFile(str))
- i++;
- else
- SendDlgItemMessage(_hWnd, 1120, LB_DELETESTRING, i, 0);
- }
- }
- void POpenFileDialog::OnListSelection(UINT listBox, UINT item, UINT operation)
- {
- if (listBox == 1120 && operation == CD_LBSELCHANGE) {
- char str[P_MAX_PATH];
- SendDlgItemMessage(_hWnd, 1120, LB_GETTEXT, item, (DWORD)str);
- OnFileSelect(str);
- }
- else if (listBox == 1121 && !fileTypes.IsEmpty())
- FilterFilesFromList();
- }
- void POpenFileDialog::WndProc()
- {
- PFileDialog::WndProc();
- switch (_msg->event) {
- case WM_INITDIALOG :
- FilterFilesFromList();
- break;
- case WM_COMMAND :
- if (LOWORD(_msg->wParam) == IDOK)
- FilterFilesFromList();
- break;
- }
- }
- ///////////////////////////////////////////////////////////////////////////////
- // PSaveFileDialog
- PSaveFileDialog::PSaveFileDialog(PInteractor * parent, PRESOURCE_ID resID)
- : PFileDialog(parent, resID)
- {
- fileDlgInfo.Flags |= OFN_CREATEPROMPT;
- if (resID != PSTD_ID_DIALOG_SAVE_FILE) {
- fileDlgInfo.Flags |= OFN_ENABLETEMPLATE;
- fileDlgInfo.lpTemplateName = MAKEINTRESOURCE(resID);
- }
- }
- int PSaveFileDialog::RunModal()
- {
- fileDlgInfo.lpstrInitialDir = directory;
- lstrcpy(fileBuffer, defaultFilename);
- if (GetSaveFileName(&fileDlgInfo)) {
- runState = Ended;
- return TRUE;
- }
- DWORD err = CommDlgExtendedError();
- #if defined(_WIN32)
- SetLastError(err);
- #endif
- PAssertOS(err == 0);
- return FALSE;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // POpenDirDialog
- POpenDirDialog::POpenDirDialog(PInteractor * parent, PRESOURCE_ID resID)
- : PFileDialog(parent, resID)
- {
- #ifdef OFN_EXPLORER
- fileDlgInfo.lpstrFilter = "~~a.ridiculous.extension A ridiculous extension ";
- lstrcpy(fileBuffer, "~~a.ridiculous.filename~~");
- if (resID != PSTD_ID_DIALOG_OPEN_DIR) {
- fileDlgInfo.Flags |= OFN_ENABLETEMPLATE;
- fileDlgInfo.lpTemplateName = MAKEINTRESOURCE(resID);
- }
- #else
- fileDlgInfo.Flags |= OFN_ENABLETEMPLATE;
- fileDlgInfo.lpTemplateName = MAKEINTRESOURCE(resID);
- #endif
- }
- int POpenDirDialog::RunModal()
- {
- fileDlgInfo.lpstrInitialDir = directory;
- if (GetOpenFileName(&fileDlgInfo)) {
- runState = Ended;
- return TRUE;
- }
- DWORD err = CommDlgExtendedError();
- #if defined(_WIN32)
- SetLastError(err);
- #endif
- PAssertOS(err == 0);
- return FALSE;
- }
- void POpenDirDialog::WndProc()
- {
- switch (_msg->event) {
- case WM_INITDIALOG :
- #ifdef OFN_EXPLORER
- {
- HWND dlgWnd = ::GetParent(_hWnd);
- ShowWindow(GetDlgItem(dlgWnd, 0x441), SW_HIDE);
- ShowWindow(GetDlgItem(dlgWnd, 0x470), SW_HIDE);
- ShowWindow(GetDlgItem(dlgWnd, 0x442), SW_HIDE);
- ShowWindow(GetDlgItem(dlgWnd, 0x480), SW_HIDE);
- }
- #else
- SetDlgItemText(_hWnd, 1159, PDirectory());
- #endif
- break;
- #ifndef OFN_EXPLORER
- case WM_COMMAND :
- if (LOWORD(_msg->wParam) == IDOK) {
- GetDlgItemText(dlgWnd, 1159, fileBuffer, sizeof(fileBuffer));
- directory = PDirectory(fileBuffer);
- SetDlgItemText(dlgWnd, 1152, "xxxxxxxx.xxx");
- }
- #endif
- }
- PFileDialog::WndProc();
- }
- void POpenDirDialog::OnListSelection(UINT, UINT, UINT)
- {
- #ifndef OFN_EXPLORER
- char str[P_MAX_PATH];
- GetDlgItemText(_hWnd, 1088, str, sizeof(str));
- SetDlgItemText(_hWnd, 1159, str);
- #endif
- }
- ///////////////////////////////////////////////////////////////////////////////
- // PPrintDialog
- #if !defined(_WIN32)
- typedef UINT (CALLBACK * LPPRINTHOOKPROC)(HWND, UINT, WPARAM, LPARAM);
- #endif
- void PPrintDialog::Construct()
- {
- memset(&printDlgInfo, 0, sizeof(printDlgInfo));
- printDlgInfo.hInstance = owner->GetInstance();
- printDlgInfo.lStructSize = sizeof(printDlgInfo);
- printDlgInfo.hwndOwner = GetParent()->GetHWND();
- printDlgInfo.lpfnPrintHook =
- (LPPRINTHOOKPROC)owner->GetWndProcPtr(PApplication::PrintDlgProcType);
- printDlgInfo.lCustData = (DWORD)this;
- printDlgInfo.Flags = PD_ENABLEPRINTHOOK;
- printDlgInfo.hDevMode = printInfo.GetHDEVMODE();
- printDlgInfo.hDevNames = printInfo.GetDevNames();
- if (printInfo.GetStartPage() == 0)
- printDlgInfo.Flags |= PD_NOPAGENUMS;
- else {
- printDlgInfo.nFromPage = (WORD)printInfo.GetStartPage();
- printDlgInfo.nToPage = (WORD)printInfo.GetEndPage();
- }
- switch (printInfo.GetSelectionOnly()) {
- case PPrintInfo::NoSelectionOnly :
- printDlgInfo.Flags |= PD_NOSELECTION;
- break;
- case PPrintInfo::SelectionOnlyOn :
- printDlgInfo.Flags |= PD_SELECTION;
- break;
- default :
- break;
- }
- printDlgInfo.nCopies = (WORD)printInfo.GetCopies();
- }
- PPrintDialog::~PPrintDialog()
- {
- if (_hWnd != P_DEAD_WINDOW)
- DestroyWindow(_hWnd);
- if (printDlgInfo.hDevMode != NULL)
- GlobalFree(printDlgInfo.hDevMode);
- if (printDlgInfo.hDevNames != NULL)
- GlobalFree(printDlgInfo.hDevNames);
- if (printDlgInfo.hDC != NULL)
- DeleteDC(printDlgInfo.hDC);
- }
- void PPrintDialog::SetTitle(const PString & title)
- {
- if (_hWnd != NULL)
- PModalDialog::SetTitle(title);
- else
- dlgTitle = title;
- }
- int PPrintDialog::RunModal()
- {
- if (PrintDlg(&printDlgInfo)) {
- runState = Ended;
- return TRUE;
- }
- DWORD err = CommDlgExtendedError();
- #if defined(_WIN32)
- SetLastError(err);
- #endif
- PAssertOS(err == 0);
- return FALSE;
- }
- void PPrintDialog::WndProc()
- {
- switch (_msg->event) {
- case WM_INITDIALOG :
- if (!dlgTitle.IsEmpty())
- SetWindowText(_hWnd, dlgTitle);
- InitStandardDialog();
- return;
- case WM_COMMAND :
- switch (LOWORD(_msg->wParam)) {
- case IDOK : {
- OnOk();
- return;
- }
- case IDCANCEL :
- OnCancel();
- return;
- }
- }
- PModalDialog::WndProc();
- }
- void PPrintDialog::OnOk()
- {
- printInfo.SetFromPrintDlg(printDlgInfo);
- DefWndProc();
- }
- void PPrintDialog::OnCancel()
- {
- DefWndProc();
- }
- void PPrintDialog::CreateHWND()
- {
- PAssertAlways("Illegal operation on Print Dialog");
- }
- ///////////////////////////////////////////////////////////////////////////////
- // PPrinterSetupDialog
- void PPrinterSetupDialog::Construct()
- {
- printDlgInfo.Flags |= PD_PRINTSETUP;
- if (pResourceID != PSTD_ID_DIALOG_PRINTER_SETUP) {
- printDlgInfo.Flags |= PD_ENABLEPRINTTEMPLATE;
- printDlgInfo.lpSetupTemplateName = MAKEINTRESOURCE(pResourceID);
- }
- }
- ///////////////////////////////////////////////////////////////////////////////
- // PPrintJobDialog
- void PPrintJobDialog::Construct()
- {
- if (pResourceID != PSTD_ID_DIALOG_PRINT_JOB) {
- printDlgInfo.Flags |= PD_ENABLEPRINTTEMPLATE;
- printDlgInfo.lpPrintTemplateName = MAKEINTRESOURCE(pResourceID);
- }
- }
- ///////////////////////////////////////////////////////////////////////////////
- // PFontDialog
- #if !defined(_WIN32)
- typedef UINT (CALLBACK * LPCFHOOKPROC)(HWND, UINT, WPARAM, LPARAM);
- #endif
- void PFontDialog::Construct(PRESOURCE_ID)
- {
- memset(&fontDlgInfo, 0, sizeof(fontDlgInfo));
- fontDlgInfo.lStructSize = sizeof(fontDlgInfo);
- fontDlgInfo.hInstance = owner->GetInstance();
- fontDlgInfo.hwndOwner = GetParent()->GetHWND();
- fontDlgInfo.lpLogFont = &logFont;
- fontDlgInfo.lpfnHook =
- (LPCFHOOKPROC)owner->GetWndProcPtr(PApplication::FontDlgProcType);
- fontDlgInfo.lCustData = (DWORD)this;
- fontDlgInfo.Flags = CF_ANSIONLY|CF_EFFECTS|CF_INITTOLOGFONTSTRUCT|CF_ENABLEHOOK;
- if (printerCanvas == NULL)
- fontDlgInfo.Flags |= CF_SCREENFONTS;
- else {
- fontDlgInfo.Flags |= CF_PRINTERFONTS;
- fontDlgInfo.hDC = printerCanvas->GetHDC();
- }
- if (pResourceID != PSTD_ID_DIALOG_FONT) {
- fontDlgInfo.Flags |= CF_ENABLETEMPLATE;
- fontDlgInfo.lpTemplateName = MAKEINTRESOURCE(pResourceID);
- }
- SetDefaultFont(selectedFont);
- }
- PFontDialog::~PFontDialog()
- {
- if (_hWnd != P_DEAD_WINDOW)
- DestroyWindow(_hWnd);
- }
- void PFontDialog::SetTitle(const PString & title)
- {
- if (_hWnd != NULL)
- PModalDialog::SetTitle(title);
- else
- dlgTitle = title;
- }
- void PFontDialog::SetDefaultFont(const PFont & font)
- {
- if (runState != Initialising)
- return;
- selectedFont = font;
- memset(&logFont, 0, sizeof(logFont));
- if (printerCanvas != NULL)
- logFont.lfHeight = printerCanvas->FromPointsY(font.GetSize());
- else {
- PDrawCanvas canvas(parent, TRUE);
- logFont.lfHeight = canvas.FromPointsY(font.GetSize());
- }
- logFont.lfWeight = font.IsBold() ? FW_BOLD : FW_NORMAL;
- logFont.lfItalic = (BYTE)font.IsItalic();
- logFont.lfUnderline = (BYTE)font.IsUnderlined();
- strncpy(logFont.lfFaceName, font.GetFacename(), LF_FACESIZE-1);
- }
- int PFontDialog::RunModal()
- {
- if (ChooseFont(&fontDlgInfo)) {
- runState = Ended;
- return TRUE;
- }
- DWORD err = CommDlgExtendedError();
- #if defined(_WIN32)
- SetLastError(err);
- #endif
- PAssertOS(err == 0);
- return FALSE;
- }
- void PFontDialog::WndProc()
- {
- switch (_msg->event) {
- case WM_INITDIALOG :
- if (!dlgTitle.IsEmpty())
- SetWindowText(_hWnd, dlgTitle);
- InitStandardDialog();
- return;
- case WM_COMMAND :
- switch (LOWORD(_msg->wParam)) {
- case IDOK : {
- SendMessage(_hWnd, WM_CHOOSEFONT_GETLOGFONT, 0, (DWORD)&logFont);
- PDIMENSION size = PABS(logFont.lfHeight);
- if (printerCanvas != NULL)
- size = printerCanvas->ToPointsY(size);
- else {
- PDrawCanvas canvas(parent, TRUE);
- size = canvas.ToPointsY(size);
- }
- WORD styles = PFont::Regular;
- if (logFont.lfWeight > FW_NORMAL)
- styles |= PFont::Bold;
- if (logFont.lfItalic)
- styles |= PFont::Italic;
- if (logFont.lfUnderline)
- styles |= PFont::Underline;
- selectedFont = PFont(logFont.lfFaceName, size, styles);
- OnOk();
- return;
- }
- case IDCANCEL :
- OnCancel();
- return;
- }
- }
- PModalDialog::WndProc();
- }
- void PFontDialog::OnOk()
- {
- DefWndProc();
- }
- void PFontDialog::OnCancel()
- {
- DefWndProc();
- }
- void PFontDialog::CreateHWND()
- {
- PAssertAlways("Illegal operation on Print Dialog");
- }
- ///////////////////////////////////////////////////////////////////////////////
- // PColourDialog
- #if !defined(_WIN32)
- typedef UINT (CALLBACK * LPCCHOOKPROC)(HWND, UINT, WPARAM, LPARAM);
- #endif
- void PColourDialog::Construct(PRESOURCE_ID)
- {
- PConfig cfg("CONTROL.INI", "Custom Colors");
- for (PINDEX i = 0; i < NumCustomColours; i++)
- customColours[i] = cfg.GetString(psprintf("Color%c", i+'A')).AsInteger(16);
- memset(&colourDlgInfo, 0, sizeof(colourDlgInfo));
- colourDlgInfo.lStructSize = sizeof(colourDlgInfo);
- colourDlgInfo.hInstance = (HWND)owner->GetInstance();
- colourDlgInfo.hwndOwner = GetParent()->GetHWND();
- colourDlgInfo.lpfnHook =
- (LPCCHOOKPROC)owner->GetWndProcPtr(PApplication::ColourDlgProcType);
- colourDlgInfo.rgbResult = colour.ToCOLORREF();
- colourDlgInfo.lpCustColors = customColours;
- colourDlgInfo.lCustData = (DWORD)this;
- colourDlgInfo.Flags = CC_RGBINIT|CC_ENABLEHOOK;
- if (pResourceID != PSTD_ID_DIALOG_COLOUR) {
- colourDlgInfo.Flags |= CC_ENABLETEMPLATE;
- colourDlgInfo.lpTemplateName = MAKEINTRESOURCE(pResourceID);
- }
- }
- PColourDialog::~PColourDialog()
- {
- if (_hWnd != P_DEAD_WINDOW)
- DestroyWindow(_hWnd);
- }
- void PColourDialog::SetTitle(const PString & title)
- {
- if (_hWnd != NULL)
- PModalDialog::SetTitle(title);
- else
- dlgTitle = title;
- }
- void PColourDialog::SetColour(const PColour & col)
- {
- if (runState == Initialising)
- colour = col;
- }
- int PColourDialog::RunModal()
- {
- if (ChooseColor(&colourDlgInfo)) {
- runState = Ended;
- colour.FromCOLORREF(colourDlgInfo.rgbResult);
- return TRUE;
- }
- DWORD err = CommDlgExtendedError();
- #if defined(_WIN32)
- SetLastError(err);
- #endif
- PAssertOS(err == 0);
- return FALSE;
- }
- void PColourDialog::WndProc()
- {
- switch (_msg->event) {
- case WM_INITDIALOG :
- if (!dlgTitle.IsEmpty())
- SetWindowText(_hWnd, dlgTitle);
- InitStandardDialog();
- return;
- case WM_COMMAND :
- switch (LOWORD(_msg->wParam)) {
- case IDOK : {
- OnOk();
- return;
- }
- case IDCANCEL :
- OnCancel();
- return;
- }
- }
- PModalDialog::WndProc();
- }
- void PColourDialog::OnOk()
- {
- DefWndProc();
- }
- void PColourDialog::OnCancel()
- {
- DefWndProc();
- }
- void PColourDialog::CreateHWND()
- {
- PAssertAlways("Illegal operation on Print Dialog");
- }
- ///////////////////////////////////////////////////////////////////////////////
- // PApplication
- BOOL PApplication::DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- if (msg == WM_INITDIALOG)
- ShowWindow(hWnd, SW_HIDE);
- PInteractor * window = (PInteractor *)createdWindows.GetAt((HWNDKey)hWnd);
- if (window == NULL)
- return FALSE;
- PInteractor::_WindowsMessage message;
- message.event = msg;
- message.wParam = wParam;
- message.lParam = lParam;
- message.lResult = 0;
- message.processed = TRUE;
- PInteractor::_WindowsMessage * oldMessage = window->_msg;
- window->_msg = &message;
- window->WndProc();
- window->_msg = oldMessage;
- if (message.processed) {
- switch (msg) {
- #ifdef WIN32
- case WM_CTLCOLORBTN :
- case WM_CTLCOLORDLG :
- case WM_CTLCOLOREDIT :
- case WM_CTLCOLORLISTBOX :
- case WM_CTLCOLORMSGBOX :
- case WM_CTLCOLORSCROLLBAR :
- case WM_CTLCOLORSTATIC :
- #else
- case WM_CTLCOLOR :
- #endif
- case WM_COMPAREITEM :
- case WM_VKEYTOITEM :
- case WM_CHARTOITEM :
- case WM_QUERYDRAGICON :
- return (UINT)message.lResult;
- }
- SetWindowLong(hWnd, DWL_MSGRESULT, message.lResult);
- }
- return message.processed;
- }
- BOOL PApplication::FileDlgProc(HWND hWnd,
- UINT msg, WPARAM wParam, LPARAM lParam)
- {
- if (msg == WM_INITDIALOG) {
- PInteractor * window = (PInteractor *)((LPOPENFILENAME)lParam)->lCustData;
- PAssert(window != NULL, PLogicError);
- AddWindowHandle(hWnd, window);
- window->_hWnd = hWnd;
- }
- return DlgProc(hWnd, msg, wParam, lParam);
- }
- BOOL PApplication::PrintDlgProc(HWND hWnd,
- UINT msg, WPARAM wParam, LPARAM lParam)
- {
- if (msg == WM_INITDIALOG) {
- PInteractor * window = (PInteractor *)((LPPRINTDLG)lParam)->lCustData;
- PAssert(window != NULL, PLogicError);
- AddWindowHandle(hWnd, window);
- window->_hWnd = hWnd;
- }
- return DlgProc(hWnd, msg, wParam, lParam);
- }
- BOOL PApplication::FontDlgProc(HWND hWnd,
- UINT msg, WPARAM wParam, LPARAM lParam)
- {
- if (msg == WM_INITDIALOG) {
- PInteractor * window = (PInteractor *)((LPCHOOSEFONT)lParam)->lCustData;
- PAssert(window != NULL, PLogicError);
- AddWindowHandle(hWnd, window);
- window->_hWnd = hWnd;
- }
- return DlgProc(hWnd, msg, wParam, lParam);
- }
- BOOL PApplication::ColourDlgProc(HWND hWnd,
- UINT msg, WPARAM wParam, LPARAM lParam)
- {
- if (msg == WM_INITDIALOG) {
- PInteractor * window = (PInteractor *)((LPCHOOSECOLOR)lParam)->lCustData;
- PAssert(window != NULL, PLogicError);
- AddWindowHandle(hWnd, window);
- window->_hWnd = hWnd;
- }
- return DlgProc(hWnd, msg, wParam, lParam);
- }
- // End Of File ///////////////////////////////////////////////////////////////