]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - form/frm_driver.c
ncurses 5.6 - patch 20080927
[ncurses.git] / form / frm_driver.c
index d550953737083fcd44b9eae145dede9ee6f4e3a9..5208113010ccdf8c39b9e287dea6e9f9d780c806 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -32,7 +32,7 @@
 
 #include "form.priv.h"
 
-MODULE_ID("$Id: frm_driver.c,v 1.78 2007/02/04 00:28:38 tom Exp $")
+MODULE_ID("$Id: frm_driver.c,v 1.87 2008/07/12 21:24:07 tom Exp $")
 
 /*----------------------------------------------------------------------------
   This is the core module of the form library. It contains the majority
@@ -676,7 +676,7 @@ Field_Grown(FIELD *field, int amount)
              new_bp[new_buflen] = myZEROS;
            }
 
-#if USE_WIDEC_SUPPORT
+#if USE_WIDEC_SUPPORT && NCURSES_EXT_FUNCS
          if (wresize(field->working, 1, Buffer_Length(field) + 1) == ERR)
            result = FALSE;
 #endif
@@ -737,6 +737,34 @@ Field_Grown(FIELD *field, int amount)
   return (result);
 }
 
+#ifdef NCURSES_MOUSE_VERSION
+/*---------------------------------------------------------------------------
+|   Facility      :  libnform
+|   Function      :  int Field_encloses(FIELD *field, int ry, int rx)
+|
+|   Description   :  Check if the given coordinates lie within the given field.
+|
+|   Return Values :  E_OK              - success
+|                    E_BAD_ARGUMENT    - invalid form pointer
+|                    E_SYSTEM_ERROR    - form has no current field or
+|                                        field-window
++--------------------------------------------------------------------------*/
+static int
+Field_encloses(FIELD *field, int ry, int rx)
+{
+  T((T_CALLED("Field_encloses(%p)"), field));
+  if (field != 0
+      && field->frow <= ry
+      && (field->frow + field->rows) > ry
+      && field->fcol <= rx
+      && (field->fcol + field->cols) > rx)
+    {
+      RETURN(E_OK);
+    }
+  RETURN(E_INVALID_FIELD);
+}
+#endif
+
 /*---------------------------------------------------------------------------
 |   Facility      :  libnform
 |   Function      :  int _nc_Position_Form_Cursor(FORM * form)
@@ -4148,7 +4176,7 @@ form_driver(FORM *form, int c)
        NULL                    /* Choice Request is generic           */
       };
       size_t nMethods = (sizeof(Generic_Methods) / sizeof(Generic_Methods[0]));
-      size_t method = ((BI->keycode & ID_Mask) >> ID_Shft) & 0xffff;
+      size_t method = (BI->keycode >> ID_Shft) & 0xffff;       /* see ID_Mask */
 
       if ((method >= nMethods) || !(BI->cmd))
        res = E_SYSTEM_ERROR;
@@ -4162,6 +4190,83 @@ form_driver(FORM *form, int c)
            res = (BI->cmd) (form);
        }
     }
+#ifdef NCURSES_MOUSE_VERSION
+  else if (KEY_MOUSE == c)
+    {
+      MEVENT event;
+      WINDOW *win = form->win ? form->win : stdscr;
+      WINDOW *sub = form->sub ? form->sub : win;
+
+      getmouse(&event);
+      if ((event.bstate & (BUTTON1_CLICKED |
+                          BUTTON1_DOUBLE_CLICKED |
+                          BUTTON1_TRIPLE_CLICKED))
+         && wenclose(win, event.y, event.x))
+       {                       /* we react only if the click was in the userwin, that means
+                                * inside the form display area or at the decoration window.
+                                */
+         int ry = event.y, rx = event.x;       /* screen coordinates */
+
+         res = E_REQUEST_DENIED;
+         if (mouse_trafo(&ry, &rx, FALSE))
+           {                   /* rx, ry are now "curses" coordinates */
+             if (ry < sub->_begy)
+               {               /* we clicked above the display region; this is
+                                * interpreted as "scroll up" request
+                                */
+                 if (event.bstate & BUTTON1_CLICKED)
+                   res = form_driver(form, REQ_PREV_FIELD);
+                 else if (event.bstate & BUTTON1_DOUBLE_CLICKED)
+                   res = form_driver(form, REQ_PREV_PAGE);
+                 else if (event.bstate & BUTTON1_TRIPLE_CLICKED)
+                   res = form_driver(form, REQ_FIRST_FIELD);
+               }
+             else if (ry > sub->_begy + sub->_maxy)
+               {               /* we clicked below the display region; this is
+                                * interpreted as "scroll down" request
+                                */
+                 if (event.bstate & BUTTON1_CLICKED)
+                   res = form_driver(form, REQ_NEXT_FIELD);
+                 else if (event.bstate & BUTTON1_DOUBLE_CLICKED)
+                   res = form_driver(form, REQ_NEXT_PAGE);
+                 else if (event.bstate & BUTTON1_TRIPLE_CLICKED)
+                   res = form_driver(form, REQ_LAST_FIELD);
+               }
+             else if (wenclose(sub, event.y, event.x))
+               {               /* Inside the area we try to find the hit item */
+                 int i;
+
+                 ry = event.y;
+                 rx = event.x;
+                 if (wmouse_trafo(sub, &ry, &rx, FALSE))
+                   {
+                     int min_field = form->page[form->curpage].pmin;
+                     int max_field = form->page[form->curpage].pmax;
+
+                     for (i = min_field; i <= max_field; ++i)
+                       {
+                         FIELD *field = form->field[i];
+
+                         if (Field_Is_Selectable(field)
+                             && Field_encloses(field, ry, rx) == E_OK)
+                           {
+                             res = _nc_Set_Current_Field(form, field);
+                             if (res == E_OK)
+                               res = _nc_Position_Form_Cursor(form);
+                             if (res == E_OK
+                                 && (event.bstate & BUTTON1_DOUBLE_CLICKED))
+                               res = E_UNKNOWN_COMMAND;
+                             break;
+                           }
+                       }
+                   }
+               }
+           }
+       }
+      else
+       res = E_REQUEST_DENIED;
+    }
+#endif /* NCURSES_MOUSE_VERSION */
   else if (!(c & (~(int)MAX_REGULAR_CHARACTER)))
     {
       /*
@@ -4274,7 +4379,7 @@ set_field_buffer(FIELD *field, int buffer, const char *value)
   wclear(field->working);
   mvwaddstr(field->working, 0, 0, value);
 
-  if ((widevalue = (FIELD_CELL *)calloc(len, sizeof(FIELD_CELL))) == 0)
+  if ((widevalue = typeCalloc(FIELD_CELL, len + 1)) == 0)
     {
       RETURN(E_SYSTEM_ERROR);
     }
@@ -4354,10 +4459,7 @@ field_buffer(const FIELD *field, int buffer)
              init_mb(state);
              next = _nc_wcrtomb(0, data[n].chars[0], &state);
              if (!isEILSEQ(next))
-               {
-                 if (next != 0)
-                   need += next;
-               }
+               need += next;
            }
        }
 
@@ -4371,7 +4473,7 @@ field_buffer(const FIELD *field, int buffer)
        {
          wclear(field->working);
          mvwadd_wchnstr(field->working, 0, 0, data, size);
-         mvwinnstr(field->working, 0, 0, result, (int)need + 1);
+         mvwinnstr(field->working, 0, 0, result, (int)need);
        }
 #else
       result = Address_Of_Nth_Buffer(field, buffer);