Class: VMAgui

Inherits:
Object
  • Object
show all
Defined in:
lib/vimamsa/gui.rb

Constant Summary collapse

VERSION =
"1.0"
HEART =
"♥"
RADIUS =
150
N_WORDS =
5
FONT =
"Serif 18"
TEXT =
"I ♥ GTK+"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVMAgui

Returns a new instance of VMAgui.



238
239
240
241
242
243
244
# File 'lib/vimamsa/gui.rb', line 238

def initialize()
  @show_overlay = true
  @da = nil
  @buffers = {}
  @view = nil
  @buf1 = nil
end

Instance Attribute Details

#buf1Object

Returns the value of attribute buf1.



228
229
230
# File 'lib/vimamsa/gui.rb', line 228

def buf1
  @buf1
end

#buffersObject

Returns the value of attribute buffers.



228
229
230
# File 'lib/vimamsa/gui.rb', line 228

def buffers
  @buffers
end

#swObject

Returns the value of attribute sw.



228
229
230
# File 'lib/vimamsa/gui.rb', line 228

def sw
  @sw
end

#viewObject

Returns the value of attribute view.



228
229
230
# File 'lib/vimamsa/gui.rb', line 228

def view
  @view
end

#windowObject

Returns the value of attribute window.



228
229
230
# File 'lib/vimamsa/gui.rb', line 228

def window
  @window
end

Instance Method Details

#add_to_minibuf(msg) ⇒ Object



359
360
361
362
363
# File 'lib/vimamsa/gui.rb', line 359

def add_to_minibuf(msg)
  startiter = @minibuf.buffer.get_iter_at(:offset => 0)
  @minibuf.buffer.insert(startiter, "#{msg}\n")
  @minibuf.signal_emit("move-cursor", Gtk::MovementStep.new(:PAGES), -1, false)
end

#clear_overlayObject



258
259
260
261
262
# File 'lib/vimamsa/gui.rb', line 258

def clear_overlay()
  if @da != nil
    @overlay.remove(@da)
  end
end

#create_menu(depth) ⇒ Object



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/vimamsa/gui.rb', line 492

def create_menu(depth)
  return nil if depth < 1

  menu = Gtk::Menu.new
  last_item = nil
  (0..5).each do |i|
    j = i + 1
    label = "item #{depth} - #{j}"
    menu_item = Gtk::RadioMenuItem.new(nil, label)
    menu_item.join_group(last_item) if last_item
    last_item = menu_item
    menu.append(menu_item)
    menu_item.sensitive = false if i == 3
    menu_item.submenu = create_menu(depth - 1)
  end

  menu
end

#create_menu_item(label, depth) ⇒ Object



486
487
488
489
490
# File 'lib/vimamsa/gui.rb', line 486

def create_menu_item(label, depth)
  menuitem = Gtk::MenuItem.new(:label => label)
  menuitem.submenu = create_menu(depth)
  @menubar.append(menuitem)
end

#end_overlay_drawObject



273
274
275
# File 'lib/vimamsa/gui.rb', line 273

def end_overlay_draw()
  @da.show_all
end

#handle_deltasObject



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/vimamsa/gui.rb', line 342

def handle_deltas()
  while d = buf.deltas.shift
    pos = d[0]
    op = d[1]
    num = d[2]
    txt = d[3]
    if op == DELETE
      startiter = @buf1.get_iter_at(:offset => pos)
      enditer = @buf1.get_iter_at(:offset => pos + num)
      @buf1.delete(startiter, enditer)
    elsif op == INSERT
      startiter = @buf1.get_iter_at(:offset => pos)
      @buf1.insert(startiter, txt)
    end
  end
end

#init_header_barObject



400
401
402
403
404
405
406
407
408
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
434
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/vimamsa/gui.rb', line 400

