frm_driver.c 138 KB
Newer Older
Berk Geveci's avatar
Berk Geveci committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/****************************************************************************
 * Copyright (c) 1998 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            *
 * "Software"), to deal in the Software without restriction, including      *
 * without limitation the rights to use, copy, modify, merge, publish,      *
 * distribute, distribute with modifications, sublicense, and/or sell       *
 * copies of the Software, and to permit persons to whom the Software is    *
 * furnished to do so, subject to the following conditions:                 *
 *                                                                          *
 * The above copyright notice and this permission notice shall be included  *
 * in all copies or substantial portions of the Software.                   *
 *                                                                          *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
 *                                                                          *
 * Except as contained in this notice, the name(s) of the above copyright   *
 * holders shall not be used in advertising or otherwise to promote the     *
 * sale, use or other dealings in this Software without prior written       *
 * authorization.                                                           *
 ****************************************************************************/

/****************************************************************************
 *   Author: Juergen Pfeifer <juergen.pfeifer@gmx.net> 1995,1997            *
 ****************************************************************************/
Berk Geveci's avatar
Berk Geveci committed
32
33
34
#if defined(__hpux)
 #define _XOPEN_SOURCE_EXTENDED
#endif
Berk Geveci's avatar
Berk Geveci committed
35
#include "form.priv.h"
Berk Geveci's avatar
Berk Geveci committed
36
37
38
#if defined(__hpux)
 #undef _XOPEN_SOURCE_EXTENDED
#endif
Berk Geveci's avatar
Berk Geveci committed
39

40
/* AIX seems to define this */
41
42
43
#undef lines
#undef columns

Berk Geveci's avatar
Berk Geveci committed
44
45
MODULE_ID("$Id$")

46
47
/* These declarations are missing from curses.h on some platforms.  */
extern int winnstr(WINDOW *, char *, int);
48
#if defined(__DECCXX_VER) || (defined(__GNUC__) && defined(__osf__))
49
50
51
52
53
54
55
56
57
58
59
60
extern int waddnstr(WINDOW *,const char *const,int);
extern void wbkgdset(WINDOW *,chtype);
#ifndef untouchwin
extern int untouchwin(WINDOW *);
#endif
extern void wcursyncup(WINDOW *);
extern int copywin(const WINDOW*,WINDOW*,int,int,int,int,int,int,int);
extern bool is_linetouched(WINDOW *,int);
extern void wsyncup(WINDOW *);
extern WINDOW *derwin(WINDOW *,int,int,int,int);
extern int winsnstr(WINDOW *, const char *,int);
extern int winsdelln(WINDOW *,int);
61
#endif
62

Berk Geveci's avatar
Berk Geveci committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
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
/*----------------------------------------------------------------------------
  This is the core module of the form library. It contains the majority
  of the driver routines as well as the form_driver function. 

  Essentially this module is nearly the whole library. This is because
  all the functions in this module depends on some others in the module,
  so it makes no sense to split them into separate files because they
  will always be linked together. The only acceptable concern is turnaround
  time for this module, but now we have all Pentiums or Riscs, so what!

  The driver routines are grouped into nine generic categories:

   a)   Page Navigation            ( all functions prefixed by PN_ )
        The current page of the form is left and some new page is
        entered.
   b)   Inter-Field Navigation     ( all functions prefixed by FN_ )
        The current field of the form is left and some new field is
        entered.
   c)   Intra-Field Navigation     ( all functions prefixed by IFN_ )
        The current position in the current field is changed. 
   d)   Vertical Scrolling         ( all functions prefixed by VSC_ )
        Esseantially this is a specialization of Intra-Field navigation.
        It has to check for a multi-line field.
   e)   Horizontal Scrolling       ( all functions prefixed by HSC_ )
        Esseantially this is a specialization of Intra-Field navigation.
        It has to check for a single-line field.
   f)   Field Editing              ( all functions prefixed by FE_ )
        The content of the current field is changed
   g)   Edit Mode requests         ( all functions prefixed by EM_ )
        Switching between insert and overlay mode
   h)   Field-Validation requests  ( all functions prefixed by FV_ )
        Perform verifications of the field.
   i)   Choice requests            ( all functions prefixed by CR_ )
        Requests to enumerate possible field values
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Some remarks on the placements of assert() macros :
  I use them only on "strategic" places, i.e. top level entries where
  I want to make sure that things are set correctly. Throughout subordinate
  routines I omit them mostly.
  --------------------------------------------------------------------------*/

/*
Some options that may effect compatibility in behavior to SVr4 forms,
but they are here to allow a more intuitive and user friendly behaviour of
our form implementation. This doesn't affect the API, so we feel it is
uncritical.

The initial implementation tries to stay very close with the behaviour
of the original SVr4 implementation, although in some areas it is quite
clear that this isn't the most appropriate way. As far as possible this
sources will allow you to build a forms lib that behaves quite similar
to SVr4, but now and in the future we will give you better options. 
Perhaps at some time we will make this configurable at runtime.
*/

/* Implement a more user-friendly previous/next word behaviour */
#define FRIENDLY_PREV_NEXT_WORD (1)
/* Fix the wrong behaviour for forms with all fields inactive */
#define FIX_FORM_INACTIVE_BUG (1)
/* Allow dynamic field growth also when navigating past the end */
#define GROW_IF_NAVIGATE (1)

/*----------------------------------------------------------------------------
  Forward references to some internally used static functions
  --------------------------------------------------------------------------*/
static int Inter_Field_Navigation ( int (* const fct) (FORM *), FORM * form );
static int FN_Next_Field (FORM * form);
static int FN_Previous_Field (FORM * form);
static int FE_New_Line(FORM *);
static int FE_Delete_Previous(FORM *);

/*----------------------------------------------------------------------------
  Macro Definitions.

  Some Remarks on that: I use the convention to use UPPERCASE for constants
  defined by Macros. If I provide a macro as a kind of inline routine to
  provide some logic, I use my Upper_Lower case style.
  --------------------------------------------------------------------------*/

/* Calculate the position of a single row in a field buffer */
#define Position_Of_Row_In_Buffer(field,row) ((row)*(field)->dcols)

/* Calculate start address for the fields buffer# N */
#define Address_Of_Nth_Buffer(field,N) \
  ((field)->buf + (N)*(1+Buffer_Length(field)))

/* Calculate the start address of the row in the fields specified buffer# N */
#define Address_Of_Row_In_Nth_Buffer(field,N,row) \
  (Address_Of_Nth_Buffer(field,N) + Position_Of_Row_In_Buffer(field,row))

/* Calculate the start address of the row in the fields primary buffer */
#define Address_Of_Row_In_Buffer(field,row) \
  Address_Of_Row_In_Nth_Buffer(field,0,row)

/* Calculate the start address of the row in the forms current field
   buffer# N */
#define Address_Of_Current_Row_In_Nth_Buffer(form,N) \
   Address_Of_Row_In_Nth_Buffer((form)->current,N,(form)->currow)

/* Calculate the start address of the row in the forms current field
   primary buffer */
#define Address_Of_Current_Row_In_Buffer(form) \
   Address_Of_Current_Row_In_Nth_Buffer(form,0)

/* Calculate the address of the cursor in the forms current field
   primary buffer */
#define Address_Of_Current_Position_In_Nth_Buffer(form,N) \
   (Address_Of_Current_Row_In_Nth_Buffer(form,N) + (form)->curcol)

/* Calculate the address of the cursor in the forms current field
   buffer# N */
#define Address_Of_Current_Position_In_Buffer(form) \
  Address_Of_Current_Position_In_Nth_Buffer(form,0)

/* Logic to decide wether or not a field is actually a field with
   vertical or horizontal scrolling */
#define Is_Scroll_Field(field)          \
   (((field)->drows > (field)->rows) || \
    ((field)->dcols > (field)->cols))

/* Logic to decide whether or not a field needs to have an individual window
   instead of a derived window because it contains invisible parts.
   This is true for non-public fields and for scrollable fields. */
#define Has_Invisible_Parts(field)     \
  (!((field)->opts & O_PUBLIC)      || \
   Is_Scroll_Field(field))

/* Logic to decide whether or not a field needs justification */
#define Justification_Allowed(field)        \
   (((field)->just != NO_JUSTIFICATION)  && \
    (Single_Line_Field(field))           && \
    (((field)->dcols == (field)->cols)   && \
    ((field)->opts & O_STATIC))             )

/* Logic to determine whether or not a dynamic field may still grow */
#define Growable(field) ((field)->status & _MAY_GROW)

/* Macro to set the attributes for a fields window */
#define Set_Field_Window_Attributes(field,win) \
(  wbkgdset((win),(chtype)((field)->pad | (field)->back)), \
   wattrset((win),(field)->fore) )

/* Logic to decide whether or not a field really appears on the form */
#define Field_Really_Appears(field)         \
  ((field->form)                          &&\
   (field->form->status & _POSTED)        &&\
   (field->opts & O_VISIBLE)              &&\
   (field->page == field->form->curpage))

