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

Last change on this file since 2992 was 2812, checked in by ansari, 20 years ago

Synchronisation avec boucle d'evenement (ZSync) pour les commandes fitw et helpwindow - Reza 28/06/2005

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