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