sup

A curses threads-with-tags style email client

ncursesw-ruby.git

git clone https://supmua.dev/git/ncursesw-ruby/
commit f653f049d70c06e5668876ba555060d911f8d745
parent 7b5bc3c8d96511fcb62024c7ca05d7d3fc13f06e
Author: Gaute Hope <eg@gaute.vetsj.com>
Date:   Sun,  8 Dec 2013 14:58:41 +0100

Merge #12: Bind get_wch and form_driver_w

form_driver_w is included in Ncurses 5.9-20131207 [0]. An example
program is provided in examples/form_get_wch.rb.

[0] http://lists.gnu.org/archive/html/bug-ncurses/2013-12/msg00010.html

Squashed commit of the following:

commit d077f467996c1f3e2bf19fda8d30419283700cd4
Author: Gaute Hope 
Date:   Mon Dec 2 11:58:50 2013 +0100

    use driver_w for class method

commit 20e4d711258fcbc25ad3283c98e40dbae7c65045
Author: Gaute Hope 
Date:   Mon Dec 2 10:53:26 2013 +0100

    dont define USE_WIDEC_SUPPORT

commit 1f67d1d78fffaba8a652dde318a760e0de75393e
Author: Gaute Hope 
Date:   Mon Nov 25 12:49:49 2013 +0100

    copyright me and fix ws

commit b1244701a3ad96bead0dcaf0d26b484284f1365d
Author: Gaute Hope 
Date:   Sat Nov 23 15:56:59 2013 +0100

    Wrap form_driver_w

commit ddd52161a44537ddb0809cbdced648eb45afaff2
Author: Gaute Hope 
Date:   Sat Nov 23 15:51:27 2013 +0100

    wrap form_driver_w

commit d4e1590e0035c5cbb2143c8941d6cd79c958bd62
Author: Gaute Hope 
Date:   Fri Nov 22 13:55:08 2013 +0100

    WIP: add_wch

commit 42b36f751fdaad30a6517397e11068dfa8accc5b
Author: Gaute Hope 
Date:   Wed Nov 20 10:15:35 2013 +0100

    Use Ncurses constants

commit aaf53e52c07e744341537d65afbec55fc360b973
Author: Gaute Hope 
Date:   Wed Nov 20 10:03:06 2013 +0100

    pack as unicode char

commit 4481f31042cc379a026c68fbfb3919c6930c673a
Author: Gaute Hope 
Date:   Wed Nov 20 07:20:15 2013 +0100

    check for formw, return long

commit 25ade76617c2145da9f0972704be2184343e77d7
Author: Gaute Hope 
Date:   Wed Nov 20 00:01:06 2013 +0100

    return array with return type and ruby string

commit 5475417b0861d2c1867ef3ecb19f0cdbc8b2828c
Author: Gaute Hope 
Date:   Tue Nov 19 18:33:29 2013 +0100

    Actually use wget_wch, wrap in function of same type as wgetch

commit cbc259754da2aa27fa1655f57e6ead6c6fa44eeb
Author: Gaute Hope 
Date:   Tue Nov 19 17:51:47 2013 +0100

    bind get_wch for wide char input

