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

Last change on this file since 2615 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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