File: input.h

package info (click to toggle)
yapet 2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,920 kB
  • sloc: cpp: 32,397; sh: 5,032; makefile: 880; ansic: 36; sed: 16
file content (715 lines) | stat: -rw-r--r-- 16,077 bytes parent folder | download | duplicates (4)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
368
369
370
371
372
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
413
414
415
416
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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
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
607
608
609
610
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
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// -*- mode: c++ -*-
//
// This file is part of libyacurs.
// Copyright (C) 2013  Rafael Ostertag
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see
// <http://www.gnu.org/licenses/>.
//
//
// $Id$

#ifndef INPUT_H
#define INPUT_H 1

#include <cassert>
#include <cerrno>
#include <cstdlib>
#include <string>

#include "yacurscurses.h"  // required for YACURS_USE_WCHAR
#ifdef YACURS_USE_WCHAR
#include <cwchar>
#include <cwctype>
#else
#include <cctype>
#endif

#include "colors.h"
#include "cursorbuf.h"
#include "eventqueue.h"
#include "focusmanager.h"
#include "input.h"
#include "widget.h"
#include "yacursex.h"

namespace YACURS {
/**
 * Base class for filtering input.
 */
class InputFilter {
   public:
    /**
     * Filter method.
     *
     * Determines whether (@c true) or not (@c false) the
     * input is accepted.
     *
     * @param c character
     *
     * @retrun @c true if the input has to be accepted, or @c
     * false if it is to be discarded.
     */
#ifdef YACURS_USE_WCHAR
    virtual bool use(wint_t) = 0;
#else
    virtual bool use(char) = 0;
#endif
    virtual InputFilter* clone() const = 0;
    virtual ~InputFilter() {}
};

/**
 * Filter printable characters.
 */
class FilterPrint : public InputFilter {
   public:
#ifdef YACURS_USE_WCHAR
    virtual bool use(wint_t wc) { return iswprint(wc) != 0; }
#else
    virtual bool use(char c) { return std::isprint(c) != 0; }
#endif
    virtual InputFilter* clone() const { return new FilterPrint; }
};

/**
 * Filter alphabetic characters.
 */
class FilterAlpha : public InputFilter {
   public:
#ifdef YACURS_USE_WCHAR
    virtual bool use(wint_t wc) { return iswalpha(wc) != 0; }
#else
    virtual bool use(char c) { return std::isalpha(c) != 0; }
#endif
    virtual InputFilter* clone() const { return new FilterAlpha; }
};

/**
 * Filter alphanumeric characters.
 */
class FilterAlnum : public InputFilter {
   public:
#ifdef YACURS_USE_WCHAR
    virtual bool use(wint_t wc) { return iswalnum(wc) != 0; }
#else
    virtual bool use(char c) { return std::isalnum(c) != 0; }
#endif
    virtual InputFilter* clone() const { return new FilterAlnum; }
};

/**
 * Filter digit characters.
 */
class FilterDigit : public InputFilter {
   public:
#ifdef YACURS_USE_WCHAR
    virtual bool use(wint_t wc) { return iswdigit(wc) != 0; }
#else
    virtual bool use(char c) { return std::isdigit(c) != 0; }
#endif
    virtual InputFilter* clone() const { return new FilterDigit; }
};

/**
 * Filter hex digit characters.
 */
class FilterXDigit : public InputFilter {
   public:
#ifdef YACURS_USE_WCHAR
    virtual bool use(wint_t wc) { return iswxdigit(wc) != 0; }
#else
    virtual bool use(char c) { return std::isxdigit(c) != 0; }
#endif
    virtual InputFilter* clone() const { return new FilterXDigit; }
};

/**
 * Input line.
 *
 * Widget providing one line for text input.
 *
 * If not length is specified, it behaves like a dynamic
 * Widget. Providing a length make it behave non-dynamic.
 *
 * Following cursor motion keys are supported:
 *
 *  - Key left: move cursor to the left
 *  - Key right: move cursor to the right
 *  - Ctrl-A: move to the beginning of line
 *  - Ctrl-E: move to the end of line
 *
 * Following editing keys are supported:
 *
 *  - Ctrl-U: delete all text
 *  - Ctrl-K: delete text from current cursor position to end of text.
 *  - Backspace: delete character left to the cursor
 *  - Delete: delete character right to the cursor
 *
 * Misc keys:
 *
 *  - Key Down, Enter, Tab: focus next Widget.
 *  - Key Up, Shift-Tab: focus previous Widget.
 *
 * When Input is put in read-only mode, all editing keys are
 * disabled. Only cursor motion is available.
 *
 * Custom input filter can be used to filter user input. However,
 * filter do not take effect on data set using the input()
 * method. Also, filters do not affect any data already in the
 * buffer when a new filter is set.
 */
template <class T = std::string>
class Input : public Widget {
   public:
    typedef typename T::size_type tsz_t;

