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

Last change on this file since 2615 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

File size: 9.9 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
116
117topwdg = new PIWdgX(narg, arg);
118
119int msx,msy;
120PrefCompSize(msx, msy);
121msy+=5;
122topwdg->SetSize(sx, sy+msy);
123//DBG printf("PIApplicationX-DBG:: Size=%d %d \n", topwdg->XSize(), topwdg->YSize());
124
125intcont = new PIContainerX((PIMsgHandler *)this, topwdg, "MBCont",
126 sx, sy+msy, 0, 0);
127// Pb avec les Popup MenuX et certains Window Manager , voir pimenux.cc
128SetTopWdgForMenuX(intcont->XtWdg());
129
130menubar = new PIMenubar(this, "DefMenubar");
131Menubar()->SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_free);
132sxt = ( sx > Menubar()->XSize() ) ? sx : Menubar()->XSize();
133syt = ( sy > 10 ) ? sy : 10;
134// syt += Menubar()->YSize();
135// syt += msy;
136// topwdg->SetSize(sxt, syt);
137// MBCont()->SetSize(sxt, syt);
138if (sx < sxt) sx = sxt;
139if (sy < 10) sy = 10;
140topcont = new PITopContAppX(MBCont(), "TopLevelCont", topwdg,
141 sx, sy, 0, msy);
142MBCont()->Show();
143//DBG printf("PIApplicationX-DBG2:: MBContSize=%d %d \n", MBCont()->XSize(), MBCont()->YSize());
144
145
146topcont->Show();
147
148
149if (!a_fgcur)
150 {
151 Display *mdsp;
152 mdsp = XtDisplay (topwdg->XtWdg());
153 a_curs[0] = XCreateFontCursor(mdsp, XC_arrow);
154 a_curs[1] = XCreateFontCursor(mdsp, XC_X_cursor);
155 a_curs[2] = XCreateFontCursor(mdsp, XC_watch);
156 }
157
158topwdg->Manage();
159
160// ------------------------------------------------------
161// Pour la gestion des close window des Window manager
162
163if (fgactl == 0) {
164 XtActionsRec desact = {(char *)"CloseWindow" ,CloseWindow};
165 int szx, szy, szf;
166 XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
167 XtAppAddActions(*appctx, &desact, 1);
168 Display *mdsp;
169 mdsp = XtDisplay (topwdg->XtWdg());
170 Atom wmd;
171 wmd = XInternAtom(mdsp, "WM_DELETE_WINDOW",False);
172 XSetWMProtocols(mdsp, XtWindow(topwdg->XtWdg()), &wmd, 1);
173 fgactl = 1;
174}
175
176XtTranslations trans;
177trans = XtParseTranslationTable("<Message>WM_PROTOCOLS:CloseWindow()");
178XtOverrideTranslations(topwdg->XtWdg() , trans);
179Arg warg[2];
180XtSetArg(warg[0], XmNdeleteResponse, XmDO_NOTHING);
181XtSetValues(topwdg->XtWdg(), warg, 1);
182
183XtAddCallback(topwdg->XtWdg(), XtNpopdownCallback,
184 (XtCallbackProc)popdwn_cb_app, (XtPointer)this);
185
186// ------------------------------------------------------
187
188cur_piapp = this;
189mState = -1;
190SetReady();
191}
192
193/* --Methode-- */
194PIApplicationX::~PIApplicationX()
195{
196#ifdef DEBUG_APPLX
197puts("PIApplicationX::~PIApplicationX()_info : App delete");
198#endif
199Display *mdsp;
200mdsp = XtDisplay (topwdg->XtWdg());
201topwdg->UnManage();
202delete menubar;
203if (topcont != MBCont()) delete topcont;
204delete intcont;
205delete topwdg;
206XtCloseDisplay(mdsp);
207cur_piapp = NULL;
208return;
209}
210
211/* --Methode-- */
212void PIApplicationX::Run()
213{
214XEvent evt;
215#ifdef DEBUG_APPLX
216puts("PIApplicationX::Run()_info : App Run ");
217#endif
218
219
220int szx, szy, szf;
221XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
222
223// Pour appeler FinishCreate() des objets dans la fenetre principale
224if (mStop) { // C'est la premiere fois
225 topcont->SetSize(topcont->XSize(), topcont->YSize());
226 // topwdg->SetSize(MBCont()->XSize(), MBCont()->YSize());
227 MBCont()->FinishCreate();
228 }
229else mStop = true; // On rerentre apres un stop
230while (mStop)
231 {
232 XtAppNextEvent(*appctx, &evt);
233 XtDispatchEvent(&evt);
234 }
235return;
236}
237
238
239/* --Methode-- */
240void PIApplicationX::SetReady()
241{
242Display * mdsp = XtDisplay (topwdg->XtWdg());
243if (mState != kReadyState)
244 {
245 mState = kReadyState;
246 Menubar()->SetSensitive();
247 XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[0]);
248 }
249XFlush(mdsp);
250}
251
252/* --Methode-- */
253void PIApplicationX::SetBusy()
254{
255Display * mdsp = XtDisplay (topwdg->XtWdg());
256if (mState != kBusyState)
257 {
258 mState = kBusyState;
259 Menubar()->SetSensitive();
260// if ( XtIsRealized(topwdg->XtWdg()) )
261 XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[2]);
262 }
263XFlush(mdsp);
264}
265
266/* --Methode-- */
267void PIApplicationX::SetBlocked()
268{
269Display * mdsp = XtDisplay (topwdg->XtWdg());
270if (mState != kBlockedState)
271 {
272 mState = kBlockedState;
273 Menubar()->SetUnSensitive();
274 XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[1]);
275 }
276XFlush(mdsp);
277return;
278}
279
280
281/* --Methode-- */
282void PIApplicationX::PrefCompSize(int& szx, int& szy)
283{
284int szf;
285PIXtAppCtx(szx, szy, szf);
286return;
287}
288
289/* --Methode-- */
290void PIApplicationX::ScreenSize(int& szx, int& szy)
291{
292Display * dsp = PIXDisplay();
293szx = DisplayWidth(dsp, DefaultScreen(dsp));
294szy = DisplayHeight(dsp, DefaultScreen(dsp));
295}
296
297/* --Methode-- */
298void PIApplicationX::ScreenSizeMM(int& szx, int& szy)
299{
300Display * dsp = PIXDisplay();
301szx = DisplayWidthMM(dsp, DefaultScreen(dsp));
302szy = DisplayHeightMM(dsp, DefaultScreen(dsp));
303}
304
305/* --Methode-- */
306void PIApplicationX::ScreenResolution(int& resolx, int& resoly)
307{
308Display * dsp = PIXDisplay();
309resolx = (int)((double)DisplayWidth(dsp, DefaultScreen(dsp))*10./
310 (double)DisplayWidthMM(dsp, DefaultScreen(dsp)));
311resoly = (int)((double)DisplayHeight(dsp, DefaultScreen(dsp))*10./
312 (double)DisplayHeightMM(dsp, DefaultScreen(dsp)));
313}
314
315
316
317
318
319/* Call-Back - Fonction privee de ce fichier */
320static void redirectstream_callback(XtPointer, int *, XtInputId*);
321
322static PIConsole* consstream[2] = {NULL, NULL};
323static unsigned char streamva[2] = { PIVA_Def, PIVA_Ital};
324static int streamno[2] = {0,1};
325static XtInputId inputid[2];
326static int origfiledes[2]={-1, -1}; // descripteurs de fichiers de depart
327
328static void redirectstream_callback(XtPointer cld, int * fd, XtInputId* /*iid*/)
329{
330char buff[512];
331int nr;
332
333int idx = *((int*)cld);
334if (idx != 1) idx = 0;
335if (!consstream[idx]) return;
336while ( (nr=read(*fd, buff, 512)) > 0 ) {
337 buff[nr] = '\0'; consstream[idx]->AddStr(buff, streamva[idx], false);
338 }
339consstream[idx]->Refresh();
340}
341
342/* --Methode-- */
343void PIApplicationX::RedirectOutStream(PIConsole* cons, unsigned char va)
344{
345if ( origfiledes[0]<0 ) origfiledes[0] = fcntl(1, F_DUPFD);
346if ( cons == consstream[0]) return;
347if ( (consstream[0]) && (cons) ) { consstream[0] = cons; streamva[0] = va; return; }
348else if (!cons) {
349 consstream[0] = NULL;
350 XtRemoveInput(inputid[0]);
351 dup2(origfiledes[0], 1);
352 return;
353 }
354
355consstream[0] = cons; streamva[0] = va;
356
357int p[2];
358pipe(p);
359// Redirection de stdout (fid=1) :
360close(1);
361dup(p[1]);
362close(p[1]);
363fcntl(p[0], F_SETFL, O_NONBLOCK);
364
365#if (!defined(__GNUG__) && !defined(HPUX))
366setlinebuf(stdout);
367#endif
368ios::sync_with_stdio();
369
370int szx, szy, szf;
371XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
372inputid[0] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
373 redirectstream_callback, (XtPointer) streamno);
374}
375
376/* --Methode-- */
377void PIApplicationX::RedirectErrStream(PIConsole* cons, unsigned char va)
378{
379if ( origfiledes[1]<0 ) origfiledes[1] = fcntl(2, F_DUPFD);
380if ( cons == consstream[1]) return;
381if ( (consstream[1]) && (cons) ) { consstream[1] = cons; streamva[1] = va; return; }
382else if (!cons) {
383 consstream[1] = NULL;
384 XtRemoveInput(inputid[1]);
385 dup2(origfiledes[1], 2);
386 return;
387 }
388
389consstream[1] = cons; streamva[1] = va;
390
391int p[2];
392pipe(p);
393// Redirection de stderr (fid=2) :
394close(2);
395dup(p[1]);
396close(p[1]);
397fcntl(p[0], F_SETFL, O_NONBLOCK);
398
399#if (!defined(__GNUG__) && !defined(HPUX))
400setlinebuf(stderr);
401#endif
402ios::sync_with_stdio();
403
404int szx, szy, szf;
405XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
406inputid[1] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
407 redirectstream_callback, (XtPointer) (streamno+1));
408}
409
Note: See TracBrowser for help on using the repository browser.