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

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

Modifs PSFile,PSGraphic pour compatibilite PIFont (Nicolas - Reza 12/8/99

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