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