Beispiel #1
0
        /// <summary>
        /// Shows "Project Options" dialog box.
        /// </summary>
        public void ShowOptionsDialog()
        {
            settingsPages.ShowTabs=false;
            settingsPages.Refresh();
            if(optionsTree.SelectedNode==null)
            {
                optionsTree.SelectedNode=optionsTree.Nodes[0].Nodes[0];
                settingsPages.SelectedIndex=0;
            }

            // Make a temporary projectOptions variable
            ProjectOptions tempSettings=new ProjectOptions();

            tempSettings.CompilerFlags=projectOptions.CompilerFlags;
            tempSettings.LinkerFlags=projectOptions.LinkerFlags;
            tempSettings.CrystalFrequency=projectOptions.CrystalFrequency;
            tempSettings.OptimizationFlags=projectOptions.OptimizationFlags;
            tempSettings.ProjectType=projectOptions.ProjectType;
            tempSettings.MCU=projectOptions.MCU;
            tempSettings.PrintfLibraryType=projectOptions.PrintfLibraryType;
            tempSettings.ScanfLibraryType=projectOptions.ScanfLibraryType;
            tempSettings.GenerateListFile=projectOptions.GenerateListFile;
            tempSettings.GenerateMapFile=projectOptions.GenerateMapFile;
            tempSettings.ProgrammingPort=projectOptions.ProgrammingPort;
            tempSettings.OutputFolder=projectOptions.OutputFolder;
            tempSettings.LinkMathLibrary=projectOptions.LinkMathLibrary;
            tempSettings.OutputType=projectOptions.OutputType;
            tempSettings.CharProperty=projectOptions.CharProperty;
            tempSettings.BitFieldsProperty=projectOptions.BitFieldsProperty;
            tempSettings.ObjCopyArguments=projectOptions.ObjCopyArguments;

            tempSettings.AllowLibTypeChange=projectOptions.AllowLibTypeChange;
            tempSettings.AllowMCUChange=projectOptions.AllowMCUChange;
            tempSettings.AllowCrysFreqChange=projectOptions.AllowCrysFreqChange;
            tempSettings.AllowMathLibChange=projectOptions.AllowMathLibChange;
            tempSettings.GCC_Version=projectOptions.GCC_Version;

            advancedOptionsPropertyGrid.SelectedObject=tempSettings;

            if(ShowDialog(MainFormClass.MainForm)==DialogResult.OK)
            {
                // Reload libraries
                projectOptions=tempSettings;

                MainFormClass.MainForm.ClearLibraries();

                // Create Instances of Libraries Newly Selected . Keeping those of selected previously.
                ArrayList tempLibList=new ArrayList(32);
                for(int index=0;index<selectedLibList.Items.Count;index++)
                {
                    bool addModule=true;
                    foreach(object libModule in libList)
                    {
                        if(libModule.GetType().Name.Equals(selectedLibList.Items[index]))
                        {
                            addModule=false;
                            tempLibList.Add(libModule);
                            break;
                        }
                    }
                    if(addModule)
                        index=tempLibList.Add(libAssembly.CreateInstance(assemblyNamespace+"."+selectedLibList.Items[index].ToString(),false));
                }
                libList.Clear();
                libList=tempLibList;

                // Adding to libraries node in the tree.
                projectFilesTree.Nodes[0].Nodes[0].Nodes[0].Nodes.Clear();
                for(int index=0;index<libList.Count;index++)
                {
                    TreeNode libNode=new TreeNode(libList[index].GetType().Name,7,7);
                    libNode.Tag=ProjectTreeItemTypes.LibraryItem;
                    projectFilesTree.Nodes[0].Nodes[0].Nodes[0].Nodes.Insert(index,libNode);
                }
                projectFilesTree.Nodes[0].Nodes[0].Nodes[0].Expand();
                MainFormClass.MainForm.PopulateLibraries(libList);

                //// Setting the library variables.
                libAssembly.GetType("MegaLib.BaseLib",true,false).GetProperty("CrystalFrequency").SetValue(null,projectOptions.CrystalFrequency,BindingFlags.SetProperty|BindingFlags.Static,null,null,null);
                libAssembly.GetType("MegaLib.BaseLib",true,false).GetProperty("Microcontroller").SetValue(null,projectOptions.MCU.ToString(),BindingFlags.SetProperty|BindingFlags.Static,null,null,null);

                isProjectModified=true;
            }
            else
            {
                for(int index=0;index<selectedLibList.Items.Count;index++)
                {
                    selectedLibList.SetSelected(index,true);
                    foreach(object libModule in libList)
                    {
                        if(libModule.GetType().Name.Equals(selectedLibList.Items[index].ToString()))
                        {
                            selectedLibList.SetSelected(index,false);
                            break;
                        }
                    }
                }
                RemoveListItems();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new project.
        /// </summary>
        public void NewProject()
        {
            // Checks for already opened project.
            if(MainFormClass.MainForm.IsProjectOpen)
            {
                if(!CloseProject())
                    return;
            }
            try
            {
                // Gets project file name and initializes project variables.
                if(NewDocumentDialogClass.NewDocumentDialog.ShowNewProjectDialog(MainFormClass.MainForm)==DialogResult.OK)
                {
                    projectFileName=NewDocumentDialogClass.NewDocumentDialog.FullFileName;
                    projectFileStream=new FileStream(projectFileName,FileMode.Create);
                    projectFileStream.Lock(0,projectFileStream.Length);

                    projectName=Path.GetFileNameWithoutExtension(projectFileName);
                    projectType=NewDocumentDialogClass.NewDocumentDialog.ProjectType;
                    projectVersion=megaLibVersion;
                    projectFolder=Path.GetDirectoryName(projectFileName);

                    fileSystemWatcher.Path=projectFolder;

                    projectOptions=new ProjectOptions();

                    #region Project Type Specific Initialization

                    if(projectType==ProjectTypes.MegaBoard_v1)
                    {
                        // SettingNameSpace
                        assemblyNamespace="MegaLib.MegaBoardLib";
                        projectOptions.GCC_Version=GCCVersions.Jan_22_2007;
                        projectOptions.PrintfLibraryType=LibTypes.FloatingPoint;
                        projectOptions.ScanfLibraryType=LibTypes.FloatingPoint;
                        projectOptions.MCU=MCUTypes.ATMEGA128;
                        projectOptions.CrystalFrequency=16000000;
                        projectOptions.OutputFolder="Output";
                        projectOptions.ProgrammingPort="COM1";
                        projectOptions.LinkerFlags="";
                        projectOptions.CompilerFlags="";
                        projectOptions.GenerateListFile=true;
                        projectOptions.GenerateMapFile=true;
                        projectOptions.OptimizationFlags= CompilerOptimizationFlags.None;
                        projectOptions.LinkMathLibrary=true;

                        projectOptions.AllowMCUChange=false;
                        projectOptions.AllowLibTypeChange=false;
                        projectOptions.AllowCrysFreqChange=false;
                        projectOptions.AllowMathLibChange=false;
                        projectOptions.AllowOutputTypeChange=true;

                        TreeNode rootNode=new TreeNode("Megaboard Project",0,0);
                        rootNode.Tag=ProjectTreeItemTypes.ProjectType;
                        projectFilesTree.Nodes.Add(rootNode);
                        TreeNode projectNode=new TreeNode(projectName,1,1);
                        projectNode.Tag=ProjectTreeItemTypes.ProjectName;
                        rootNode.Nodes.Add(projectNode);
                        rootNode.Expand();
                        TreeNode libNode=new TreeNode("Libraries",5,5);
                        libNode.Tag=ProjectTreeItemTypes.Library;
                        projectNode.Nodes.Add(libNode);
                        projectNode.Expand();

                        // Creates a new file main.c .If file already exists then chooses a suitable name.
                        string fileName=projectFolder+@"\Main";
                        int indexNumber=0;
                        if(File.Exists(fileName+".c"))
                            while(File.Exists(fileName+(++indexNumber).ToString()+".c"));
                        if(indexNumber>0)
                            fileName+=indexNumber.ToString();
                        fileName+=".c";
                        System.IO.StreamWriter fileWriter=new StreamWriter(fileName,false,System.Text.Encoding.ASCII);
                        fileWriter.Write("#include\"MegaLib.h\"\nint main()\n{\n MegaLibInit();//Initialization Function\n //Write here\n while(1); //do not remove this line\n}\n");
                        fileWriter.Flush();
                        fileWriter.Close();

                        TreeNode mainFileNode=new TreeNode(Path.GetFileName(fileName),2,2);
                        mainFileNode.Tag=ProjectTreeItemTypes.File;
                        projectNode.Nodes.Add(mainFileNode);
                        MainFormClass.MainForm.OpenFile(fileName);
                        projectFilesTree.SelectedNode=mainFileNode;

                    }
                    else if(projectType==ProjectTypes.ATMega128_Project)
                    {
                        // SettingNameSpace
                        assemblyNamespace="MegaLib.ATMega128Lib";
                        projectOptions.GCC_Version=GCCVersions.Jan_22_2007;
                        projectOptions.PrintfLibraryType=LibTypes.FloatingPoint;
                        projectOptions.ScanfLibraryType=LibTypes.FloatingPoint;
                        projectOptions.MCU=MCUTypes.ATMEGA128;
                        projectOptions.CrystalFrequency=16000000;
                        projectOptions.OutputFolder="Output";
                        projectOptions.ProgrammingPort="COM1";
                        projectOptions.LinkerFlags="";
                        projectOptions.CompilerFlags="";
                        projectOptions.GenerateListFile=true;
                        projectOptions.GenerateMapFile=true;
                        projectOptions.OptimizationFlags= CompilerOptimizationFlags.None;
                        projectOptions.LinkMathLibrary=true;

                        projectOptions.AllowMCUChange=true;
                        projectOptions.AllowLibTypeChange=true;
                        projectOptions.AllowCrysFreqChange=true;
                        projectOptions.AllowMathLibChange=true;
                        projectOptions.AllowOutputTypeChange=true;

                        TreeNode rootNode=new TreeNode("AVR Project",0,0);
                        rootNode.Tag=ProjectTreeItemTypes.ProjectType;
                        projectFilesTree.Nodes.Add(rootNode);
                        TreeNode projectNode=new TreeNode(projectName,1,1);
                        projectNode.Tag=ProjectTreeItemTypes.ProjectName;
                        rootNode.Nodes.Add(projectNode);
                        rootNode.Expand();
                        TreeNode libNode=new TreeNode("Libraries",5,5);
                        libNode.Tag=ProjectTreeItemTypes.Library;
                        projectNode.Nodes.Add(libNode);
                        projectNode.Expand();

                        // Creates a new file main.c .If file already exists then chooses a suitable name.
                        string fileName=projectFolder+@"\Main";
                        int indexNumber=0;
                        if(File.Exists(fileName+".c"))
                            while(File.Exists(fileName+(++indexNumber).ToString()+".c"));
                        if(indexNumber>0)
                            fileName+=indexNumber.ToString();
                        fileName+=".c";
                        System.IO.StreamWriter fileWriter=new StreamWriter(fileName,false,System.Text.Encoding.ASCII);
                        fileWriter.Write("#include\"MegaLib.h\"\nint main()\n{\n MegaLibInit();//Initialization Function\n //Write here\n while(1); //do not remove this line\n}\n");
                        fileWriter.Flush();
                        fileWriter.Close();

                        TreeNode mainFileNode=new TreeNode(Path.GetFileName(fileName),2,2);
                        mainFileNode.Tag=ProjectTreeItemTypes.File;
                        projectNode.Nodes.Add(mainFileNode);
                        MainFormClass.MainForm.OpenFile(fileName);
                        projectFilesTree.SelectedNode=mainFileNode;

                    }

                    #endregion

                    // Enabling file watcher.
                    fileSystemWatcher.EnableRaisingEvents=true;

                    // Init Lists
                    availableLibList.Items.Clear();
                    selectedLibList.Items.Clear();
                    foreach(Type selectedType in libAssembly.GetExportedTypes())
                    {
                        if( selectedType.Namespace==assemblyNamespace && !selectedType.IsEnum)
                        {
                            selectedType.GetProperty("IsIncluded").SetValue(null,false,BindingFlags.SetProperty,null,null,null);
                            availableLibList.Items.Add(selectedType.Name);
                        }
                    }

                    MainFormClass.MainForm.IsProjectOpen=true;
                    MainFormClass.MainForm.Text="MegaIDE - "+projectName+".mbp";

                    RecentDocuments.AddToRecentProjects(projectFileName);
                    MainFormClass.MainForm.UpdateRecentDocumentsList();

                    // Sets the tab page to first page in project options dialog.
                    optionsTree.SelectedNode=optionsTree.Nodes[0].Nodes[0];
                    settingsPages.SelectedIndex=0;

                    // Setting Base static properties.
                    libAssembly.GetType("MegaLib.BaseLib",true,false).GetProperty("CrystalFrequency").SetValue(null,projectOptions.CrystalFrequency,BindingFlags.SetProperty|BindingFlags.Static,null,null,null);
                    libAssembly.GetType("MegaLib.BaseLib",true,false).GetProperty("Microcontroller").SetValue(null,projectOptions.MCU.ToString(),BindingFlags.SetProperty|BindingFlags.Static,null,null,null);

                    ShowOptionsDialog();

                    SaveProject();

                    MainFormClass.MainForm.BringProjectTreeToFront();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error creating new project! "+ex.Message,"MegaIDE",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Opens a project from a project file.
        /// </summary>
        /// <param name="fileName">
        /// Path of the project file.
        /// </param>
        public void OpenProject(string fileName)
        {
            //Checking for already opened project.
            if(MainFormClass.MainForm.IsProjectOpen)
            {
                if(fileName.ToLower()==projectFileName.ToLower())
                {
                    MessageBox.Show("The project is already open!","MegaIDE",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }

                if(!CloseProject())
                    return;
            }

            ArrayList openFilesList=new ArrayList(5);
            isProjectModified=false;
            try
            {
                projectFileName=fileName;
                projectFileStream=new FileStream(projectFileName,FileMode.Open);
                projectFileStream.Lock(0,projectFileStream.Length);
                projectFolder=Path.GetDirectoryName(fileName);

                //Deserializing saved variables.
                BinaryFormatter deserializer=new BinaryFormatter();
                deserializer.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
                deserializer.TypeFormat = System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesWhenNeeded;

                projectName=(string)deserializer.Deserialize(projectFileStream);
                string projectTypeString=(string)deserializer.Deserialize(projectFileStream);
                projectType=(ProjectTypes)Enum.Parse(typeof(ProjectTypes),projectTypeString);
                projectVersion=(string)deserializer.Deserialize(projectFileStream);

                // Loading Project Tree
                ArrayList treeNodesList = (ArrayList)deserializer.Deserialize(projectFileStream);
                foreach (TreeNode treeNode in treeNodesList)
                {
                    projectFilesTree.Nodes.Add(treeNode);
                }

                openFilesList=(ArrayList)deserializer.Deserialize(projectFileStream);
                projectOptions=(ProjectOptions)deserializer.Deserialize(projectFileStream);
                projectOptions.GCC_Version = projectOptions.GCC_Version;
                if (projectVersion!=megaLibVersion)
                {
                    if(MessageBox.Show("The project has been created using MegaLib version "+projectVersion.Substring(0,3)+" . It will be changed to the current version ( "+megaLibVersion.Substring(0,3)+" ) .Would you like to continue ?","MegaIDE",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.No)
                    {
                        MainFormClass.MainForm.ClearLibraries();
                        if(projectFilesTree.Nodes.Count != 0)
                        {
                            MainFormClass.MainForm.CloseProjectFiles();
                            projectFilesTree.Nodes.Clear();
                        }
                        try
                        {
                            projectFileStream.Close();
                        }
                        catch{}
                        MainFormClass.MainForm.IsProjectOpen=false;
                        return;
                    }
                    else
                    {
                        projectVersion=megaLibVersion;
                        isProjectModified=true;
                    }
                }
                libList=(ArrayList)deserializer.Deserialize(projectFileStream);

                // SetAssemblyNameSpace
                if(projectType==ProjectTypes.MegaBoard_v1)
                    assemblyNamespace="MegaLib.MegaBoardLib";
                else if(projectType==ProjectTypes.ATMega128_Project)
                    assemblyNamespace="MegaLib.ATMega128Lib";

                // Setting Base static properties.
                libAssembly.GetType("MegaLib.BaseLib",true,false).GetProperty("CrystalFrequency").SetValue(null,projectOptions.CrystalFrequency,BindingFlags.SetProperty|BindingFlags.Static,null,null,null);
                libAssembly.GetType("MegaLib.BaseLib",true,false).GetProperty("Microcontroller").SetValue(null,projectOptions.MCU.ToString(),BindingFlags.SetProperty|BindingFlags.Static,null,null,null);

                // Loading available libraries
                availableLibList.Items.Clear();
                selectedLibList.Items.Clear();
                foreach(object selectedObject in libList)
                {
                    selectedLibList.Items.Add(selectedObject.GetType().Name);
                }
                foreach(Type selectedType in libAssembly.GetExportedTypes())
                {
                    if(selectedType.Namespace==assemblyNamespace && !selectedType.IsEnum)
                    {
                        if(selectedLibList.Items.Contains(selectedType.Name))
                        {
                            selectedType.GetProperty("IsIncluded").SetValue(null,true,BindingFlags.SetProperty|BindingFlags.Static,null,null,null);
                        }
                        else
                        {
                            availableLibList.Items.Add(selectedType.Name);
                            selectedType.GetProperty("IsIncluded").SetValue(null,false,BindingFlags.SetProperty|BindingFlags.Static,null,null,null);
                        }
                    }
                }

                MainFormClass.MainForm.PopulateLibraries(libList);
                fileSystemWatcher.Path=projectFolder;
                fileSystemWatcher.EnableRaisingEvents=true;
            }
            catch(SerializationException)
            {
                // Code to run in case of invalid file
                MainFormClass.MainForm.ClearLibraries();
                if(projectFilesTree.Nodes.Count != 0)
                {
                    MainFormClass.MainForm.CloseProjectFiles();
                    projectFilesTree.Nodes.Clear();
                    libList.Clear();
                }
                MessageBox.Show("Error opening project! The path may not be correct or the file could be corrupt.","MegaIDE",MessageBoxButtons.OK,MessageBoxIcon.Error);
                try
                {
                    projectFileStream.Close();
                }
                catch{}
                MainFormClass.MainForm.IsProjectOpen=false;
                return;
            }
            catch(Exception ex)
            {
                // Code to run in case of invalid file
                MainFormClass.MainForm.ClearLibraries();
                if(projectFilesTree.Nodes.Count != 0)
                {
                    MainFormClass.MainForm.CloseProjectFiles();
                    projectFilesTree.Nodes.Clear();
                }
                MessageBox.Show("Error opening project! "+ex.Message,"MegaIDE",MessageBoxButtons.OK,MessageBoxIcon.Error);
                try
                {
                    projectFileStream.Close();
                }
                catch{}
                MainFormClass.MainForm.IsProjectOpen=false;
                return;
            }

            //Checking for missing files
            ArrayList missingFilesList=new ArrayList(20);
            ArrayList missingNodesList=new ArrayList(20);
            CheckMissingFiles(projectFilesTree.Nodes[0].Nodes[0],missingFilesList,missingNodesList);

            if(missingFilesList.Count != 0)
            {
                if(ExitDialogClass.ExitDialog.ShowMissingFilesDialog(MainFormClass.MainForm,missingFilesList)==DialogResult.Cancel)
                {
                    projectFilesTree.Nodes.Clear();
                    MainFormClass.MainForm.ClearLibraries();
                    ExitDialogClass.ExitDialog.RestoreSettings();
                    return;
                }
                ExitDialogClass.ExitDialog.RestoreSettings();

                //Remove the missing files from the opened files list and tree.
                ArrayList fileNamesToRemove=new ArrayList(3);
                foreach(TreeNode fileNode in missingNodesList)
                {
                    if(openFilesList.Count != 0)
                    {
                        foreach(string openFileName in openFilesList)
                        {
                            if((projectFolder+"\\"+openFileName).ToLower().StartsWith(GetAbsolutePath(fileNode).ToLower()))
                                fileNamesToRemove.Add(openFileName);
                        }
                    }
                    fileNode.Remove();
                }
                foreach(string fileNameToRemove in fileNamesToRemove)
                {
                    openFilesList.Remove(fileNameToRemove);
                }
                isProjectModified=true;
            }
            if(isProjectModified)
            {
                SaveProject();
            }
            MainFormClass.MainForm.IsProjectOpen=true;
            isProjectModified=false;
            if(openFilesList.Count != 0)
                MainFormClass.MainForm.OpenProjectFiles(projectFolder,openFilesList);

            NewDocumentDialogClass.NewDocumentDialog.DialogMode=NewDocumentDialogClass.DialogModes.None;
            MainFormClass.MainForm.Text="MegaIDE - "+projectName+".mbp";
            RecentDocuments.AddToRecentProjects(projectFileName);
            MainFormClass.MainForm.UpdateRecentDocumentsList();
            projectFilesTree.ExpandAll();
        }