summaryrefslogtreecommitdiff
path: root/iup/srclua5/iuplua_pplot.c
blob: 08b04bdf4f141540f814a23ee7f27b4ba3afced9 (plain)
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
/** \file
 * \brief pplot binding for Lua 5.
 *
 * See Copyright Notice in "iup.h"
 */

#include <lua.h>
#include <lauxlib.h>

#include "iup.h"
#include "iup_pplot.h"

#include <cd.h>
#include <cdlua.h>

#include "iuplua.h"
#include "iuplua_pplot.h"
#include "il.h"


static int pplot_edit_cb(Ihandle *self, int p0, int p1, float p2, float p3, float *p4, float *p5)
{
  int ret;
  lua_State *L = iuplua_call_start(self, "edit_cb");
  lua_pushnumber(L, p0);
  lua_pushnumber(L, p1);
  lua_pushnumber(L, p2);
  lua_pushnumber(L, p3);
  ret = iuplua_call_raw(L, 4+2, LUA_MULTRET);  /* 4 args + 2 args(errormsg, handle), variable number of returns */
  if (ret || lua_isnil(L, -1))
    return IUP_DEFAULT;
  ret = (int)lua_tonumber(L,-1);

  if (ret == IUP_IGNORE) 
  {
    lua_pop(L, 1);
    return IUP_IGNORE;
  }

  *p4 = (float)lua_tonumber(L, -3); 
  *p5 = (float)lua_tonumber(L, -2); 
  lua_pop(L, 1);
  return IUP_DEFAULT;
}

static int pplot_postdraw_cb(Ihandle *self, cdCanvas* cnv)
{
  lua_State *L = iuplua_call_start(self, "postdraw_cb");
  cdlua_pushcanvas(L, cnv);
  return iuplua_call(L, 1);
}

static int pplot_predraw_cb(Ihandle *self, cdCanvas* cnv)
{
  lua_State *L = iuplua_call_start(self, "predraw_cb");
  cdlua_pushcanvas(L, cnv);
  return iuplua_call(L, 1);
}

static int PPlotBegin(lua_State *L)
{
  Ihandle *ih = iuplua_checkihandle(L,1);
  IupPPlotBegin(ih, luaL_checkint(L,2));
  return 0;
}

static int PPlotAdd(lua_State *L)
{
  Ihandle *ih = iuplua_checkihandle(L,1);
  IupPPlotAdd(ih, (float)luaL_checknumber(L,2), (float)luaL_checknumber(L,3));
  return 0;
}

static int PPlotAddStr(lua_State *L)
{
  Ihandle *ih = iuplua_checkihandle(L,1);
  IupPPlotAddStr(ih, luaL_checkstring(L,2), (float)luaL_checknumber(L,3));
  return 0;
}

static int PPlotEnd(lua_State *L)
{
  Ihandle *ih = iuplua_checkihandle(L,1);
  int ret = IupPPlotEnd(ih);
  lua_pushnumber(L, ret);
  return 1;
}

static int PPlotInsertStr(lua_State *L)
{
  Ihandle *ih = iuplua_checkihandle(L,1);
  IupPPlotInsertStr(ih, luaL_checkint(L,2), luaL_checkint(L,3), luaL_checkstring(L,4), (float)luaL_checknumber(L,5));
  return 0;
}

static int PPlotInsert(lua_State *L)
{
  Ihandle *ih = iuplua_checkihandle(L,1);
  IupPPlotInsert(ih, luaL_checkint(L,2), luaL_checkint(L,3), (float)luaL_checknumber(L,4), (float)luaL_checknumber(L,5));
  return 0;
}

static int PPlotTransform(lua_State *L)
{
  Ihandle *ih = iuplua_checkihandle(L,1);
  int ix, iy;
  IupPPlotTransform(ih, (float)luaL_checknumber(L,2), (float)luaL_checknumber(L,3), &ix, &iy);
  lua_pushnumber(L, ix);
  lua_pushnumber(L, iy);
  return 2;
}

static int PPlotPaintTo(lua_State *L)
{
  Ihandle *ih = iuplua_checkihandle(L,1);
  IupPPlotPaintTo(ih, cdlua_checkcanvas(L,2));
  return 0;
}

void iuplua_pplotfuncs_open (lua_State *L)
{
  iuplua_register_cb(L, "EDIT_CB", (lua_CFunction)pplot_edit_cb, "pplot");
  iuplua_register_cb(L, "PREDRAW_CB", (lua_CFunction)pplot_predraw_cb, NULL);
  iuplua_register_cb(L, "POSTDRAW_CB", (lua_CFunction)pplot_postdraw_cb, NULL);

  iuplua_register(L, PPlotBegin       ,"PPlotBegin");
  iuplua_register(L, PPlotAdd         ,"PPlotAdd");
  iuplua_register(L, PPlotAddStr      ,"PPlotAddStr");
  iuplua_register(L, PPlotEnd         ,"PPlotEnd");
  iuplua_register(L, PPlotInsertStr   ,"PPlotInsertStr");
  iuplua_register(L, PPlotInsert      ,"PPlotInsert");
  iuplua_register(L, PPlotTransform   ,"PPlotTransform");
  iuplua_register(L, PPlotPaintTo     ,"PPlotPaintTo");
}

int iuppplotlua_open(lua_State * L);

int iup_pplotlua_open(lua_State * L)
{
  if (iuplua_opencall_internal(L))
    IupPPlotOpen();

  iuplua_changeEnv(L);
  iuppplotlua_open(L);
  iuplua_returnEnv(L);
  return 0;
}

/* obligatory to use require"iuplua_pplot" */
int luaopen_iuplua_pplot(lua_State* L)
{
  return iup_pplotlua_open(L);
}

/* obligatory to use require"iuplua_pplot51" */
int luaopen_iuplua_pplot51(lua_State* L)
{
  return iup_pplotlua_open(L);
}