Class: VSourceView
- Inherits:
-
GtkSource::View
- Object
- GtkSource::View
- VSourceView
- Defined in:
- lib/vimamsa/gui_sourceview.rb
Overview
class VSourceView < Gtk::TextView
Instance Attribute Summary collapse
-
#bufo ⇒ Object
Returns the value of attribute bufo.
Instance Method Summary collapse
- #check_controllers ⇒ Object
- #coord_to_iter(xloc, yloc, transform_coord = false) ⇒ Object
- #cursor_visible_idle_func ⇒ Object
-
#delete_cursorchar ⇒ Object
Delete the extra char added to buffer to represent the cursor.
- #draw_cursor ⇒ Object
- #ensure_cursor_visible ⇒ Object
- #gutter_width ⇒ Object
- #handle_deltas ⇒ Object
-
#handle_key_event(keyval, keyname, sig) ⇒ Object
def handle_key_event(event, sig).
- #handle_scrolling ⇒ Object
-
#initialize(title, bufo) ⇒ VSourceView
constructor
def initialize(title = nil,bufo=nil).
- #is_cursor_visible ⇒ Object
- #pos_to_coord(i) ⇒ Object
- #register_signals ⇒ Object
- #sanity_check ⇒ Object
- #set_cursor_pos(pos) ⇒ Object
- #show_controllers ⇒ Object
Constructor Details
#initialize(title, bufo) ⇒ VSourceView
def initialize(title = nil,bufo=nil)
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 |
# File 'lib/vimamsa/gui_sourceview.rb', line 16 def initialize(title, bufo) # super(:toplevel) @highlight_matching_brackets = true @idle_func_running = false super() @bufo = bufo #object of Buffer class buffer.rb debug "vsource init" @last_keyval = nil @last_event = [nil, nil] @removed_controllers = [] self.highlight_current_line = true @tt = nil # self.drag_dest_add_image_targets #TODO:gtk4 # self.drag_dest_add_uri_targets #TODO:gtk4 # signal_connect("drag-data-received") do |widget, event, x, y, data, info, time| #TODO:gtk4 # puts "drag-data-received" # puts # if data.uris.size >= 1 # imgpath = CGI.unescape(data.uris[0]) # m = imgpath.match(/^file:\/\/(.*)/) # if m # fp = m[1] # handle_drag_and_drop(fp) # end # end # true # end # Mainly after page-up or page-down signal_connect("move-cursor") do |, event| if event.name == "GTK_MOVEMENT_PAGES" and (last_action == "page_up" or last_action == "page_down") # Ripl.start :binding => binding debug("MOVE-CURSOR", 2) # $update_cursor = true handle_scrolling() end # handle_scrolling() # curpos = buffer.cursor_position # debug "MOVE CURSOR (sig): #{curpos}" # run_as_idle proc { # curpos = buffer.cursor_position # debug "MOVE CURSOR (sig2): #{curpos}" # } false end return #TODO:gtk4 signal_connect "button-release-event" do |, event| vma.buf.set_pos(buffer.cursor_position) false end @curpos_mark = nil end |
Instance Attribute Details
#bufo ⇒ Object
Returns the value of attribute bufo.
3 4 5 |
# File 'lib/vimamsa/gui_sourceview.rb', line 3 def bufo @bufo end |
Instance Method Details
#check_controllers ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/vimamsa/gui_sourceview.rb', line 94 def check_controllers clist = self.observe_controllers to_remove = [] (0..(clist.n_items - 1)).each { |x| ctr = clist.get_item(x) # Sometimes a GestureClick EventController appears from somewhere # not initiated from this file. # if ctr.class == Gtk::EventControllerKey or ctr.class == Gtk::GestureClick if ctr != @click # to_remove << ctr if ctr.class != Gtk::GestureDrag to_remove << ctr end } if to_remove.size > 0 debug "Removing controllers:" pp to_remove to_remove.each { |x| # To avoid GC. https://github.com/ruby-gnome/ruby-gnome/issues/15790 @removed_controllers << x self.remove_controller(x) } end end |
#coord_to_iter(xloc, yloc, transform_coord = false) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/vimamsa/gui_sourceview.rb', line 210 def coord_to_iter(xloc, yloc, transform_coord = false) if transform_coord xloc = (xloc - gutter_width).to_i yloc = (yloc + visible_rect.y).to_i end # Try to get exact character position i = get_iter_at_location(xloc, yloc) # If doesn't work, at least get the start of correct line # TODO: sometimes end of line is better choice if i.nil? r = get_line_at_y(yloc) i = r[0] if !r.nil? end return i.offset if !i.nil? return nil end |
#cursor_visible_idle_func ⇒ Object
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/vimamsa/gui_sourceview.rb', line 435 def cursor_visible_idle_func debug "cursor_visible_idle_func" # From https://picheta.me/articles/2013/08/gtk-plus--a-method-to-guarantee-scrolling.html # vr = visible_rect # b = $view.buffer # iter = buffer.get_iter_at(:offset => buffer.cursor_position) # iterxy = get_iter_location(iter) # This is not the current buffer return false if vma.gui.view != self sleep(0.01) # intr = iterxy.intersect(vr) if is_cursor_visible == false # set_cursor_pos(buffer.cursor_position) itr = buffer.get_iter_at(:offset => buffer.cursor_position) within_margin = 0.075 #margin as a [0.0,0.5) fraction of screen size use_align = false xalign = 0.5 #0.0=top 1.0=bottom, 0.5=center yalign = 0.5 scroll_to_iter(itr, within_margin, use_align, xalign, yalign) return true # Call this func again else @idle_func_running = false return false # Don't call this idle func again end end |
#delete_cursorchar ⇒ Object
Delete the extra char added to buffer to represent the cursor
500 501 502 503 504 505 506 507 |
# File 'lib/vimamsa/gui_sourceview.rb', line 500 def delete_cursorchar if !@cursorchar.nil? itr = buffer.get_iter_at(:offset => @cursorchar) itr2 = buffer.get_iter_at(:offset => @cursorchar + 1) buffer.delete(itr, itr2) @cursorchar = nil end end |
#draw_cursor ⇒ Object
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/vimamsa/gui_sourceview.rb', line 509 def draw_cursor # if @tt.nil? # @tt = buffer.create_tag("font_tag") # @tt.font = "Arial" # end mode = vma.kbd.get_mode ctype = vma.kbd.get_cursor_type delete_cursorchar vma.gui. if ctype == :command if @bufo[@bufo.pos] == "\n" # If we are at end of line, it's not possible to draw the cursor by making a selection. I tried to do this by drawing an overlay, but that generates issues. If moving the cursor causes the ScrolledWindow to be scrolled, these errors randomly appear and the whole view shows blank: # (ruby:21016): Gtk-WARNING **: 19:52:23.181: Trying to snapshot GtkSourceView 0x55a97524c8c0 without a current allocation # (ruby:21016): Gtk-WARNING **: 19:52:23.181: Trying to snapshot GtkGizmo 0x55a9727d2580 without a current allocation # (ruby:21016): Gtk-WARNING **: 19:52:23.243: Trying to snapshot GtkSourceView 0x55a97524c8c0 without a current allocation # vma.gui.overlay_draw_cursor(@bufo.pos) # Current workaround is to add an empty space to the place where the cursor is and then remove this whenever we get any kind of event that might cause this class to be accessed. itr = buffer.get_iter_at(:offset => @bufo.pos) buffer.insert(itr, " ") # normal space # buffer.insert(itr, " ") # thin space (U+2009) # buffer.insert(itr, "l") @cursorchar = @bufo.pos # Apparently we need to redo this after buffer.insert: itr = buffer.get_iter_at(:offset => @bufo.pos) itr2 = buffer.get_iter_at(:offset => @bufo.pos + 1) # buffer.apply_tag(@tt, itr, itr2) buffer.select_range(itr, itr2) else itr = buffer.get_iter_at(:offset => @bufo.pos) itr2 = buffer.get_iter_at(:offset => @bufo.pos + 1) buffer.select_range(itr, itr2) end # elsif @bufo.visual_mode? elsif ctype == :visual # debug "VISUAL MODE" (_start, _end) = @bufo.get_visual_mode_range2 # debug "#{_start}, #{_end}" itr = buffer.get_iter_at(:offset => _start) itr2 = buffer.get_iter_at(:offset => _end + 1) # Pango-CRITICAL **: pango_layout_get_cursor_pos: assertion 'index >= 0 && index <= layout->length' failed buffer.select_range(itr, itr2) elsif ctype == :insert # Not sure why this is needed itr = buffer.get_iter_at(:offset => @bufo.pos) buffer.select_range(itr, itr) # Via trial and error, this combination is only thing that seems to work: vma.gui.sw.child.toggle_cursor_visible vma.gui.sw.child.cursor_visible = true debug "INSERT MODE" else # TODO end end |
#ensure_cursor_visible ⇒ Object
485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/vimamsa/gui_sourceview.rb', line 485 def ensure_cursor_visible return #TODO:gtk4 debug "@idle_func_running=#{@idle_func_running}" return if @idle_func_running if is_cursor_visible == false @idle_func_running = true debug "Starting idle func" Thread.new { sleep 0.01 GLib::Idle.add(proc { cursor_visible_idle_func }) } end end |
#gutter_width ⇒ Object
80 81 82 83 84 |
# File 'lib/vimamsa/gui_sourceview.rb', line 80 def gutter_width() winwidth = width view_width = visible_rect.width gutter_width = winwidth - view_width end |
#handle_deltas ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/vimamsa/gui_sourceview.rb', line 368 def handle_deltas() delete_cursorchar any_change = false while d = @bufo.deltas.shift any_change = true pos = d[0] op = d[1] num = d[2] txt = d[3] if op == DELETE startiter = buffer.get_iter_at(:offset => pos) enditer = buffer.get_iter_at(:offset => pos + num) buffer.delete(startiter, enditer) elsif op == INSERT startiter = buffer.get_iter_at(:offset => pos) buffer.insert(startiter, txt) end end if any_change #TODO: only when necessary self.set_cursor_pos(pos) end # sanity_check #TODO end |
#handle_key_event(keyval, keyname, sig) ⇒ Object
def handle_key_event(event, sig)
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/vimamsa/gui_sourceview.rb', line 251 def handle_key_event(keyval, keyname, sig) delete_cursorchar if $update_cursor handle_scrolling end debug $view.visible_rect.inspect debug "key event" # debug event # key_name = event.string #TODO:?? # if event.state.control_mask? # key_name = Gdk::Keyval.to_name(event.keyval) # Gdk::Keyval.to_name() # end keyval_trans = {} keyval_trans[Gdk::Keyval::KEY_Control_L] = "ctrl" keyval_trans[Gdk::Keyval::KEY_Control_R] = "ctrl" keyval_trans[Gdk::Keyval::KEY_Escape] = "esc" keyval_trans[Gdk::Keyval::KEY_Return] = "enter" keyval_trans[Gdk::Keyval::KEY_ISO_Enter] = "enter" keyval_trans[Gdk::Keyval::KEY_KP_Enter] = "enter" keyval_trans[Gdk::Keyval::KEY_Alt_L] = "alt" keyval_trans[Gdk::Keyval::KEY_Alt_R] = "alt" keyval_trans[Gdk::Keyval::KEY_BackSpace] = "backspace" keyval_trans[Gdk::Keyval::KEY_KP_Page_Down] = "pagedown" keyval_trans[Gdk::Keyval::KEY_KP_Page_Up] = "pageup" keyval_trans[Gdk::Keyval::KEY_Page_Down] = "pagedown" keyval_trans[Gdk::Keyval::KEY_Page_Up] = "pageup" keyval_trans[Gdk::Keyval::KEY_Left] = "left" keyval_trans[Gdk::Keyval::KEY_Right] = "right" keyval_trans[Gdk::Keyval::KEY_Down] = "down" keyval_trans[Gdk::Keyval::KEY_Up] = "up" keyval_trans[Gdk::Keyval::KEY_space] = "space" keyval_trans[Gdk::Keyval::KEY_Shift_L] = "shift" keyval_trans[Gdk::Keyval::KEY_Shift_R] = "shift" keyval_trans[Gdk::Keyval::KEY_Tab] = "tab" key_trans = {} key_trans["\e"] = "esc" tk = keyval_trans[keyval] keyname = tk if !tk.nil? key_str_parts = [] key_str_parts << "ctrl" if vma.kbd.modifiers[:ctrl] key_str_parts << "alt" if vma.kbd.modifiers[:alt] key_str_parts << "shift" if vma.kbd.modifiers[:shift] key_str_parts << "meta" if vma.kbd.modifiers[:meta] key_str_parts << "super" if vma.kbd.modifiers[:super] key_str_parts << keyname if key_str_parts[0] == key_str_parts[1] # We don't want "ctrl-ctrl" or "alt-alt" # TODO:There should be a better way to do this key_str_parts.delete_at(0) end if key_str_parts[0] == "shift" and key_str_parts[1].class == String #"shift-P" to just "P" # key_str_parts.delete_at(0) if key_str_parts[1].match(/^[[:upper:]]$/) key_str_parts.delete_at(0) end key_str = key_str_parts.join("-") if key_str == "\u0000" key_str = "" end keynfo = { :key_str => key_str, :key_name => keyname, :keyval => keyval } debug keynfo.inspect # $kbd.match_key_conf(key_str, nil, :key_press) # debug "key_str=#{key_str} key_" if key_str != "" # or prefixed_key_str != "" if sig == :key_release and keyval == @last_keyval $kbd.match_key_conf(key_str + "!", nil, :key_release) @last_event = [keynfo, :key_release] elsif sig == :key_press $kbd.match_key_conf(key_str, nil, :key_press) @last_event = [keynfo, key_str, :key_press] end @last_keyval = keyval #TODO: outside if? end handle_deltas # set_focus(5) # false draw_cursor #TODO: only when needed end |
#handle_scrolling ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/vimamsa/gui_sourceview.rb', line 230 def handle_scrolling() delete_cursorchar # curpos = buffer.cursor_position # debug "MOVE CURSOR: #{curpos}" return nil if vma.gui.nil? return nil if @bufo.nil? vma.gui.run_after_scrolling proc { debug "START UPDATE POS AFTER SCROLLING", 2 delete_cursorchar bc = window_to_buffer_coords(Gtk::TextWindowType::WIDGET, gutter_width + 2, 60) if !bc.nil? i = coord_to_iter(bc[0], bc[1]) if !i.nil? @bufo.set_pos(i) end end $update_cursor = false } end |
#is_cursor_visible ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/vimamsa/gui_sourceview.rb', line 468 def is_cursor_visible vr = visible_rect iter = buffer.get_iter_at(:offset => buffer.cursor_position) iterxy = get_iter_location(iter) iterxy.width = 1 if iterxy.width == 0 iterxy.height = 1 if iterxy.height == 0 intr = iterxy.intersect(vr) if intr.nil? debug iterxy.inspect debug vr.inspect return false else return true end end |
#pos_to_coord(i) ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/vimamsa/gui_sourceview.rb', line 349 def pos_to_coord(i) b = buffer iter = b.get_iter_at(:offset => i) iterxy = get_iter_location(iter) winw = width #TODO view_width = visible_rect.width gutter_width = winw - view_width #TODO x = iterxy.x + gutter_width y = iterxy.y # buffer_to_window_coords(Gtk::TextWindowType::TEXT, iterxy.x, iterxy.y).inspect # debug buffer_to_window_coords(Gtk::TextWindowType::TEXT, x, y).inspect (x, y) = buffer_to_window_coords(Gtk::TextWindowType::TEXT, x, y) return [x, y] end |
#register_signals ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/vimamsa/gui_sourceview.rb', line 119 def register_signals() check_controllers # Implement mouse selections using @cnt_mo and @cnt_drag @cnt_mo = Gtk::EventControllerMotion.new self.add_controller(@cnt_mo) @cnt_mo.signal_connect "motion" do |gesture, x, y| if !@range_start.nil? and !x.nil? and !y.nil? and buf.visual_mode? i = coord_to_iter(x, y, true) @bufo.set_pos(i) if !i.nil? and @last_iter != i @last_iter = i end end @last_coord = nil @cnt_drag = Gtk::GestureDrag.new self.add_controller(@cnt_drag) @cnt_drag.signal_connect "drag-begin" do |gesture, x, y| debug "drag-begin", 2 i = coord_to_iter(x, y, true) pp i @range_start = i if !buf.visual_mode? buf.start_visual_mode end end @cnt_drag.signal_connect "drag-end" do |gesture, offsetx, offsety| debug "drag-end", 2 # Not enough drag if offsetx.abs < 5 and offsety.abs < 5 buf.end_visual_mode end @range_start = nil end click = Gtk::GestureClick.new click.set_propagation_phase(Gtk::PropagationPhase::CAPTURE) self.add_controller(click) # Detect mouse click @click = click @range_start = nil click.signal_connect "pressed" do |gesture, n_press, x, y, z| debug "SourceView, GestureClick released x=#{x} y=#{y}" if buf.visual_mode? buf.end_visual_mode end xloc = (x - gutter_width).to_i yloc = (y + visible_rect.y).to_i debug "xloc=#{xloc} yloc=#{yloc}" i = coord_to_iter(xloc, yloc) # @range_start = i # This needs to happen after xloc calculation, otherwise xloc gets a wrong value (around 200 bigger) if vma.gui.current_view != self vma.gui.set_current_view(self) end @bufo.set_pos(i) if !i.nil? true end click.signal_connect "released" do |gesture, n_press, x, y, z| debug "SourceView, GestureClick released x=#{x} y=#{y}" xloc = (x - gutter_width).to_i yloc = (y + visible_rect.y).to_i debug "xloc=#{xloc} yloc=#{yloc}" # This needs to happen after xloc calculation, otherwise xloc gets a wrong value (around 200 bigger) if vma.gui.current_view != self vma.gui.set_current_view(self) end i = coord_to_iter(xloc, yloc) # if i != @range_start # debug "RANGE #{[@range_start, i]}", 2 # end @bufo.set_pos(i) if !i.nil? # @range_start = nil true end end |
#sanity_check ⇒ Object
394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/vimamsa/gui_sourceview.rb', line 394 def sanity_check() a = buffer.text b = buf.to_s # debug "====================" # debug a.lines[0..10].join() # debug "====================" # debug b.lines[0..10].join() # debug "====================" if a == b debug "Buffers match" else debug "ERROR: Buffer's don't match." end end |
#set_cursor_pos(pos) ⇒ Object
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/vimamsa/gui_sourceview.rb', line 409 def set_cursor_pos(pos) delete_cursorchar # return itr = buffer.get_iter_at(:offset => pos) itr2 = buffer.get_iter_at(:offset => pos + 1) buffer.place_cursor(itr) within_margin = 0.075 #margin as a [0.0,0.5) fraction of screen size use_align = false xalign = 0.5 #0.0=top 1.0=bottom, 0.5=center yalign = 0.5 if @curpos_mark.nil? @curpos_mark = buffer.create_mark("cursor", itr, false) else buffer.move_mark(@curpos_mark, itr) end scroll_to_mark(@curpos_mark, within_margin, use_align, xalign, yalign) $idle_scroll_to_mark = true ensure_cursor_visible draw_cursor return true end |
#show_controllers ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/vimamsa/gui_sourceview.rb', line 86 def show_controllers clist = self.observe_controllers (0..(clist.n_items - 1)).each { |x| ctr = clist.get_item(x) pp ctr } end |