vimamsa 0.1.11 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
data/lib/vimamsa/gui.rb CHANGED
@@ -90,33 +90,6 @@ def page_down
90
90
  return true
91
91
  end
92
92
 
93
- def paste_system_clipboard()
94
-
95
- #TODO: Check if something useful in this old GTK3 code.
96
- utf8_string = Gdk::Atom.intern("UTF8_STRING")
97
-
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
- if clipboard.wait_is_text_available?
105
- received_text = clipboard.wait_for_text
106
- end
107
-
108
- if received_text != "" and !received_text.nil?
109
- max_clipboard_items = 100
110
- if received_text != $clipboard[-1]
111
- #TODO: HACK
112
- $paste_lines = false
113
- end
114
- $clipboard << received_text
115
- # debug $clipboard[-1]
116
- $clipboard = $clipboard[-([$clipboard.size, max_clipboard_items].min)..-1]
117
- end
118
- return received_text
119
- end
120
93
 
121
94
  def set_system_clipboard(arg)
122
95
  vma.gui.window.display.clipboard.set(arg)
@@ -178,38 +151,6 @@ def gui_set_cursor_pos(id, pos)
178
151
  vma.buf.view.set_cursor_pos(pos)
179
152
  end
180
153
 
181
- def gui_set_active_window(winid)
182
- sw = vma.gui.sw
183
- if winid == 2
184
- sw = vma.gui.sw2
185
- end
186
-
187
- sw.set_child(view)
188
- view.grab_focus
189
-
190
- vma.gui.view = view
191
- vma.gui.buf1 = view.buffer
192
- $view = view
193
- $vbuf = view.buffer
194
- end
195
-
196
- #TODO:delete?
197
- def gui_attach_buf_to_window(id, winid)
198
- view = vma.gui.buffers[id]
199
- sw = vma.gui.sw
200
- if winid == 2
201
- sw = vma.gui.sw2
202
- end
203
-
204
- sw.set_child(view)
205
- view.grab_focus
206
-
207
- vma.gui.view = view
208
- vma.gui.buf1 = view.buffer
209
- $view = view
210
- $vbuf = view.buffer
211
- end
212
-
213
154
  def gui_set_current_buffer(id)
214
155
  vma.gui.set_current_buffer(id)
215
156
  return
@@ -328,72 +269,63 @@ class VMAgui
328
269
  @da.show
329
270
  end
330
271
 
331
- def toggle_overlay
332
- @show_overlay = @show_overlay ^ 1
333
- if !@show_overlay
334
- if @da != nil
335
- @overlay.remove(@da)
336
- end
337
- return
338
- else
339
- @da = Gtk::Fixed.new
340
- @overlay.add_overlay(@da)
341
- @overlay.set_overlay_pass_through(@da, true)
272
+ def remove_overlay_cursor()
273
+ if !@cursorov.nil?
274
+ @overlay.remove_overlay(@cursorov)
275
+ @cursorov = nil
342
276
  end
277
+ end
343
278
 
344
- (startpos, endpos) = get_visible_area
345
- s = @view.buffer.text
346
- wpos = s.enum_for(:scan, /\W(\w)/).map { Regexp.last_match.begin(0) + 1 }
347
- wpos = wpos[0..130]
348
-
349
- # vr = @view.visible_rect
350
- # # gtk_text_view_get_line_at_y
351
- # # gtk_text_view_get_iter_at_position
352
- # gtk_text_view_get_iter_at_position(vr.
353
- # istart = @view.get_iter_at_position(vr.x,vr.y)
354
- # istart = @view.get_iter_at_y(vr.y)
355
- # startpos = @view.get_iter_at_position_raw(vr.x,vr.y)[1].offset
356
- # endpos = @view.get_iter_at_position_raw(vr.x+vr.width,vr.y+vr.height)[1].offset
357
- # debug "startpos,endpos:#{[startpos, endpos]}"
358
-
359
- da = @da
360
- if false
361
- da.signal_connect "draw" do |widget, cr|
362
- cr.save
363
- for pos in wpos
364
- (x, y) = @view.pos_to_coord(pos)
365
-
366
- layout = da.create_pango_layout("XY")
367
- desc = Pango::FontDescription.new("sans bold 11")
368
- layout.font_description = desc
369
-
370
- cr.move_to(x, y)
371
- # cr.move_to(gutter_width, 300)
372
- cr.pango_layout_path(layout)
373
-
374
- cr.set_source_rgb(1.0, 0.0, 0.0)
375
- cr.fill_preserve
376
- end
377
- cr.restore
378
- false # = draw other
379
- # true # = Don't draw others
279
+ def overlay_draw_cursor(textpos)
280
+ # return
281
+ remove_overlay_cursor
282
+ GLib::Idle.add(proc { self.overlay_draw_cursor_(textpos) })
283
+ # overlay_draw_cursor_(textpos)
284
+ end
285
+
286
+ # Run proc after animated scrolling has stopped (e.g. after page down)
287
+ def run_after_scrolling(p)
288
+ Thread.new {
289
+ # After running
290
+ # view.signal_emit("move-cursor", Gtk::MovementStep.new(:PAGES)
291
+ # have to wait for animated page down scrolling to actually start
292
+ # Then have to wait determine that it has stopped if scrolling adjustment stops changing. There should be a better way to do this.
293
+ sleep 0.1
294
+ while Time.now - @last_adj_time < 0.1
295
+ sleep 0.1
380
296
  end
381
- end
297
+ debug "SCROLLING ENDED", 2
298
+ run_as_idle p
299
+ }
300
+ end
382
301
 
