EditViewDlg.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "qe3.h"
  23. #include "Radiant.h"
  24. #include "EditViewDlg.h"
  25. // CEditViewDlg dialog
  26. IMPLEMENT_DYNAMIC(CEditViewDlg, CDialog)
  27. CEditViewDlg::CEditViewDlg(CWnd* pParent /*=NULL*/)
  28. : CDialog(CEditViewDlg::IDD, pParent)
  29. {
  30. findDlg = NULL;
  31. }
  32. CEditViewDlg::~CEditViewDlg() {
  33. }
  34. void CEditViewDlg::DoDataExchange(CDataExchange* pDX) {
  35. CDialog::DoDataExchange(pDX);
  36. DDX_Control(pDX, IDC_EDIT_INFO, editInfo);
  37. }
  38. static UINT FindDialogMessage = ::RegisterWindowMessage(FINDMSGSTRING);
  39. BEGIN_MESSAGE_MAP(CEditViewDlg, CDialog)
  40. ON_WM_SIZE()
  41. ON_BN_CLICKED(IDC_BUTTON_OPEN, OnBnClickedButtonOpen)
  42. ON_BN_CLICKED(IDC_BUTTON_SAVE, OnBnClickedButtonSave)
  43. ON_WM_DESTROY()
  44. ON_WM_TIMER()
  45. ON_BN_CLICKED(IDC_BUTTON_GOTO, OnBnClickedButtonGoto)
  46. ON_REGISTERED_MESSAGE(FindDialogMessage, OnFindDialogMessage)
  47. END_MESSAGE_MAP()
  48. // CEditViewDlg message handlers
  49. void CEditViewDlg::OnSize(UINT nType, int cx, int cy) {
  50. CDialog::OnSize(nType, cx, cy);
  51. if (GetSafeHwnd() == NULL) {
  52. return;
  53. }
  54. CRect rect, crect;
  55. GetClientRect(rect);
  56. CWnd *wnd = GetDlgItem(IDC_BUTTON_OPEN);
  57. if (wnd == NULL || (wnd && wnd->GetSafeHwnd() == NULL)) {
  58. return;
  59. }
  60. wnd->GetWindowRect(crect);
  61. wnd->SetWindowPos(NULL, 4, 4, crect.Width(), crect.Height(), SWP_SHOWWINDOW);
  62. wnd = GetDlgItem(IDC_BUTTON_SAVE);
  63. int left = 8 + crect.Width();
  64. wnd->SetWindowPos(NULL, left, 4, crect.Width(), crect.Height(), SWP_SHOWWINDOW);
  65. wnd = GetDlgItem(IDOK);
  66. wnd->SetWindowPos(NULL, rect.Width() - crect.Width() - 4, 4, crect.Width(), crect.Height(), SWP_SHOWWINDOW);
  67. editInfo.SetWindowPos(NULL, 4, 8 + crect.Height(), rect.Width() - 8, rect.Height() - crect.Height() * 2 - 16, SWP_SHOWWINDOW);
  68. wnd = GetDlgItem(IDC_BUTTON_GOTO);
  69. wnd->SetWindowPos(NULL, 4, rect.Height() - 4 - crect.Height(), crect.Width(), crect.Height(), SWP_SHOWWINDOW);
  70. wnd = GetDlgItem(IDC_EDIT_GOTO);
  71. wnd->SetWindowPos(NULL, 8 + crect.Width(), rect.Height() - 3 - crect.Height(), crect.Width() + 8, crect.Height() - 3, SWP_SHOWWINDOW);
  72. wnd = GetDlgItem(IDC_STATIC_LINE);
  73. wnd->SetWindowPos(NULL, 30 + crect.Width() * 2, rect.Height() - crect.Height(), crect.Width() * 2, crect.Height(), SWP_SHOWWINDOW);
  74. wnd = GetDlgItem(IDC_EDIT_LINE);
  75. wnd->SetWindowPos(NULL, 40 + crect.Width() * 3, rect.Height() - crect.Height(), crect.Width() + 8, crect.Height(), SWP_SHOWWINDOW);
  76. }
  77. void CEditViewDlg::ShowFindDlg() {
  78. if (findDlg) {
  79. return;
  80. }
  81. findDlg = new CFindReplaceDialog();
  82. findDlg->Create(TRUE, findStr, NULL, FR_DOWN, this);
  83. }
  84. void CEditViewDlg::OnBnClickedButtonOpen() {
  85. CPreviewDlg *dlg = NULL;
  86. dlg = ((mode == MATERIALS) ? CEntityDlg::ShowMaterialChooser() : CEntityDlg::ShowGuiChooser());
  87. if (dlg) {
  88. if (mode == MATERIALS) {
  89. const idMaterial *mat = declManager->FindMaterial(dlg->mediaName);
  90. SetMaterialInfo(mat->GetName(), mat->GetFileName(), mat->GetLineNum());
  91. } else {
  92. SetGuiInfo(dlg->mediaName);
  93. }
  94. }
  95. }
  96. void CEditViewDlg::OnBnClickedButtonSave() {
  97. if (fileName.Length()) {
  98. CString text;
  99. editInfo.GetWindowText(text);
  100. fileSystem->WriteFile(fileName, text.GetBuffer(0), text.GetLength(), "fs_devpath");
  101. if (mode == MATERIALS) {
  102. declManager->Reload( false );
  103. } else {
  104. uiManager->Reload(false);
  105. }
  106. }
  107. }
  108. void CEditViewDlg::UpdateEditPreview() {
  109. if (GetSafeHwnd() && editInfo.GetSafeHwnd()) {
  110. editInfo.SetWindowText(editText);
  111. editInfo.LineScroll(line);
  112. int cindex = editInfo.LineIndex(line);
  113. int len = editInfo.LineLength(line);
  114. editInfo.SetSel(cindex, cindex);
  115. mediaPreview.SetMode((mode == MATERIALS) ? CMediaPreviewDlg::MATERIALS : CMediaPreviewDlg::GUIS);
  116. mediaPreview.SetMedia((mode == MATERIALS) ? matName : fileName);
  117. SetWindowText(va("Editing %s in file <%s>", (mode == MATERIALS) ? matName.c_str() : fileName.c_str(), fileName.c_str()));
  118. editInfo.SetFocus();
  119. }
  120. }
  121. BOOL CEditViewDlg::OnInitDialog() {
  122. CDialog::OnInitDialog();
  123. mediaPreview.Create(IDD_DIALOG_EDITPREVIEW, this);
  124. mediaPreview.ShowWindow(SW_SHOW);
  125. CRect rct;
  126. LONG lSize = sizeof(rct);
  127. if (LoadRegistryInfo("Radiant::EditViewWindow", &rct, &lSize)) {
  128. SetWindowPos(NULL, rct.left, rct.top, rct.Width(), rct.Height(), SWP_SHOWWINDOW);
  129. }
  130. editInfo.SetTabStops();
  131. editInfo.SetLimitText(1024 * 1024);
  132. UpdateEditPreview();
  133. SetTimer(1, 250, NULL);
  134. return TRUE; // return TRUE unless you set the focus to a control
  135. // EXCEPTION: OCX Property Pages should return FALSE
  136. }
  137. void CEditViewDlg::OnDestroy() {
  138. if (GetSafeHwnd()) {
  139. CRect rct;
  140. GetWindowRect(rct);
  141. SaveRegistryInfo("Radiant::EditViewWindow", &rct, sizeof(rct));
  142. }
  143. CDialog::OnDestroy();
  144. }
  145. void CEditViewDlg::SetMaterialInfo(const char *name, const char *file, int _line) {
  146. idStr str;
  147. void *buf;
  148. fileName = "";
  149. matName = "";
  150. line = 0;
  151. str = fileSystem->OSPathToRelativePath( file );
  152. int size = fileSystem->ReadFile( str, &buf );
  153. if (size > 0) {
  154. fileName = str;
  155. matName = name;
  156. line = _line - 1;
  157. if (line < 0) {
  158. line = 0;
  159. }
  160. editText = (char*)buf;
  161. fileSystem->FreeFile(buf);
  162. }
  163. UpdateEditPreview();
  164. }
  165. void CEditViewDlg::SetGuiInfo(const char *name) {
  166. fileName = "";
  167. line = 0;
  168. void *buf;
  169. int size = fileSystem->ReadFile(name, &buf, NULL);
  170. if (size > 0) {
  171. fileName = name;
  172. editText = (char*)buf;
  173. fileSystem->FreeFile(buf);
  174. }
  175. UpdateEditPreview();
  176. }
  177. void CEditViewDlg::OnTimer(UINT nIDEvent) {
  178. CDialog::OnTimer(nIDEvent);
  179. CWnd *wnd = GetDlgItem(IDC_EDIT_LINE);
  180. if (wnd) {
  181. int start, end;
  182. editInfo.GetSel(start, end);
  183. wnd->SetWindowText(va("%i",editInfo.LineFromChar(start)));
  184. }
  185. }
  186. void CEditViewDlg::OnBnClickedButtonGoto() {
  187. CWnd *wnd = GetDlgItem(IDC_EDIT_GOTO);
  188. if (wnd) {
  189. CString str;
  190. wnd->GetWindowText(str);
  191. if (str.GetLength()) {
  192. int l = atoi(str);
  193. editInfo.SetSel(0, 0);
  194. editInfo.LineScroll(l);
  195. int cindex = editInfo.LineIndex(l);
  196. int len = editInfo.LineLength(l);
  197. editInfo.SetSel(cindex, cindex);
  198. editInfo.RedrawWindow();
  199. editInfo.SetFocus();
  200. }
  201. }
  202. }
  203. BOOL CEditViewDlg::PreTranslateMessage(MSG* pMsg) {
  204. if (pMsg->message == WM_KEYDOWN && (pMsg->wParam == 's' || pMsg->wParam == 'S') && GetAsyncKeyState(VK_CONTROL) & 0x8000) {
  205. OnBnClickedButtonSave();
  206. return TRUE;
  207. }
  208. if (pMsg->message == WM_KEYDOWN && (pMsg->wParam == 'o' || pMsg->wParam == 'O') && GetAsyncKeyState(VK_CONTROL) & 0x8000) {
  209. OnBnClickedButtonOpen();
  210. return TRUE;
  211. }
  212. if (pMsg->message == WM_KEYDOWN && (pMsg->wParam == 'f' || pMsg->wParam == 'F') && GetAsyncKeyState(VK_CONTROL) & 0x8000) {
  213. ShowFindDlg();
  214. return TRUE;
  215. }
  216. if (pMsg->hwnd == editInfo.GetSafeHwnd() && (pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_TAB)) {
  217. // get the char index of the caret position
  218. int nPos = LOWORD(editInfo.CharFromPos(editInfo.GetCaretPos()));
  219. // select zero chars
  220. editInfo.SetSel(nPos, nPos);
  221. // then replace that selection with a TAB
  222. editInfo.ReplaceSel("\t", TRUE);
  223. return TRUE;
  224. }
  225. return CDialog::PreTranslateMessage(pMsg);
  226. }
  227. LRESULT CEditViewDlg::OnFindDialogMessage(WPARAM wParam, LPARAM lParam) {
  228. if (findDlg == NULL) {
  229. return 0;
  230. }
  231. if (findDlg->IsTerminating()) {
  232. findDlg = NULL;
  233. return 0;
  234. }
  235. // If the FR_FINDNEXT flag is set,
  236. // call the application-defined search routine
  237. // to search for the requested string.
  238. if(findDlg->FindNext()) {
  239. //read data from dialog
  240. findStr = findDlg->GetFindString().GetBuffer(0);
  241. CString str;
  242. editInfo.GetWindowText(str);
  243. editText = str;
  244. int start, end;
  245. editInfo.GetSel(start, end);
  246. start = editText.Find(findStr, false, end);
  247. if (start >= 0) {
  248. editInfo.SetSel(start, start + findStr.Length());
  249. editInfo.Invalidate();
  250. editInfo.RedrawWindow();
  251. }
  252. }
  253. return 0;
  254. }