wxruby 1.9.6-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
Files changed (239) hide show
  1. data/LICENSE +53 -0
  2. data/README +297 -0
  3. data/lib/wx/accessors.rb +52 -0
  4. data/lib/wx/classes/acceleratortable.rb +28 -0
  5. data/lib/wx/classes/animation.rb +18 -0
  6. data/lib/wx/classes/app.rb +45 -0
  7. data/lib/wx/classes/artprovider.rb +31 -0
  8. data/lib/wx/classes/bitmap.rb +28 -0
  9. data/lib/wx/classes/busycursor.rb +12 -0
  10. data/lib/wx/classes/checklistbox.rb +45 -0
  11. data/lib/wx/classes/choice.rb +4 -0
  12. data/lib/wx/classes/clientdc.rb +13 -0
  13. data/lib/wx/classes/clipboard.rb +16 -0
  14. data/lib/wx/classes/colour.rb +47 -0
  15. data/lib/wx/classes/combobox.rb +4 -0
  16. data/lib/wx/classes/commandevent.rb +7 -0
  17. data/lib/wx/classes/controlwithitems.rb +10 -0
  18. data/lib/wx/classes/dc.rb +57 -0
  19. data/lib/wx/classes/event.rb +5 -0
  20. data/lib/wx/classes/evthandler.rb +921 -0
  21. data/lib/wx/classes/font.rb +118 -0
  22. data/lib/wx/classes/functions.rb +44 -0
  23. data/lib/wx/classes/gauge.rb +12 -0
  24. data/lib/wx/classes/grid.rb +138 -0
  25. data/lib/wx/classes/helpcontroller.rb +5 -0
  26. data/lib/wx/classes/htmlhelpcontroller.rb +5 -0
  27. data/lib/wx/classes/htmlwindow.rb +14 -0
  28. data/lib/wx/classes/icon.rb +21 -0
  29. data/lib/wx/classes/iconbundle.rb +3 -0
  30. data/lib/wx/classes/image.rb +31 -0
  31. data/lib/wx/classes/imagelist.rb +3 -0
  32. data/lib/wx/classes/listbox.rb +4 -0
  33. data/lib/wx/classes/listctrl.rb +21 -0
  34. data/lib/wx/classes/locale.rb +28 -0
  35. data/lib/wx/classes/mediactrl.rb +22 -0
  36. data/lib/wx/classes/menu.rb +62 -0
  37. data/lib/wx/classes/menuitem.rb +7 -0
  38. data/lib/wx/classes/object.rb +7 -0
  39. data/lib/wx/classes/paintdc.rb +12 -0
  40. data/lib/wx/classes/point.rb +48 -0
  41. data/lib/wx/classes/previewframe.rb +13 -0
  42. data/lib/wx/classes/rect.rb +10 -0
  43. data/lib/wx/classes/size.rb +49 -0
  44. data/lib/wx/classes/sound.rb +23 -0
  45. data/lib/wx/classes/styledtextctrl.rb +92 -0
  46. data/lib/wx/classes/textctrl.rb +14 -0
  47. data/lib/wx/classes/texturlevent.rb +6 -0
  48. data/lib/wx/classes/timer.rb +94 -0
  49. data/lib/wx/classes/toolbartool.rb +4 -0
  50. data/lib/wx/classes/treectrl.rb +44 -0
  51. data/lib/wx/classes/window.rb +82 -0
  52. data/lib/wx/classes/xmlresource.rb +37 -0
  53. data/lib/wx/keyword_ctors.rb +222 -0
  54. data/lib/wx/keyword_defs.rb +492 -0
  55. data/lib/wx/version.rb +3 -0
  56. data/lib/wx.rb +42 -0
  57. data/lib/wxruby2.exp +0 -0
  58. data/lib/wxruby2.lib +0 -0
  59. data/lib/wxruby2.so +0 -0
  60. data/lib/wxruby2.so.manifest +13 -0
  61. data/samples/SAMPLES-LICENSE.TXT +18 -0
  62. data/samples/aui/aui.rb +1356 -0
  63. data/samples/bigdemo/About.rbw +39 -0
  64. data/samples/bigdemo/ColorPanel.rbw +23 -0
  65. data/samples/bigdemo/GridSimple.rbw +78 -0
  66. data/samples/bigdemo/MDIDemo.rbw +57 -0
  67. data/samples/bigdemo/PopupMenu.rbw +149 -0
  68. data/samples/bigdemo/ShapedWindow.rbw +131 -0
  69. data/samples/bigdemo/Sizers.rbw +543 -0
  70. data/samples/bigdemo/bigdemo.rb +823 -0
  71. data/samples/bigdemo/demoTemplate.rbw +33 -0
  72. data/samples/bigdemo/helpfile.htb +0 -0
  73. data/samples/bigdemo/icons/Test 015.jpg +0 -0
  74. data/samples/bigdemo/icons/Test 015.png +0 -0
  75. data/samples/bigdemo/icons/choice.bmp +0 -0
  76. data/samples/bigdemo/icons/choice.xpm +27 -0
  77. data/samples/bigdemo/icons/combo.bmp +0 -0
  78. data/samples/bigdemo/icons/combo.xpm +27 -0
  79. data/samples/bigdemo/icons/copy.xpm +25 -0
  80. data/samples/bigdemo/icons/cut.xpm +24 -0
  81. data/samples/bigdemo/icons/gauge.bmp +0 -0
  82. data/samples/bigdemo/icons/gauge.xpm +27 -0
  83. data/samples/bigdemo/icons/help.xpm +25 -0
  84. data/samples/bigdemo/icons/list.bmp +0 -0
  85. data/samples/bigdemo/icons/list.xpm +27 -0
  86. data/samples/bigdemo/icons/mondrian.ico +0 -0
  87. data/samples/bigdemo/icons/mondrian.xpm +44 -0
  88. data/samples/bigdemo/icons/new.xpm +24 -0
  89. data/samples/bigdemo/icons/ogl.ico +0 -0
  90. data/samples/bigdemo/icons/ogl.xpm +45 -0
  91. data/samples/bigdemo/icons/open.xpm +26 -0
  92. data/samples/bigdemo/icons/paste.bmp +0 -0
  93. data/samples/bigdemo/icons/paste.xpm +38 -0
  94. data/samples/bigdemo/icons/pointy.png +0 -0
  95. data/samples/bigdemo/icons/preview.xpm +26 -0
  96. data/samples/bigdemo/icons/print.xpm +26 -0
  97. data/samples/bigdemo/icons/radio.bmp +0 -0
  98. data/samples/bigdemo/icons/radio.xpm +27 -0
  99. data/samples/bigdemo/icons/robert.xpm +415 -0
  100. data/samples/bigdemo/icons/ruby.png +0 -0
  101. data/samples/bigdemo/icons/sashtest.ico +0 -0
  102. data/samples/bigdemo/icons/save.xpm +25 -0
  103. data/samples/bigdemo/icons/smiles.bmp +0 -0
  104. data/samples/bigdemo/icons/smiles.xpm +39 -0
  105. data/samples/bigdemo/icons/smiley.ico +0 -0
  106. data/samples/bigdemo/icons/smiley.xpm +42 -0
  107. data/samples/bigdemo/icons/stattext.xpm +24 -0
  108. data/samples/bigdemo/icons/test2.bmp +0 -0
  109. data/samples/bigdemo/icons/test2.png +0 -0
  110. data/samples/bigdemo/icons/test2.xpm +79 -0
  111. data/samples/bigdemo/icons/text.bmp +0 -0
  112. data/samples/bigdemo/icons/text.xpm +27 -0
  113. data/samples/bigdemo/icons/tog1.bmp +0 -0
  114. data/samples/bigdemo/icons/tog1.xpm +38 -0
  115. data/samples/bigdemo/icons/tog2.bmp +0 -0
  116. data/samples/bigdemo/icons/tog2.xpm +38 -0
  117. data/samples/bigdemo/icons/wxwin.ico +0 -0
  118. data/samples/bigdemo/icons/wxwin16x16.png +0 -0
  119. data/samples/bigdemo/icons/wxwin16x16.xpm +25 -0
  120. data/samples/bigdemo/icons/wxwin32x32.png +0 -0
  121. data/samples/bigdemo/icons/wxwin48x48.png +0 -0
  122. data/samples/bigdemo/run.rb +90 -0
  123. data/samples/bigdemo/tips.txt +7 -0
  124. data/samples/bigdemo/utils.rb +12 -0
  125. data/samples/bigdemo/wxArtProvider.rbw +281 -0
  126. data/samples/bigdemo/wxBitmapButton.rbw +65 -0
  127. data/samples/bigdemo/wxButton.rbw +64 -0
  128. data/samples/bigdemo/wxCalendarCtrl.rbw +60 -0
  129. data/samples/bigdemo/wxCheckBox.rbw +50 -0
  130. data/samples/bigdemo/wxCheckListBox.rbw +75 -0
  131. data/samples/bigdemo/wxChoice.rbw +47 -0
  132. data/samples/bigdemo/wxChoicebook.rbw +78 -0
  133. data/samples/bigdemo/wxColourDialog.rbw +31 -0
  134. data/samples/bigdemo/wxComboBox.rbw +77 -0
  135. data/samples/bigdemo/wxCursor.rbw +136 -0
  136. data/samples/bigdemo/wxDialog.rbw +74 -0
  137. data/samples/bigdemo/wxDirDialog.rbw +29 -0
  138. data/samples/bigdemo/wxDragImage.rbw +70 -0
  139. data/samples/bigdemo/wxFileDialog.rbw +37 -0
  140. data/samples/bigdemo/wxFileDialog_Save.rbw +35 -0
  141. data/samples/bigdemo/wxFindReplaceDialog.rbw +82 -0
  142. data/samples/bigdemo/wxFontDialog.rbw +173 -0
  143. data/samples/bigdemo/wxFrame.rbw +53 -0
  144. data/samples/bigdemo/wxGauge.rbw +71 -0
  145. data/samples/bigdemo/wxGenericDirCtrl.rbw +74 -0
  146. data/samples/bigdemo/wxGrid.rbw +66 -0
  147. data/samples/bigdemo/wxHtmlHelpController.rbw +52 -0
  148. data/samples/bigdemo/wxListBox.rbw +140 -0
  149. data/samples/bigdemo/wxListCtrl_virtual.rbw +107 -0
  150. data/samples/bigdemo/wxMDIWindows.rbw +50 -0
  151. data/samples/bigdemo/wxMenu.rbw +236 -0
  152. data/samples/bigdemo/wxMessageDialog.rbw +27 -0
  153. data/samples/bigdemo/wxMiniFrame.rbw +70 -0
  154. data/samples/bigdemo/wxMultipleChoiceDialog.rbw +32 -0
  155. data/samples/bigdemo/wxNotebook.rbw +136 -0
  156. data/samples/bigdemo/wxProgressDialog.rbw +43 -0
  157. data/samples/bigdemo/wxRadioBox.rbw +72 -0
  158. data/samples/bigdemo/wxRadioButton.rbw +125 -0
  159. data/samples/bigdemo/wxSashWindow.rbw +141 -0
  160. data/samples/bigdemo/wxScrolledMessageDialog.rbw +57 -0
  161. data/samples/bigdemo/wxScrolledWindow.rbw +199 -0
  162. data/samples/bigdemo/wxSingleChoiceDialog.rbw +33 -0
  163. data/samples/bigdemo/wxSlider.rbw +42 -0
  164. data/samples/bigdemo/wxSpinButton.rbw +50 -0
  165. data/samples/bigdemo/wxSpinCtrl.rbw +51 -0
  166. data/samples/bigdemo/wxSplitterWindow.rbw +63 -0
  167. data/samples/bigdemo/wxStaticBitmap.rbw +51 -0
  168. data/samples/bigdemo/wxStaticText.rbw +55 -0
  169. data/samples/bigdemo/wxStatusBar.rbw +126 -0
  170. data/samples/bigdemo/wxTextCtrl.rbw +149 -0
  171. data/samples/bigdemo/wxTextEntryDialog.rbw +31 -0
  172. data/samples/bigdemo/wxToggleButton.rbw +49 -0
  173. data/samples/bigdemo/wxToolBar.rbw +131 -0
  174. data/samples/bigdemo/wxTreeCtrl.rbw +190 -0
  175. data/samples/calendar/calendar.rb +271 -0
  176. data/samples/caret/caret.rb +282 -0
  177. data/samples/caret/mondrian.xpm +44 -0
  178. data/samples/controls/controls.rb +1136 -0
  179. data/samples/controls/get_item_sample.rb +87 -0
  180. data/samples/controls/icons/choice.xpm +27 -0
  181. data/samples/controls/icons/combo.xpm +27 -0
  182. data/samples/controls/icons/gauge.xpm +27 -0
  183. data/samples/controls/icons/list.xpm +27 -0
  184. data/samples/controls/icons/radio.xpm +27 -0
  185. data/samples/controls/icons/stattext.xpm +24 -0
  186. data/samples/controls/icons/text.xpm +27 -0
  187. data/samples/controls/mondrian.ico +0 -0
  188. data/samples/controls/mondrian.xpm +44 -0
  189. data/samples/controls/test2.bmp +0 -0
  190. data/samples/dialogs/dialogs.rb +723 -0
  191. data/samples/dialogs/tips.txt +18 -0
  192. data/samples/drawing/graphics_drawing.rb +235 -0
  193. data/samples/drawing/images.rb +37 -0
  194. data/samples/drawing/paperclip.png +0 -0
  195. data/samples/etc/activation.rb +102 -0
  196. data/samples/etc/choice.rb +67 -0
  197. data/samples/etc/miniframe.rb +79 -0
  198. data/samples/etc/sash.rb +130 -0
  199. data/samples/etc/scrollwin.rb +110 -0
  200. data/samples/etc/system_settings.rb +252 -0
  201. data/samples/etc/threaded.rb +72 -0
  202. data/samples/etc/wizard.rb +74 -0
  203. data/samples/event/event.rb +182 -0
  204. data/samples/grid/grid.rb +198 -0
  205. data/samples/grid/gridtablebase.rb +134 -0
  206. data/samples/html/html.rb +262 -0
  207. data/samples/listbook/listbook.rb +174 -0
  208. data/samples/listbook/listbook.xrc +370 -0
  209. data/samples/mdi/mdi.rb +82 -0
  210. data/samples/media/mediactrl.rb +167 -0
  211. data/samples/minimal/minimal.rb +77 -0
  212. data/samples/minimal/mondrian.ico +0 -0
  213. data/samples/minimal/mondrian.png +0 -0
  214. data/samples/minimal/nothing.rb +16 -0
  215. data/samples/opengl/cube.rb +117 -0
  216. data/samples/printing/mondrian.ico +0 -0
  217. data/samples/printing/mondrian.xpm +44 -0
  218. data/samples/printing/printing.rb +310 -0
  219. data/samples/sockets/SocketPackets.rb +27 -0
  220. data/samples/sockets/res/message-new.png +0 -0
  221. data/samples/sockets/res/user.png +0 -0
  222. data/samples/sockets/wxClient.rb +395 -0
  223. data/samples/sockets/wxServer.rb +422 -0
  224. data/samples/sockets/wxSocketGUI.rb +97 -0
  225. data/samples/text/mondrian.ico +0 -0
  226. data/samples/text/mondrian.xpm +44 -0
  227. data/samples/text/scintilla.rb +169 -0
  228. data/samples/text/textctrl.rb +113 -0
  229. data/samples/text/unicode.rb +242 -0
  230. data/samples/text/utf8.txt +15 -0
  231. data/samples/treectrl/icon1.xpm +79 -0
  232. data/samples/treectrl/icon2.xpm +53 -0
  233. data/samples/treectrl/icon3.xpm +79 -0
  234. data/samples/treectrl/icon4.xpm +43 -0
  235. data/samples/treectrl/icon5.xpm +79 -0
  236. data/samples/treectrl/treectrl.rb +1174 -0
  237. data/samples/xrc/samples.xrc +46 -0
  238. data/samples/xrc/xrc_sample.rb +96 -0
  239. metadata +316 -0
