ide/i18n: Added some more i18n'ed strings
[sdk] / ide / src / dialogs / NewProjectDialog.ec
1 import "ide"
2
3 FileDialog fileDialog { type = selectDir, text = $"Select project directory" };
4
5 class NewProjectDialog : Window
6 {
7    background = activeBorder;
8    minClientSize = { 316, 170 };
9    maxClientSize = { 640, 170 };
10    borderStyle = sizable;
11    tabCycle = true;
12    hasClose = true;
13    text = $"New Project";
14
15    char path[MAX_LOCATION];
16    char name[MAX_FILENAME];
17
18    PathBox locationEditBox
19    {
20       this, position = { 10, 80 }, size = { 120, 22 }, anchor = { left = 10, top = 80, right = 10 };
21       hotKey = altL, text = $"Location";
22       typeExpected = directory, browseDialog = fileDialog;
23
24       //NotifyUpdate = EditBoxUpdate;
25
26       bool NotifyModified(PathBox pathBox)
27       {
28          char location[MAX_LOCATION];
29          char lastPart[MAX_FILENAME];
30          char * text = pathBox.slashPath;
31          
32          //replacing this: NotifyUpdate = EditBoxUpdate;
33          okBtn.disabled = !(text[0] && projectName.contents[0]);
34
35          GetWorkingDir(location, sizeof(location) - 1);
36          PathCatSlash(location, text);
37          
38          GetLastDirectory(path, lastPart);
39          /*if(text[0] && (!name[0] || !strcmp(lastPart, name)))
40          {
41             char newName[MAX_FILENAME];
42             GetLastDirectory(location, newName);
43             if(strcmp(newName, location))
44             {
45                strcpy(name, newName);
46                projectName.contents = name;
47             }
48          }*/
49          strcpy(path, location);
50          pathBox.path = path;
51          return true;
52       }
53    };
54    Label { this, position = { 10, 60 }, labeledWindow = locationEditBox };
55
56    DropBox targetType { this, position = { 10, 130 }, size = { 130 }, hotKey = altT, text = $"Target Type" };
57
58    Label { this, position = { 10, 110 }, labeledWindow = targetType };
59
60    Button okBtn
61    {
62       parent = this, isDefault = true, disabled = true, position = { 170, 130 }, size = { 60 }, text = $"OK";
63       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
64       {
65          char * location = locationEditBox.slashPath;
66          char * prjName = projectName.contents;
67          char filePath[MAX_LOCATION];
68          char fileName[MAX_LOCATION];  // Windows Friendly path
69          char extension[MAX_EXTENSION];
70          FileAttribs exists;
71          Project project;
72          Workspace workspace;
73          ProjectView projectWindow;
74          ProjectConfig debug, release;
75          
76          if(!FileExists(location).isDirectory)
77          {
78             if(MessageBox { type = yesNo, master = this, 
79                   text = $"Directory doesn't exist", contents = $"Create directory?" }.Modal() == yes)
80             {
81                if(!MakeDir(location))
82                {
83                   MessageBox { type = ok, master = this, text = location, contents = $"Error creating directory" }.Modal();
84                   return true;
85                }
86             }
87             else
88                return true;
89          }
90
91          strcpy(filePath, location);
92          PathCatSlash(filePath, prjName);
93          GetExtension(filePath, extension);
94          //if(!extension[0])
95             ChangeExtension(filePath, ProjectExtension, filePath);
96
97          GetSystemPathBuffer(fileName, filePath);
98          exists = FileExists(filePath);
99
100          if(exists)
101          {
102             if(MessageBox { type = yesNo, master = this, 
103                   text = $"Project Already Exists", contents = $"Replace existing project?" }.Modal() == no)
104                return true;
105          }
106
107          debug = ProjectConfig
108          {
109             name = CopyString("Debug");
110             options = 
111             {
112                optimization = none;
113                debug = true;
114                preprocessorDefinitions = { [ CopyString("_DEBUG") ] };
115             };
116             makingModified = true;
117             compilingModified = true;
118             linkingModified = true;
119          };
120
121          release = ProjectConfig
122          {
123             name = CopyString("Release");
124             options =
125             {
126                optimization = speed;
127                debug = false;
128             };
129             makingModified = true;
130             compilingModified = true;
131             linkingModified = true;
132          };
133
134          /* error: too few arguments to function ‘__ecereProp_DirExpression_Set_char__PTR_’ -- debug.objDir = "debug";*/ 
135
136          project = Project
137          {
138             filePath = filePath;
139             moduleName = CopyString(name);
140             topNode.type = NodeTypes::project;
141             config = debug;
142          };
143          /*project.topNode.options = */project.options =
144          {
145             warnings = all;
146             // TOFIX: Precomp problems withou the extra ( )
147             targetType = ((TargetTypes)targetType.GetTag());
148             targetFileName = /*CopyString(*/name/*)*/;
149          };
150          if(project.options.targetType != staticLibrary)
151          {
152             project.options.libraries = { [ CopyString("ecere") ] };
153          }
154
155          {
156             char workspaceFile[MAX_LOCATION];
157             strcpy(workspaceFile, filePath);
158             ChangeExtension(workspaceFile, WorkspaceExtension, workspaceFile);
159             workspace = Workspace { compiler = ideSettings.defaultCompiler, workspaceFile = workspaceFile };
160          } 
161          workspace.projects.Add(project);
162
163          project.topNode.configurations = project.configurations = { [ debug, release ] };
164          project.resNode = project.topNode.Add(project, "Resources", null, resources, archiveFile, false);
165
166          if(!project.Save(filePath))
167          {
168             MessageBox { type = ok, master = this, text = filePath, contents = $"Error writing project file" }.Modal();
169             delete project;
170             return true;
171          }
172
173          projectWindow = ide.CreateProjectView(workspace, fileName);
174          
175          {
176             char newWorkingDir[MAX_LOCATION];
177             StripLastDirectory(filePath, newWorkingDir);
178
179             ide.ChangeFileDialogsDirectory(newWorkingDir, false);
180             
181             StripLastDirectory(newWorkingDir, newWorkingDir);
182             ide.ChangeProjectFileDialogDirectory(newWorkingDir);
183          }
184
185          if(createForm.checked)
186          {
187             char className[256];
188             char varName[256];
189             CodeEditor codeEditor = projectWindow.CreateNew("Form", "form", "Window", className);
190             EditBox editBox = codeEditor.editBox;
191             strcpy(varName, className);
192             if(islower(varName[0]))
193             {
194                memmove(varName+1, varName, strlen(varName)+1);
195                varName[0] = '_';
196             }
197             else
198                varName[0] = (char)tolower(varName[0]);
199
200             editBox.End();
201             editBox.Printf("\n%s %s {};\n", className, varName);
202
203             codeEditor.EnsureUpToDate();
204
205             project.Save(filePath);
206
207             projectWindow.modifiedDocument = false;
208             project.topNode.modified = false;
209
210             /*
211             editBox.Printf("class FormApplication : GuiApplication\n");
212             editBox.Printf("{\n");
213             editBox.Printf("   %s %s {};\n", className, varName);
214             editBox.Printf("}\n");
215             editBox.Home();
216             */
217          }
218
219          if(project && projectWindow)
220          {
221             CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
222             ProjectConfig config = project.config;
223             projectWindow.ProjectPrepareMakefile(project, force, true, true, compiler, config);
224             delete compiler;
225          }
226
227          Destroy(0);
228          return true;
229       }
230    };
231    
232    Button
233    {
234       parent = this, position = { 240, 130 }, size = { 60 }, hotKey = escape, text = $"Cancel";
235       NotifyClicked = ButtonCloseDialog;
236    };
237    Button createForm
238    {
239       parent = this, text = $"Create Form", hotKey = altF, position = { 200, 30 };
240       isCheckbox = true, checked = true;
241    };
242    
243    EditBox projectName
244    {
245       parent = this, textHorzScroll = true, position = { 10, 30 }, size = { 160 };
246       hotKey = altP, text = $"Project Name";
247       NotifyUpdate = EditBoxUpdate;
248
249       bool NotifyModified(EditBox editBox)
250       {
251          char name[MAX_FILENAME];
252          char lastPart[MAX_LOCATION];
253          char * text = editBox.contents;
254
255          // drop leading path stuff that has no business here
256          GetLastDirectory(text, name);
257
258          GetLastDirectory(path, lastPart);
259          if(name[0] && (!strcmp(path, lastPart) || !path[0] || !strcmp(this.name, lastPart)))
260          {
261             if(strcmp(path, lastPart))
262                StripLastDirectory(path, path);
263             PathCatSlash(path, name);
264             locationEditBox.path = path;
265          }
266          else if(!name[0] && !strcmp(this.name, lastPart))
267          {
268             if(strcmp(path, lastPart))
269                StripLastDirectory(path, path);
270             locationEditBox.path = path;
271          }
272          else if(strcmp(name, lastPart))
273          {
274             PathCatSlash(path, name);
275             locationEditBox.path = path;
276          }
277          strcpy(this.name, name);
278          editBox.contents = name;
279          return true;
280       }
281    };
282    Label { this, position = { 10, 10 }, labeledWindow = projectName };
283
284    void EditBoxUpdate(EditBox editBox)
285    {
286       okBtn.disabled = !(locationEditBox.path[0] && projectName.contents[0]);
287    }
288
289    NewProjectDialog()
290    {
291       char location[MAX_LOCATION];
292
293       if(ideSettings.ideProjectFileDialogLocation)
294          strcpy(location, ideSettings.ideProjectFileDialogLocation);
295       else
296          GetWorkingDir(location, sizeof(location) - 1);
297
298       locationEditBox.path = location;
299       strcpy(path, location);
300
301       {
302          DataRow row;
303
304          //targetType.AddField("String", null, 0);
305
306          row = targetType.AddRow();
307          row.tag = TargetTypes::executable;
308          row.SetData(null, $"Executable");
309
310          row = targetType.AddRow();
311          row.tag = TargetTypes::sharedLibrary;
312          row.SetData(null, $"Shared Library");
313
314          row = targetType.AddRow();
315          row.tag = TargetTypes::staticLibrary;
316          row.SetData(null, $"Static Library");
317
318          targetType.currentRow = targetType.FindRow(TargetTypes::executable);
319       }
320    }
321 }
322
323 class QuickProjectDialog : Window
324 {
325    background = activeBorder;
326    minClientSize = { 316, 110 };
327    maxClientSize = { 640, 110 };
328    borderStyle = sizable;
329    tabCycle = true;
330    hasClose = true;
331    text = $"Quick Project";
332
333    char path[MAX_LOCATION];
334    char name[MAX_FILENAME];
335
336    Label message { this, position = { 10, 10 }, size = { 200 }, text = $"Do you want to quickly create a temporary project?" };
337
338    DropBox targetType { this, position = { 10, 70 }, size = { 130 }, hotKey = altT, text = $"Target Type" };
339    Label { this, position = { 10, 50 }, labeledWindow = targetType };
340
341    Button okBtn
342    {
343       parent = this, isDefault = true, position = { 170, 70 }, size = { 60 }, text = $"OK";
344       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
345       {
346          char tempDir[MAX_LOCATION] = "";
347          char filePath[MAX_LOCATION];
348          char prjName[MAX_LOCATION] = "quick_project";
349          Project project;
350          Workspace workspace;
351          ProjectView projectWindow;
352          ProjectConfig debug, release;
353
354          /*if(!*/CreateTemporaryDir(tempDir, "ecereide");/*)
355          {
356             MessageBox { type = ok, master = this, text = tempDir, contents = "Error creating temporary directory." }.Modal();
357             return true;
358          }*/
359
360          GetLastDirectory(tempDir, prjName);
361          {
362             char * dot;
363             if((dot = strstr(prjName, ".")))
364                dot[0] = '_';
365          }
366
367          if(!FileExists(tempDir).isDirectory)
368          {
369             MessageBox { type = ok, master = this, text = tempDir, contents = $"Temporary directory does not exist." }.Modal();
370             return true;
371          }
372          
373          ide.tmpPrjDir = tempDir;
374
375          strcpy(filePath, tempDir);
376          PathCatSlash(filePath, prjName);
377          //GetExtension(filePath, extension);
378          //if(!extension[0])
379             ChangeExtension(filePath, ProjectExtension, filePath);
380          GetSystemPathBuffer(filePath, filePath);
381
382          debug = ProjectConfig
383          {
384             name = CopyString("Debug");
385             options = 
386             {
387                optimization = none;
388                debug = true;
389                preprocessorDefinitions = { [ CopyString("_DEBUG") ] };
390             };
391             makingModified = true;
392             compilingModified = true;
393             linkingModified = true;
394          };
395
396          release = ProjectConfig
397          {
398             name = CopyString("Release");
399             options =
400             {
401                optimization = speed;
402                debug = false;
403             };
404             makingModified = true;
405             compilingModified = true;
406             linkingModified = true;
407          };
408
409          project = Project
410          {
411             filePath = filePath;
412             moduleName = CopyString(prjName);
413             topNode.type = NodeTypes::project;
414             config = debug;
415          };
416          /*project.topNode.options = */project.options =
417          {
418             warnings = all;
419             // TOFIX: Precomp problems withou the extra ( )
420             targetType = ((TargetTypes)targetType.GetTag());
421             targetFileName = /*CopyString(*/prjName/*)*/;
422          };
423
424          if(project.options.targetType != staticLibrary)
425          {
426             project.options.libraries = { [ CopyString("ecere") ] };
427          }
428
429          {
430             char workspaceFile[MAX_LOCATION];
431             strcpy(workspaceFile, filePath);
432             ChangeExtension(workspaceFile, WorkspaceExtension, workspaceFile);
433             workspace = Workspace { compiler = ideSettings.defaultCompiler, workspaceFile = workspaceFile };
434          } 
435
436          workspace.projects.Add(project);
437
438          project.topNode.configurations = project.configurations = { [ debug, release ] };
439          project.resNode = project.topNode.Add(project, "Resources", null, resources, archiveFile, false);
440
441          if(!project.Save(filePath))
442          {
443             MessageBox { type = ok, master = this, text = filePath, contents = $"Error writing project file" }.Modal();
444             delete project;
445             delete workspace;
446             ide.DestroyTemporaryProjectDir();
447             return true;
448          }
449
450          projectWindow = ide.CreateProjectView(workspace, filePath);
451
452          {
453             char extension[MAX_EXTENSION];
454             Window document = null;
455             for(document = ide.firstChild; document; document = document.next)
456             {
457                char * fileName = document.fileName;
458                if(document.isDocument && document._class == class(CodeEditor) && document.created && fileName)
459                {
460                   CodeEditor codeEditor = (CodeEditor)document;
461                   ide.projectView.AddFile(project.topNode, fileName, false, false);
462                   codeEditor.DebugMenusDisabled();
463                }
464             }
465          }
466
467          if(project.topNode.modified)
468          {
469             project.Save(filePath);
470             projectWindow.modifiedDocument = false;
471             project.topNode.modified = false;
472          }
473          else
474          {
475             char className[256];
476             char varName[256];
477             CodeEditor codeEditor = projectWindow.CreateNew("Form", "form", "Window", className);
478             EditBox editBox = codeEditor.editBox;
479             strcpy(varName, className);
480             if(islower(varName[0]))
481             {
482                memmove(varName+1, varName, strlen(varName)+1);
483                varName[0] = '_';
484             }
485             else
486                varName[0] = (char)tolower(varName[0]);
487
488             editBox.End();
489             editBox.Printf("\n%s %s {};\n", className, varName);
490
491             codeEditor.EnsureUpToDate();
492
493             // TODO: how to save that new file?
494
495             project.Save(filePath);
496             projectWindow.modifiedDocument = false;
497             project.topNode.modified = false;
498          }
499
500          visible = false;
501
502          if(project && projectWindow)
503          {
504             CompilerConfig compiler = ideSettings.GetCompilerConfig(ide.workspace.compiler);
505             ProjectConfig config = project.config;
506             projectWindow.ProjectPrepareMakefile(project, force, true, true, compiler, config);
507             delete compiler;
508          }
509
510          ide.projectView.ProjectBuild(null, Modifiers { });
511
512          Destroy(0);
513          return true;
514       }
515    };
516    
517    Button
518    {
519       parent = this, position = { 240, 70 }, size = { 60 }, hotKey = escape, text = $"Cancel";
520       NotifyClicked = ButtonCloseDialog;
521    };
522    
523    QuickProjectDialog()
524    {
525       DataRow row;
526
527       row = targetType.AddRow();
528       row.tag = TargetTypes::executable;
529       row.SetData(null, $"Executable");
530
531       row = targetType.AddRow();
532       row.tag = TargetTypes::sharedLibrary;
533       row.SetData(null, $"Shared Library");
534
535       row = targetType.AddRow();
536       row.tag = TargetTypes::staticLibrary;
537       row.SetData(null, $"Static Library");
538
539       targetType.currentRow = targetType.FindRow(TargetTypes::executable);
540    }
541 }