Fix UI restore when window is resized while in loading screen
[ryzomcore.git] / nelns / login_system / nel_launcher_windows_ext / WebDlg.cpp
blob2c6d3ae5d276517102ac55b2964bfd7e38960661
1 // WebDlg.cpp : implementation file
2 //
4 #include "stdafx.h"
5 #include "nel_launcher.h"
6 #include "nel_launcherDlg.h"
7 #include "WebDlg.h"
8 #include "LoginDlg.h"
10 #include <comdef.h>
11 #include <mshtml.h>
13 #ifdef _DEBUG
14 #define new DEBUG_NEW
15 #undef THIS_FILE
16 static char THIS_FILE[] = __FILE__;
17 #endif
19 /////////////////////////////////////////////////////////////////////////////
20 // CWebDlg dialog
23 CWebDlg::CWebDlg(CWnd* pParent /*=NULL*/)
24 : CDialog(CWebDlg::IDD, pParent)
26 //{{AFX_DATA_INIT(CWebDlg)
27 // NOTE: the ClassWizard will add member initialization here
28 //}}AFX_DATA_INIT
32 void CWebDlg::DoDataExchange(CDataExchange* pDX)
34 CDialog::DoDataExchange(pDX);
35 //{{AFX_DATA_MAP(CWebDlg)
36 DDX_Control(pDX, IDC_EXPLORER1, m_explore);
37 //}}AFX_DATA_MAP
41 BEGIN_MESSAGE_MAP(CWebDlg, CDialog)
42 //{{AFX_MSG_MAP(CWebDlg)
43 ON_WM_SIZE()
44 ON_WM_CTLCOLOR()
45 ON_WM_ERASEBKGND()
46 //}}AFX_MSG_MAP
47 END_MESSAGE_MAP()
49 /////////////////////////////////////////////////////////////////////////////
50 // CWebDlg message handlers
52 BEGIN_EVENTSINK_MAP(CWebDlg, CDialog)
53 //{{AFX_EVENTSINK_MAP(CWebDlg)
54 ON_EVENT(CWebDlg, IDC_EXPLORER1, 250 /* BeforeNavigate2 */, OnBeforeNavigate2, VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
55 ON_EVENT(CWebDlg, IDC_EXPLORER1, 259 /* DocumentComplete */, OnDocumentComplete, VTS_DISPATCH VTS_PVARIANT)
56 ON_EVENT(CWebDlg, IDC_EXPLORER1, 252 /* NavigateComplete2 */, OnNavigateComplete2Explorer1, VTS_DISPATCH VTS_PVARIANT)
57 //}}AFX_EVENTSINK_MAP
58 END_EVENTSINK_MAP()
60 void CWebDlg::OnBeforeNavigate2(LPDISPATCH pDisp, VARIANT FAR* URL, VARIANT FAR* Flags, VARIANT FAR* TargetFrameName, VARIANT FAR* PostData, VARIANT FAR* Headers, BOOL FAR* Cancel)
62 /* CRect r;
64 GetClientRect(&r);
65 ClientToScreen(&r);
66 m_dlgLoading.MoveWindow(r.left+BROWSER_X, r.top+BROWSER_Y, BROWSER_W, BROWSER_H);
67 m_dlgLoading.ShowWindow(SW_SHOW);*/
69 // set the user agent to nel_launcher changing the header
70 CString csHeader(Headers->bstrVal);
72 if(csHeader.IsEmpty())
74 IWebBrowser2 *pBrowser;
75 LPDISPATCH pWebDisp;
77 pDisp->QueryInterface(IID_IWebBrowser2, (void**) &pBrowser);
78 pBrowser->get_Container(&pWebDisp);
80 BSTR bstr = SysAllocString(L"User-Agent: nel_launcher\r\n");
81 Headers->bstrVal = bstr;
83 pBrowser->Navigate2(URL, Flags, TargetFrameName, PostData, Headers);
85 if (!pWebDisp)
86 (*Cancel) = true;
88 if (pWebDisp)
89 pWebDisp->Release();
90 if (pBrowser)
91 pBrowser->Release();
93 SysFreeString(bstr);
94 return;
98 VARIANT CWebDlg::ExecuteScript(CString csCode, CString csLanguage)
100 COleVariant varRet;
102 IHTMLDocument2* pHTMLDocument2;
103 IHTMLWindow2* pHTMLWindow2;
104 LPDISPATCH lpDispatch;
105 lpDispatch = m_explore.GetDocument();
107 BSTR bstrCode = _bstr_t((const char *)csCode);
108 BSTR bstrLang = _bstr_t((const char *)csLanguage);
109 HRESULT hr;
111 if(lpDispatch)
113 hr = lpDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&pHTMLDocument2);
114 if(hr == S_OK)
116 hr = pHTMLDocument2->get_parentWindow(&pHTMLWindow2);
117 if(hr == S_OK)
119 pHTMLWindow2->execScript(bstrCode, bstrLang, &varRet);
120 pHTMLWindow2->Release();
122 pHTMLDocument2->Release();
124 lpDispatch->Release();
126 return varRet;
129 VARIANT CWebDlg::ExecuteJavascript(CString csCode)
131 return ExecuteScript(csCode, "Javascript");
134 void CWebDlg::OpenUrl(CString csUrl)
136 m_csUrl = csUrl;
137 m_explore.Navigate(csUrl, NULL, NULL, NULL, NULL);
140 void CWebDlg::OpenUrl(const std::string &url)
142 m_csUrl = url.c_str();
143 m_explore.Navigate(url.c_str(), NULL, NULL, NULL, NULL);
146 void CWebDlg::OnDocumentComplete(LPDISPATCH pDisp, VARIANT FAR* URL)
148 IHTMLDocument2* pHTMLDocument2;
149 LPDISPATCH lpDispatch;
150 lpDispatch = m_explore.GetDocument();
152 if (lpDispatch)
154 HRESULT hr;
155 hr = lpDispatch->QueryInterface(IID_IHTMLDocument2, (LPVOID*) &pHTMLDocument2);
156 lpDispatch->Release();
158 if (FAILED(hr))
159 return;
161 if (pHTMLDocument2 == NULL)
162 return;
164 IHTMLElement* pBody;
165 hr = pHTMLDocument2->get_body(&pBody);
167 if (FAILED(hr))
168 return;
170 if (pBody == NULL)
171 return;
173 BSTR bstr;
174 pBody->get_innerHTML(&bstr);
176 CString csSourceCode(bstr);
177 CString csAction;
178 CString csComment;
180 SysFreeString(bstr);
181 pBody->Release();
183 if(csSourceCode.Find("newlogin") != -1)
185 // Authentication not ok !
186 APP.m_bAuthGame = FALSE;
187 APP.m_bAuthWeb = FALSE;
188 ((CNel_launcherDlg*)AfxGetMainWnd())->m_wndTabs.DestroyWindow();
189 DestroyWindow();
190 ((CNel_launcherDlg*)AfxGetMainWnd())->Login();
191 return;
194 // ACE: BADDDDDD!!!! the good web page can contains 404 in the cookie because the cookie contains any number we want
195 /* else if(csSourceCode.Find("404") != -1)
197 // Authentication lost, try to redo the authentication
198 CLoginDlg dlg;
200 dlg.AuthWeb();
201 OpenUrl(m_csUrl);
202 return;
204 */ int iFindAction1 = csSourceCode.Find("<!--nel");
205 int iFindAction2;
206 int iFindComment1;
207 int iFindComment2;
209 if(iFindAction1 != -1)
211 iFindAction1 = csSourceCode.Find('"', iFindAction1);
212 if(iFindAction1 != -1)
214 iFindAction2 = csSourceCode.Find('"', iFindAction1+1);
215 if(iFindAction2 != -1)
217 csAction = csSourceCode.Mid(iFindAction1+1, iFindAction2 - iFindAction1-1);
219 iFindComment1 = iFindAction2 + 2;
220 if(iFindComment1 != -1)
222 iFindComment2 = csSourceCode.Find("-->", iFindAction1);
223 if(iFindComment2 != -1)
225 csComment = csSourceCode.Mid(iFindComment1, iFindComment2 - iFindComment1);
226 csComment.TrimRight();
227 csComment.TrimLeft();
229 if(csAction == "launch")
230 ((CNel_launcherDlg*)GetParent())->launch(csComment);
231 else if(csAction == "patch")
232 ((CNel_launcherDlg*)GetParent())->patch(csComment);
239 // m_dlgLoading.ShowWindow(SW_HIDE);
242 BOOL CWebDlg::OnInitDialog()
244 CDialog::OnInitDialog();
246 CRect r;
248 m_brushBG.CreateSolidBrush(RGB(0, 16, 0));
250 GetClientRect(&r);
251 GetDlgItem(IDC_EXPLORER1)->MoveWindow(-2, -2, r.Width()-48, r.Height());
253 // m_dlgLoading.Create(IDD_LOADING_PAGE, this);
254 // m_dlgLoading.ShowWindow(SW_HIDE);
256 // ClientToScreen(&r);
257 // m_dlgLoading.MoveWindow(r.left+BROWSER_X, r.top+BROWSER_Y, BROWSER_W, BROWSER_H);
259 return TRUE; // return TRUE unless you set the focus to a control
260 // EXCEPTION: OCX Property Pages should return FALSE
263 void CWebDlg::OnSize(UINT nType, int cx, int cy)
265 CDialog::OnSize(nType, cx, cy);
268 HBRUSH CWebDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
270 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
272 if(nCtlColor == CTLCOLOR_DLG)
274 hbr = m_brushBG;
275 pDC->SetBkColor(RGB(0, 16, 0));
276 pDC->SetBkMode(OPAQUE);
278 return hbr;
281 void CWebDlg::OnNavigateComplete2Explorer1(LPDISPATCH pDisp, VARIANT FAR* URL)
285 BOOL CWebDlg::OnEraseBkgnd(CDC* pDC)
287 CBrush backBrush(RGB(0, 16, 0));
288 CBrush* pOldBrush = pDC->SelectObject(&backBrush);
289 CRect r;
291 pDC->GetClipBox(&r); // Erase the area needed.
292 pDC->PatBlt(r.left, r.top, r.Width(), r.Height(), PATCOPY);
293 pDC->SelectObject(pOldBrush);
294 return TRUE;