soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SHostDialog.cpp
1#include <souistd.h>
2#include <core/SHostDialog.h>
3#include <core/SMsgLoop.h>
4
5SNSBEGIN
6
7#define RC_INIT 0xcccccccc
8
9SHostDialog::SHostDialog(LPCWSTR pszXmlName)
10 : THostWndProxy<IHostDialog>(pszXmlName)
11 , m_nRetCode(RC_INIT)
12{
13}
14
15SHostDialog::SHostDialog(LPCSTR pszXmlName)
16 : THostWndProxy<IHostDialog>(pszXmlName)
17 , m_nRetCode(RC_INIT)
18{
19}
20
22{
23 m_MsgLoop = NULL;
24}
25
26INT_PTR SHostDialog::DoModal(HWND hParent, DWORD dwStyle, DWORD dwExStyle)
27{
28 SASSERT(!m_MsgLoop);
31
32 if (!hParent)
33 {
34 hParent = ::GetActiveWindow();
35 }
36
37 BOOL bEnableParent = FALSE;
38 if (hParent && hParent != ::GetDesktopWindow() && ::IsWindowEnabled(hParent))
39 {
40 ::EnableWindow(hParent, FALSE);
41 bEnableParent = TRUE;
42 }
43
44 if (!SHostWnd::CreateEx(hParent, dwStyle, dwExStyle, 0, 0, 0, 0))
45 {
46 if (bEnableParent)
47 {
48 ::EnableWindow(hParent, TRUE);
49 }
50 return 0;
51 }
52 SNativeWnd::SendMessage(WM_INITDIALOG, (WPARAM)m_hWnd);
53
54 if (m_nRetCode == RC_INIT)
55 {
56 HWND hWndLastActive = ::SetActiveWindow(m_hWnd);
57
58 if (GetExStyle() & WS_EX_TOOLWINDOW)
59 ::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
60 else
61 ::ShowWindow(m_hWnd, SW_SHOWNORMAL);
62
63 int nRet = m_MsgLoop->Run();
64
65 // From MFC
66 // hide the window before enabling the parent, etc.
67 if (IsWindow())
68 {
69 ShowHostWnd(SW_HIDE, TRUE);
70 }
71
72 if (m_nRetCode == RC_INIT)
73 { //可能是程序中主动退出app,而不是自己EndDialog关闭窗口,重新把WM_QUIT放回消息队列。
74 parentMsgLoop->Quit(nRet);
75 }
76
77 if (bEnableParent)
78 {
79 ::EnableWindow(hParent, TRUE);
80 }
81
82 ::SetActiveWindow(hWndLastActive);
83 }
84
85 if (IsWindow())
87 m_MsgLoop = NULL;
88 return m_nRetCode;
89}
90
91void SHostDialog::EndDialog(INT_PTR nResult)
92{
93 if (!m_MsgLoop)
94 {
95 SSLOGW() << "dialog is not show by DoModal";
96 return;
97 }
98 SASSERT(nResult != RC_INIT);
99 if (m_nRetCode == RC_INIT)
100 {
101 m_nRetCode = nResult;
102 m_MsgLoop->Quit((int)m_nRetCode);
103 }
104}
105
107{
108 if (m_MsgLoop)
109 return m_MsgLoop;
110 else
111 return SHostWnd::GetMsgLoop();
112}
113
115{
116 EndDialog(IDOK);
117}
118
120{
121 EndDialog(IDCANCEL);
122}
123
124void SHostDialog::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
125{
126 SHostWnd::OnKeyEvent(WM_KEYDOWN, (WPARAM)nChar, MAKELPARAM(nRepCnt, nFlags));
127 if (SHostWnd::IsMsgHandled())
128 return;
129 if (nChar == VK_ESCAPE || nChar == VK_RETURN)
130 {
131 SWindow *pBtnExit = FindChildByID(nChar == VK_ESCAPE ? IDCANCEL : IDOK);
132 if (pBtnExit)
133 {
134 pBtnExit->FireCommand();
135 }
136 }
137}
138
139SNSEND
SOUI Dialog Module.
IMessageLoop * GetMsgLoop(tid_t tid=::GetCurrentThreadId()) SCONST OVERRIDE
Get the message loop for a specific thread.
Definition SApp.cpp:718
IMsgLoopFactory * GetMsgLoopFactory() OVERRIDE
Get the message loop factory.
Definition SApp.cpp:598
Smart pointer class for managing COM-style reference-counted objects.
void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
Handles the key down event.
SAutoRefPtr< IMessageLoop > m_MsgLoop
INT_PTR m_nRetCode
INT_PTR DoModal(HWND hParent=0, DWORD dwStyle=WS_POPUP|WS_CLIPCHILDREN, DWORD dwExStyle=0) OVERRIDE
Displays the dialog as a modal window.
void OnCancel()
Handles the Cancel button click event.
void EndDialog(INT_PTR nResult) OVERRIDE
Ends the dialog.
IMessageLoop * GetMsgLoop() OVERRIDE
Gets the message loop for the dialog.
~SHostDialog(void)
Destructor.
SHostDialog(LPCWSTR pszXmlName=NULL)
Constructor.
void OnOK()
Handles the OK button click event.
LRESULT OnKeyEvent(UINT uMsg, WPARAM wParam, LPARAM lParam)
Handles key events.
SWindow * FindChildByID(int nID, int nDeep=-1)
Finds a child window by its ID.
Definition SHostWnd.h:648
IMessageLoop * GetMsgLoop() OVERRIDE
Gets the message loop interface.
HWND CreateEx(HWND hWndParent, DWORD dwStyle, DWORD dwExStyle, int x, int y, int nWidth, int nHeight, IXmlNode *xmlInit=NULL) OVERRIDE
Creates the host window with extended styles.
Definition shostwnd.cpp:361
LRESULT SendMessage(UINT message, WPARAM wParam=0, LPARAM lParam=0) OVERRIDE
Sends a message to the window.
HWND m_hWnd
Handle to the window.
Definition SNativeWnd.h:744
BOOL DestroyWindow() OVERRIDE
Destroys the window.
static SApplication & getSingleton(void)
Definition SSingleton.h:63
static SApplication * getSingletonPtr(void)
Definition SSingleton.h:73
Base class for SOUI DUI windows.
Definition SWnd.h:286
BOOL FireCommand() OVERRIDE
Fires a command event.
Definition Swnd.cpp:2713
Interface for message loops.
Definition SMsgLoop-i.h:42
HRESULT CreateMsgLoop(IMessageLoop **ppMsgLoop, IMessageLoop *pParentLoop=NULL) PURE
Creates a message loop.