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.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/vimamsa/gui.rb', line 221

def initialize()
  @show_overlay = true
  @da = nil
  @buffers = {}
  @view = nil
  @buf1 = nil
  @img_resizer_active = false
  imgproc = proc {
    GLib::Idle.add(proc {
      if !buf.images.empty?
        vma.gui.scale_all_images

        w = Gtk::Window.new(:toplevel)
        w.set_default_size(1, 1)
        w.show_all
        Thread.new { sleep 0.1; w.destroy }
      end

      false
    })
  }
  @delex = DelayExecutioner.new(1, imgproc)
end

Instance Attribute Details

#buf1Object

Returns the value of attribute buf1.



211
212
213
# File 'lib/vimamsa/gui.rb', line 211

def buf1
  @buf1
end

#buffersObject

Returns the value of attribute buffers.



211
212
213
# File 'lib/vimamsa/gui.rb', line 211

def buffers
  @buffers
end

#delexObject

Returns the value of attribute delex.



211
212
213
# File 'lib/vimamsa/gui.rb', line 211

def delex
  @delex
end

#statnfoObject

Returns the value of attribute statnfo.



211
212
213
# File 'lib/vimamsa/gui.rb', line 211

def statnfo
  @statnfo
end

#swObject

Returns the value of attribute sw.



211
212
213
# File 'lib/vimamsa/gui.rb', line 211

def sw
  @sw
end

#viewObject

Returns the value of attribute view.



211
212
213
# File 'lib/vimamsa/gui.rb', line 211

def view
  @view
end

#windowObject

Returns the value of attribute window.



211
212
213
# File 'lib/vimamsa/gui.rb', line 211

def window
  @window
end

Instance Method Details

#add_to_minibuf(msg) ⇒ Object



390
391
392
393
394
# File 'lib/vimamsa/gui.rb', line 390

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



289
290
291
292
293
# File 'lib/vimamsa/gui.rb', line 289

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

#delay_scaleObject



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

def delay_scale()
  if Time.now - @dtime > 2.0
  end
end

#end_overlay_drawObject



304
305
306
# File 'lib/vimamsa/gui.rb', line 304

def end_overlay_draw()
  @da.show_all
end

#handle_deltasObject



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/vimamsa/gui.rb', line 373

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

#handle_image_resizeObject



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/vimamsa/gui.rb', line 265

def handle_image_resize
  return if @img_resizer_active == true
  @dtime = Time.now

  $gcrw = 0
  vma.gui.window.signal_connect "configure-event" do |widget, cr|
    # Ripl.start :binding => binding

    if $gcrw != cr.width
      @delex.run
    end
    $gcrw = cr.width
    false
  end

  @img_resizer_active = true
end

#init_header_barObject



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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/vimamsa/gui.rb', line 431

def init_header_bar()
  header = Gtk::HeaderBar.new
  @header = header
  header.show_close_button = true
  header.title = ""
  header.has_subtitle = true
  header.subtitle = ""

  # 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



396
397
398
399
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
# File 'lib/vimamsa/gui.rb', line 396

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, 2, 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(nil, nil)
  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 #TODO
  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



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

def init_window
  @window = Gtk::Window.new(:toplevel)
  sh = @window.screen.height
  sw = @window.screen.width
  # TODO:Maximise vertically
  @window.set_default_size((sw * 0.45).to_i, sh - 20)

  @window.title = "Multiple Views"
  @window.show_all
  @vpaned = Gtk::Paned.new(:vertical)

  @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)

  init_header_bar

  @statnfo = Gtk::Label.new
  provider = Gtk::CssProvider.new
  provider.load(data: "textview {   background-color:#353535; font-family: Monospace; font-size: 10pt; margin-top:4px;}")
  @statnfo.style_context.add_provider(provider)

  # Deprecated, but found no other way to do it. css doesn't work.
  # TODO: should select color automatically from theme
  @statnfo.override_background_color(Gtk::StateFlags::NORMAL, "#353535")

  # column, row, width height
  @vbox.attach(@menubar, 0, 0, 1, 1)
  @vbox.attach(@statnfo, 1, 0, 1, 1)
  @vbox.attach(@overlay, 0, 1, 2, 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



295
296
297
298
299
300
301
302
# File 'lib/vimamsa/gui.rb', line 295

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

#runObject



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

def run
  init_window
  # init_rtext
  Gtk.main
end

#scale_all_imagesObject



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

def scale_all_images
  # puts "scale all"
  for img in buf.images
    if !img[:obj].destroyed?
      img[:obj].scale_image
    end
  end
end

#start_overlay_drawObject



283
284
285
286
287
# File 'lib/vimamsa/gui.rb', line 283

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

#toggle_overlayObject



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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/vimamsa/gui.rb', line 308

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
  # debug "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

  # debug @view.pos_to_coord(300).inspect

  @da.show_all
end