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