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

Last change on this file since 3215 was 3215, checked in by ansari, 18 years ago

Remplacement de XtResizeWidget/XtMoveWidget par XtMakeGeometryRequest (pour regler le problemes menu/bouton inactif... sur AIX et MacPowerPC), Reza 12/04/2007

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