def init_header_bar()
  header = Gtk::HeaderBar.new
  @header = header
  header.show_close_button = true
  header.title = ""
  header.has_subtitle = true
  header.subtitle = ""
  # Ripl.start :binding => binding

  # icon = Gio::ThemedIcon.new("mail-send-receive-symbolic")
  # icon = Gio::ThemedIcon.new("document-open-symbolic")
  # icon = Gio::ThemedIcon.new("dialog-password")

  #edit-redo edit-paste edit-find-replace edit-undo edit-find edit-cut edit-copy
  #document-open document-save document-save-as document-properties document-new
  # document-revert-symbolic
  #

  #TODO:
  # button = Gtk::Button.new
  # icon = Gio::ThemedIcon.new("open-menu-symbolic")
  # image = Gtk::Image.new(:icon => icon, :size => :button)
  # button.add(image)
  # header.pack_end(button)

  button = Gtk::Button.new
  icon = Gio::ThemedIcon.new("document-open-symbolic")
  image = Gtk::Image.new(:icon => icon, :size => :button)
  button.add(image)
  header.pack_end(button)

  button.signal_connect "clicked" do |_widget|
    open_file_dialog
  end

  button = Gtk::Button.new
  icon = Gio::ThemedIcon.new("document-save-symbolic")
  image = Gtk::Image.new(:icon => icon, :size => :button)
  button.add(image)
  header.pack_end(button)
  button.signal_connect "clicked" do |_widget|
    buf.save
  end

  button = Gtk::Button.new
  icon = Gio::ThemedIcon.new("document-new-symbolic")
  image = Gtk::Image.new(:icon => icon, :size => :button)
  button.add(image)
  header.pack_end(button)
  button.signal_connect "clicked" do |_widget|
    create_new_file
  end

  box = Gtk::Box.new(:horizontal, 0)
  box.style_context.add_class("linked")

  button = Gtk::Button.new
  image = Gtk::Image.new(:icon_name => "pan-start-symbolic", :size => :button)
  button.add(image)
  box.add(button)
  button.signal_connect "clicked" do |_widget|
    history_switch_backwards
  end

  button = Gtk::Button.new
  image = Gtk::Image.new(:icon_name => "pan-end-symbolic", :size => :button)
  button.add(image)
  box.add(button)
  button.signal_connect "clicked" do |_widget|
    history_switch_forwards
  end

  button = Gtk::Button.new
  icon = Gio::ThemedIcon.new("window-close-symbolic")
  image = Gtk::Image.new(:icon => icon, :size => :button)
  button.add(image)
  box.add(button)
  button.signal_connect "clicked" do |_widget|
    bufs.close_current_buffer
  end

  header.pack_start(box)
  @window.titlebar = header
  @window.add(Gtk::TextView.new)
end

#init_minibufferObject



365
366
367
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
393
394
395
396
397
398
# File 'lib/vimamsa/gui.rb', line 365

def init_minibuffer()
  # Init minibuffer
  sw = Gtk::ScrolledWindow.new
  sw.set_policy(:automatic, :automatic)
  overlay = Gtk::Overlay.new
  overlay.add(sw)
  # @vpaned.pack2(overlay, :resize => false)
  @vbox.attach(overlay, 0, 2, 1, 1)
  # overlay.set_size_request(-1, 50)
  # $ovrl = overlay
  # $ovrl.set_size_request(-1, 30)
  $sw2 = sw
  sw.set_size_request(-1, 12)

  view = VSourceView.new()
  view.set_highlight_current_line(false)
  view.set_show_line_numbers(false)
  # view.set_buffer(buf1)
  ssm = GtkSource::StyleSchemeManager.new
  ssm.set_search_path(ssm.search_path << ppath("styles/"))
  sty = ssm.get_scheme("molokai_edit")
  view.buffer.highlight_matching_brackets = false
  view.buffer.style_scheme = sty
  provider = Gtk::CssProvider.new
  # provider.load(data: "textview { font-family: Monospace; font-size: 11pt; }")
  provider.load(data: "textview { font-family: Arial; font-size: 10pt; color:#ff0000}")
  view.style_context.add_provider(provider)
  view.wrap_mode = :char
  @minibuf = view
  # Ripl.start :binding => binding
  # startiter = view.buffer.get_iter_at(:offset => 0)
  message("STARTUP")
  sw.add(view)
end

#init_windowObject



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
# File 'lib/vimamsa/gui.rb', line 511

