0023682: Implement Draw_Window with Cocoa framework
[occt.git] / src / Draw / Draw_PloadCommands.cxx
CommitLineData
b311480e 1// Created on: 2003-10-09
2// Created by: Mikhail KUZMITCHEV
3// Copyright (c) 2003-2012 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
7fd59977 20
21#include <TCollection_AsciiString.hxx>
22#include <OSD_Path.hxx>
23#include <OSD_Directory.hxx>
24#include <OSD_File.hxx>
25#include <OSD_Environment.hxx>
26#include <Resource_Manager.hxx>
27#include <Draw_Interpretor.hxx>
28#include <Draw_MapOfAsciiString.hxx>
29#include <Draw.hxx>
30
31static Handle(Resource_Manager) myResources;
32
33//=======================================================================
34//function : FindPluginFile
35//purpose : Searches for the existence of the plugin file according to its name thePluginName:
36// - if thePluginName is empty then it defaults to DrawPlugin
37// - the search directory is defined according to the variable
38// CSF_<filename>Defaults (if it is omitted then it defaults to
39// $CASROOT/src/DrawResources)
40// - finally existence of the file is verified in the search directory
41// - if the file exists but corresponding variable (CSF_...) has not been
42// explicitly set, it is forced to (for further reuse by Resource_Manager)
43// Returns True if the file exists, otherwise - False.
44//=======================================================================
45
46#define FAILSTR "Failed to load plugin: "
47
48//static Standard_Boolean FindPluginFile (TCollection_AsciiString& thePluginName)
49static Standard_Boolean FindPluginFile (TCollection_AsciiString& thePluginName, TCollection_AsciiString& aPluginDir)
50{
51 Standard_Boolean aResult = Standard_True;
52
53 // check if the file name has been specified and use default value if not
54 if (thePluginName.IsEmpty()) {
55 thePluginName += "DrawPlugin";
56#ifdef DEB
57 cout << "Plugin file name has not been specified. Defaults to " << thePluginName.ToCString() << endl;
58#endif
59 }
60
61 //TCollection_AsciiString aPluginDir; // the search directory
62 Standard_Boolean aDirFound = Standard_True, aToSetCSFVariable = Standard_False;
63
64 // the order of search : by CSF_<PluginFileName>Defaults and then by CASROOT
65 TCollection_AsciiString aCSFVariable = TCollection_AsciiString ("CSF_") + thePluginName + "Defaults";
66 aPluginDir = getenv (aCSFVariable.ToCString());
67
68 if (aPluginDir.IsEmpty()) {
69 // now try by CASROOT
70 aPluginDir = getenv("CASROOT");
71
72 if ( !aPluginDir.IsEmpty() ) {
73 aPluginDir +="/src/DrawResources" ;
74 aToSetCSFVariable = Standard_True; //CSF variable to be set later
75 } else {
76 aResult = aDirFound = Standard_False;
77 cout << FAILSTR "Neither " << aCSFVariable.ToCString() << ", nor CASROOT variables have been set" << endl;
78 }
79 }
80
81 if (aDirFound) {
82 // search directory name has been constructed, now check whether it and the file exist
83
84 TCollection_AsciiString aPluginFileName = aPluginDir + "/" + thePluginName;
85 OSD_File PluginFile ( aPluginFileName );
86 if ( PluginFile.Exists() ) {
87 if (aToSetCSFVariable) {
88 OSD_Environment aCSFVarEnv ( aCSFVariable, aPluginDir );
89 aCSFVarEnv.Build();
90#ifdef DEB
91 cout << "Variable " << aCSFVariable.ToCString() << " has not been explicitly defined. Set to " << aPluginDir.ToCString() << endl;
92#endif
93 if ( aCSFVarEnv.Failed() ) {
94 aResult = Standard_False;
95 cout << FAILSTR "Failed to initialize " << aCSFVariable.ToCString() << " with " << aPluginDir.ToCString() << endl;
96 }
97 }
98 } else {
99 aResult = Standard_False;
100 cout << FAILSTR "File " << aPluginFileName.ToCString() << " not found" << endl;
101 }
102 }
103
104 return aResult;
105}
106
107//=======================================================================
108//function : Parse
109//purpose : Parse the input keys to atomic keys (<key> --> <akey>[<akey> ..])
110//=======================================================================
111
112static void Parse (Draw_MapOfAsciiString& theMap)
113{
114 Draw_MapOfAsciiString aMap, aMap2;
115 Standard_Integer j, k;
116 Standard_Integer aMapExtent, aMap2Extent;
117 aMapExtent = theMap.Extent();
118 for(j = 1; j <= aMapExtent; j++) {
119 if (!myResources.IsNull()) {
120 const TCollection_AsciiString& aKey = theMap.FindKey(j);
121 TCollection_AsciiString aResource = aKey;
122 if(myResources->Find(aResource.ToCString())) {
123#ifdef DEB
124 cout << "Parse Value ==> " << myResources->Value(aResource.ToCString()) << endl;
125#endif
126 TCollection_AsciiString aValue(myResources->Value(aResource.ToCString()));
127 // parse aValue string
128 Standard_Integer i=1;
129 for(;;) {
130 TCollection_AsciiString aCurKey = aValue.Token(" \t,", i++);
131#ifdef DEB
132 cout << "Parse aCurKey = " << aCurKey.ToCString() << endl;
133#endif
134 if(aCurKey.IsEmpty()) break;
135 if(!myResources->Find(aCurKey.ToCString())) {
136 // It is toolkit
137 aMap.Add(aResource);
138 }
139 else
140 aMap2.Add(aCurKey);
141 }
142 } else
143 cout <<"Pload : Resource = " << aResource << " is not found" << endl;
144 if(!aMap2.IsEmpty())
145 Parse(aMap2);
146 //
147 aMap2Extent = aMap2.Extent();
148 for(k = 1; k <= aMap2Extent; k++) {
149 aMap.Add(aMap2.FindKey(k));
150 }
151
152 }
153 }
154
155 theMap.Assign(aMap);
156}
157
158//=======================================================================
159//function : Pload
160//purpose :
161//=======================================================================
162
163static Standard_Integer Pload (Draw_Interpretor& di,
164 Standard_Integer n,
165 const char** argv)
166{
167 char adef[] = "-";
168 TCollection_AsciiString aPluginFileName("");
169 TCollection_AsciiString aPluginDir(""), aPluginDir2("");
170 Standard_Integer aStart = 0;
171 Standard_Integer aFinish = n - 1;
172
173 if (n == 1) {
174 // Load DEFAULT key
175 aStart = 0;
176 } else {
177 if(argv[1][0] == adef[0]) {
178 aPluginFileName = argv[1];
179 aPluginFileName.Remove(1,1);
180 if (n == 2) {
181 // Load DEFAULT key from aPluginFileName file
182 aStart = 0;
183 aFinish = n - 2;
184 } else {
185 aStart = 2;
186 }
187 } else {
188 aStart = 1;
189 }
190 }
191
192 //if ( !FindPluginFile (aPluginFileName) ) {
193 if ( !FindPluginFile (aPluginFileName, aPluginDir) ) {
194 return 1;
195 }
196
197 Draw_MapOfAsciiString aMap;
198 TCollection_AsciiString aDEFAULT("DEFAULT");
199 //for(Standard_Integer i = aStart; i < n; i++)
200 for(Standard_Integer i = aStart; i <= aFinish; i++)
201 if (i == 0) {
202 // Load DEFAULT key
203 aMap.Add(aDEFAULT);
204 } else {
205 TCollection_AsciiString aTK(argv[i]);
206 aMap.Add(aTK);
207 }
208
209 //myResources = new Resource_Manager(aPluginFileName.ToCString());
210 myResources = new Resource_Manager(aPluginFileName.ToCString(), aPluginDir, aPluginDir2, Standard_False);
211
212 Parse(aMap);
213 Standard_Integer j;
214 Standard_Integer aMapExtent;
215 aMapExtent = aMap.Extent();
216 for(j = 1; j <= aMapExtent; j++) {
217 const TCollection_AsciiString& aKey = aMap.FindKey(j);
218 TCollection_AsciiString aResource = aKey;
219#ifdef DEB
220 cout << "aResource = " << aResource << endl;
221#endif
222 if(myResources->Find(aResource.ToCString())) {
223 const TCollection_AsciiString& aValue = myResources->Value(aResource.ToCString());
224#ifdef DEB
225 cout << "Value ==> " << aValue << endl;
226#endif
227
228 //Draw::Load(di, aKey, aPluginFileName);
229 Draw::Load(di, aKey, aPluginFileName, aPluginDir, aPluginDir2, Standard_False);
230
231 // Load TclScript
232 TCollection_AsciiString aCSFVariable ("CSF_DrawPluginTclDir");
233 TCollection_AsciiString aTclScriptDir;
234 aTclScriptDir = getenv (aCSFVariable.ToCString());
235 TCollection_AsciiString aTclScriptFileName;
236 TCollection_AsciiString aTclScriptFileNameDefaults;
237 aTclScriptFileName = aTclScriptDir + "/" + aValue + ".tcl";
238 aTclScriptFileNameDefaults = aPluginDir + "/" + aValue + ".tcl";
239 OSD_File aTclScriptFile ( aTclScriptFileName );
240 OSD_File aTclScriptFileDefaults ( aTclScriptFileNameDefaults );
241 if (!aTclScriptDir.IsEmpty() && aTclScriptFile.Exists()) {
242#ifdef DEB
243 cout << "Load " << aTclScriptFileName << " TclScript" << endl;
244#endif
245 di.EvalFile( aTclScriptFileName.ToCString() );
246 } else if (!aPluginDir.IsEmpty() && aTclScriptFileDefaults.Exists()) {
247#ifdef DEB
248 cout << "Load " << aTclScriptFileNameDefaults << " TclScript" << endl;
249#endif
250 di.EvalFile( aTclScriptFileNameDefaults.ToCString() );
251 }
252
253 } else
254 cout <<"Pload : Resource = " << aResource << " is not found" << endl;
255 }
256 return 0;
257}
258
259//=======================================================================
260//function : PloadCommands
261//purpose :
262//=======================================================================
263
264void Draw::PloadCommands(Draw_Interpretor& theCommands)
265{
266 static Standard_Boolean Done = Standard_False;
267 if (Done) return;
268 Done = Standard_True;
269
270 const char* g = "Draw Plugin";
271
272 theCommands.Add("pload" , "pload [-PluginFilename] [[Key1] [Key2] ...]: Loads Draw plugins " ,
273 __FILE__, Pload, g);
274}