   private:
    INTERNAL::CursorBuffer _buffer;

    /**
     * Length of the input line.
     *
     * May be zero when dynamically sized.
     */
    int _length;

    /**
     * Mode, whether or not editing is allowed.
     */
    bool _read_only;

    /**
     * Mode, whether or not input is obscurred.
     */
    bool _obscure_input;

    /**
     * Mode, whether or not input is hidden.
     */
    bool _hide_input;

    /**
     * Character used for obscurring input.
     */
    char _obscure_char;

    /**
     * The size of the Input Widget.
     *
     * May be Size::zero() when dynamically sized, else the
     */
    Size _size;

    /**
     * Input filter.
     */
    InputFilter* _filter;

   protected:
    virtual void key_handler(Event& e);

    // From Realizeable
    void realize();

    void unrealize();

   public:
    /**
     * @param length the length (columns) the widget
     * requires. If zero, it will dynamically size. Default 0.
     *
     * @param max_size maximum size of input. Default 255.
     *
     * @param t text to display initially. Default empty.
     */
    Input(int length = 0, tsz_t max_size = 255, const T& t = T());
    Input<T>& operator=(const Input<T>&) = delete;
    Input<T>& operator=(Input<T>&&) = delete;
    Input<T>(const Input<T>&) = delete;
    Input<T>(Input<T>&&) = delete;

    virtual ~Input();

    /**
     * Set the content of the input buffer.
     *
     * @param i the content of the input buffer.
     */
    virtual void input(const T& i);

    /**
     * Return the content of the input buffer.
     *
     * @return the content of the input buffer.
     */
    virtual const T& input() const;

    /**
     * Clear the input buffer.
     */
    virtual void clear();

    /**
     * Set readonly mode.
     *
     * @param s @c true, no editing allowed. @c false,
     * editing allowed.
     */
    void readonly(bool s);

    /**
     * Get read-only mode
     *
     * @return @c true if Input Widget is in read-only mode, @c
     * false otherwise.
     */
    bool readonly() const;

    /**
     * Set maximum size of input.
     *
     * Set maximum size (characters) of input.
     *
     * @param max_input new maximum size of input
     */
    void max_input(tsz_t max_input);

    /**
     * Get maximum input size.
     */
    tsz_t max_input() const;

    /**
     * Set input obscure mode
     *
     * @param m @c true, will obscure input, @c false will show input.
     */
    void obscure_input(bool m);

    /**
     * Get obscure input mode.
     *
     * @return @c true if obscure input mode is enabled, @c false
     * otherwise.
     */
    bool obscure_input() const;

    /**
     * @param c character used to obscure input. Only used when
     * obscuring input
     */
    void obscure_char(char c);

    /**
     * @return character used to obscure input.
     */
    char obscure_char() const;

    void hide_input(bool m);

    bool hide_input() const;

    /**
     * Indicate whether or not input has changed.
     */
    bool changed() const;

    /**
     * Set a new character filter.
     *
     * Setting a new filter does not affect characters already
     * in buffer.
     *
     * @param f reference to filter.
     */
    void filter(const InputFilter& f);

    // From WidgetBase

    void size_available(const Size& s);

    /**
     * Size the Input Widget requires.
     *
     * @return Either Size::zero() if _length is 0, or the
     * Size(1, _length).
     */
    Size size() const;

    Size size_hint() const;

    /**
     * Dummy. Does nothing.
     *
     * Input Widget is not a container Widget, hence it may not
     * notified of size changes().
     *
     * @return always @false
     */
    bool size_change();