/* Logic to determine whether or not we are on the first position in the
   current field */
#define First_Position_In_Current_Field(form) \
  (((form)->currow==0) && ((form)->curcol==0))


#define Minimum(a,b) (((a)<=(b)) ? (a) : (b))
#define Maximum(a,b) (((a)>=(b)) ? (a) : (b))

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static char *Get_Start_Of_Data(char * buf, int blen)
|   
|   Description   :  Return pointer to first non-blank position in buffer.
|                    If buffer is empty return pointer to buffer itself.
|
|   Return Values :  Pointer to first non-blank position in buffer
+--------------------------------------------------------------------------*/
INLINE static char *Get_Start_Of_Data(char * buf, int blen)
{
  char *p   = buf;
  char *end = &buf[blen];

  assert(buf && blen>=0);
  while( (p < end) && is_blank(*p) ) 
    p++;
  return( (p==end) ? buf : p );
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static char *After_End_Of_Data(char * buf, int blen)
|   
|   Description   :  Return pointer after last non-blank position in buffer.
|                    If buffer is empty, return pointer to buffer itself.
|
|   Return Values :  Pointer to position after last non-blank position in 
|                    buffer.
+--------------------------------------------------------------------------*/
INLINE static char *After_End_Of_Data(char * buf,int blen)
{
  char *p   = &buf[blen];
  
  assert(buf && blen>=0);
  while( (p>buf) && is_blank(p[-1]) ) 
    p--;
  return( p );
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static char *Get_First_Whitespace_Character(
|                                     char * buf, int   blen)
|   
|   Description   :  Position to the first whitespace character.
|
|   Return Values :  Pointer to first whitespace character in buffer.
+--------------------------------------------------------------------------*/
INLINE static char *Get_First_Whitespace_Character(char * buf, int blen)
{
  char *p   = buf;
  char *end = &p[blen];
  
  assert(buf && blen>=0);
  while( (p < end) && !is_blank(*p)) 
    p++;
  return( (p==end) ? buf : p );
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static char *After_Last_Whitespace_Character(
|                                     char * buf, int blen)
|   
|   Description   :  Get the position after the last whitespace character.
|
|   Return Values :  Pointer to position after last whitespace character in 
|                    buffer.
+--------------------------------------------------------------------------*/
INLINE static char *After_Last_Whitespace_Character(char * buf, int blen)
{
  char *p   = &buf[blen];
  
  assert(buf && blen>=0);
  while( (p>buf) && !is_blank(p[-1]) ) 
    p--;
  return( p );
}

/* Set this to 1 to use the div_t version. This is a good idea if your
   compiler has an intrinsic div() support. Unfortunately GNU-C has it
   not yet. 
   N.B.: This only works if form->curcol follows immediately form->currow
         and both are of type int. 
*/
#define USE_DIV_T (0)

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static void Adjust_Cursor_Position(
|                                       FORM * form, const char * pos)
|   
|   Description   :  Set current row and column of the form to values 
|                    corresponding to the buffer position.
|
|   Return Values :  -
+--------------------------------------------------------------------------*/
INLINE static void Adjust_Cursor_Position(FORM * form, const char * pos)
{
  FIELD *field;
  int idx;

  field = form->current;
  assert( pos >= field->buf && field->dcols > 0);
  idx = (int)( pos - field->buf );
#if USE_DIV_T
  *((div_t *)&(form->currow)) = div(idx,field->dcols);
#else
  form->currow = idx / field->dcols;
  form->curcol = idx - field->cols * form->currow;
#endif  
  if ( field->drows < form->currow )
    form->currow = 0;
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static void Buffer_To_Window(
|                                      const FIELD  * field,
|                                      WINDOW * win)
|   
|   Description   :  Copy the buffer to the window. If its a multiline
|                    field, the buffer is split to the lines of the
|                    window without any editing.
|
|   Return Values :  -
+--------------------------------------------------------------------------*/
static void Buffer_To_Window(const FIELD  * field, WINDOW * win)
{
  int width, height;
  int len;
  int row;
  char *pBuffer;

  assert(win && field);

  width  = getmaxx(win);
  height = getmaxy(win);

  for(row=0, pBuffer=field->buf; 
      row < height; 
      row++, pBuffer += width )
    {
      if ((len = (int)( After_End_Of_Data( pBuffer, width ) - pBuffer )) > 0)
368
369
370
371
372
        {
          wmove( win, row, 0 );
          waddnstr( win, pBuffer, len );
        }
    }   
Berk Geveci's avatar
Berk Geveci committed
373
374
375
376
377
378
379
380
381
382
383
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
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static void Window_To_Buffer(
|                                          WINDOW * win,
|                                          FIELD  * field)
|   
|   Description   :  Copy the content of the window into the buffer.
|                    The multiple lines of a window are simply
|                    concatenated into the buffer. Pad characters in
|                    the window will be replaced by blanks in the buffer.
|
|   Return Values :  -
+--------------------------------------------------------------------------*/
static void Window_To_Buffer(WINDOW * win, FIELD  * field)
{
  int pad;
  int len = 0;
  char *p;
  int row, height;
  
  assert(win && field && field->buf );

  pad = field->pad;
  p = field->buf;
  height = getmaxy(win);

  for(row=0; (row < height) && (row < field->drows); row++ )
    {
      wmove( win, row, 0 );
      len += winnstr( win, p+len, field->dcols );
    }
  p[len] = '\0';

  /* replace visual padding character by blanks in buffer */
  if (pad != C_BLANK)
    {
      int i;
      for(i=0; i<len; i++, p++)
413
414
415
416
        {
          if (*p==pad) 
            *p = C_BLANK;
        }
Berk Geveci's avatar
Berk Geveci committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static void Synchronize_Buffer(FORM * form)
|   
|   Description   :  If there was a change, copy the content of the
|                    window into the buffer, so the buffer is synchronized
|                    with the windows content. We have to indicate that the
|                    buffer needs validation due to the change.
|
|   Return Values :  -
+--------------------------------------------------------------------------*/
INLINE static void Synchronize_Buffer(FORM * form)
{
  if (form->status & _WINDOW_MODIFIED)
    {
      form->status &= ~_WINDOW_MODIFIED;
      form->status |=  _FCHECK_REQUIRED;
      Window_To_Buffer(form->w,form->current);
      wmove(form->w,form->currow,form->curcol);
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static bool Field_Grown( FIELD *field, int amount)
|   
|   Description   :  This function is called for growable dynamic fields
|                    only. It has to increase the buffers and to allocate
|                    a new window for this field.
|                    This function has the side effect to set a new
|                    field-buffer pointer, the dcols and drows values
|                    as well as a new current Window for the field.
|
|   Return Values :  TRUE     - field successfully increased
|                    FALSE    - there was some error
+--------------------------------------------------------------------------*/
static bool Field_Grown(FIELD * field, int amount)
{
  bool result = FALSE;

  if (field && Growable(field))
    {
      bool single_line_field = Single_Line_Field(field);
      int old_buflen = Buffer_Length(field);
      int new_buflen;
      int old_dcols = field->dcols;
      int old_drows = field->drows;
      char *oldbuf  = field->buf;
      char *newbuf;

      int growth;
      FORM *form = field->form;
      bool need_visual_update = ((form != (FORM *)0)      &&
473
474
                                 (form->status & _POSTED) &&
                                 (form->current==field));
Berk Geveci's avatar
Berk Geveci committed
475
476
      
      if (need_visual_update)
477
        Synchronize_Buffer(form);
Berk Geveci's avatar
Berk Geveci committed
478
479
      
      if (single_line_field)
480
481
482
483
484
485
486
487
        {
          growth = field->cols * amount;
          if (field->maxgrow)
            growth = Minimum(field->maxgrow - field->dcols,growth);
          field->dcols += growth;
          if (field->dcols == field->maxgrow)
            field->status &= ~_MAY_GROW;
        }
Berk Geveci's avatar
Berk Geveci committed
488
      else
489
490
491
492
493
494
495
496
        {
          growth = (field->rows + field->nrow) * amount;
          if (field->maxgrow)
            growth = Minimum(field->maxgrow - field->drows,growth);
          field->drows += growth;
          if (field->drows == field->maxgrow)
            field->status &= ~_MAY_GROW;
        }
Berk Geveci's avatar
Berk Geveci committed
497
498
499
500
      /* drows, dcols changed, so we get really the new buffer length */
      new_buflen = Buffer_Length(field);
      newbuf=(char *)malloc((size_t)Total_Buffer_Size(field));
      if (!newbuf)
501
502
503
504
505
506
507
508
        { /* restore to previous state */
          field->dcols = old_dcols;
          field->drows = old_drows;
          if (( single_line_field && (field->dcols!=field->maxgrow)) ||
              (!single_line_field && (field->drows!=field->maxgrow)))
            field->status |= _MAY_GROW;
          return FALSE;
        }
Berk Geveci's avatar
Berk Geveci committed
509
      else
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
        { /* Copy all the buffers. This is the reason why we can't
             just use realloc().
             */
          int i;
          char *old_bp;
          char *new_bp;
          
          field->buf = newbuf;
          for(i=0;i<=field->nbuf;i++)
            {
              new_bp = Address_Of_Nth_Buffer(field,i);
              old_bp = oldbuf + i*(1+old_buflen);
              memcpy(new_bp,old_bp,(size_t)old_buflen);
              if (new_buflen > old_buflen)
                memset(new_bp + old_buflen,C_BLANK,
                       (size_t)(new_buflen - old_buflen));
              *(new_bp + new_buflen) = '\0';
            }

          if (need_visual_update)
            {         
              WINDOW *new_window = newpad(field->drows,field->dcols);
              if (!new_window)
                { /* restore old state */
                  field->dcols = old_dcols;
                  field->drows = old_drows;
                  field->buf   = oldbuf;
                  if (( single_line_field              && 
                        (field->dcols!=field->maxgrow)) ||
                      (!single_line_field              && 
                       (field->drows!=field->maxgrow)))
                    field->status |= _MAY_GROW;
                  free( newbuf );
                  return FALSE;
                }
              assert(form!=(FORM *)0);
              delwin(form->w);
              form->w = new_window;
              Set_Field_Window_Attributes(field,form->w);
              werase(form->w);
              Buffer_To_Window(field,form->w);
              untouchwin(form->w);
              wmove(form->w,form->currow,form->curcol);
            }

          free(oldbuf);
          /* reflect changes in linked fields */
          if (field != field->link)
            {
              FIELD *linked_field;
              for(linked_field = field->link;
                  linked_field!= field;
                  linked_field = linked_field->link)
                {
                  linked_field->buf   = field->buf;
                  linked_field->drows = field->drows;
                  linked_field->dcols = field->dcols;
                }
            }
          result = TRUE;
        }       
Berk Geveci's avatar
Berk Geveci committed
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
    }
  return(result);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  int _nc_Position_Form_Cursor(FORM * form)
|   
|   Description   :  Position the cursor in the window for the current
|                    field to be in sync. with the currow and curcol 
|                    values.
|
|   Return Values :  E_OK              - success
|                    E_BAD_ARGUMENT    - invalid form pointer
|                    E_SYSTEM_ERROR    - form has no current field or
|                                        field-window
+--------------------------------------------------------------------------*/
int
_nc_Position_Form_Cursor(FORM * form)
{
  FIELD  *field;
  WINDOW *formwin;
  
  if (!form)
    return(E_BAD_ARGUMENT);

  if (!form->w || !form->current) 
    return(E_SYSTEM_ERROR);

  field    = form->current;
  formwin  = Get_Form_Window(form);

  wmove( form->w, form->currow, form->curcol );
  if ( Has_Invisible_Parts(field) )
    {
      /* in this case fieldwin isn't derived from formwin, so we have
607
         to move the cursor in formwin by hand... */
Berk Geveci's avatar
Berk Geveci committed
608
      wmove(formwin,
609
610
            field->frow + form->currow - form->toprow,
            field->fcol + form->curcol - form->begincol);
Berk Geveci's avatar
Berk Geveci committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
      wcursyncup(formwin);
    }
  else 
    wcursyncup(form->w);
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  int _nc_Refresh_Current_Field(FORM * form)
|   
|   Description   :  Propagate the changes in the fields window to the
|                    window of the form.
|
|   Return Values :  E_OK              - on success
|                    E_BAD_ARGUMENT    - invalid form pointer
|                    E_SYSTEM_ERROR    - general error
+--------------------------------------------------------------------------*/
int
_nc_Refresh_Current_Field(FORM * form)
{
  WINDOW *formwin;
  FIELD  *field;

  if (!form)
    RETURN(E_BAD_ARGUMENT);

  if (!form->w || !form->current) 
    RETURN(E_SYSTEM_ERROR);

  field    = form->current;
  formwin  = Get_Form_Window(form);

  if (field->opts & O_PUBLIC)
    {
      if (Is_Scroll_Field(field))
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
        {
          /* Again, in this case the fieldwin isn't derived from formwin,
             so we have to perform a copy operation. */
          if (Single_Line_Field(field))
            { /* horizontal scrolling */
              if (form->curcol < form->begincol)
                  form->begincol = form->curcol;
              else
                {
                  if (form->curcol >= (form->begincol + field->cols))
                      form->begincol = form->curcol - field->cols + 1;
                }
              copywin(form->w,
                      formwin,
                      0,
                      form->begincol,
                      field->frow,
                      field->fcol,
                      field->frow,
                      field->cols + field->fcol - 1,
                      0);
            }
          else
            { /* A multiline, i.e. vertical scrolling field */
              int row_after_bottom,first_modified_row,first_unmodified_row;

              if (field->drows > field->rows)
                {
                  row_after_bottom = form->toprow + field->rows;
                  if (form->currow < form->toprow)
                    {
                      form->toprow = form->currow;
                      field->status |= _NEWTOP;
                    }
                  if (form->currow >= row_after_bottom)
                    {
                      form->toprow = form->currow - field->rows + 1;
                      field->status |= _NEWTOP;
                    }
                  if (field->status & _NEWTOP)
                    { /* means we have to copy whole range */
                      first_modified_row = form->toprow;
                      first_unmodified_row = first_modified_row + field->rows;
                      field->status &= ~_NEWTOP;
                    }
                  else 
                    { /* we try to optimize : finding the range of touched
Berk Geveci's avatar
Berk Geveci committed
694
                         lines */
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
                      first_modified_row = form->toprow;
                      while(first_modified_row < row_after_bottom)
                        {
                          if (is_linetouched(form->w,first_modified_row)) 
                            break;
                          first_modified_row++;
                        }
                      first_unmodified_row = first_modified_row;
                      while(first_unmodified_row < row_after_bottom)
                        {
                          if (!is_linetouched(form->w,first_unmodified_row)) 
                            break;
                          first_unmodified_row++;
                        }
                    }
                }
              else
                {
                  first_modified_row   = form->toprow;
                  first_unmodified_row = first_modified_row + field->rows;
                }
              if (first_unmodified_row != first_modified_row)
                copywin(form->w,
                        formwin,
                        first_modified_row,
                        0,
                        field->frow + first_modified_row - form->toprow,
                        field->fcol,
                        field->frow + first_unmodified_row - form->toprow - 1,
                        field->cols + field->fcol - 1,
                        0);
            }
          wsyncup(formwin);
        }
Berk Geveci's avatar
Berk Geveci committed
729
      else
730
731
732
733
734
        { /* if the field-window is simply a derived window, i.e. contains
             no invisible parts, the whole thing is trivial 
          */
          wsyncup(form->w);
        }
Berk Geveci's avatar
Berk Geveci committed
735
736
737
738
    }
  untouchwin(form->w);
  return _nc_Position_Form_Cursor(form);
}
739
        
Berk Geveci's avatar
Berk Geveci committed
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static void Perform_Justification(
|                                        FIELD  * field,
|                                        WINDOW * win)
|   
|   Description   :  Output field with requested justification 
|
|   Return Values :  -
+--------------------------------------------------------------------------*/
static void Perform_Justification(FIELD  * field, WINDOW * win)
{
  char *bp;
  int len;
  int col  = 0;

  bp  = Get_Start_Of_Data(field->buf,Buffer_Length(field));
  len = (int)(After_End_Of_Data(field->buf,Buffer_Length(field)) - bp);

  if (len>0)
    {
      assert(win && (field->drows == 1) && (field->dcols == field->cols));

      switch(field->just)
764
765
766
767
768
769
770
771
772
773
774
775
        {
        case JUSTIFY_LEFT:
          break;
        case JUSTIFY_CENTER:
          col = (field->cols - len)/2;
          break;
        case JUSTIFY_RIGHT:
          col = field->cols - len;
          break;
        default:
          break;
        }
Berk Geveci's avatar
Berk Geveci committed
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802

      wmove(win,0,col);
      waddnstr(win,bp,len);
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static void Undo_Justification(
|                                     FIELD  * field,
|                                     WINDOW * win)
|   
|   Description   :  Display field without any justification, i.e.
|                    left justified
|
|   Return Values :  -
+--------------------------------------------------------------------------*/
static void Undo_Justification(FIELD  * field, WINDOW * win)
{
  char *bp;
  int len;

  bp  = Get_Start_Of_Data(field->buf,Buffer_Length(field));
  len = (int)(After_End_Of_Data(field->buf,Buffer_Length(field))-bp);

  if (len>0)
    {
803
      assert(win != 0);
Berk Geveci's avatar
Berk Geveci committed
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
      wmove(win,0,0);
      waddnstr(win,bp,len);
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static bool Check_Char(
|                                           FIELDTYPE * typ,
|                                           int ch,
|                                           TypeArgument *argp)
|   
|   Description   :  Perform a single character check for character ch
|                    according to the fieldtype instance.  
|
|   Return Values :  TRUE             - Character is valid
|                    FALSE            - Character is invalid
+--------------------------------------------------------------------------*/
static bool Check_Char(FIELDTYPE * typ, int ch, TypeArgument *argp)
{
  if (typ) 
    {
      if (typ->status & _LINKED_TYPE)
827
828
829
830
831
832
        {
          assert(argp != 0);
          return(
            Check_Char(typ->left ,ch,argp->left ) ||
            Check_Char(typ->right,ch,argp->right) );
        } 
Berk Geveci's avatar
Berk Geveci committed
833
      else 
834
835
836
837
        {
          if (typ->ccheck)
            return typ->ccheck(ch,(void *)argp);
        }
Berk Geveci's avatar
Berk Geveci committed
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
    }
  return (isprint((unsigned char)ch) ? TRUE : FALSE);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int Display_Or_Erase_Field(
|                                           FIELD * field,
|                                           bool bEraseFlag)
|   
|   Description   :  Create a subwindow for the field and display the
|                    buffer contents (apply justification if required)
|                    or simply erase the field.
|
|   Return Values :  E_OK           - on success
|                    E_SYSTEM_ERROR - some error (typical no memory)
+--------------------------------------------------------------------------*/
static int Display_Or_Erase_Field(FIELD * field, bool bEraseFlag)
{
  WINDOW *win;
  WINDOW *fwin;

  if (!field)
    return E_SYSTEM_ERROR;

  fwin = Get_Form_Window(field->form);
  win  = derwin(fwin,
865
                field->rows,field->cols,field->frow,field->fcol);
Berk Geveci's avatar
Berk Geveci committed
866
867
868
869
870
871

  if (!win) 
    return E_SYSTEM_ERROR;
  else
    {
      if (field->opts & O_VISIBLE)
872
        Set_Field_Window_Attributes(field,win);
Berk Geveci's avatar
Berk Geveci committed
873
      else
874
        wattrset(win,getattrs(fwin));
Berk Geveci's avatar
Berk Geveci committed
875
876
877
878
879
880
      werase(win);
    }

  if (!bEraseFlag)
    {
      if (field->opts & O_PUBLIC)
881
882
883
884
885
886
        {
          if (Justification_Allowed(field))
            Perform_Justification(field,win);
          else
            Buffer_To_Window(field,win);
        }
Berk Geveci's avatar
Berk Geveci committed
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
      field->status &= ~_NEWTOP;
    }
  wsyncup(win);
  delwin(win);
  return E_OK;
}

/* Macros to preset the bEraseFlag */
#define Display_Field(field) Display_Or_Erase_Field(field,FALSE)
#define Erase_Field(field)   Display_Or_Erase_Field(field,TRUE)

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int Synchronize_Field(FIELD * field)
|   
|   Description   :  Synchronize the windows content with the value in
|                    the buffer.
|
|   Return Values :  E_OK                - success
|                    E_BAD_ARGUMENT      - invalid field pointer 
|                    E_SYSTEM_ERROR      - some severe basic error
+--------------------------------------------------------------------------*/
static int Synchronize_Field(FIELD * field)
{
  FORM *form;
  int res = E_OK;

  if (!field)
    return(E_BAD_ARGUMENT);

  if (((form=field->form) != (FORM *)0)
      && Field_Really_Appears(field))
    {
      if (field == form->current)
921
922
923
        { 
          form->currow  = form->curcol = form->toprow = form->begincol = 0;
          werase(form->w);
Berk Geveci's avatar
Berk Geveci committed
924
      
925
926
927
928
929
930
931
932
          if ( (field->opts & O_PUBLIC) && Justification_Allowed(field) )
            Undo_Justification( field, form->w );
          else
            Buffer_To_Window( field, form->w );
          
          field->status |= _NEWTOP;
          res = _nc_Refresh_Current_Field( form );
        }
Berk Geveci's avatar
Berk Geveci committed
933
      else
934
        res = Display_Field( field );
Berk Geveci's avatar
Berk Geveci committed
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
    }
  field->status |= _CHANGED;
  return(res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int Synchronize_Linked_Fields(FIELD * field)
|   
|   Description   :  Propagate the Synchronize_Field function to all linked
|                    fields. The first error that occurs in the sequence
|                    of updates is the returnvalue.
|
|   Return Values :  E_OK                - success
|                    E_BAD_ARGUMENT      - invalid field pointer 
|                    E_SYSTEM_ERROR      - some severe basic error
+--------------------------------------------------------------------------*/
static int Synchronize_Linked_Fields(FIELD * field)
{
  FIELD *linked_field;
  int res = E_OK;
  int syncres;

  if (!field)
    return(E_BAD_ARGUMENT);

  if (!field->link)
    return(E_SYSTEM_ERROR);

  for(linked_field = field->link; 
      linked_field!= field;
      linked_field = linked_field->link )
    {
      if (((syncres=Synchronize_Field(linked_field)) != E_OK) &&
969
970
          (res==E_OK))
        res = syncres;
Berk Geveci's avatar
Berk Geveci committed
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
    }
  return(res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  int _nc_Synchronize_Attributes(FIELD * field)
|   
|   Description   :  If a fields visual attributes have changed, this
|                    routine is called to propagate those changes to the
|                    screen.  
|
|   Return Values :  E_OK             - success
|                    E_BAD_ARGUMENT   - invalid field pointer
|                    E_SYSTEM_ERROR   - some severe basic error
+--------------------------------------------------------------------------*/
int _nc_Synchronize_Attributes(FIELD * field)
{
  FORM *form;
  int res = E_OK;
  WINDOW *formwin;

  if (!field)
    return(E_BAD_ARGUMENT);

  if (((form=field->form) != (FORM *)0)
      && Field_Really_Appears(field))
    {    
      if (form->current==field)
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
        {
          Synchronize_Buffer(form);
          Set_Field_Window_Attributes(field,form->w);
          werase(form->w);
          if (field->opts & O_PUBLIC)
            {
              if (Justification_Allowed(field))
                Undo_Justification(field,form->w);
              else 
                Buffer_To_Window(field,form->w);
            }
          else 
            {
              formwin = Get_Form_Window(form); 
              copywin(form->w,formwin,
                      0,0,
                      field->frow,field->fcol,
                      field->rows-1,field->cols-1,0);
              wsyncup(formwin);
              Buffer_To_Window(field,form->w);
              field->status |= _NEWTOP; /* fake refresh to paint all */
              _nc_Refresh_Current_Field(form);
            }
        }
Berk Geveci's avatar
Berk Geveci committed
1024
      else 
1025
1026
1027
        {
          res = Display_Field(field);
        }
Berk Geveci's avatar
Berk Geveci committed
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
    }
  return(res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  int _nc_Synchronize_Options(FIELD * field,
|                                                Field_Options newopts)
|   
|   Description   :  If a fields options have changed, this routine is
|                    called to propagate these changes to the screen and
|                    to really change the behaviour of the field.
|
|   Return Values :  E_OK                - success
|                    E_BAD_ARGUMENT      - invalid field pointer 
|                    E_SYSTEM_ERROR      - some severe basic error
+--------------------------------------------------------------------------*/
int
_nc_Synchronize_Options(FIELD *field, Field_Options newopts)
{
  Field_Options oldopts;
  Field_Options changed_opts;
  FORM *form;
  int res = E_OK;

  if (!field)
    return(E_BAD_ARGUMENT);

  oldopts      = field->opts;
  changed_opts = oldopts ^ newopts;
  field->opts  = newopts;
  form         = field->form;

  if (form)
    {
      if (form->current == field)
1064
1065
1066
1067
        {
          field->opts = oldopts;
          return(E_CURRENT);
        }
Berk Geveci's avatar
Berk Geveci committed
1068
1069

      if (form->status & _POSTED)
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
        {
          if ((form->curpage == field->page))
            {
              if (changed_opts & O_VISIBLE)
                {
                  if (newopts & O_VISIBLE)
                    res = Display_Field(field);
                  else
                    res = Erase_Field(field);
                }
              else
                {
                  if ((changed_opts & O_PUBLIC) &&
                      (newopts & O_VISIBLE))
                    res = Display_Field(field);
                }
            }
        }
Berk Geveci's avatar
Berk Geveci committed
1088
1089
1090
1091
1092
1093
1094
1095
    }

  if (changed_opts & O_STATIC)
    {
      bool single_line_field = Single_Line_Field(field);
      int res2 = E_OK;

      if (newopts & O_STATIC)
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
        { /* the field becomes now static */
          field->status &= ~_MAY_GROW;
          /* if actually we have no hidden columns, justification may
             occur again */
          if (single_line_field                 &&
              (field->cols == field->dcols)     &&
              (field->just != NO_JUSTIFICATION) &&
              Field_Really_Appears(field))
            {
              res2 = Display_Field(field);
            }
        }
Berk Geveci's avatar
Berk Geveci committed
1108
      else
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
        { /* field is no longer static */
          if ((field->maxgrow==0) ||
              ( single_line_field && (field->dcols < field->maxgrow)) ||
              (!single_line_field && (field->drows < field->maxgrow)))
            {
              field->status |= _MAY_GROW;
              /* a field with justification now changes its behaviour,
                 so we must redisplay it */
              if (single_line_field                 &&
                  (field->just != NO_JUSTIFICATION) &&
                  Field_Really_Appears(field))
                {
                  res2 = Display_Field(field);
                }        
            }     
        }
Berk Geveci's avatar
Berk Geveci committed
1125
      if (res2 != E_OK)
1126
        res = res2;
Berk Geveci's avatar
Berk Geveci committed
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
    }

  return(res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  int _nc_Set_Current_Field(FORM  * form,
|                                              FIELD * newfield)
|   
|   Description   :  Make the newfield the new current field.
|
|   Return Values :  E_OK                - success
|                    E_BAD_ARGUMENT      - invalid form or field pointer 
|                    E_SYSTEM_ERROR      - some severe basic error
+--------------------------------------------------------------------------*/
int
_nc_Set_Current_Field(FORM  *form, FIELD *newfield)
{
  FIELD  *field;
  WINDOW *new_window;

  if (!form || !newfield || !form->current || (newfield->form!=form))
    return(E_BAD_ARGUMENT);

  if ( (form->status & _IN_DRIVER) )
    return(E_BAD_STATE);

  if (!(form->field))
    return(E_NOT_CONNECTED);

  field = form->current;
 
  if ((field!=newfield) || 
      !(form->status & _POSTED))
    {
      if ((form->w) && 
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
          (field->opts & O_VISIBLE) &&
          (field->form->curpage == field->page))
        {
          _nc_Refresh_Current_Field(form);
          if (field->opts & O_PUBLIC)
            {
              if (field->drows > field->rows)
                {
                  if (form->toprow==0)
                    field->status &= ~_NEWTOP;
                  else 
                    field->status |= _NEWTOP;
                } 
              else 
                {
                  if (Justification_Allowed(field))
                    {
                      Window_To_Buffer(form->w,field);
                      werase(form->w);
                      Perform_Justification(field,form->w);
                      wsyncup(form->w);
                    }
                }
            }
          delwin(form->w);
        }
Berk Geveci's avatar
Berk Geveci committed
1190
1191
1192
1193
      
      field = newfield;

      if (Has_Invisible_Parts(field))
1194
        new_window = newpad(field->drows,field->dcols);
Berk Geveci's avatar
Berk Geveci committed
1195
      else 
1196
1197
        new_window = derwin(Get_Form_Window(form),
                            field->rows,field->cols,field->frow,field->fcol);
Berk Geveci's avatar
Berk Geveci committed
1198
1199

      if (!new_window) 
1200
        return(E_SYSTEM_ERROR);
Berk Geveci's avatar
Berk Geveci committed
1201
1202
1203
1204
1205
1206
1207

      form->current = field;
      form->w       = new_window;
      form->status &= ~_WINDOW_MODIFIED;
      Set_Field_Window_Attributes(field,form->w);

      if (Has_Invisible_Parts(field))
1208
1209
1210
1211
        {
          werase(form->w);
          Buffer_To_Window(field,form->w);
        } 
Berk Geveci's avatar
Berk Geveci committed
1212
      else 
1213
1214
1215
1216
1217
1218
1219
1220
        {
          if (Justification_Allowed(field))
            {
              werase(form->w);
              Undo_Justification(field,form->w);
              wsyncup(form->w);
            }
        }
Berk Geveci's avatar
Berk Geveci committed
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249

      untouchwin(form->w);
    }

  form->currow = form->curcol = form->toprow = form->begincol = 0;
  return(E_OK);
}

/*----------------------------------------------------------------------------
  Intra-Field Navigation routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_Next_Character(FORM * form)
|   
|   Description   :  Move to the next character in the field. In a multiline
|                    field this wraps at the end of the line.
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - at the rightmost position
+--------------------------------------------------------------------------*/
static int IFN_Next_Character(FORM * form)
{
  FIELD *field = form->current;
  
  if ((++(form->curcol))==field->dcols)
    {
      if ((++(form->currow))==field->drows)
1250
        {
Berk Geveci's avatar
Berk Geveci committed
1251
#if GROW_IF_NAVIGATE
1252
1253
1254
1255
          if (!Single_Line_Field(field) && Field_Grown(field,1)) {
            form->curcol = 0;
            return(E_OK);
          }
Berk Geveci's avatar
Berk Geveci committed
1256
#endif
1257
          form->currow--;
Berk Geveci's avatar
Berk Geveci committed
1258
#if GROW_IF_NAVIGATE
1259
1260
          if (Single_Line_Field(field) && Field_Grown(field,1))
            return(E_OK);
Berk Geveci's avatar
Berk Geveci committed
1261
#endif
1262
1263
1264
          form->curcol--;
          return(E_REQUEST_DENIED);
        }
Berk Geveci's avatar
Berk Geveci committed
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
      form->curcol = 0;
    }
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_Previous_Character(FORM * form)
|   
|   Description   :  Move to the previous character in the field. In a 
|                    multiline field this wraps and the beginning of the 
|                    line.
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - at the leftmost position
+--------------------------------------------------------------------------*/
static int IFN_Previous_Character(FORM * form)
{
  if ((--(form->curcol))<0)
    {
      if ((--(form->currow))<0)
1286
1287
1288
1289
1290
        {
          form->currow++;
          form->curcol++;
          return(E_REQUEST_DENIED);
        }
Berk Geveci's avatar
Berk Geveci committed
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
      form->curcol = form->current->dcols - 1;
    }
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_Next_Line(FORM * form)
|   
|   Description   :  Move to the beginning of the next line in the field
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - at the last line
+--------------------------------------------------------------------------*/
static int IFN_Next_Line(FORM * form)
{
  FIELD *field = form->current;

  if ((++(form->currow))==field->drows)
    {
#if GROW_IF_NAVIGATE
      if (!Single_Line_Field(field) && Field_Grown(field,1))
1313
        return(E_OK);
Berk Geveci's avatar
Berk Geveci committed
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
#endif
      form->currow--;
      return(E_REQUEST_DENIED);
    }
  form->curcol = 0;
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_Previous_Line(FORM * form)
|   
|   Description   :  Move to the beginning of the previous line in the field
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - at the first line
+--------------------------------------------------------------------------*/
static int IFN_Previous_Line(FORM * form)
{
  if ( (--(form->currow)) < 0 )
    {
      form->currow++;
      return(E_REQUEST_DENIED);
    }
  form->curcol = 0;
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_Next_Word(FORM * form)
|   
|   Description   :  Move to the beginning of the next word in the field.
|
|   Return Values :  E_OK             - success
|                    E_REQUEST_DENIED - there is no next word
+--------------------------------------------------------------------------*/
static int IFN_Next_Word(FORM * form)
{
  FIELD *field = form->current;
  char  *bp    = Address_Of_Current_Position_In_Buffer(form);
  char  *s;
  char  *t;

  /* We really need access to the data, so we have to synchronize */
  Synchronize_Buffer(form);

  /* Go to the first whitespace after the current position (including
     current position). This is then the startpoint to look for the
    next non-blank data */
  s = Get_First_Whitespace_Character(bp,Buffer_Length(field) -
1365
                                     (int)(bp - field->buf));
Berk Geveci's avatar
Berk Geveci committed
1366
1367
1368

  /* Find the start of the next word */
  t = Get_Start_Of_Data(s,Buffer_Length(field) -
1369
                        (int)(s - field->buf));
Berk Geveci's avatar
Berk Geveci committed
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
#if !FRIENDLY_PREV_NEXT_WORD
  if (s==t) 
    return(E_REQUEST_DENIED);
  else
#endif
    {
      Adjust_Cursor_Position(form,t);
      return(E_OK);
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_Previous_Word(FORM * form)
|   
|   Description   :  Move to the beginning of the previous word in the field.
|
|   Return Values :  E_OK             - success
|                    E_REQUEST_DENIED - there is no previous word
+--------------------------------------------------------------------------*/
static int IFN_Previous_Word(FORM * form)
{
  FIELD *field = form->current;
  char  *bp    = Address_Of_Current_Position_In_Buffer(form);
  char  *s;
  char  *t;
  bool  again = FALSE;

  /* We really need access to the data, so we have to synchronize */
  Synchronize_Buffer(form);

  s = After_End_Of_Data(field->buf,(int)(bp-field->buf));
  /* s points now right after the last non-blank in the buffer before bp.
     If bp was in a word, s equals bp. In this case we must find the last
     whitespace in the buffer before bp and repeat the game to really find
     the previous word! */
  if (s==bp)
    again = TRUE;
  
  /* And next call now goes backward to look for the last whitespace
     before that, pointing right after this, so it points to the begin
     of the previous word. 
  */
  t = After_Last_Whitespace_Character(field->buf,(int)(s - field->buf));
#if !FRIENDLY_PREV_NEXT_WORD
  if (s==t) 
    return(E_REQUEST_DENIED);
#endif
  if (again)
    { /* and do it again, replacing bp by t */
      s = After_End_Of_Data(field->buf,(int)(t - field->buf));
      t = After_Last_Whitespace_Character(field->buf,(int)(s - field->buf));
#if !FRIENDLY_PREV_NEXT_WORD
      if (s==t) 
1424
        return(E_REQUEST_DENIED);
Berk Geveci's avatar
Berk Geveci committed
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
#endif
    }
  Adjust_Cursor_Position(form,t);
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_Beginning_Of_Field(FORM * form)
|   
|   Description   :  Place the cursor at the first non-pad character in
|                    the field. 
|
|   Return Values :  E_OK             - success            
+--------------------------------------------------------------------------*/
static int IFN_Beginning_Of_Field(FORM * form)
{
  FIELD *field = form->current;

  Synchronize_Buffer(form);
  Adjust_Cursor_Position(form,
1446
                 Get_Start_Of_Data(field->buf,Buffer_Length(field)));
Berk Geveci's avatar
Berk Geveci committed
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_End_Of_Field(FORM * form)
|   
|   Description   :  Place the cursor after the last non-pad character in
|                    the field. If the field occupies the last position in
|                    the buffer, the cursos is positioned on the last 
|                    character.
|
|   Return Values :  E_OK              - success
+--------------------------------------------------------------------------*/
static int IFN_End_Of_Field(FORM * form)
{
  FIELD *field = form->current;
  char *pos;

  Synchronize_Buffer(form);
  pos = After_End_Of_Data(field->buf,Buffer_Length(field));
  if (pos==(field->buf + Buffer_Length(field)))
    pos--;
  Adjust_Cursor_Position(form,pos);
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_Beginning_Of_Line(FORM * form)
|   
|   Description   :  Place the cursor on the first non-pad character in
|                    the current line of the field.
|
|   Return Values :  E_OK         - success
+--------------------------------------------------------------------------*/
static int IFN_Beginning_Of_Line(FORM * form)
{
  FIELD *field = form->current;

  Synchronize_Buffer(form);
  Adjust_Cursor_Position(form,
1489
1490
                 Get_Start_Of_Data(Address_Of_Current_Row_In_Buffer(form),
                                   field->dcols));
Berk Geveci's avatar
Berk Geveci committed
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_End_Of_Line(FORM * form)
|   
|   Description   :  Place the cursor after the last non-pad character in the
|                    current line of the field. If the field occupies the 
|                    last column in the line, the cursor is positioned on the
|                    last character of the line.
|
|   Return Values :  E_OK        - success
+--------------------------------------------------------------------------*/
static int IFN_End_Of_Line(FORM * form)
{
  FIELD *field = form->current;
  char *pos;
  char *bp;

  Synchronize_Buffer(form);
  bp  = Address_Of_Current_Row_In_Buffer(form); 
  pos = After_End_Of_Data(bp,field->dcols);
  if (pos == (bp + field->dcols))
    pos--;
  Adjust_Cursor_Position(form,pos);
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_Left_Character(FORM * form)
|   
|   Description   :  Move one character to the left in the current line.
|                    This doesn't cycle.  
|
|   Return Values :  E_OK             - success
|                    E_REQUEST_DENIED - already in first column
+--------------------------------------------------------------------------*/
static int IFN_Left_Character(FORM * form)
{
  if ( (--(form->curcol)) < 0 )
    {
      form->curcol++;
      return(E_REQUEST_DENIED);
    }
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_Right_Character(FORM * form)
|   
|   Description   :  Move one character to the right in the current line.
|                    This doesn't cycle.
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - already in last column
+--------------------------------------------------------------------------*/
static int IFN_Right_Character(FORM * form)
{
  if ( (++(form->curcol)) == form->current->dcols )
    {
#if GROW_IF_NAVIGATE
      FIELD *field = form->current;
      if (Single_Line_Field(field) && Field_Grown(field,1))
1557
        return(E_OK);
Berk Geveci's avatar
Berk Geveci committed
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
#endif
      --(form->curcol);
      return(E_REQUEST_DENIED);
    }
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_Up_Character(FORM * form)
|   
|   Description   :  Move one line up. This doesn't cycle through the lines
|                    of the field.
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - already in last column
+--------------------------------------------------------------------------*/
static int IFN_Up_Character(FORM * form)
{
  if ( (--(form->currow)) < 0 )
    {
      form->currow++;
      return(E_REQUEST_DENIED);
    }
  return(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int IFN_Down_Character(FORM * form)
|   
|   Description   :  Move one line down. This doesn't cycle through the
|                    lines of the field.
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - already in last column
+--------------------------------------------------------------------------*/
static int IFN_Down_Character(FORM * form)
{
  FIELD *field = form->current;

  if ( (++(form->currow)) == field->drows )
    {
#if GROW_IF_NAVIGATE
      if (!Single_Line_Field(field) && Field_Grown(field,1))
1603
        return(E_OK);
Berk Geveci's avatar
Berk Geveci committed
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
#endif
      --(form->currow);
      return(E_REQUEST_DENIED);
    }
  return(E_OK);
}
/*----------------------------------------------------------------------------
  END of Intra-Field Navigation routines 
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Vertical scrolling helper routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int VSC_Generic(FORM *form, int lines)
|
|   Description   :  Scroll multi-line field forward (lines>0) or
|                    backward (lines<0) this many lines.
|
|   Return Values :  E_OK              - success 
|                    E_REQUEST_DENIED  - can't scroll
+--------------------------------------------------------------------------*/
static int VSC_Generic(FORM *form, int lines)
{
  FIELD *field = form->current;
  int res = E_REQUEST_DENIED;
  int rows_to_go = (lines > 0 ? lines : -lines);

  if (lines > 0)
    {
      if ( (rows_to_go + form->toprow) > (field->drows - field->rows) )
1637
        rows_to_go = (field->drows - field->rows - form->toprow);
Berk Geveci's avatar
Berk Geveci committed
1638
1639

      if (rows_to_go > 0)
1640
1641
1642
1643
1644
        {
          form->currow += rows_to_go;
          form->toprow += rows_to_go;
          res = E_OK;
        }
Berk Geveci's avatar
Berk Geveci committed
1645
1646
1647
1648
    }
  else
    {
      if (rows_to_go > form->toprow)
1649
        rows_to_go = form->toprow;
Berk Geveci's avatar
Berk Geveci committed
1650
1651
      
      if (rows_to_go > 0)
1652
1653
1654
1655
1656
        {
          form->currow -= rows_to_go;
          form->toprow -= rows_to_go;
          res = E_OK;
        }
Berk Geveci's avatar
Berk Geveci committed
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
    }
  return(res);
}
/*----------------------------------------------------------------------------
  End of Vertical scrolling helper routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Vertical scrolling routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int Vertical_Scrolling(
|                                           int (* const fct) (FORM *),
|                                           FORM * form)
|   
|   Description   :  Performs the generic vertical scrolling routines. 
|                    This has to check for a multi-line field and to set
|                    the _NEWTOP flag if scrolling really occured.
|
|   Return Values :  Propagated error code from low-level driver calls
+--------------------------------------------------------------------------*/
static int Vertical_Scrolling(int (* const fct) (FORM *), FORM * form)
{
  int res = E_REQUEST_DENIED;

  if (!Single_Line_Field(form->current))
    {
      res = fct(form);
      if (res == E_OK)
1688
        form->current->status |= _NEWTOP;
Berk Geveci's avatar
Berk Geveci committed
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
    }
  return(res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int VSC_Scroll_Line_Forward(FORM * form)
|   
|   Description   :  Scroll multi-line field forward a line
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data ahead
+--------------------------------------------------------------------------*/
static int VSC_Scroll_Line_Forward(FORM * form)
{
  return VSC_Generic(form,1);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int VSC_Scroll_Line_Backward(FORM * form)
|   
|   Description   :  Scroll multi-line field backward a line
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data behind
+--------------------------------------------------------------------------*/
static int VSC_Scroll_Line_Backward(FORM * form)
{
  return VSC_Generic(form,-1);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int VSC_Scroll_Page_Forward(FORM * form)
|   
|   Description   :  Scroll a multi-line field forward a page
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - no data ahead
+--------------------------------------------------------------------------*/
static int VSC_Scroll_Page_Forward(FORM * form)
{
  return VSC_Generic(form,form->current->rows);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int VSC_Scroll_Half_Page_Forward(FORM * form)
|   
|   Description   :  Scroll a multi-line field forward half a page
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - no data ahead
+--------------------------------------------------------------------------*/
static int VSC_Scroll_Half_Page_Forward(FORM * form)
{
  return VSC_Generic(form,(form->current->rows + 1)/2);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int VSC_Scroll_Page_Backward(FORM * form)
|   
|   Description   :  Scroll a multi-line field backward a page
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - no data behind
+--------------------------------------------------------------------------*/
static int VSC_Scroll_Page_Backward(FORM * form)
{
  return VSC_Generic(form, -(form->current->rows));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int VSC_Scroll_Half_Page_Backward(FORM * form)
|   
|   Description   :  Scroll a multi-line field backward half a page
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - no data behind
+--------------------------------------------------------------------------*/
static int VSC_Scroll_Half_Page_Backward(FORM * form)
{
  return VSC_Generic(form, -((form->current->rows + 1)/2));
}
/*----------------------------------------------------------------------------
  End of Vertical scrolling routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Horizontal scrolling helper routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int HSC_Generic(FORM *form, int columns)
|
|   Description   :  Scroll single-line field forward (columns>0) or
|                    backward (columns<0) this many columns.
|
|   Return Values :  E_OK              - success 
|                    E_REQUEST_DENIED  - can't scroll
+--------------------------------------------------------------------------*/
static int HSC_Generic(FORM *form, int columns)
{
  FIELD *field = form->current;
  int res = E_REQUEST_DENIED;
  int cols_to_go = (columns > 0 ? columns : -columns);

  if (columns > 0)
    {
      if ((cols_to_go + form->begincol) > (field->dcols - field->cols))
1803
        cols_to_go = field->dcols - field->cols - form->begincol;
Berk Geveci's avatar
Berk Geveci committed
1804
1805
      
      if (cols_to_go > 0)
1806
1807
1808
1809
1810
        {
          form->curcol   += cols_to_go;
          form->begincol += cols_to_go;
          res = E_OK;
        }
Berk Geveci's avatar
Berk Geveci committed
1811
1812
1813
1814
    }
  else
    {
      if ( cols_to_go > form->begincol )
1815
        cols_to_go = form->begincol;
Berk Geveci's avatar
Berk Geveci committed
1816
1817

      if (cols_to_go > 0)
1818
1819
1820
1821
1822
        {
          form->curcol   -= cols_to_go;
          form->begincol -= cols_to_go;
          res = E_OK;
        }
Berk Geveci's avatar
Berk Geveci committed
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
    }
  return(res);
}
/*----------------------------------------------------------------------------
  End of Horizontal scrolling helper routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Horizontal scrolling routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int Horizontal_Scrolling(
|                                          int (* const fct) (FORM *),
|                                          FORM * form)
|   
|   Description   :  Performs the generic horizontal scrolling routines. 
|                    This has to check for a single-line field.
|
|   Return Values :  Propagated error code from low-level driver calls
+--------------------------------------------------------------------------*/
static int Horizontal_Scrolling(int (* const fct) (FORM *), FORM * form)
{
  if (Single_Line_Field(form->current))
    return fct(form);
  else
    return(E_REQUEST_DENIED);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int HSC_Scroll_Char_Forward(FORM * form)
|   
|   Description   :  Scroll single-line field forward a character
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data ahead
+--------------------------------------------------------------------------*/
static int HSC_Scroll_Char_Forward(FORM *form)
{
  return HSC_Generic(form,1);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int HSC_Scroll_Char_Backward(FORM * form)
|   
|   Description   :  Scroll single-line field backward a character
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data behind
+--------------------------------------------------------------------------*/
static int HSC_Scroll_Char_Backward(FORM *form)
{
  return HSC_Generic(form,-1);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int HSC_Horizontal_Line_Forward(FORM* form)
|   
|   Description   :  Scroll single-line field forward a line
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data ahead
+--------------------------------------------------------------------------*/
static int HSC_Horizontal_Line_Forward(FORM * form)
{
  return HSC_Generic(form,form->current->cols);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int HSC_Horizontal_Half_Line_Forward(FORM* form)
|   
|   Description   :  Scroll single-line field forward half a line
|
|   Return Values :  E_OK               - success
|                    E_REQUEST_DENIED   - no data ahead
+--------------------------------------------------------------------------*/
static int HSC_Horizontal_Half_Line_Forward(FORM * form)
{
  return HSC_Generic(form,(form->current->cols + 1)/2);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int HSC_Horizontal_Line_Backward(FORM* form)
|   
|   Description   :  Scroll single-line field backward a line
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data behind
+--------------------------------------------------------------------------*/
static int HSC_Horizontal_Line_Backward(FORM * form)
{
  return HSC_Generic(form,-(form->current->cols));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int HSC_Horizontal_Half_Line_Backward(FORM* form)
|   
|   Description   :  Scroll single-line field backward half a line
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data behind
+--------------------------------------------------------------------------*/
static int HSC_Horizontal_Half_Line_Backward(FORM * form)
{
  return HSC_Generic(form,-((form->current->cols + 1)/2));
}

/*----------------------------------------------------------------------------
  End of Horizontal scrolling routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Helper routines for Field Editing
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static bool Is_There_Room_For_A_Line(FORM * form)
|   
|   Description   :  Check whether or not there is enough room in the
|                    buffer to enter a whole line.
|
|   Return Values :  TRUE   - there is enough space
|                    FALSE  - there is not enough space
+--------------------------------------------------------------------------*/
INLINE static bool Is_There_Room_For_A_Line(FORM * form)
{
  FIELD *field = form->current;
  char *begin_of_last_line, *s;
  
  Synchronize_Buffer(form);
  begin_of_last_line = Address_Of_Row_In_Buffer(field,(field->drows-1));
  s  = After_End_Of_Data(begin_of_last_line,field->dcols);
  return ((s==begin_of_last_line) ? TRUE : FALSE);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static bool Is_There_Room_For_A_Char_In_Line(FORM * form)
|   
|   Description   :  Checks whether or not there is room for a new character
|                    in the current line.
|
|   Return Values :  TRUE    - there is room
|                    FALSE   - there is not enough room (line full)
+--------------------------------------------------------------------------*/
INLINE static bool Is_There_Room_For_A_Char_In_Line(FORM * form)
{
  int last_char_in_line;

  wmove(form->w,form->currow,form->current->dcols-1);
  last_char_in_line  = (int)(winch(form->w) & A_CHARTEXT);
  wmove(form->w,form->currow,form->curcol);
  return (((last_char_in_line == form->current->pad) ||
1984
           is_blank(last_char_in_line)) ? TRUE : FALSE);
Berk Geveci's avatar
Berk Geveci committed
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
}

#define There_Is_No_Room_For_A_Char_In_Line(f) \
  !Is_There_Room_For_A_Char_In_Line(f)

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int Insert_String(
|                                             FORM * form,
|                                             int row,
|                                             char *txt,
|                                             int  len )
|   
|   Description   :  Insert the 'len' characters beginning at pointer 'txt'
|                    into the 'row' of the 'form'. The insertion occurs
|                    on the beginning of the row, all other characters are
|                    moved to the right. After the text a pad character will 
|                    be inserted to separate the text from the rest. If
|                    necessary the insertion moves characters on the next
|                    line to make place for the requested insertion string.
|
|   Return Values :  E_OK              - success 
|                    E_REQUEST_DENIED  -
|                    E_SYSTEM_ERROR    - system error
+--------------------------------------------------------------------------*/
static int Insert_String(FORM *form, int row, char *txt, int len)
{ 
  FIELD  *field    = form->current;
  char *bp         = Address_Of_Row_In_Buffer(field,row);
  int datalen      = (int)(After_End_Of_Data(bp,field->dcols) - bp);
  int freelen      = field->dcols - datalen;
  int requiredlen  = len+1;
  char *split;
  int result = E_REQUEST_DENIED;
Berk Geveci's avatar
Berk Geveci committed
2019
2020
2021
2022
  char *Space;

  Space = (char*)malloc(2*sizeof(char));
  strcpy(Space, " ");
Berk Geveci's avatar
Berk Geveci committed
2023
2024
2025
2026
2027
2028
2029

  if (freelen >= requiredlen)
    {
      wmove(form->w,row,0);
      winsnstr(form->w,txt,len);
      wmove(form->w,row,len);
      winsnstr(form->w,Space,1);
Berk Geveci's avatar
Berk Geveci committed
2030
      free(Space);
Berk Geveci's avatar
Berk Geveci committed
2031
2032
2033
2034
      return E_OK;
    }
  else
    { /* we have to move characters on the next line. If we are on the
2035
         last line this may work, if the field is growable */
Berk Geveci's avatar
Berk Geveci committed
2036
      if ((row == (field->drows - 1)) && Growable(field))
2037
2038
2039
2040
2041
2042
2043
2044
2045
        {
          if (!Field_Grown(field,1))
            {
            free(Space);
            return(E_SYSTEM_ERROR);
            }
          /* !!!Side-Effect : might be changed due to growth!!! */
          bp = Address_Of_Row_In_Buffer(field,row); 
        }
Berk Geveci's avatar
Berk Geveci committed
2046
2047

      if (row < (field->drows - 1)) 
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
        { 
          split = After_Last_Whitespace_Character(bp,
                    (int)(Get_Start_Of_Data(bp + field->dcols - requiredlen ,
                                            requiredlen) - bp));
          /* split points now to the first character of the portion of the
             line that must be moved to the next line */
          datalen = (int)(split-bp); /* + freelen has to stay on this line   */
          freelen = field->dcols - (datalen + freelen); /* for the next line */

          if ((result=Insert_String(form,row+1,split,freelen))==E_OK) 
            {
              wmove(form->w,row,datalen);
              wclrtoeol(form->w);
              wmove(form->w,row,0);
              winsnstr(form->w,txt,len);
              wmove(form->w,row,len);
              winsnstr(form->w,Space,1);
              free(Space);
              return E_OK;
            }
        }
Berk Geveci's avatar
Berk Geveci committed
2069
      free(Space);
Berk Geveci's avatar
Berk Geveci committed
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
      return(result);
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int Wrapping_Not_Necessary_Or_Wrapping_Ok(
|                                             FORM * form)
|   
|   Description   :  If a character has been entered into a field, it may
|                    be that wrapping has to occur. This routine checks
|                    whether or not wrapping is required and if so, performs
|                    the wrapping.
|
|   Return Values :  E_OK              - no wrapping required or wrapping
|                                        was successfull
|                    E_REQUEST_DENIED  -
|                    E_SYSTEM_ERROR    - some system error
+--------------------------------------------------------------------------*/
static int Wrapping_Not_Necessary_Or_Wrapping_Ok(FORM * form)
{
  FIELD  *field = form->current;
  int result = E_REQUEST_DENIED;
  bool Last_Row = ((field->drows - 1) == form->currow);

  if ( (field->opts & O_WRAP)                     &&  /* wrapping wanted     */
      (!Single_Line_Field(field))                 &&  /* must be multi-line  */
      (There_Is_No_Room_For_A_Char_In_Line(form)) &&  /* line is full        */
      (!Last_Row || Growable(field))               )  /* there are more lines*/
    {
      char *bp;
      char *split;
      int chars_to_be_wrapped;
      int chars_to_remain_on_line;
      if (Last_Row)
2105
2106
2107
2108
2109
        { /* the above logic already ensures, that in this case the field
             is growable */
          if (!Field_Grown(field,1))
            return E_SYSTEM_ERROR;
        }
Berk Geveci's avatar
Berk Geveci committed
2110
2111
2112
2113
2114
2115
2116
2117
      bp = Address_Of_Current_Row_In_Buffer(form);
      Window_To_Buffer(form->w,field);
      split = After_Last_Whitespace_Character(bp,field->dcols);
      /* split points to the first character of the sequence to be brought
         on the next line */
      chars_to_remain_on_line = (int)(split - bp);
      chars_to_be_wrapped     = field->dcols - chars_to_remain_on_line;
      if (chars_to_remain_on_line > 0)
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
        {
          if ((result=Insert_String(form,form->currow+1,split,
                                    chars_to_be_wrapped)) == E_OK)
            {
              wmove(form->w,form->currow,chars_to_remain_on_line);
              wclrtoeol(form->w);
              if (form->curcol >= chars_to_remain_on_line)
                {
                  form->currow++;
                  form->curcol -= chars_to_remain_on_line;
                }
              return E_OK;
            }
        }
Berk Geveci's avatar
Berk Geveci committed
2132
      else
2133
        return E_OK;
Berk Geveci's avatar
Berk Geveci committed
2134
      if (result!=E_OK)
2135
2136
2137
2138
2139
2140
        {
          wmove(form->w,form->currow,form->curcol);
          wdelch(form->w);
          Window_To_Buffer(form->w,field);
          result = E_REQUEST_DENIED;
        }
Berk Geveci's avatar
Berk Geveci committed
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
    }
  else
    result = E_OK; /* wrapping was not necessary */
  return(result);
}

/*----------------------------------------------------------------------------
  Field Editing routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int Field_Editing(
|                                    int (* const fct) (FORM *),
|                                    FORM * form)
|   
|   Description   :  Generic routine for field editing requests. The driver
|                    routines are only called for editable fields, the
|                    _WINDOW_MODIFIED flag is set if editing occured.
|                    This is somewhat special due to the overload semantics
|                    of the NEW_LINE and DEL_PREV requests.
|
|   Return Values :  Error code from low level drivers.
+--------------------------------------------------------------------------*/
static int Field_Editing(int (* const fct) (FORM *), FORM * form)
{
  int res = E_REQUEST_DENIED;

  /* We have to deal here with the specific case of the overloaded 
     behaviour of New_Line and Delete_Previous requests.
     They may end up in navigational requests if we are on the first
     character in a field. But navigation is also allowed on non-
     editable fields.
  */ 
  if ((fct==FE_Delete_Previous)            && 
      (form->opts & O_BS_OVERLOAD)         &&
      First_Position_In_Current_Field(form) )
    {
      res = Inter_Field_Navigation(FN_Previous_Field,form);
    }
  else
    {
      if (fct==FE_New_Line)
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
        {
          if ((form->opts & O_NL_OVERLOAD)         &&
              First_Position_In_Current_Field(form))
            {
              res = Inter_Field_Navigation(FN_Next_Field,form);
            }
          else
            /* FE_New_Line deals itself with the _WINDOW_MODIFIED flag */
            res = fct(form);
        }
Berk Geveci's avatar
Berk Geveci committed
2194
      else
2195
2196
2197
2198
2199
2200
2201
2202
2203
        {
          /* From now on, everything must be editable */
          if (form->current->opts & O_EDIT)
            {
              res = fct(form);
              if (res==E_OK)
                form->status |= _WINDOW_MODIFIED;
            }
        }
Berk Geveci's avatar
Berk Geveci committed
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
    }
  return res;
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int FE_New_Line(FORM * form)
|   
|   Description   :  Perform a new line request. This is rather complex
|                    compared to other routines in this code due to the 
|                    rather difficult to understand description in the
|                    manuals.
|
|   Return Values :  E_OK               - success
|                    E_REQUEST_DENIED   - new line not allowed
|                    E_SYSTEM_ERROR     - system error
+--------------------------------------------------------------------------*/
static int FE_New_Line(FORM * form)
{
  FIELD  *field = form->current;
  char *bp, *t;
  bool Last_Row = ((field->drows - 1)==form->currow);
  
  if (form->status & _OVLMODE) 
    {
      if (Last_Row && 
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
          (!(Growable(field) && !Single_Line_Field(field))))
        {
          if (!(form->opts & O_NL_OVERLOAD))
            return(E_REQUEST_DENIED);
          wclrtoeol(form->w);
          /* we have to set this here, although it is also
             handled in the generic routine. The reason is,
             that FN_Next_Field may fail, but the form is
             definitively changed */
          form->status |= _WINDOW_MODIFIED;
          return Inter_Field_Navigation(FN_Next_Field,form);
        }
Berk Geveci's avatar
Berk Geveci committed
2242
      else 
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
        {
          if (Last_Row && !Field_Grown(field,1))
            { /* N.B.: due to the logic in the 'if', LastRow==TRUE
                 means here that the field is growable and not
                 a single-line field */
              return(E_SYSTEM_ERROR);
            }
          wclrtoeol(form->w);
          form->currow++;
          form->curcol = 0;
          form->status |= _WINDOW_MODIFIED;
          return(E_OK);
        }
Berk Geveci's avatar
Berk Geveci committed
2256
2257
2258
2259
    }
  else 
    { /* Insert Mode */
      if (Last_Row &&
2260
2261
2262
2263
2264
2265
          !(Growable(field) && !Single_Line_Field(field)))
        {
          if (!(form->opts & O_NL_OVERLOAD))
            return(E_REQUEST_DENIED);
          return Inter_Field_Navigation(FN_Next_Field,form);
        }
Berk Geveci's avatar
Berk Geveci committed
2266
      else 
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
        {
          bool May_Do_It = !Last_Row && Is_There_Room_For_A_Line(form);
          
          if (!(May_Do_It || Growable(field)))
            return(E_REQUEST_DENIED);
          if (!May_Do_It && !Field_Grown(field,1))
            return(E_SYSTEM_ERROR);
          
          bp= Address_Of_Current_Position_In_Buffer(form);
          t = After_End_Of_Data(bp,field->dcols - form->curcol);
          wclrtoeol(form->w);
          form->currow++;
          form->curcol=0;
          wmove(form->w,form->currow,form->curcol);
          winsertln(form->w);
          waddnstr(form->w,bp,(int)(t-bp));
          form->status |= _WINDOW_MODIFIED;
          return E_OK;
        }
Berk Geveci's avatar
Berk Geveci committed
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static int FE_Insert_Character(FORM * form)
|   
|   Description   :  Insert blank character at the cursor position
|
|   Return Values :  E_OK
|                    E_REQUEST_DENIED
+--------------------------------------------------------------------------*/
static int FE_Insert_Character(FORM * form)
{
  FIELD *field = form->current;
  int result = E_REQUEST_DENIED;

  if (Check_Char(field->type,(int)C_BLANK,(TypeArgument *)(field->arg)))
    {
      bool There_Is_Room = Is_There_Room_For_A_Char_In_Line(form);

      if (There_Is_Room ||