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