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