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

Last change on this file since 3216 was 3216, checked in by ansari, 18 years ago

suppression ligne de print de debug ds piapplx/gen.cc commite par erreur, Reza 12/04/2007

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