2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-10 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "jucer_NewFileWizard.h"
29 //==============================================================================
30 static bool fillInNewCppFileTemplate (const File
& file
, const Project::Item
& item
, const char* templateName
)
32 String s
= item
.getProject().getFileTemplate (templateName
)
33 .replace ("FILENAME", file
.getFileName(), false)
34 .replace ("DATE", Time::getCurrentTime().toString (true, true, true), false)
35 .replace ("AUTHOR", SystemStats::getFullUserName(), false)
36 .replace ("HEADERGUARD", CodeHelpers::makeHeaderGuardName (file
), false);
38 return FileHelpers::overwriteFileWithNewDataIfDifferent (file
, s
);
41 //==============================================================================
42 class NewCppFileWizard
: public NewFileWizard::Type
46 ~NewCppFileWizard() {}
48 const String
getName() { return "CPP File"; }
50 void createNewFile (Project::Item parent
)
52 const File
newFile (askUserToChooseNewFile ("SourceCode.cpp", "*.cpp", parent
));
54 if (newFile
!= File::nonexistent
)
55 create (parent
, newFile
);
58 static bool create (Project::Item parent
, const File
& newFile
)
60 if (fillInNewCppFileTemplate (newFile
, parent
, "jucer_NewCppFileTemplate_cpp"))
62 parent
.addFile (newFile
, 0);
66 showFailedToWriteMessage (newFile
);
71 //==============================================================================
72 class NewHeaderFileWizard
: public NewFileWizard::Type
75 NewHeaderFileWizard() {}
76 ~NewHeaderFileWizard() {}
78 const String
getName() { return "Header File"; }
80 void createNewFile (Project::Item parent
)
82 const File
newFile (askUserToChooseNewFile ("SourceCode.h", "*.h", parent
));
84 if (newFile
!= File::nonexistent
)
85 create (parent
, newFile
);
88 static bool create (Project::Item parent
, const File
& newFile
)
90 if (fillInNewCppFileTemplate (newFile
, parent
, "jucer_NewCppFileTemplate_h"))
92 parent
.addFile (newFile
, 0);
96 showFailedToWriteMessage (newFile
);
101 //==============================================================================
102 class NewCppAndHeaderFileWizard
: public NewFileWizard::Type
105 NewCppAndHeaderFileWizard() {}
106 ~NewCppAndHeaderFileWizard() {}
108 const String
getName() { return "CPP & Header File"; }
110 void createNewFile (Project::Item parent
)
112 const File
newFile (askUserToChooseNewFile ("SourceCode.h", "*.h;*.cpp", parent
));
114 if (newFile
!= File::nonexistent
)
116 if (NewHeaderFileWizard::create (parent
, newFile
.withFileExtension ("h")))
117 NewCppFileWizard::create (parent
, newFile
.withFileExtension ("cpp"));
122 //==============================================================================
123 void NewFileWizard::Type::showFailedToWriteMessage (const File
& file
)
125 AlertWindow::showMessageBox (AlertWindow::WarningIcon
,
126 "Failed to Create File!",
127 "Couldn't write to the file: " + file
.getFullPathName());
130 const File
NewFileWizard::Type::askUserToChooseNewFile (const String
& suggestedFilename
, const String
& wildcard
,
131 const Project::Item
& projectGroupToAddTo
)
133 FileChooser
fc ("Select File to Create",
134 projectGroupToAddTo
.determineGroupFolder()
135 .getChildFile (suggestedFilename
)
136 .getNonexistentSibling(),
139 if (fc
.browseForFileToSave (true))
140 return fc
.getResult();
142 return File::nonexistent
;
145 //==============================================================================
146 NewFileWizard::NewFileWizard()
148 registerWizard (new NewCppFileWizard());
149 registerWizard (new NewHeaderFileWizard());
150 registerWizard (new NewCppAndHeaderFileWizard());
153 NewFileWizard::~NewFileWizard()
155 clearSingletonInstance();
158 juce_ImplementSingleton_SingleThreaded (NewFileWizard
)
160 static const int menuBaseID
= 0x12d83f0;
162 void NewFileWizard::addWizardsToMenu (PopupMenu
& m
) const
164 for (int i
= 0; i
< wizards
.size(); ++i
)
165 m
.addItem (menuBaseID
+ i
, "Add New " + wizards
.getUnchecked(i
)->getName() + "...");
168 bool NewFileWizard::runWizardFromMenu (int chosenMenuItemID
, const Project::Item
& projectGroupToAddTo
) const
170 Type
* wiz
= wizards
[chosenMenuItemID
- menuBaseID
];
174 wiz
->createNewFile (projectGroupToAddTo
);
181 void NewFileWizard::registerWizard (Type
* newWizard
)
183 wizards
.add (newWizard
);