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 | // Probleme sur Sun - Reza 10/99 , REGLE 08/2000
|
---|
67 |
|
---|
68 | mTopWdg->SetSize(sx+px, sy+py);
|
---|
69 |
|
---|
70 | XtConfigureWidget(Parent()->XtWdg(), 0, 0, sx+px, sy+py, 0);
|
---|
71 | XtConfigureWidget(XtWdg(), px, py, sx, sy, 0);
|
---|
72 | // SetPos(px, py);
|
---|
73 | // PIContainer::SetSize(sx, sy);
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | /* --Methode-- */
|
---|
78 | void PITopContAppX::SetPos(int px, int py)
|
---|
79 | {
|
---|
80 | }
|
---|
81 |
|
---|
82 | /* --Methode-- */
|
---|
83 | PIApplicationX::PIApplicationX(int sx, int sy, int narg, char *arg[])
|
---|
84 | : PIApplicationGen()
|
---|
85 | {
|
---|
86 | int sxt, syt;
|
---|
87 |
|
---|
88 | #ifdef DEBUG_APPLX
|
---|
89 | puts("PIApplicationX::PIApplicationX()_info : App creation");
|
---|
90 | #endif
|
---|
91 | mStop = true;
|
---|
92 |
|
---|
93 |
|
---|
94 | topwdg = new PIWdgX(narg, arg);
|
---|
95 |
|
---|
96 | int msx,msy;
|
---|
97 | PrefCompSz(msx, msy);
|
---|
98 | msy+=5;
|
---|
99 | topwdg->SetSize(sx, sy+msy);
|
---|
100 | //DBG printf("PIApplicationX-DBG:: Size=%d %d \n", topwdg->XSize(), topwdg->YSize());
|
---|
101 |
|
---|
102 | intcont = new PIContainerX((PIMsgHandler *)this, topwdg, "MBCont",
|
---|
103 | sx, sy+msy, 0, 0);
|
---|
104 | // Pb avec les Popup MenuX et certains Window Manager , voir pimenux.cc
|
---|
105 | SetTopWdgForMenuX(intcont->XtWdg());
|
---|
106 |
|
---|
107 | menubar = new PIMenubar(this, "DefMenubar");
|
---|
108 | Menubar()->SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_free);
|
---|
109 | sxt = ( sx > Menubar()->XSize() ) ? sx : Menubar()->XSize();
|
---|
110 | syt = ( sy > 10 ) ? sy : 10;
|
---|
111 | // syt += Menubar()->YSize();
|
---|
112 | // syt += msy;
|
---|
113 | // topwdg->SetSize(sxt, syt);
|
---|
114 | // MBCont()->SetSize(sxt, syt);
|
---|
115 | if (sx < sxt) sx = sxt;
|
---|
116 | if (sy < 10) sy = 10;
|
---|
117 | topcont = new PITopContAppX(MBCont(), "TopLevelCont", topwdg,
|
---|
118 | sx, sy, 0, msy);
|
---|
119 | MBCont()->Show();
|
---|
120 | //DBG printf("PIApplicationX-DBG2:: MBContSize=%d %d \n", MBCont()->XSize(), MBCont()->YSize());
|
---|
121 |
|
---|
122 |
|
---|
123 | topcont->Show();
|
---|
124 |
|
---|
125 |
|
---|
126 | if (!a_fgcur)
|
---|
127 | {
|
---|
128 | Display *mdsp;
|
---|
129 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
130 | a_curs[0] = XCreateFontCursor(mdsp, XC_arrow);
|
---|
131 | a_curs[1] = XCreateFontCursor(mdsp, XC_X_cursor);
|
---|
132 | a_curs[2] = XCreateFontCursor(mdsp, XC_watch);
|
---|
133 | }
|
---|
134 |
|
---|
135 | topwdg->Manage();
|
---|
136 | cur_piapp = this;
|
---|
137 | mState = -1;
|
---|
138 | SetReady();
|
---|
139 | }
|
---|
140 |
|
---|
141 | /* --Methode-- */
|
---|
142 | PIApplicationX::~PIApplicationX()
|
---|
143 | {
|
---|
144 | #ifdef DEBUG_APPLX
|
---|
145 | puts("PIApplicationX::~PIApplicationX()_info : App delete");
|
---|
146 | #endif
|
---|
147 | Display *mdsp;
|
---|
148 | mdsp = XtDisplay (topwdg->XtWdg());
|
---|
149 | topwdg->UnManage();
|
---|
150 | delete menubar;
|
---|
151 | if (topcont != MBCont()) delete topcont;
|
---|
152 | delete intcont;
|
---|
153 | delete topwdg;
|
---|
154 | XtCloseDisplay(mdsp);
|
---|
155 | cur_piapp = NULL;
|
---|
156 | return;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /* --Methode-- */
|
---|
160 | void PIApplicationX::Run()
|
---|
161 | {
|
---|
162 | XEvent evt;
|
---|
163 | #ifdef DEBUG_APPLX
|
---|
164 | puts("PIApplicationX::Run()_info : App Run ");
|
---|
165 | #endif
|
---|
166 |
|
---|
167 |
|
---|
168 | int szx, szy, szf;
|
---|
169 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
170 |
|
---|
171 | // Pour appeler FinishCreate() des objets dans la fenetre principale
|
---|
172 | if (mStop) { // C'est la premiere fois
|
---|
173 | topcont->SetSize(topcont->XSize(), topcont->YSize());
|
---|
174 | // topwdg->SetSize(MBCont()->XSize(), MBCont()->YSize());
|
---|
175 | MBCont()->FinishCreate();
|
---|
176 | }
|
---|
177 | else mStop = true; // On rerentre apres un stop
|
---|
178 | while (mStop)
|
---|
179 | {
|
---|
180 | XtAppNextEvent(*appctx, &evt);
|
---|
181 | XtDispatchEvent(&evt);
|
---|
182 | }
|
---|
183 | return;
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | /* --Methode-- */
|
---|
188 | void PIApplicationX::SetReady()
|
---|
189 | {
|
---|
190 | Display * mdsp = XtDisplay (topwdg->XtWdg());
|
---|
191 | if (mState != kReadyState)
|
---|
192 | {
|
---|
193 | mState = kReadyState;
|
---|
194 | Menubar()->SetSensitive();
|
---|
195 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[0]);
|
---|
196 | }
|
---|
197 | XFlush(mdsp);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /* --Methode-- */
|
---|
201 | void PIApplicationX::SetBusy()
|
---|
202 | {
|
---|
203 | Display * mdsp = XtDisplay (topwdg->XtWdg());
|
---|
204 | if (mState != kBusyState)
|
---|
205 | {
|
---|
206 | mState = kBusyState;
|
---|
207 | Menubar()->SetSensitive();
|
---|
208 | // if ( XtIsRealized(topwdg->XtWdg()) )
|
---|
209 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[2]);
|
---|
210 | }
|
---|
211 | XFlush(mdsp);
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* --Methode-- */
|
---|
215 | void PIApplicationX::SetBlocked()
|
---|
216 | {
|
---|
217 | Display * mdsp = XtDisplay (topwdg->XtWdg());
|
---|
218 | if (mState != kBlockedState)
|
---|
219 | {
|
---|
220 | mState = kBlockedState;
|
---|
221 | Menubar()->SetUnSensitive();
|
---|
222 | XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[1]);
|
---|
223 | }
|
---|
224 | XFlush(mdsp);
|
---|
225 | return;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | /* --Methode-- */
|
---|
230 | void PIApplicationX::PrefCompSz(int& szx, int& szy)
|
---|
231 | {
|
---|
232 | int szf;
|
---|
233 | PIXtAppCtx(szx, szy, szf);
|
---|
234 | return;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /* --Methode-- */
|
---|
238 | void PIApplicationX::ScreenSz(int& szx, int& szy)
|
---|
239 | {
|
---|
240 | Display * dsp = PIXDisplay();
|
---|
241 | szx = DisplayWidth(dsp, DefaultScreen(dsp));
|
---|
242 | szy = DisplayHeight(dsp, DefaultScreen(dsp));
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 |
|
---|
247 | /* Call-Back - Fonction privee de ce fichier */
|
---|
248 | static void redirectstream_callback(XtPointer, int *, XtInputId*);
|
---|
249 |
|
---|
250 | static PIConsole* consstream[2] = {NULL, NULL};
|
---|
251 | static unsigned char streamva[2] = { PIVA_Def, PIVA_Ital};
|
---|
252 | static int streamno[2] = {0,1};
|
---|
253 | static XtInputId inputid[2];
|
---|
254 | static int origfiledes[2]={-1, -1}; // descripteurs de fichiers de depart
|
---|
255 |
|
---|
256 | static void redirectstream_callback(XtPointer cld, int * fd, XtInputId* /*iid*/)
|
---|
257 | {
|
---|
258 | char buff[128];
|
---|
259 | int nr;
|
---|
260 |
|
---|
261 | int idx = *((int*)cld);
|
---|
262 | if (idx != 1) idx = 0;
|
---|
263 | if (!consstream[idx]) return;
|
---|
264 | while ( (nr=read(*fd, buff, 127)) > 0 ) {
|
---|
265 | buff[nr] = '\0'; consstream[idx]->AddStr(buff, streamva[idx], false);
|
---|
266 | }
|
---|
267 | consstream[idx]->Refresh();
|
---|
268 | }
|
---|
269 |
|
---|
270 | /* --Methode-- */
|
---|
271 | void PIApplicationX::RedirectOutStream(PIConsole* cons, unsigned char va)
|
---|
272 | {
|
---|
273 | if ( origfiledes[0]<0 ) origfiledes[0] = fcntl(1, F_DUPFD);
|
---|
274 | if ( cons == consstream[0]) return;
|
---|
275 | if ( (consstream[0]) && (cons) ) { consstream[0] = cons; streamva[0] = va; return; }
|
---|
276 | else if (!cons) {
|
---|
277 | consstream[0] = NULL;
|
---|
278 | XtRemoveInput(inputid[0]);
|
---|
279 | dup2(origfiledes[0], 1);
|
---|
280 | return;
|
---|
281 | }
|
---|
282 |
|
---|
283 | consstream[0] = cons; streamva[0] = va;
|
---|
284 |
|
---|
285 | int p[2];
|
---|
286 | pipe(p);
|
---|
287 | // Redirection de stdout (fid=1) :
|
---|
288 | close(1);
|
---|
289 | dup(p[1]);
|
---|
290 | close(p[1]);
|
---|
291 | fcntl(p[0], F_SETFL, O_NONBLOCK);
|
---|
292 |
|
---|
293 | #if (!defined(__GNUG__) && !defined(HPUX))
|
---|
294 | setlinebuf(stdout);
|
---|
295 | #endif
|
---|
296 | ios::sync_with_stdio();
|
---|
297 |
|
---|
298 | int szx, szy, szf;
|
---|
299 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
300 | inputid[0] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
|
---|
301 | redirectstream_callback, (XtPointer) streamno);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /* --Methode-- */
|
---|
305 | void PIApplicationX::RedirectErrStream(PIConsole* cons, unsigned char va)
|
---|
306 | {
|
---|
307 | if ( origfiledes[1]<0 ) origfiledes[1] = fcntl(2, F_DUPFD);
|
---|
308 | if ( cons == consstream[1]) return;
|
---|
309 | if ( (consstream[1]) && (cons) ) { consstream[1] = cons; streamva[1] = va; return; }
|
---|
310 | else if (!cons) {
|
---|
311 | consstream[1] = NULL;
|
---|
312 | XtRemoveInput(inputid[1]);
|
---|
313 | dup2(origfiledes[1], 2);
|
---|
314 | return;
|
---|
315 | }
|
---|
316 |
|
---|
317 | consstream[1] = cons; streamva[1] = va;
|
---|
318 |
|
---|
319 | int p[2];
|
---|
320 | pipe(p);
|
---|
321 | // Redirection de stderr (fid=2) :
|
---|
322 | close(2);
|
---|
323 | dup(p[1]);
|
---|
324 | close(p[1]);
|
---|
325 | fcntl(p[0], F_SETFL, O_NONBLOCK);
|
---|
326 |
|
---|
327 | #if (!defined(__GNUG__) && !defined(HPUX))
|
---|
328 | setlinebuf(stderr);
|
---|
329 | #endif
|
---|
330 | ios::sync_with_stdio();
|
---|
331 |
|
---|
332 | int szx, szy, szf;
|
---|
333 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
|
---|
334 | inputid[1] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
|
---|
335 | redirectstream_callback, (XtPointer) (streamno+1));
|
---|
336 | }
|
---|
337 |
|
---|