Class: RubyCurses::Button
- Defined in:
- lib/rbcurse/core/widgets/rwidget.rb
Overview
action buttons NOTE: When firing event, an ActionEvent will be passed as the first parameter, followed by anything you may have passed when binding, or calling the command() method.
- Action: may have to listen to Action property changes so enabled, name etc change can be reflected
2011-11-26 : define button as default, so it can show differently and also fire on ENTER trying out behavior change. space to fire current button, ENTER for default button which has > Name < look.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Widget
#_object_created, #col_offset, #cols_panned, #config, #curpos, #focussed, #form, #id, #key_label, #parent_component, #row_offset, #rows_panned, #state
Class Method Summary collapse
-
.button_layout(buttons, row, startcol = 0, cols = Ncurses.COLS-1, gap = 5) ⇒ Object
temporary method, shoud be a proper class.
Instance Method Summary collapse
-
#action(a) ⇒ Object
set button based on Action 2009-01-21 19:59.
-
#bind_hotkey ⇒ Object
bind hotkey to form keys.
-
#command(*args, &block) ⇒ Object
command of button (invoked on press, hotkey, space) added args 2008-12-20 19:22.
- #default_button(tf = nil) ⇒ Object
-
#fire ⇒ Object
fires PRESS event of button.
- #getvalue ⇒ Object
-
#getvalue_for_paint ⇒ Object
ensure text has been passed or action.
-
#handle_key(ch) ⇒ Object
Button.
-
#initialize(form, config = {}, &block) ⇒ Button
constructor
boolean for whether this is the default button.
-
#mnemonic(char = nil) ⇒ Object
FIXME this will not work in messageboxes since no form available if already set mnemonic, then unbind_key, ?? NOTE: Some buttons like checkbox directly call mnemonic, so if they have no form then this processing does not happen.
-
#repaint ⇒ Object
button.
-
#selected? ⇒ Boolean
for campatibility with all buttons, will apply to radio buttons mostly.
-
#text(*val) ⇒ Object
button: sets text, checking for ampersand, uses that for hotkey and underlines.
Methods inherited from Widget
#action_manager, #changed, #click, #color_pair, #destroy, #enter, #event_list, #focus, #get_preferred_size, #height, #height=, #hide, #init_vars, #leave, #modified?, #move, #on_enter, #on_leave, #override_graphic, #process_key, #remove, #repaint_all, #repaint_required, #rowcol, #set_buffer_modified, #set_buffering, #set_form, #set_form_col, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #text_variable, #unbind_key, #width, #width=
Methods included from Io
#__create_footer_window, #clear_this, #get_file, #print_this, #rb_getchar, #rb_gets, #rbgetstr, #warn
Methods included from Utils
#OLDdefine_key, #_process_key, #bind_key, #bind_keys, #clean_string!, #define_key, #define_prefix_command, #display_app_help, #get_attrib, #get_color, #keycode_tos, #last_line, #one_line_window, #parse_formatted_text, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #view, #wrap_text
Methods included from ConfigSetup
#cget, #config_setup, #configure, #variable_set
Methods included from EventHandler
#bind, #fire_handler, #fire_property_change
Constructor Details
#initialize(form, config = {}, &block) ⇒ Button
boolean for whether this is the default button. Careful, don’t do this for more than 1
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2811 def initialize form, config={}, &block require 'rbcurse/core/include/ractionevent' @focusable = true @editable = false # hotkey denotes we should bind the key itself not alt-key (for menulinks) @hotkey = config.delete(:hotkey) $log.debug "XXX: HOTKEY #{@hotkey} " @handler={} # event handler @event_args ||= {} @_events ||= [] @_events.push :PRESS @default_chars = ['> ', ' <'] super @surround_chars ||= ['[ ', ' ]'] @col_offset = @surround_chars[0].length @text_offset = 0 end |
Class Method Details
.button_layout(buttons, row, startcol = 0, cols = Ncurses.COLS-1, gap = 5) ⇒ Object
temporary method, shoud be a proper class
3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 3034 def self. , row, startcol=0, cols=Ncurses.COLS-1, gap=5 col = startcol .each_with_index do |b, ix| $log.debug " BUTTON #{b}: #{b.col} " b.row = row b.col col $log.debug " after BUTTON #{b}: #{b.col} " len = b.text.length + gap col += len end end |
Instance Method Details
#action(a) ⇒ Object
set button based on Action
2009-01-21 19:59
2833 2834 2835 2836 2837 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2833 def action a text a.name mnemonic a.mnemonic unless a.mnemonic.nil? command { a.call } end |
#bind_hotkey ⇒ Object
bind hotkey to form keys. added 2008-12-15 20:19 use ampersand in name or underline
2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2885 def bind_hotkey if @form.nil? if @underline @when_form ||= [] @when_form << lambda { bind_hotkey } end return end _value = @text || getvalue # hack for Togglebutton FIXME #_value = getvalue $log.debug " bind hot #{_value} #{@underline}" ch = _value[@underline,1].downcase()[0].ord ## 1.9 2009-10-05 18:55 TOTEST @mnemonic = _value[@underline,1] # meta key mch = ?\M-a.getbyte(0) + (ch - ?a.getbyte(0)) @form.bind_key(mch, "hotkey for button #{self.text}" ) { |_form, _butt| self.fire } end |
#command(*args, &block) ⇒ Object
command of button (invoked on press, hotkey, space) added args 2008-12-20 19:22
2986 2987 2988 2989 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2986 def command *args, &block bind :PRESS, *args, &block $log.debug "#{text} bound PRESS" end |
#default_button(tf = nil) ⇒ Object
2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2902 def tf=nil return @default_button unless tf raise ArgumentError, "default button must be true or false" if ![false,true].include? tf $log.debug "XXX: BUTTON DEFAULT setting to true : #{tf} " @default_button = tf if tf @surround_chars = @default_chars @form.bind_key(13, "fire #{self.text} ") { |_form, _butt| self.fire } else # i have no way of reversing the above end end |
#fire ⇒ Object
fires PRESS event of button
2991 2992 2993 2994 2995 2996 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2991 def fire $log.debug "firing PRESS #{text}" # why the .... am i passing form ? Pass a ActionEvent with source, text() and getvalue() #fire_handler :PRESS, @form changed on 2010-09-12 19:22 fire_handler :PRESS, ActionEvent.new(self, :PRESS, text) end |
#getvalue ⇒ Object
2915 2916 2917 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2915 def getvalue @text_variable.nil? ? @text : @text_variable.get_value(@name) end |
#getvalue_for_paint ⇒ Object
ensure text has been passed or action
2920 2921 2922 2923 2924 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2920 def getvalue_for_paint ret = getvalue @text_offset = @surround_chars[0].length @surround_chars[0] + ret + @surround_chars[1] end |
#handle_key(ch) ⇒ Object
Button
3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 3001 def handle_key ch case ch when FFI::NCurses::KEY_LEFT, FFI::NCurses::KEY_UP return :UNHANDLED # @form.select_prev_field when FFI::NCurses::KEY_RIGHT, FFI::NCurses::KEY_DOWN return :UNHANDLED # @form.select_next_field when FFI::NCurses::KEY_ENTER, 10, 13, 32 # added space bar also # I am really confused about this. Default button really confuses things in some # situations, but is great if you are not on the buttons. # shall we keep ENTER for default button #when 32 # added space bar also if respond_to? :fire fire end else if $key_map == :vim case ch when ?j.getbyte(0) @form.window.ungetch(KEY_DOWN) return 0 when ?k.getbyte(0) @form.window.ungetch(KEY_UP) return 0 end end return :UNHANDLED end end |
#mnemonic(char = nil) ⇒ Object
FIXME this will not work in messageboxes since no form available if already set mnemonic, then unbind_key, ?? NOTE: Some buttons like checkbox directly call mnemonic, so if they have no form then this processing does not happen
2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2863 def mnemonic char=nil return @mnemonic unless char # added 2011-11-24 so caller can get mne $log.error "ERROR WARN #{self} COULD NOT SET MNEMONIC since form NIL" if @form.nil? unless @form @when_form ||= [] @when_form << lambda { mnemonic char } return end #return if @form.nil? @mnemonic = char ch = char.downcase()[0].ord ## 1.9 # meta key ch = ?\M-a.getbyte(0) + (ch - ?a.getbyte(0)) unless @hotkey $log.debug " #{self} setting MNEMO to #{char} #{ch}, #{@hotkey} " _t = self.text || self.name || "Unnamed #{self.class} " @form.bind_key(ch, "hotkey for button #{_t} ") { |_form, _butt| self.fire } end |
#repaint ⇒ Object
button
2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2925 def repaint # button if @form if @when_form $log.debug "XXX:WHEN calling when_forms commands" @when_form.each { |c| c.call() } @when_form = nil end end @bgcolor ||= $def_bg_color @color ||= $def_fg_color $log.debug("BUTTON repaint : #{self} r:#{@row} c:#{@col} , #{@color} , #{@bgcolor} , #{getvalue_for_paint}" ) r,c = @row, @col #rowcol include offset for putting cursor # NOTE: please override both (if using a string), or else it won't work @highlight_foreground ||= $reversecolor @highlight_background ||= 0 _bgcolor = @bgcolor _color = @color if @state == :HIGHLIGHTED _bgcolor = @state==:HIGHLIGHTED ? @highlight_background : @bgcolor _color = @state==:HIGHLIGHTED ? @highlight_foreground : @color elsif selected? # only for certain buttons lie toggle and radio _bgcolor = @selected_background || @bgcolor _color = @selected_foreground || @color end $log.debug "XXX: button #{text} STATE is #{@state} color #{_color} , bg: #{_bgcolor} " if _bgcolor.is_a?( Fixnum) && _color.is_a?( Fixnum) else _color = get_color($datacolor, _color, _bgcolor) end value = getvalue_for_paint $log.debug("button repaint :#{self} r:#{r} c:#{c} col:#{_color} bg #{_bgcolor} v: #{value} ul #{@underline} mnem #{@mnemonic} datacolor #{$datacolor} ") len = @display_length || value.length @graphic = @form.window if @graphic.nil? ## cell editor listbox hack @graphic.printstring r, c, "%-*s" % [len, value], _color, @attr # @form.window.mvchgat(y=r, x=c, max=len, Ncurses::A_NORMAL, bgcolor, nil) # in toggle buttons the underline can change as the text toggles if @underline || @mnemonic uline = @underline && (@underline + @text_offset) || value.index(@mnemonic) || value.index(@mnemonic.swapcase) # if the char is not found don't print it if uline y=r #[email protected] x=c+uline #[email protected] if @graphic.window_type == :PAD x -= @graphic.left y -= @graphic.top end # # NOTE: often values go below zero since root windows are defined # with 0 w and h, and then i might use that value for calcaluting # $log.error "XXX button underline location error #{x} , #{y} " if x < 0 or c < 0 raise " #{r} #{c} #{uline} button underline location error x:#{x} , y:#{y}. left #{@graphic.left} top:#{@graphic.top} " if x < 0 or c < 0 @graphic.mvchgat(y, x, max=1, Ncurses::A_BOLD|Ncurses::A_UNDERLINE, _color, nil) end end end |
#selected? ⇒ Boolean
for campatibility with all buttons, will apply to radio buttons mostly
2998 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2998 def selected?; false; end |
#text(*val) ⇒ Object
button: sets text, checking for ampersand, uses that for hotkey and underlines
2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 |
# File 'lib/rbcurse/core/widgets/rwidget.rb', line 2840 def text(*val) if val.empty? return @text else s = val[0].dup s = s.to_s if !s.is_a? String # 2009-01-15 17:32 if (( ix = s.index('&')) != nil) s.slice!(ix,1) # 2011-10-20 NOTE XXX I have removed form check since bindkey is called conditionally @underline = ix #unless @form.nil? # this setting a fake underline in messageboxes @text = s # mnemo needs this for setting description mnemonic s[ix,1] end @text = s end end |