Newer
Older
/*
* FLTK History widget: list browser plus command input box.
* Stuart Levy, slevy@ncsa.uiuc.edu
* National Center for Supercomputing Applications,
* University of Illinois 2001.
* This file is part of partiview, released under the
* Illinois Open Source License; see the file LICENSE.partiview for details.
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
#if unix /* but not WIN32 */
# include <alloca.h>
#else
# include "winjunk.h"
#endif
#include "FL/Fl.H"
#include "FL/Fl_Browser.H"
#include "FL/Fl_Input.H"
#include "Hist.H"
void HistInput::hist( Hist *h ) {
hist_ = h;
if(h) h->input(this);
}
int HistInput::handle( int ev ) {
if(hist_ && hist_->handle_nav(ev))
return 1;
return Fl_Input::handle(ev);
}
void HistBrowser::hist( Hist *h ) {
hist_ = h;
if(h) h->browser(this);
}
int HistBrowser::handle( int ev ) {
if(hist_ && hist_->handle_nav(ev))
return 1;
return Fl_Browser::handle(ev);
}
int Hist::handle( int ev ) {
if(handle_nav(ev))
return 1;
return Fl_Group::handle(ev);
}
int HistBrowser::selscanup( int from ) {
while(from < size() && !selected(from))
from++;
return from;
}
int HistBrowser::selscandown( int from ) {
if(from > size()) from = size()-1;
while(from > 0 && selected(from))
from--;
return from;
}
void HistBrowser::tighten_histrange() {
int i;
if(min <= 0) min = 1;
if(max > size()) max = size();
for(i = min; i <= max && !selected(i); i++)
;
min = i;
for(i = max; i >= min && !selected(i); i--)
;
max = i;
}
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
int HistInput::scaletext( float amount ) {
int newsize;
if(amount < 0) newsize = (int) (-amount * textsize() + .5);
else if(amount > 0) newsize = (int) amount;
else newsize = 14;
textsize(newsize);
int dh = (int) (1.6*newsize + 2) - h();
if(this->parent() == this->hist()) {
resize( x(), y() - dh, w(), h() + dh );
} else {
Fl_Widget *subhist = this->parent();
while(subhist->parent() && subhist->parent() != this->hist())
subhist = subhist->parent();
((Fl_Group *)subhist)->resize( subhist->x(), subhist->y() - dh,
subhist->w(), subhist->h() + dh );
}
return dh;
}
void HistBrowser::scaletext( float amount ) {
int newsize;
if(amount < 0) newsize = (int) (-amount * textsize() + .5);
else if(amount > 0) newsize = (int) amount;
else newsize = 14;
textsize(newsize);
}
void Hist::scaletext( float amount ) {
int dh = 0;
if(input()) {
dh = input()->scaletext(amount);
}
if(browser()) {
browser()->scaletext(amount);
browser()->resize( browser()->x(), browser()->y(), browser()->w(),
browser()->h() - dh );
}
redraw();
}
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
static char msgprefix[] = "@C2@.# ";
#define MSGPREFIXSKIP (sizeof(msgprefix) - 3) /* retain "# " */
int HistBrowser::is_cmd( int line ) {
if(line <= 0 || line > size())
return 0;
return 0!=strncmp(text(line), msgprefix, sizeof(msgprefix)-1);
}
#define MAXHISTORY 500
void HistBrowser::addline( const char *str, int as_cmd ) {
char *what = (char *)str;
if(as_cmd) {
what = (char *)alloca( strlen(str) + sizeof(msgprefix) + 1 );
strcpy(what, msgprefix);
strcpy(what+sizeof(msgprefix)-1, str);
}
if(size() >= MAXHISTORY) {
for(int hyst = 0; hyst < 5; hyst++) {
remove( 1 );
min--;
max--;
}
}
add( what, (void *)as_cmd );
middleline( size() ); // bottomline() won't do
}
int HistBrowser::find_cmd( int fromline, int incr )
{
while(fromline > 0 && fromline <= size()) {
if(is_cmd(fromline))
return fromline;
fromline += incr;
}
return 0;
}
int HistBrowser::handle_nav( int ev ) {
int incr = 1;
int line;
HistInput *inp;
if(hist_ == NULL || (inp = hist_->input()) == NULL)
return 0;
if(ev == FL_KEYBOARD || ev == FL_SHORTCUT) {
switch(Fl::event_key()) {
case FL_Up:
case FL_Page_Up:
incr = -1; /* and fall into... */
case FL_Down:
case FL_Page_Down:
tighten_histrange();
line = selected(min) ? min : size()-incr;
line = find_cmd( line+incr, incr );
if(line) {
deselect(0);
select(line, 1);
middleline( line );
min = max = line;
inp->value( text(line) );
inp->position( inp->size(), inp->size() );
}
inp->take_focus();
return 1;
}
}
return 0;
}
void HistBrowser::picked_cb( HistBrowser *brow, void * ) {
int i, which = brow->value();
if(which <= 0) return;
if(brow->min > which) brow->min = which;
if(brow->max < which) brow->max = which;
brow->tighten_histrange();
if(!Fl::event_state(FL_BUTTON1|FL_BUTTON2|FL_BUTTON3)) {
// All mouse-buttons released -- it's worth rebuilding our selection.
int pos, len = 0;
const char *s;
for(i = brow->min; i <= brow->max; i++) {
if(brow->selected(i)) {
s = brow->text(i);
if(!strncmp(s, msgprefix, sizeof(msgprefix)-1))
s += MSGPREFIXSKIP;
len += strlen(s) + 1;
}
}
if(len == 0) {
Fl::selection_owner( NULL );
} else {
char *all = (char *)alloca( len + 1 );
pos = 0;
for(i = brow->min; i <= brow->max; i++) {
if(brow->selected(i)) {
s = brow->text(i);
if(!strncmp(s, msgprefix, sizeof(msgprefix)-1))
s += MSGPREFIXSKIP;
len = strlen(s);
memcpy(&all[pos], s, len);
pos += len;
all[pos++] = '\n';
}
}
Fl::selection( *brow, all, pos );
}
}
}