Class: RubyCurses::CommandWindow::ListObject

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcurse/core/util/rcommandwindow.rb

Overview

displays a list Why did we create another list ? 2011-12-5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cw, _list, config = {}) ⇒ ListObject

Returns a new instance of ListObject.



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
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 321

def initialize cw, _list, config={}
  @cw  = cw
  layout = @cw.layout
  @window = @cw.window
  @height = layout[:height]
  @width = layout[:width]
  content(_list)
  @height_adjust = config.fetch(:box, true) == :border ? 3 : 2
  @selected_index = nil
  @current_index = 0
  @row_offset = 1
  @toprow = 0
  $multiplier = 0 # till we can do something

  @focussed_symbol = ''
  @row_selected_symbol = ''
  #@show_selector = true
  if @show_selector
    @row_selected_symbol ||= '*'
    @row_unselected_symbol ||= ' '
    @left_margin ||= @row_selected_symbol.length
  end
  #@show_selector = true
  #@row_selected_symbol = '*'
  #@row_unselected_symbol = ' '
end

Instance Attribute Details

#current_indexObject (readonly)

Returns the value of attribute current_index.



318
319
320
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 318

def current_index
  @current_index
end

#cwObject (readonly)

Returns the value of attribute cw.



316
317
318
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 316

def cw
  @cw
end

#focussed_attribObject

Returns the value of attribute focussed_attrib.



319
320
321
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 319

def focussed_attrib
  @focussed_attrib
end

#focussed_symbolObject

Returns the value of attribute focussed_symbol.



320
321
322
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 320

def focussed_symbol
  @focussed_symbol
end

#listObject (readonly)

Returns the value of attribute list.



317
318
319
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 317

def list
  @list
end

Instance Method Details

#bounds_checkObject

please set oldrow before calling this. Store current_index as oldrow before changing. NOTE



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 541

def bounds_check
  h = scrollatrow()
  rc = row_count

  @current_index = 0 if @current_index < 0  # not lt 0
  @current_index = rc-1 if @current_index >= rc && rc>0 # not gt rowcount
  @toprow = rc-h-1 if rc > h && @toprow > rc - h - 1 # toprow shows full page if possible
  # curr has gone below table,  move toprow forward
  if @current_index - @toprow > h
    @toprow = @current_index - h
  elsif @current_index < @toprow
    # curr has gone above table,  move toprow up
    @toprow = @current_index
  end
  set_form_row

end

#content(txt, config = {}) ⇒ Object



347
348
349
350
351
352
353
354
355
356
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 347

def content txt, config={}
  @current_index = 0 # sometimes it gets left at a higher value than there are rows to show
  case txt
  when String
    txt = wrap_text(txt, @width-2).split("\n")
  when Array
    # okay
  end
  @list = txt
end

#display_contentObject

:nodoc:



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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
430
431
432
433
434
435
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 381

def display_content #:nodoc:

  @graphic = @window
  @start ||= 0
  @toprow ||= 0
  @left_margin ||= @row_selected_symbol.length + @focussed_symbol.length

  #print_borders unless @suppress_borders # do this once only, unless everything changes
  #maxlen = @maxlen ||= @width-2
  tm = @list
  rc = tm.size
  @col_offset = 1
  #@longest_line = @width
  if rc > 0     # just added in case no data passed
    #tr = @start #@toprow
    tr = @toprow
    acolor = get_color $datacolor
    #h = @height - 3 #2
    h = @height - @height_adjust
    r,c = @row_offset, @col_offset
    0.upto(h) do |hh|
      crow = tr+hh
      if crow < rc
        focussed = @current_index == crow  # row focussed ?
        selected = is_row_selected crow
        content = tm[crow]   # 2009-01-17 18:37 chomp giving error in some cases says frozen
        # by now it has to be a String
        ## set the selector symbol if requested
        selection_symbol = ''
        if @show_selector
          if selected
            selection_symbol = @row_selected_symbol
          else
            selection_symbol =  @row_unselected_symbol
          end
          @graphic.printstring r+hh, c, selection_symbol, acolor,@attr
        end
        #renderer = get_default_cell_renderer_for_class content.class.to_s
        if focussed
          if @focussed_symbol
            @graphic.printstring r+hh, c, @focussed_symbol, acolor,@attr
          end
          @graphic.printstring r+hh, c+@left_margin, content, acolor, @focussed_attrib || 'reverse'
        else
          @graphic.printstring r+hh, c+@left_margin, content, acolor,@attr
        end
        #renderer.repaint @graphic, r+hh, c+@left_margin, crow, content, focussed, selected
      else
        # clear rows
        @graphic.printstring r+hh, c, " " * (@width-2), acolor,@attr
      end
    end
  end # rc == 0
  set_form_row
end

#display_interactiveObject

maybe we should just use a textview or label rather than try to do it all voer again !



359
360
361
362
363
364
365
366
367
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 359

