4
#include <wx/datetime.h>
5
#include <wx/dynarray.h>
6
#include <wx/hashmap.h>
7
#include <wx/treectrl.h>
10
#include "openfilestree.h"
11
#include "compiletargetbase.h"
13
#include "projectbuildtarget.h"
19
class ProjectBuildTarget;
21
class FilesGroupsAndMasks;
23
// hashmap for fast searches in cbProject::GetFileByFilename()
24
WX_DECLARE_STRING_HASH_MAP(ProjectFile*, ProjectFiles);
26
typedef std::map<wxString, wxArrayString> VirtualBuildTargetsMap;
28
class DLLIMPORT FileTreeData : public MiscTreeItemData
31
/// The kind of tree node
38
ftdkVirtualGroup, // wilcard matching
42
FileTreeData(cbProject* project, FileTreeDataKind kind = ftdkUndefined)
49
FileTreeDataKind GetKind() const { return m_kind; }
50
cbProject* GetProject() const { return m_Project; }
51
int GetFileIndex() const { return m_Index; }
52
ProjectFile* GetProjectFile() const { return m_file; }
53
const wxString& GetFolder() const { return m_folder; }
55
void SetKind(FileTreeDataKind kind){ m_kind = kind; }
56
void SetProject(cbProject* project){ m_Project = project; }
57
// only valid for file selections
58
void SetFileIndex(int index){ m_Index = index; }
59
void SetProjectFile(ProjectFile* file){ m_file = file; }
60
// only valid for folder selections
61
void SetFolder(const wxString& folder){ m_folder = folder; }
67
FileTreeDataKind m_kind;
70
/** Precompiled headers mode.
71
* Defines where and how are the project's precompiled headers generated.
72
* Currently implemented only for GCC (3.4 and above).
76
pchSourceDir = 0, /// In a dir (named by the PCH) on the same level as the source header (default).
77
pchObjectDir, /// In the objects output dir, along with other object files.
78
pchSourceFile, /// In a file alongside the source header (with .gch appended).
81
/** @brief Represents a Code::Blocks project.
83
* A project is a collection of build targets and files.
84
* Each project can contain any number of build targets and files.
85
* @see ProjectBuildTarget, ProjectFile.
87
class DLLIMPORT cbProject : public CompileTargetBase
91
cbProject(const wxString& filename = wxEmptyString);
95
/** @return True if the project fully loaded, false if not. */
96
bool IsLoaded(){ return m_Loaded; }
98
/** (Re)build the project tree.
99
* @param tree The wxTreeCtrl to use.
100
* @param root The tree item to use as root. The project is built as a child of this item.
101
* @param categorize If true, use virtual folders like "Sources", "Headers", etc.
102
* @param useFolders If true, create folders as needed. If false, the list is flat.
103
* @param fgam If not NULL, use these file groups and masks for virtual folders.
105
void BuildTree(wxTreeCtrl* tree, const wxTreeItemId& root, bool categorize, bool useFolders, FilesGroupsAndMasks* fgam = 0L);
107
/** This resets the project to a clear state. Like it's just been new'ed. */
108
void ClearAllProperties();
110
/** Calculates the top-level path common to all project files.
111
* This is called automatically (no need for you to call it) and is used
112
* to find the top-level folder for building the tree.
114
void CalculateCommonTopLevelPath();
116
/** @return the top-level path common to all project files. */
117
wxString GetCommonTopLevelPath();
119
/** @return True if the project is modified in any way. */
122
/** Mark the project as modified or not.
123
* @param modified If true, the project is marked as modified. If false, as not-modified.
125
void SetModified(bool modified = true);
127
/** Access a file of the project.
128
* @param index The index of the file. Must be greater or equal than zero and less than GetFilesCount().
129
* @return A pointer to the file or NULL if not found.
131
ProjectFile* GetFile(int index);
133
/** Access a file of the project.
134
* @param filename The filename of the file.
135
* @param isRelative True if @c filename is a relative filename, false if not.
136
* @param isUnixFilename True if @c filename is already normalized with UnixFilename(), false if not.
137
* @return A pointer to the file or NULL if not found.
139
ProjectFile* GetFileByFilename(const wxString& filename, bool isRelative = true, bool isUnixFilename = false);
141
/** @return The number of files in the project. */
142
int GetFilesCount(){ return m_Files.GetCount(); }
144
/** Set the Makefile filename used when exporting a Makefile for the project,
145
* or when using a custom Makefile to build the project.
146
* @param makefile The filename for the Makefile.
148
void SetMakefile(const wxString& makefile){ m_Makefile = makefile; SetModified(true); }
150
/** @return The filename for the Makefile. */
151
const wxString& GetMakefile();
153
/** Mark if the project should use a custom Makefile for compilation.
154
* @param custom If true, use a custom Makefile for compilation. If false, use direct C::B build mode.
156
void SetMakefileCustom(bool custom);
158
/** @return True if the project is using a custom Makefile for compilation, false if not. */
159
bool IsMakefileCustom(){ return m_CustomMakefile; }
161
/** Is there a build target (virtual or real) by @c name?
162
* @param name The build target's name.
163
* @param virtuals_too Include virtual build targets in query.
164
* @return True if exists a build target (virtual or real) by that name, false if not.
166
bool BuildTargetValid(const wxString& name, bool virtuals_too = true) const;
168
/** @return The first valid (virtual or real) build target. */
169
wxString GetFirstValidBuildTargetName(bool virtuals_too = true) const;
171
/** @return The build target index which will be pre-selected when the "Select target"
172
* dialog appears when running the project. Valid only for multi-target projects. */
173
const wxString& GetDefaultExecuteTarget() const;
175
/** Set the build target index which will be pre-selected when the "Select target"
176
* dialog appears when running the project.
177
* @param name The build target's name.
179
void SetDefaultExecuteTarget(const wxString& name);
181
/** @return The number of build targets this project contains. */
182
int GetBuildTargetsCount(){ return m_Targets.GetCount(); }
184
/** Access a build target.
185
* @param index The build target index. Must be greater or equal to zero and less than GetBuildTargetsCount().
186
* @return The build target or NULL if not found.
188
ProjectBuildTarget* GetBuildTarget(int index);
190
/** Access a build target.
191
* @param targetName The build target name.
192
* @return The build target or NULL if not found.
194
ProjectBuildTarget* GetBuildTarget(const wxString& targetName);
196
/** Add a new build target.
197
* @param targetName The build target name.
198
* @return The build target that was just created.
200
ProjectBuildTarget* AddBuildTarget(const wxString& targetName);
202
/** Rename a build target.
203
* @param index The build target's index to rename.
204
* @param targetName The new name for the build target.
205
* @return True if @c index was valid, false if not.
207
bool RenameBuildTarget(int index, const wxString& targetName);
209
/** Rename a build target.
210
* @param oldTargetName The build target's old name.
211
* @param newTargetName The new name for the build target.
212
* @return True if @c oldTargetName was valid, false if not.
214
bool RenameBuildTarget(const wxString& oldTargetName, const wxString& newTargetName);
216
/** Duplicate a build target.
217
* @param index The index of the build target to duplicate.
218
* @param newName The name for the new build target. If empty, it will be named like "Copy of <base_target_name>".
219
* @return The new build target if @c index was valid, NULL if not (or something went wrong).
221
ProjectBuildTarget* DuplicateBuildTarget(int index, const wxString& newName = wxEmptyString);
223
/** Duplicate a build target.
224
* @param targetName The name of the build target to duplicate.
225
* @param newName The name for the new build target. If empty, it will be named like "Copy of <base_target_name>".
226
* @return The new build target if @c index was valid, NULL if not (or something went wrong).
228
ProjectBuildTarget* DuplicateBuildTarget(const wxString& targetName, const wxString& newName = wxEmptyString);
230
/** Export a target as a new project.
231
* In other words, save a copy of the project containing only the specified target.
232
* The user will be prompted with a dialog to select the new project name.
233
* @param index The index of the build target to export.
234
* @return True on success, false on failure (or dialog cancellation).
235
* @note The dialog to select the new project name is not a file dialog because
236
* the user is not allowed to save anywhere. The new project must remain "operational"
237
* (assuming the target to export, was "operational") so the new project must be saved
238
* in the same directory as the original to preserve relative paths.
240
bool ExportTargetAsProject(int index);
242
/** Export a target as a new project.
243
* In other words, save a copy of the project containing only the specified target.
244
* The user will be prompted with a dialog to select the new project name.
245
* @param targetName The name of the build target to export.
246
* @return True on success, false on failure (or dialog cancellation).
247
* @see ExportTargetAsProject(int).
249
bool ExportTargetAsProject(const wxString& targetName);
251
/** Remove a build target.
252
* @param index The index of the build target to remove.
253
* @return True if @c index was valid, false if not.
255
bool RemoveBuildTarget(int index);
257
/** Remove a build target.
258
* @param targetName The build target name.
259
* @return True if the @c targetName was valid, false if not.
261
bool RemoveBuildTarget(const wxString& targetName);
263
/** Reorder the list of build targets.
264
* This is useful, so that when the special build target "All" is being built
265
* targets are built in the desired order.
266
* @param nameOrder An array of strings containing build target names in the order you desire.
267
* The number of array elements must be equal to GetBuildTargetsCount().
269
void ReOrderTargets(const wxArrayString& nameOrder);
271
/** Set the active build target. Mainly used by the compiler.
272
* @param name The build target name to set as active. If @c name does
273
* not exist, then the first virtual target is set
274
* or the first real target, depending which is valid.
275
* @return True if @c name was valid, false if not.
277
bool SetActiveBuildTarget(const wxString& name);
279
/** @return The active build target name. Note that this might be a virtual target. */
280
const wxString& GetActiveBuildTarget() const;
282
/** @return The mode precompiled headers are handled. */
283
PCHMode GetModeForPCH() const { return m_PCHMode; }
285
/** Set the mode to handle precompiled headers.
286
* @param mode The desired PCH mode.
288
void SetModeForPCH(PCHMode mode){ m_PCHMode = mode; SetModified(true); }
290
void SetCompilerID(const wxString& id); // overriden
292
/** @return The root item of this project in the project manager's tree. */
293
wxTreeItemId GetProjectNode(){ return m_ProjectNode; }
295
/** Act like closing all project files, but don't do it.
296
* Used to check if any of the project files need saving.
297
* @return True if even one project file needs to be saved, false if not.
299
bool QueryCloseAllFiles();
301
/** Close all project files.
302
* @param dontsave If true, no project file will be saved even if modified.
303
* If false, any modified file will be saved (default).
304
* @return True if succesfull, false otherwise.
306
bool CloseAllFiles(bool dontsave=false);
308
/** Save all project files.
309
* @return True if succesfull, false otherwise.
313
/** Save the project.
314
* @return True if succesfull, false otherwise.
318
/** Save the project under a different name.
319
* A dialog pops up for the user to choose a new filename for the project.
320
* @return True if succesfull, false otherwise.
324
/** Save the project's layout.
325
* Layout is the list of open project files, which one is active,
326
* where the cursor is located on each one of those, etc.
327
* @return True if succesfull, false otherwise.
331
/** Load the project's layout.
332
* @see SaveLayout() for info.
333
* @return True if succesfull, false otherwise.
337
/** Add a file to the project.
338
* This variation, takes a target name as first parameter.
339
* @param targetName The name of the build target to add this file to.
340
* @param filename The file's filename. This *must* be a filename relative to the project's path.
341
* @param compile If true this file is compiled when building the project.
342
* @param link If true this file is linked when building the project.
343
* @param weight A value between zero and 100 (defaults to 50). Smaller weight, makes the file compile earlier than those with larger weight.
344
* @return The newly added file or NULL if something went wrong.
346
ProjectFile* AddFile(const wxString& targetName, const wxString& filename, bool compile = true, bool link = true, unsigned short int weight = 50);
348
/** Add a file to the project.
349
* This variation, takes a target index as first parameter.
350
* @param targetIndex The index of the build target to add this file to.
351
* @param filename The file's filename. This *must* be a filename relative to the project's path.
352
* @param compile If true this file is compiled when building the project.
353
* @param link If true this file is linked when building the project.
354
* @param weight A value between zero and 100 (defaults to 50). Smaller weight, makes the file compile earlier than those with larger weight.
355
* @return The newly added file or NULL if something went wrong.
357
ProjectFile* AddFile(int targetIndex, const wxString& filename, bool compile = true, bool link = true, unsigned short int weight = 50);
359
/** Remove a file from the project.
360
* @param index The index of the file.
361
* @return True if @c index was valid, false if not.
363
bool RemoveFile(int index);
365
/** Remove a file from the project.
366
* @param pf The pointer to ProjectFile.
367
* @return True if @c pf was a valid project file, false if not.
369
bool RemoveFile(ProjectFile* pf);
371
/** Display the project options dialog.
372
* @return True if the dialog was closed with "OK", false if closed with "Cancel".
376
/** Convenience function for remembering the project's tree state when refreshing it.
377
* @return An array of strings containing the tree-path names of expanded nodes.
379
const wxArrayString& ExpandedNodes(){ return m_ExpandedNodes; }
381
/** Convenience function for remembering the project's tree state when refreshing it.
382
* Adds an expanded node in this internal list.
383
* @param path The tree-path to add.
385
void AddExpandedNode(const wxString& path){ m_ExpandedNodes.Add(path); }
387
/** Convenience function for remembering the project's tree state when refreshing it.
388
* @param tree The tree control to save its expanded state.
390
void SaveTreeState(wxTreeCtrl* tree);
392
/** Convenience function for remembering the project's tree state when refreshing it.
393
* @param tree The tree control to restore its expanded state to a previously saved.
395
void RestoreTreeState(wxTreeCtrl* tree);
397
/** Displays a target selection dialog.
398
* When invoked, a selection dialog is presented to the user so that he/she
399
* can select one target from the list of this project's targets.
400
* @param initial The index of the pre-selected target when the dialog is displayed.
401
* Defaults to none (-1).
402
* @param evenIfOne If true, the dialog is still shown even if the project contains only one target.
403
* The default behaviour is to not show the dialog if the project has only one target.
404
* @return The target's index that the user selected or -1 if the dialog was cancelled.
406
int SelectTarget(int initial = -1, bool evenIfOne = false);
408
/** Rename the project's title in the tree.
409
* @param newname The new title for the project.
410
* @note This does *not* actually alter the project's title. It just changes it on the tree.
412
void RenameInTree(const wxString &newname);
414
/** Get a pointer to the currently compiling target.
415
* @return While the project is being built, this function returns the currently building
416
* target. For all other times, NULL is returned.
418
ProjectBuildTarget* GetCurrentlyCompilingTarget(){ return m_CurrentlyCompilingTarget; }
420
/** Set the currently compiling target.
421
* @note This function is for internal use by compilers only.
422
* Using this function in any other place results in undefined behaviour!
423
* @param bt The build target that is currently building.
425
void SetCurrentlyCompilingTarget(ProjectBuildTarget* bt);
427
/** Define a new virtual build target.
429
* A virtual build target is not really a build target itself but it is an alias
430
* for a group of other build targets, real or virtual.
431
* An example is the "All" virtual build target which means "all build targets".
433
* @param alias The virtual build target's name.
434
* @param targets A list of build target names to include in this virtual build target.
435
* They can be real or other virtual build targets.
436
* @return True for success, false for failure. The only reason for this function
437
* to return false is if a *real* build target exists with the same name as @c alias
438
* (or if you pass an empty @c targets array).
439
* @note Every time you call this function with the same @c alias parameter, the virtual
440
* build target is re-defined. In other words, it's not an error if @c alias is already
443
bool DefineVirtualBuildTarget(const wxString& alias, const wxArrayString& targets);
445
/** Does a virtual build target exist?
447
* @param alias The virtual build target's name.
448
* @return True if the virtual build target exists, false if not.
450
bool HasVirtualBuildTarget(const wxString& alias) const;
452
/** Remove a virtual build target.
454
* @param alias The virtual build target's name.
455
* @return True if the virtual build target was removed, false if not.
457
bool RemoveVirtualBuildTarget(const wxString& alias);
459
/** Get a list of all defined virtual build targets.
461
* @return A list of all defined virtual build targets.
463
wxArrayString GetVirtualBuildTargets() const;
465
/** Access a virtual build target's group of build targets.
467
* @param alias The virtual build target's name.
468
* @return The list of all build targets under the alias @c alias.
470
const wxArrayString& GetVirtualBuildTargetGroup(const wxString& alias) const;
472
/** Access a virtual build target's expanded group of build targets.
474
* The difference from GetVirtualBuildTargetGroup() lies in that this function
475
* returns the full list of real build targets in this group (by recursively
476
* expanding virtual build targets in the group).
477
* @param alias The virtual build target's name.
478
* @return The expanded list of all real build targets under the alias @c alias.
480
wxArrayString GetExpandedVirtualBuildTargetGroup(const wxString& alias) const;
482
/** Checks if a build target (virtual or real) can be added to a virtual build target,
483
* without causing a circular-reference.
485
* @param alias The "parent" virtual build target to add the build target in.
486
* @param target The build target to add in the @c alias virtual build target.
487
* @return True if a circular reference is not detected, false if it is.
489
bool CanAddToVirtualBuildTarget(const wxString& alias, const wxString& target);
491
/** Request if a specific tree node can be dragged.
493
* @note Called by ProjectManager.
494
* @return True if it is allowed to drag this node, false not.
496
bool CanDragNode(wxTreeCtrl* tree, wxTreeItemId node);
498
/** Notify that a specific tree node has been dragged.
500
* @note Called by ProjectManager.
501
* @return True if succeeded, false if not.
503
bool NodeDragged(wxTreeCtrl* tree, wxTreeItemId from, wxTreeItemId to);
505
/** Notify that a virtual folder has been added.
506
* @return True if it is allowed, false if not. */
507
bool VirtualFolderAdded(wxTreeCtrl* tree, wxTreeItemId parent_node, const wxString& virtual_folder);
509
/** Notify that a virtual folder has been deleted. */
510
void VirtualFolderDeleted(wxTreeCtrl* tree, wxTreeItemId node);
512
/** Notify that a virtual folder has been renamed.
513
* @return True if the renaming is allowed, false if not. */
514
bool VirtualFolderRenamed(wxTreeCtrl* tree, wxTreeItemId node, const wxString& new_name);
516
/** Get a list of the virtual folders. Normally used by the project loader only.*/
517
const wxArrayString& GetVirtualFolders() const;
519
/** Set the virtual folders list. Normally used by the project loader only. */
520
void SetVirtualFolders(const wxArrayString& folders);
522
/** Returns the last modification time for the file. Used to detect modifications outside the Program. */
523
wxDateTime GetLastModificationTime() const { return m_LastModified; }
525
/** Sets the last modification time for the project to 'now'. Used to detect modifications outside the Program. */
528
/** Sets object names generation to extended/normal mode.
530
* In normal mode (the default), the file @c foo.cpp generates the @c foo.o object file.
531
* In extended mode, the file @c foo.cpp generates the @c foo.cpp.o object file.
533
* This option is useful for large projects containing similarly named files
534
* (in the same directory) differing only on their extensions. Using the
535
* extended mode with said projects guarantees that each object name will
538
* @param ext Set to true to switch to extended mode, false for normal mode.
540
void SetExtendedObjectNamesGeneration(bool ext);
542
/** Gets object names generation mode (extended/normal).
543
* @return True for extended mode, false for normal mode.
544
* @see SetExtendedObjectNamesGeneration.
546
bool GetExtendedObjectNamesGeneration() const;
548
/** Set notes on the project.
550
* @param notes Simple text notes about the project.
552
void SetNotes(const wxString& notes);
554
/** Get notes on the project.
556
* @return Simple text notes about the project.
558
const wxString& GetNotes() const;
560
/** Set show project notes on load automatically.
562
* @param show If true show project notes on load.
564
void SetShowNotesOnLoad(bool show);
566
/** Get show project notes on load automatically.
568
* @return True if show project notes on load is set, false if not.
570
bool GetShowNotesOnLoad() const;
572
/** Show project notes now.
574
* @param nonEmptyOnly If true, show notes only if non-empty.
575
* @param editable If true, the notes will be editable.
577
* @note If @c editable is true, the @c nonEmptyOnly parameter is ignored...
579
void ShowNotes(bool nonEmptyOnly, bool editable = false);
581
/** Changes project title
583
* This function overrides CompileTargetBase::SetTitle.
584
* It sends additional notification event to plugins
585
* and than calls base function.
587
virtual void SetTitle(const wxString& title);
591
void ExpandVirtualBuildTargetGroup(const wxString& alias, wxArrayString& result) const;
592
wxTreeItemId AddTreeNode(wxTreeCtrl* tree, const wxString& text, const wxTreeItemId& parent, bool useFolders, FileTreeData::FileTreeDataKind folders_kind, bool compiles, int image, FileTreeData* data = 0L);
593
wxTreeItemId FindNodeToInsertAfter(wxTreeCtrl* tree, const wxString& text, const wxTreeItemId& parent, bool in_folders); // alphabetical sorting
594
ProjectBuildTarget* AddDefaultBuildTarget();
595
int IndexOfBuildTargetName(const wxString& targetName) const;
596
wxString CreateUniqueFilename();
597
void NotifyPlugins(wxEventType type);
598
void CopyTreeNodeRecursively(wxTreeCtrl* tree, const wxTreeItemId& item, const wxTreeItemId& new_parent);
601
VirtualBuildTargetsMap m_VirtualTargets;
602
BuildTargets m_Targets;
603
wxString m_ActiveTarget;
604
wxString m_LastSavedActiveTarget;
605
wxString m_DefaultExecuteTarget;
607
bool m_CustomMakefile;
610
wxArrayString m_ExpandedNodes;
612
wxTreeItemId m_ProjectNode;
614
wxArrayString m_VirtualFolders; // not saved, just used throughout cbProject's lifetime
616
bool m_CurrentlyLoading;
617
wxString m_CommonTopLevelPath;
622
// hashmap for fast searches in cbProject::GetFileByFilename()
623
ProjectFiles m_ProjectFilesMap; // keeps UnixFilename(ProjectFile::relativeFilename)
624
ProjectBuildTarget* m_CurrentlyCompilingTarget;
626
wxDateTime m_LastModified;
628
bool m_ExtendedObjectNamesGeneration;
630
bool m_AutoShowNotesOnLoad;
633
#endif // CBPROJECT_H