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
RevLine 
[2615]1#include "sopnamsp.h"
[293]2#include "piacmd.h"
[165]3#include <stdio.h>
4#include <stdlib.h>
[349]5#include <ctype.h>
[165]6#include <math.h>
7
[293]8#include "basexecut.h"
[165]9
[293]10#include "pdlmgr.h"
[165]11#include "ctimer.h"
[2214]12#include "strutil.h"
[2215]13#include "strutilxx.h"
[293]14// #include "dlftypes.h"
[2307]15#ifdef SANS_EVOLPLANCK
[2308]16#include "nbrandom.h"
[2307]17#else
18#include "srandgen.h"
19#endif
[165]20
[293]21#include "pistdimgapp.h"
[165]22#include "nobjmgr.h"
[361]23#include "piafitting.h"
[463]24#include "pawexecut.h"
[1224]25#include "cxxexecutor.h"
[1251]26#include "cxxexecwin.h"
[1828]27#include "contmodex.h"
[1920]28#include "flowmodex.h"
[165]29
[293]30#include PISTDWDG_H
31#include PILIST_H
[165]32
[293]33// ------------------------------------------------------------
[349]34// Gestion d'une fenetre d'aide interactive
35// Classe PIAHelpWind
[293]36// ------------------------------------------------------------
[165]37
[2474]38#define HGRPMSGOFFSET 100000
39
[293]40class PIAHelpWind : public PIWindow {
41public :
42 PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
43 virtual ~PIAHelpWind();
[330]44 virtual void Show();
[293]45 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
[330]46 inline void AddHelpGroup(const char * hgrp, int gid)
[2474]47 { hgrpom->AppendItem(hgrp, HGRPMSGOFFSET+gid); }
[2465]48 inline void AddHelpGroup(string const & hgrp, int gid)
[2474]49 { hgrpom->AppendItem(hgrp.c_str(), HGRPMSGOFFSET+gid); }
[330]50 inline void ClearHelpList()
51 { mNitem=0; hitemlist->DeleteAllItems(); }
[293]52 inline void AddHelpItem(const char * hitem)
[330]53 { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
[2465]54 inline void SetHelpTextLabel(string const & htxt, string const & hlab)
55 { mTxt->SetText(htxt); mLab->SetLabel(hlab); }
56
[293]57protected :
58 PIStdImgApp* dap;
59 PIACmd* piac;
60 int mNitem;
61 PIList* hitemlist;
[330]62 PIOptMenu* hgrpom;
[293]63 PIButton * mBut;
64 PILabel * mLab;
65 PIText* mTxt;
66};
[165]67
[293]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;
[330]75SetMsg(77);
[165]76
[293]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);
[330]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);
[293]87hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
88// hitemlist->SetBorderWidth(2);
[324]89mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
90// mTxt->SetMutiLineMode(true);
[293]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("");
[330]98mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
[293]99mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
100}
[165]101
102/* --Methode-- */
[293]103PIAHelpWind::~PIAHelpWind()
104{
[330]105delete hgrpom;
[293]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);
[330]116if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
[293]117 Hide();
118 return;
119 }
[2474]120else if ( (um >= HGRPMSGOFFSET) && (sender == hgrpom)) { // Selection de groupe de Help
[330]121 mTxt->SetText("");
122 mLab->SetLabel("");
[2474]123 piac->UpdateHelpList(this, um-HGRPMSGOFFSET);
[330]124}
125else if ( (um > 100) && (sender == hitemlist) && (ModMsg(msg) == PIMsg_Select) ) {
[293]126 string s = hitemlist->GetSelectionStr();
127 mTxt->SetText(piac->GetUsage(s));
128 mLab->SetLabel(s);
129 }
130}
131
[330]132/* --Methode-- */
133void PIAHelpWind::Show()
134{
[2474]135hgrpom->SetValue(HGRPMSGOFFSET); // Groupe All
[330]136mTxt->SetText("");
137mLab->SetLabel("");
138piac->UpdateHelpList(this, 0);
139PIWindow::Show();
140}
[293]141
[349]142
143// ------------------------------------------------------------
144// Classe PIACmd
145// ------------------------------------------------------------
146
[2463]147// static PIACmd* curpiacmd = NULL;
[293]148/* --Methode-- */
[2490]149PIACmd::PIACmd(PIStdImgApp* app)
[2463]150 : Commander()
[165]151{
[2490]152mObjMgr = new NamedObjMgr;
[165]153mImgApp = app;
[293]154helpwin = new PIAHelpWind(app, this);
[2465]155helpwin_initdone = false;
[293]156
[2463]157string kw = "exitpiapp";
[2305]158string grp = "PIACmd";
[2465]159string gdesc = "piapp interpreter (class PIACmd) additional builtin commands";
160AddHelpGroup(grp, gdesc);
161
[2463]162string usage = "To end the piapp session ";
[2419]163RegisterCommand(kw, usage, NULL, grp);
[2463]164kw = "helpwindow";
165usage = "To display the Help window ";
[2419]166RegisterCommand(kw, usage, NULL, grp);
[2203]167
[2490]168basexec = new PIABaseExecutor(this, mObjMgr, app);
[361]169fitexec = new PIAFitter(this, app);
[463]170pawexec = new PAWExecutor(this, app);
[1251]171CxxExecutor * cxxe = new CxxExecutor(this, app);
172cxxexec = cxxe;
[1828]173
174ContModExecutor *cntxx = new ContModExecutor(this, app);//_OP_
175cntexec = cntxx; //_OP_
[1920]176FlowModExecutor *flwxx = new FlowModExecutor(this, app);//_OP_
177flwexec = flwxx; //_OP_
[1828]178
[1251]179cxxoptwin = new CxxOptionWind(app, cxxe);
180cxxexwin = new CxxExecWind(app, cxxe);
181
[2465]182InitializeHelpWindowMenu();
[2486]183// <ZThread> Thread control attributes
184fg_thrstop = false;
185fg_threxe = false;
[165]186}
187
188/* --Methode-- */
189PIACmd::~PIACmd()
190{
[2490]191delete mObjMgr;
[293]192delete helpwin;
[1251]193delete cxxexwin;
194delete cxxoptwin;
[361]195delete basexec;
196delete fitexec;
[463]197delete pawexec;
[1224]198delete cxxexec;
[165]199}
200
201
[293]202/* --Methode-- */
[2486]203void PIACmd::AddInputLine(string const & line)
204{
205 mutx_inps.lock();
[2536]206 inputlines.push(line); // Ajout d'un element en fin de queue
[2486]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 }
[2536]227 string line = inputlines.front(); // On recupere l'element de tete
228 inputlines.pop(); // On supprime l'element de tete
[2486]229 fg_threxe = true;
230 mutx_inps.unlock();
[2606]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 }
[2486]246 }
247}
248
249/* --Methode-- */
[2465]250bool PIACmd::CheckHelpGrp(string& grp, int& gid, string& desc)
[330]251{
[2465]252bool fgnewgrp = Commander::CheckHelpGrp(grp, gid, desc);
253if (helpwin_initdone && fgnewgrp) helpwin->AddHelpGroup(grp.c_str(), gid);
254return fgnewgrp;
[330]255}
256
257/* --Methode-- */
258void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
259{
260helpwin->ClearHelpList();
[2465]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}
[330]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
[2465]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}
[165]297
[357]298/* Fonction */
[368]299static string GetStringFrStdin(PIACmd* piac)
[357]300{
[368]301PIStdImgApp* piapp = piac->GetImgApp();
302if (piapp) {
303 PIBaseWdg* wdg = piapp->CurrentBaseWdg();
304 if (wdg) wdg->Refresh();
[357]305#ifndef __mac__
[368]306 /* On vide le buffer X-Window */
[374]307 XSync(PIXDisplay(),False);
[357]308#endif
[368]309}
[357]310char buff[128];
311fgets(buff, 128, stdin);
312buff[127] = '\0';
313return((string)buff);
314}
315
[165]316
[1262]317/* --Methode-- */
318int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
319{
320int rc = 0;
321NamedObjMgr omg;
[349]322
[165]323// >>>>>>>>>>> Commande d'interpreteur
[2465]324if (kw == "helpwindow") {
325 ShowHelpWindow();
326 return 0;
327}
[2307]328// ----> Sortie d'application
329else if (kw == "exitpiapp") {
330 mImgApp->Stop();
[2465]331 return 0;
[2307]332}
[2463]333else return Commander::ExecuteCommandLine(kw, tokens, toks);
[2465]334return 0;
[165]335}
336
[333]337
[165]338/* --Methode-- */
[293]339void PIACmd::ShowHelpWindow()
[165]340{
[293]341helpwin->Show();
[165]342}
[357]343
[463]344/* --Methode-- */
[1251]345void PIACmd::ShowCxxOptionWindow()
346{
347cxxoptwin->Show();
348}
349
350/* --Methode-- */
351void PIACmd::ShowCxxExecWindow()
352{
353cxxexwin->Show();
354}
355
[2463]356/* --Methode-- */
357void PIACmd::SetCurrentPrompt(const char* pr)
[2377]358{
[2463]359 Commander::SetCurrentPrompt(pr);
[2489]360 if (mImgApp) {
361 ZSync(mImgApp->getMutex());
362 mImgApp->GetConsole()->SetPrompt(pr);
363 }
[2377]364}
365
[1251]366/* --Methode-- */
[2463]367void PIACmd::ShowMessage(const char * msg, int att)
[463]368{
[2489]369 /*
370 Il faut faire quelque chose pour gerer correctement ca en multithread
[2463]371 char va = (att == 0) ? 0 : PIVA_Magenta;
[2489]372 if (mImgApp) {
373 ZSync(mImgApp->getMutex());
374 mImgApp->GetConsole()->AddStr(msg, va);
375 }
376 */
377Commander::ShowMessage(msg, att);
[2263]378}
Note: See TracBrowser for help on using the repository browser.