Diffstat:
A examples/form_get_wch.rb | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M extconf.rb | 14 +++++++++++++-
M form_wrap.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
M form_wrap.h | 5 +++--
M ncurses_wrap.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++--
M ncurses_wrap.h | 3 +++
6 files changed, 271 insertions(+), 64 deletions(-)
diff --git a/examples/form_get_wch.rb b/examples/form_get_wch.rb
@@ -0,0 +1,121 @@
+#!/usr/bin/ruby
+# Copyright (c) 2004 by Simon Kaczor <skaczor@cox.net>
+# Example from the NCurses Programming HOWTO
+# This example uses module functions that are documented in the ncurses man page.
+# For a more rubyish approach that uses Ruby objects see form2.rb
+#
+# The original example contained the following copyright:
+# Copyright (c) 2001 by Pradeep Padala. This document may be distributed
+# under the terms set forth in the LDP license at linuxdoc.org/COPYRIGHT.html.
+
+require 'ncursesw.rb'
+
+begin
+  scr = Ncurses.initscr()
+  Ncurses.cbreak()
+  Ncurses.noecho()
+  Ncurses.keypad(scr, true)
+
+  #create some fields
+  fields = Array.new
+  fields.push(Ncurses::Form.new_field(1,10,4,18,0,0))
+  fields.push(Ncurses::Form.new_field(1,10,6,18,0,0))
+
+  # set field options
+  Ncurses::Form.set_field_back(fields[0], Ncurses::A_UNDERLINE)
+  Ncurses::Form.field_opts_off(fields[0], Ncurses::Form::O_AUTOSKIP)
+
+  Ncurses::Form.set_field_back(fields[1], Ncurses::A_UNDERLINE)
+  Ncurses::Form.field_opts_off(fields[1], Ncurses::Form::O_AUTOSKIP)
+
+
+  # create a form
+  form = Ncurses::Form.new_form(fields)
+
+  # post the form and refresh the screen
+  Ncurses::Form.post_form(form)
+  scr.refresh()
+
+  Ncurses.mvprintw(4, 10, "Value 1:")
+  Ncurses.mvprintw(6, 10, "Value 2:")
+  scr.refresh()
+
+  buf = ""
+
+  # Loop through to get user requests
+  while(true) do
+    ret = Ncurses.get_wch()
+
+    ch = ret[1]
+
+    Ncurses.mvprintw(8, 10, "Got: #{ch.inspect} (#{[ch].pack("U")})")
+
+    case (ret[0])
+    when Ncurses::OK
+      # If this is a normal character, it gets Printed
+      #prbuf = fields[0].field_buffer 0
+      ##buf = buf.force_encoding('utf-8') + [ch].pack('U')
+      #buf = prbuf + [ch].pack('U') 
+
+
+      ##buf = sbuf.force_encoding('utf-8') + [ch].pack('U')
+      #fields[0].set_field_buffer(0, buf)
+      #sbuf = fields[0].field_buffer 0
+      ##fields[0].working.add_wch 'a'
+      #Ncurses.mvprintw(12, 10, "buf: #{sbuf}")
+      #Ncurses.mvprintw(13, 10, "pbuf: #{prbuf}")
+      #Ncurses.mvprintw(14, 10, "mbuf: #{buf}")
+
+      #Ncurses.wadd_wch(form.win, ch)
+
+      #buf = fields[0].field_buffer(0)
+      #buf = buf.force_encoding('utf-8') + [ch].pack("U")
+      #fields[0].set_field_buffer(0, buf)
+      #fields[0].set_field_buffer(0, [ch].pack("U"))
+
+      Ncurses::Form.form_driver_w(form, ret[0], ret[1])
+      #puts "{fields[0].methods}"
+
+      #puts "#{form.methods}"
+      #puts "#{fields[0].methods}"
+      #Ncurses.mvprintw(9, 10, "Type: OK           ")
+    when Ncurses::KEY_CODE_YES
+      Ncurses.mvprintw(9, 10, "Type: KEY_CODE_YES")
+      case(ch)
+      when Ncurses::KEY_DOWN
+        # Go to next field
+        Ncurses::Form.form_driver(form, Ncurses::Form::REQ_NEXT_FIELD)
+        # Go to the end of the present buffer
+        # Leaves nicely at the last character
+        Ncurses::Form.form_driver(form, Ncurses::Form::REQ_END_LINE)
+
+      when Ncurses::KEY_UP
+        #Go to previous field
+        Ncurses::Form.form_driver(form, Ncurses::Form::REQ_PREV_FIELD)
+        Ncurses::Form.form_driver(form, Ncurses::Form::REQ_END_LINE);
+      end
+    else
+      Ncurses.mvprintw(9, 10, "Type: Unknown: #{ret[0].inspect}            ")
+    end
+  end
+
+  # unpost and free form
+  Ncurses::Form.unpost_form(form);
+  Ncurses::Form.free_form(form)
+  Ncurses::Form.free_field(fields[0]);
+  Ncurses::Form.free_field(fields[1]);
+
+
+  #using class methods this time
+#  form = Ncurses::Form::FORM.new(fields)
+#  puts "Created form: #{form.inspect}\n"
+#  form.free()
+
+ensure
+  # put the screen back in its normal state
+  Ncurses.echo()
+  Ncurses.nocbreak()
+  Ncurses.nl()
+  Ncurses.endwin()
+end
+
diff --git a/extconf.rb b/extconf.rb
@@ -3,6 +3,7 @@
 # ncurses-ruby is a ruby module for accessing the FSF's ncurses library
 # (C) 2002, 2004 Tobias Peters <t-peters@users.berlios.de>
 # (C) 2005, 2009, 2011 Tobias Herzke
+# (C) 2013 Gaute Hope <eg@gaute.vetsj.com>
 #
 # This module is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -105,6 +106,11 @@ have_func("wchgat")
 have_func("wcolor_set")
 have_func("getattrs")
 
+puts "checking for ncursesw (wide char) functions..."
+if not have_func("wget_wch")
+  raise "no wget_wch found."
+end
+
 puts "checking which debugging functions to wrap..."
 have_func("_tracef")
 have_func("_tracedump")
@@ -129,11 +135,17 @@ end
 
 puts "checking for the form library..."
 if have_header("form.h")
-  have_library("formw", "new_form")
+  if not have_library("formw", "new_form")
+    raise "formw library not found"
+  end
 else
   raise "form library not found."
 end
 
+if have_library("formw", "form_driver_w")
+  $CFLAGS += " -DHAVE_FORM_DRIVER_W"
+end
+
 puts "checking for the menu library..."
 if have_header("menu.h")
   have_library("menu", "new_menu")
diff --git a/form_wrap.c b/form_wrap.c
@@ -3,10 +3,11 @@
  * Contributed by Simon Kaczor <skaczor@cox.net>
  * Prognosoft Inc. <http://www.prognosoft.biz>
  * Copyright 2004
- * 
+ *
  * Changes:
  * (C) 2004 Tobias Peters
  * (C) 2005 2009 Tobias Herzke
+ * (C) 2013 Gaute Hope <eg@gaute.vetsj.com>
  *
  *  This module is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
@@ -29,6 +30,8 @@
 
 #if defined(HAVE_FORM_H) || defined(HAVE_NCURSESW_FORM_H)
 
+# include <wchar.h>
+
 #include "form_wrap.h"
 #include "ncurses_wrap.h"
 #include "compat.h"
