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