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

Last change on this file since 2138 was 2138, checked in by ansari, 23 years ago

1/ Classes PILineAtt et PIGrCoord mises dans des fichiers separes

(pigraphgen.h .cc ---> pilineatt.h .cc pigrcoord.h .cc)

2/ Classe PIDraw3DWdg mis ds fichiers separes (pi3dwdg.h .cc)
3/ Mise a jour documentation
4/ Mise a jour programmes test et MAJ Makefile

Reza , 29/7/2002

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