Initial git commit -- Transition from CodeGuard repository
[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.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             projectWindow.ProjectPrepareMakefile(project, force, true, true);
221
222          Destroy(0);
223          return true;
224       }
225    };
226    
227    Button
228    {
229       parent = this, position = { 240, 130 }, size = { 60 }, hotKey = escape, text = "Cancel";
230       NotifyClicked = ButtonCloseDialog;
231    };
232    Button createForm
233    {
234       parent = this, text = "Create Form", hotKey = altF, position = { 200, 30 };
235       isCheckbox = true, checked = true;
236    };
237    
238    EditBox projectName
239    {
240       parent = this, textHorzScroll = true, position = { 10, 30 }, size = { 160 };
241       hotKey = altP, text = "Project Name";
242       NotifyUpdate = EditBoxUpdate;
243
244       bool NotifyModified(EditBox editBox)
245       {
246          char name[MAX_FILENAME];
247          char lastPart[MAX_LOCATION];
248          char * text = editBox.contents;
249
250          // drop leading path stuff that has no business here
251          GetLastDirectory(text, name);
252
253          GetLastDirectory(path, lastPart);
254          if(name[0] && (!strcmp(path, lastPart) || !path[0] || !strcmp(this.name, lastPart)))
255          {
256             if(strcmp(path, lastPart))
257                StripLastDirectory(path, path);
258             PathCatSlash(path, name);
259             locationEditBox.path = path;
260          }
261          else if(!name[0] && !strcmp(this.name, lastPart))
262          {
263             if(strcmp(path, lastPart))
264                StripLastDirectory(path, path);
265             locationEditBox.path = path;
266          }
267          else if(strcmp(name, lastPart))
268          {
269             PathCatSlash(path, name);
270             locationEditBox.path = path;
271          }
272          strcpy(this.name, name);
273          editBox.contents = name;
274          return true;
275       }
276    };
277    Label { this, position = { 10, 10 }, labeledWindow = projectName };
278
279    void EditBoxUpdate(EditBox editBox)
280    {
281       okBtn.disabled = !(locationEditBox.path[0] && projectName.contents[0]);
282    }
283
284    NewProjectDialog()
285    {
286       char location[MAX_LOCATION];
287
288       if(ideSettings.ideProjectFileDialogLocation)
289          strcpy(location, ideSettings.ideProjectFileDialogLocation);
290       else
291          GetWorkingDir(location, sizeof(location) - 1);
292
293       locationEditBox.path = location;
294       strcpy(path, location);
295
296       {
297          DataRow row;
298
299          //targetType.AddField("String", null, 0);
300
301          row = targetType.AddRow();
302          row.tag = TargetTypes::executable;
303          row.SetData(null, "Executable");
304
305          row = targetType.AddRow();
306          row.tag = TargetTypes::sharedLibrary;
307          row.SetData(null, "Shared Library");
308
309          row = targetType.AddRow();
310          row.tag = TargetTypes::staticLibrary;
311          row.SetData(null, "Static Library");
312
313          targetType.currentRow = targetType.FindRow(TargetTypes::executable);
314       }
315    }
316 }
317
318 class QuickProjectDialog : Window
319 {
320    background = activeBorder;
321    minClientSize = { 316, 110 };
322    maxClientSize = { 640, 110 };
323    borderStyle = sizable;
324    tabCycle = true;
325    hasClose = true;
326    text = "Quick Project";
327
328    char path[MAX_LOCATION];
329    char name[MAX_FILENAME];
330
331    Label message { this, position = { 10, 10 }, size = { 200 }, text = "Do you want to quickly create a temporary project?" };
332
333    DropBox targetType { this, position = { 10, 70 }, size = { 130 }, hotKey = altT, text = "Target Type" };
334    Label { this, position = { 10, 50 }, labeledWindow = targetType };
335
336    Button okBtn
337    {
338       parent = this, isDefault = true, position = { 170, 70 }, size = { 60 }, text = "OK";
339       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
340       {
341          char tempDir[MAX_LOCATION] = "";
342          char filePath[MAX_LOCATION];
343          char prjName[MAX_LOCATION] = "quick_project";
344          Project project;
345          Workspace workspace;
346          ProjectView projectWindow;
347          ProjectConfig debug, release;
348
349          /*if(!*/CreateTemporaryDir(tempDir, "ecereide");/*)
350          {
351             MessageBox { type = ok, master = this, text = tempDir, contents = "Error creating temporary directory." }.Modal();
352             return true;
353          }*/
354
355          GetLastDirectory(tempDir, prjName);
356          {
357             char * dot;
358             if((dot = strstr(prjName, ".")))
359                dot[0] = '_';
360          }
361
362          if(!FileExists(tempDir).isDirectory)
363          {
364             MessageBox { type = ok, master = this, text = tempDir, contents = "Temporary directory does not exist." }.Modal();
365             return true;
366          }
367          
368          ide.tmpPrjDir = tempDir;
369
370          strcpy(filePath, tempDir);
371          PathCatSlash(filePath, prjName);
372          //GetExtension(filePath, extension);
373          //if(!extension[0])
374             ChangeExtension(filePath, ProjectExtension, filePath);
375          GetSystemPathBuffer(filePath, filePath);
376
377          debug = ProjectConfig
378          {
379             name = CopyString("Debug");
380             options = 
381             {
382                optimization = none;
383                debug = true;
384                preprocessorDefinitions = { [ CopyString("_DEBUG") ] };
385             };
386             makingModified = true;
387             compilingModified = true;
388             linkingModified = true;
389          };
390
391          release = ProjectConfig
392          {
393             name = CopyString("Release");
394             options =
395             {
396                optimization = speed;
397                debug = false;
398             };
399             makingModified = true;
400             compilingModified = true;
401             linkingModified = true;
402          };
403
404          project = Project
405          {
406             filePath = filePath;
407             moduleName = CopyString(prjName);
408             topNode.type = NodeTypes::project;
409             config = debug;
410          };
411          project.topNode.options = project.options =
412          {
413             warnings = all;
414             // TOFIX: Precomp problems withou the extra ( )
415             targetType = ((TargetTypes)targetType.GetTag());
416             targetFileName = CopyString(prjName);
417          };
418
419          if(project.targetType != staticLibrary)
420          {
421             project.options.libraries = { [ CopyString("ecere") ] };
422          }
423
424          {
425             char workspaceFile[MAX_LOCATION];
426             strcpy(workspaceFile, filePath);
427             ChangeExtension(workspaceFile, WorkspaceExtension, workspaceFile);
428             workspace = Workspace { compiler = ideSettings.defaultCompiler, workspaceFile = workspaceFile };
429          } 
430
431          workspace.projects.Add(project);
432
433          project.topNode.configurations = project.configurations = { [ debug, release ] };
434          project.resNode = project.topNode.Add(project, "Resources", null, resources, archiveFile, false);
435
436          if(!project.Save(filePath))
437          {
438             MessageBox { type = ok, master = this, text = filePath, contents = "Error writing project file" }.Modal();
439             delete project;
440             delete workspace;
441             ide.DestroyTemporaryProjectDir();
442             return true;
443          }
444
445          projectWindow = ide.CreateProjectView(workspace, filePath);
446
447          {
448             char extension[MAX_EXTENSION];
449             Window document = null;
450             for(document = ide.firstChild; document; document = document.next)
451             {
452                char * fileName = document.fileName;
453                if(document.isDocument && document._class == class(CodeEditor) && document.created && fileName)
454                {
455                   CodeEditor codeEditor = (CodeEditor)document;
456                   ide.projectView.AddFile(project.topNode, fileName, false);
457                   codeEditor.DebugMenusDisabled();
458                }
459             }
460          }
461
462          if(project.topNode.modified)
463          {
464             project.Save(filePath);
465             projectWindow.modifiedDocument = false;
466             project.topNode.modified = false;
467          }
468          else
469          {
470             char className[256];
471             char varName[256];
472             CodeEditor codeEditor = projectWindow.CreateNew("Form", "form", "Window", className);
473             EditBox editBox = codeEditor.editBox;
474             strcpy(varName, className);
475             if(islower(varName[0]))
476             {
477                memmove(varName+1, varName, strlen(varName)+1);
478                varName[0] = '_';
479             }
480             else
481                varName[0] = (char)tolower(varName[0]);
482
483             editBox.End();
484             editBox.Printf("\n%s %s {};\n", className, varName);
485
486             codeEditor.EnsureUpToDate();
487
488             // TODO: how to save that new file?
489
490             project.Save(filePath);
491             projectWindow.modifiedDocument = false;
492             project.topNode.modified = false;
493          }
494
495          visible = false;
496
497          if(project && projectWindow)
498             projectWindow.ProjectPrepareMakefile(project, force, true, true);
499
500          ide.projectView.ProjectBuild(null, Modifiers { });
501
502          Destroy(0);
503          return true;
504       }
505    };
506    
507    Button
508    {
509       parent = this, position = { 240, 70 }, size = { 60 }, hotKey = escape, text = "Cancel";
510       NotifyClicked = ButtonCloseDialog;
511    };
512    
513    QuickProjectDialog()
514    {
515       DataRow row;
516
517       row = targetType.AddRow();
518       row.tag = TargetTypes::executable;
519       row.SetData(null, "Executable");
520
521       row = targetType.AddRow();
522       row.tag = TargetTypes::sharedLibrary;
523       row.SetData(null, "Shared Library");
524
525       row = targetType.AddRow();
526       row.tag = TargetTypes::staticLibrary;
527       row.SetData(null, "Static Library");
528
529       targetType.currentRow = targetType.FindRow(TargetTypes::executable);
530    }
531 }