]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/tools/pda/DialogPDAEditor.cpp
hello world
[icculus/iodoom3.git] / neo / tools / pda / DialogPDAEditor.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 "../../game/game.h"
33 #include "../../sys/win32/win_local.h"
34 #include "../../sys/win32/rc/common_resource.h"
35 #include "../../sys/win32/rc/PDAEditor_resource.h"
36 #include "../comafx/DialogName.h"
37
38 #include "DialogPDAEditor.h"
39
40 #ifdef ID_DEBUG_MEMORY
41 #undef new
42 #undef DEBUG_NEW
43 #define DEBUG_NEW new
44 #endif
45
46 /////////////////////////////////////////////////////////////////////////////
47 // CCDialogPDAEditor dialog
48 CDialogPDAEditor *g_PDAEditorDialog = NULL;
49
50
51 CDialogPDAEditor::CDialogPDAEditor(CWnd* pParent /*=NULL*/)
52         : CDialog(CDialogPDAEditor::IDD, pParent)
53 {
54         //{{AFX_DATA_INIT(CDialogPDAEditor)
55         //}}AFX_DATA_INIT
56 }
57
58
59 void CDialogPDAEditor::DoDataExchange(CDataExchange* pDX)
60 {
61         CDialog::DoDataExchange(pDX);
62         //{{AFX_DATA_MAP(CDialogPDAEditor)
63         DDX_Control( pDX, IDC_LIST_PDAS, pdaList );
64         DDX_Control( pDX, IDC_LIST_EMAIL, emailList );
65         DDX_Control( pDX, IDC_LIST_AUDIO, audioList );
66         DDX_Control( pDX, IDC_LIST_VIDEO, videoList );
67
68         DDX_Text( pDX, IDC_EDIT_FULLNAME, fullName );
69         DDX_Text( pDX, IDC_EDIT_SHORTNAME, shortName );
70         DDX_Text( pDX, IDC_EDIT_POST, post );
71         DDX_Text( pDX, IDC_EDIT_TITLE, title );
72         DDX_Text( pDX, IDC_EDIT_SECURITY, security );
73         DDX_Text( pDX, IDC_EDIT_IDNUM, idnum );
74
75         DDX_Control( pDX, IDC_BUTTON_SAVE, saveButton );
76         //}}AFX_DATA_MAP
77 }
78
79
80 BEGIN_MESSAGE_MAP(CDialogPDAEditor, CDialog)
81         //{{AFX_MSG_MAP(CDialogPDAEditor)
82         ON_LBN_SELCHANGE( IDC_LIST_PDAS, OnSelChangePDA )
83         ON_BN_CLICKED( IDC_BUTTON_SAVE, OnBtnClickedSave )
84         ON_BN_CLICKED( IDC_BUTTON_RANDOMID, OnBtnClickedRandom )
85         ON_BN_CLICKED( IDC_BUTTON_PDA_ADD, OnBtnClickedPDAAdd )
86         ON_BN_CLICKED( IDC_BUTTON_PDA_DEL, OnBtnClickedPDADel )
87         ON_BN_CLICKED( IDC_BUTTON_EMAIL_ADD, OnBtnClickedEmailAdd )
88         ON_BN_CLICKED( IDC_BUTTON_EMAIL_EDIT, OnBtnClickedEmailEdit )
89         ON_BN_CLICKED( IDC_BUTTON_EMAIL_DELETE, OnBtnClickedEmailDel )
90         ON_BN_CLICKED( IDC_BUTTON_AUDIO_ADD, OnBtnClickedAudioAdd )
91         ON_BN_CLICKED( IDC_BUTTON_AUDIO_EDIT, OnBtnClickedAudioEdit )
92         ON_BN_CLICKED( IDC_BUTTON_AUDIO_DELETE, OnBtnClickedAudioDel )
93         ON_BN_CLICKED( IDC_BUTTON_VIDEO_ADD, OnBtnClickedVideoAdd )
94         ON_BN_CLICKED( IDC_BUTTON_VIDEO_EDIT, OnBtnClickedVideoEdit )
95         ON_BN_CLICKED( IDC_BUTTON_VIDEO_DELETE, OnBtnClickedVideoDel )
96         //}}AFX_MSG_MAP
97 END_MESSAGE_MAP()
98
99 /////////////////////////////////////////////////////////////////////////////
100 // CDialogPDAEditor message handlers
101
102 void PDAEditorInit( const idDict *spawnArgs ) {
103
104         if ( renderSystem->IsFullScreen() ) {
105                 common->Printf( "Cannot run the PDA editor in fullscreen mode.\n"
106                                         "Set r_fullscreen to 0 and vid_restart.\n" );
107                 return;
108         }
109
110         if ( g_PDAEditorDialog == NULL ) {
111                 InitAfx();
112                 g_PDAEditorDialog = new CDialogPDAEditor();
113         }
114
115         if ( g_PDAEditorDialog->GetSafeHwnd() == NULL ) {
116                 g_PDAEditorDialog->Create(IDD_DIALOG_PDA_EDITOR);
117 /*
118                 // FIXME: restore position
119                 CRect rct;
120                 g_PDAEditorDialog->SetWindowPos( NULL, rct.left, rct.top, 0,0, SWP_NOSIZE );
121 */
122         }
123
124         idKeyInput::ClearStates();
125
126         g_PDAEditorDialog->ShowWindow( SW_SHOW );
127         g_PDAEditorDialog->SetFocus();
128
129         if ( spawnArgs ) {
130                 // select PDA based on spawn args
131                 const char *name = spawnArgs->GetString( "pda" );
132                 idDeclPDA *decl = static_cast<idDeclPDA *>( const_cast<idDecl *>( declManager->FindType( DECL_PDA, name ) ) );
133                 // FIXME: select this PDA
134         }
135 }
136
137 void PDAEditorRun( void ) {
138 #if _MSC_VER >= 1300
139         MSG *msg = AfxGetCurrentMessage();                      // TODO Robert fix me!!
140 #else
141         MSG *msg = &m_msgCur;
142 #endif
143
144         while( ::PeekMessage(msg, NULL, NULL, NULL, PM_NOREMOVE) ) {
145                 // pump message
146                 if ( !AfxGetApp()->PumpMessage() ) {
147                 }
148         }
149 }
150
151 void PDAEditorShutdown( void ) {
152         delete g_PDAEditorDialog;
153         g_PDAEditorDialog = NULL;
154 }
155
156 void CDialogPDAEditor::OnActivate( UINT nState, CWnd *pWndOther, BOOL bMinimized ) {
157         CDialog::OnActivate( nState, pWndOther, bMinimized );
158         if ( nState != WA_INACTIVE ) {
159         }
160 }
161
162 void CDialogPDAEditor::OnMove( int x, int y ) {
163         if ( GetSafeHwnd() ) {
164                 CRect rct;
165                 GetWindowRect( rct );
166                 // FIXME: save position
167         }
168         CDialog::OnMove( x, y );
169 }
170
171 void CDialogPDAEditor::OnDestroy() {
172
173         com_editors &= ~EDITOR_PDA;
174
175         return CDialog::OnDestroy();
176 }
177
178 BOOL CDialogPDAEditor::OnInitDialog() 
179 {
180         CDialog::OnInitDialog();
181         
182         // Indicate the PDA dialog is opened
183         com_editors |= EDITOR_PDA;
184
185         PopulatePDAList();
186
187         return TRUE;  // return TRUE unless you set the focus to a control
188                       // EXCEPTION: OCX Property Pages should return FALSE
189 }
190
191 BOOL CDialogPDAEditor::PreTranslateMessage(MSG* pMsg)
192 {
193         return CDialog::PreTranslateMessage(pMsg);
194 }
195
196 void CDialogPDAEditor::PopulatePDAList()
197 {
198         pdaList.ResetContent();
199
200         int i;
201         int num = declManager->GetNumDecls(DECL_PDA);
202         for ( i=0; i < num; i++ ) {
203                 const idDeclPDA *pda = dynamic_cast<const idDeclPDA *>( declManager->DeclByIndex(DECL_PDA, i) );
204                 pdaList.AddString( pda->GetName() );
205         }
206 }
207
208 void CDialogPDAEditor::OnSelChangePDA()
209 {
210         int i, num;
211
212         int index = pdaList.GetCurSel();
213         if ( index < 0 ) {
214                 return;
215         }
216
217         const idDeclPDA *pda = dynamic_cast<const idDeclPDA *>( declManager->DeclByIndex(DECL_PDA, index) );
218         if ( !pda ) {
219                 return;
220         }
221
222         CString windowTitle;
223         windowTitle.Format("PDA Editor - %s", pda->GetName());
224
225         idFile *file = fileSystem->OpenFileAppend( pda->GetFileName() );
226         if ( file ) {
227                 fileSystem->CloseFile(file);
228                 saveButton.EnableWindow( true );
229         } else {
230                 windowTitle += " [Read Only]";
231                 saveButton.EnableWindow( false );
232         }
233
234         SetWindowText( windowTitle );
235
236         emailList.ResetContent();
237         num = pda->GetNumEmails();
238         for ( i=0; i < num; i++ ) {
239                 emailList.AddString( pda->GetEmailByIndex( i )->GetSubject() );
240         }
241
242         audioList.ResetContent();
243         num = pda->GetNumAudios();
244         for ( i=0; i < num; i++ ) {
245                 audioList.AddString( pda->GetAudioByIndex( i )->GetAudioName() );
246         }
247
248         videoList.ResetContent();
249         num = pda->GetNumVideos();
250         for ( i=0; i < num; i++ ) {
251                 videoList.AddString( pda->GetVideoByIndex( i )->GetVideoName() );
252         }
253
254         fullName = pda->GetFullName();
255         shortName = pda->GetPdaName();
256         post = pda->GetPost();
257         title = pda->GetTitle();
258         security = pda->GetSecurity();
259         idnum = pda->GetID();
260
261         UpdateData( FALSE );
262 }
263
264 void CDialogPDAEditor::OnBtnClickedSave()
265 {
266         UpdateData();
267
268         int index = pdaList.GetCurSel();
269         if ( index < 0 ) {
270                 return;
271         }
272
273         const idDeclPDA *pdaConst = dynamic_cast<const idDeclPDA *>( declManager->DeclByIndex(DECL_PDA, index) );
274         if ( pdaConst ) {
275                 idDeclPDA *pda = const_cast<idDeclPDA *>(pdaConst);
276
277                 CString declText = "\n";
278                 declText += "pda ";
279                 declText += pda->GetName();
280                 declText += " {\n";
281         
282                 declText += "\tname    \t\t\"" + shortName + "\"\n";
283                 declText += "\tfullname\t\t\"" + fullName + "\"\n";
284                 declText += "\ticon    \t\t\"\"\n";
285                 declText += "\tid      \t\t\"" + idnum + "\"\n";
286                 declText += "\tpost    \t\t\"" + post + "\"\n";
287                 declText += "\ttitle   \t\t\"" + title + "\"\n";
288                 declText += "\tsecurity\t\t\"" + security + "\"\n";
289
290                 for ( int i = 0; i < pda->GetNumEmails(); i++ ) {
291                         declText += "\tpda_email\t\t\"";
292                         declText += pda->GetEmailByIndex(i)->GetName();
293                         declText += "\"\n";
294                 }
295
296                 for ( int i = 0; i < pda->GetNumAudios(); i++ ) {
297                         declText += "\tpda_audio\t\t\"";
298                         declText += pda->GetAudioByIndex(i)->GetName();
299                         declText += "\"\n";
300                 }
301
302                 for ( int i = 0; i < pda->GetNumVideos(); i++ ) {
303                         declText += "\tpda_video\t\t\"";
304                         declText += pda->GetVideoByIndex(i)->GetName();
305                         declText += "\"\n";
306                 }
307
308                 declText += "}";
309
310                 pda->SetText( declText );
311                 pda->ReplaceSourceFileText();
312                 pda->Invalidate();
313         }
314 }
315
316 void CDialogPDAEditor::OnBtnClickedRandom()
317 {
318         idnum.Format("%d-%02X", 1000+(rand()%8999), (rand()%255));
319         UpdateData( FALSE );
320 }
321
322 class CDialogPDAAdd : public CDialog
323 {
324 public:
325         CDialogPDAAdd() : CDialog(IDD_DIALOG_PDA_ADD) {}
326         CString name;
327         void OnOK() { GetDlgItemText( IDC_EDIT1, name ); CDialog::OnOK(); }
328 };
329
330 void CDialogPDAEditor::OnBtnClickedPDAAdd()
331 {
332         CDialogPDAAdd dlg;
333         if ( dlg.DoModal() == IDOK ) {
334                 dlg.name.MakeLower();
335                 idDecl *decl = declManager->CreateNewDecl( DECL_PDA, dlg.name, "newpdas/" + dlg.name + ".pda" );
336                 decl->ReplaceSourceFileText();
337                 decl->Invalidate();
338                 PopulatePDAList();
339                 pdaList.SelectString( 0, dlg.name );
340                 OnSelChangePDA();
341         }
342 }
343
344 void CDialogPDAEditor::OnBtnClickedPDADel()
345 {
346 }
347
348 void CDialogPDAEditor::OnBtnClickedEmailAdd()
349 {
350         int index = pdaList.GetCurSel();
351         if ( index < 0 ) {
352                 return;
353         }
354         const idDeclPDA *pda = dynamic_cast<const idDeclPDA *>( declManager->DeclByIndex(DECL_PDA, index) );
355
356         if ( pda ) {
357                 CString name;
358
359                 // Search for an unused name
360                 int newIndex = pda->GetNumEmails();
361                 do {
362                         name.Format("%s_email_%d", pda->GetName(), newIndex++);
363                 } while ( declManager->FindType(DECL_EMAIL, name, false) != NULL );
364
365                 CDialogPDAEditEmail addDlg;
366                 addDlg.SetName(name);
367                 if ( addDlg.DoModal() == IDOK ) {
368                         idDeclEmail *email = static_cast<idDeclEmail *>(declManager->CreateNewDecl(DECL_EMAIL, name, pda->GetFileName()));
369                         email->SetText( addDlg.GetDeclText() );
370                         email->ReplaceSourceFileText();
371                         email->Invalidate();
372
373                         pda->AddEmail( name );
374
375                         // Get it again to reparse
376                         const idDeclEmail *emailConst = static_cast<const idDeclEmail *>( declManager->FindType( DECL_EMAIL, name) );
377                         emailList.AddString( emailConst->GetSubject() );
378
379                         // Save the pda to include this email in the list
380                         // This has a side-effect of saving any other changes, but I don't really care right now
381                         OnBtnClickedSave();
382                 }
383         }
384 }
385
386 void CDialogPDAEditor::OnBtnClickedEmailEdit()
387 {
388         int index = pdaList.GetCurSel();
389         if ( index < 0 ) {
390                 return;
391         }
392         const idDeclPDA *pda = dynamic_cast<const idDeclPDA *>( declManager->DeclByIndex(DECL_PDA, index) );
393
394         if ( pda ) {
395                 index = emailList.GetCurSel();
396                 if ( index < 0 ) {
397                         return;
398                 }
399
400                 CDialogPDAEditEmail editDlg;
401                 editDlg.SetEmail( pda->GetEmailByIndex( index ) );
402                 if ( editDlg.DoModal() == IDOK ) {
403                         idDeclEmail *email = const_cast<idDeclEmail *>( pda->GetEmailByIndex( index ) );
404                         email->SetText( editDlg.GetDeclText() );
405                         email->ReplaceSourceFileText();
406                         email->Invalidate();
407
408                         // Get it again to reparse
409                         email = const_cast<idDeclEmail *>( pda->GetEmailByIndex( index ) );
410
411                         emailList.DeleteString( index );
412                         emailList.InsertString( index, email->GetSubject() );
413                 }
414         }
415 }
416
417 void CDialogPDAEditor::OnBtnClickedEmailDel()
418 {
419 }
420
421 void CDialogPDAEditor::OnBtnClickedAudioAdd()
422 {
423 }
424
425 void CDialogPDAEditor::OnBtnClickedAudioEdit()
426 {
427 }
428
429 void CDialogPDAEditor::OnBtnClickedAudioDel()
430 {
431 }
432
433 void CDialogPDAEditor::OnBtnClickedVideoAdd()
434 {
435 }
436
437 void CDialogPDAEditor::OnBtnClickedVideoEdit()
438 {
439 }
440
441 void CDialogPDAEditor::OnBtnClickedVideoDel()
442 {
443 }
444
445
446
447
448 CDialogPDAEditEmail::CDialogPDAEditEmail(CWnd* pParent /*=NULL*/)
449         : CDialog(CDialogPDAEditEmail::IDD, pParent)
450 {
451         //{{AFX_DATA_INIT(CDialogPDAEditEmail)
452         //}}AFX_DATA_INIT
453 }
454
455
456 void CDialogPDAEditEmail::DoDataExchange(CDataExchange* pDX)
457 {
458         CDialog::DoDataExchange(pDX);
459         //{{AFX_DATA_MAP(CDialogPDAEditEmail)
460         DDX_Text( pDX, IDC_EDIT_TO, to );
461         DDX_Text( pDX, IDC_EDIT_FROM, from );
462         DDX_Text( pDX, IDC_EDIT_DATE, date );
463         DDX_Text( pDX, IDC_EDIT_SUBJECT, subject );
464         DDX_Text( pDX, IDC_EDIT_BODY, body );
465         //}}AFX_DATA_MAP
466 }
467
468
469 BEGIN_MESSAGE_MAP(CDialogPDAEditEmail, CDialog)
470         //{{AFX_MSG_MAP(CDialogPDAEditEmail)
471         //}}AFX_MSG_MAP
472 END_MESSAGE_MAP()
473
474 /////////////////////////////////////////////////////////////////////////////
475 // CDialogPDAEditor message handlers
476
477 BOOL CDialogPDAEditEmail::OnInitDialog() 
478 {
479         CDialog::OnInitDialog();
480
481         SetWindowText( "Editing Email: " + name );
482
483         return TRUE;  // return TRUE unless you set the focus to a control
484                       // EXCEPTION: OCX Property Pages should return FALSE
485 }
486
487 void CDialogPDAEditEmail::SetName( CString &_name )
488 {
489         name = _name;
490 }
491
492 void CDialogPDAEditEmail::SetEmail( const idDeclEmail *email )
493 {
494         to = email->GetTo();
495         from = email->GetFrom();
496         date = email->GetDate();
497         subject = email->GetSubject();
498         body = email->GetBody();
499         body.Replace("\n", "\r\n");
500
501         name = email->GetName();
502
503         if ( IsWindow( m_hWnd ) ) {
504                 UpdateData(FALSE);
505         }
506 }
507
508 CString CDialogPDAEditEmail::GetDeclText()
509 {
510         CString mungedBody = body;
511         mungedBody.Replace("\r\n\r\n", "\\n\\n\"\n\n\"");
512         mungedBody.Replace("\r\n", "\\n\"\n\"");
513
514         CString declText;
515         declText += "\n";
516         declText += "email " + name + " {\n";
517         declText += "\tto     \t\t\"" + to + "\"\n";
518         declText += "\tfrom   \t\t\"" + from + "\"\n";
519         declText += "\tdate   \t\t\"" + date + "\"\n";
520         declText += "\tsubject\t\t\"" + subject + "\"\n";
521         declText += "\ttext {\n";
522         declText += "\"" + mungedBody + "\"\n";
523         declText += "\t}\n";
524         declText += "}";
525
526         return declText;
527 }
528