source: Sophya/trunk/SophyaPI/PI/piapplx.cc@ 135

Last change on this file since 135 was 135, checked in by ercodmgr, 27 years ago

Petits bugs + modir App::Redirect() 14/08/98 Reza

File size: 6.8 KB
RevLine 
[2]1#include <stdio.h>
[18]2#include <X11/cursorfont.h>
[2]3#include "piapplx.h"
4#include "picontainerx.h"
5
6
[78]7// Pour rediriger stdout
8#include <unistd.h>
9#include <fcntl.h>
[2]10
[135]11#include <iostream.h>
[78]12
[135]13
[78]14// #define DEBUG_APPLX
15
[18]16static Cursor a_curs[3];
17static bool a_fgcur=false;
18
[60]19// Voir fichier pimenux.cc , Pour resoudre certains conflits avec les WindowManagers
20void SetTopWdgForMenuX(SysDWdg mtw);
21
[111]22static PIApplication* cur_piapp = NULL;
23PIApplication* PIApplicationGetApp() { return cur_piapp; }
24
[126]25
[132]26// Classe de container special pour top-container de Application X
[126]27
[132]28class PITopContAppX : public PIContainer {
29public :
30 PITopContAppX(PIContainer *par, char *nom,
31 int sx=10, int sy=10, int px=0, int py=0);
32 virtual ~PITopContAppX();
33
34 virtual void SetSize(int sx, int sy);
35 virtual void SetPos(int px, int py);
36};
37
[2]38/* --Methode-- */
[132]39PITopContAppX::PITopContAppX(PIContainer *par, char *nom, int sx, int sy, int px, int py)
40 : PIContainer(par, nom, sx, sy, px, py)
41{
42 SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_fixed);
43}
44
45/* --Methode-- */
46PITopContAppX::~PITopContAppX()
47{
48}
49
50/* --Methode-- */
51void PITopContAppX::SetSize(int sx, int sy)
52{
53Parent()->SetSize(sx+XPos(), sy+YPos());
54PIContainer::SetSize(sx, sy);
55}
56
57/* --Methode-- */
58void PITopContAppX::SetPos(int px, int py)
59{
60}
61
62/* --Methode-- */
[2]63PIApplicationX::PIApplicationX(int sx, int sy, int narg, char *arg[])
64: PIApplicationGen()
65{
66int sxt, syt;
67
68#ifdef DEBUG_APPLX
69puts("PIApplicationX::PIApplicationX()_info : App creation");
70#endif
71mStop = true;
72topwdg = new PIWdgX(narg, arg);
73
74intcont = new PIContainerX((PIMsgHandler *)this, topwdg, "MBCont",
75 10, 10, 0, 0);
[60]76// Pb avec les Popup MenuX et certains Window Manager , voir pimenux.cc
77SetTopWdgForMenuX(intcont->XtWdg());
78
[2]79menubar = new PIMenubar(this, "DefMenubar");
[23]80Menubar()->SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_free);
[2]81sxt = ( sx > Menubar()->XSize() ) ? sx : Menubar()->XSize();
82syt = ( sy > 0 ) ? sy : 0;
[92]83int msx,msy;
84PrefCompSz(msx, msy);
[7]85//syt += Menubar()->YSize();
[92]86syt += msy;
[2]87MBCont()->SetSize(sxt, syt);
[132]88if (sx < sxt) sx = sxt;
89if (sy < 10) sy = 10;
90topcont = new PITopContAppX(MBCont(), "TopLevelCont",
91 sx, sy, 0, msy);
92topcont->Show();
[2]93
94MBCont()->Show();
95topwdg->Manage();
[18]96
97if (!a_fgcur)
98 {
99 Display *mdsp;
100 mdsp = XtDisplay (topwdg->XtWdg());
101 a_curs[0] = XCreateFontCursor(mdsp, XC_arrow);
102 a_curs[1] = XCreateFontCursor(mdsp, XC_X_cursor);
103 a_curs[2] = XCreateFontCursor(mdsp, XC_watch);
104 }
105
[111]106cur_piapp = this;
[18]107mState = -1;
108SetReady();
[2]109}
110
111/* --Methode-- */
112PIApplicationX::~PIApplicationX()
113{
114#ifdef DEBUG_APPLX
115puts("PIApplicationX::~PIApplicationX()_info : App delete");
116#endif
[18]117Display *mdsp;
118mdsp = XtDisplay (topwdg->XtWdg());
[52]119topwdg->UnManage();
[6]120delete menubar;
121if (topcont != MBCont()) delete topcont;
122delete intcont;
[2]123delete topwdg;
[18]124XtCloseDisplay(mdsp);
[111]125cur_piapp = NULL;
[18]126return;
[2]127}
128
129/* --Methode-- */
130void PIApplicationX::Run()
131{
132XEvent evt;
133#ifdef DEBUG_APPLX
134puts("PIApplicationX::Run()_info : App Run ");
135#endif
136
137topwdg->SetSize(MBCont()->XSize(), MBCont()->YSize());
138
[114]139int szx, szy, szf;
140XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
[90]141
[110]142// Pour appeler FinishCreate() des objets dans la fenetre principale
143MBCont()->FinishCreate();
[2]144while (mStop)
145 {
[90]146 XtAppNextEvent(*appctx, &evt);
[2]147 XtDispatchEvent(&evt);
148 }
149return;
150}
[18]151
152
153/* --Methode-- */
154void PIApplicationX::SetReady()
155{
156if (mState != kReadyState)
157 {
158 Display * mdsp;
159 mState = kReadyState;
[106]160 Menubar()->SetSensitive();
[18]161 mdsp = XtDisplay (topwdg->XtWdg());
162 XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[0]);
163 XFlush(mdsp);
164 }
165}
166
167/* --Methode-- */
168void PIApplicationX::SetBusy()
169{
170if (mState != kBusyState)
171 {
172 Display * mdsp;
173 mState = kBusyState;
[106]174 Menubar()->SetSensitive();
[18]175 mdsp = XtDisplay (topwdg->XtWdg());
176// if ( XtIsRealized(topwdg->XtWdg()) )
177 XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[2]);
178 XFlush(mdsp);
179 }
180return;
181}
182
183/* --Methode-- */
184void PIApplicationX::SetBlocked()
185{
186if (mState != kBlockedState)
187 {
188 Display * mdsp;
189 mState = kBlockedState;
[106]190 Menubar()->SetUnSensitive();
[18]191 mdsp = XtDisplay (topwdg->XtWdg());
192 XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[1]);
193 XFlush(mdsp);
194 }
195}
[75]196
197
198/* --Methode-- */
[90]199void PIApplicationX::PrefCompSz(int& szx, int& szy)
[75]200{
[114]201int szf;
202PIXtAppCtx(szx, szy, szf);
[90]203return;
[75]204}
205
[92]206/* --Methode-- */
207void PIApplicationX::ScreenSz(int& szx, int& szy)
208{
209Display * dsp = PIXDisplay();
210szx = DisplayWidth(dsp, DefaultScreen(dsp));
211szy = DisplayHeight(dsp, DefaultScreen(dsp));
212}
[75]213
[92]214
215
[78]216/* Call-Back - Fonction privee de ce fichier */
[81]217static void redirectstream_callback(XtPointer, int *, XtInputId*);
[78]218
[81]219static PIConsole* consstream[2] = {NULL, NULL};
220static unsigned char streamva[2] = { PIVA_Def, PIVA_Ital};
221static int streamno[2] = {0,1};
[135]222static XtInputId inputid[2];
223static int origfiledes[2]={-1, -1}; // descripteurs de fichiers de depart
224static bool firstcall[2]={true, true}; // 1er appel a Redirect
[78]225
[81]226static void redirectstream_callback(XtPointer cld, int * fd, XtInputId* /*iid*/)
[78]227{
228char buff[128];
229int nr;
[81]230
231int idx = *((int*)cld);
232if (idx != 1) idx = 0;
[135]233if (!consstream[idx]) return;
[78]234while ( (nr=read(*fd, buff, 127)) > 0 ) {
[81]235 buff[nr] = '\0'; consstream[idx]->AddStr(buff, streamva[idx], false);
[78]236 }
[81]237consstream[idx]->Refresh();
[78]238}
239
240/* --Methode-- */
[81]241void PIApplicationX::RedirectOutStream(PIConsole* cons, unsigned char va)
[78]242{
[135]243if ( origfiledes[0]<0 ) origfiledes[0] = fcntl(1, F_DUPFD);
244if ( cons == consstream[0]) return;
245if ( (consstream[0]) && (cons) ) { consstream[0] = cons; streamva[0] = va; return; }
246else if (!cons) {
247 consstream[0] = NULL;
248 XtRemoveInput(inputid[0]);
249 dup2(origfiledes[0], 1);
250 return;
251 }
[78]252
[81]253consstream[0] = cons; streamva[0] = va;
254
[78]255int p[2];
256pipe(p);
[90]257// Redirection de stdout (fid=1) :
[78]258close(1);
259dup(p[1]);
260close(p[1]);
261fcntl(p[0], F_SETFL, O_NONBLOCK);
[135]262
[85]263setlinebuf(stdout);
[135]264ios::sync_with_stdio();
[78]265
[114]266int szx, szy, szf;
267XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
[135]268inputid[0] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
269 redirectstream_callback, (XtPointer) streamno);
[78]270}
[81]271
272/* --Methode-- */
273void PIApplicationX::RedirectErrStream(PIConsole* cons, unsigned char va)
274{
[135]275if ( origfiledes[1]<0 ) origfiledes[1] = fcntl(2, F_DUPFD);
276if ( cons == consstream[1]) return;
277if ( (consstream[1]) && (cons) ) { consstream[1] = cons; streamva[1] = va; return; }
278else if (!cons) {
279 consstream[1] = NULL;
280 XtRemoveInput(inputid[1]);
281 dup2(origfiledes[1], 2);
282 return;
283 }
[81]284
285consstream[1] = cons; streamva[1] = va;
286
287int p[2];
288pipe(p);
[135]289// Redirection de stderr (fid=2) :
[81]290close(2);
291dup(p[1]);
292close(p[1]);
293fcntl(p[0], F_SETFL, O_NONBLOCK);
[135]294
[85]295setlinebuf(stderr);
[135]296ios::sync_with_stdio();
[81]297
[114]298int szx, szy, szf;
299XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
[135]300inputid[1] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
301 redirectstream_callback, (XtPointer) (streamno+1));
[81]302}
303
Note: See TracBrowser for help on using the repository browser.