source: Sophya/trunk/SophyaPI/PI/piapplx.cc@ 2607

Last change on this file since 2607 was 2607, checked in by ansari, 21 years ago

Supression de l'attribut par defaut PIVA_Ital ds PIApplication::RedirectErrStream() (pb avec largeur caracteres italiques different de caracteres roman) - augmentation de la taille du buffer callback redirection stdout,stderr - Reza 7 Septembre 2004

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