383
- for pos in wpos
384
- (x, y) = @view.pos_to_coord(pos)
385
- # da.put(Gtk::Label.new("AB"), x, y)
386
- label = Gtk::Label.new("<span background='#00000088' foreground='#ff0000' weight='ultrabold'>AB</span>")
387
- label.use_markup = true
388
- da.put(label, x, y)
389
- end
302
+ # To draw on empty lines and line-ends (where select_range doesn't work)
303
+ def overlay_draw_cursor_(textpos)
304
+ # Thread.new {
305
+ # GLib::Idle.add(proc { p.call; false })
306
+ # }
307
+
308
+ # while Time.now - @last_adj_time < 0.3
309
+ # return true
310
+ # end
390
311
 
391
- # debug @view.pos_to_coord(300).inspect
312
+ remove_overlay_cursor
313
+ @cursorov = Gtk::Fixed.new
314
+ @overlay.add_overlay(@cursorov)
392
315
 
393
- @da.show
316
+ (x, y) = @view.pos_to_coord(textpos)
317
+ pp [x, y]
318
+
319
+ # Trying to draw only background of character "I"
320
+ label = Gtk::Label.new("<span background='#00ffaaff' foreground='#00ffaaff' weight='ultrabold'>I</span>")
321
+ label.use_markup = true
322
+ @cursorov.put(label, x, y)
323
+ @cursorov.show
324
+ return false
394
325
  end
395
326
 
396
327
  def handle_deltas()
328
+ view.delete_cursor_char
397
329
  while d = buf.deltas.shift
398
330
  pos = d[0]
399
331
  op = d[1]
@@ -551,6 +483,7 @@ class VMAgui
551
483
  return true
552
484
  end
553
485
 
486
+ # Remove widget event controllers added by gtk, and add our own.
554
487
  def reset_controllers
555
488
  clist = @window.observe_controllers
556
489
  to_remove = []
@@ -629,23 +562,32 @@ class VMAgui
629
562
  end
630
563
  end
631
564
 
565
+ def idle_set_size()
566
+ # Need to wait for a while to window to be maximized to get correct @window.width
567
+ sleep 0.1
568
+ width = @window.width / 2
569
+ height = @window.height - 5
570
+ # Ripl.start :binding => binding
571
+ @window.unmaximize
572
+ @window.set_size_request(width, height)
573
+ return false
574
+ end
575
+
632
576
  def init_window
633
577
  @last_debug_idle = Time.now
634
578
  app = Gtk::Application.new("net.samiddhi.vimamsa.r#{rand(1000)}", :flags_none)
635
579
  @app = app
636
580
 
637
- app.signal_connect "activate" do
581
+ Gtk::Settings.default.gtk_application_prefer_dark_theme = true
582
+ Gtk::Settings.default.gtk_theme_name = "Adwaita"
638
583
 
639
- # @window = Gtk::Window.new(:toplevel)
640
- # @window = Gtk::Window.new()
584
+ app.signal_connect "activate" do
641
585
  @window = Gtk::ApplicationWindow.new(app)
