soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SMessageBox.cpp
1#include "souistd.h"
2#include "SApp.h"
3#include "control/SMessageBox.h"
4#include "control/SCmnCtrl.h"
5#include <control/STabCtrl.h>
6
7SNSBEGIN
8//////////////////////////////////////////////////////////////////////////
9
14
15static struct MsgBoxInfo
16{
17 LPCTSTR pszText;
18 LPCTSTR pszCaption;
19 UINT uType;
20} s_MsgBoxInfo;
21
22INT_PTR SMessageBoxImpl::MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
23{
25 if (!xmlTemplate)
26 return ::MessageBox(hWnd, lpText, lpCaption, uType);
27 s_MsgBoxInfo.pszText = lpText;
28 s_MsgBoxInfo.pszCaption = lpCaption;
29 s_MsgBoxInfo.uType = uType;
30 return DoModal(hWnd);
31}
32
34{
35 SImageWnd *pIcon = FindChildByName2<SImageWnd>(NAME_MSGBOX_ICON);
36 if (!pIcon)
37 return FALSE;
38 switch (uType & 0xF0)
39 {
40 case MB_USERICON: // done
41 pIcon->SetIcon(0);
42 break;
43 case MB_ICONWARNING:
44 pIcon->SetIcon(1);
45 break;
46 case MB_ICONINFORMATION:
47 pIcon->SetIcon(2);
48 break;
49 case MB_ICONSTOP:
50 pIcon->SetIcon(3);
51 break;
52 case MB_ICONQUESTION:
53 pIcon->SetIcon(4);
54 break;
55 default:
56 pIcon->SetVisible(FALSE, TRUE);
57 break;
58 }
59 return TRUE;
60}
61
62struct MSGBTN_TEXT
63{
64 int nBtns; //按钮数,<=3
65 struct
66 {
67 UINT uBtnID; //按钮ID
68 WCHAR szText[20]; //按钮字符
69 } btnInfo[3];
70} g_msgBtnText[] = {
71 // MB_OK
72 { 1, { { IDOK, L"ok" }, { 0, L"" }, { 0, L"" } } },
73 // MB_OKCANCEL
74 { 2, { { IDOK, L"ok" }, { IDCANCEL, L"cancel" }, { 0, L"" } } },
75 // MB_ABORTRETRYIGNORE
76 { 3, { { IDABORT, L"abort" }, { IDRETRY, L"retry" }, { IDIGNORE, L"ignore" } } },
77 // MB_YESNOCANCEL
78 { 3, { { IDYES, L"yes" }, { IDNO, L"no" }, { IDCANCEL, L"cancel" } } },
79 // MB_YESNO
80 { 2, { { IDYES, L"yes" }, { IDNO, L"no" }, { 0, L"" } } },
81 // MB_RETRYCANCEL
82 { 2, { { IDRETRY, L"retry" }, { IDCANCEL, L"cancel" }, { 0, L"" } } }
83};
84
85const WCHAR *g_wcsNameOfBtns[] = { NAME_MSGBOX_BTN1, NAME_MSGBOX_BTN2, NAME_MSGBOX_BTN3 };
86
87SStringT SMessageBoxImpl::OnGetButtonText(int nBtnID) const
88{
89 return SStringT();
90}
91
96
97BOOL SMessageBoxImpl::OnInitDialog(HWND wnd, LPARAM lInitParam)
98{
100 UINT uType = s_MsgBoxInfo.uType & 0x0F;
101
102 STabCtrl *pBtnSwitch = FindChildByName2<STabCtrl>(NAME_MSGBOX_BTNSWITCH);
103 SASSERT(pBtnSwitch);
104 pBtnSwitch->SetCurSel(g_msgBtnText[uType].nBtns - 1);
105 SWindow *pBtnPanel = pBtnSwitch->GetItem(g_msgBtnText[uType].nBtns - 1);
106 SASSERT(pBtnPanel);
107
108 SXmlNode nodeBtnTxt = uiRoot.child(L"buttonText");
109 for (int i = 0; i < g_msgBtnText[uType].nBtns; i++)
110 {
111 SWindow *pBtn = pBtnPanel->FindChildByName(g_wcsNameOfBtns[i]);
112 int nID = g_msgBtnText[uType].btnInfo[i].uBtnID;
113 pBtn->SetID(nID);
114
115 SStringT strText = OnGetButtonText(nID);
116 SStringW strAccel;
117 if (strText.IsEmpty())
118 {
119 SStringW strBtnText = g_msgBtnText[uType].btnInfo[i].szText;
120 //先从模板中的buttonText节点里查按钮的文字
121 SXmlNode nodeTxt = nodeBtnTxt.child(strBtnText);
122 if (nodeTxt)
123 {
124 strBtnText = nodeTxt.Text();
125 strBtnText.TrimBlank();
126 strBtnText = GETSTRING(strBtnText);
127 strAccel = nodeTxt.attribute(L"accel").as_string();
128 }
129 //从翻译引擎中翻译
130 strText = S_CW2T(GetRoot()->tr(strBtnText));
131 }
132 if (!strAccel.IsEmpty())
133 {
134 pBtn->SetAttribute(L"accel", strAccel, TRUE);
135 }
136 pBtn->SetWindowText(strText);
137 }
138
139 SStringW strMinSize = uiRoot.attribute(L"minSize").value();
140 SStringWList lstMinSize;
141 SplitString(strMinSize, L',', lstMinSize);
142 SASSERT(lstMinSize.GetCount() == 2);
143 SLayoutSize szMin[2] = { SLayoutSize::fromString(lstMinSize[0]), SLayoutSize::fromString(lstMinSize[1]) };
144
145 SWindow *pTitle = FindChildByName(NAME_MSGBOX_TITLE);
146 SASSERT(pTitle);
147 SStringT strTitle = pTitle->GetWindowText();
148 if (s_MsgBoxInfo.pszCaption)
149 strTitle = s_MsgBoxInfo.pszCaption;
150 pTitle->SetWindowText(S_CW2T(TR(S_CT2W(strTitle), GetTranslatorContext())));
151
152 SWindow *pMsg = FindChildByName(NAME_MSGBOX_TEXT);
153 SASSERT(pMsg);
154 pMsg->SetWindowText(S_CW2T(TR(S_CT2W(s_MsgBoxInfo.pszText), GetTranslatorContext())));
155
156 OnSetIcon(s_MsgBoxInfo.uType);
158
159 CRect rcWnd = SHostWnd::GetWindowRect();
160 CSize szWnd = rcWnd.Size();
161 if (szWnd.cx < szMin[0].toPixelSize(GetScale()))
162 szWnd.cx = szMin[0].toPixelSize(GetScale());
163 if (szWnd.cy < szMin[1].toPixelSize(GetScale()))
164 szWnd.cy = szMin[1].toPixelSize(GetScale());
165
166 SetWindowPos(0, 0, 0, szWnd.cx, szWnd.cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
167
168 CenterWindow();
169 SetForegroundWindow(m_hWnd);
170 SetMsgHandled(FALSE); // chain to dpihandler.
171 return 0;
172}
173
174//////////////////////////////////////////////////////////////////////////
175INT_PTR SMessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
176{
177 SMessageBoxImpl msgBox;
178 return msgBox.MessageBox(hWnd, lpText, lpCaption, uType);
179}
180
181SNSEND
通用控件
Definition of the STabCtrl and related classes.
SXmlNode GetMessageBoxTemplate() const
Get the message box template XML node.
Definition SApp.cpp:818
INT_PTR DoModal(HWND hParent=0, DWORD dwStyle=WS_POPUP|WS_CLIPCHILDREN, DWORD dwExStyle=0) OVERRIDE
Displays the dialog as a modal window.
SHostDialog(LPCWSTR pszXmlName=NULL)
Constructor.
SWindow * FindChildByName(LPCWSTR strName, int nDeep=-1)
Finds a child window by its name (Unicode version).
Definition SHostWnd.h:596
T * FindChildByName2(LPCWSTR pszName, int nDeep=-1)
Finds a child window by its name (Unicode version) with template support.
Definition SHostWnd.h:622
CRect GetWindowRect() const
Gets the window rectangle.
SWindow * GetRoot() const
Gets the root window.
Definition SHostWnd.h:685
LPCWSTR GetTranslatorContext() const OVERRIDE
Gets the translator context for the container.
int GetScale() const OVERRIDE
Gets the scale factor for the container.
图片控件类
Definition SCmnCtrl.h:391
BOOL SetIcon(int nSubID) OVERRIDE
设置图标
Definition SCmnCtrl.cpp:685
布局大小类
Definition SLayoutSize.h:10
int toPixelSize(int scale) const
将大小转换为像素值
static SLayoutSize fromString(const SStringW &strSize)
从字符串创建大小对象
Message Box Implementation.
Definition SMessageBox.h:26
virtual SStringT OnGetButtonText(int nBtnID) const
Get the button text.
SXmlNode OnGetInitXmlNode(SXmlDoc &xmlDoc) override
Get the initialization XML node.
SMessageBoxImpl()
Constructor.
virtual BOOL OnSetIcon(UINT uType)
Set the icon.
INT_PTR MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
Display a message box.
BOOL OnInitDialog(HWND wnd, LPARAM lInitParam)
Initialize the dialog.
HWND m_hWnd
Handle to the window.
Definition SNativeWnd.h:744
void SetID(int nID) OVERRIDE
Sets the object's ID.
Definition Sobject.hpp:142
static SApplication * getSingletonPtr(void)
Definition SSingleton.h:73
A class representing an ASCII string.
Definition sstringw.h:96
void TrimBlank()
Trims leading and trailing whitespace characters from the string.
Definition sstringw.cpp:762
BOOL IsEmpty() SCONST
Checks if the string is empty.
A tab control for managing multiple tab pages.
Definition STabCtrl.h:117
STabPage * GetItem(int iPage)
Gets a tab page by index.
Definition STabCtrl.cpp:506
BOOL SetCurSel(int nIndex) OVERRIDE
Sets the currently selected tab page.
Definition STabCtrl.cpp:560
Base class for SOUI DUI windows.
Definition SWnd.h:286
void SetVisible(BOOL bVisible, BOOL bUpdate=FALSE) OVERRIDE
Sets the visibility of the window.
Definition Swnd.cpp:655
int GetWindowText(TCHAR *pBuf, int nBufLen, BOOL bRawText) OVERRIDE
Retrieves the window text.
Definition Swnd.cpp:263
SWindow * FindChildByName(LPCWSTR strName, int nDeep=-1)
Finds a child window by its name.
Definition Swnd.cpp:795
void UpdateLayout() OVERRIDE
Updates the layout of the window.
Definition Swnd.cpp:2251
void SetWindowText(LPCTSTR lpszText) OVERRIDE
Sets the window text.
Definition Swnd.cpp:311
const wchar_t * as_string(const wchar_t *def=L"") const
Gets the attribute value as a string.
Definition SXml.cpp:95
const wchar_t * value() const
Gets the attribute value.
Definition SXml.cpp:90
Implementation of IXmlDoc.
Definition SXml.h:912
Class representing an XML node.
Definition SXml.h:352
SXmlAttr attribute(const wchar_t *name, bool bCaseSensitive=false) const
Gets the attribute with the specified name.
Definition SXml.cpp:428
SXmlNode child(const wchar_t *name, bool bCaseSensitive=false) const
Gets the child node, attribute, or next/previous sibling with the specified name.
Definition SXml.cpp:423
const wchar_t * Text() SCONST OVERRIDE
Gets the node text.
Definition SXml.cpp:265