def display_interactive
  display_content
  while !@stop
    @window.wrefresh # FFI 2011-09-12 
    # FIXME only clear and redisplay if change has happened (repaint_require)
    handle_keys { |ch| @cw.clear; display_content }
  end
  return @list[@current_index]
end

#goto_bottomObject Also known as: goto_end



501
502
503
504
505
506
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 501

def goto_bottom
  @oldrow = @current_index
  rc = row_count
  @current_index = rc -1
  bounds_check
end

#goto_topObject Also known as: goto_start



508
509
510
511
512
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 508

def goto_top
  @oldrow = @current_index
  @current_index = 0
  bounds_check
end

#handle_keysObject

bounds_check



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 585

def handle_keys #:nodoc:
  begin
    while((ch = @window.getchar()) != 999 )
      case ch
      when -1
        next
      else
        press ch
        break if @stop
        yield ch if block_given?
      end
    end
  ensure
    @cw.destroy  
  end
  return @current_index
end

#is_row_selected(row) ⇒ Object



368
369
370
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 368

def is_row_selected row
  @selected_index == row
end

#next_row(num = (($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)) ⇒ Object Also known as: down



492
493
494
495
496
497
498
499
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 492

def next_row num=(($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)
  rc = row_count
  return false if @current_index == rc-1 
  @oldrow = @current_index
  @current_index += 1*num if @current_index < rc
  bounds_check
  $multiplier = 0
end

#OLDbounds_checkObject

:nodoc:



577
578
579
580
581
582
583
584
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 577

def OLDbounds_check #:nodoc:
  @start = 0 if @start < 0
  row_offset = 1
  last = (@list.length)-(@height-row_offset-1)
  if @start > last
    @start = last
  end
end

#press(ch) ⇒ Object

listobject



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
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 437

def press ch # list TODO copy from rbasiclist
  ch = ch.getbyte(0) if ch.class==String ## 1.9
  $log.debug " XXX press #{ch} " if $log.debug? 
  case ch
  when -1
    return
  when KEY_F1, 27, ?\C-q.getbyte(0)   
    @stop = true
    return
  when KEY_ENTER, 10, 13
    #$log.debug "popup ENTER : #{@selected_index} "
    #$log.debug "popup ENTER :  #{field.name}" if !field.nil?
    @stop = true
    return
  when 32, ?\C-d.getbyte(0)
    scroll_forward
    #@start += @height-1
    #bounds_check
  when KEY_UP, ?k.getbyte(0)
    previous_row
    #@start -= 1
    #@current_index -= 1
    #@current_index = 0 if @current_index < 0
    #@start = 0 if @start < 0
  when KEY_DOWN, ?j.getbyte(0)
    next_row
    #@start += 1
    #@current_index += 1
    #bounds_check
  when ?\C-b.getbyte(0)
    scroll_backward
    #@start -= @height-1
    #@start = 0 if @start < 0
  when 0, ?g.getbyte(0)
    goto_top
  when ?G.getbyte(0)
    goto_bottom
  end
  #@form.repaint
  Ncurses::Panel.update_panels();
  Ncurses.doupdate();
  @window.wrefresh
end

#previous_row(num = (($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)) ⇒ Object Also known as: up



481
482
483
484
485
486
487
488
489
490
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 481

def previous_row num=(($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)
  #return :UNHANDLED if @current_index == 0 # EVIL
  return false if @current_index == 0 
  @oldrow = @current_index
  num.times { 
    @current_index -= 1 if @current_index > 0
  }
  bounds_check
  $multiplier = 0
end

#row_countObject



375
376
377
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 375

def row_count
  @list.size
end

#rowcolObject



378
379
380
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 378

def rowcol
  return @row_offset, @col_offset
end

#scroll_backwardObject



514
515
516
517
518
519
520
521
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 514

def scroll_backward
  @oldrow = @current_index
  h = scrollatrow()
  m = $multiplier == 0? 1 : $multiplier
  @current_index -= h * m
  bounds_check
  $multiplier = 0
end

#scroll_forwardObject



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 522

def scroll_forward
  @oldrow = @current_index
  h = scrollatrow()
  rc = row_count
  m = $multiplier == 0? 1 : $multiplier
  # more rows than box
  if h * m < rc
    @toprow += h+1 #if @current_index+h < rc
    @current_index = @toprow
  else
    # fewer rows than box
    @current_index = rc -1
  end
  #@current_index += h+1 #if @current_index+h < rc
  bounds_check
end

#scrollatrowObject



372
373
374
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 372

def scrollatrow
  @height - 3
end

#set_form_rowObject



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 559

def set_form_row
  r,c = rowcol


  # when the toprow is set externally then cursor can be mispositioned since 
  # bounds_check has not been called
  if @current_index < @toprow
    # cursor is outside table
    @current_index = @toprow # ??? only if toprow 2010-10-19 12:56 
  end

  row = r + (@current_index-@toprow) 
  # row should not be < r or greater than r+height TODO FIXME

  #setrowcol row, nil
  @window.wmove row, c
  @window.wrefresh   # FFI added to keep cursor display in synch with selection
end