642
586
  @window.set_application(app)
643
587
 
644
- # sh = @window.screen.height #TODO:gtk4
645
- # sw = @window.screen.width #TODO:gtk4
646
- # TODO:Maximise vertically
647
- # @window.set_default_size((sw * 0.45).to_i, sh - 20) #TODO:gtk4
648
- @window.set_default_size(800, 600)
588
+ @window.maximize
589
+ # Need to let Gtk process after maximize
590
+ run_as_idle proc { idle_set_size }
649
591
 
650
592
  @window.title = "Multiple Views"
651
593
  @vpaned = Gtk::Paned.new(:vertical)
@@ -659,15 +601,22 @@ class VMAgui
659
601
 
660
602
  reset_controllers
661
603
 
662
- # @window.signal_connect("key-pressed") { puts "Hello World!" }
663
- # @window.signal_connect("clicked") { puts "Hello World!" }
664
-
665
- # @menubar = Gtk::PopoverMenuBar.new #TODO:gtk4
666
- # @menubar.expand = false #TODO:gtk4
667
-
668
604
  @sw = Gtk::ScrolledWindow.new
669
605
  @sw.set_policy(:automatic, :automatic)
670
606
 
607
+
608
+ @last_adj_time = Time.now
609
+ @sw.vadjustment.signal_connect("value-changed") { |x|
610
+ # pp x.page_increment
611
+ # pp x.page_size
612
+ # pp x.step_increment
613
+ # pp x.upper
614
+ # pp x.value
615
+ # pp x
616
+ @last_adj_time = Time.now
617
+ # puts "@sw.vadjustment"
618
+ }
619
+
671
620
  # @sw.signal_connect("clicked") { puts "Hello World!" }
672
621
  # @sw.signal_connect("key-pressed") { puts "Hello World!" }
673
622
  @overlay = Gtk::Overlay.new
@@ -813,10 +762,10 @@ class VMAgui
813
762
  @vbox.remove(@overlay)
814
763
 
815
764
  # numbers: left, top, width, height
816
- @pane.set_start_child(@overlay)
817
- @pane.set_end_child(@overlay2)
765
+ @pane.set_start_child(@overlay2)
766
+ @pane.set_end_child(@overlay)
818
767
 
819
- @vbox.attach(@pane, 0, 1, 2, 1)
768
+ @vbox.attach(@pane, 0, 2, 2, 1)
820
769
 
821
770
  @sw2.vexpand = true
822
771
  @sw2.hexpand = true
@@ -831,7 +780,7 @@ class VMAgui
831
780
  last = vma.buffers.get_last_visited_id
832
781
  set_buffer_to_window(last, 2)
833
782
  else
834
- bf = create_new_buffer "\n\n"
783
+ bf = create_new_buffer "\n\n", "buff", false
835
784
  set_buffer_to_window(bf.id, 2)
836
785
  end
837
786
  end
@@ -848,8 +797,14 @@ class VMAgui
848
797
  else
849
798
  set_active_window(1)
850
799
  end
800
+ end
851
801
 
852
- vma.buffers.set_current_buffer_by_id(@sw.child.bufo.id)
802
+ # activate that window which has the given view
803
+ def set_current_view(view)
804
+ w = @windows.find { |k, v| v[:sw].child == view }
805
+ if !w.nil?
806
+ set_active_window(w[0])
807
+ end
853
808
  end
854
809
 
855
810
  def set_active_window(id)
@@ -868,16 +823,26 @@ class VMAgui
868
823
  @sw = @windows[id][:sw]
869
824
  @overlay = @windows[id][:overlay]
870
825
 
826
+ vma.buffers.set_current_buffer_by_id(@sw.child.bufo.id)
827
+
871
828
  #TODO: set buf & view of active window??
872
829
 
873
830
  end
874
831
 
832
+ def current_view
833
+ return @sw.child
834
+ end
835
+
875
836
  def set_buffer_to_window(bufid, winid)
876
837
  view = @buffers[bufid]
877
838
  debug "vma.gui.set_buffer_to_window(#{bufid}), winid=#{winid}"
878
839
  buf1 = view.buffer
879
840
 
880
841
  @windows[winid][:sw].set_child(view)
842
+ idle_ensure_cursor_drawn
843
+
844
+ # @overlay = Gtk::Overlay.new
845
+ # @overlay.add_overlay(view)
881
846
 
