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

Last change on this file since 1133 was 1133, checked in by ercodmgr, 25 years ago

petit nettoyage lie a SunOS/XmForm - Reza 23/8/2000

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