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