882
847
  #TODO:???
883
848
  # @view = view
@@ -896,19 +861,26 @@ class VMAgui
896
861
  $view = view
897
862
  $vbuf = buf1
898
863
 
899
- # If already open in another column
900
-
864
+ # Check if buffer is already open in another column
901
865
  if @two_column and @active_column == 2 and id == @sw1.child.bufo.id
902
866
  toggle_active_window
903
- elsif @two_column and @active_column == 1 and id == @sw2.child.bufo.id
867
+ elsif @two_column && @active_column == 1 && !@sw2.child.nil? && id == @sw2.child.bufo.id
868
+ #TODO: should not need !@sw2.child.nil? here. If this happens then other column is empty.
904
869
  toggle_active_window
905
870
  else
906
871
  @sw.set_child(view)
907
872
  end
908
873
  view.grab_focus
909
- # TODO:needed?
910
- # view.set_cursor_visible(true)
911
- # view.place_cursor_onscreen
912
- # @sw.show
874
+
875
+ idle_ensure_cursor_drawn
876
+ end
877
+
878
+ def idle_ensure_cursor_drawn
879
+ run_as_idle proc { self.ensure_cursor_drawn }
880
+ end
881
+
882
+ def ensure_cursor_drawn
883
+ # view.place_cursor_onscreen #TODO: needed?
884
+ view.draw_cursor
913
885
  end
914
886
  end
@@ -43,6 +43,10 @@ module Vimamsa
43
43
  add_to_menu "Actions.experimental.EnableDebug", { :label => "Enable debug", :action => :enable_debug }
44
44
  add_to_menu "Actions.experimental.DisableDebug", { :label => "Disable debug", :action => :disable_debug }
45
45
  add_to_menu "Actions.experimental.ShowImages", { :label => "Show images ⟦img:path⟧", :action => :show_images }
46
+
47
+
48
+ add_to_menu "View.TwoColumn", { :label => "Start two column mode", :action => :toggle_two_column }
49
+
46
50
 
47
51
  add_to_menu "Actions.EncryptFile", { :label => "Encrypt file", :action => :encrypt_file }
48
52
  add_to_menu "Help.KeyBindings", { :label => "Show key bindings", :action => :show_key_bindings }
@@ -69,13 +69,19 @@ class SelectUpdateWindow
69
69
 
70
70
  @window.add(vbox)
71
71
 
72
+ if !opt[:desc].nil?
73
+ descl = Gtk::Label.new(opt[:desc])
74
+ set_margin_all(descl,15)
75
+ # vbox.append(descl, :expand => false, :fill => false, :padding => 0)
76
+ vbox.append(descl)
77
+ end
78
+
72
79
  @entry = Gtk::SearchEntry.new
73
80
  @entry.width_chars = 45
74
81
  container = Gtk::Box.new(:horizontal, 10)
75
82
  # container.halign = :start
76
83
  container.halign = :center
77
- container.pack_start(@entry,
78
- :expand => false, :fill => false, :padding => 0)
84
+ container.append(@entry)
79
85
 
80
86
  # create tree view
81
87
  @model = Gtk::ListStore.new(String, String)
@@ -135,11 +141,6 @@ class SelectUpdateWindow
135
141
  @entry.signal_connect("changed") { debug "[changed] " }
136
142
  @entry.signal_connect("next-match") { debug "[next-match] " }
137
143
 
138
- if !opt[:desc].nil?
139
- descl = Gtk::Label.new(opt[:desc])
140
- vbox.pack_start(descl, :expand => false, :fill => false, :padding => 0)
141
- end
142
-
143
144
  # label = Gtk::Label.new(<<-EOF)
144
145
  # Search:
145
146
  # EOF
@@ -147,7 +148,7 @@ class SelectUpdateWindow
147
148
  # label = Gtk::Label.new("Input:")
148
149
  # vbox.pack_start(label, :expand => false, :fill => false, :padding => 0)
149
150
 
150
- vbox.pack_start(container, :expand => false, :fill => false, :padding => 0)
151
+ vbox.append(container)
151
152
  sw = Gtk::ScrolledWindow.new(nil, nil)
152
153
  # sw.shadow_type = :etched_in #TODO:gtk4
153
154
  sw.set_policy(:never, :automatic)