Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf80e54

Browse files
committed
* stdwinmodule.c: various new commands: setwin{pos,size},
listfontnames, bitmap ops. * listobject.c: use mkvalue() when possible; avoid weird error when calling append() without args. * modsupport.c: new feature in getargs(): if the format string contains a semicolor the string after that is used as the error message instead of "bad argument list (format %s)" when there's an error.
1 parent 3f2ef09 commit bf80e54

3 files changed

Lines changed: 301 additions & 20 deletions

File tree

Modules/stdwinmodule.c

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3030
dp: a drawing structure (only one can exist at a time)
3131
mp: a menu
3232
tp: a textedit block
33+
bp: a bitmap
3334
*/
3435

3536
/* Rules for translating C stdwin function calls into Python stwin:
@@ -116,6 +117,16 @@ extern typeobject Menutype; /* Really static, forward */
116117

117118
#define is_menuobject(mp) ((mp)->ob_type == &Menutype)
118119

120+
typedef struct {
121+
OB_HEAD
122+
BITMAP *b_bitmap;
123+
object *b_attr; /* Attributes dictionary */
124+
} bitmapobject;
125+
126+
extern typeobject Bitmaptype; /* Really static, forward */
127+
128+
#define is_bitmapobject(mp) ((mp)->ob_type == &Bitmaptype)
129+
119130

120131
/* Strongly stdwin-specific argument handlers */
121132

@@ -664,7 +675,46 @@ drawing_setfgcolor(self, args)
664675
return None;
665676
}
666677

