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

Last change on this file since 2607 was 2607, checked in by ansari, 21 years ago

Supression de l'attribut par defaut PIVA_Ital ds PIApplication::RedirectErrStream() (pb avec largeur caracteres italiques different de caracteres roman) - augmentation de la taille du buffer callback redirection stdout,stderr - Reza 7 Septembre 2004

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