source: Sophya/trunk/SophyaPI/PI/Tests/pit1.cc@ 458

Last change on this file since 458 was 403, checked in by ercodmgr, 26 years ago

Ameliorations mineures du menu d'options PIConsole - Reza 10/09/99

File size: 14.8 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4
5#include <iostream.h>
6
7#include "pisysdep.h"
8#include PIAPP_H
9#include PIWIN_H
10#include PIMENU_H
11#include PISTDWDG_H
12#include PIBWDG_H
13#include "piscdrawwdg.h"
14#include "pievthandler.h"
15#include "parradapter.h"
16
17
18/* Exemple d'utilisation PIBaseWdg - Creation d'une nouvelle composante graphique */
19
20// ---- Declaration de la classe ExBWdg --------
21class ExBWdg : public PIBaseWdg
22{
23public:
24// Les methodes ...
25
26 ExBWdg(PIContainerGen *par, char *nom,
27 int sx=10, int sy=10, int px=0, int py=0);
28 virtual ~ExBWdg();
29
30// Traitement des evenements
31// virtual void Resize();
32 virtual void Draw(PIGraphic* g, int x0, int y0, int dx, int dy);
33
34 virtual void But1Press(int x, int y);
35 virtual void But1Release(int x, int y);
36 virtual void But2Press(int x, int y);
37 virtual void But2Release(int x, int y);
38 virtual void But3Press(int x, int y);
39 virtual void But3Release(int x, int y);
40 virtual void Keyboard(int key, PIKeyModifier kmod);
41
42protected:
43 int mfnt, mmrk;
44 int nc;
45 char str[32];
46};
47
48
49/* ........................................................... */
50/* Classe ExBWdg (Implementation des methodes) */
51/* ........................................................... */
52
53/* --Methode-- */
54ExBWdg::ExBWdg(PIContainerGen *par, char *nom,
55 int sx, int sy, int px, int py)
56: PIBaseWdg(par, nom, sx, sy, px, py)
57
58{
59mmrk = mfnt = nc = 0;
60strcpy(str, "Hello !");
61for(int i=strlen(str); i<20; i++) str[i] = ' ';
62str[20] = '\0';
63
64ActivateKeyboard();
65ActivateButton(1);
66ActivateButton(2);
67ActivateButton(3);
68}
69
70/* --Methode-- */
71ExBWdg::~ExBWdg()
72{
73}
74
75/* --Methode--
76void ExBWdg::Resize()
77{
78}
79*/
80
81/* --Methode-- */
82void ExBWdg::Draw(PIGraphicGen* g, int /*x0*/, int /*y0*/, int /*dx*/, int /*dy*/)
83{
84PIFontAtt att[3] = { PI_RomanFont, PI_BoldFont, PI_ItalicFont};
85PIMarker pmk[11] = { PI_DotMarker , PI_PlusMarker, PI_CrossMarker,
86 PI_CircleMarker, PI_FCircleMarker,
87 PI_BoxMarker, PI_FBoxMarker,
88 PI_TriangleMarker, PI_FTriangleMarker,
89 PI_StarMarker, PI_FStarMarker };
90EraseWindow();
91g->SelFont(PI_NormalSizeFont, att[mfnt]);
92g->SelMarker(8, pmk[mmrk]);
93
94g->SelLine(PI_NormalLine);
95g->DrawBox(10,10, XSize()-20, YSize()-20);
96
97g->SelLine(PI_ThinLine);
98g->DrawFBox(20, 20, 20, 20);
99g->DrawCircle(30, 30, 15);
100
101g->SelForeground(PI_Red);
102g->DrawFCircle(70, 30, 15);
103
104g->SelForeground(PI_Yellow);
105g->DrawFBox(10, 60, 120, 20);
106
107PIFont f0;
108g->SelFont(f0);
109g->SelForeground(PI_Black);
110g->DrawString(20, 110, str);
111PIFont f(PI_TimesFont);
112g->SelFont(f);
113g->DrawString(20, 130, "Times-Font");
114PIFont f2(14,PI_CourierFont,PI_BoldFont);
115g->SelFont(f2);
116g->DrawString(20, 150, "Courier-Font-Bold");
117PIFont f3(14,PI_TimesFont,PI_BoldFont);
118g->SelFont(f3);
119g->DrawString(20, 170, "Times-Font-Bold");
120PIFont f4(14,PI_HelveticaFont,PI_BoldFont);
121g->SelFont(f4);
122g->DrawString(20, 190, "Helvetica-Font-Bold");
123PIFont f5(14,PI_SymbolFont);
124g->SelFont(f5);
125g->DrawString(20, 210, "Symbol-Font");
126g->SelFont(f0);
127
128
129g->SelLine(PI_ThinLine);
130g->DrawLine(20, 140, 50, 130);
131g->SelLine(PI_NormalLine);
132g->DrawLine(50, 140, 80, 130);
133g->SelLine(PI_ThickLine);
134g->DrawLine(80, 140, 110, 130);
135
136g->SelForeground(PI_Magenta);
137g->DrawFCircle(120, 30, 15);
138g->SelForeground(PI_Grey);
139g->DrawFCircle(120, 60, 15);
140
141
142
143
144PIGrCoord x[5] = { 150, 165, 180, 195, 210 };
145PIGrCoord y[5] = { 50, 50, 50, 50, 50 };
146
147g->SelForeground(PI_Black);
148g->DrawMarkers(x, y, 5);
149return;
150}
151
152
153static char strbuff[128];
154/* --Methode-- */
155void ExBWdg::But1Press(int x, int y)
156{
157SelPointerShape(PI_CrossPointer);
158sprintf(strbuff,"Pos X= %d Y= %d", x, y);
159mWGrC->SelFont(PI_NormalSizeFont, PI_BoldFont);
160mWGrC->DrawString(x+10, y, strbuff);
161Send(Msg(), PIMsg_Active);
162return;
163}
164
165/* --Methode-- */
166void ExBWdg::But1Release(int x, int y)
167{
168SelPointerShape(PI_ArrowPointer);
169Refresh();
170return;
171}
172
173/* --Methode-- */
174void ExBWdg::But2Press(int x, int y)
175{
176if (++mfnt > 2) mfnt = 0;
177if (++mmrk > 10) mmrk = 0;
178return;
179}
180
181/* --Methode-- */
182void ExBWdg::But2Release(int x, int y)
183{
184Refresh();
185return;
186}
187
188/* --Methode-- */
189void ExBWdg::But3Press(int x, int y)
190{
191SelPointerShape(PI_HandPointer);
192mWGrC->SelForeground(PI_Blue);
193mWGrC->DrawFBox(XSize()/3, YSize()/3, XSize()/8, YSize()/8);
194return;
195}
196
197/* --Methode-- */
198void ExBWdg::But3Release(int x, int y)
199{
200mWGrC->SelForeground(PI_White);
201mWGrC->DrawFBox(XSize()/3, YSize()/3, XSize()/8, YSize()/8);
202mWGrC->SelForeground(PI_Black);
203SelPointerShape(PI_ArrowPointer);
204return;
205}
206
207/* --Methode-- */
208void ExBWdg::Keyboard(int key, PIKeyModifier kmod)
209{
210str[nc] = key; str[nc+1] = '\0';
211if (++nc > 30) nc = 0;
212char buf[16];
213
214if (key == PIK_Return) { Refresh(); nc = 0; return; }
215mWGrC->SelFont(PI_NormalSizeFont, PI_BoldFont);
216sprintf(buf," %c (%3d) ", (char) key, key, (int)kmod);
217mWGrC->DrawOpaqueString(20, 75, buf);
218
219if ( (kmod == PIKM_Alt) && (key == 'c') ) {
220 printf("ExBWdg::Keyboard: Test-Copy ... \n");
221 ClaimSelection();
222}
223if ( (kmod == PIKM_Alt) && (key == 'v') ) {
224 printf("ExBWdg::Keyboard: Test-Paste ... \n");
225 RequestSelection();
226}
227
228return;
229}
230
231// ----- Exemple d'une classe heritant de PIScDrawWdg ----
232/* ........................................................... */
233/* Classe ScSample */
234/* ........................................................... */
235
236class ScSample : public PIScDrawWdg {
237public:
238 ScSample(PIContainerGen *par, char *nom,
239 int sx=10, int sy=10, int px=0, int py=0);
240 virtual ~ScSample();
241};
242
243static double fpol(double x) {return (0.3*x*x*x-x*x+7.);}
244static float xpts[10] = {-2.4, -2 , -1., 1., 1.7, 2.2, 3., 3.75, 4.3, 4.8};
245static float ypts[10] = {-5 , -1.5, 2.5, 5., 8.0, 12., 9., 6.00, 2.2, -3.0};
246
247ScSample::ScSample(PIContainerGen *par, char *nom,
248 int sx, int sy, int px, int py)
249:PIScDrawWdg(par, nom, sx, sy, px, py)
250
251{
252// Definition des limites
253SetLimits(-3,5,-10,15);
254
255// Creation et ajout d'un drawer de fonctions
256PIFuncDrawer* mfd = new PIFuncDrawer(fpol);
257mfd->SetColAtt(PI_Blue);
258mfd->SetLineAtt(PI_NormalLine);
259AddScDrawer(mfd,true);
260
261// Creation et ajout d'un drawer de points
262PIYfXDrawer* mxyd = new PIYfXDrawer(new P1DAdapter<float>(xpts, 10, false),
263 new P1DAdapter<float>(ypts, 10, false), true);
264mxyd->SetColAtt(PI_Magenta);
265mxyd->SetMarkerAtt(7, PI_FBoxMarker);
266AddScDrawer(mxyd, true);
267
268// Ajout de texte
269BaseDrawer()->SetFontAtt(PI_NormalSizeFont, PI_BoldFont);
270BaseDrawer()->ElAddText(-2.8, 12., "---- ScSample ----", PI_Red);
271BaseDrawer()->ElAddText(-2.8, 10., "f(x)=0.3*x^3-x^2+7.", PI_Red);
272
273SetAxesFlags(kAxesDflt);
274}
275
276ScSample::~ScSample()
277{
278}
279
280// ----------- Une classe EventHandler ----------
281class MyEvHandler : public PIEventHandler {
282public :
283 MyEvHandler();
284 ~MyEvHandler();
285 virtual void Draw(PIGraphic* g, int x0, int y0, int dx, int dy);
286 virtual void ProcessEvent();
287 inline bool GrabOK() { return(grabok); }
288
289protected :
290 PIScreenBuffer* mscb;
291 int mposx, mposy;
292 int mposx2, mposy2;
293 int msx, msy;
294 bool grabok;
295};
296
297/* --Methode-- */
298MyEvHandler::MyEvHandler()
299 : PIEventHandler()
300{
301mscb = new PIScreenBuffer(100, 100);
302msx = 100; msy = 100;
303mposx = mposy = 0;
304mposx2 = mposy2 = 0;
305grabok = false;
306}
307
308/* --Methode-- */
309MyEvHandler::~MyEvHandler()
310{
311delete mscb;
312}
313
314/* --Methode-- */
315void MyEvHandler::Draw(PIGraphic* g, int, int, int, int)
316{
317if (GrabOK()) {
318 printf("MyEvHandler::Draw() : Copying from ScreenBuffer to wdg (%lx) \n", (unsigned long)CurrentWdg());
319 mscb->CopyToWdg(CurrentWdg(), 0, 0, msx, msy, CurrentWdg()->XSize()/2, CurrentWdg()->YSize()/2);
320 }
321else printf("MyEvHandler::Draw() : Grab first from source \n");
322}
323
324/* --Methode-- */
325void MyEvHandler::ProcessEvent()
326{
327printf("MyEvHandler::ProcessEvent() \n");
328switch (EventType()) {
329
330 case PIEvent_But3Press :
331 mposx = GetPosX();
332 mposy = GetPosY();
333 break;
334
335 case PIEvent_But3Release :
336 mposx2 = GetPosX();
337 mposy2 = GetPosY();
338 printf("MyEvHandler::ProcessEvent(): Rectangle(%d,%d - %d,%d) \n", mposx, mposy, mposx2, mposy2);
339// ((PIBaseWdg*)CurrentWdg())->WindowGraphic()->DrawBox(mposx, mposy, (mposx2-mposx), (mposy2-mposy));
340 break;
341
342 case PIEvent_Keyboard :
343 if ((GetKeyMod() == PIKM_Alt) && ((GetKey() == 'G') || (GetKey() == 'g')) ) {
344 printf("MyEvHandler::ProcessEvent(): Grabbing(%dx%d) from Wdg(%lx)@(%d,%d) \n",
345 msx, msy, (unsigned long)CurrentWdg(), mposx, mposy);
346 mscb->CopyFromWdg(CurrentWdg(), mposx, mposy, msx, msy, 0, 0);
347 grabok = true;
348 }
349 break;
350
351 default :
352 printf("MyEvHandler::ProcessEvent(): Unexpected Event (Type = %d) received \n", (int)EventType());
353 break;
354 }
355
356return;
357}
358
359/* ........................................................... */
360/* Classe PITApp */
361/* ........................................................... */
362class PITApp : public PIApplication {
363public:
364 PITApp(int narg=0, char* arg[]=NULL);
365 ~PITApp();
366 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
367private :
368 PIMenu* m[2];
369 PIText* txt;
370 MyEvHandler* evh;
371 PIWindow* mcurwin;
372 PIWdg* mdrhw;
373 PIWdg* mevhw;
374 PIWdg* mcurw;
375};
376
377
378/* --Methode-- */
379PITApp::PITApp(int narg, char* arg[])
380: PIApplication(300, 100, narg, arg)
381{
382
383// Menus, les numeros ds AppendItem sont les numeros de messages
384// qui seront passes a Process() et permettent d'identifier l'action
385
386// Creation du premier menu
387
388m[0] = new PIMenu(Menubar(),"Actions");
389m[0]->AppendItem("Info", 10101);
390m[0]->AppendItem("New ExBwdg", 10102);
391m[0]->AppendItem("New ScSample", 10103);
392m[0]->AppendItem("->eps", 10111);
393m[0]->AppendItem("Exit", 10105);
394// On accroche le menu au Menubar
395AppendMenu(m[0]);
396
397m[1] = new PIMenu(Menubar(),"Edit");
398m[1]->AppendItem("Copy", 10201);
399m[1]->AppendItem("Paste", 10202);
400// On accroche le menu au Menubar
401AppendMenu(m[1]);
402
403// Creation d' zone texte multiligne
404txt = new PIText(MainWin(), "mltext", true, true, 290, 90, 5, 5);
405txt->SetBinding(PIBK_elastic,PIBK_elastic,PIBK_elastic,PIBK_elastic);
406// txt->SetMutiLineMode(true);
407txt->SetTextEditable(false);
408txt->SetText("");
409
410evh = new MyEvHandler;
411mcurwin = NULL;
412mcurw = NULL;
413mdrhw = NULL;
414mdrhw = NULL;
415SetReady();
416}
417
418/* --Methode-- */
419PITApp::~PITApp()
420{
421delete m[0]; delete m[1]; delete txt;
422}
423
424static int numpsf = 0;
425static int nbwin = 0;
426/* --Methode-- */
427void PITApp::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
428{
429
430char strg[256];
431
432// Gestion des actions en fonctions des messages
433
434if ( ( ModMsg(msg) == PIMsg_Active) || (ModMsg(msg) == PIMsg_Close) ) {
435 PIWdg *sndw;
436 sndw = (PIWdg *)sender;
437 if (sndw->kind() == PIWindow::ClassId) {
438 if (ModMsg(msg) == PIMsg_Close) {
439// printf("PITApp::Process()/Debug : Closing Window %lx \n", (long)sender);
440 if (mcurwin->UserData() == mcurw) mcurw = NULL;
441 if (mcurwin->UserData() == mdrhw) mdrhw = NULL;
442 if (mcurwin->UserData() == mevhw) mevhw = NULL;
443 delete (PIWindow *)sender;
444 mcurwin = NULL;
445 }
446 else mcurwin = (PIWindow *)sender;
447 }
448 else if ( ModMsg(msg) == PIMsg_Active) mcurw = sndw;
449}
450
451else switch(UserMsg(msg)) {
452
453 case 10101: // Info
454 txt->SetText("pit1: PITApp \n PIBaseWdg - PIScDrawWdg test \n ExBWdg (:PIBaseWdg) Creation \n ScSample (:PIScDrawWdg) creation");
455 break;
456
457 case 10102: // Creation d'un ExBWdg
458 {
459 nbwin++;
460 sprintf(strg, "Creation ExBwdg \n Fenetre No %d \n Test keyborard, button 1/2/3", nbwin);
461 txt->SetText(strg);
462 sprintf(strg,"Window %d - ExBwdg", nbwin);
463 PIWindow* wp = new PIWindow(this, strg, PIWK_normal, 250, 250, 150, 150);
464 wp->SetAutoDelChilds(true);
465 ExBWdg* exb = new ExBWdg(wp, "bwdg", 250, 250, 0, 0);
466 exb->SetBinding(PIBK_fixed,PIBK_elastic,PIBK_elastic,PIBK_elastic);
467 wp->SetUserData(exb);
468 wp->Show();
469 }
470 break;
471
472 case 10103: // Creation d'un ScSample
473 {
474 nbwin++;
475 sprintf(strg, "Creation Scsample \n Fenetre No %d \n Button 1 -> mouse position \n Button 2 -> Zoom \n keyboard <Alt>O <Alt>V", nbwin);
476 txt->SetText(strg);
477 sprintf(strg,"Window %d - ScSample", nbwin);
478 PIWindow* wp = new PIWindow(this, strg, PIWK_normal, 400, 400, 200, 200);
479 wp->SetAutoDelChilds(true);
480 ScSample* sc = new ScSample(wp, "scsample", 400, 400, 0, 0);
481 sc->SetBinding(PIBK_elastic,PIBK_elastic,PIBK_elastic,PIBK_elastic);
482 wp->SetUserData(sc);
483 wp->Show();
484 }
485 break;
486
487 case 10111: // -> eps
488 if (mcurwin) {
489 char buff[64];
490 sprintf(buff, "pit_%d.eps", numpsf); numpsf++;
491 printf("Creating EPS File %s (Encapsulated PostScript) \n", buff);
492 PSFile *mps;
493 mps = new PSFile(buff);
494 mcurwin->PSPrint(mps,0,0);
495 delete mps;
496 }
497 break;
498 case 10105: // Sortie d'application
499 Stop();
500 break;
501
502
503 case 10201: // 2 eme menu Copy
504 if (mcurw == NULL) { printf("PITApp::Process()/Warning : Select a window for copy \n");
505 sprintf(strg, "Warning : Select a window for copy");
506 txt->SetText(strg);
507 }
508 else {
509 printf("Adding EventHandler to Wdg %lx", (long)mcurw);
510 sprintf(strg, "Adding EventHandler to Wdg %lx \n, Use button 3 to select copy zone \n, <Alt>G to Get copy",
511 (long)mcurw);
512 txt->SetText(strg);
513 if (mevhw != NULL) mevhw->RemoveEventHandler(evh); mevhw = NULL;
514 mcurw->AddEventHandler(evh, PIEvent_But3Press | PIEvent_But3Release | PIEvent_Keyboard, false);
515 }
516 break;
517
518 case 10202: // 2eme Menu - Paste
519 if (mcurw == NULL) { printf("PITApp::Process()/Warning : Select a window for paste \n");
520 sprintf(strg, "Warning : Select a window for paste");
521 txt->SetText(strg);
522 }
523 else {
524 printf("Adding DrawHandler to Wdg %lx \n - Refreshing Wdg", (long)mcurw);
525 sprintf(strg, "Adding DrawHandler to Wdg %lx \n - Refreshing Wdg", (long)mcurw);
526 txt->SetText(strg);
527 mcurw->AddDrawHandler(evh, false);
528 mdrhw = mcurw;
529 ((PIBaseWdg*)mcurw)->Refresh();
530 }
531 break;
532
533 default :
534 // printf("PITApp::Process() Msg= %d (%d - %d)??? \n",
535 // (int)msg, (int)UserMsg(msg),(int)ModMsg(msg));
536 break;
537
538 }
539return;
540}
541
542
543// ................................................................
544// programme principal
545// ................................................................
546
547int main(int narg, char *arg[])
548{
549if ( (narg > 1) && (strcmp(arg[1],"-h") == 0) ) {
550 printf("pit1 : ExBWdg (:PIBaseWdg) / ScSample (:PIScDrawWdg) test \n");
551 printf(" Usage : pit1 [Xt options] \n");
552 }
553// Creation de l'objet application et appel de Run()
554PITApp* app = new PITApp(narg, arg);
555// puts("appel de Application.Run() ... ");
556app->Run();
557
558delete app;
559exit(0);
560}
561
562
Note: See TracBrowser for help on using the repository browser.