Debian Codegraph @

DiffChooser.cxx

raw symbols SCIP
1 //
2 // "$Id: DiffChooser.cxx 386 2006-03-04 14:15:27Z mike $"
3 //
4 // DiffChooser widget code.
5 //
6 // Copyright 2005 by Michael Sweet.
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License v2 as published
10 // by the Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // Contents:
18 //
19 // DiffChooser::DiffChooser() - Create a new chooser.
20 // DiffChooser::browser_cb() - Handle selections in the file browser.
21 // DiffChooser::count() - Return the number of selected files.
22 // DiffChooser::input_cb() - Handle input in the filename field.
23 // DiffChooser::menu_cb() - Handle favorites menu selections.
24 // DiffChooser::value() - Set the filename value.
25 // DiffChooser::value() - Get the filename for the specified item.
26 //
27
28 #include "DiffChooser.h"
29 #include "FavoritesWindow.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34
35 //
36 // 'DiffChooser::DiffChooser()' - Create a new chooser.
37 //
38
39 DiffChooser::DiffChooser(int X, // I - X position
40 int Y, // I - Y position
41 int W, // I - Width
42 int H, // I - Height
43 const char *L) // I - Label
44 : Fl_Group(X, Y, W, H, L),
45 input_(X, Y, W, 35),
46 menu_(X, Y + 35, W, 25, "Favorites"),
47 browser_(X, Y + 60, W, H - 60)
48 {
49 end();
50
51 resizable(&browser_);
52
53 browser_.callback((Fl_Callback *)browser_cb, this);
54 browser_.type(FL_MULTI_BROWSER);
55 browser_.when(FL_WHEN_CHANGED | FL_WHEN_NOT_CHANGED);
56
57 input_.callback((Fl_Callback *)input_cb, this);
58 input_.when(FL_WHEN_ENTER_KEY_CHANGED);
59
60 menu_.callback((Fl_Callback *)menu_cb, this);
61
62 value(".");
63 }
64
65
66 //
67 // 'DiffChooser::browser_cb()' - Handle selections in the file browser.
68 //
69
70 void
71 DiffChooser::browser_cb(Fl_File_Browser *fb,
72 // I - File browser widget
73 DiffChooser *dc)
74 // I - DiffChooser widget
75 {
76 char filename[1024]; // Filename
77
78
79 if (fb->value() <= 0)
80 return;
81
82 snprintf(filename, sizeof(filename), "%s%s", dc->directory_,
83 fb->text(fb->value()));
84
85 if (fl_filename_isdir(filename))
86 {
87 if (Fl::event_clicks())
88 dc->value(filename);
89 else
90 {
91 filename[strlen(filename) - 1] = '\0';
92
93 dc->input_.value(filename);
94 dc->do_callback();
95 }
96 }
97 else
98 {
99 dc->input_.value(filename);
100 dc->do_callback();
101 }
102 }
103
104
105 //
106 // 'DiffChooser::count()' - Return the number of selected files.
107 //
108
109 int // O - Count of selected items
110 DiffChooser::count()
111 {
112 int i, // Looping var
113 count; // Count of selected items
114
115
116 for (i = 1, count = 0; i <= browser_.size(); i ++)
117 if (browser_.selected(i))
118 count ++;
119
120 return (count);
121 }
122
123
124 //
125 // 'DiffChooser::input_cb()' - Handle input in the filename field.
126 //
127
128 void
129 DiffChooser::input_cb(Fl_File_Input *fi,// I - File input widget
130 DiffChooser *dc)// I - DiffChooser widget
131 {
132 const char *val; // Filename value
133 char matchname[1024], // Matching filename
134 *ptr; // Pointer into filename
135 int i, // Looping var
136 min_match, // Minimum number of matching chars
137 max_match, // Maximum number of matching chars
138 num_files, // Number of files in directory
139 first_line; // First matching line
140 const char *file; // File from directory
141
142
143 val = fi->value();
144 i = strlen(val);
145
146 if (Fl::event_key() == FL_Enter ||
147 Fl::event_key() == FL_KP_Enter ||
148 Fl::event_key() == '/' ||
149 (i > 0 && val[i - 1] == '/'))
150 {
151 if (!fl_filename_isdir(val) && *val)
152 {
153 dc->do_callback();
154 return;
155 }
156
157 dc->value(val);
158 }
159
160 matchname[sizeof(matchname) - 1] = '\0';
161
162 if (Fl::event_key() == '/' ||
163 Fl::event_key() == FL_Delete ||
164 Fl::event_key() == FL_BackSpace)
165 {
166 strncpy(matchname, val, sizeof(matchname) - 1);
167 if ((ptr = strrchr(matchname, '/')) != NULL)
168 ptr[1] = '\0';
169
170 if (strcmp(matchname, dc->directory_))
171 {
172 strcpy(dc->directory_, matchname);
173 dc->browser_.load(dc->directory_);
174 }
175 }
176
177 if ((val = strrchr(val, '/')) == NULL)
178 val = fi->value();
179 else
180 val ++;
181
182 // Do filename completion as possible...
183 num_files = dc->browser_.size();
184 min_match = strlen(val);
185 max_match = min_match + 1;
186 first_line = 0;
187
188 if (min_match > 0)
189 {
190 for (i = 1; i <= num_files && max_match > min_match; i ++)
191 {
192 file = dc->browser_.text(i);
193
194 if (!strncmp(val, file, min_match))
195 {
196 // OK, this one matches; check against the previous match
197 if (!first_line)
198 {
199 // First match; copy stuff over...
200 strncpy(matchname, file, sizeof(matchname) - 1);
201 max_match = strlen(matchname);
202
203 // Strip trailing /, if any...
204 if (matchname[max_match - 1] == '/')
205 {
206 max_match --;
207 matchname[max_match] = '\0';
208 }
209
210 // And then make sure that the item is visible
211 dc->browser_.topline(i);
212 first_line = i;
213 }
214 else
215 {
216 // Succeeding match; compare to find maximum string match...
217 while (max_match > min_match)
218 if (!strncmp(file, matchname, max_match))
219 break;
220 else
221 max_match --;
222
223 // Truncate the string as needed...
224 matchname[max_match] = '\0';
225 }
226 }
227 }
228 }
229
230 // Scroll the file list if we have a match...
231 dc->browser_.deselect(0);
232 if (first_line)
233 dc->browser_.select(first_line);
234 dc->browser_.redraw();
235
236 // Add a partial match to the input field...
237 if (first_line && min_match < max_match &&
238 Fl::event_key() != FL_BackSpace &&
239 Fl::event_key() != FL_Delete)
240 {
241 // Replace the text...
242 i = val - fi->value();
243 fi->replace(i, i + min_match, matchname);
244
245 // Highlight it with the cursor at the end of the selection so
246 // s/he can press the right arrow to accept the selection
247 // (Tab and End also do this for both cases.)
248 fi->position(i + max_match, i + min_match);
249 }
250 else
251 fi->position(fi->size());
252 }
253
254
255 //
256 // 'DiffChooser::menu_cb()' - Handle favorites menu selections.
257 //
258
259 void
260 DiffChooser::menu_cb(FavoritesMenu *fm, // I - Favorites menu widget
261 DiffChooser *dc) // I - DiffChooser widget
262 {
263 int i; // Menu item
264 FavoritesWindow *fw; // Favorites window
265
266
267 switch (i = fm->value())
268 {
269 case 0 : // Add to favorites
270 fm->add_favorite(dc->directory_);
271 break;
272
273 case 1 : // Manage favorites
274 fw = new FavoritesWindow();
275 fw->show();
276
277 while (fw->shown())
278 Fl::wait();
279
280 delete fw;
281 break;
282
283 case 2 : // Filesystems
284 dc->value("");
285 break;
286
287 default : // Something else
288 dc->value(fm->text(i));
289 break;
290 }
291 }
292
293
294 //
295 // 'DiffChooser::value()' - Set the filename value.
296 //
297
298 void
299 DiffChooser::value(const char *f) // I - New filename
300 {
301 char *ptr; // Pointer in filename
302
303
304 if (!f || !*f)
305 {
306 browser_.load("");
307 input_.value("");
308
309 directory_[0] = '\0';
310 return;
311 }
312
313 fl_filename_absolute(filename_, sizeof(filename_), f);
314
315 ptr = filename_ + strlen(filename_) - 3;
316 if (ptr > filename_ && (!strcmp(ptr, "/..") || !strcmp(ptr - 1, "/../")))
317 {
318 if (*ptr != '/')
319 ptr --;
320
321 *ptr = '\0';
322 if ((ptr = strrchr(filename_, '/')) != NULL)
323 *ptr = '\0';
324 }
325
326 if (fl_filename_isdir(filename_))
327 {
328 if (filename_[strlen(filename_) - 1] != '/')
329 snprintf(directory_, sizeof(directory_), "%s/", filename_);
330 else
331 strcpy(directory_, filename_);
332
333 input_.value(directory_);
334 }
335 else
336 {
337 input_.value(filename_);
338
339 if ((ptr = strrchr(filename_, '/')) != NULL)
340 ptr[1] = '\0';
341
342 strcpy(directory_, filename_);
343 }
344
345 browser_.load(directory_);
346 }
347
348
349 //
350 // 'DiffChooser::value()' - Get the filename for the specified item.
351 //
352
353 const char * // O - Filename
354 DiffChooser::value(int i) // I - Item number
355 {
356 // Range check...
357 if (i < 1 || i > browser_.size())
358 return (NULL);
359
360 snprintf(filename_, sizeof(filename_), "%s%s", directory_, browser_.text(i));
361 return (filename_);
362 }
363
364
365 //
366 // End of "$Id: DiffChooser.cxx 386 2006-03-04 14:15:27Z mike $".
367 //