<commit_before>require 'canis/core/util/app'
#include Ncurses # FFI 2011-09-8 
include Canis

# This paints a vertical white bar given row and col, and length. It also calculates and prints
# a small bar over this based on relaetd objects list.length and current_index.
# Typically, after setup one would keep updating only current_index from the repaint method
# of caller or in the traversal event. This would look best if the listbox also has a reverse video border, or none.
# @example
#     lb = list_box ....
#     sb = Scrollbar.new @form, :row => lb.row, :col => lb.col, :length => lb.height, :list_length => lb.row_count, :current_index => 0
#      .... later as user traverses
#      sb.current_index = lb.current_index
#     sb = Scrollbar.new @form, :parent => list
#
# At a later stage, we will integrate this with lists and tables, so it will happen automatically.
#
# @since 1.2.0    UNTESTED
module Canis
  class Scrollbar < Widget
    # row to start, same as listbox, required.
    dsl_property :row
    # column to start, same as listbox, required.
    dsl_property :col
    # how many rows is this (should be same as listboxes height, required.
    dsl_property :length
    # vertical or horizontal currently only VERTICAL
    dsl_property :orientation
    # initialize based on parent's values
    dsl_property :parent
    # which row is focussed, current_index of listbox, required.
    dsl_property :current_index
    # how many total rows of data does the list have, same as @list.length, required.
    dsl_property :list_length

    # TODO: if parent passed, we shold bind to ON_ENTER and get current_index, so no extra work is required.

    def initialize form, config={}, &block

      # setting default first or else Widget will place its BW default
      #@color, @bgcolor = ColorMap.get_colors_for_pair $bottomcolor
      super
      @color_pair = get_color $datacolor, @color, @bgcolor
      @scroll_pair = get_color $bottomcolor, :green, :white
      #$log.debug "SCROLLBAR COLOR cp #{@color_pair} sp #{@scroll_pair} " if $log.debug? 
      @window = form.window
      @editable = false
      @focusable = false
      @repaint_required = true
      @orientation = :V
      if @parent
        @parent.bind :ENTER_ROW do |p|
          # parent must implement row_count, and have a @current_index
          raise StandardError, "Parent must implement row_count" unless p.respond_to? :row_count
          self.current_index = p.current_index
          @repaint_required = true  #requred otherwise at end when same value sent, prop handler
          # will not be fired (due to optimization).
        end
        # in some cases, on leaving a listbox or other component redraws itself to reduce
        # selected or highlighted object, so the scrollbar gets overwritten. We need to repaint it.
        @parent.bind :LEAVE do |p|
          @repaint_required = true  
        end
      end
    end

    ##
    # repaint the scrollbar<commit_msg>fixed due to textpad<commit_after>require 'canis/core/util/app'
#include Ncurses # FFI 2011-09-8 
include Canis

# This paints a vertical white bar given row and col, and length. It also calculates and prints
# a small bar over this based on relaetd objects list.length and current_index.
# Typically, after setup one would keep updating only current_index from the repaint method
# of caller or in the traversal event. This would look best if the listbox also has a reverse video border, or none.
# @example
#     lb = list_box ....
#     sb = Scrollbar.new @form, :row => lb.row, :col => lb.col, :length => lb.height, :list_length => lb.row_count, :current_index => 0
#      .... later as user traverses
#      sb.current_index = lb.current_index
#     sb = Scrollbar.new @form, :parent => list
#
# At a later stage, we will integrate this with lists and tables, so it will happen automatically.
#
# @since 1.2.0    UNTESTED
module Canis
  class Scrollbar < Widget
    # row to start, same as listbox, required.
    dsl_property :row
    # column to start, same as listbox, required.
    dsl_property :col
    # how many rows is this (should be same as listboxes height, required.
    dsl_property :length
    # vertical or horizontal currently only VERTICAL
    dsl_property :orientation
    # initialize based on parent's values
    dsl_property :parent
    # which row is focussed, current_index of listbox, required.
    dsl_property :current_index
    # how many total rows of data does the list have, same as @list.length, required.
    dsl_property :list_length

    # TODO: if parent passed, we shold bind to ON_ENTER and get current_index, so no extra work is required.

    def initialize form, config={}, &block

      # setting default first or else Widget will place its BW default
      #@color, @bgcolor = ColorMap.get_colors_for_pair $bottomcolor
      super
      @color_pair = get_color $datacolor, @color, @bgcolor
      @scroll_pair = get_color $bottomcolor, :green, :white
      #$log.debug "SCROLLBAR COLOR cp #{@color_pair} sp #{@scroll_pair} " if $log.debug? 
      @window = form.window
      @editable = false
      @focusable = false
      @repaint_required = true
      @orientation = :V
      if @parent
        @parent.bind :ENTER_ROW do |p|
          # textview sent self, textpad sends textactionevent
          if p.instance_of? TextActionEvent
            p = p.source
          end
          # parent must implement row_count, and have a @current_index
          raise StandardError, "Parent (#{p.class.to_s}) must implement row_count" unless p.respond_to? :row_count
          self.current_index = p.current_index
          @repaint_required = true  #requred otherwise at end when same value sent, prop handler
          # will not be fired (due to optimization).
        end
        # in some cases, on leaving a listbox or other component redraws itself to reduce
        # selected or highlighted object, so the scrollbar gets overwritten. We need to repaint it.
        @parent.bind :LEAVE do |p|
          @repaint_required = true  
        end
      end
    end

    ##
    # repaint the scrollbar
{'fraction_non_alphanumeric': 0.07509816753926701, 'fraction_numerical': 0.003599476439790576, 'mean_word_length': 2.901084875558392, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1347735', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1740, 'n_words': 715}