extras; samples: Fixed warnings
[sdk] / samples / threads / threadsAndListBoxes / blank.ec
1 import "ecere"
2
3 class AddThread : Thread
4 {
5    char string[400];
6    Seconds delay;
7
8    uint Main()
9    {
10       Sleep(delay);
11       myApp.Lock();
12       // MessageBox {}.Modal();
13       form1.AddString(string);
14       myApp.Unlock();
15       return 0;
16    }
17
18    void AddItem(const char * string, Seconds delay)
19    {
20       this.delay = delay;
21       strcpy(this.string,string);
22       Create();
23    }
24 }
25
26 class AddPacket : Packet
27 {
28    Seconds delay;
29    char string[1];
30    // Data follows...
31 }
32
33 class MySocket : Socket
34 {
35    void OnReceivePacket(Packet packet)
36    {
37       AddPacket addPacket = (AddPacket)packet;
38       AddThread{}.AddItem(addPacket.string, addPacket.delay);
39    }
40
41    void AddItem(const char * string, Seconds delay)
42    {
43       int len = strlen(string);
44       uint size = sizeof(class AddPacket) + len;
45       AddPacket packet = (AddPacket)new byte[size];
46
47       packet.size = size;
48       packet.delay = delay;
49       CopyBytes(packet.string, string, len+1);
50       SendPacket(packet);
51
52       delete packet;
53    }
54 }
55
56 class MyServer : Service
57 {
58    void OnAccept()
59    {
60       MySocket { this };
61    }
62 }
63
64 class Form1 : Window
65 {
66    caption = "Form1";
67    background = activeBorder;
68    borderStyle = sizable;
69    hasMaximize = true;
70    hasMinimize = true;
71    hasClose = true;
72    tabCycle = true;
73    size = Size { 640, 480 };
74
75    Button deleteBtn
76    {
77       this, caption = "Delete", hotKey = del, size = Size { 51, 21 }, position = Point { 424, 152 }, disabled = true;
78
79       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
80       {
81          listBox1.DeleteRow(listBox1.currentRow);
82          listBox1.Activate();
83          return true;
84       }
85    };
86    Button addBtn
87    {
88       parent = this, caption = "Add", isDefault = true, size = Size { 31, 21 }, position = Point { 432, 120 };
89
90       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
91       {
92          socket.AddItem(editBox1.contents, 2);
93          //editBox1.Clear();
94          return true;
95       }
96    };
97    Button addBtn2
98    {
99       this, caption = "Add2", position = Point { 496, 120 };
100
101       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
102       {
103          socket2.AddItem(editBox1.contents, 0);
104          //editBox1.Clear();
105          return true;
106       }
107    };
108    ListBox listBox1
109    {
110       this, alwaysHighLight = true, caption = "listBox1", hasVertScroll = true, size = Size { 172, 268 }, position = Point { 56, 72 };
111
112       bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
113       {
114          deleteBtn.disabled = row ? false : true;
115          return true;
116       }
117    };
118    EditBox editBox1 { parent = this, caption = "What to add", contents = "Value", hotKey = w, size = Size { 84, 19 }, position = Point { 304, 120 } };
119    Label label1 { labeledWindow = editBox1, parent = this, size = Size { 68, 13 }, position = Point { 304, 96 } };
120    MySocket socket {};
121    MySocket socket2 {};
122
123    void AddString(char * string)
124    {
125       DataRow row = null;
126
127       /*for(row = listBox1.firstRow; row; row = row.next)
128          if(!strcmp(row.string, string))
129             break;*/
130       if(!row)
131       {
132          listBox1.AddString(string);
133       }
134       else
135       {
136          char temp[256];
137          sprintf(temp, "Error: The value \"%s\" is already in the list!", string);
138          MessageBox { caption = "Test App", contents = temp }.Create();
139       }
140    }
141
142    bool OnCreate()
143    {
144       socket.Connect("localhost", 1234);
145       socket2.Connect("localhost", 1234);
146       return true;
147    }
148 }
149
150 Form1 form1 {};
151
152 MyServer server { port = 1234 };
153
154 class MyApplication : GuiApplication
155 {
156    bool Init()
157    {
158       myApp = this;
159       server.Start();
160       return true;
161    }
162 }
163
164 MyApplication myApp;