Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

389 lines
11 KiB

  1. // TipDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include "title.h"
  6. #include "Tip.h"
  7. #include "TipDlg.h"
  8. #include "ServCntr.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. #define REGKEY_STP _T("SOFTWARE\\Microsoft\\INetStp")
  15. #define REGKEY_INSTALLKEY _T("InstallPath")
  16. #define SZ_PWS_SECTION _T("Header")
  17. #define SZ_NUM_TIPS _T("TotalTips")
  18. #define SZ_TIP_URL _T("TipFile")
  19. #define MAX_TIP_LENGTH 1000
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CTipDlg dialog
  22. //----------------------------------------------------------------
  23. CTipDlg::CTipDlg(CWnd* pParent /*=NULL*/)
  24. : CDialog(CTipDlg::IDD, pParent)
  25. {
  26. //{{AFX_DATA_INIT(CTipDlg)
  27. m_bool_showtips = FALSE;
  28. //}}AFX_DATA_INIT
  29. // m_ctitle_title.m_fTipText = TRUE;
  30. // get the inetsrv directory path and put it in szFileStarter
  31. CW3ServerControl::GetServerDirectory( szFileStarter );
  32. }
  33. //----------------------------------------------------------------
  34. void CTipDlg::DoDataExchange(CDataExchange* pDX)
  35. {
  36. CDialog::DoDataExchange(pDX);
  37. //{{AFX_DATA_MAP(CTipDlg)
  38. DDX_Control(pDX, IDC_BACK, m_cbtn_back);
  39. DDX_Control(pDX, IDC_NEXT, m_cbtn_next);
  40. DDX_Check(pDX, IDC_SHOWTIPS, m_bool_showtips);
  41. DDX_Control(pDX, IDC_EXPLORER, m_ie);
  42. //}}AFX_DATA_MAP
  43. }
  44. //----------------------------------------------------------------
  45. BEGIN_MESSAGE_MAP(CTipDlg, CDialog)
  46. //{{AFX_MSG_MAP(CTipDlg)
  47. ON_BN_CLICKED(IDC_BACK, OnBack)
  48. ON_BN_CLICKED(IDC_NEXT, OnNext)
  49. ON_WM_CLOSE()
  50. //}}AFX_MSG_MAP
  51. END_MESSAGE_MAP()
  52. //----------------------------------------------------------------
  53. BOOL CTipDlg::OnInitDialog()
  54. {
  55. // initialize the show tips flag
  56. m_bool_showtips = FShowAtStartup();
  57. // call the parental oninitdialog
  58. BOOL f = CDialog::OnInitDialog();
  59. // what is the name of the tips file?
  60. CString szTipFile;
  61. GetTipPath( szTipFile );
  62. // get the number of tip strings
  63. nNumTips = GetPrivateProfileInt( SZ_PWS_SECTION, SZ_NUM_TIPS, 0, szTipFile );
  64. // if there are no tips - or the file couldn't be found - do nothing
  65. if ( nNumTips > 0)
  66. {
  67. // Base the shown tip on the day. Cycle through the tips, one per day.
  68. SYSTEMTIME time;
  69. GetLocalTime( &time );
  70. UINT seed;
  71. seed = (time.wYear << 16) | (time.wMonth << 8) | (time.wDay);
  72. iCurTip = (seed % nNumTips) + 1;
  73. // record that starting tip
  74. m_iStartTip = iCurTip;
  75. // load the first tip
  76. LoadTip( iCurTip );
  77. }
  78. else
  79. EndDialog(0);
  80. // return the answer
  81. return f;
  82. }
  83. //----------------------------------------------------------------
  84. void CTipDlg::LoadTip( int iTip )
  85. {
  86. LPTSTR psz;
  87. DWORD cbSz = MAX_TIP_LENGTH;
  88. // prepare the tips file name
  89. CString szTipFile;
  90. GetTipPath( szTipFile );
  91. // prepare the section name
  92. CString szSection;
  93. szSection.Format( _T("%d"), iTip );
  94. // lock down the tip buffer
  95. CString szTip;
  96. psz = szTip.GetBuffer( cbSz );
  97. // get the tip
  98. cbSz = GetPrivateProfileString( szSection, SZ_TIP_URL, _T(""), psz, cbSz, szTipFile );
  99. // release the tip buffer
  100. szTip.ReleaseBuffer( cbSz );
  101. // finish munging together the tip file path
  102. CString szTipeFile = _T("file://");
  103. szTipeFile += szFileStarter + szTip;
  104. // go to the tip
  105. CWaitCursor wait;
  106. m_ie.Navigate( szTipeFile, NULL, NULL, NULL, NULL );
  107. // enable or disable the next/back buttons as appropriate
  108. // first check the next tip
  109. int iFutureTip;
  110. // figure out what the next tip would be
  111. iFutureTip = iTip + 1;
  112. if ( iFutureTip > nNumTips )
  113. iFutureTip = 1;
  114. // if it is the original tip, disable the next button
  115. m_cbtn_next.EnableWindow( (iFutureTip != m_iStartTip) );
  116. // only disable back if we are on the first tip
  117. m_cbtn_back.EnableWindow( (iTip != m_iStartTip) );
  118. }
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CTipDlg message handlers
  121. //----------------------------------------------------------------
  122. void CTipDlg::OnBack()
  123. {
  124. // check for roll-over
  125. if ( iCurTip <= 1 )
  126. iCurTip = nNumTips;
  127. else
  128. iCurTip--;
  129. // load the tip
  130. LoadTip( iCurTip );
  131. }
  132. //----------------------------------------------------------------
  133. void CTipDlg::OnNext()
  134. {
  135. // increment the tip index
  136. iCurTip++;
  137. // check for roll-over
  138. if ( iCurTip > nNumTips )
  139. iCurTip = 1;
  140. // load the tip
  141. LoadTip( iCurTip );
  142. }
  143. //----------------------------------------------------------------
  144. void CTipDlg::GetTipPath( CString &sz )
  145. {
  146. HKEY hKey;
  147. TCHAR chPath[MAX_PATH+1];
  148. DWORD cbPath;
  149. DWORD err, type;
  150. // get the server install path from the registry
  151. // open the registry key, if it exists
  152. err = RegOpenKeyEx(
  153. HKEY_LOCAL_MACHINE, // handle of open key
  154. REGKEY_STP, // address of name of subkey to open
  155. 0, // reserved
  156. KEY_READ, // security access mask
  157. &hKey // address of handle of open key
  158. );
  159. // if we did not open the key for any reason (say... it doesn't exist)
  160. // then leave right away
  161. if ( err != ERROR_SUCCESS )
  162. return;
  163. cbPath = sizeof(chPath);
  164. type = REG_SZ;
  165. err = RegQueryValueEx(
  166. hKey, // handle of key to query
  167. REGKEY_INSTALLKEY, // address of name of value to query
  168. NULL, // reserved
  169. &type, // address of buffer for value type
  170. (PUCHAR)chPath, // address of data buffer
  171. &cbPath // address of data buffer size
  172. );
  173. // close the key
  174. RegCloseKey( hKey );
  175. // if we did get the key for any reason (say... it doesn't exist)
  176. // then leave right away
  177. if ( err != ERROR_SUCCESS )
  178. return;
  179. // the tips file is in the inetsrv directory
  180. CString szFile;
  181. szFile.LoadString( IDS_TIPS_FILE );
  182. // put it all together
  183. sz = chPath;
  184. sz += _T('\\');
  185. sz += szFile;
  186. }
  187. //------------------------------------------------------------------------
  188. void CTipDlg::SaveShowTips()
  189. {
  190. // save the value in the registry
  191. DWORD err;
  192. HKEY hKey;
  193. UpdateData( TRUE );
  194. BOOL fShowTips = TRUE;
  195. DWORD type = REG_DWORD;
  196. DWORD data = m_bool_showtips;
  197. DWORD cbData = sizeof(data);
  198. DWORD dwDisposition;
  199. // open the registry key, if it exists
  200. err = RegOpenKeyEx(
  201. HKEY_CURRENT_USER, // handle of open key
  202. SZ_REG_PWS_PREFS, // address of name of subkey to open
  203. 0, // reserved
  204. KEY_WRITE, // security access mask
  205. &hKey // address of handle of open key
  206. );
  207. // if we did not open the key, try creating a new one
  208. if ( err != ERROR_SUCCESS )
  209. {
  210. // try to make a new key
  211. err = RegCreateKeyEx(
  212. HKEY_CURRENT_USER,
  213. SZ_REG_PWS_PREFS,
  214. NULL,
  215. _T(""),
  216. REG_OPTION_NON_VOLATILE,
  217. KEY_ALL_ACCESS,
  218. NULL,
  219. &hKey,
  220. &dwDisposition
  221. );
  222. // if we still didn't get the key - fail
  223. if ( err != ERROR_SUCCESS )
  224. return;
  225. }
  226. // save the value in the registry
  227. RegSetValueEx( hKey, SZ_REG_PWS_SHOWTIPS, NULL, type, (PUCHAR)&data, cbData );
  228. // all done, close the key before leaving
  229. RegCloseKey( hKey );
  230. }
  231. //------------------------------------------------------------------------
  232. void CTipDlg::OnOK()
  233. {
  234. SaveShowTips();
  235. // call the default
  236. CDialog::OnOK();
  237. }
  238. //------------------------------------------------------------------------
  239. void CTipDlg::OnClose()
  240. {
  241. SaveShowTips();
  242. // call the default
  243. CDialog::OnClose();
  244. }
  245. //----------------------------------------------------------------
  246. // returns a flag indicating if the tips should be shown at startup
  247. BOOL CTipDlg::FShowAtStartup()
  248. {
  249. BOOL fShowTips = TRUE;
  250. DWORD err;
  251. HKEY hKey;
  252. DWORD type = REG_DWORD;
  253. DWORD data;
  254. DWORD cbData = sizeof(data);
  255. // open the registry key, if it exists
  256. err = RegOpenKeyEx(
  257. HKEY_CURRENT_USER, // handle of open key
  258. SZ_REG_PWS_PREFS, // address of name of subkey to open
  259. 0, // reserved
  260. KEY_READ, // security access mask
  261. &hKey // address of handle of open key
  262. );
  263. // if we did not open the key for any reason (say... it doesn't exist)
  264. // then leave right away
  265. if ( err != ERROR_SUCCESS )
  266. return fShowTips;
  267. // query the value of the registry
  268. err = RegQueryValueEx( hKey, SZ_REG_PWS_SHOWTIPS, NULL, &type, (PUCHAR)&data, &cbData );
  269. if ( err == ERROR_SUCCESS )
  270. fShowTips = data;
  271. // all done, close the key before leaving
  272. RegCloseKey( hKey );
  273. return fShowTips;
  274. }
  275. //=============================================================================
  276. #define COLOR_WHITE RGB(0xFF, 0xFF, 0xFF)
  277. #define COLOR_BLACK RGB(0, 0, 0)
  278. /////////////////////////////////////////////////////////////////////////////
  279. // CTipText message handlers
  280. BEGIN_MESSAGE_MAP(CTipText, CButton)
  281. //{{AFX_MSG_MAP(CStaticTitle)
  282. // NOTE - the ClassWizard will add and remove mapping macros here.
  283. //}}AFX_MSG_MAP
  284. END_MESSAGE_MAP()
  285. //------------------------------------------------------------------------
  286. void CTipText::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
  287. {
  288. // static m_fInitializedFont = FALSE;
  289. // prep the device context
  290. CDC* pdc = CDC::FromHandle(lpDrawItemStruct->hDC);
  291. // get the drawing rect
  292. CRect rect = lpDrawItemStruct->rcItem;
  293. /*
  294. if ( ! m_fInitializedFont )
  295. {
  296. // get the window font
  297. CFont* pfont = GetFont();
  298. LOGFONT logfont;
  299. pfont->GetLogFont( &logfont );
  300. // modify the font - add underlining
  301. logfont.lfHeight = 18;
  302. logfont.lfWidth = 0;
  303. logfont.lfPitchAndFamily = DEFAULT_PITCH | FF_ROMAN;
  304. logfont.lfFaceName[0] = 0;
  305. // set the font back
  306. pfont->CreateFontIndirect( &logfont );
  307. SetFont( pfont, TRUE );
  308. m_fInitializedFont = TRUE;
  309. }
  310. */
  311. // fill in the background of the rectangle
  312. pdc->FillSolidRect( &rect, COLOR_WHITE );
  313. pdc->SetTextColor( COLOR_BLACK );
  314. // draw the text
  315. CString sz;
  316. GetWindowText( sz );
  317. pdc->DrawText( sz, &rect, DT_LEFT|DT_WORDBREAK );
  318. }