vimamsa 0.1.9 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,122 @@
1
+ # PopupFormGenerator.new().run
2
+ class PopupFormGenerator
3
+ def submit()
4
+ for id, entry in @vals
5
+ @ret[id] = entry.text
6
+ end
7
+ if !@callback.nil?
8
+ @callback.call(@ret)
9
+ end
10
+ @window.destroy
11
+ end
12
+
13
+ def initialize(params = nil)
14
+ @ret = {}
15
+ @window = Gtk::Window.new()
16
+ # @window.screen = main_window.screen
17
+ # @window.title = title
18
+ # params = {}
19
+ # params["inputs"] = {}
20
+ # params["inputs"]["search"] = { :label => "Search", :type => :entry }
21
+ # params["inputs"]["replace"] = { :label => "Replace", :type => :entry }
22
+ # params["inputs"]["btn1"] = { :label => "Replace all", :type => :button }
23
+ # params[:callback] = proc { |x| puts "====="; puts x.inspect; puts "=====" }
24
+
25
+
26
+ @callback = params[:callback]
27
+ @window.title = ""
28
+
29
+ frame = Gtk::Frame.new()
30
+ frame.margin_bottom = 8
31
+ frame.margin_top = 8
32
+ frame.margin_end = 8
33
+ frame.margin_start = 8
34
+
35
+ @window.set_child(frame)
36
+
37
+ # @window.title = params["title"]
38
+
39
+ # @callback = params["callback"]
40
+
41
+ vbox = Gtk::Box.new(:vertical, 8)
42
+ vbox.margin_bottom = 8
43
+ vbox.margin_top = 8
44
+ vbox.margin_end = 8
45
+ vbox.margin_start = 8
46
+
47
+ frame.set_child(vbox)
48
+
49
+ if params.has_key?("title")
50
+ infolabel = Gtk::Label.new
51
+ infolabel.markup = params["title"]
52
+ #TODO:gtk4
53
+ # vbox.pack_start(infolabel, :expand => false, :fill => false, :padding => 0)
54
+ vbox.pack_end(infolabel, :expand => false, :fill => false, :padding => 0)
55
+ end
56
+
57
+ hbox = Gtk::Box.new(:horizontal, 8)
58
+ @vals = {}
59
+ @default_button = nil
60
+
61
+ for id, elem in params["inputs"]
62
+ if elem[:type] == :button
63
+ button = Gtk::Button.new(:label => elem[:label])
64
+ hbox.pack_end(button, :expand => false, :fill => false, :padding => 0)
65
+ if elem[:default_focus] == true
66
+ @default_button = button
67
+ end
68
+ button.signal_connect "clicked" do
69
+ @ret[id] = "submit"
70
+ submit
71
+ end
72
+ elsif elem[:type] == :entry
73
+ label = Gtk::Label.new(elem[:label])
74
+ entry = Gtk::Entry.new
75
+ if elem.has_key?(:initial_text)
76
+ entry.text = elem[:initial_text]
77
+ end
78
+ hbox.pack_end(label, :expand => false, :fill => false, :padding => 0)
79
+ hbox.pack_end(entry, :expand => false, :fill => false, :padding => 0)
80
+ @vals[id] = entry
81
+
82
+ press = Gtk::EventControllerKey.new
83
+ press.set_propagation_phase(Gtk::PropagationPhase::CAPTURE)
84
+ entry.add_controller(press)
85
+ press.signal_connect "key-pressed" do |gesture, keyval, keycode, y|
86
+ if keyval == Gdk::Keyval::KEY_Return
87
+ submit
88
+ true
89
+ elsif keyval == Gdk::Keyval::KEY_Escape
90
+ @window.destroy
91
+ true
92
+ else
93
+ false
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ vbox.pack_end(hbox, :expand => false, :fill => false, :padding => 0)
100
+
101
+ cancel_button = Gtk::Button.new(:label => "Cancel")
102
+ cancel_button.signal_connect "clicked" do
103
+ @window.destroy
104
+ end
105
+ hbox.pack_end(cancel_button, :expand => false, :fill => false, :padding => 0)
106
+ @cancel_button = cancel_button
107
+ return
108
+ end
109
+
110
+ def run
111
+ if !@window.visible?
112
+ @window.show
113
+ else
114
+ @window.destroy
115
+ end
116
+ if !@default_button.nil?
117
+ @default_button.grab_focus
118
+ end
119
+ @window.set_focus_visible(true)
120
+ @window
121
+ end
122
+ end