File: mainPanel.ec

package info (click to toggle)
ecere-sdk 0.44.15-1
  • links: PTS
  • area: main
  • in suites: sid, stretch
  • size: 97,712 kB
  • ctags: 54,695
  • sloc: ansic: 593,042; makefile: 12,250; yacc: 4,955; lex: 707; objc: 259; python: 252; xml: 102
file content (186 lines) | stat: -rw-r--r-- 5,170 bytes parent folder | download | duplicates (2)
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
import "ecere"
import "connection"

define ECOMMUNICATOR_PORT = 3113;

class eComApp;

define app = (eComApp) __thisModule;

class ConnectionSocket;
class Connection;
class eComService;

class MainPanel : Window
{
   eComService service { mainPanel = this };
   OldList connections;

   background = activeBorder, tabCycle = true;
   borderStyle = sizable, hasClose = true, hasMinimize = true, text = app.appName, size = Size { 500, 320 }, minClientSize = Size { 320, 200 };

   Button connect
   {
      this, isDefault = true, text = "Connect", position = Point { 220, 30 }, size = Size { 80, 0 }, hotKey = altC;

      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         ConnectionSocket { mainPanel = this }.Connect(address.contents, service.port);
         return true;
      }
   };
   Button host
   {
      this, text = "Host", position = Point { 220, 60 }, size = Size { 80, 0 }, hotKey = altH;

      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         if(service.Start())
         {
            log.End();
            log.Printf("\nHosting on port %d", service.port);
            host.disabled = true;
            stopHosting.disabled = false;
         }
         return true;
      }
   };
   Button localHost
   {
      this, text = "Local Host", position = Point { 320, 30 }, size = Size { 80, 0 }, hotKey = altL;

      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         char hostName[256], address[64];
         GetHostName(hostName, sizeof(hostName));
         GetAddressFromName(hostName, address);
         log.End();
         log.Printf("\nLocal Host: %s => %s", hostName, address);
         return true;
      }
   };
   Button stopHosting
   {
       this, text = "Stop", position = Point { 320, 60 }, size = Size { 80, 0 }, hotKey = altS;

       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
       {
         service.Stop();
         stopHosting.disabled = true;
         host.disabled = false;
         return true;
       }
   };

   EditBox log
   {
      this, readOnly = true, multiLine = true, autoEmpty = true, hasVertScroll = true, hasHorzScroll = true, inactive = true,
      text = "Communication Log",
      anchor = Anchor { left = 10, top = 100, right = 10, bottom = 30 },
      contents = app.appName
   };
   Label logLabel
   {
      this, position = Point { 10, 80 }, labeledWindow = log
   };
   EditBox nameBox
   {
      this, text = "Name", position = Point { 50, 55 }, size = Size { 160,20 };

      void NotifyUpdate(EditBox editBox)
      {
         const char * string = nameBox.contents;
         OldLink link;
         for(link = connections.first; link; link = link.next)
            ((Connection)link.data).SendName(string);
      }
   };
   Label nameLabel
   {
      this, position = Point { 10, 55 }, labeledWindow = nameBox
   };

   Button clear
   {
      this, text = "Clear", anchor = Anchor { left = 10, bottom = 5 }, size = Size { 80, 0 }, hotKey = altR;

      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         log.Clear();
         return true;
      }
   };
   Button exit
   {
      this, text = "Exit", size = Size { 80, 0 }, anchor = Anchor { right = 10, bottom = 5 }, hotKey = altX;

      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         Destroy(0);
         return true;
      }
   };

   EditBox address
   {
      this, contents = "localhost", hasHorzScroll = true, text = "Internet Address", position = Point { 10, 30 }, size = Size { 200,20 }, hotKey = altA;

      void NotifyUpdate(EditBox editBox)
      {
         connect.disabled = address.contents[0] ? false : true;
      }
   };
   Label addressLabel
   {
      this, position = Point { 10, 10 }, labeledWindow = address
   };

   void ConnectionDestroyed(Connection connection)
   {
      log.End();
      log.Printf("\n%s disconnected", connection.text);
      connections.Delete(connections.FindLink(connection));
   }

   void OnConnect(Connection connection, const char * inetAddress, const char * address)
   {
      log.End();
      log.Printf("\nConnected to %s (%s)", inetAddress, address);
      connection.SendName(nameBox.contents);
      connections.Add(OldLink{data = connection});
   }
}

MainPanel mainPanel {};

class eComApp : GuiApplication
{
   appName = "ECERE Communicator (Build 0002.5)";
}

class eComService : Service
{
   MainPanel mainPanel;

   port = ECOMMUNICATOR_PORT;

   void OnAccept()
   {
      char address[256] = "";
      Connection connection;

      ConnectionSocket socket { this, mainPanel = mainPanel };

      GetNameFromAddress(socket.inetAddress, address);
      mainPanel.log.End();
      mainPanel.log.Printf("\nConnection accepted from %s (%s)", socket.inetAddress, address);

      socket.connection = connection = Connection { text = "Incoming Connection...", socket = socket, master = mainPanel };
      mainPanel.connections.Add(OldLink{data = connection});

      connection.Create();
      connection.SendName(mainPanel.nameBox.contents);
   }

   property MainPanel mainPanel { set { mainPanel = value; } }
}