Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / ryzom / tools / leveldesign / georges_dll / dfn_dialog.cpp
blobca6c073d66e95faa2656cf71143346ed59f16a16
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2019 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 // dfn_dialog.cpp : implementation file
23 #include "stdafx.h"
24 #include "georges_edit.h"
25 #include "georges_edit_view.h"
26 #include "georges_edit_doc.h"
27 #include "dfn_dialog.h"
28 #include "left_view.h"
29 #include "action.h"
31 #include "nel/misc/path.h"
32 #include "nel/georges/type.h"
34 using namespace std;
35 using namespace NLMISC;
36 using namespace NLGEORGES;
39 // ***************************************************************************
41 CDfnDialog::CDfnDialog () : CBaseDialog (IDR_MAINFRAME)
43 //{{AFX_DATA_INIT(CDfnDialog)
44 // NOTE: the ClassWizard will add member initialization here
45 //}}AFX_DATA_INIT
46 View = NULL;
49 // ***************************************************************************
51 void CDfnDialog::DoDataExchange(CDataExchange* pDX)
53 CDialog::DoDataExchange(pDX);
54 //{{AFX_DATA_MAP(CDfnDialog)
55 // NOTE: the ClassWizard will add DDX and DDV calls here
56 //}}AFX_DATA_MAP
59 // ***************************************************************************
61 BEGIN_MESSAGE_MAP(CDfnDialog, CDialog)
62 //{{AFX_MSG_MAP(CDfnDialog)
63 ON_WM_SIZE()
64 //}}AFX_MSG_MAP
65 END_MESSAGE_MAP()
67 // ***************************************************************************
69 void CDfnDialog::OnSize(UINT nType, int cx, int cy)
71 CBaseDialog::OnSize(nType, cx, cy);
73 // TODO: Add your message handler code here
77 // ***************************************************************************
79 BOOL CDfnDialog::OnInitDialog()
81 CBaseDialog::OnInitDialog();
83 RECT viewRect;
84 View->GetClientRect (&viewRect);
85 uint virtualWidth = std::max ((uint)MinViewWidth, (uint)(viewRect.right-viewRect.left));
87 // Refresh sizes
88 CBaseDialog::resizeWidgets (virtualWidth, 0);
90 // Get first item coordinate
91 RECT currentPos;
92 getFirstItemPos (currentPos);
94 // Create the type combo
95 setStaticSize (currentPos);
96 LabelParents.Create (_T("Parents:"), WS_VISIBLE, currentPos, this);
97 initWidget (LabelParents);
98 getNextPosLabel (currentPos);
100 setListSize (currentPos, SmallWidget, ParentHeight);
101 Parents.create (WS_TABSTOP, currentPos, this, LtParents);
102 Parents.insertColumn (0, _T("Parent Dfn"));
103 Parents.Dialog = this;
104 Parents.recalcColumn ();
105 initWidget (Parents);
106 getNextPos (currentPos);
109 // Create the type combo
110 setStaticSize (currentPos);
111 LabelStruct.Create (_T("Structure:"), WS_VISIBLE, currentPos, this);
112 initWidget (LabelStruct);
113 getNextPosLabel (currentPos);
115 setListSize (currentPos, Width, DfnHeight);
116 Struct.create (WS_TABSTOP, currentPos, this, LtStruct);
117 Struct.insertColumn (0, _T("Name"));
118 Struct.insertColumn (1, _T("Type"));
119 Struct.insertColumn (2, _T("Value"));
120 Struct.insertColumn (3, _T("Default"));
121 Struct.insertColumn (4, _T("FilenameExt"));
122 Struct.Dialog = this;
123 Struct.recalcColumn ();
124 initWidget (Struct);
125 getNextPos (currentPos);
127 registerLastControl ();
129 resizeWidgets ();
131 UpdateData (FALSE);
133 return TRUE; // return TRUE unless you set the focus to a control
134 // EXCEPTION: OCX Property Pages should return FALSE
137 // ***************************************************************************
139 void CDfnDialog::OnOK ()
141 Parents.UpdateData ();
142 Struct.UpdateData ();
144 CWnd *focusWnd = CWnd::GetFocus ();
145 if (focusWnd)
147 focusWnd = focusWnd->GetParent ();
148 if (focusWnd)
150 if (focusWnd->m_hWnd == Parents.m_hWnd)
152 Parents.onOK ();
154 else if (focusWnd->m_hWnd == Struct.m_hWnd)
156 Struct.onOK ();
162 // ***************************************************************************
164 void CDfnDialog::OnCancel ()
166 CWnd *focusWnd = CWnd::GetFocus ();
167 if (focusWnd)
169 focusWnd = focusWnd->GetParent ();
170 if (focusWnd)
172 if (focusWnd->m_hWnd == Parents.m_hWnd)
174 Parents.onCancel ();
175 return;
177 else if (focusWnd->m_hWnd == Struct.m_hWnd)
179 Struct.onCancel ();
180 return;
184 CBaseDialog::OnCancel ();
187 // ***************************************************************************
189 void CDfnDialog::getFromDocument (const NLGEORGES::CFormDfn &dfn)
191 if (View)
193 // Add the parents
194 Parents.ListCtrl.DeleteAllItems ();
195 uint parent;
196 for (parent=0; parent<dfn.getNumParent (); parent++)
198 // Add the label and value
199 Parents.ListCtrl.InsertItem(parent, nlUtf8ToTStr(dfn.getParentFilename(parent)));
202 // Add the struct element
203 Struct.ListCtrl.DeleteAllItems ();
204 uint elm;
205 for (elm=0; elm<dfn.getNumEntry (); elm++)
207 // Add the label and value
208 Struct.ListCtrl.InsertItem(elm, nlUtf8ToTStr(dfn.getEntry(elm).getName()));
209 switch (elm, dfn.getEntry (elm).getType ())
211 case UFormDfn::EntryType:
212 Struct.ListCtrl.SetItemText (elm, 1, dfn.getEntry (elm).getArrayFlag () ? _T("Type array") : _T("Type"));
213 Struct.ListCtrl.SetItemText(elm, 4, nlUtf8ToTStr(dfn.getEntry(elm).getFilenameExt()));
214 break;
215 case UFormDfn::EntryDfn:
216 Struct.ListCtrl.SetItemText (elm, 1, dfn.getEntry (elm).getArrayFlag () ? _T("Dfn array") : _T("Dfn"));
217 break;
218 case UFormDfn::EntryVirtualDfn:
219 Struct.ListCtrl.SetItemText (elm, 1, _T("Virtual Dfn"));
220 break;
222 Struct.ListCtrl.SetItemText(elm, 2, nlUtf8ToTStr(dfn.getEntry(elm).getFilename()));
223 Struct.ListCtrl.SetItemText(elm, 3, nlUtf8ToTStr(dfn.getEntry(elm).getDefault()));
228 // ***************************************************************************
230 void CDfnDialog::setParentsToDocument ()
232 CGeorgesEditDoc *doc = View->GetDocument ();
233 if (doc)
235 // Build a vector of string
236 vector<string> vectValue (Parents.ListCtrl.GetItemCount ());
238 // For each string
239 uint parent;
240 for (parent=0; parent<(uint)Parents.ListCtrl.GetItemCount (); parent++)
242 // Add the label and value
243 CString str = Parents.ListCtrl.GetItemText ( parent, 0);
244 vectValue[parent] = tStrToUtf8(str);
247 // Modify the document
248 if (!doc->modify (new CActionStringVector (IAction::DfnParents, vectValue, *doc, "", doc->getLeftView ()->getCurrentSelectionId (), 0)))
249 getFromDocument (*doc->getDfnPtr ());
253 // ***************************************************************************
255 void CDfnDialog::setStructToDocument ()
257 CGeorgesEditDoc *doc = View->GetDocument ();
258 if (doc)
260 // Add the entries
261 vector<vector<string> > stringVector (Struct.ListCtrl.GetItemCount ());
262 uint elm;
263 for (elm=0; elm<stringVector.size (); elm++)
265 // Resize the array
266 stringVector[elm].resize (5);
268 uint subElm;
269 for (subElm = 0; subElm<5; subElm++)
271 // Get the name
272 CString name= Struct.ListCtrl.GetItemText (elm, subElm);
273 stringVector[elm][subElm] = tStrToUtf8(name);
276 doc->modify (new CActionStringVectorVector (IAction::DfnStructure, stringVector, *doc, doc->getLeftView ()->getCurrentSelectionId (), 0));
280 // ***************************************************************************
282 LRESULT CDfnDialog::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
284 switch (message)
286 case LC_CHANGE:
287 if (wParam == LtParents)
288 setParentsToDocument ();
289 else
290 setStructToDocument ();
291 break;
294 return CDialog::WindowProc(message, wParam, lParam);
297 // ***************************************************************************
299 CEditListCtrl::TItemEdit CDfnEditListCtrl::getItemEditMode (uint item, uint subItem)
301 if (subItem == 0)
302 return CEditListCtrl::EditMemCombo;
303 else if (subItem == 1)
304 return CEditListCtrl::EditFixedCombo;
305 else if (subItem == 2)
307 string type = tStrToUtf8(ListCtrl.GetItemText (item, 1));
308 if (type != "Virtual Dfn")
309 return CEditListCtrl::EditMemCombo;
311 else if (subItem == 3)
313 string type = tStrToUtf8(ListCtrl.GetItemText (item, 1));
314 if ((type == "Type") || (type == "Type array"))
315 return CEditListCtrl::EditMemCombo;
317 else if (subItem == 4)
319 string type = tStrToUtf8(ListCtrl.GetItemText (item, 1));
320 if ((type == "Type") || (type == "Type array"))
321 return CEditListCtrl::EditMemCombo;
323 return CEditListCtrl::NoEdit;
326 // ***************************************************************************
328 void CDfnEditListCtrl::getComboBoxStrings (uint item, uint subItem, std::vector<std::string> &retStrings)
330 if (subItem == 1)
332 retStrings.reserve (5);
333 retStrings.push_back ("Type");
334 retStrings.push_back ("Dfn");
335 retStrings.push_back ("Virtual Dfn");
336 retStrings.push_back ("Type array");
337 retStrings.push_back ("Dfn array");
341 // ***************************************************************************
343 void CDfnEditListCtrl::getMemComboBoxProp (uint item, uint subItem, std::string &regAdr, bool &browse)
345 if (subItem == 0)
347 browse = false;
348 regAdr = tStrToUtf8(GEORGES_EDIT_BASE_REG_KEY _T("\\Label MemCombo"));
350 else if (subItem == 2)
352 browse = true;
354 // Get type string
355 string type = tStrToUtf8(ListCtrl.GetItemText (item, 1));
356 if ((type == "Type") || (type == "Type array"))
357 regAdr = tStrToUtf8(GEORGES_EDIT_BASE_REG_KEY _T("\\Type MemCombo"));
358 else if ((type == "Dfn") || (type == "Dfn array"))
359 regAdr = tStrToUtf8(GEORGES_EDIT_BASE_REG_KEY _T("\\Dfn MemCombo"));
361 else if (subItem == 3)
363 browse = false;
364 regAdr = tStrToUtf8(GEORGES_EDIT_BASE_REG_KEY _T("\\Default MemCombo"));
366 else if (subItem == 3)
368 browse = false;
369 regAdr = tStrToUtf8(GEORGES_EDIT_BASE_REG_KEY _T("\\FilenameExt MemCombo"));
373 // ***************************************************************************
375 void CDfnEditListCtrl::getNewItemText (uint item, uint subItem, std::string &ret)
377 if (subItem == 0)
378 ret = "new";
379 else if (subItem == 1)
380 ret = "Type";
381 else if (subItem == 2)
382 ret = theApp.DefaultType;
383 else if (subItem == 3)
384 ret.clear();
385 else if (subItem == 4)
386 ret.clear();
389 // ***************************************************************************
391 void CDfnEditListCtrl::getBrowseInfo (uint item, uint subItem, std::string &defExt, std::string &defFilename, std::string &defDir, NLMISC::tstring &filter)
393 if (subItem == 2)
395 // Get type string
396 string type = tStrToUtf8(ListCtrl.GetItemText (item, 1));
397 if ((type == "Type") || (type == "Type array"))
399 filter = TypeFilter;
400 defDir = theApp.RootSearchPath;
401 defFilename = "*.typ";
402 defExt = "*.typ";
404 else if ((type == "Dfn") || (type == "Dfn array"))
406 filter = DfnFilter;
407 defDir = theApp.RootSearchPath;
408 defFilename = "*.dfn";
409 defExt = "*.dfn";
414 // ***************************************************************************
416 void CDfnEditListCtrl::onItemChanged (uint item, uint subItem)
418 if (subItem == 1)
420 // Get type string
421 string type = tStrToUtf8(ListCtrl.GetItemText (item, 1));
422 if ((type == "Type") || (type == "Type array"))
424 CString str;
425 str = Dialog->Struct.ListCtrl.GetItemText (item, 2);
426 std::string ext = NLMISC::CFile::getExtension(tStrToUtf8(str));
427 if (ext == "typ")
428 Dialog->Struct.ListCtrl.SetItemText(item, 2, nlUtf8ToTStr(theApp.DefaultType));
430 else if ((type == "Dfn") || (type == "Dfn array"))
432 CString str;
433 str = Dialog->Struct.ListCtrl.GetItemText (item, 2);
434 std::string ext = NLMISC::CFile::getExtension(tStrToUtf8(str));
435 if (ext == "dfn")
436 Dialog->Struct.ListCtrl.SetItemText(item, 2, nlUtf8ToTStr(theApp.DefaultDfn));
438 // Clear default value
439 Dialog->Struct.ListCtrl.SetItemText (item, 3, _T(""));
441 else if (type == "Virtual Dfn")
443 // Clear the value
444 Dialog->Struct.ListCtrl.SetItemText (item, 2, _T(""));
446 // Clear default value
447 Dialog->Struct.ListCtrl.SetItemText (item, 3, _T(""));
452 // ***************************************************************************
454 void CDfnDialog::onOpenSelected ()
456 Parents.UpdateData ();
457 Struct.UpdateData ();
459 CWnd *focusWnd = CWnd::GetFocus ();
460 if (focusWnd)
462 focusWnd = focusWnd->GetParent ();
463 if (focusWnd)
465 if (focusWnd->m_hWnd == Parents.m_hWnd)
467 // For each selected
468 POSITION pos = Parents.ListCtrl.GetFirstSelectedItemPosition();
469 while (pos)
471 int nItem = Parents.ListCtrl.GetNextSelectedItem(pos);
473 // Get the string
474 CString str = Parents.ListCtrl.GetItemText (nItem, 0);
475 if (str != "")
477 // Look for the file
478 CString name = nlUtf8ToTStr(CPath::lookup(tStrToUtf8(str), false, false));
479 if (name.IsEmpty())
480 name = str;
482 // Open the file
483 theApp.OpenDocumentFile(name);
487 else if (focusWnd->m_hWnd == Struct.m_hWnd)
489 // For each selected
490 POSITION pos = Struct.ListCtrl.GetFirstSelectedItemPosition();
491 while (pos)
493 int nItem = Struct.ListCtrl.GetNextSelectedItem(pos);
495 // Get the string
496 CString str = Struct.ListCtrl.GetItemText (nItem, 2);
497 if (str != "")
499 // Look for the file
500 CString name = nlUtf8ToTStr(CPath::lookup(tStrToUtf8(str), false, false));
501 if (name.IsEmpty())
502 name = str;
504 // Open the file
505 theApp.OpenDocumentFile(name);
513 // ***************************************************************************
515 void CDfnDialog::OnSetFocus(CWnd* pOldWnd)
517 CBaseDialog::OnSetFocus(pOldWnd);
520 // ***************************************************************************
522 CWnd* CDfnDialog::GetNextDlgTabItem( CWnd* pWndCtl, BOOL bPrevious) const
524 return NULL;
527 // ***************************************************************************
529 void CDfnDialog::onFirstFocus ()
531 View->SetFocus ();
534 // ***************************************************************************
536 void CDfnDialog::onLastFocus ()
538 View->setFocusLeftView ();
541 // ***************************************************************************
543 void CDfnDialog::resizeWidgets ()
545 RECT viewRect;
546 View->GetClientRect (&viewRect);
547 uint virtualWidth = std::max ((uint)MinViewWidth, (uint)(viewRect.right-viewRect.left));
549 // Refresh sizes
550 CBaseDialog::resizeWidgets (virtualWidth, 0);
552 // Get first item coordinate
553 RECT currentPos;
554 getFirstItemPos (currentPos);
555 setStaticSize (currentPos);
556 getNextPosLabel (currentPos);
557 setListSize (currentPos, SmallWidget, ParentHeight);
558 getNextPos (currentPos);
559 setStaticSize (currentPos);
560 getNextPosLabel (currentPos);
561 setListSize (currentPos, Width, DfnHeight);
562 getNextPos (currentPos);
564 // Refresh sizes
565 CBaseDialog::resizeWidgets (virtualWidth, currentPos.bottom);
567 // Get first item coordinate
568 currentPos;
569 getFirstItemPos (currentPos);
571 // Create the type combo
572 setStaticSize (currentPos);
573 LabelParents.SetWindowPos (NULL, currentPos.left, currentPos.top, currentPos.right - currentPos.left,
574 currentPos.bottom - currentPos.top, SWP_NOZORDER|SWP_NOOWNERZORDER);
575 getNextPosLabel (currentPos);
577 setListSize (currentPos, SmallWidget, ParentHeight);
578 Parents.SetWindowPos (NULL, currentPos.left, currentPos.top, currentPos.right - currentPos.left,
579 currentPos.bottom - currentPos.top, SWP_NOZORDER|SWP_NOOWNERZORDER);
580 Parents.recalcColumn ();
581 getNextPos (currentPos);
583 // Create the type combo
584 setStaticSize (currentPos);
585 LabelStruct.SetWindowPos (NULL, currentPos.left, currentPos.top, currentPos.right - currentPos.left,
586 currentPos.bottom - currentPos.top, SWP_NOZORDER|SWP_NOOWNERZORDER);
587 getNextPosLabel (currentPos);
589 setListSize (currentPos, Width, DfnHeight + AdjusteHeight);
590 Struct.SetWindowPos (NULL, currentPos.left, currentPos.top, currentPos.right - currentPos.left,
591 currentPos.bottom - currentPos.top, SWP_NOZORDER|SWP_NOOWNERZORDER);
592 Struct.recalcColumn ();
593 getNextPos (currentPos);
595 // Resize the current view
596 View->setViewSize (
597 virtualWidth,
598 currentPos.bottom+CGeorgesEditView::WidgetTopMargin+CGeorgesEditView::WidgetBottomMargin);
601 // ***************************************************************************
602 // CDfnParentEditListCtrl
603 // ***************************************************************************
605 CEditListCtrl::TItemEdit CDfnParentEditListCtrl::getItemEditMode (uint item, uint subItem)
607 return CEditListCtrl::EditMemCombo;
610 // ***************************************************************************
612 void CDfnParentEditListCtrl::getMemComboBoxProp (uint item, uint subItem, std::string &regAdr, bool &browse)
614 browse = true;
615 regAdr = tStrToUtf8(GEORGES_EDIT_BASE_REG_KEY _T("\\Dfn MemCombo"));
618 // ***************************************************************************
620 void CDfnParentEditListCtrl::getNewItemText (uint item, uint subItem, std::string &ret)
622 ret = theApp.DefaultDfn;
625 // ***************************************************************************
627 void CDfnParentEditListCtrl::getBrowseInfo (uint item, uint subItem, std::string &defExt, std::string &defFilename, std::string &defDir, NLMISC::tstring &filter)
629 filter = DfnFilter;
630 defDir = theApp.RootSearchPath;
631 defFilename = "*.dfn";
632 defExt = "*.dfn";