source: Sophya/trunk/SophyaPI/PIext/piacmd.cc@ 4015

Last change on this file since 4015 was 3572, checked in by cmv, 17 years ago

char* -> const char* pour regler les problemes de deprecated string const... + comparaison unsigned signed + suppression EVOL_PLANCK rz+cmv 07/02/2009

File size: 12.1 KB
Line 
1#include "sopnamsp.h"
2#include "piacmd.h"
3#include <stdio.h>
4#include <stdlib.h>
5#include <ctype.h>
6#include <math.h>
7
8
9#include "pdlmgr.h"
10#include "ctimer.h"
11#include "strutil.h"
12#include "strutilxx.h"
13// #include "dlftypes.h"
14#include "srandgen.h"
15
16#include "pistdimgapp.h"
17#include "nobjmgr.h"
18
19// Les differentes classes CommandExecutor
20#include "basexecut.h"
21#include "graphexecut.h"
22#include "piafitting.h"
23#include "pawexecut.h"
24#include "cxxexecutor.h"
25#include "cxxexecwin.h"
26#include "contmodex.h"
27#include "flowmodex.h"
28
29#include PISTDWDG_H
30#include PILIST_H
31
32// ------------------------------------------------------------
33// Gestion d'une fenetre d'aide interactive
34// Classe PIAHelpWind
35// ------------------------------------------------------------
36
37#define HGRPMSGOFFSET 100000
38
39class PIAHelpWind : public PIWindow {
40public :
41 PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
42 virtual ~PIAHelpWind();
43 virtual void Show();
44 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
45 inline void AddHelpGroup(const char * hgrp, int gid)
46 { hgrpom->AppendItem(hgrp, HGRPMSGOFFSET+gid); }
47 inline void AddHelpGroup(string const & hgrp, int gid)
48 { hgrpom->AppendItem(hgrp.c_str(), HGRPMSGOFFSET+gid); }
49 inline void ClearHelpList()
50 { mNitem=0; hitemlist->DeleteAllItems(); }
51 inline void AddHelpItem(const char * hitem)
52 { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
53 inline void SetHelpTextLabel(string const & htxt, string const & hlab)
54 { mTxt->SetText(htxt); mLab->SetLabel(hlab); }
55
56protected :
57 PIStdImgApp* dap;
58 PIACmd* piac;
59 int mNitem;
60 PIList* hitemlist;
61 PIOptMenu* hgrpom;
62 PIButton * mBut;
63 PILabel * mLab;
64 PIText* mTxt;
65};
66
67/* --Methode-- */
68PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd)
69 : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350)
70{
71dap = par;
72piac = piacmd;
73mNitem = 0;
74SetMsg(77);
75
76int bsx, bsy;
77int tsx, tsy;
78int spx, spy;
79PIApplicationPrefCompSize(bsx, bsy);
80spx = bsx/6; spy = bsy/6;
81tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy;
82SetSize(tsx,tsy);
83hgrpom = new PIOptMenu(this, "hgrpoptmen", bsx*2.0, bsy, spx/2, spy);
84hgrpom->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
85hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-3*spy-bsy, spx/2, 2*spy+bsy);
86hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
87// hitemlist->SetBorderWidth(2);
88mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
89// mTxt->SetMutiLineMode(true);
90mTxt->SetTextEditable(false);
91mTxt->SetText("");
92mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
93mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy);
94mLab->SetBorderWidth(1);
95mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
96mLab->SetLabel("");
97mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
98mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
99}
100
101/* --Methode-- */
102PIAHelpWind::~PIAHelpWind()
103{
104delete hgrpom;
105delete hitemlist;
106delete mTxt;
107delete mLab;
108delete mBut;
109}
110
111/* --Methode-- */
112void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
113{
114PIMessage um = UserMsg(msg);
115if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
116 Hide();
117 return;
118 }
119else if ( (um >= HGRPMSGOFFSET) && (sender == hgrpom)) { // Selection de groupe de Help
120 mTxt->SetText("");
121 mLab->SetLabel("");
122 piac->UpdateHelpList(this, um-HGRPMSGOFFSET);
123}
124else if ( (um > 100) && (sender == hitemlist) && (ModMsg(msg) == PIMsg_Select) ) {
125 string s = hitemlist->GetSelectionStr();
126 mTxt->SetText(piac->GetUsage(s));
127 mLab->SetLabel(s);
128 }
129}
130
131/* --Methode-- */
132void PIAHelpWind::Show()
133{
134hgrpom->SetValue(HGRPMSGOFFSET); // Groupe All
135mTxt->SetText("");
136mLab->SetLabel("");
137piac->UpdateHelpList(this, 0);
138PIWindow::Show();
139}
140
141
142// ------------------------------------------------------------
143// Classe PIACmd
144// ------------------------------------------------------------
145
146// static PIACmd* curpiacmd = NULL;
147/* --Methode-- */
148PIACmd::PIACmd(PIStdImgApp* app)
149 : Commander()
150{
151mObjMgr = new NamedObjMgr;
152mImgApp = app;
153helpwin = new PIAHelpWind(app, this);
154helpwin_initdone = false;
155
156string grp = "PIACmd";
157string gdesc = "piapp interpreter (class PIACmd) additional builtin commands";
158AddHelpGroup(grp, gdesc);
159
160string kw = "exitpiapp";
161string usage = "To end the piapp session ";
162RegisterCommand(kw, usage, NULL, grp);
163kw = "helpwindow";
164usage = "To display the Help window ";
165RegisterCommand(kw, usage, NULL, grp);
166kw = "stop";
167usage = "To stop (break) interpreter (PIACmd/Commander) execution";
168RegisterCommand(kw, usage, NULL, grp);
169
170basexec = new PIABaseExecutor(this, mObjMgr, app);
171graphexec = new PIAGraphicExecutor(this, mObjMgr, app);
172fitexec = new PIAFitter(this, app);
173pawexec = new PAWExecutor(this, app);
174CxxExecutor * cxxe = new CxxExecutor(this, app);
175cxxexec = cxxe;
176
177ContModExecutor *cntxx = new ContModExecutor(this, app);//_OP_
178cntexec = cntxx; //_OP_
179FlowModExecutor *flwxx = new FlowModExecutor(this, app);//_OP_
180flwexec = flwxx; //_OP_
181
182cxxoptwin = new CxxOptionWind(app, cxxe);
183cxxexwin = new CxxExecWind(app, cxxe);
184
185InitializeHelpWindowMenu();
186// <ZThread> Thread control attributes
187fg_thrstop = false;
188fg_threxe = false;
189}
190
191/* --Methode-- */
192PIACmd::~PIACmd()
193{
194delete mObjMgr;
195delete helpwin;
196delete cxxexwin;
197delete cxxoptwin;
198delete basexec;
199delete graphexec;
200delete fitexec;
201delete pawexec;
202delete cxxexec;
203}
204
205
206/* --Methode-- */
207void PIACmd::AddInputLine(string const & line)
208{
209 if (line == "stop") {
210 StopExecution();
211 return;
212 }
213 mutx_inps.lock();
214 inputlines.push(line); // Ajout d'un element en fin de queue
215 mutx_inps.unlock();
216 mutx_inps.broadcast();
217}
218
219/* --Methode-- */
220void PIACmd::StopThread()
221{
222 fg_thrstop = true;
223 mutx_inps.broadcast();
224}
225
226/* --Methode-- */
227void PIACmd::run()
228{
229 while (!fg_thrstop) {
230 mutx_inps.lock();
231 while (inputlines.empty()) {
232 fg_threxe = false;
233 mutx_inps.wait();
234 }
235 string line = inputlines.front(); // On recupere l'element de tete
236 inputlines.pop(); // On supprime l'element de tete
237 fg_threxe = true;
238 mutx_inps.unlock();
239 try {
240 Interpret(line);
241 }
242 catch (PThrowable& pex) { // Catching SOPHYA exceptions
243 cerr << "PIACmd::run() SOPHYA exception in call to Interpret(" << line << ")\n"
244 << (string)typeid(pex).name() << " Msg= " << pex.Msg() << endl;
245 }
246 catch (std::exception& sex) {
247 cerr << "PIACmd::run() std::exception in call to Interpret(" << line << ")\n"
248 << (string)typeid(sex).name() << " Msg= " << sex.what() << endl;
249 }
250 catch (...) {
251 cerr << "PIACmd::run() unknown exception (...) in call to Interpret("
252 << line << ")" << endl;
253 }
254 }
255}
256
257/* --Methode-- */
258bool PIACmd::CheckHelpGrp(string& grp, int& gid, string& desc)
259{
260bool fgnewgrp = Commander::CheckHelpGrp(grp, gid, desc);
261if (helpwin_initdone && fgnewgrp) helpwin->AddHelpGroup(grp.c_str(), gid);
262return fgnewgrp;
263}
264
265/* --Methode-- */
266void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
267{
268helpwin->ClearHelpList();
269if (gid == 0) {
270 string htxt = "All registered commands";
271 string hlab = "All (HelpGroup)";
272 helpwin->SetHelpTextLabel(htxt, hlab);
273}
274else {
275 CmdHGroup::iterator ith;
276 for(ith = cmdhgrp.begin(); ith != cmdhgrp.end(); ith++) {
277 if ((*ith).second.gid == gid) {
278 string hlab = (*ith).first;
279 hlab += " (HelpGroup)";
280 helpwin->SetHelpTextLabel((*ith).second.desc, hlab);
281 break;
282 }
283 }
284}
285CmdExmap::iterator it;
286for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
287 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
288 helpwin->AddHelpItem((*it).first.c_str());
289 }
290for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
291 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
292 helpwin->AddHelpItem((*it).first.c_str());
293 }
294}
295
296/* --Methode-- */
297void PIACmd::InitializeHelpWindowMenu()
298{
299 helpwin->AddHelpGroup("All", 0);
300 CmdHGroup::iterator it;
301 for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++)
302 helpwin->AddHelpGroup((*it).first, (*it).second.gid);
303 helpwin_initdone = true;
304}
305
306/* Fonction */
307static string GetStringFrStdin(PIACmd* piac)
308{
309PIStdImgApp* piapp = piac->GetImgApp();
310if (piapp) {
311 PIBaseWdg* wdg = piapp->CurrentBaseWdg();
312 if (wdg) wdg->Refresh();
313}
314char buff[128];
315fgets(buff, 128, stdin);
316buff[127] = '\0';
317return((string)buff);
318}
319
320/* --Methode-- */
321bool PIACmd::GetVarApp(string const & vn, string & vv)
322{
323 vv = "";
324 if (mObjMgr == NULL) return false;
325 vector<string> opts;
326 SplitStringToVString(vn, opts, '.');
327 string nom = opts[0];
328 opts.erase(opts.begin());
329 NObjMgrAdapter* obja = mObjMgr->GetObjAdapter(nom);
330 if (obja == NULL) return false;
331 vv = obja->GetInfoString(opts);
332 return true;
333}
334
335/* --Methode-- */
336int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
337{
338int rc = 0;
339NamedObjMgr omg;
340
341// >>>>>>>>>>> Commande d'interpreteur
342if ( (kw == "helpwindow") && (mImgApp)) {
343 ZSync zs(mImgApp->getMutex());
344 ShowHelpWindow();
345 zs.NOp();
346 return 0;
347}
348// ----> Sortie d'application
349else if ((kw == "exitpiapp") && (mImgApp)) {
350 mImgApp->Stop();
351 return 0;
352}
353else return Commander::ExecuteCommandLine(kw, tokens, toks);
354return 0;
355}
356
357
358/* --Methode-- */
359void PIACmd::ShowHelpWindow()
360{
361 helpwin->Show();
362}
363
364/* --Methode-- */
365void PIACmd::ShowCxxOptionWindow()
366{
367 cxxoptwin->Show();
368}
369
370/* --Methode-- */
371void PIACmd::ShowCxxExecWindow()
372{
373 cxxexwin->Show();
374}
375
376/* --Methode-- */
377void PIACmd::SetCurrentPrompt(const char* pr)
378{
379 Commander::SetCurrentPrompt(pr);
380 if (mImgApp) {
381 ZSync zs(mImgApp->getMutex()); zs.NOp();
382 mImgApp->GetConsole()->SetPrompt(pr);
383 }
384}
385
386/* --Methode-- */
387void PIACmd::ShowMessage(const char * msg, int att)
388{
389 /*
390 Il faut faire quelque chose pour gerer correctement ca en multithread
391 char va = (att == 0) ? 0 : PIVA_Magenta;
392 if (mImgApp) {
393 ZSync(mImgApp->getMutex()); zs.NOp();
394 mImgApp->GetConsole()->AddStr(msg, va);
395 }
396 */
397Commander::ShowMessage(msg, att);
398}
399
400
401/* --Methode-- */
402SOpExObj::SOpExObj(PIStdImgApp* app)
403{
404 mImgApp = app;
405 fg_thrstop = false;
406 fg_threxe = false;
407}
408
409/* --Methode-- */
410SOpExObj::~SOpExObj()
411{
412}
413
414/* --Methode-- */
415void SOpExObj::AddOpe(string const & line)
416{
417 mutx_inps.lock();
418 inputlines.push(line); // Ajout d'un element en fin de queue
419 mutx_inps.unlock();
420 mutx_inps.broadcast();
421}
422
423/* --Methode-- */
424void SOpExObj::run()
425{
426 NamedObjMgr omg;
427 while (!fg_thrstop) {
428 mutx_inps.lock();
429 while (inputlines.empty()) {
430 fg_threxe = false;
431 mutx_inps.wait();
432 }
433 string cmd = inputlines.front().substr(0,3); // On recupere l'element de tete
434 string nom = inputlines.front().substr(4); // On recupere l'element de tete
435 inputlines.pop(); // On supprime l'element de tete
436 fg_threxe = true;
437 mutx_inps.unlock();
438 try {
439 if (cmd == "UPD") { // UpdateObjMgrWindow()
440 int did = atoi(nom.c_str());
441 omg.UpdateObjMgrWindow(did);
442 }
443 else if (cmd == "SCO") { // set cobj
444 string ss = "set cobj ";
445 ss += nom;
446 cout << " Defining cobj : " << ss << endl;
447 mImgApp->CmdInterpreter()->AddInputLine(ss);
448 }
449 else if (cmd == "DIS") { // display obj
450 omg.DisplayObj(nom);
451 }
452 else if (cmd == "PRT") { // print obj
453 omg.PrintObj(nom);
454 }
455 else if (cmd == "DEL") { // delete obj
456 omg.DelObj(nom);
457 }
458 else if (cmd == "SAV") { // saveppf obj
459 if (mImgApp->mPpfout) omg.SaveObj(nom, *(mImgApp->mPpfout), false);
460 }
461 else cerr << "SOpExObj::run() ERROR CMD= " << cmd << endl;
462 }
463 catch (PThrowable& pex) { // Catching SOPHYA exceptions
464 cerr << "SOpExObj::run() SOPHYA exception CMD=" << cmd << " NOM=" << nom << "\n"
465 << (string)typeid(pex).name() << " Msg= " << pex.Msg() << endl;
466 }
467 catch (std::exception& sex) {
468 cerr << "SOpExObj::run() std::exception CMD=" << cmd << " NOM=" << nom << "\n"
469 << (string)typeid(sex).name() << " Msg= " << sex.what() << endl;
470 }
471 catch (...) {
472 cerr << "SOpExObj::run() unknown exception (...) CMD="
473 << cmd << " NOM=" << nom << endl;
474 }
475 }
476}
Note: See TracBrowser for help on using the repository browser.