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

Last change on this file since 194 was 181, checked in by ercodmgr, 27 years ago

Creation du module de programme test de PI - Reza 24/12/98

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