7 use Glib qw/TRUE FALSE/;
10 use Gtk2::Gdk::Keysyms;
12 my $callsign = uc $ARGV[0];
13 #check and warn if we received wrong argument
15 print "usage <program name> <callsign> [<host> <port>]\n";
19 my ($sock, $MAXLEN, $SEND_PORT, $SEND_HOST) = (undef, 1024, 7301, "localhost");
22 $SEND_HOST = $ARGV[1];
23 $SEND_PORT = $ARGV[2];
28 #---------------------------------
29 #set up a udp server waiting for incomming messages
30 $sock = IO::Socket::INET->new(PeerHost => $SEND_HOST, PeerPort => $SEND_PORT, Proto => 'tcp')
33 #add a Gtk2::Helper watch on any incomming connections
34 Gtk2::Helper->add_watch ( fileno $sock, 'in', sub{
35 my ($fd,$condition,$fh) = @_;
36 #call 'watch_callback' to handle the incoming data
37 \&watch_callback($fh, $tview);
40 print "Awaiting TCP messages on $SEND_HOST:$SEND_PORT\n";
41 #---------------------------------
43 #Gtk2::Rc->parse ('/usr/share/themes/Anger/gtk/gtkrc');
44 #this is parsing our theme file, giving a personal touch
45 #Gtk2::Rc->parse ('gtkrc');
47 #standard window creation, placement, and signal connecting
48 my $window = Gtk2::Window->new('toplevel');
49 $window->signal_connect('delete_event' => sub { exit;});
50 $window->set_border_width(5);
51 $window->set_position('center_always');
52 #again just some fine touches
53 $window->set_title("gconsole: $callsign");
54 #$window->set_icon_from_file($img_send);
56 #this vbox will geturn the bulk of the gui
57 my ($vbox) = &ret_vbox();
59 #add and show the vbox
68 my $vbox = Gtk2::VBox->new(FALSE,0);
69 #add an image to indicate who you are
70 # my $img_who = Gtk2::Image->new_from_file($img_big);
72 # $vbox->pack_start($img_who,TRUE,TRUE,0);
74 my $frame = Gtk2::Frame->new("gconsole: $callsign");
76 #method of Gtk2::Container
77 $frame->set_border_width(5);
79 my $sw = Gtk2::ScrolledWindow->new (undef, undef);
80 $sw->set_shadow_type ('etched-out');
81 $sw->set_policy ('automatic', 'automatic');
82 #This is a method of the Gtk2::Widget class,it will force a minimum
83 #size on the widget. Handy to give intitial size to a
84 #Gtk2::ScrolledWindow class object
85 $sw->set_size_request (800, 600);
86 #method of Gtk2::Container
87 $sw->set_border_width(5);
89 $tview = Gtk2::TextView->new();
90 #we do not want to edit anything
91 $tview->set_editable(FALSE);
92 $tview->set_cursor_visible (FALSE);
94 my $buffer = $tview->get_buffer();
95 $buffer->create_tag ("monospace", family => "monospace");
97 #create a mark at the end of the buffer, and on each
98 #'insert_text' we tell the textview to scroll to that mark
99 $buffer->create_mark ('end', $buffer->get_end_iter, FALSE);
100 # $buffer->signal_connect (insert_text => sub {
101 # $tview->scroll_to_mark ($buffer->get_mark ('end'),
102 # 0.0, TRUE, 0, 0.5);
105 #create a tag for the shreck
106 # $buffer->create_tag ("shrek",
108 # weight => PANGO_WEIGHT_ULTRALIGHT,
109 # family => 'flubber',
110 # foreground => "#189f3b",
114 #create a tag for the donkey
115 # $buffer->create_tag ("donkey",
117 # weight => PANGO_WEIGHT_ULTRALIGHT,
119 # foreground => "blue",
125 $vbox->pack_start($frame,TRUE,TRUE,4);
126 #--------------------------------------
127 my $hbox = Gtk2::HBox->new(FALSE,5);
129 my $ent_send = Gtk2::Entry->new;
130 $hbox->pack_start($ent_send,TRUE,TRUE,0);
132 my $btn_send = Gtk2::Button->new_from_stock('gtk-ok');
133 #connect the 'key_press_signal' to a handler that will
134 #filter for 'Return'; if TRUE, trigger a button click
135 $ent_send->signal_connect('key_press_event'=> sub {
136 my ($widget,$event) = @_;
137 if ($event->keyval == $Gtk2::Gdk::Keysyms{Return}) {
144 $btn_send->signal_connect("clicked" =>sub {
145 #get the contents of the entry
146 my $msg_send = $ent_send->get_text;
148 $ent_send->set_text("");
149 #grab focus again for the next round of talks
150 $ent_send->grab_focus;
151 #if there was bogus input, ignore it!
152 if ($msg_send !~ m/^$/) {
154 $msg =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
155 syswrite($sock, $msg . "\r\n") or die "syswrite: $!";
156 update_buffer($buffer, $msg_send, TRUE);
160 $hbox->pack_end($btn_send,TRUE,TRUE,0);
161 #--------------------------------------
162 $vbox->pack_start($hbox,TRUE,TRUE,4);
164 $vbox->set_focus_child($hbox);
174 my ($fh,$tview) = @_;
176 my $r = sysread($fh, $msg, $MAXLEN) or die "sysread: $!";
177 if (defined $r && $r) {
178 $msg =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
180 my $buffer = $tview->get_buffer();
181 update_buffer($buffer, $msg, FALSE);
189 my ($buffer,$msg,$send)= @_;
192 my $iter = $buffer->get_end_iter;
193 $buffer->insert_with_tags_by_name($iter, $msg, "monospace");
194 $tview->scroll_to_mark ($buffer->get_mark ('end'), 0.0, TRUE, 0, 0.5);