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