-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui2.rb
100 lines (81 loc) · 2.43 KB
/
gui2.rb
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
#!/usr/bin/env ruby
require 'gtk2'
require './client2.rb'
class GUI
def initialize
# layout elements
@layoutBox = Gtk::VBox.new(false, 5)
@submitBox = Gtk::HBox.new(false, 5)
# gui elements
@chatArea = Gtk::TextView.new
@inputArea = Gtk::TextView.new
@submit = Gtk::Button.new("Nachricht senden")
# size of elements
@chatArea.set_size_request(500, 500)
@inputArea.set_size_request(350, 30)
@submit.set_size_request(150, 30)
# chatArea settings
@chatArea.set_editable false
@chatArea.cursor_visible = false
@chatArea.buffer.text = "Herzlich Willkommen zur Test-Version des P2P-Clients\n"
@chatArea.buffer.text += "+----------------------\n"
@chatArea.modify_base(Gtk::STATE_NORMAL,Gdk::Color.new(0,0,0))
@chatArea.modify_text(Gtk::STATE_NORMAL,Gdk::Color.new(65535,65535,65535))
# inputArea settings
@inputArea.modify_base(Gtk::STATE_NORMAL,Gdk::Color.new(0,0,0))
@inputArea.modify_text(Gtk::STATE_NORMAL,Gdk::Color.new(65535,65535,65535))
@inputArea.add_events(Gdk::Event::KEY_PRESS)
@inputArea.set_left_margin 4
@inputArea.buffer.place_cursor(@inputArea.buffer.start_iter)
# layout settings
@submitBox.add(@inputArea)
@submitBox.add(@submit)
@layoutBox.add(@chatArea)
@layoutBox.add(@submitBox)
# main window
@window = Gtk::Window.new("P2P-Chat-Client")
@window.add(@layoutBox)
@window.border_width = 10
@window.set_resizable false
@window.show_all
# Client instance for functionality
@client = Client.new
# @client.connect
#signals
@window.signal_connect("destroy") {
@client.disconnect
Gtk.main_quit
}
@submit.signal_connect("clicked") {
send_text
}
@inputArea.signal_connect("key-press-event") do |w, e|
if e.keyval == 65293
send_text
end
end
end
def start_gui
begin
t1 = Thread.new {
while message = @client.receive_message
@chatArea.buffer.text += message += "\n"
end
}
Gtk.main
rescue Interrupt
@client.disconnect
puts "\nProgramm beenden..."
rescue Exception=>e
@client.disconnect
puts "\nEin unbekannter Fehler ist aufgetreten."
end
end
def send_text
@client.send_message(@inputArea.buffer.text)
@chatArea.buffer.text += "Ich: " + @inputArea.buffer.text + "\n"
@inputArea.buffer.text = "";
end
end
root = GUI.new
root.start_gui