678+
static object *
679+
drawing_bitmap(self, args)
680+
object *self;
681+
object *args;
682+
{
683+
int h, v;
684+
object *bp;
685+
object *mask = NULL;
686+
if (!getargs(args, "((ii)O)", &h, &v, &bp)) {
687+
err_clear();
688+
if (!getargs(args, "((ii)OO)", &h, &v, &bp, &mask))
689+
return NULL;
690+
if (mask == None)
691+
mask = NULL;
692+
else if (!is_bitmapobject(mask)) {
693+
err_badarg();
694+
return NULL;
695+
}
696+
}
697+
if (!is_bitmapobject(bp)) {
698+
err_badarg();
699+
return NULL;
700+
}
701+
if (((bitmapobject *)bp)->b_bitmap == NULL ||
702+
mask != NULL && ((bitmapobject *)mask)->b_bitmap == NULL) {
703+
err_setstr(StdwinError, "bitmap object already close");
704+
return NULL;
705+
}
706+
if (mask == NULL)
707+
wdrawbitmap(h, v, ((bitmapobject *)bp)->b_bitmap, ALLBITS);
708+
else
709+
wdrawbitmap(h, v,
710+
((bitmapobject *)bp)->b_bitmap,
711+
((bitmapobject *)bp)->b_bitmap);
712+
INCREF(None);
713+
return None;
714+
}
715+
667716
static struct methodlist drawing_methods[] = {
717+
{"bitmap", drawing_bitmap},
668718
{"box", drawing_box},
669719
{"circle", drawing_circle},
670720
{"cliprect", drawing_cliprect},
@@ -1293,6 +1343,161 @@ typeobject Menutype = {
12931343
};
12941344

12951345

1346+
/* Bitmaps objects */
1347+
1348+
static bitmapobject *newbitmapobject PROTO((int, int));
1349+
static bitmapobject *
1350+
newbitmapobject(width, height)
1351+
int width, height;
1352+
{
1353+
BITMAP *bitmap;
1354+
bitmapobject *bp;
1355+
bitmap = wnewbitmap(width, height);
1356+
if (bitmap == NULL)
1357+
return (bitmapobject *) err_nomem();
1358+
bp = NEWOBJ(bitmapobject, &Bitmaptype);
1359+
if (bp != NULL) {
1360+
bp->b_bitmap = bitmap;
1361+
bp->b_attr = NULL;
1362+
}
1363+
else
1364+
wfreebitmap(bitmap);
1365+
return bp;
1366+
}
1367+
1368+
/* Bitmap methods */
1369+
1370+
static void
1371+
bitmap_dealloc(bp)
1372+
bitmapobject *bp;
1373+
{
1374+
if (bp->b_bitmap != NULL)
1375+
wfreebitmap(bp->b_bitmap);
1376+
XDECREF(bp->b_attr);
1377+
DEL(bp);
1378+
}
1379+
1380+
static object *
1381+
bitmap_close(bp, args)
1382+
bitmapobject *bp;
1383+
object *args;
1384+
{
1385+
if (bp->b_bitmap != NULL)
1386+
wfreebitmap(bp->b_bitmap);
1387+
bp->b_bitmap = NULL;
1388+
XDECREF(bp->b_attr);
1389+
bp->b_attr = NULL;
1390+
INCREF(None);
1391+
return None;
1392+
}
1393+
1394+
static object *
1395+
bitmap_setbit(self, args)
1396+
bitmapobject *self;
1397+
object *args;
1398+
{
1399+
int a[3];
1400+
if (!getpointintarg(args, a))
1401+
return NULL;
1402+
wsetbit(self->b_bitmap, a[0], a[1], a[2]);
1403+
INCREF(None);
1404+
return None;
1405+
}
1406+
1407+
static object *
1408+
bitmap_getbit(self, args)
1409+
bitmapobject *self;
1410+
object *args;
1411+
{
1412+
int a[2];
1413+
if (!getpointarg(args, a))
1414+
return NULL;
1415+
return newintobject((long) wgetbit(self->b_bitmap, a[0], a[1]));
1416+
}
1417+
1418+
static object *
1419+
bitmap_getsize(self, args)
1420+
bitmapobject *self;
1421+
object *args;
1422+
{
1423+
int width, height;
1424+
if (!getnoarg(args))
1425+
return NULL;
1426+
wgetbitmapsize(self->b_bitmap, &width, &height);
1427+
return mkvalue("(ii)", width, height);
1428+
}
1429+
1430+
static struct methodlist bitmap_methods[] = {
1431+
{"close", bitmap_close},
1432+
{"getsize", bitmap_getsize},
1433+
{"getbit", bitmap_getbit},
1434+
{"setbit", bitmap_setbit},
1435+
{NULL, NULL} /* sentinel */
1436+
};
1437+
1438+
static object *
1439+
bitmap_getattr(bp, name)
1440+
bitmapobject *bp;
1441+
char *name;
1442+
{
1443+
object *v = NULL;
1444+
if (bp->b_bitmap == NULL) {
1445+
err_setstr(StdwinError, "bitmap object already closed");
1446+
return NULL;
1447+
}
1448+
if (strcmp(name, "__dict__") == 0) {
1449+
v = bp->b_attr;
1450+
if (v == NULL)
1451+
v = None;
1452+
}
1453+
else if (bp->b_attr != NULL) {
1454+
v = dictlookup(bp->b_attr, name);
1455+
}
1456+
if (v != NULL) {
1457+
INCREF(v);
1458+
return v;
1459+
}
1460+
return findmethod(bitmap_methods, (object *)bp, name);
1461+
}
1462+
1463+
static int
1464+
bitmap_setattr(bp, name, v)
1465+
bitmapobject *bp;
1466+
char *name;
1467+
object *v;
1468+
{
1469+
if (bp->b_attr == NULL) {
1470+
bp->b_attr = newdictobject();
1471+
if (bp->b_attr == NULL)
1472+
return -1;
1473+
}
1474+
if (v == NULL) {
1475+
int rv = dictremove(bp->b_attr, name);
1476+
if (rv < 0)
1477+
err_setstr(AttributeError,
1478+
"delete non-existing bitmap object attribute");
1479+
return rv;
1480+
}
1481+
else
1482+
return dictinsert(bp->b_attr, name, v);
1483+
}
1484+
1485+
typeobject Bitmaptype = {
1486+
OB_HEAD_INIT(&Typetype)
1487+
0, /*ob_size*/
1488+
"bitmap", /*tp_name*/
1489+
sizeof(bitmapobject), /*tp_size*/
1490+
0, /*tp_itemsize*/
1491+
/* methods */
1492+
bitmap_dealloc, /*tp_dealloc*/
1493+
0, /*tp_print*/
1494+
bitmap_getattr, /*tp_getattr*/
1495+
bitmap_setattr, /*tp_setattr*/
1496+
0, /*tp_compare*/
1497+
0, /*tp_repr*/
1498+
};
1499+
1500+
12961501
/* Windows */
12971502

12981503
#define MAXNWIN 50
@@ -1405,6 +1610,32 @@ window_getwinsize(wp, args)
14051610
return makepoint(width, height);
14061611
}
14071612

