TipDlg.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. FinalSun/FinalAlert 2 Mission Editor
  3. Copyright (C) 1999-2024 Electronic Arts, Inc.
  4. Authored by Matthias Wagner
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #include "stdafx.h"
  17. #include "resource.h"
  18. #include "mapdata.h"
  19. #include "variables.h"
  20. #include "functions.h"
  21. #include <winreg.h>
  22. #include <sys\stat.h>
  23. #include <sys\types.h>
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CTipDlg dialog field
  31. #define MAX_BUFLEN 1000
  32. static const TCHAR szSection[] = _T("Tip");
  33. static const TCHAR szIntFilePos[] = _T("FilePos");
  34. static const TCHAR szTimeStamp[] = _T("TimeStamp");
  35. static const TCHAR szIntStartup[] = _T("StartUp");
  36. extern char AppPath[];
  37. CTipDlg::CTipDlg(CWnd* pParent /*=NULL*/)
  38. : CDialog(IDD_TIP, pParent)
  39. {
  40. //{{AFX_DATA_INIT(CTipDlg)
  41. m_bStartup = TRUE;
  42. //}}AFX_DATA_INIT
  43. CIniFile optini;
  44. CString iniFile;
  45. iniFile=AppPath;
  46. #ifndef RA2_MODE
  47. iniFile+="\\FinalSun.ini";
  48. #else
  49. iniFile+="\\FinalAlert.ini";
  50. #endif
  51. optini.LoadFile(iniFile);
  52. CWinApp* pApp = AfxGetApp();
  53. m_bStartup = !atoi(optini.sections[szSection].values[szIntStartup]);
  54. UINT iFilePos = atoi(optini.sections[szSection].values[szIntFilePos]);
  55. // try top open the tips file
  56. CString tipsfile=AppPath;
  57. tipsfile+="\\tips.";
  58. tipsfile+=language.sections[theApp.m_Options.LanguageName+"Header"].values["ExtensionName"];
  59. m_pStream = fopen(tipsfile, "r");
  60. if (m_pStream == NULL)
  61. {
  62. m_strTip=GetLanguageStringACP("CG_IDS_FILE_ABSENT");
  63. return;
  64. }
  65. //now check if the tips file is changed! (check the date the tips file was created)
  66. struct _stat buf;
  67. _fstat(_fileno(m_pStream), &buf);
  68. CString strCurrentTime = ctime(&buf.st_ctime);
  69. strCurrentTime.TrimRight();
  70. CString strStoredTime = optini.sections[szSection].values[szTimeStamp];
  71. if (strCurrentTime != strStoredTime)
  72. {
  73. iFilePos = 0;
  74. optini.sections[szSection].values[szTimeStamp]=(LPCTSTR)strCurrentTime;
  75. }
  76. if (fseek(m_pStream, iFilePos, SEEK_SET) != 0)
  77. {
  78. AfxMessageBox(GetLanguageStringACP("CG_IDP_FILE_CORRUPT"));
  79. }
  80. else
  81. {
  82. GetNextTipString(m_strTip);
  83. }
  84. optini.SaveFile(iniFile);
  85. }
  86. CTipDlg::~CTipDlg()
  87. {
  88. if (m_pStream != NULL)
  89. {
  90. CIniFile optini;
  91. CString iniFile;
  92. iniFile=AppPath;
  93. #ifndef RA2_MODE
  94. iniFile+="\\FinalSun.ini";
  95. #else
  96. iniFile+="\\FinalAlert.ini";
  97. #endif
  98. optini.LoadFile(iniFile);
  99. char val[50];
  100. itoa(ftell(m_pStream),val, 10);
  101. optini.sections[szSection].values[szIntFilePos]=val;
  102. optini.SaveFile(iniFile);
  103. fclose(m_pStream);
  104. }
  105. }
  106. void CTipDlg::DoDataExchange(CDataExchange* pDX)
  107. {
  108. CDialog::DoDataExchange(pDX);
  109. //{{AFX_DATA_MAP(CTipDlg)
  110. DDX_Check(pDX, IDC_STARTUP, m_bStartup);
  111. DDX_Text(pDX, IDC_TIPSTRING, m_strTip);
  112. //}}AFX_DATA_MAP
  113. }
  114. BEGIN_MESSAGE_MAP(CTipDlg, CDialog)
  115. //{{AFX_MSG_MAP(CTipDlg)
  116. ON_BN_CLICKED(IDC_NEXTTIP, OnNextTip)
  117. ON_WM_CTLCOLOR()
  118. ON_WM_PAINT()
  119. //}}AFX_MSG_MAP
  120. END_MESSAGE_MAP()
  121. /////////////////////////////////////////////////////////////////////////////
  122. // CTipDlg - handlers
  123. void CTipDlg::OnNextTip()
  124. {
  125. GetNextTipString(m_strTip);
  126. UpdateData(FALSE);
  127. }
  128. void CTipDlg::GetNextTipString(CString& strNext)
  129. {
  130. LPTSTR lpsz = strNext.GetBuffer(MAX_BUFLEN);
  131. BOOL bStop = FALSE;
  132. while (!bStop)
  133. {
  134. if (_fgetts(lpsz, MAX_BUFLEN, m_pStream) == NULL)
  135. {
  136. if (fseek(m_pStream, 0, SEEK_SET) != 0)
  137. AfxMessageBox(GetLanguageStringACP("CG_IDP_FILE_CORRUPT"));
  138. }
  139. else
  140. {
  141. if (*lpsz != ' ' && *lpsz != '\t' &&
  142. *lpsz != '\n' && *lpsz != ';')
  143. {
  144. bStop = TRUE;
  145. }
  146. }
  147. }
  148. strNext.ReleaseBuffer();
  149. }
  150. HBRUSH CTipDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  151. {
  152. if (pWnd->GetDlgCtrlID() == IDC_TIPSTRING)
  153. return (HBRUSH)GetStockObject(WHITE_BRUSH);
  154. return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
  155. }
  156. void CTipDlg::OnOK()
  157. {
  158. CDialog::OnOK();
  159. // actualize startup
  160. CIniFile optini;
  161. CString iniFile;
  162. iniFile=AppPath;
  163. #ifndef RA2_MODE
  164. iniFile+="\\FinalSun.ini";
  165. #else
  166. iniFile+="\\FinalAlert.ini";
  167. #endif
  168. optini.LoadFile(iniFile);
  169. char val[50];
  170. itoa(!m_bStartup,val, 10);
  171. optini.sections[szSection].values[szIntStartup]=val;
  172. optini.SaveFile(iniFile);
  173. }
  174. BOOL CTipDlg::OnInitDialog()
  175. {
  176. CDialog::OnInitDialog();
  177. // deactivate next tip if no tips are present
  178. if (m_pStream == NULL)
  179. GetDlgItem(IDC_NEXTTIP)->EnableWindow(FALSE);
  180. SetDlgItemText(IDC_STARTUP, GetLanguageStringACP("TipDialogShowAtStartup"));
  181. SetDlgItemText(IDC_NEXTTIP, GetLanguageStringACP("TipDialogNext"));
  182. SetDlgItemText(IDOK, GetLanguageStringACP("TipDialogClose"));
  183. SetWindowText(GetLanguageStringACP("TipDialogCaption"));
  184. return TRUE;
  185. }
  186. void CTipDlg::OnPaint()
  187. {
  188. CPaintDC dc(this);
  189. // prepare draw area for the big static rectangle
  190. CWnd* pStatic = GetDlgItem(IDC_TOOLTIPCENTER);
  191. CRect rect;
  192. pStatic->GetWindowRect(&rect);
  193. ScreenToClient(&rect);
  194. // draw white background
  195. CBrush brush;
  196. brush.CreateStockObject(WHITE_BRUSH);
  197. dc.FillRect(rect, &brush);
  198. // load bitmap
  199. CBitmap bmp;
  200. bmp.LoadBitmap(IDB_LIGHTBULB);
  201. BITMAP bmpInfo;
  202. bmp.GetBitmap(&bmpInfo);
  203. // draw bitmap in the left corner
  204. CDC dcTmp;
  205. dcTmp.CreateCompatibleDC(&dc);
  206. dcTmp.SelectObject(&bmp);
  207. rect.bottom = bmpInfo.bmHeight + rect.top;
  208. dc.BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
  209. &dcTmp, 0, 0, SRCCOPY);
  210. // draw "Did you know" in the correct language
  211. CString strMessage;
  212. strMessage=GetLanguageStringACP("CG_IDS_DIDYOUKNOW");
  213. rect.left += bmpInfo.bmWidth;
  214. dc.DrawText(strMessage, rect, DT_VCENTER | DT_SINGLELINE);
  215. }