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

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

MAJ/ compil pour V3.90 , Reza 2/1/2003

File size: 15.1 KB
RevLine 
[181]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
[1933]17#include "piyfxdrw.h"
[181]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{
[1563]85PIFontAtt att[4] = { PI_RomanFont, PI_BoldFont, PI_ItalicFont, PI_BoldItalicFont};
[2138]86PIFontSize fsz[4] = { PI_TinySizeFont, PI_SmallSizeFont, PI_NormalSizeFont, PI_LargeSizeFont};
[403]87PIMarker pmk[11] = { PI_DotMarker , PI_PlusMarker, PI_CrossMarker,
[181]88 PI_CircleMarker, PI_FCircleMarker,
89 PI_BoxMarker, PI_FBoxMarker,
[403]90 PI_TriangleMarker, PI_FTriangleMarker,
91 PI_StarMarker, PI_FStarMarker };
[1563]92
93if (g->kind() != PI_PSFileGraphics ) EraseWindow();
94
[181]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
[382]110PIFont f0;
111g->SelFont(f0);
[181]112g->SelForeground(PI_Black);
113g->DrawString(20, 110, str);
[382]114PIFont f(PI_TimesFont);
[1563]115f.SetFontSz(fsz[mfnt]);
[382]116g->SelFont(f);
117g->DrawString(20, 130, "Times-Font");
[1563]118PIFont f2(14,PI_CourierFont, att[mfnt]);
[382]119g->SelFont(f2);
[1563]120g->SelForeground(PI_Red);
121g->DrawString(20, 150, "Courier-Font");
122PIFont f3(14,PI_TimesFont, att[mfnt]);
[382]123g->SelFont(f3);
[1563]124g->SelForeground(PI_Blue);
125g->DrawString(20, 170, "Times-Font");
126PIFont f4(14,PI_HelveticaFont, att[mfnt]);
[382]127g->SelFont(f4);
[1563]128g->SelForeground(PI_Black);
129g->DrawString(20, 190, "Helvetica-Font");
[403]130PIFont f5(14,PI_SymbolFont);
[382]131g->SelFont(f5);
132g->DrawString(20, 210, "Symbol-Font");
[402]133g->SelFont(f0);
[181]134
[382]135
[181]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
[382]148
149
150
[403]151PIGrCoord x[5] = { 150, 165, 180, 195, 210 };
152PIGrCoord y[5] = { 50, 50, 50, 50, 50 };
[181]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{
[1563]183if (++mfnt > 3) mfnt = 0;
[403]184if (++mmrk > 10) mmrk = 0;
[181]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);
[1933]264mfd->GetGraphicAtt().SetColAtt(PI_Blue);
265mfd->GetGraphicAtt().SetLineAtt(PI_NormalLine);
[181]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);
[1933]271mxyd->GetGraphicAtt().SetColAtt(PI_Magenta);
272mxyd->GetGraphicAtt().SetMarkerAtt(7, PI_FBoxMarker);
[181]273AddScDrawer(mxyd, true);
274
275// Ajout de texte
[1933]276BaseDrawer()->GetGraphicAtt().SetFontAtt(PI_NormalSizeFont, PI_BoldFont);
[2302]277PIGraphicAtt gatt;
278gatt.SetColAtt(PI_Red);
279BaseDrawer()->ElDrwMgr().ElAddText(-2.8, 12., "---- ScSample ----", gatt, 0);
280BaseDrawer()->ElDrwMgr().ElAddText(-2.8, 10., "f(x)=0.3*x^3-x^2+7.", gatt, 0);
[181]281
282SetAxesFlags(kAxesDflt);
283}
284
285ScSample::~ScSample()
286{
287}
288
289// ----------- Une classe EventHandler ----------
290class MyEvHandler : public PIEventHandler {
291public :
292 MyEvHandler();
293 ~MyEvHandler();
294 virtual void Draw(PIGraphic* g, int x0, int y0, int dx, int dy);
295 virtual void ProcessEvent();
296 inline bool GrabOK() { return(grabok); }
297
298protected :
299 PIScreenBuffer* mscb;
300 int mposx, mposy;
301 int mposx2, mposy2;
302 int msx, msy;
303 bool grabok;
304};
305
306/* --Methode-- */
307MyEvHandler::MyEvHandler()
308 : PIEventHandler()
309{
310mscb = new PIScreenBuffer(100, 100);
311msx = 100; msy = 100;
312mposx = mposy = 0;
313mposx2 = mposy2 = 0;
314grabok = false;
315}
316
317/* --Methode-- */
318MyEvHandler::~MyEvHandler()
319{
320delete mscb;
321}
322
323/* --Methode-- */
324void MyEvHandler::Draw(PIGraphic* g, int, int, int, int)
325{
326if (GrabOK()) {
327 printf("MyEvHandler::Draw() : Copying from ScreenBuffer to wdg (%lx) \n", (unsigned long)CurrentWdg());
328 mscb->CopyToWdg(CurrentWdg(), 0, 0, msx, msy, CurrentWdg()->XSize()/2, CurrentWdg()->YSize()/2);
329 }
330else printf("MyEvHandler::Draw() : Grab first from source \n");
331}
332
333/* --Methode-- */
334void MyEvHandler::ProcessEvent()
335{
336printf("MyEvHandler::ProcessEvent() \n");
337switch (EventType()) {
338
339 case PIEvent_But3Press :
340 mposx = GetPosX();
341 mposy = GetPosY();
342 break;
343
344 case PIEvent_But3Release :
345 mposx2 = GetPosX();
346 mposy2 = GetPosY();
347 printf("MyEvHandler::ProcessEvent(): Rectangle(%d,%d - %d,%d) \n", mposx, mposy, mposx2, mposy2);
348// ((PIBaseWdg*)CurrentWdg())->WindowGraphic()->DrawBox(mposx, mposy, (mposx2-mposx), (mposy2-mposy));
349 break;
350
351 case PIEvent_Keyboard :
352 if ((GetKeyMod() == PIKM_Alt) && ((GetKey() == 'G') || (GetKey() == 'g')) ) {
353 printf("MyEvHandler::ProcessEvent(): Grabbing(%dx%d) from Wdg(%lx)@(%d,%d) \n",
354 msx, msy, (unsigned long)CurrentWdg(), mposx, mposy);
355 mscb->CopyFromWdg(CurrentWdg(), mposx, mposy, msx, msy, 0, 0);
356 grabok = true;
357 }
358 break;
359
360 default :
361 printf("MyEvHandler::ProcessEvent(): Unexpected Event (Type = %d) received \n", (int)EventType());
362 break;
363 }
364
365return;
366}
367
368/* ........................................................... */
369/* Classe PITApp */
370/* ........................................................... */
371class PITApp : public PIApplication {
372public:
373 PITApp(int narg=0, char* arg[]=NULL);
374 ~PITApp();
375 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
376private :
377 PIMenu* m[2];
378 PIText* txt;
379 MyEvHandler* evh;
380 PIWindow* mcurwin;
381 PIWdg* mdrhw;
382 PIWdg* mevhw;
383 PIWdg* mcurw;
384};
385
386
387/* --Methode-- */
388PITApp::PITApp(int narg, char* arg[])
389: PIApplication(300, 100, narg, arg)
390{
391
392// Menus, les numeros ds AppendItem sont les numeros de messages
393// qui seront passes a Process() et permettent d'identifier l'action
394
395// Creation du premier menu
396
397m[0] = new PIMenu(Menubar(),"Actions");
398m[0]->AppendItem("Info", 10101);
399m[0]->AppendItem("New ExBwdg", 10102);
400m[0]->AppendItem("New ScSample", 10103);
[382]401m[0]->AppendItem("->eps", 10111);
[181]402m[0]->AppendItem("Exit", 10105);
403// On accroche le menu au Menubar
404AppendMenu(m[0]);
405
406m[1] = new PIMenu(Menubar(),"Edit");
407m[1]->AppendItem("Copy", 10201);
408m[1]->AppendItem("Paste", 10202);
409// On accroche le menu au Menubar
410AppendMenu(m[1]);
411
412// Creation d' zone texte multiligne
[323]413txt = new PIText(MainWin(), "mltext", true, true, 290, 90, 5, 5);
[181]414txt->SetBinding(PIBK_elastic,PIBK_elastic,PIBK_elastic,PIBK_elastic);
[323]415// txt->SetMutiLineMode(true);
[181]416txt->SetTextEditable(false);
417txt->SetText("");
418
419evh = new MyEvHandler;
420mcurwin = NULL;
421mcurw = NULL;
422mdrhw = NULL;
423mdrhw = NULL;
424SetReady();
425}
426
427/* --Methode-- */
428PITApp::~PITApp()
429{
430delete m[0]; delete m[1]; delete txt;
431}
432
[382]433static int numpsf = 0;
[181]434static int nbwin = 0;
435/* --Methode-- */
436void PITApp::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
437{
438
439char strg[256];
440
441// Gestion des actions en fonctions des messages
442
443if ( ( ModMsg(msg) == PIMsg_Active) || (ModMsg(msg) == PIMsg_Close) ) {
444 PIWdg *sndw;
445 sndw = (PIWdg *)sender;
446 if (sndw->kind() == PIWindow::ClassId) {
447 if (ModMsg(msg) == PIMsg_Close) {
448// printf("PITApp::Process()/Debug : Closing Window %lx \n", (long)sender);
449 if (mcurwin->UserData() == mcurw) mcurw = NULL;
450 if (mcurwin->UserData() == mdrhw) mdrhw = NULL;
451 if (mcurwin->UserData() == mevhw) mevhw = NULL;
452 delete (PIWindow *)sender;
453 mcurwin = NULL;
454 }
455 else mcurwin = (PIWindow *)sender;
456 }
457 else if ( ModMsg(msg) == PIMsg_Active) mcurw = sndw;
458}
459
460else switch(UserMsg(msg)) {
461
462 case 10101: // Info
463 txt->SetText("pit1: PITApp \n PIBaseWdg - PIScDrawWdg test \n ExBWdg (:PIBaseWdg) Creation \n ScSample (:PIScDrawWdg) creation");
464 break;
465
466 case 10102: // Creation d'un ExBWdg
467 {
468 nbwin++;
469 sprintf(strg, "Creation ExBwdg \n Fenetre No %d \n Test keyborard, button 1/2/3", nbwin);
470 txt->SetText(strg);
471 sprintf(strg,"Window %d - ExBwdg", nbwin);
472 PIWindow* wp = new PIWindow(this, strg, PIWK_normal, 250, 250, 150, 150);
473 wp->SetAutoDelChilds(true);
474 ExBWdg* exb = new ExBWdg(wp, "bwdg", 250, 250, 0, 0);
475 exb->SetBinding(PIBK_fixed,PIBK_elastic,PIBK_elastic,PIBK_elastic);
476 wp->SetUserData(exb);
477 wp->Show();
478 }
479 break;
480
481 case 10103: // Creation d'un ScSample
482 {
483 nbwin++;
484 sprintf(strg, "Creation Scsample \n Fenetre No %d \n Button 1 -> mouse position \n Button 2 -> Zoom \n keyboard <Alt>O <Alt>V", nbwin);
485 txt->SetText(strg);
486 sprintf(strg,"Window %d - ScSample", nbwin);
487 PIWindow* wp = new PIWindow(this, strg, PIWK_normal, 400, 400, 200, 200);
488 wp->SetAutoDelChilds(true);
489 ScSample* sc = new ScSample(wp, "scsample", 400, 400, 0, 0);
490 sc->SetBinding(PIBK_elastic,PIBK_elastic,PIBK_elastic,PIBK_elastic);
491 wp->SetUserData(sc);
492 wp->Show();
493 }
494 break;
495
[382]496 case 10111: // -> eps
497 if (mcurwin) {
498 char buff[64];
499 sprintf(buff, "pit_%d.eps", numpsf); numpsf++;
500 printf("Creating EPS File %s (Encapsulated PostScript) \n", buff);
501 PSFile *mps;
502 mps = new PSFile(buff);
503 mcurwin->PSPrint(mps,0,0);
504 delete mps;
505 }
506 break;
[181]507 case 10105: // Sortie d'application
508 Stop();
509 break;
510
511
512 case 10201: // 2 eme menu Copy
513 if (mcurw == NULL) { printf("PITApp::Process()/Warning : Select a window for copy \n");
514 sprintf(strg, "Warning : Select a window for copy");
515 txt->SetText(strg);
516 }
517 else {
518 printf("Adding EventHandler to Wdg %lx", (long)mcurw);
519 sprintf(strg, "Adding EventHandler to Wdg %lx \n, Use button 3 to select copy zone \n, <Alt>G to Get copy",
520 (long)mcurw);
521 txt->SetText(strg);
522 if (mevhw != NULL) mevhw->RemoveEventHandler(evh); mevhw = NULL;
523 mcurw->AddEventHandler(evh, PIEvent_But3Press | PIEvent_But3Release | PIEvent_Keyboard, false);
524 }
525 break;
526
527 case 10202: // 2eme Menu - Paste
528 if (mcurw == NULL) { printf("PITApp::Process()/Warning : Select a window for paste \n");
529 sprintf(strg, "Warning : Select a window for paste");
530 txt->SetText(strg);
531 }
532 else {
533 printf("Adding DrawHandler to Wdg %lx \n - Refreshing Wdg", (long)mcurw);
534 sprintf(strg, "Adding DrawHandler to Wdg %lx \n - Refreshing Wdg", (long)mcurw);
535 txt->SetText(strg);
536 mcurw->AddDrawHandler(evh, false);
537 mdrhw = mcurw;
538 ((PIBaseWdg*)mcurw)->Refresh();
539 }
540 break;
541
542 default :
543 // printf("PITApp::Process() Msg= %d (%d - %d)??? \n",
544 // (int)msg, (int)UserMsg(msg),(int)ModMsg(msg));
545 break;
546
547 }
548return;
549}
550
551
552// ................................................................
553// programme principal
554// ................................................................
555
556int main(int narg, char *arg[])
557{
558if ( (narg > 1) && (strcmp(arg[1],"-h") == 0) ) {
559 printf("pit1 : ExBWdg (:PIBaseWdg) / ScSample (:PIScDrawWdg) test \n");
560 printf(" Usage : pit1 [Xt options] \n");
561 }
562// Creation de l'objet application et appel de Run()
563PITApp* app = new PITApp(narg, arg);
564// puts("appel de Application.Run() ... ");
565app->Run();
566
567delete app;
568exit(0);
569}
570
571
Note: See TracBrowser for help on using the repository browser.