1613+
static object *
1614+
window_setwinpos(wp, args)
1615+
windowobject *wp;
1616+
object *args;
1617+
{
1618+
int a[2];
1619+
if (!getpointarg(args, a))
1620+
return NULL;
1621+
wsetwinpos(wp->w_win, a[0], a[1]);
1622+
INCREF(None);
1623+
return None;
1624+
}
1625+
1626+
static object *
1627+
window_setwinsize(wp, args)
1628+
windowobject *wp;
1629+
object *args;
1630+
{
1631+
int a[2];
1632+
if (!getpointarg(args, a))
1633+
return NULL;
1634+
wsetwinsize(wp->w_win, a[0], a[1]);
1635+
INCREF(None);
1636+
return None;
1637+
}
1638+
14081639
static object *
14091640
window_getdocsize(wp, args)
14101641
windowobject *wp;
@@ -1617,6 +1848,8 @@ static struct methodlist window_methods[] = {
16171848
{"settimer", window_settimer},
16181849
{"settitle", window_settitle},
16191850
{"setwincursor",window_setwincursor},
1851+
{"setwinpos", window_setwinpos},
1852+
{"setwinsize", window_setwinsize},
16201853
{"show", window_show},
16211854
{"textcreate", window_textcreate},
16221855
#ifdef CWI_HACKS
@@ -2168,6 +2401,46 @@ stdwin_connectionnumber(self, args)
21682401
}
21692402
#endif
21702403

2404+
static object *
2405+
stdwin_listfontnames(self, args)
2406+
object *self;
2407+
object *args;
2408+
{
2409+
char *pattern;
2410+
char **fontnames;
2411+
int count;
2412+
object *list;
2413+
if (!getargs(args, "z", &pattern))
2414+
return NULL;
2415+
fontnames = wlistfontnames(pattern, &count);
2416+
list = newlistobject(count);
2417+
if (list != NULL) {
2418+
int i;
2419+
for (i = 0; i < count; i++) {
2420+
object *v = newstringobject(fontnames[i]);
2421+
if (v == NULL) {
2422+
DECREF(list);
2423+
list = NULL;
2424+
break;
2425+
}
2426+
setlistitem(list, i, v);
2427+
}
2428+
}
2429+
return list;
2430+
}
2431+
2432+
static object *
2433+
stdwin_newbitmap(self, args)
2434+
object *self;
2435+
object *args;
2436+
{
2437+
int width, height;
2438+
bitmapobject *bp;
2439+
if (!getargs(args, "(ii)", &width, &height))
2440+
return NULL;
2441+
return (object *)newbitmapobject(width, height);
2442+
}
2443+
21712444
static struct methodlist stdwin_methods[] = {
21722445
{"askfile", stdwin_askfile},
21732446
{"askstr", stdwin_askstr},
@@ -2187,8 +2460,10 @@ static struct methodlist stdwin_methods[] = {
21872460
{"getscrmm", stdwin_getscrmm},
21882461
{"getscrsize", stdwin_getscrsize},
21892462
{"getselection", stdwin_getselection},
2463+
{"listfontnames", stdwin_listfontnames},
21902464
{"menucreate", stdwin_menucreate},
21912465
{"message", stdwin_message},
2466+
{"newbitmap", stdwin_newbitmap},
21922467
{"open", stdwin_open},
21932468
{"pollevent", stdwin_pollevent},
21942469
{"resetselection", stdwin_resetselection},

Objects/listobject.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -458,21 +458,21 @@ listinsert(self, args)
458458
object *args;
459459
{
460460
int i;
461-
if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) {
462-
err_badarg();
463-
return NULL;
464-
}
465-
if (!getintarg(gettupleitem(args, 0), &i))
461+
object *v;
462+
if (!getargs(args, "(iO)", &i, &v))
466463
return NULL;
467-
return ins(self, i, gettupleitem(args, 1));
464+
return ins(self, i, v);
468465
}
469466

470467
static object *
471468
listappend(self, args)
472469
listobject *self;
473470
object *args;
474471
{
475-
return ins(self, (int) self->ob_size, args);
472+
object *v;
473+
if (!getargs(args, "O", &v))
474+
return NULL;
475+
return ins(self, (int) self->ob_size, v);
476476
}
477477

478478
static object *cmpfunc;
@@ -495,13 +495,9 @@ cmp(v, w)
495495
return cmpobject(* (object **) v, * (object **) w);
496496

497497
/* Call the user-supplied comparison function */
498-
t = newtupleobject(2);
498+
t = mkvalue("OO", v, w);
499499
if (t == NULL)
500500
return 0;
501-
INCREF(* (object **) v);
502-
settupleitem(t, 0, * (object **) v);
503-
INCREF(* (object **) w);
504-
settupleitem(t, 1, * (object **) w);
505501
res = call_object(cmpfunc, t);
506502
DECREF(t);
507503
if (res == NULL)

0 commit comments

Comments
 (0)