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

Last change on this file since 2615 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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