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

Last change on this file since 458 was 458, checked in by ercodmgr, 26 years ago

Correction pb sur Sun - Reza 10/99

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