Class: RubyCurses::CommandWindow::ListObject

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

Overview

displays a list

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ListObject.



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

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.



265
266
267
# File 'lib/rbcurse/rcommandwindow.rb', line 265

def current_index
  @current_index
end

#cwObject (readonly)

Returns the value of attribute cw.



263
264
265
# File 'lib/rbcurse/rcommandwindow.rb', line 263

def cw
  @cw
end

#focussed_attribObject

Returns the value of attribute focussed_attrib.



266
267
268
# File 'lib/rbcurse/rcommandwindow.rb', line 266

def focussed_attrib
  @focussed_attrib
end

#focussed_symbolObject

Returns the value of attribute focussed_symbol.



267
268
269
# File 'lib/rbcurse/rcommandwindow.rb', line 267

def focussed_symbol
  @focussed_symbol
end

#listObject (readonly)

Returns the value of attribute list.



264
265
266
# File 'lib/rbcurse/rcommandwindow.rb', line 264

def list
  @list
end

Instance Method Details

#bounds_checkObject

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



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/rbcurse/rcommandwindow.rb', line 488

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



294
295
296
297
298
299
300
301
302
303
# File 'lib/rbcurse/rcommandwindow.rb', line 294

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:



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
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/rbcurse/rcommandwindow.rb', line 328

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 !



306
307
308
309
310
311
312
313
314
# File 'lib/rbcurse/rcommandwindow.rb', line 306

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



448
449
450
451
452
453
# File 'lib/rbcurse/rcommandwindow.rb', line 448

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

#goto_topObject Also known as: goto_start



455
456
457
458
459
# File 'lib/rbcurse/rcommandwindow.rb', line 455

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

#handle_keysObject

bounds_check



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/rbcurse/rcommandwindow.rb', line 532

def handle_keys
  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



315
316
317
# File 'lib/rbcurse/rcommandwindow.rb', line 315

def is_row_selected row
  @selected_index == row
end

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



439
440
441
442
443
444
445
446
# File 'lib/rbcurse/rcommandwindow.rb', line 439

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



524
525
526
527
528
529
530
531
# File 'lib/rbcurse/rcommandwindow.rb', line 524

def OLDbounds_check
  @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



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

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



428
429
430
431
432
433
434
435
436
437
# File 'lib/rbcurse/rcommandwindow.rb', line 428

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



322
323
324
# File 'lib/rbcurse/rcommandwindow.rb', line 322

def row_count
  @list.size
end

#rowcolObject



325
326
327
# File 'lib/rbcurse/rcommandwindow.rb', line 325

def rowcol
  return @row_offset, @col_offset
end

#scroll_backwardObject



461
462
463
464
465
466
467
468
# File 'lib/rbcurse/rcommandwindow.rb', line 461

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

#scroll_forwardObject



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/rbcurse/rcommandwindow.rb', line 469

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



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

def scrollatrow
  @height - 3
end

#set_form_rowObject



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

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