1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  */
5 
6 #ifndef PROJECTFILE_H
7 #define PROJECTFILE_H
8 
9 #include <vector>
10 
11 #include "settings.h"
12 #include "globals.h"
13 #include "prep.h"
14 #include <wx/dynarray.h>
15 #include <wx/filename.h>
16 #include <wx/treectrl.h>
17 #include <wx/hashset.h>
18 
19 class cbProject;
20 class ProjectBuildTarget;
21 class pfDetails;
22 
23 WX_DECLARE_HASH_MAP(ProjectBuildTarget*, pfDetails*, wxPointerHash, wxPointerEqual, PFDMap);
24 
25 struct pfCustomBuild
26 {
pfCustomBuildpfCustomBuild27     pfCustomBuild() : useCustomBuildCommand(false) { }
28     wxString buildCommand;
29     bool     useCustomBuildCommand;
30 };
31 WX_DECLARE_HASH_MAP(wxString, pfCustomBuild, wxStringHash, wxStringEqual, pfCustomBuildMap);
32 
33 class ProjectFile;
34 typedef std::vector<ProjectFile*> ProjectFilesVector;
35 
36 WX_DEFINE_ARRAY_INT(int, editorFoldLinesArray);
37 
38 /** Represents a file in a Code::Blocks project. */
39 class DLLIMPORT ProjectFile
40 {
41     public:
42         /// Constructor
43         explicit ProjectFile(cbProject* prj);
44         /// Destructor
45         ~ProjectFile();
46 
47         /** Change filename of the file. Note that this does only update
48           * the internal variables. It does NOT rename the file on disk...
49           * It updates @c file, @c relativeFilename, @c relativeToCommonTopLevelPath
50           * and finally marks the parent project as modified.
51           * @note This allows renaming only the LAST part of the filename (the name and extension)
52           */
53         void Rename(const wxString& new_name);
54 
55         /** Make this file belong to an additional build target.
56           * @param targetName The build target to add this file to. */
57         void AddBuildTarget(const wxString& targetName);
58 
59         /** Rename a build target this file belongs in.
60           * @param oldTargetName The build target's old name.
61           * @param newTargetName The build target's new name.
62           * @note This does *not* change the build target's name, just the reference in the project file.
63           * This is actually used by cbProject::RenameBuildTarget(). */
64         void RenameBuildTarget(const wxString& oldTargetName, const wxString& newTargetName);
65 
66         /** Remove this file from the specified build target.
67           * @param targetName The build target's name to remove this file from. */
68         void RemoveBuildTarget(const wxString& targetName);
69 
70         /**
71          * @return An array of strings, containing the names of all the build
72          * targets this file belongs to. */
73         const wxArrayString& GetBuildTargets() const;
74 
75         /** Show the file properties dialog.
76           * @param parent The parent window for the dialog (can be NULL).
77           * @return True if the user closed the dialog with "OK", false if closed it with "Cancel".
78           */
79         bool ShowOptions(wxWindow* parent);
80 
81         // take as example the relative file sdk/cbProject.cpp
82         /** @return The relative (to the project) filename without extension. */
83         wxString GetBaseName() const; // returns sdk/cbProject
84 
85         /** @return The generated object filename. */
86         const wxString& GetObjName(); // returns sdk/cbProject.o
87 
88         /** Set the generated object filename.
89           * @param name The filename for the generated object. */
90         void SetObjName(const wxString& name);
91 
92         /** @return The parent project. */
GetParentProject()93         cbProject* GetParentProject(){ return project; }
94 
95         /** This is called automatically when adding/removing build targets.
96           * @param target A pointer to the build target whose file details should be updated. */
97         void UpdateFileDetails(ProjectBuildTarget* target = nullptr);
98 
99         /** Access the file details for this project file for the specified target.
100           * @param target A pointer to the build target whose file details should be updated.
101           * @return The details for this project file for the specified build target. */
102         const pfDetails& GetFileDetails(ProjectBuildTarget* target);
103 
104         /** Set the visual state (modified, read-only, etc).
105           * @param state The new visual state. */
106         void SetFileState(FileVisualState state);
107 
108         /** @return The visual state (modified, read-only, etc). */
109         FileVisualState GetFileState() const;
110 
111         /** Modify 'Use custom command to build this file' for a compilerId. */
112         void SetUseCustomBuildCommand(const wxString& compilerId, bool useCustomBuildCommand);
113 
114         /** Modify customBuild command for a compilerId. */
115         void SetCustomBuildCommand(const wxString& compilerId, const wxString& newBuildCommand);
116 
117         /** Read 'Use custom command to build this file' for a compilerId. */
118         bool GetUseCustomBuildCommand(const wxString& compilerId);
119 
120         /** Read customBuild command for a compilerId. */
121         wxString GetCustomBuildCommand(const wxString& compilerId);
122 
123         /** The full filename of this file. Usually you need to read from it and never write to it.
124           * @note Use Rename() if you want to change this or else bad things will happen
125           */
126         wxFileName file;
127 
128         /** The relative (to the project) filename of this file. Usually you need to read from it and never write to it.
129           * @note Use Rename() if you want to change this or else bad things will happen
130           */
131         wxString relativeFilename;
132 
133         /** The relative filename to the common top-level path.
134           * This is used mainly for the tree, as this is guaranteed to not contain '..' */
135         wxString relativeToCommonTopLevelPath;
136 
137         /** Compile flag. If it's true, the file is compiled (generates object file) else it is not. */
138         bool compile;
139 
140         /** Link flag. If it's true, the (generated object) file is linked else it is not. */
141         bool link;
142 
143         /** The weight. This is a number between 0 and 100 (defaults to 50).
144           * Files with smaller weights are compiled earlier than those with larger weights. */
145         unsigned short int weight;
146 
147         /** If true, the file is open inside an editor. */
148         bool editorOpen; // layout info
149 
150         /** Split type of the editor as int. */
151         int editorSplit; // layout info
152 
153         /** Last active splitview (1 or 2). */
154         int editorSplitActive; // layout info
155 
156         /** Last splitter position. */
157         int editorSplitPos; // layout info
158 
159         /** The last known caret position in an editor for this file (left/top control if split). */
160         int editorPos; // layout info
161 
162         /** The last known caret line in an editor for this file (left/top control if split). */
163         int editorTopLine; // layout info
164 
165         /** The zoom-factor of the editor for this file (left/top control if split). */
166         int editorZoom; // layout info
167 
168         /** The last known caret position in an editor for this file (right/bottom control if split). */
169         int editorPos_2; // layout info
170 
171         /** The last known caret line in an editor for this file(right/bottom control if split). */
172         int editorTopLine_2; // layout info
173 
174         /** The zoom-factor of the editor for this file(right/bottom control if split). */
175         int editorZoom_2; // layout info
176 
177         /** The position of the editor-tab for this file. */
178         int editorTabPos; // layout info
179 
180         /** Fold lines */
181         wxArrayInt editorFoldLinesArray; // layout info
182 
183         /** A map for custom builds. Key is compiler ID, value is pfCustomBuild struct. */
184         pfCustomBuildMap customBuild;
185 
186         /** The compiler variable used for this file (e.g CPP, CC, etc). */
187         wxString compilerVar;
188 
189         /** An array of strings, containing the names of all the build targets this file belongs to. */
190         wxArrayString buildTargets;
191 
192         /** A string that represents the virtual folder this file will appear in.
193           * This is a relative path which doesn't have to exist in the filesystem
194           * hierarchy. */
195         wxString virtual_path;
196 
197         /** If this is an auto-generated file, which file is generating it? */
198         ProjectFile* autoGeneratedBy; // deprecated --> use the method ; die public member var's
199 
200         /** If this is an auto-generated file, which file is generating it? */
AutoGeneratedBy()201         ProjectFile* AutoGeneratedBy() const { return autoGeneratedBy;}
202 
203         /** If this is an auto-generated file, set the file which is generating it? */
SetAutoGeneratedBy(ProjectFile * TheFile)204         void SetAutoGeneratedBy(ProjectFile* TheFile) { autoGeneratedBy = TheFile; }
205 
206         /** Auto-generated files when compiling this file */
207         ProjectFilesVector generatedFiles;
208 
209         /** Returns the wxTreeItemId for the file */
GetTreeItemId()210         const wxTreeItemId& GetTreeItemId() const { return m_TreeItemId; }
211 
212         /** Sets the tree item id for the file. Should not be called by users! */
SetTreeItemId(wxTreeItemId id)213         void SetTreeItemId(wxTreeItemId id) { m_TreeItemId = id; }
214 
215         /** Compare relative names of projectfiles.
216           * Static helper function to sort array of projectfiles.
217           * Needed because the order of files in a hashset is not guaranteed.
218           * @param item1 first projectfile.
219           * @param item2 second projectfile.
220           * @return A negative value, 0, or positive value if the relative
221           * filename of item1 is less than, equal to or greater than the one of item2. */
222         static int CompareProjectFiles(ProjectFile* item1, ProjectFile* item2);
223     protected:
224         friend class cbProject;
225 
226         /// Something like a copy constructor. Used by cbProject::operator=.
227         ProjectFile(cbProject* prj, const ProjectFile &pf);
228 
229         void DoUpdateFileDetails(ProjectBuildTarget* target);
230         cbProject* project;
231         FileVisualState m_VisualState;
232         wxTreeItemId m_TreeItemId; // set by the project when building the tree
233         wxString m_ObjName;
234         PFDMap m_PFDMap;
235 };
236 WX_DECLARE_HASH_SET   ( ProjectFile*, wxPointerHash, wxPointerEqual, FilesList );
237 WX_DEFINE_SORTED_ARRAY( ProjectFile*, ProjectFileArray                         );
238 
239 /** This is a helper class that caches various filenames for one ProjectFile.
240   * These include the source filename, the generated object filename,
241   * relative and absolute versions of the above, etc.
242   * Mainly used by the compiler...
243   */
244 class pfDetails
245 {
246     public:
247         pfDetails(ProjectBuildTarget* target, ProjectFile* pf);
248         void Update(ProjectBuildTarget* target, ProjectFile* pf);
249         // all the members below, are set in the constructor
250         wxString source_file;
251         wxString object_file;
252         wxString dep_file;
253         wxString object_dir;
254         wxString object_dir_flat;
255         wxString dep_dir;
256         wxString object_file_flat;
257         // those below, have no UnixFilename() applied, nor QuoteStringIfNeeded()
258         wxString source_file_native;
259         wxString object_file_native;
260         wxString dep_file_native;
261         wxString object_dir_native;
262         wxString object_dir_flat_native;
263         wxString dep_dir_native;
264         wxString source_file_absolute_native;
265         wxString object_file_absolute_native;
266         wxString object_file_flat_absolute_native;
267         wxString dep_file_absolute_native;
268         wxString object_file_flat_native;
269 };
270 
271 #endif // PROJECTFILE_H
272