    /**
     * Reset size.
     *
     * If _length is zero, resets _size. Else does nothing.
     */
    void reset_size();

    // From Realizeable

    /**
     * Refresh the Input.
     *
     * @param immediate not directly used by Label::refresh() but
     * passed to Widget::refresh().
     */
    void refresh(bool immediate);
};

//
// Private
//


//
// Protected
//
template <class T>
void Input<T>::key_handler(Event& e) {
    assert(e.type() == EVT_KEY);

    if (!focus()) return;

#ifdef YACURS_USE_WCHAR
    EventEx<wint_t>& ekey = dynamic_cast<EventEx<wint_t>&>(e);
#else
    EventEx<int>& ekey = dynamic_cast<EventEx<int>&>(e);
#endif

    switch (ekey.data()) {
        case KEY_DOWN:
        case KEY_ENTER:
        case KEY_RETURN:
        case KEY_RETURN2:
        case KEY_TAB:
            EventQueue::submit(EVT_FOCUS_NEXT);
            break;

        case KEY_UP:
        case KEY_BTAB:
            EventQueue::submit(EVT_FOCUS_PREVIOUS);
            break;

        case KEY_DL:
            if (_read_only) return;
            _buffer.clear();
            break;

        case KEY_CTRL_U:
            if (_read_only) return;
            _buffer.clear_sol();
            break;

        case KEY_EOL:
        case KEY_CTRL_K:
            if (_read_only) return;
            _buffer.clear_eol();
            break;

        case KEY_BKSPC_SOL:
        case KEY_BACKSPACE:
            if (_read_only) return;
            _buffer.backspace();
            break;

        case KEY_CTRL_D:
        case KEY_DC:  // Delete
            if (_read_only) return;
            _buffer.del();
            break;

        case KEY_HOME:
        case KEY_CTRL_A:
            _buffer.home();
            break;

        case KEY_END:
        case KEY_CTRL_E:
            _buffer.end();
            break;

        case KEY_LEFT:
        case KEY_CTRL_B:
            _buffer.back_step();
            break;

        case KEY_RIGHT:
        case KEY_CTRL_F:
            _buffer.forward_step();
            break;

        default:  // regular key presses
            if (_read_only) return;

            // Call the filter
            if (!_filter->use(ekey.data())) return;

            _buffer.insert(ekey.data());
            break;
    }

    refresh(true);
}

template <class T>
void Input<T>::realize() {
    REALIZE_ENTER;

    Widget::realize();

    EventQueue::connect_event(
        EventConnectorMethod1<Input>(EVT_KEY, this, &Input::key_handler));

    assert(focusgroup_id() != FocusManager::nfgid);

    REALIZE_LEAVE;
}

template <class T>
void Input<T>::unrealize() {
    UNREALIZE_ENTER;
    EventQueue::disconnect_event(
        EventConnectorMethod1<Input>(EVT_KEY, this, &Input::key_handler));

    assert(focusgroup_id() != FocusManager::nfgid);
    Widget::unrealize();
    UNREALIZE_LEAVE;
}

//
// Public
//
template <class T>
Input<T>::Input(int length, tsz_t max_size, const T& t)
    : Widget(),
      _buffer(t, max_size),
      _length(length),
      _read_only(false),
      _obscure_input(false),
      _hide_input(false),
      _obscure_char('*'),
      _size(_length > 0 ? Size(1, _length) : Size::zero()),
      _filter(new FilterPrint) {
    can_focus(true);
}

template <class T>
Input<T>::~Input() {
    EventQueue::disconnect_event(
        EventConnectorMethod1<Input>(EVT_KEY, this, &Input<T>::key_handler));

    assert(_filter != 0);
    delete _filter;
}

template <class T>
void Input<T>::input(const T& i) {
    _buffer.set(i);

    refresh(true);
}

template <class T>
const T& Input<T>::input() const {
    return _buffer.string();
}

template <class T>
void Input<T>::clear() {
    _buffer.clear();
}

template <class T>
void Input<T>::readonly(bool s) {
    _read_only = s;
}

template <class T>
bool Input<T>::readonly() const {
    return _read_only;
}

template <class T>
void Input<T>::max_input(typename Input<T>::tsz_t max_input) {
    _buffer.max_size(max_input);

    if (realization() == REALIZED) refresh(true);
}

template <class T>
typename Input<T>::tsz_t Input<T>::max_input() const {
    return _buffer.max_size();
}

template <class T>
void Input<T>::obscure_input(bool m) {
    if (m && _hide_input) _hide_input = false;

    _obscure_input = m;

    if (realization() == REALIZED) refresh(true);
}

template <class T>
bool Input<T>::obscure_input() const {
    return _obscure_input;
}

template <class T>
void Input<T>::obscure_char(char c) {
    _obscure_char = c;
}

template <class T>
char Input<T>::obscure_char() const {
    return _obscure_char;
}

template <class T>
void Input<T>::hide_input(bool m) {
    if (m && _obscure_input) _obscure_input = false;

    _hide_input = m;

    if (realization() == REALIZED) refresh(true);
}

template <class T>
bool Input<T>::hide_input() const {
    return _hide_input;
}

template <class T>
bool Input<T>::changed() const {
    return _buffer.changed();
}

template <class T>
void Input<T>::filter(const InputFilter& f) {
    assert(_filter != 0);
    delete _filter;

    _filter = f.clone();
    assert(_filter != 0);
}

template <class T>
void Input<T>::size_available(const Size& s) {
    WidgetBase::size_available(s);

    // if we're dynamic, take the cols from s as cols for our
    // _size. Else, do nothing.
    if (_length == 0) {
        _size.rows(1);  // this is fixed for Input Widgets
        _size.cols(s.cols());
    }
    assert(_size.rows() <= s.rows());
    assert(_size.cols() <= s.cols());
}

template <class T>
Size Input<T>::size() const {
    return _size;
}

template <class T>
Size Input<T>::size_hint() const {
    // if _length != 0 then we're not dynmically sized, and the ctor
    // has set the size, which we return. See also comment for
    // size_hint() in widgetbase.h.
    if (_length != 0) return _size;

    return Size(1, _buffer.length());
}

template <class T>
bool Input<T>::size_change() {
    return false;
}

template <class T>
void Input<T>::reset_size() {
    // If _length != 0 then we are not dynamically sized, thus not
    // resetting size.
    if (_length != 0) return;

    _size = Size::zero();
}

template <class T>
void Input<T>::refresh(bool immediate) {
    if (realization() != REALIZED) return;
    assert(widget_subwin() != 0);

    // setting background is essential for coloring entire input
    // widget.

    if (focus()) {
        curs_set(1);
        widget_subwin()->leaveok(false);
    } else {
        curs_set(0);
        widget_subwin()->leaveok(true);
    }

    int16_t curs_pos;

    // Make sure that in the following if/else block curs_pos is
    // set in any case.

    if (_obscure_input) {
        int16_t len;

        _buffer.info(_size.cols(), &len, &curs_pos);
        CurStr obs_out(
            "", Coordinates(),
            focus() ? YACURS::INPUTWIDGET_FOCUS : YACURS::INPUTWIDGET_NOFOCUS);
        obs_out.assign(len, _obscure_char);
        widget_subwin()->addlinex(obs_out);
    } else {
        CurStr out(
            _buffer.string(_size.cols(), &curs_pos), Coordinates(),
            focus() ? YACURS::INPUTWIDGET_FOCUS : YACURS::INPUTWIDGET_NOFOCUS);
        if (_hide_input) {
            // first, give the widget nice color
            CurStr filler("", Coordinates(),
                          focus() ? YACURS::INPUTWIDGET_FOCUS
                                  : YACURS::INPUTWIDGET_NOFOCUS);
            widget_subwin()->addlinex(filler);
            // Now, put the string with hidden attributes
            out.color(YACURS::INPUTWIDGET_HIDDEN);
            widget_subwin()->addstr(out);
        } else {
            widget_subwin()->addlinex(out);
        }
    }

    widget_subwin()->move(Coordinates(curs_pos, 0));

    Widget::refresh(immediate);
}
}  // namespace YACURS

#endif  // INPUT_H