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