vimamsa 0.1.4 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,562 @@
1
+ $idle_scroll_to_mark = false
2
+
3
+ def gui_open_file_dialog(dirpath)
4
+ dialog = Gtk::FileChooserDialog.new(:title => "Open file",
5
+ :action => :open,
6
+ :buttons => [[Gtk::Stock::OPEN, :accept],
7
+ [Gtk::Stock::CANCEL, :cancel]])
8
+ dialog.set_current_folder(dirpath)
9
+
10
+ dialog.signal_connect("response") do |dialog, response_id|
11
+ if response_id == Gtk::ResponseType::ACCEPT
12
+ open_new_file(dialog.filename)
13
+ # puts "uri = #{dialog.uri}"
14
+ end
15
+ dialog.destroy
16
+ end
17
+ dialog.run
18
+ end
19
+
20
+ def gui_file_saveas(dirpath)
21
+ dialog = Gtk::FileChooserDialog.new(:title => "Save as",
22
+ :action => :save,
23
+ :buttons => [[Gtk::Stock::SAVE, :accept],
24
+ [Gtk::Stock::CANCEL, :cancel]])
25
+ dialog.set_current_folder(dirpath)
26
+ dialog.signal_connect("response") do |dialog, response_id|
27
+ if response_id == Gtk::ResponseType::ACCEPT
28
+ file_saveas(dialog.filename)
29
+ end
30
+ dialog.destroy
31
+ end
32
+
33
+ dialog.run
34
+ end
35
+
36
+ def idle_func
37
+ # puts "IDLEFUNC"
38
+ if $idle_scroll_to_mark
39
+ # Ripl.start :binding => binding
40
+ # $view.get_visible_rect
41
+ vr = $view.visible_rect
42
+
43
+ # iter = b.get_iter_at(:offset => i)
44
+
45
+ b = $view.buffer
46
+ iter = b.get_iter_at(:offset => b.cursor_position)
47
+ iterxy = $view.get_iter_location(iter)
48
+ # puts "ITERXY" + iterxy.inspect
49
+ # Ripl.start :binding => binding
50
+
51
+ intr = iterxy.intersect(vr)
52
+ if intr.nil?
53
+ $view.set_cursor_pos($view.buffer.cursor_position)
54
+ else
55
+ $idle_scroll_to_mark = false
56
+ end
57
+
58
+ sleep(0.1)
59
+ end
60
+ sleep(0.01)
61
+ return true
62
+ end
63
+
64
+ def center_on_current_line()
65
+ b = $view.buffer
66
+ iter = b.get_iter_at(:offset => b.cursor_position)
67
+ within_margin = 0.0 #margin as a [0.0,0.5) fraction of screen size
68
+ use_align = true
69
+ xalign = 0.0 #0.0=top 1.0=bottom, 0.5=center
70
+ yalign = 0.5
71
+ $view.scroll_to_iter(iter, within_margin, use_align, xalign, yalign)
72
+ end
73
+
74
+ def get_visible_area()
75
+ view = $view
76
+ vr = view.visible_rect
77
+ startpos = view.get_iter_at_position_raw(vr.x, vr.y)[1].offset
78
+ endpos = view.get_iter_at_position_raw(vr.x + vr.width, vr.y + vr.height)[1].offset
79
+ return [startpos, endpos]
80
+ end
81
+
82
+ def page_up
83
+ $view.signal_emit("move-cursor", Gtk::MovementStep.new(:PAGES), -1, false)
84
+ return true
85
+ end
86
+
87
+ def page_down
88
+ $view.signal_emit("move-cursor", Gtk::MovementStep.new(:PAGES), 1, false)
89
+ return true
90
+ end
91
+
92
+ def paste_system_clipboard()
93
+ # clipboard = $vmag.window.get_clipboard(Gdk::Selection::CLIPBOARD)
94
+ utf8_string = Gdk::Atom.intern("UTF8_STRING")
95
+ # x = clipboard.request_contents(utf8_string)
96
+
97
+ widget = Gtk::Invisible.new
98
+ clipboard = Gtk::Clipboard.get_default($vmag.window.display)
99
+ received_text = ""
100
+
101
+ target_string = Gdk::Selection::TARGET_STRING
102
+ ti = clipboard.request_contents(target_string)
103
+
104
+ # clipboard.request_contents(target_string) do |_clipboard, selection_data|
105
+ # received_text = selection_data.text
106
+ # puts "received_text=#{received_text}"
107
+ # end
108
+ if clipboard.wait_is_text_available?
109
+ received_text = clipboard.wait_for_text
110
+ end
111
+
112
+ if received_text != "" and !received_text.nil?
113
+ max_clipboard_items = 100
114
+ if received_text != $clipboard[-1]
115
+ #TODO: HACK
116
+ $paste_lines = false
117
+ end
118
+ $clipboard << received_text
119
+ # puts $clipboard[-1]
120
+ $clipboard = $clipboard[-([$clipboard.size, max_clipboard_items].min)..-1]
121
+ end
122
+ return received_text
123
+ end
124
+
125
+ def set_system_clipboard(arg)
126
+ # return if arg.class != String
127
+ # return if s.size < 1
128
+ # utf8_string = Gdk::Atom.intern("UTF8_STRING")
129
+ widget = Gtk::Invisible.new
130
+ clipboard = Gtk::Clipboard.get_default($vmag.window.display)
131
+ clipboard.text = arg
132
+ end
133
+
134
+ def gui_create_buffer(id)
135
+ puts "gui_create_buffer(#{id})"
136
+ buf1 = GtkSource::Buffer.new()
137
+ view = VSourceView.new()
138
+
139
+ view.set_highlight_current_line(true)
140
+ view.set_show_line_numbers(true)
141
+ view.set_buffer(buf1)
142
+
143
+ ssm = GtkSource::StyleSchemeManager.new
144
+ ssm.set_search_path(ssm.search_path << ppath("styles/"))
145
+ # sty = ssm.get_scheme("dark")
146
+ sty = ssm.get_scheme("molokai_edit")
147
+ # puts ssm.scheme_ids
148
+
149
+ view.buffer.highlight_matching_brackets = true
150
+ view.buffer.style_scheme = sty
151
+
152
+ provider = Gtk::CssProvider.new
153
+ provider.load(data: "textview { font-family: Monospace; font-size: 11pt; }")
154
+ # provider.load(data: "textview { font-family: Arial; font-size: 12pt; }")
155
+ view.style_context.add_provider(provider)
156
+ view.wrap_mode = :char
157
+ view.set_tab_width(4) #TODO:add as setting
158
+
159
+ $vmag.buffers[id] = view
160
+ end
161
+
162
+ def gui_set_file_lang(id, lname)
163
+ view = $vmag.buffers[id]
164
+ lm = GtkSource::LanguageManager.new
165
+ lang = nil
166
+ lm.set_search_path(lm.search_path << ppath("lang/"))
167
+ lang = lm.get_language(lname)
168
+
169
+ view.buffer.language = lang
170
+ view.buffer.highlight_syntax = true
171
+ end
172
+
173
+ def gui_add_image(imgpath, pos)
174
+ end
175
+
176
+ # TODO:?
177
+ def gui_select_window_close(arg = nil)
178
+ end
179
+
180
+ # def set_window_title(str)
181
+ # unimplemented
182
+ # end
183
+
184
+ def gui_set_buffer_contents(id, txt)
185
+ # $vbuf.set_text(txt)
186
+ puts "gui_set_buffer_contents(#{id}, txt)"
187
+
188
+ $vmag.buffers[id].buffer.set_text(txt)
189
+ end
190
+
191
+ def gui_set_cursor_pos(id, pos)
192
+ $view.set_cursor_pos(pos)
193
+ # Ripl.start :binding => binding
194
+ end
195
+
196
+ def gui_set_current_buffer(id)
197
+ view = $vmag.buffers[id]
198
+ puts "gui_set_current_buffer(#{id}), view=#{view}"
199
+ buf1 = view.buffer
200
+ $vmag.view = view
201
+ $vmag.buf1 = buf1
202
+ $view = view
203
+ $vbuf = buf1
204
+
205
+ $vmag.sw.remove($vmag.sw.child) if !$vmag.sw.child.nil?
206
+ $vmag.sw.add(view)
207
+
208
+ view.grab_focus
209
+ #view.set_focus(10)
210
+ view.set_cursor_visible(true)
211
+ #view.move_cursor(1, 1, false)
212
+ view.place_cursor_onscreen
213
+
214
+ #TODO:
215
+ # itr = view.buffer.get_iter_at(:offset => 0)
216
+ # view.buffer.place_cursor(itr)
217
+
218
+ # wtitle = ""
219
+ # wtitle = buf.fname if !buf.fname.nil?
220
+ $vmag.sw.show_all
221
+ end
222
+
223
+ def gui_set_window_title(wtitle, subtitle = "")
224
+ $vmag.window.title = wtitle
225
+ $vmag.window.titlebar.subtitle = subtitle
226
+ end
227
+
228
+ class VMAgui
229
+ attr_accessor :buffers, :sw, :view, :buf1, :window
230
+
231
+ VERSION = "1.0"
232
+
233
+ HEART = "♥"
234
+ RADIUS = 150
235
+ N_WORDS = 5
236
+ FONT = "Serif 18"
237
+ TEXT = "I ♥ GTK+"
238
+
239
+ def initialize()
240
+ @show_overlay = true
241
+ @da = nil
242
+ @buffers = {}
243
+ @view = nil
244
+ @buf1 = nil
245
+ end
246
+
247
+ def run
248
+ init_window
249
+ # init_rtext
250
+ Gtk.main
251
+ end
252
+
253
+ def start_overlay_draw()
254
+ @da = Gtk::Fixed.new
255
+ @overlay.add_overlay(@da)
256
+ @overlay.set_overlay_pass_through(@da, true)
257
+ end
258
+
259
+ def clear_overlay()
260
+ if @da != nil
261
+ @overlay.remove(@da)
262
+ end
263
+ end
264
+
265
+ def overlay_draw_text(text, textpos)
266
+ # puts "overlay_draw_text #{[x,y]}"
267
+ (x, y) = @view.pos_to_coord(textpos)
268
+ # puts "overlay_draw_text #{[x,y]}"
269
+ label = Gtk::Label.new("<span background='#00000088' foreground='#ff0000' weight='ultrabold'>#{text}</span>")
270
+ label.use_markup = true
271
+ @da.put(label, x, y)
272
+ end
273
+
274
+ def end_overlay_draw()
275
+ @da.show_all
276
+ end
277
+
278
+ def toggle_overlay
279
+ @show_overlay = @show_overlay ^ 1
280
+ if !@show_overlay
281
+ if @da != nil
282
+ @overlay.remove(@da)
283
+ end
284
+ return
285
+ else
286
+ @da = Gtk::Fixed.new
287
+ @overlay.add_overlay(@da)
288
+ @overlay.set_overlay_pass_through(@da, true)
289
+ end
290
+
291
+ (startpos, endpos) = get_visible_area
292
+ s = @view.buffer.text
293
+ wpos = s.enum_for(:scan, /\W(\w)/).map { Regexp.last_match.begin(0) + 1 }
294
+ wpos = wpos[0..130]
295
+
296
+ # vr = @view.visible_rect
297
+ # # gtk_text_view_get_line_at_y
298
+ # # gtk_text_view_get_iter_at_position
299
+ # gtk_text_view_get_iter_at_position(vr.
300
+ # istart = @view.get_iter_at_position(vr.x,vr.y)
301
+ # istart = @view.get_iter_at_y(vr.y)
302
+ # startpos = @view.get_iter_at_position_raw(vr.x,vr.y)[1].offset
303
+ # endpos = @view.get_iter_at_position_raw(vr.x+vr.width,vr.y+vr.height)[1].offset
304
+ # puts "startpos,endpos:#{[startpos, endpos]}"
305
+
306
+ da = @da
307
+ if false
308
+ da.signal_connect "draw" do |widget, cr|
309
+ cr.save
310
+ for pos in wpos
311
+ (x, y) = @view.pos_to_coord(pos)
312
+
313
+ layout = da.create_pango_layout("XY")
314
+ desc = Pango::FontDescription.new("sans bold 11")
315
+ layout.font_description = desc
316
+
317
+ cr.move_to(x, y)
318
+ # cr.move_to(gutter_width, 300)
319
+ cr.pango_layout_path(layout)
320
+
321
+ cr.set_source_rgb(1.0, 0.0, 0.0)
322
+ cr.fill_preserve
323
+ end
324
+ cr.restore
325
+ false # = draw other
326
+ # true # = Don't draw others
327
+ end
328
+ end
329
+
330
+ for pos in wpos
331
+ (x, y) = @view.pos_to_coord(pos)
332
+ # da.put(Gtk::Label.new("AB"), x, y)
333
+ label = Gtk::Label.new("<span background='#00000088' foreground='#ff0000' weight='ultrabold'>AB</span>")
334
+ label.use_markup = true
335
+ da.put(label, x, y)
336
+ end
337
+
338
+ # puts @view.pos_to_coord(300).inspect
339
+
340
+ @da.show_all
341
+ end
342
+
343
+ def handle_deltas()
344
+ while d = buf.deltas.shift
345
+ pos = d[0]
346
+ op = d[1]
347
+ num = d[2]
348
+ txt = d[3]
349
+ if op == DELETE
350
+ startiter = @buf1.get_iter_at(:offset => pos)
351
+ enditer = @buf1.get_iter_at(:offset => pos + num)
352
+ @buf1.delete(startiter, enditer)
353
+ elsif op == INSERT
354
+ startiter = @buf1.get_iter_at(:offset => pos)
355
+ @buf1.insert(startiter, txt)
356
+ end
357
+ end
358
+ end
359
+
360
+ def add_to_minibuf(msg)
361
+ startiter = @minibuf.buffer.get_iter_at(:offset => 0)
362
+ @minibuf.buffer.insert(startiter, "#{msg}\n")
363
+ @minibuf.signal_emit("move-cursor", Gtk::MovementStep.new(:PAGES), -1, false)
364
+ end
365
+
366
+ def init_minibuffer()
367
+ # Init minibuffer
368
+ sw = Gtk::ScrolledWindow.new
369
+ sw.set_policy(:automatic, :automatic)
370
+ overlay = Gtk::Overlay.new
371
+ overlay.add(sw)
372
+ # @vpaned.pack2(overlay, :resize => false)
373
+ @vbox.attach(overlay, 0, 2, 1, 1)
374
+ # overlay.set_size_request(-1, 50)
375
+ # $ovrl = overlay
376
+ # $ovrl.set_size_request(-1, 30)
377
+ $sw2 = sw
378
+ sw.set_size_request(-1, 12)
379
+
380
+ view = VSourceView.new()
381
+ view.set_highlight_current_line(false)
382
+ view.set_show_line_numbers(false)
383
+ # view.set_buffer(buf1)
384
+ ssm = GtkSource::StyleSchemeManager.new
385
+ ssm.set_search_path(ssm.search_path << ppath("styles/"))
386
+ sty = ssm.get_scheme("molokai_edit")
387
+ view.buffer.highlight_matching_brackets = false
388
+ view.buffer.style_scheme = sty
389
+ provider = Gtk::CssProvider.new
390
+ # provider.load(data: "textview { font-family: Monospace; font-size: 11pt; }")
391
+ provider.load(data: "textview { font-family: Arial; font-size: 10pt; color:#ff0000}")
392
+ view.style_context.add_provider(provider)
393
+ view.wrap_mode = :char
394
+ @minibuf = view
395
+ # Ripl.start :binding => binding
396
+ # startiter = view.buffer.get_iter_at(:offset => 0)
397
+ message("STARTUP")
398
+ sw.add(view)
399
+ end
400
+
401
+ def init_header_bar()
402
+ header = Gtk::HeaderBar.new
403
+ @header = header
404
+ header.show_close_button = true
405
+ header.title = ""
406
+ header.has_subtitle = true
407
+ header.subtitle = ""
408
+ # Ripl.start :binding => binding
409
+
410
+ # icon = Gio::ThemedIcon.new("mail-send-receive-symbolic")
411
+ # icon = Gio::ThemedIcon.new("document-open-symbolic")
412
+ # icon = Gio::ThemedIcon.new("dialog-password")
413
+
414
+ #edit-redo edit-paste edit-find-replace edit-undo edit-find edit-cut edit-copy
415
+ #document-open document-save document-save-as document-properties document-new
416
+ # document-revert-symbolic
417
+ #
418
+
419
+ #TODO:
420
+ # button = Gtk::Button.new
421
+ # icon = Gio::ThemedIcon.new("open-menu-symbolic")
422
+ # image = Gtk::Image.new(:icon => icon, :size => :button)
423
+ # button.add(image)
424
+ # header.pack_end(button)
425
+
426
+ button = Gtk::Button.new
427
+ icon = Gio::ThemedIcon.new("document-open-symbolic")
428
+ image = Gtk::Image.new(:icon => icon, :size => :button)
429
+ button.add(image)
430
+ header.pack_end(button)
431
+
432
+ button.signal_connect "clicked" do |_widget|
433
+ open_file_dialog
434
+ end
435
+
436
+ button = Gtk::Button.new
437
+ icon = Gio::ThemedIcon.new("document-save-symbolic")
438
+ image = Gtk::Image.new(:icon => icon, :size => :button)
439
+ button.add(image)
440
+ header.pack_end(button)
441
+ button.signal_connect "clicked" do |_widget|
442
+ buf.save
443
+ end
444
+
445
+ button = Gtk::Button.new
446
+ icon = Gio::ThemedIcon.new("document-new-symbolic")
447
+ image = Gtk::Image.new(:icon => icon, :size => :button)
448
+ button.add(image)
449
+ header.pack_end(button)
450
+ button.signal_connect "clicked" do |_widget|
451
+ create_new_file
452
+ end
453
+
454
+ box = Gtk::Box.new(:horizontal, 0)
455
+ box.style_context.add_class("linked")
456
+
457
+ button = Gtk::Button.new
458
+ image = Gtk::Image.new(:icon_name => "pan-start-symbolic", :size => :button)
459
+ button.add(image)
460
+ box.add(button)
461
+ button.signal_connect "clicked" do |_widget|
462
+ history_switch_backwards
463
+ end
464
+
465
+ button = Gtk::Button.new
466
+ image = Gtk::Image.new(:icon_name => "pan-end-symbolic", :size => :button)
467
+ button.add(image)
468
+ box.add(button)
469
+ button.signal_connect "clicked" do |_widget|
470
+ history_switch_forwards
471
+ end
472
+
473
+ button = Gtk::Button.new
474
+ icon = Gio::ThemedIcon.new("window-close-symbolic")
475
+ image = Gtk::Image.new(:icon => icon, :size => :button)
476
+ button.add(image)
477
+ box.add(button)
478
+ button.signal_connect "clicked" do |_widget|
479
+ bufs.close_current_buffer
480
+ end
481
+
482
+ header.pack_start(box)
483
+ @window.titlebar = header
484
+ @window.add(Gtk::TextView.new)
485
+ end
486
+
487
+ def create_menu_item(label, depth)
488
+ menuitem = Gtk::MenuItem.new(:label => label)
489
+ menuitem.submenu = create_menu(depth)
490
+ @menubar.append(menuitem)
491
+ end
492
+
493
+ def create_menu(depth)
494
+ return nil if depth < 1
495
+
496
+ menu = Gtk::Menu.new
497
+ last_item = nil
498
+ (0..5).each do |i|
499
+ j = i + 1
500
+ label = "item #{depth} - #{j}"
501
+ menu_item = Gtk::RadioMenuItem.new(nil, label)
502
+ menu_item.join_group(last_item) if last_item
503
+ last_item = menu_item
504
+ menu.append(menu_item)
505
+ menu_item.sensitive = false if i == 3
506
+ menu_item.submenu = create_menu(depth - 1)
507
+ end
508
+
509
+ menu
510
+ end
511
+
512
+ def init_window
513
+ @window = Gtk::Window.new(:toplevel)
514
+ @window.set_default_size(650, 850)
515
+ @window.title = "Multiple Views"
516
+ @window.show_all
517
+ # vpaned = Gtk::Paned.new(:horizontal)
518
+ @vpaned = Gtk::Paned.new(:vertical)
519
+ #@vpaned = Gtk::Box.new(:vertical, 0)
520
+ # @vbox = Gtk::Box.new(:vertical, 0)
521
+ @vbox = Gtk::Grid.new()
522
+ @window.add(@vbox)
523
+
524
+ @menubar = Gtk::MenuBar.new
525
+ @menubar.expand = false
526
+
527
+
528
+ @sw = Gtk::ScrolledWindow.new
529
+ @sw.set_policy(:automatic, :automatic)
530
+ @overlay = Gtk::Overlay.new
531
+ @overlay.add(@sw)
532
+
533
+ # @vpaned.pack1(@overlay, :resize => true)
534
+ # @vpaned.pack2(@menubar, :resize => false)
535
+ # @vbox.add(@menubar, :resize => false)
536
+
537
+ init_header_bar
538
+
539
+ # @window.show_all
540
+
541
+ # @vbox.pack_start(@menubar, :expand => false, :fill => false, :padding => 0 )
542
+ # @vbox.pack_start(@menubar)
543
+ # @vbox.pack_start(@overlay, :expand => true, :fill => true, :padding => 0 )
544
+ # @vbox.pack_start(@overlay, :expand => true, :fill => true, :padding => 0 )
545
+ @vbox.attach(@menubar, 0, 0, 1, 1)
546
+ @vbox.attach(@overlay, 0, 1, 1, 1)
547
+ @overlay.vexpand = true
548
+ @overlay.hexpand = true
549
+
550
+ @menubar.vexpand = false
551
+ @menubar.hexpand = false
552
+
553
+ init_minibuffer
554
+
555
+
556
+ @window.show_all
557
+ vma.start
558
+ Vimamsa::Menu.new(@menubar)
559
+ @window.show_all
560
+
561
+ end
562
+ end