summaryrefslogtreecommitdiff
path: root/inputline.h
blob: 82c3e5a00bc146b33be17a33ce66d4ec297606b9 (plain)
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
// Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.

#ifndef BDE_INPUTLINE_H
#define BDE_INPUTLINE_H

#include <vector>

#include "editbox.h"


class InputLine : public EditBox {

public:

    typedef std::vector<unistring> StringArray;

    // What kind of filenames, if any, to complete?
    enum CompleteType {
	    cmpltOff,		// turn off filename completion.
	    cmpltAll,
	    cmpltDirectories	// complete only directory names.
				// this is useful for, e.g., a "Change Dir:"
				// input line.
	};

private:

    unistring	vis_label;
    direction_t	label_dir;
    int		label_width;

    // Filename completion variables:

    CompleteType complete_type;
    StringArray  files_list;
    u8string     files_directory;   // the directory we're listing

    int event_num;
    int last_tab_event_num;

    int insertion_pos;
    int prefix_len;
    int slice_begin;
    int slice_end;
    int curr_choice;

    /*
	A little example can help in explaining what each completion
	variable means.

	Let's suppose our input-line looks like:

	~/documents/no_ -cp862
		      ^-----------cursor

	We press TAB and a "TAB session" starts:

	0. "no" is the partial filename component and "~/documents/" is the
	   directory name component.

	1. files_list is populated with all filenames in "~/documents":

	    0. archive.txt
	    1. notes.txt
	    2. notes2.txt
	    3. november.txt
	    4. questions.txt
    
	2. only entries 1 to 3 are relevant in our TAB session (because
	   they start in "no"), so slice_begin is set to 1 and slice_end
	   to 3.
	
	3. insertion_pos is set to the cursor position.

	4. prefix_len is set to the length of the partial filename
	   component: 2. (that is, strlen("no")).

	5. While moving forward and backward in files_list (as a result
	   of pressing TAB and M-TAB), curr_choice is updated to point
	   to the current entry.
    */

    // History variables:

    StringArray *history;
    int history_idx;

protected:

    // Filename completion

    void init_completion();
    void get_directory_files(u8string directory, const char *prefix = NULL);
    void complete(bool forward);

    // History

    void init_history(int history_set);
    void update_history();

    ////

    void trim();

protected:
    virtual void do_syntax_highlight(const unistring &str, 
	    AttributeArray &attributes, int para_num);

public:

    HAS_ACTIONS_MAP(InputLine, EditBox);
    HAS_BINDINGS_MAP(InputLine, EditBox);

    InputLine(const char *aLabel, const unistring &default_text,
	      int history_set = 0, CompleteType complete = cmpltOff);
    void set_label(const char *aLabel);
    void set_text(const unistring &s);
    unistring get_text() { return curr_para()->str; }

    INTERACTIVE void end_modal();
    INTERACTIVE void next_completion();
    INTERACTIVE void previous_completion();
    INTERACTIVE void next_history();
    INTERACTIVE void previous_history();

    virtual void redraw_paragraph(Paragraph &p,
			    int window_start_line, bool only_cursor, int);
    
    virtual bool handle_event(const Event &evt);
};

#endif