ide: fix lack of support of variables in node path for find in files.
[sdk] / ide / src / dialogs / NodeProperties.ec
1 import "ide"
2
3 enum NodePropertiesMode { normal, newFile, newFolder };
4
5 class NodeProperties : Window
6 {
7    tabCycle = true;
8    background = formColor;
9    hasClose = true;
10    borderStyle = sizable;
11    isModal = true;
12    text = $"Properties";
13    //size = { 280, 260 };
14
15    ProjectNode node, topNode;
16    NodePropertiesMode mode;
17
18    Label pathLabel { parent = this, position = { 10, 60 }, labeledWindow = path };
19    EditBox path
20    {
21       this, textHorzScroll = true, position = { 10, 80 }, size = { 260 }, text = $"Path";
22       anchor = { left = 10, top = 80, right = 10 };
23       NotifyModified = PathNotifyModified;
24    };
25
26    Label absolutePathLabel { parent = this, position = { 10, 110 }, labeledWindow = absolutePath };
27    EditBox absolutePath
28    {
29       this, background = formColor, textHorzScroll = true, readOnly = true;
30       position = { 10, 130 }, size = { 260 }, text = $"Absolute Path";
31       anchor = { left = 10, top = 130, right = 10 };
32    };
33
34    Label nameLabel { parent = this, position = { 10, 10 }, labeledWindow = name };
35    EditBox name
36    {
37       this, textHorzScroll = true, position = { 10, 30 }, size = { 260 }, text = $"Name";
38       anchor = { left = 10, top = 30, right = 10 };
39       NotifyModified = NameNotifyModified;
40    };
41
42    bool NameNotifyModified(EditBox editBox)
43    {
44       char filePath[MAX_LOCATION];
45       char * oldName = node.name;
46       Map<Platform, SetBool> exclusionInfo { };
47
48       node.name = null;
49       GetLastDirectory(name.contents, filePath);
50       topNode.CollectExclusionInfo(exclusionInfo, null);
51       if(topNode.FindSameNameConflict(name.contents, false, exclusionInfo, null))
52       {
53          MessageBox { type = ok, master = this, text = filePath, contents = $"File with same name already in project." }.Modal();
54          node.name = oldName;
55          return false;
56       }
57       delete exclusionInfo;
58       delete oldName;
59       node.name = CopyString(filePath);
60       if(node.type == file)
61          node.icon = NodeIcons::SelectFileIcon(filePath);
62       {
63          char * s;
64          text = (s = PrintString(node.name, $" Properties"));
65          delete s;
66       }
67       if(node.type == folder)
68       {
69          strcpy(filePath, (node.parent.type == project) ? "" : node.parent.path);
70          PathCatSlash(filePath, node.name);
71          delete node.path;
72          node.path = CopyString(filePath);
73          MakeSystemPath(filePath);
74          path.contents = filePath;
75       }
76
77       UpdateFileName();
78       return true;
79    }
80
81    bool PathNotifyModified(EditBox editBox)
82    {
83       delete node.path;
84       node.path = CopyUnixPath(path.contents);
85
86       if(node.type == resources)
87       {
88          master.modifiedDocument = true;
89          node.project.topNode.modified = true;
90          ide.projectView.Update(null);
91       }
92       else
93          UpdateFileName();
94       return true;
95    }
96
97    property ProjectNode node
98    {
99       set
100       {
101          Size minSize = { 280, 260 };
102
103          // name              { 10,  10 }    { 10,  30 }
104          // path              { 10,  60 }    { 10,  80 }
105          // absolutePath      { 10, 110 }    { 10, 130 }
106
107          node = value;
108          if(node)
109          {
110             for(topNode = node; topNode && topNode.parent; topNode = topNode.parent);
111
112             switch(node.type)
113             {
114                case project:
115                {
116                   minSize.h = 160;
117                   QuickReadOnly(path);
118                   break;
119                }
120                case file:
121                {
122                   minSize.h = 160;
123                   break;
124                }
125                case folder:
126                case resources:
127                {
128                   minSize.h = 110;
129                   absolutePath.visible = false;
130                   absolutePathLabel.visible = false;
131                   if(node.type == resources) QuickReadOnly(name);
132                   break;
133                }
134             }
135
136             // project:    - name path absolute -
137             // file
138             //   in res:   - name path absolute -
139             //   not:      - name path absolute -
140             // folder:     - name               -
141             // resources:  - name path          -
142
143             {
144                char * s;
145                text = (s = PrintString(node.name, $" Properties"));
146                delete s;
147             }
148             name.contents = node.name;
149             {
150                char temp[MAX_LOCATION];
151                path.contents = node.path ? GetSystemPathBuffer(temp, node.path) : "";
152             }
153             if(node.type != folder && node.type != resources)
154             {
155                char filePath[MAX_LOCATION];
156                GetSystemPathBuffer(filePath, topNode.path);
157                if(node.path) PathCat(filePath, node.path);
158                PathCat(filePath, node.name);
159                if(node.type == project)
160                   ChangeExtension(filePath, ProjectExtension, filePath);
161                absolutePath.contents = filePath;
162             }
163          }
164          minClientSize = minSize;
165       }
166    }
167
168    void QuickReadOnly(EditBox editBox)
169    {
170       editBox.textHorzScroll = true;
171       editBox.readOnly = true;
172       editBox.background = formColor;
173    }
174
175    bool OnKeyDown(Key key, unichar ch)
176    {
177       if(key == escape || key.code == enter || key.code == keyPadEnter)
178       {
179          StopEditing();
180          if(key.code == enter || key.code == keyPadEnter)
181          {
182             if(mode == newFile)
183             {
184                char filePath[MAX_LOCATION];
185                Window document;
186                node.GetFullFilePath(filePath, true, true);
187                if(FileExists(filePath))
188                   ide.projectView.OpenNode(node, key.ctrl && key.shift);
189                else
190                {
191                   document = (Window)NewCodeEditor(ide, normal, false);
192                   document.NotifySaved = ide.DocumentSaved;
193                   MakeSystemPath(filePath);
194                   document.fileName = filePath;
195                }
196             }
197             if(mode == newFile || mode == newFolder)
198             {
199                ide.projectView.modifiedDocument = true;
200                node.project.topNode.modified = true;
201                if(mode == newFile)
202                   node.project.ModifiedAllConfigs(true, false, true, true);
203             }
204          }
205          else if(mode == newFile || mode == newFolder)
206          {
207             ide.projectView.DeleteNode(node);
208             node = null;
209          }
210          Destroy(0);
211          return false;
212       }
213       return true;
214    }
215
216    bool OnClose(bool parentClosing)
217    {
218       StopEditing();
219       return true;
220    }
221
222    void StopEditing()
223    {
224       // tocheck: do we have a better way of doing this?
225       bool b;
226       name.OnActivate(false, null, &b, false);
227       path.OnActivate(false, null, &b, false);
228    }
229
230    void UpdateFileName()
231    {
232       char filePath[MAX_LOCATION];
233       Project prj = node.project;
234
235       GetSystemPathBuffer(filePath, topNode.path);
236       PathCat(filePath, node.path);
237       PathCat(filePath, node.name);
238       if(node.type == project)
239       {
240          ChangeExtension(filePath, ProjectExtension, filePath);
241          absolutePath.contents = filePath;
242          master.fileName = filePath;
243       }
244       else
245          absolutePath.contents = filePath;
246
247       name.modifiedDocument = false;
248       if(path)
249          path.modifiedDocument = false;
250       if(absolutePath)
251          absolutePath.modifiedDocument = false;
252
253       if(prj && mode == normal)
254       {
255          ide.projectView.modifiedDocument = true;
256          prj.topNode.modified = true;
257          ide.projectView.Update(null);
258          if(node.type == file)
259             prj.ModifiedAllConfigs(true, false, false, true);
260       }
261    }
262 }