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.



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

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.



302
303
304
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 302

def current_index
  @current_index
end

#cwObject (readonly)

Returns the value of attribute cw.



300
301
302
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 300

def cw
  @cw
end

#focussed_attribObject

Returns the value of attribute focussed_attrib.



303
304
305
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 303

def focussed_attrib
  @focussed_attrib
end

#focussed_symbolObject

Returns the value of attribute focussed_symbol.



304
305
306
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 304

def focussed_symbol
  @focussed_symbol
end

#listObject (readonly)

Returns the value of attribute list.



301
302
303
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 301

def list
  @list
end

Instance Method Details

#bounds_checkObject

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



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 525

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



331
332
333
334
335
336
337
338
339
340
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 331

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:



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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 365

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 !



343
344
345
346
347
348
349
350
351
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 343

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



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

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

#goto_topObject Also known as: goto_start



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

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

#handle_keysObject

bounds_check



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 569

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



352
353
354
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 352

def is_row_selected row
  @selected_index == row
end

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



476
477
478
479
480
481
482
483
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 476

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:



561
562
563
564
565
566
567
568
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 561

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



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

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



465
466
467
468
469
470
471
472
473
474
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 465

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



359
360
361
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 359

def row_count
  @list.size
end

#rowcolObject



362
363
364
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 362

def rowcol
  return @row_offset, @col_offset
end

#scroll_backwardObject



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

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

#scroll_forwardObject



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 506

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



356
357
358
# File 'lib/rbcurse/core/util/rcommandwindow.rb', line 356

def scrollatrow
  @height - 3
end

#set_form_rowObject



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

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