@@ -38,7 +41,7 @@ VALUE cFIELD;
 VALUE cFIELDTYPE;
 VALUE cFORM;
 
-void init_err_codes() 
+void init_err_codes()
 {
   /* The routine succeeded. */
   FORM_DEF_CONST(E_OK);
@@ -362,14 +365,14 @@ static VALUE get_proc_hash(int hook) {
 }
 
 /*
- * Returns an existing Ruby Proc for a given owning "object" and hook type. 
+ * Returns an existing Ruby Proc for a given owning "object" and hook type.
  * Qnil will be returned if no Proc was associated with the owner
  */
 static VALUE get_proc(void* owner, int hook) {
   if (owner == 0) return Qnil;
   {
 	 VALUE owner_adress  = INT2NUM((long)(owner));
-	 VALUE proc_hash     = get_proc_hash(hook);		 
+	 VALUE proc_hash     = get_proc_hash(hook);
 	 VALUE proc          = rb_hash_aref(proc_hash, owner_adress);
 	 return proc;
   }
@@ -400,12 +403,12 @@ static VALUE rbncurs_m_new_form(VALUE dummy, VALUE rb_field_array)
   long n = rbncurs_array_length(rb_field_array);
   /* Will ncurses free this array? If not, must do it after calling free_form(). */
   FIELD** fields = ALLOC_N(FIELD*, (n+1));
-  long i;  
+  long i;
   for (i=0; i<n; i++){
-	 fields[i] = get_field(rb_ary_entry(rb_field_array, i)); 
+	 fields[i] = get_field(rb_ary_entry(rb_field_array, i));
   }
   fields[n] = NULL;
-  return wrap_form(new_form(fields)); 
+  return wrap_form(new_form(fields));
 }
 
 static VALUE rbncurs_c_free_form(VALUE rb_form) {
@@ -446,12 +449,22 @@ static VALUE rbncurs_c_form_driver(VALUE rb_form, VALUE c) {
 static VALUE rbncurs_m_form_driver(VALUE dummy, VALUE rb_form, VALUE c)
 { return rbncurs_c_form_driver(rb_form, c); }
 
+# ifdef HAVE_FORM_DRIVER_W
+/* Form driver W */
+static VALUE rbncurs_c_form_driver_w(VALUE rb_form, VALUE type, VALUE c) {
+  FORM* form = get_form(rb_form);
+  return INT2NUM(form_driver_w(form, NUM2INT(type), NUM2INT(c)));
+}
+static VALUE rbncurs_m_form_driver_w(VALUE dummy, VALUE rb_form, VALUE type, VALUE c)
+{ return rbncurs_c_form_driver_w(rb_form, type, c); }
+# endif
+
 /*
  * form_page(3x)
  */
 static VALUE rbncurs_c_set_current_field(VALUE rb_form, VALUE rb_field) {
   FORM* form = get_form(rb_form);
-  FIELD* field = get_field(rb_field);	 
+  FIELD* field = get_field(rb_field);
   return INT2NUM(set_current_field(form, field));
 }
 static VALUE rbncurs_m_set_current_field(VALUE dummy, VALUE rb_form, VALUE rb_field)
@@ -540,12 +553,12 @@ static VALUE rbncurs_c_free_field(VALUE rb_field) {
 }
 static VALUE rbncurs_m_free_field(VALUE dummy, VALUE rb_field)
 { return rbncurs_c_free_field(rb_field); }
- 
+
 
 /*
  * form_field_info(3x)
  */
-static VALUE rbncurs_c_field_info(VALUE rb_field, VALUE rows, VALUE cols, 
+static VALUE rbncurs_c_field_info(VALUE rb_field, VALUE rows, VALUE cols,
 											 VALUE frow, VALUE fcol, VALUE nrow, VALUE nbuf) {
   if (rb_obj_is_instance_of(rows, rb_cArray) != Qtrue
 		|| rb_obj_is_instance_of(cols, rb_cArray) != Qtrue
@@ -560,7 +573,7 @@ static VALUE rbncurs_c_field_info(VALUE rb_field, VALUE rows, VALUE cols,
   else {
 	 FIELD* field = get_field(rb_field);
 	 int vals[6] = {0,0,0,0,0,0};
-	
+
 	 int result = field_info(field, &vals[0],&vals[1],&vals[2],&vals[3],&vals[4],&vals[5]);
 	 rb_ary_push(rows, INT2NUM(vals[0]));
 	 rb_ary_push(cols, INT2NUM(vals[1]));
@@ -571,15 +584,15 @@ static VALUE rbncurs_c_field_info(VALUE rb_field, VALUE rows, VALUE cols,
 	 return INT2NUM(result);
   }
 }
-static VALUE rbncurs_m_field_info(VALUE dummy, VALUE rb_field, VALUE rows, VALUE cols, 
+static VALUE rbncurs_m_field_info(VALUE dummy, VALUE rb_field, VALUE rows, VALUE cols,
 											 VALUE frow, VALUE fcol, VALUE nrow, VALUE nbuf)
 { return rbncurs_c_field_info(rb_field, rows, cols, frow, fcol, nrow, nbuf); }
 
-static VALUE rbncurs_c_dynamic_field_info(VALUE rb_field, VALUE rows, VALUE cols, 
+static VALUE rbncurs_c_dynamic_field_info(VALUE rb_field, VALUE rows, VALUE cols,
 														VALUE max) {
   if (rb_obj_is_instance_of(rows, rb_cArray) != Qtrue
 		|| rb_obj_is_instance_of(cols, rb_cArray) != Qtrue
-		|| rb_obj_is_instance_of(max, rb_cArray) != Qtrue) {		
+		|| rb_obj_is_instance_of(max, rb_cArray) != Qtrue) {
 	 rb_raise(rb_eArgError,
 				 "rows, cols and max arguments must be empty Arrays");
 	 return Qnil;
@@ -587,7 +600,7 @@ static VALUE rbncurs_c_dynamic_field_info(VALUE rb_field, VALUE rows, VALUE cols
   else {
   	FIELD* field = get_field(rb_field);
   	int vals[3] = {0,0,0};
-	
+
   	int result = dynamic_field_info(field, &vals[0],&vals[1],&vals[2]);
 	rb_ary_push(rows, INT2NUM(vals[0]));
 	rb_ary_push(cols, INT2NUM(vals[1]));
@@ -595,7 +608,7 @@ static VALUE rbncurs_c_dynamic_field_info(VALUE rb_field, VALUE rows, VALUE cols
 	return INT2NUM(result);
   }
 }
-static VALUE rbncurs_m_dynamic_field_info(VALUE dummy, VALUE rb_field, VALUE rows, VALUE cols, 
+static VALUE rbncurs_m_dynamic_field_info(VALUE dummy, VALUE rb_field, VALUE rows, VALUE cols,
 														VALUE max)
 { return rbncurs_c_dynamic_field_info(rb_field, rows, cols, max); }
 /*
@@ -608,12 +621,12 @@ static VALUE rbncurs_c_set_field_type(int argc, VALUE* argv, VALUE rb_field) {
 
   rb_scan_args(argc, argv, "13", &rb_fieldtype, &arg3, &arg4, &arg5);
   ftype = get_fieldtype(rb_fieldtype);
-	
+
   if (ftype == TYPE_ALNUM ||
 		ftype == TYPE_ALPHA) {
 	 if (argc != 2)
 		rb_raise(rb_eArgError, "TYPE_ALNUM and TYPE_ALPHA require one additional argument");
-	 return INT2NUM(set_field_type(field, ftype, 
+	 return INT2NUM(set_field_type(field, ftype,
 											 NUM2INT(arg3)));
   }
   if (ftype == TYPE_ENUM) {
@@ -629,7 +642,7 @@ static VALUE rbncurs_c_set_field_type(int argc, VALUE* argv, VALUE rb_field) {
 		  list[i] = STR2CSTR(rb_ary_entry(arg3, (long)i));
 		}
 		list[n] = NULL;
-		return INT2NUM(set_field_type(field, ftype, 
+		return INT2NUM(set_field_type(field, ftype,
 												list,
 												RTEST(arg4),
 												RTEST(arg5)));
@@ -638,7 +651,7 @@ static VALUE rbncurs_c_set_field_type(int argc, VALUE* argv, VALUE rb_field) {
   else if (ftype == TYPE_INTEGER) {
 	 if (argc != 4)
 		rb_raise(rb_eArgError, "TYPE_INTEGER requires three additional arguments");
-	 return INT2NUM(set_field_type(field, ftype, 
+	 return INT2NUM(set_field_type(field, ftype,
 											 NUM2INT(arg3),
 											 NUM2LONG(arg4),
 											 NUM2LONG(arg5)));
@@ -646,7 +659,7 @@ static VALUE rbncurs_c_set_field_type(int argc, VALUE* argv, VALUE rb_field) {
   else if (ftype == TYPE_NUMERIC) {
 	 if (argc != 4)
 		rb_raise(rb_eArgError, "TYPE_NUMERIC requires three additional arguments");
-	 return INT2NUM(set_field_type(field, ftype, 
+	 return INT2NUM(set_field_type(field, ftype,
 											 NUM2INT(arg3),
 											 NUM2DBL(arg4),
 											 NUM2DBL(arg5)));
@@ -654,15 +667,15 @@ static VALUE rbncurs_c_set_field_type(int argc, VALUE* argv, VALUE rb_field) {
   else if (ftype == TYPE_REGEXP){
 	 if (argc != 2)
 		rb_raise(rb_eArgError, "TYPE_REGEXP requires one additional argument");
-	 return INT2NUM(set_field_type(field, ftype, 
+	 return INT2NUM(set_field_type(field, ftype,
 											 STR2CSTR(arg3)));
   }
-  else if (ftype == TYPE_IPV4){	
+  else if (ftype == TYPE_IPV4){
 	 if (argc != 1)
 		rb_raise(rb_eArgError, "TYPE_IPV4 has no additional arguments");
 	 return INT2NUM(set_field_type(field, ftype));
   }
-  else {	
+  else {
 	 /*  It is a user-defined field type. */
 	 /*  Will store the arguments associated with this field */
 	 /*  for use in the callback function. */
@@ -673,7 +686,7 @@ static VALUE rbncurs_c_set_field_type(int argc, VALUE* argv, VALUE rb_field) {
 	 /*  the block-argument used in finding the appropriate Ruby Proc */
 	 return INT2NUM(set_field_type(field, ftype, field));
   }
-	
+
 }
 static VALUE rbncurs_m_set_field_type(int argc, VALUE* argv, VALUE dummy)
 { return rbncurs_c_set_field_type(argc-1, argv+1, argv[0]); }
@@ -787,7 +800,7 @@ static VALUE rbncurs_c_set_form_fields(VALUE rb_form, VALUE rb_field_array) {
   long i;
   FORM* form = NULL;
   for (i=0; i<n; i++){
-	 fields[i] = get_field(rb_ary_entry(rb_field_array, i)); 
+	 fields[i] = get_field(rb_ary_entry(rb_field_array, i));
   }
   fields[n] = NULL;
   form = get_form(rb_form);
@@ -801,7 +814,7 @@ static VALUE rbncurs_c_form_fields(VALUE rb_form) {
   FIELD** fields = form_fields(form);
   VALUE arr = Qundef;
   int i;
-  if (fields == NULL) 
+  if (fields == NULL)
 	 rb_raise(rb_eRuntimeError, "Error retrieving form fields");
   arr = rb_ary_new();
   i=0;
@@ -843,12 +856,12 @@ static void field_init_hook(FORM* form) {
 static VALUE rbncurs_c_set_field_init(VALUE rb_form, VALUE proc) {
   FORM* form = NULL;
   if (!rb_obj_is_kind_of(rb_form, cFORM))
-	 rb_raise(rb_eArgError, "arg1 must be a FORM object");		 
+	 rb_raise(rb_eArgError, "arg1 must be a FORM object");
   if (!rb_obj_is_kind_of(proc, rb_cProc))
-	 rb_raise(rb_eArgError, "arg2 must be a Proc object");		 
+	 rb_raise(rb_eArgError, "arg2 must be a Proc object");
   form = get_form(rb_form);
   reg_proc(form, FIELD_INIT_HOOK, proc);
-  if (proc != Qnil)	{	
+  if (proc != Qnil)	{
 	 return INT2NUM(set_field_init(form, field_init_hook));
   }
   else {
@@ -876,12 +889,12 @@ static void field_term_hook(FORM* form) {
 static VALUE rbncurs_c_set_field_term(VALUE rb_form, VALUE proc) {
   FORM * form = NULL;
   if (!rb_obj_is_kind_of(rb_form, cFORM))
-	 rb_raise(rb_eArgError, "arg1 must be a FORM object");		 
+	 rb_raise(rb_eArgError, "arg1 must be a FORM object");
   if (!rb_obj_is_kind_of(proc, rb_cProc))
-	 rb_raise(rb_eArgError, "arg2 must be a Proc object");		 
+	 rb_raise(rb_eArgError, "arg2 must be a Proc object");
   form = get_form(rb_form);
   reg_proc(form, FIELD_TERM_HOOK, proc);
-  if (proc != Qnil)	{	
+  if (proc != Qnil)	{
 	 return INT2NUM(set_field_term(form, field_term_hook));
   }
   else {
@@ -909,12 +922,12 @@ static void form_init_hook(FORM* form) {
 static VALUE rbncurs_c_set_form_init(VALUE rb_form, VALUE proc) {
   FORM * form = NULL;
   if (!rb_obj_is_kind_of(rb_form, cFORM))
-	 rb_raise(rb_eArgError, "arg1 must be a FORM object");		 
+	 rb_raise(rb_eArgError, "arg1 must be a FORM object");
   if (!rb_obj_is_kind_of(proc, rb_cProc))
-	 rb_raise(rb_eArgError, "arg2 must be a Proc object");		 
+	 rb_raise(rb_eArgError, "arg2 must be a Proc object");
   form = get_form(rb_form);
   reg_proc(form, FORM_INIT_HOOK, proc);
-  if (proc != Qnil)	{	
+  if (proc != Qnil)	{
 	 return INT2NUM(set_form_init(form, form_init_hook));
   }
   else {
@@ -942,12 +955,12 @@ static void form_term_hook(FORM* form) {
 static VALUE rbncurs_c_set_form_term(VALUE rb_form, VALUE proc) {
   FORM * form = NULL;
   if (!rb_obj_is_kind_of(rb_form, cFORM))
-	 rb_raise(rb_eArgError, "arg1 must be a FORM object");		 
+	 rb_raise(rb_eArgError, "arg1 must be a FORM object");
   if (!rb_obj_is_kind_of(proc, rb_cProc))
-	 rb_raise(rb_eArgError, "arg2 must be a Proc object");		 
+	 rb_raise(rb_eArgError, "arg2 must be a Proc object");
   form = get_form(rb_form);
   reg_proc(form, FORM_TERM_HOOK, proc);
-  if (proc != Qnil)	{	
+  if (proc != Qnil)	{
 	 return INT2NUM(set_form_term(form, form_term_hook));
   }
   else {
@@ -1100,7 +1113,7 @@ static VALUE rbncurs_c_scale_form(VALUE rb_form, VALUE rows, VALUE columns) {
 	 return Qnil;
   }
   else {
-	 int vals[2] = {0,0};	
+	 int vals[2] = {0,0};
 	 int result = scale_form(form, &vals[0],&vals[1]);
 	 rb_ary_push(rows, INT2NUM(vals[0]));
 	 rb_ary_push(columns, INT2NUM(vals[1]));
@@ -1113,28 +1126,28 @@ static VALUE rbncurs_m_scale_form(VALUE dummy, VALUE rb_form, VALUE rows, VALUE 
 /*
  * form_fieldtype
  */
-static void* make_arg(va_list* ap) {	
+static void* make_arg(va_list* ap) {
   /*  This method creates a list of arguments to be passed */
   /*  to the validation functions (char_check and field_check). */
-  FIELD* field =  va_arg(*ap, FIELD*);	
+  FIELD* field =  va_arg(*ap, FIELD*);
   FIELDTYPE* fieldtype = field_type(field);
   VALUE proc = get_proc(fieldtype, FIELDTYPE_FIELD_CHECK_HOOK);
   if (proc == Qnil) {
 	 proc = get_proc(fieldtype, FIELDTYPE_CHAR_CHECK_HOOK);
   }
-	
+
   /*  Compare number of arguments in Ruby Proc with that of set_field_type */
-  if (proc != Qnil) {	
+  if (proc != Qnil) {
 	 VALUE argc = rb_funcall(proc, rb_intern("arity"),0);
 	 VALUE args = get_proc(field, FIELDTYPE_ARGS);
-	 if (args != Qnil) {		
-		if (NUM2INT(argc)-1 != rbncurs_array_length(args)) {	
+	 if (args != Qnil) {
+		if (NUM2INT(argc)-1 != rbncurs_array_length(args)) {
 		  char msg[500];
 		  snprintf(msg, 500, "The validation functions for this field type need %d additional arguments.",(int)(NUM2INT(argc)-1));
 		  msg[499]=0;
 		  rb_raise(rb_eArgError, "%s", msg);
 		}
-	 } 
+	 }
   }
   /*  field will be the only argument in field_check/char_check callback */
   /*  and will be used to locate the appropriate Ruby Proc */
@@ -1143,10 +1156,10 @@ static void* make_arg(va_list* ap) {
 static bool field_check(FIELD* field, const void* argblock) {
   FIELDTYPE* fieldtype = field_type(field);
   VALUE proc = get_proc(fieldtype, FIELDTYPE_FIELD_CHECK_HOOK);
-  if (proc != Qnil) 
+  if (proc != Qnil)
 	 {
 		VALUE args = rb_ary_dup(get_proc(field, FIELDTYPE_ARGS));
-		rb_ary_unshift(args, wrap_field(field));		
+		rb_ary_unshift(args, wrap_field(field));
 		return RTEST(rb_apply(proc, rb_intern("call"), args));
 	 }
   return 1;
@@ -1165,18 +1178,18 @@ static bool char_check(int c, const void* argblock) {
   }
   return 1;
 }
-static VALUE rbncurs_m_new_fieldtype(VALUE dummy, VALUE field_check_proc, VALUE char_check_proc) 
+static VALUE rbncurs_m_new_fieldtype(VALUE dummy, VALUE field_check_proc, VALUE char_check_proc)
 {
   FIELDTYPE* fieldtype = new_fieldtype(field_check_proc == Qnil ? NULL : field_check,
-													char_check_proc == Qnil ? NULL : char_check);	
+													char_check_proc == Qnil ? NULL : char_check);
   set_fieldtype_arg(fieldtype, make_arg, NULL, NULL);
   if (field_check_proc != Qnil) {
 	 reg_proc(fieldtype, FIELDTYPE_FIELD_CHECK_HOOK, field_check_proc);
-  }  
+  }
   if (char_check_proc != Qnil) {
 	 reg_proc(fieldtype, FIELDTYPE_CHAR_CHECK_HOOK, char_check_proc);
   }
-  
+
   return wrap_fieldtype(fieldtype);
 }
 
@@ -1208,13 +1221,13 @@ static bool prev_choice(FIELD* field, const void* argblock) {
 
 static VALUE rbncurs_c_set_fieldtype_choice(VALUE rb_fieldtype, VALUE next_choice_proc, VALUE prev_choice_proc) {
   FIELDTYPE* fieldtype = get_fieldtype(rb_fieldtype);
-  int result = set_fieldtype_choice(fieldtype, 
-												next_choice_proc == Qnil ? NULL : next_choice, 
+  int result = set_fieldtype_choice(fieldtype,
+												next_choice_proc == Qnil ? NULL : next_choice,
 												prev_choice_proc == Qnil ? NULL : prev_choice);
   if (next_choice_proc != Qnil)
 	 reg_proc(fieldtype, FIELDTYPE_NEXT_CHOICE_HOOK, next_choice_proc);
   if (prev_choice_proc != Qnil)
-	 reg_proc(fieldtype, FIELDTYPE_PREV_CHOICE_HOOK, prev_choice_proc);	
+	 reg_proc(fieldtype, FIELDTYPE_PREV_CHOICE_HOOK, prev_choice_proc);
   return INT2NUM(result);
 }
 static VALUE rbncurs_m_set_fieldtype_choice(VALUE dummy, VALUE rb_fieldtype, VALUE next_choice_proc, VALUE prev_choice_proc)
@@ -1254,9 +1267,9 @@ static VALUE rbncurs_m_pos_form_cursor(VALUE dummy, VALUE rb_form)
 
 void init_form(void)
 {
-	
+
   mForm = rb_define_module_under(mNcurses, "Form");
-	
+
   FORM_SNG_FUNC(current_field,1);
   FORM_SNG_FUNC(data_ahead,1);
   FORM_SNG_FUNC(data_behind,1);
@@ -1280,6 +1293,11 @@ void init_form(void)
   FORM_SNG_FUNC(field_type,1);
   /*   FORM_SNG_FUNC(field_userptr,1); */
   FORM_SNG_FUNC(form_driver,2);
+
+  # ifdef HAVE_FORM_DRIVER_W
+  FORM_SNG_FUNC(form_driver_w,3);
+  #endif
+
   FORM_SNG_FUNC(form_fields,1);
   FORM_SNG_FUNC(form_init,1);
   FORM_SNG_FUNC(form_opts,1);
@@ -1335,7 +1353,7 @@ void init_form(void)
   init_opts_constants();
   init_just_constants();
   init_form_opts_constants();
-	
+
   /*  Hashes to store registered blocks (Proc) */
   {
 	 VALUE hashes = rb_iv_set(mForm, "@proc_hashes", rb_ary_new());
@@ -1360,6 +1378,11 @@ void init_form(void)
   RB_CLASS_METH(cFORM, NULL, field_init,0);
   RB_CLASS_METH(cFORM, NULL, field_term,0);
   RB_CLASS_METH(cFORM, "driver", form_driver,1);
+
+  # ifdef HAVE_FORM_DRIVER_W
+  RB_CLASS_METH(cFORM, "driver_w", form_driver_w,2);
+  #endif
+
   RB_CLASS_METH(cFORM, "fields", form_fields,0);
   RB_CLASS_METH(cFORM, "init", form_init,0);
   RB_CLASS_METH(cFORM, "opts", form_opts,0);
diff --git a/form_wrap.h b/form_wrap.h
@@ -3,7 +3,7 @@
  * Contributed by Simon Kaczor <skaczor@cox.net>
  * Prognosoft Inc. <http://www.prognosoft.biz>
  * Copyright 2004
- * 
+ *
  *  This module is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
  *  License as published by the Free Software Foundation; either
@@ -20,6 +20,7 @@
  *
  * Changes
  * (C) 2011 Tobias Herzke
+ * (C) 2013 Gaute Hope <eg@gaute.vetsj.com>
  */
 
 #if !defined(FORM_HH) && (defined(HAVE_FORM_H) || defined(HAVE_NCURSESW_FORM_H))
@@ -40,7 +41,7 @@ extern VALUE cFORM;
 /*extern VALUE cPAGE;*/
 
 typedef struct {
- 		bool (* field_check)(FIELD *,const void *); 
+ 		bool (* field_check)(FIELD *,const void *);
 		FIELDTYPE* fieldtype;
 } FLDCHKFUNC;
 
diff --git a/ncurses_wrap.c b/ncurses_wrap.c
@@ -3,6 +3,7 @@
  * (C) 2002, 2003, 2004 Tobias Peters <t-peters@berlios.de>
  * (C) 2004 Simon Kaczor <skaczor@cox.net>
  * (C) 2005 2006 2009 2011 Tobias Herzke
+ * (C) 2013 Gaute Hope <eg@gaute.vetsj.com>
  *
  *  This module is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
@@ -572,6 +573,10 @@ static chtype * RB2CHSTR(VALUE array)
 static VALUE rbncurs_addch(VALUE dummy, VALUE arg1) {
     return INT2NUM(addch((int) NUM2ULONG(arg1)));
 }
+static VALUE rbncurs_add_wch(VALUE dummy, VALUE arg1) {
+  wchar_t c = NUM2ULONG(arg1);
+  return INT2NUM(add_wch((cchar_t *)&c));
+}
 static VALUE rbncurs_addchnstr(VALUE dummy, VALUE arg1, VALUE arg2) {
     chtype * chstr = RB2CHSTR(arg1);
     VALUE return_value = INT2NUM(addchnstr(chstr,  NUM2INT(arg2)));
@@ -814,7 +819,11 @@ static VALUE rbncurs_getbkgd(VALUE dummy, VALUE arg1) {
     return INT2NUM(getbkgd(get_window(arg1)));
 }
 
-static int rbncurshelper_nonblocking_wgetch(WINDOW *c_win) {
+/* typedef of a pointer to a wgetch function */
+typedef int (*wgetch_func) (WINDOW *);
+
+/* functor for getting a char nonblocking, pass getchar function */
+static int rbncurshelper_do_wgetch_functor (WINDOW *c_win, wgetch_func _wgetch_func) {
     /* nonblocking wgetch only implemented for Ncurses */
     int halfdelay = NUM2INT(rb_iv_get(mNcurses, "@halfdelay"));
     int infd = NUM2INT(rb_iv_get(mNcurses, "@infd"));
@@ -846,7 +855,7 @@ static int rbncurshelper_nonblocking_wgetch(WINDOW *c_win) {
 #if defined(NCURSES_VERSION) && defined(NCURSES_OPAQUE) && !NCURSES_OPAQUE
     c_win->_delay = 0;
 #endif
-    while (doupdate() /* detects resize */, (result = wgetch(c_win)) == ERR) {
+    while (doupdate() /* detects resize */, (result = _wgetch_func(c_win)) == ERR) {
 #ifdef HAVE_RB_THREAD_FD_SELECT
       rb_fdset_t fdsets[3];
       rb_fdset_t *rfds = NULL;
@@ -903,9 +912,34 @@ static int rbncurshelper_nonblocking_wgetch(WINDOW *c_win) {
 #endif
     return result;
 }
+
+/* non-wide char getch */
+static int rbncurshelper_nonblocking_wgetch(WINDOW *c_win) {
+  return rbncurshelper_do_wgetch_functor (c_win, &wgetch);
+}
+
+/* not thread safe: wide char getch */
+static wint_t wget_wch_back;
+static int my_wget_wch (WINDOW *c_win) {
+  return wget_wch (c_win, &wget_wch_back);
+}
+
+/* return array with first element being return key code status,
+ * and second element the key code */
+static VALUE rbncurshelper_nonblocking_wget_wch(WINDOW *c_win) {
+  int retcode = rbncurshelper_do_wgetch_functor (c_win, &my_wget_wch);
+  VALUE r = rb_assoc_new (INT2NUM(retcode), LONG2NUM(wget_wch_back));
+  return r;
+}
+
 static VALUE rbncurs_getch(VALUE dummy) {
     return INT2NUM(rbncurshelper_nonblocking_wgetch(stdscr));
 }
+
+static VALUE rbncurs_get_wch(VALUE dummy) {
+    return rbncurshelper_nonblocking_wget_wch(stdscr);
+}
+
 static VALUE rbncurs_halfdelay(VALUE dummy, VALUE arg1) {
     return INT2NUM(rbncurshelper_halfdelay_cbreak(NUM2INT(arg1), 1));
 }
@@ -1472,6 +1506,10 @@ static VALUE rbncurs_vline(VALUE dummy, VALUE arg1, VALUE arg2) {
 static VALUE rbncurs_waddch(VALUE dummy, VALUE arg1, VALUE arg2) {
     return INT2NUM(waddch(get_window(arg1),  (int) NUM2ULONG(arg2)));
 }
+static VALUE rbncurs_wadd_wch(VALUE dummy, VALUE arg1, VALUE arg2) {
+  cchar_t t = { 0, { NUM2ULONG(arg2), 0 } };
+  return INT2NUM(wadd_wch(get_window(arg1), &t));
+}
 static VALUE rbncurs_waddchnstr(VALUE dummy, VALUE arg1, VALUE arg2, VALUE arg3) {
     chtype * chstr = RB2CHSTR(arg2);
     VALUE return_value = INT2NUM(waddchnstr(get_window(arg1), chstr,
@@ -1546,6 +1584,11 @@ static VALUE rbncurs_werase(VALUE dummy, VALUE arg1) {
 static VALUE rbncurs_wgetch(VALUE dummy, VALUE arg1) {
     return INT2NUM(rbncurshelper_nonblocking_wgetch(get_window(arg1)));
 }
+
+static VALUE rbncurs_wget_wch(VALUE dummy, VALUE arg1) {
+    return rbncurshelper_nonblocking_wget_wch(get_window(arg1));
+}
+
 static VALUE rbncurs_whline(VALUE dummy, VALUE arg1, VALUE arg2, VALUE arg3) {
     return INT2NUM(whline(get_window(arg1),  (int) NUM2ULONG(arg2),  NUM2INT(arg3)));
 }
@@ -1688,6 +1731,7 @@ static VALUE rbncurs_newterm(VALUE dummy, VALUE rb_type, VALUE rb_outfd, VALUE r
 
 static void init_functions_2(void) {
     NCFUNC(addch, 1);
+    NCFUNC(add_wch, 1);
     NCFUNC(addchnstr, 2);
     NCFUNC(addchstr, 1);
     NCFUNC(addnstr, 2);
@@ -1770,6 +1814,8 @@ static void init_functions_2(void) {
     NCFUNC(flushinp, 0);
     NCFUNC(getbkgd, 1);
     NCFUNC(getch, 0);
+    NCFUNC(get_wch, 0);
+    NCFUNC(wget_wch, 1);
     NCFUNC(halfdelay, 1);
     rb_define_module_function(mNcurses, "has_colors?",
                               (&rbncurs_has_colors),
@@ -1949,6 +1995,7 @@ static void init_functions_2(void) {
 #endif
     NCFUNC(vline, 2);
     NCFUNC(waddch, 2);
+    NCFUNC(wadd_wch, 2);
     NCFUNC(waddchnstr, 3);
     NCFUNC(waddchstr, 2);
     NCFUNC(waddnstr, 3);
diff --git a/ncurses_wrap.h b/ncurses_wrap.h
@@ -3,6 +3,7 @@
  * (C) 2002, 2003 Tobias Peters <t-peters@berlios.de>
  * (C) 2004 Simon Kaczor <skaczor@cox.net>
  * (C) 2009 2011 Tobias Herzke
+ * (C) 2013 Gaute Hope <eg@gaute.vetsj.com>
  *
  *  This module is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
@@ -64,6 +65,8 @@
   - v*printw functions (but normal printw functions are supported!)
 */
 
+# define _XOPEN_SOURCE_EXTENDED
+
 #if defined(HAVE_GETWIN) || defined(HAVE_PUTWIN)
 #  ifdef HAVE_UNISTD_H
 #    include <unistd.h>