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

Last change on this file since 1563 was 1563, checked in by ansari, 24 years ago

amelioration test dessin texte / fontes - Reza 4/7/2001

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