New problem: Newlines

I have this:

test2 = TextCtrl.new(frame, 10, "Output here!", Point.new(0, 501),

Size.new(500, 25), TE_PROCESS_ENTER)
display.write_text " \n You entered: #{str}"

But the ‘\n’ isn’t… calculated? Inserted? It reads that there is
SOMETHING there, because the \n doesn’t show up in the text, but I can
comment it out (\n) and the \n will then show. It looked like to me
that the API documentation says that newline characters are the only
control characters allowed in write_text, and that they work. (Oh, and
display is a variable for test2, in a different method)

Oh and also, is it possible/would it be ‘correct’ for me to add some
stuff into the ‘main_loop’? I would like to add something like:

def main_loop
Thread.new do ||
inp = connection.recv(1000000)
display.write_text inp
end
end

would that screw with what is already in the main_loop too much?

Tim M. wrote:

I have this:

test2 = TextCtrl.new(frame, 10, "Output here!", Point.new(0, 501),

Size.new(500, 25), TE_PROCESS_ENTER)
display.write_text " \n You entered: #{str}"

But the ‘\n’ isn’t… calculated? Inserted? It reads that there is
SOMETHING there, because the \n doesn’t show up in the text,

I’m sure that’ll be a newline; you could check by doing “p text2.value”
in Ruby. I guess what’s happening is that Ruby is translating \n into a
newline because it’s inside a double-quoted string. This is done before
wxRuby gets the string, so wxRuby gets a literal newline character. But
that works fine I think.

but I can
comment it out (\n) and the \n will then show.
So - what’s the problem you’re trying to solve?

alex

Alex F. wrote:

Tim M. wrote:

would that screw with what is already in the main_loop too much?

It should work OK, but you’ll need to add something like

Wx::Timer.every(50) { Thread.pass }

to your main_loop. This will ensure that on Ruby 1.8 your non-GUI ruby
threads get execution time, otherwise they won’t progress.

Have a look at the threaded.rb and chat client examples in the samples
directory as they demonstrate threads and network programming.

hth
alex

Thanks!

And the problem in the first post is this: When I run in, there is no
linebreaks. I am trying to make it so that there ARE line breaks via the
newline character.

Sorry for even more noobiness, but; did I just find a new problem?

Code [
require ‘rubygems’
require ‘socket’
require ‘wx’
include Wx

$connection = TCPSocket.new(“dark-legacy.com”, 9898)

class GuiMudClientApp < App
THREAD_TIMER_ID = ID_HIGHEST + 1
def on_init
#Timer.every(25) { Thread.pass }
frame = Frame.new(nil, -1, “GMC: Incoming”, Point.new(100, 100),
Size.new(500, 550))
test = TextCtrl.new(frame, -1, “-----Mud stuff here-----”,
Point.new(0, 0), Size.new(500, 500), TE_READONLY)
test.append_text “Pfft!”
test2 = TextCtrl.new(frame, 10, “Output here!”, Point.new(0, 501),
Size.new(500, 25), TE_PROCESS_ENTER)
frame.show
evt_text_enter(10) do |event|
cmdentered(event.get_string, test2, test)
end
end

def cmdentered(str, textbox, display)
display.write_text “–(#{str})–”
$connection.write(str)
textbox.clear
end

def main_loop
a = Thread.new do
until connection.closed? == true
input = $connection.recv(1000000)
test.append_text input
end
end
Timer.every(25) { Thread.pass } ### LINE 35 <----
super
end
end

nowai = GuiMudClientApp.new

nowai.main_loop
]

Error:

/Library/Ruby/Gems/1.8/gems/wxruby-1.9.9-universal-darwin-9/lib/wx/classes/timer.rb:18:in
every': uninitialized constant Wxruby2::THE_APP (NameError) from guimc.rb:35:in main_loop’
from guimc.rb:42

Line 35 is the marked line in the above code. What’s wrong?

Tim M. wrote:

would that screw with what is already in the main_loop too much?

It should work OK, but you’ll need to add something like

Wx::Timer.every(50) { Thread.pass }

to your main_loop. This will ensure that on Ruby 1.8 your non-GUI ruby
threads get execution time, otherwise they won’t progress.

Have a look at the threaded.rb and chat client examples in the samples
directory as they demonstrate threads and network programming.

hth
alex

Mario S. wrote:

You need to make sure that you pass the TE_MULTILINE to the creation of
the
TextCtrl, EG:

test2 = TextCtrl.new(frame, 10, “Output here!”, Point.new(0,
501),Size.new(500, 25), TE_PROCESS_ENTER|TE_MULTILINE)

This will create a Multi-Line Text Control, which can display new lines,
if
you create a TextCtrl without the TE_MULTILINE, it’ll create a Single
Line
Text Control, even though the sizer will resize it to look like it’s a
Multi-Line Text control.

Thats good, thanks for that.

Have any idea about the ‘THE_APP’ error?

Right now, I’m trying to get a compile of the latest Subversion that
will be
deemed 2.0 going, so that I can run my tests to see if there are any
outstanding bugs that need to be covered. The WxRuby::THE_APP error, is
something that is kinda curious. I don’t have a Mac system in which to
test
out on, but from the looks of it, somehow WxRuby::THE_APP (AKA
Wx::THE_APP),
is not being set, when your code calls the GuiMudClientApp.new, which is
suppose to be handled internally by wxRuby when you create the very
first
App class or App subclass.

Meaning, that there is only 1 App that can be in existance during the
entire
run of wxRuby. There are ways around it, but it’s only for thoes who
are
more experienced with the library, and know of the dangers of it.
However,
in this case, your problem occurs in main_loop(), which is weird. My
suggestion to you, would be to move the code out of main_loop(), and
throw
it into on_init(). Since you will have $connection setup by that time.
Just to test and see if the problem actually does lie in main_loop(), or
not… And actually, I think on_init() isn’t actually called till you
execute main_loop(), it’s part of the process it goes through once
everything is initialized, it finalizes by running any on_init() methods
you
have defined. So try moving the code there. That’s the best I can
suggest,
till I get wxWidgets and wxRuby compiled on Windows, to test your code,
to
see if it has the same problem.

You need to make sure that you pass the TE_MULTILINE to the creation of
the
TextCtrl, EG:

test2 = TextCtrl.new(frame, 10, “Output here!”, Point.new(0,
501),Size.new(500, 25), TE_PROCESS_ENTER|TE_MULTILINE)

This will create a Multi-Line Text Control, which can display new lines,
if
you create a TextCtrl without the TE_MULTILINE, it’ll create a Single
Line
Text Control, even though the sizer will resize it to look like it’s a
Multi-Line Text control.

Okay, thanks you two! :slight_smile:

I’ll test it in a bit.

Tim M. wrote:

def on_init
#Timer.every(25) { Thread.pass }

def main_loop
a = Thread.new do
until connection.closed? == true
input = $connection.recv(1000000)
test.append_text input
end
end

nowai = GuiMudClientApp.new

Don’t override main_loop unless you know what you’re doing. And if you
do override it, you must call super() within it. This is the source of
your error.

Basically, all of your start-up code - including your Thread
initialisation - should go in the on_init method. This goes for prety
much any App.

a