Add remaining files
[juce-lv2.git] / juce / source / extras / Introjucer / Source / Project / jucer_NewFileWizard.cpp
blobb0b7109ad9beb463279445121253ba176ecda015
1 /*
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
44 public:
45 NewCppFileWizard() {}
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);
63 return true;
66 showFailedToWriteMessage (newFile);
67 return false;
71 //==============================================================================
72 class NewHeaderFileWizard : public NewFileWizard::Type
74 public:
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);
93 return true;
96 showFailedToWriteMessage (newFile);
97 return false;
101 //==============================================================================
102 class NewCppAndHeaderFileWizard : public NewFileWizard::Type
104 public:
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(),
137 wildcard);
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];
172 if (wiz != nullptr)
174 wiz->createNewFile (projectGroupToAddTo);
175 return true;
178 return false;
181 void NewFileWizard::registerWizard (Type* newWizard)
183 wizards.add (newWizard);