Informations sur: CFenetre

Publié par Freem le 13/06/2007

Description

Classe qui permet de gérer une fenêtre en mode texte (pour l'instant, quand j'aurai le temps, je la ferai evoluer vers la gestion de graphisme, avec SDL)

Code source (langage cpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//### HEADER ###
 
#ifndef CFENETRE_H
#define CFENETRE_H
 
#include "tasklib.h"
#include "ccomserie.h"
#include <string.h>
#include "chaine.h"
#include "campex.h"
 
class CFenetre
	: public CChaine<CFenetre>
{
	//attributs
	public:
		CAmpex *io;
	protected:
		char *texte, *attTexte, *cadre, *attCadre;
		unsigned char pX, pY; //position (valeurs absolues)
	private:
		unsigned char tX, tY; //dimensions (valeurs absolues)
		char *zoneCouverte, *zoneAffichee; // dessins de la fenêtre avant et après affichage
		bool affiche;
		CFenetre *mere;
		CFenetre *filles;
		bool (*evFunc)(char,CFenetre);
 
	//méthodes
	public:
		CFenetre *boutons(void);
		void initCanal(CAmpex *io);
		bool setText(char * txt, char *attr=0); //détermine un texte ou le supprime
		bool setCadre(char * Cadre, char *attr=0); //détermine un cadre ou le supprime
		void getText(char *Texte, char *attr=0); // retourne le texte, vaut 0 si echec (texte trop petite, par ex)
		void getCadre(char *Cadre, char *attr=0); // retourne le cadre, vaut 0 si echec (cadre trop petite, par ex)
		void deplacer(short x, short y=0); // déplacement relatif à la fenêtre mère
		void positioner(short x=-1, short y=-1); // position relative à la fenêtre mère
		void positioner(unsigned char x=0xFF, unsigned char y=0xFF); // position absolue
		void dimensionner(unsigned char x=0xFF, unsigned char y=0xFF);
		bool afficher(void);
		void effacer(void);
		virtual char gesEvent(void);
		CFenetre(CFenetre *mere=0);
		void gesEvent(bool (*callback)(char,CFenetre)=0);
		virtual ~CFenetre();
	protected:
	private:
};
 
#endif // CFENETRE_H
 
//### SOURCE ###
 
#include "fenetre.h"
 
CFenetre::CFenetre(CFenetre *mere)
	:CChaine<CFenetre>()
{
	//ctor
	this->mere=mere;
	filles=0;
	affiche=0;
	texte=0;
	attTexte=0;
	cadre=0;
	attCadre=0;
}
 
CFenetre::~CFenetre()
{
	//dtor
	if(affiche)
	{
		io->deplace(pX, pY);
		for(unsigned char y=pY;y<pY+tY;y++)
			for(unsigned char x=pX;x<pX+tX;x++)
			{
				io->attribut(zoneCouverte[((x-pX)+(y-pY)*tX)<<1]); //envoie les attributs
				io->ecrire(zoneCouverte[(((x-pX)+(y-pY)*tX)<<1)+1]); //envoie les caractères
			}
	}
	if(zoneCouverte)
		delete zoneCouverte;
	if(zoneAffichee)
		delete zoneAffichee;
	if(cadre)
		delete cadre;
	if(cadre)
		delete attCadre;
	if(texte)
		delete texte;
	if(cadre)
		delete attTexte;
	delete filles;
}
 
bool CFenetre::afficher(void)
{
	unsigned char x,y;
 
	if(!(zoneCouverte && zoneAffichee) && affiche)// si une memoire n'est pas reservee ou que la fenetre est affichée
		return false;
 
	//enregistre la zone qui va être écrasée
	io->deplace(pX, pY);
	for(y=pY;y<pY+tY;y++)
		for(x=pX;x<pX+tX;x++)
		{
			io->litAtt(zoneCouverte[((x-pX)+(y-pY)*tX)<<1]); //lit l'attribut
			io->lire(zoneCouverte[(((x-pX)+(y-pY)*tX)<<1)+1]); //lit le caractère
		}
 
	//dessine la fenêtre à partir des éléments possédés
	for(y=0;y<tY;y++)
		for(x=0;x<tX;x++)
		{
			if(cadre && !((!y && y==tY)||(!x && x==tX))) //si un cadre à été définit et que l'on est pas dans la zone de texte
			{
				zoneAffichee[(x+y*tX)<<1]=cadre[x+y*tX];
				if(attCadre)
					zoneAffichee[((x+y*tX)<<1)+1]=attCadre[x+y*tX];
				else
					zoneAffichee[((x+y*tX)<<1)+1]=VID_ATTR_NORMAL;
			}
			else if(texte)
			{
				zoneAffichee[(x+y*tX)<<1]=texte[x+y*tX];
				if(attTexte)
					zoneAffichee[((x+y*tX)<<1)+1]=attTexte[x+y*tX];
				else
					zoneAffichee[((x+y*tX)<<1)+1]=VID_ATTR_NORMAL;
			}
		}
 
	//affiche la fenêtre
	io->deplace(pX, pY);
	for(y=pY;y<pY+tY;y++)
		for(x=pX;x<pX+tX;x++)
		{
			io->ecrAtt(zoneAffichee[((x-pX)+(y-pY)*tX)<<1]); //envoie l'attribut
			io->ecrire(zoneAffichee[(((x-pX)+(y-pY)*tX)<<1)+1]); //envoie le caractère
		}
 
	affiche=true;
	return true;
}
 
void CFenetre::effacer(void)
{
	int y,x=0;
	if(!affiche)// si la fenetre n'est pas affichée
		return;
 
	io->deplace(pX, pY);
	for(y=pY;y<pY+tY;y++)
	{
		//lireChaine
		//io->ecrireChaine
		for(x=pX;x<pX+tX;x++)
		{
			io->ecrAtt(zoneCouverte[((x-pX)+(y-pY)*tX)<<1]); //envoie l'attribut
			io->ecrire(zoneCouverte[(((x-pX)+(y-pY)*tX)<<1)+1]); //envoie le caractère
		}
	}
	affiche=false;
}
 
bool CFenetre::setText(char * txt, char *attr) //détermine un texte ou le supprime
{
	// texte
	if(texte)
		delete texte;
	texte=0;
	texte=new char[strlen(txt)];
	if(!texte)
		return false;
	strcpy(texte,txt);
 
	//attributs du texte
	if(attTexte)
		delete attTexte;
	attTexte=0;
	if(attr)
	{
		attTexte=new char[strlen(attr)];
		if(!attTexte)
			return false;
		strcpy(attTexte,attr);
	}
 
	return true;
}
 
bool CFenetre::setCadre(char * Cadre, char *attr) //détermine un cadre ou le supprime
{
	// cadre
	if(cadre)
		delete cadre;
	cadre=0;
	cadre=new char[strlen(Cadre)];
	if(!cadre)
		return false;
	strcpy(cadre,Cadre);
 
	//attributs du cadre
	if(attCadre)
		delete attCadre;
	attCadre=0;
	if(attr)
	{
		attCadre=new char[strlen(attr)];
		if(!attCadre)
			return false;
		strcpy(attCadre,attr);
	}
 
	return true;
}
 
void CFenetre::getText(char *Texte, char *attr) // retourne le texte, vaut 0 si echec (texte trop petite, par ex)
{
	if(Texte)
		delete Texte;
	Texte=new char[strlen(texte)];
	strcpy(Texte,texte);
	if(attTexte && attr)
	{
		delete attr;
		attr=new char[strlen(attTexte)];
		strcpy(attr, attTexte);
	}
}
 
void CFenetre::getCadre(char *Cadre, char *attr) // retourne le cadre, vaut 0 si echec (cadre trop petite, par ex)
{
	if(Cadre)
		delete Cadre;
	Cadre=new char[strlen(cadre)];
	strcpy(Cadre,cadre);
	if(attCadre && attr)
	{
		delete attr;
		attr=new char[strlen(attCadre)];
		strcpy(attr, attCadre);
	}
}
 
void CFenetre::deplacer(short x, short y) // déplacement relatif à la fenêtre mère
{
	bool ancienAff=affiche;
	effacer();
 
	if(x)
		pX+=x;
	if(y)
		pY+=y;
 
	if(ancienAff)
		afficher();
}
 
void CFenetre::positioner(short x, short y) // position relative à la fenêtre mère
{
	bool ancienAff=affiche;
	effacer();
 
	if(mere)
	{
		if(x!=-1)
			pX=mere->pX+x;
		if(y!=-1)
			pY=mere->pY+y;
	}
	if(ancienAff)
		afficher();
}
 
void CFenetre::positioner(unsigned char x, unsigned char y) // position absolue
{
	bool ancienAff=affiche;
	effacer();
 
	if(x!=0xFF)
		pX=x;
	if(y!=0xFF)
		pY=y;
 
	if(ancienAff)
		afficher();
}
 
void CFenetre::dimensionner(unsigned char x, unsigned char y)
{
	bool ancienAff=affiche;
	effacer();
 
	if(x!=0xFF)
		tX=x;
	if(y!=0xFF)
		tY=y;
	if(zoneCouverte)
		delete zoneCouverte;
	if(zoneAffichee)
		delete zoneAffichee;
	zoneCouverte=new char[(tX*tY)<<1];
	zoneAffichee=new char[(tX*tY)<<1];
 
	if(ancienAff)
		afficher();
}
 
inline CFenetre *CFenetre::boutons(void)
{
	return filles;
}
 
inline void CFenetre::initCanal(CAmpex *io)
{
	this->io=io;
}
 
inline void CFenetre::gesEvent(bool (*callback)(char,CFenetre))
{
	char car;
	if(callback)
		evFunc=callback;
 
	do
	{
		while(!io->lireCar(&car))
			taskDelay(1);
	}
	while(evFunc(car,this));
}
v6 © Computaid SPRL 2005-2008 - Tous droits réservés - Hébergé par eTigris - Page générée en 0,047 s - Crédits - Stats
1 connecté