bindings: Fixed support for C++ instantiating objects from eC
[sdk] / bindings / cpp / samples / form.cpp
1 #define MODULE_NAME  "HelloForm"
2
3 // #define __CONSOLE_APP__
4
5 #include "ecere.hpp"
6
7 GuiApplication app;
8
9 MAIN_DEFINITION;
10
11 class Foo : public Instance
12 {
13 public:
14    int a, b;
15    Foo(int a, int b) : Foo() { this->a = a, this->b = b; };
16
17    CONSTRUCT(Foo, Instance)
18    {
19       onCopy = [](Foo & self, Foo & other)
20       {
21          printf("%d, %d\n", other.a, other.b);
22          printf("self: %p\n", &self);
23          self.a = other.a;
24          self.b = other.b;
25       };
26
27       onCompare = [](Foo & self, Foo & other)
28       {
29          if(self.a > other.a) return  1;
30          if(self.a < other.a) return -1;
31          if(self.b > other.b) return  1;
32          if(self.b < other.b) return -1;
33          return 0;
34       };
35
36       /*onDisplay = [](Foo & self, Surface & s, int x , int y , int w, void * fd, Alignment a, DataDisplayFlags f)
37       {
38          printf("Meh\n");
39       };
40       */
41    }
42
43    REGISTER()
44    {
45       //Instance::class_registration(_class);
46       register_onDisplay(_class, [](Foo & _self, Surface & s, int x , int y , int w, void * fd, Alignment a, DataDisplayFlags f) { printf("Meh\n"); });
47    }
48 };
49 REGISTER_CLASS_DEF(Foo, Instance, app);
50
51 void testStuff();
52
53 class HelloForm2 : public Window
54 {
55 public:
56    Button button;
57
58    CONSTRUCT(HelloForm2, Window)
59    {
60       caption = $("My Second Ecere/C++ Bindings App");
61       borderStyle = sizable;
62       clientSize = { 640, 480 };
63       hasClose = true;
64       hasMaximize = true;
65       hasMinimize = true;
66       background = formColor;
67       font = { "Arial", 30 };
68
69       button.parent = this;
70       button.position = { 200, 200 };
71       button.caption = $("Yay!!");
72       // button.onRedraw = [](Window & w, Surface s){ };
73       button.notifyClicked = [](Window & owner, Button & btn, int x, int y, Modifiers mods)
74       {
75          double i = 3.14159265;
76          char tmp[256];
77          constString s = _onGetString(class_double, &i, tmp, null, null);
78          //PrintLn(class_String, "Hello! -- ", class_String, s, null); // Need to terminate with a null!
79          //HelloForm2 & self = (HelloForm2 &)owner;
80          //MessageBox($("C++ Bindings!"), self.button.caption).modal();
81
82          /*FontResource a("Arial", 20, true);
83          FontResource b("Comic Sans MS", 20, true);
84          FontResource c("Arial", 20, true);
85
86          int ab = ((FontResource *)null)->onCompare(b);
87          //bool ab = ((FontResource *)null)->onCompare(*(FontResource *)null);
88          //bool ac = a.onCompare(c);
89          int ac = a.onCompare(*(FontResource *)null);
90          printf("a compare b = %d\n", ab);
91          printf("a compare c = %d\n", ac);*/
92
93          {
94             Foo obj1 { 2, 3 };
95             Foo obj2 { 2, 4 };
96             Foo obj3 { 2, 3 };
97             /*Foo obj4; obj4.onCopy(obj1);
98             FontResource fr;
99             fr.onCopy(a);
100             printf("result: face = %s, size = %f, bold = %d\n",
101                (constString)fr.faceName, (float)fr.size, (int)fr.bold);
102             printf("after");
103             */
104             Surface s { };
105             Foo * o = (Foo *)null; o->onDisplay(s, 0,0,0, null, 0, 0);
106             //((Foo *)&obj3)->onDisplay(Surface { }, 0,0,0, null, 0, 0);
107
108             int r;
109             r = obj1.onCompare(obj2); printf("result: %d\n", r);
110             //r = obj2.onCompare(obj1); printf("result: %d\n", r);
111             //r = obj1.onCompare(obj3); printf("result: %d\n", r);
112             //printf("result: a = %d, b = %d\n", obj4.a, obj4.b);
113
114             testStuff();
115          }
116          return true;
117       };
118
119       /*onCreate = [](Window & w)
120       {
121          MessageBox($("Cool"), $("Creation!")).modal();
122          return true;
123       };*/
124
125       //onRedraw(Surface());
126       //onCreate();
127    }
128
129    /*static void myOnRedraw(Window & w, Surface & surface)
130    {
131       surface.writeTextf(100, 100, $("Testing stuff!"));
132       //surface.writeTextf(100, 100, "%d + %d = %d", 2, 3, 2+3);
133    };*/
134
135    REGISTER()
136    {
137       Window::class_registration(_class);
138       register_onRedraw(_class, [](Window & w, Surface & surface)
139       {
140          surface.writeTextf(100, 100, $("Class Method!"));
141          //surface.writeTextf(100, 100, "%d + %d = %d", 2, 3, 2+3);
142       });
143    }
144 };
145 REGISTER_CLASS_DEF(HelloForm2, Window, app);
146
147 void testStuff()
148 {
149    //eC_Class * c = eC_findClass(app.impl, "HelloForm2");
150    eC_Class * c = HelloForm2::_class.impl;
151    ((HelloForm2 *)_INSTANCE(newi(c), c))->modal();
152 }
153
154 class HelloForm3 : public HelloForm2
155 {
156 public:
157    CONSTRUCT(HelloForm3, HelloForm2)
158    {
159       background = 0x50B0F0;
160       position = { 0, 0 };
161       font = { "Monaco", 30, true };
162
163       onRedraw = [](Window & w, Surface & surface)
164       {
165          surface.writeTextf(100, 100, $("Instance Method!"));
166          //surface.writeTextf(100, 100, "%d + %d = %d", 2, 3, 2+3);
167       };
168    }
169 };
170 REGISTER_CLASS_DEF(HelloForm3, HelloForm2, app);
171
172 //HelloForm2 hello2;
173 HelloForm3 hello3;