@@ -0,0 +1,395 @@
1
+ #!/usr/bin/env ruby
2
+ # wxRuby2 Sample Code. Copyright (c) 2007-???? Mario J. Steele
3
+ # Freely reusable code: see SAMPLES-LICENSE.TXT for details
4
+
5
+ # This sample is of a client impelmentation using Ruby's green
6
+ # threads, Ruby's Asynchronous Sockets, and wxRuby Widgets, so as
7
+ # to not block the GUI while waiting for data.
8
+
9
+ begin
10
+ require 'wx'
11
+ rescue LoadError => no_wx_err
12
+ begin
13
+ require 'rubygems'
14
+ require 'wx'
15
+ rescue LoadError
16
+ raise no_wx_err
17
+ end
18
+ end
19
+
20
+ require 'thread' # For Thread and Mutex
21
+ require 'socket' # For TCP/IP Communications
22
+
23
+ $LOAD_PATH << File.dirname(__FILE__)
24
+ require 'wxSocketGUI' # Our GUI Interface
25
+ require 'SocketPackets' # For simplfying Socket communciation
26
+
27
+ class String
28
+ # String#index_from(index,to_index)
29
+ # This finds the to_index from the index onwards till the end of the string.
30
+ # Returns the find_index + offset value of index.
31
+ def index_from(i,mtch)
32
+ self[i..-1].index(mtch) + i
33
+ end
34
+ end
35
+
36
+
37
+ class MyClientFrame < SocketGUI
38
+ include Socket::Constants
39
+ @@constants = %w[
40
+ ID_CONNECT
41
+ ID_DISCONNECT
42
+ ID_NAME
43
+ ID_EXIT
44
+ ID_TIMER_POLL
45
+ ]
46
+
47
+ # Creates the menus used by the Client.
48
+ def create_menus()
49
+ mb = Wx::MenuBar.new
50
+
51
+ fileMenu = Wx::Menu.new
52
+ fileMenu.append(ID_CONNECT,"&Connect\tCtrl+S","Connect to a server")
53
+ fileMenu.append(ID_DISCONNECT,"&Disconnect\tCtrl+G","Disconnect from a server")
54
+ fileMenu.append_separator()
55
+ fileMenu.append(ID_NAME,"&Nickname\tCtrl+N","Set your nickname")
56
+ fileMenu.append_separator()
57
+ fileMenu.append(ID_EXIT,"E&xit\tCtrl+X","Exit the client")
58
+
59
+ mb.append(fileMenu,"&File")
60
+
61
+ set_menu_bar(mb)
62
+ end
63
+
64
+ # Called from SocketGUI, to initialize our client window.
65
+ def on_init()
66
+ @nick = "Client"
67
+ @port = "1234"
68
+ @addr = $LOCALHOST_NAME
69
+ @mutex = Mutex.new
70
+ @buffer = ""
71
+ @timer = Wx::Timer.new(self,ID_TIMER_POLL)
72
+
73
+ evt_menu(ID_CONNECT) { on_connect }
74
+ evt_menu(ID_DISCONNECT) { on_disconnect }
75
+ evt_menu(ID_NAME) { on_name }
76
+ evt_menu(ID_EXIT) { on_exit }
77
+
78
+ evt_close { |evt| on_close(evt) }
79
+ evt_text_enter(@input.get_id) { on_text }
80
+ evt_timer(ID_TIMER_POLL) { Thread.pass }
81
+
82
+ get_menu_bar.enable(ID_CONNECT,true)
83
+ get_menu_bar.enable(ID_DISCONNECT,false)
84
+ end
85
+
86
+ # MyClientFrame#on_connect()
87
+ # Called from File > Connect, which prompts the user where they want to connect
88
+ # to, and starts the process for connecting to the server. Since we're looking
89
+ # at a possibility of the connection not being completed immediately, we delegate
90
+ # the confirmation from the connection, to our polling loop here. And we utilize
91
+ # the less known method Socket#connect_nonblock(). Oviously, Ruby will throw
92
+ # an exception if the connection isn't completed immedately, which is alright,
93
+ # we are expecting such. So we ignore any errors, and process thoes in our poll
94
+ # loop for connecting.
95
+ def on_connect
96
+ unless @socket.nil?
97
+ Wx::message_box("You are already connected to a server!","Connect to...",Wx::ID_OK|Wx::ICON_ERROR)
98
+ return
99
+ end
100
+ msgbox = Wx::TextEntryDialog.new(self,"Enter the server you wish to connect to: (Default: #{$LOCALHOST_NAME}:1234)",
101
+ "Connect to server",@addr + ":" + @port.to_s)
102
+ ret = msgbox.show_modal
103
+ if ret == Wx::ID_CANCEL
104
+ return
105
+ end
106
+ @socket = Socket.new(AF_INET,SOCK_STREAM,0)
107
+ sockaddr = msgbox.get_value
108
+ unless sockaddr.index(":")
109
+ sockaddr += ":1234"
110
+ end
111
+ @host,@port = sockaddr.split(":")
112
+ sockaddr = Socket.sockaddr_in(@port,@host)
113
+ begin
114
+ @socket.connect_nonblock(sockaddr)
115
+ rescue
116
+ # We're polling for connection anyways
117
+ end
118
+ append_prog_msg("Attempting to connect to #{@host}:#{@port}")
119
+ get_menu_bar.enable(ID_CONNECT,false)
120
+ get_menu_bar.enable(ID_DISCONNECT,false)
121
+ start_client_thread
122
+ end
123
+
124
+ # MyClientFrame#disconnect()
125
+ # This method utitilizes the closing of the socket connection, and killing
126
+ # any thread that may be running, and since we have no threads, we don't need
127
+ # the timer to be running, so we stop that as well.
128
+ def disconnect()
129
+ return if @socket.nil?
130
+ Thread.kill(@thread) unless @thread.nil?
131
+ @socket.send_packet("QUIT")
132
+ @socket.close()
133
+ @socket = nil
134
+ @timer.stop
135
+ get_menu_bar.enable(ID_CONNECT,true)
136
+ get_menu_bar.enable(ID_DISCONNECT,false)
137
+ end
138
+
139
+ # MyClientFrame#on_disconnect()
140
+ # This method is ran from File > Disconnect and processes the disconnection from
141
+ # the server.
142
+ def on_disconnect()
143
+ disconnect()
144
+ append_prog_msg("You have disconnected from the server.")
145
+ end
146
+
147
+ # MyClientFrame#on_name()
148
+ # This method is ran from File > Nickname, and processes the nickname that the
149
+ # client will use.
150
+ def on_name
151
+ msgbox = Wx::TextEntryDialog.new(self,"Enter the new nickname you want to be (Default: Client)",
152
+ "Change Nickname",@nick)
153
+ ret = msgbox.show_modal
154
+ if ret == Wx::ID_CANCEL
155
+ return
156
+ end
157
+
158
+ nick = msgbox.get_value
159
+ nick = nick.split(" ").join("_") if nick.index(" ")
160
+ @socket.send_packet("NICK #{nick}") unless @socket.nil?
161
+ append_prog_msg("You changed nicknames from #{@nick} to #{nick}")
162
+ @nick = nick
163
+ @socket.send_packet("WHO") unless @socket.nil?
164
+ end
165
+
166
+ # MyClientFrame#on_exit()
167
+ # This method is ran from File > Exit, and delegates the control over to
168
+ # MyClientFrame#on_close() to do it's checks to see if they are still connected
169
+ # or not.
170
+ def on_exit
171
+ self.close
172
+ end
173
+
174
+ # MyClientFrame#on_close(evt)
175
+ # This method is ran from the OnCloseEvent, and checks to see if the client is still
176
+ # connected to the server. If they are, it will confirm if they want to disconnect
177
+ # from the server or not. If they don't want to disconnect, it will veto the close
178
+ # event.
179
+ def on_close(evt)
180
+ unless @socket.nil?
181
+ ret = Wx::message_box("You are still connected to the server, do you wish to disconnect?","Disconnect",Wx::YES|Wx::NO|Wx::ICON_QUESTION)
182
+ if ret == Wx::NO
183
+ evt.veto
184
+ else
185
+ disconnect()
186
+ Wx::get_app.exit_main_loop()
187
+ end
188
+ else
189
+ Wx::get_app.exit_main_loop()
190
+ end
191
+ end
192
+
193
+ # MyClientFrame#on_text()
194
+ # This method is executed from when the user presses the Enter button from the
195
+ # input box. It will processes it for any /commands, and run them as nesscary
196
+ # otherwise, it will send the data to the server.
197
+ def on_text
198
+ msg = @input.get_value
199
+ @input.set_value("")
200
+ case msg
201
+ when /\/connect/
202
+ on_connect
203
+ when /\/disconnect/
204
+ on_disconnect
205
+ when /\/nick/
206
+ on_name
207
+ when /\/exit/
208
+ self.close
209
+ when /\/eval/
210
+ evl_str = msg[5..-1]
211
+ append_prog_msg(self.instance_eval(evl_str).inspect)
212
+ when /\/me/
213
+ append_msg("<-- #{@nick} #{msg[4..-1]}")
214
+ msg = "EMOTE " + msg[4..-1]
215
+ @socket.send_packet(msg)
216
+ else
217
+ append_msg("<-- #{@nick} says: #{msg}")
218
+ msg = "MSG " + msg
219
+ @socket.send_packet(msg)
220
+ end
221
+ end
222
+
223
+ # MyClientFrame#poll_connect()
224
+ # This method is called from within the polling thread, and will run till
225
+ # it either finds that the socket has been completed, and connected to a server,
226
+ # otherwise, it will check to see if there is an error. If there is, then that
227
+ # means we weren't able to connect to the server. This will then run the method
228
+ # Socket#connect() inside a rescue condition, so that we can see why we wasn't
229
+ # able to connect to the server.
230
+ def poll_connect()
231
+ loop do
232
+ @mutex.lock
233
+ ret = IO::select(nil,[@socket],[@socket],0.3)
234
+ unless ret.nil?
235
+ if Wx::PLATFORM == "WXMSW"
236
+ unless ret[2] == []
237
+ # We have an error
238
+ error = nil
239
+ begin
240
+ error = @socket.connect(Socket.sockaddr_in(@port,@host))
241
+ rescue => e
242
+ error = e
243
+ end
244
+ append_error("Error attempting to connect to #{@host}:#{@port}")
245
+ append_error("Message: #{e} (#{e.class})")
246
+ @mutex.unlock
247
+ @socket.close()
248
+ @socket = nil
249
+ get_menu_bar.enable(ID_CONNECT,true)
250
+ get_menu_bar.enable(ID_DISCONNECT,false)
251
+ @timer.stop()
252
+ Thread.kill(@thread)
253
+ @thread.exit
254
+ return
255
+ end
256
+ else # For WXGTK and WXMAC
257
+ # Since on WXGTK and WXMAC doesn't return the error part in ret, we need
258
+ # to check and see if we are connected.
259
+ error = nil
260
+ begin
261
+ error = @socket.connect(Socket.sockaddr_in(@port,@host))
262
+ rescue => e
263
+ error = e
264
+ end
265
+ if error.class != Fixnum && !error.nil?
266
+ # We're not connected!
267
+ append_error("Error attempting to connect to #{@host}:#{@port}")
268
+ append_error("Message: #{e} (#{e.class})")
269
+ @mutex.unlock
270
+ @socket.close()
271
+ @socket = nil
272
+ get_menu_bar.enable(ID_CONNECT,true)
273
+ get_menu_bar.enable(ID_DISCONNECT,false)
274
+ @timer.stop()
275
+ Thread.kill(@thread)
276
+ @thread.exit
277
+ return
278
+ end
279
+ end
280
+ append_prog_msg("Connected to #{@host}:#{@port}")
281
+ get_menu_bar.enable(ID_CONNECT,false)
282
+ get_menu_bar.enable(ID_DISCONNECT,true)
283
+ @socket.send_packet("CONNECT #{@nick}")
284
+ @socket.send_packet("WHO")
285
+ @mutex.unlock
286
+ break
287
+ end
288
+ @mutex.unlock
289
+ end
290
+ end
291
+
292
+ # MyClientFrame#parse_message(msg)
293
+ # This method checks to see what message is being received from the server
294
+ # add display it to the user, as needed.
295
+ def parse_message(msg)
296
+ case msg
297
+ when /CONNECT/
298
+ append_msg("--> #{msg[8..-1]} has joined the chat server.")
299
+ #@users.insert_item(0,msg[8..-1],0)
300
+ add_user(msg[8..-1])
301
+ when /DISCONN/
302
+ append_msg("--> #{msg[8..-1]} has been removed from the chat server.")
303
+ when /CONN/
304
+ append_msg("<!> New connection incoming to the server.")
305
+ when /WHO/
306
+ cmd, *usrs = msg.split(" ")
307
+ clear_users()
308
+ usrs.each do |usr|
309
+ add_user(usr)
310
+ end
311
+ when /NICK/
312
+ a = msg.index_from(5," ")
313
+ old = msg[5..a-1]
314
+ new = msg[a+1..-1]
315
+ append_msg("--> #{old} has changed their nickname to #{new}")
316
+ @socket.send_packet("WHO")
317
+ when /MSG/
318
+ a = msg.index_from(4," ")
319
+ user = msg[4..a-1]
320
+ txt = msg[a+1..-1]
321
+ append_msg("--> #{user} says: #{txt}")
322
+ when /EMOTE/
323
+ a = msg.index_from(6," ")
324
+ user = msg[6..a-1]
325
+ txt = msg[a+1..-1]
326
+ append_msg("--> #{user} #{txt}")
327
+ when /QUIT/
328
+ append_msg("--> #{msg[5..-1]} is quitting the chat server.")
329
+ @socket.send_packet("WHO")
330
+ when /SHUTDOWN/
331
+ append_msg("--> #{msg[9..-1]}")
332
+ else
333
+ append_error("Unknown message received from the server.")
334
+ append_error("Message: #{msg}")
335
+ end
336
+ end
337
+
338
+ # MyClientFrame#poll_data()
339
+ # This method will poll the connection for incoming data, and to see if it
340
+ # has been disconnected from the server or not. It will continue to poll
341
+ # till a connection has been closed, either by the server, or by the user.
342
+ def poll_data()
343
+ loop do
344
+ @mutex.lock
345
+ ret = IO::select([@socket],nil,nil,0.3)
346
+ unless ret.nil?
347
+ if @socket.eof?
348
+ append_prog_msg("You have been disconnected from the server!")
349
+ get_menu_bar.enable(ID_CONNECT,true)
350
+ get_menu_bar.enable(ID_DISCONNECT,false)
351
+ @socket.close
352
+ @socket = nil
353
+ @timer.stop
354
+ @mutex.unlock
355
+ @thread.exit
356
+ break
357
+ end
358
+ msg = @socket.recv_packet()
359
+ @buffer += msg
360
+ loop do
361
+ at = @buffer.index("\n")
362
+ if at.nil?
363
+ break
364
+ end
365
+ data = @buffer[0..at-1]
366
+ @buffer = @buffer[at+1..-1]
367
+ parse_message(data)
368
+ end
369
+ end
370
+ @mutex.unlock
371
+ end
372
+ end
373
+
374
+ # MyClientFrame#start_client_thread()
375
+ # This method will actually start the polling of a connection for the connection
376
+ # and data. First it will start the polling of the connection, after it successfully
377
+ # receives the connection, it will start polling for incoming data.
378
+ def start_client_thread
379
+ @thread = Thread.new do
380
+ @timer.start(25)
381
+ poll_connect()
382
+ poll_data()
383
+ end
384
+ end
385
+ end
386
+
387
+ class MySocketApp < Wx::App
388
+ def on_init
389
+ Thread.abort_on_exception = true
390
+ frame = MyClientFrame.new(nil,-1,"Socket Demo >> Client")
391
+ frame.show
392
+ end
393
+ end
394
+
395
+ MySocketApp.new.main_loop