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

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

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