def init_window
  @window = Gtk::Window.new(:toplevel)
  @window.set_default_size(650, 850)
  @window.title = "Multiple Views"
  @window.show_all
  # vpaned = Gtk::Paned.new(:horizontal)
  @vpaned = Gtk::Paned.new(:vertical)
  #@vpaned = Gtk::Box.new(:vertical, 0)
  # @vbox = Gtk::Box.new(:vertical, 0)
  @vbox = Gtk::Grid.new()
  @window.add(@vbox)

  @menubar = Gtk::MenuBar.new
  @menubar.expand = false
  

  @sw = Gtk::ScrolledWindow.new
  @sw.set_policy(:automatic, :automatic)
  @overlay = Gtk::Overlay.new
  @overlay.add(@sw)

  # @vpaned.pack1(@overlay, :resize => true)
  # @vpaned.pack2(@menubar, :resize => false)
  # @vbox.add(@menubar, :resize => false)

  init_header_bar

  # @window.show_all

  # @vbox.pack_start(@menubar, :expand => false, :fill => false, :padding => 0 )
  # @vbox.pack_start(@menubar)
  # @vbox.pack_start(@overlay, :expand => true, :fill => true, :padding => 0 )
  # @vbox.pack_start(@overlay, :expand => true, :fill => true, :padding => 0 )
  @vbox.attach(@menubar, 0, 0, 1, 1)
  @vbox.attach(@overlay, 0, 1, 1, 1)
  @overlay.vexpand = true
  @overlay.hexpand = true

  @menubar.vexpand = false
  @menubar.hexpand = false

  init_minibuffer


  @window.show_all
  vma.start
  Vimamsa::Menu.new(@menubar)
  @window.show_all
  
end

#overlay_draw_text(text, textpos) ⇒ Object



264
265
266
267
268
269
270
271
# File 'lib/vimamsa/gui.rb', line 264

def overlay_draw_text(text, textpos)
  # puts "overlay_draw_text #{[x,y]}"
  (x, y) = @view.pos_to_coord(textpos)
  # puts "overlay_draw_text #{[x,y]}"
  label = Gtk::Label.new("<span background='#00000088' foreground='#ff0000' weight='ultrabold'>#{text}</span>")
  label.use_markup = true
  @da.put(label, x, y)
end

#runObject



246
247
248
249
250
# File 'lib/vimamsa/gui.rb', line 246

def run
  init_window
  # init_rtext
  Gtk.main
end

#start_overlay_drawObject



252
253
254
255
256
# File 'lib/vimamsa/gui.rb', line 252

def start_overlay_draw()
  @da = Gtk::Fixed.new
  @overlay.add_overlay(@da)
  @overlay.set_overlay_pass_through(@da, true)
end

#toggle_overlayObject



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
# File 'lib/vimamsa/gui.rb', line 277

def toggle_overlay
  @show_overlay = @show_overlay ^ 1
  if !@show_overlay
    if @da != nil
      @overlay.remove(@da)
    end
    return
  else
    @da = Gtk::Fixed.new
    @overlay.add_overlay(@da)
    @overlay.set_overlay_pass_through(@da, true)
  end

  (startpos, endpos) = get_visible_area
  s = @view.buffer.text
  wpos = s.enum_for(:scan, /\W(\w)/).map { Regexp.last_match.begin(0) + 1 }
  wpos = wpos[0..130]

  # vr =  @view.visible_rect
  # # gtk_text_view_get_line_at_y
  # # gtk_text_view_get_iter_at_position
  # gtk_text_view_get_iter_at_position(vr.
  # istart = @view.get_iter_at_position(vr.x,vr.y)
  # istart = @view.get_iter_at_y(vr.y)
  # startpos = @view.get_iter_at_position_raw(vr.x,vr.y)[1].offset
  # endpos = @view.get_iter_at_position_raw(vr.x+vr.width,vr.y+vr.height)[1].offset
  # puts "startpos,endpos:#{[startpos, endpos]}"

  da = @da
  if false
    da.signal_connect "draw" do |widget, cr|
      cr.save
      for pos in wpos
        (x, y) = @view.pos_to_coord(pos)

        layout = da.create_pango_layout("XY")
        desc = Pango::FontDescription.new("sans bold 11")
        layout.font_description = desc

        cr.move_to(x, y)
        # cr.move_to(gutter_width, 300)
        cr.pango_layout_path(layout)

        cr.set_source_rgb(1.0, 0.0, 0.0)
        cr.fill_preserve
      end
      cr.restore
      false # = draw other
      # true # = Don't draw others
    end
  end

  for pos in wpos
    (x, y) = @view.pos_to_coord(pos)
    # da.put(Gtk::Label.new("AB"), x, y)
    label = Gtk::Label.new("<span background='#00000088' foreground='#ff0000' weight='ultrabold'>AB</span>")
    label.use_markup = true
    da.put(label, x, y)
  end

  # puts @view.pos_to_coord(300).inspect

  @da.show_all
end