filebox changes, mts fix, dvd/bd create changes, motionwindow layout
[goodguy/history.git] / cinelerra-5.1 / guicast / bcfilebox.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcdelete.h"
23 #include "bcfilebox.h"
24 #include "bclistboxitem.h"
25 #include "bcnewfolder.h"
26 #include "bcpixmap.h"
27 #include "bcrename.h"
28 #include "bcresources.h"
29 #include "bcsignals.h"
30 #include "bctitle.h"
31 #include "clip.h"
32 #include "condition.h"
33 #include "filesystem.h"
34 #include "keys.h"
35 #include "language.h"
36 #include "mutex.h"
37 #include <string.h>
38 #include <sys/stat.h>
39
40
41
42
43
44
45
46 BC_FileBoxRecent::BC_FileBoxRecent(BC_FileBox *filebox, int x, int y)
47  : BC_ListBox(x, y, 250,
48                 filebox->get_text_height(MEDIUMFONT) * FILEBOX_HISTORY_SIZE +
49                 BC_ScrollBar::get_span(SCROLL_HORIZ) +
50                 LISTBOX_MARGIN * 2, LISTBOX_TEXT, &filebox->recent_dirs,
51                 0, 0, 1, 0, 1)
52 {
53         this->filebox = filebox;
54 }
55
56 int BC_FileBoxRecent::handle_event()
57 {
58         BC_ListBoxItem *selection = get_selection(0, 0);
59         if( selection != 0 ) {
60                 char *path = selection->get_text();
61                 filebox->submit_dir(path);
62         }
63         return 1;
64 }
65
66
67
68
69
70
71
72
73
74
75
76 BC_FileBoxListBox::BC_FileBoxListBox(int x, int y, BC_FileBox *filebox)
77  : BC_ListBox(x, y, filebox->get_listbox_w(), filebox->get_listbox_h(y),
78                 filebox->get_display_mode(), filebox->list_column,
79                 filebox->column_titles, filebox->column_width,
80                 filebox->columns, 0, 0,
81                 filebox->select_multiple ? LISTBOX_MULTIPLE : LISTBOX_SINGLE,
82                 ICON_LEFT, 0)
83 {
84         this->filebox = filebox;
85         set_sort_column(filebox->sort_column);
86         set_sort_order(filebox->sort_order);
87         set_allow_drag_column(1);
88 }
89
90 BC_FileBoxListBox::~BC_FileBoxListBox()
91 {
92 }
93
94 int BC_FileBoxListBox::handle_event()
95 {
96         filebox->submit_file(filebox->textbox->get_text());
97         return 1;
98 }
99
100 int BC_FileBoxListBox::selection_changed()
101 {
102         BC_ListBoxItem *item = get_selection(
103                 filebox->column_of_type(FILEBOX_NAME), 0);
104
105         if(item) {
106                 char path[BCTEXTLEN];
107                 strcpy(path, item->get_text());
108                 filebox->textbox->update(path);
109                 filebox->fs->extract_dir(filebox->directory, path);
110                 filebox->fs->extract_name(filebox->filename, path);
111                 filebox->fs->complete_path(path);
112                 strcpy(filebox->current_path, path);
113                 strcpy(filebox->submitted_path, path);
114         }
115         return 1;
116 }
117
118 int BC_FileBoxListBox::column_resize_event()
119 {
120         for(int i = 0; i < filebox->columns; i++)
121                 BC_WindowBase::get_resources()->filebox_columnwidth[i] =
122                         filebox->column_width[i] =
123                         get_column_width(i);
124         return 1;
125 }
126
127 int BC_FileBoxListBox::sort_order_event()
128 {
129         get_resources()->filebox_sortcolumn = filebox->sort_column = get_sort_column();
130         get_resources()->filebox_sortorder = filebox->sort_order = get_sort_order();
131         filebox->refresh(-1);
132         return 1;
133 }
134
135 int BC_FileBoxListBox::move_column_event()
136 {
137         filebox->move_column(get_from_column(), get_to_column());
138         return 1;
139 }
140
141 int BC_FileBoxListBox::evaluate_query(char *string)
142 {
143 // Search name column
144         ArrayList<BC_ListBoxItem*> *column =
145                 &filebox->list_column[filebox->column_of_type(FILEBOX_NAME)];
146 // Get current selection
147         int current_selection = get_selection_number(0, 0);
148
149 // Get best score in remaining items
150         int lowest_score = 0x7fffffff;
151         int best_item = -1;
152         if(current_selection < 0) current_selection = 0;
153 //      for(int i = current_selection + 1; i < column->size(); i++)
154         for(int i = current_selection; i < column->size(); i++)
155         {
156                 int len1 = strlen(string);
157                 int len2 = strlen(column->get(i)->get_text());
158                 int current_score = strncasecmp(string,
159                         column->get(i)->get_text(),
160                         MIN(len1, len2));
161 //printf(" %d i=%d %d %s %s\n", __LINE__, i, current_score, string, column->get(i)->get_text());
162
163                 if(abs(current_score) < lowest_score)
164                 {
165                         lowest_score = abs(current_score);
166                         best_item = i;
167                 }
168         }
169
170
171         return best_item;
172 }
173
174
175
176
177 BC_FileBoxTextBox::BC_FileBoxTextBox(int x, int y, BC_FileBox *filebox)
178  : BC_TextBox(x, y, filebox->get_w() - 20, 1,
179         filebox->want_directory ?  filebox->directory : filebox->filename)
180 {
181         this->filebox = filebox;
182 }
183
184 BC_FileBoxTextBox::~BC_FileBoxTextBox()
185 {
186 }
187
188
189
190
191 int BC_FileBoxTextBox::handle_event()
192 {
193         int result = 0;
194         if(get_keypress() != RETURN)
195         {
196                 result = calculate_suggestions(&filebox->list_column[0]);
197         }
198         return result;
199 }
200
201
202
203
204 BC_FileBoxDirectoryText::BC_FileBoxDirectoryText(int x, int y, BC_FileBox *filebox)
205  : BC_TextBox(x, y, filebox->get_w() - 40, 1, filebox->fs->get_current_dir())
206 {
207         this->filebox = filebox;
208 }
209
210 int BC_FileBoxDirectoryText::handle_event()
211 {
212         const char *path = get_text();
213         if( !filebox->fs->is_dir(path) ) return 0;
214         const char *cp = path;
215         while( *cp ) ++cp;
216         if( cp > path && *--cp != '/' ) return 0;
217         char *file_path = FileSystem::basepath(path);
218         char *dir_path = FileSystem::basepath(filebox->fs->get_current_dir());
219         int ret = !strcmp(file_path, dir_path) ? 0 : 1;
220         if( ret ) {
221                 strcpy(filebox->directory, file_path);
222                 filebox->update_history();
223                 filebox->fs->change_dir(file_path);
224                 filebox->refresh(1);
225         }
226         delete [] dir_path;
227         delete [] file_path;
228         return ret;
229 }
230
231
232
233
234
235 BC_FileBoxFilterText::BC_FileBoxFilterText(int x, int y, BC_FileBox *filebox)
236  : BC_TextBox(x, y, filebox->get_w() - 50, 1, filebox->get_resources()->filebox_filter)
237 {
238         this->filebox = filebox;
239 }
240
241 int BC_FileBoxFilterText::handle_event()
242 {
243         filebox->update_filter(get_text());
244         return 1;
245 }
246
247
248
249
250 BC_FileBoxFilterMenu::BC_FileBoxFilterMenu(int x, int y, BC_FileBox *filebox)
251  : BC_ListBox(x,
252         y,
253         filebox->get_w() - 30,
254         120,
255         LISTBOX_TEXT,
256         &filebox->filter_list,
257         0,
258         0,
259         1,
260         0,
261         1)
262 {
263         this->filebox = filebox;
264         set_tooltip(_("Change the filter"));
265 }
266
267 int BC_FileBoxFilterMenu::handle_event()
268 {
269         filebox->filter_text->update(
270                 get_selection(filebox->column_of_type(FILEBOX_NAME), 0)->get_text());
271         filebox->update_filter(
272                 get_selection(filebox->column_of_type(FILEBOX_NAME), 0)->get_text());
273         return 1;
274 }
275
276
277
278
279
280
281
282
283
284
285 BC_FileBoxCancel::BC_FileBoxCancel(BC_FileBox *filebox)
286  : BC_CancelButton(filebox)
287 {
288         this->filebox = filebox;
289         set_tooltip(_("Cancel the operation"));
290 }
291
292 BC_FileBoxCancel::~BC_FileBoxCancel()
293 {
294 }
295
296 int BC_FileBoxCancel::handle_event()
297 {
298 //      filebox->submit_file(filebox->textbox->get_text());
299         filebox->newfolder_thread->interrupt();
300         filebox->set_done(1);
301         return 1;
302 }
303
304
305
306
307
308
309
310 BC_FileBoxUseThis::BC_FileBoxUseThis(BC_FileBox *filebox)
311  : BC_Button(filebox->get_w() / 2 -
312                 BC_WindowBase::get_resources()->usethis_button_images[0]->get_w() / 2,
313         filebox->ok_button->get_y(),
314         BC_WindowBase::get_resources()->usethis_button_images)
315 {
316         this->filebox = filebox;
317         set_tooltip(_("Submit the directory"));
318 }
319
320 BC_FileBoxUseThis::~BC_FileBoxUseThis()
321 {
322 }
323
324 int BC_FileBoxUseThis::handle_event()
325 {
326 // printf("BC_FileBoxUseThis::handle_event %d '%s'\n",
327 // __LINE__,
328 // filebox->textbox->get_text());
329         filebox->submit_file(filebox->textbox->get_text(), 1);
330         return 1;
331 }
332
333
334
335
336
337 BC_FileBoxOK::BC_FileBoxOK(BC_FileBox *filebox)
338  : BC_OKButton(filebox,
339         !filebox->want_directory ?
340                 BC_WindowBase::get_resources()->ok_images :
341                 BC_WindowBase::get_resources()->filebox_descend_images)
342 {
343         this->filebox = filebox;
344         if(filebox->want_directory)
345                 set_tooltip(_("Descend directory"));
346         else
347                 set_tooltip(_("Submit the file"));
348 }
349
350 BC_FileBoxOK::~BC_FileBoxOK()
351 {
352 }
353
354 int BC_FileBoxOK::handle_event()
355 {
356 //printf("BC_FileBoxOK::handle_event %d\n", __LINE__);
357         const char *path = filebox->textbox->get_text();
358         if( *path ) filebox->submit_file(path);
359         return 1;
360 }
361
362
363 BC_FileBoxText::BC_FileBoxText(int x, int y, BC_FileBox *filebox)
364  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_text_images)
365 {
366         this->filebox = filebox;
367         set_tooltip(_("Display text"));
368 }
369 int BC_FileBoxText::handle_event()
370 {
371         filebox->create_listbox(filebox->listbox->get_x(), filebox->listbox->get_y(), LISTBOX_TEXT);
372         filebox->listbox->show_window(1);
373         return 1;
374 }
375
376
377 BC_FileBoxIcons::BC_FileBoxIcons(int x, int y, BC_FileBox *filebox)
378  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_icons_images)
379 {
380         this->filebox = filebox;
381         set_tooltip(_("Display icons"));
382 }
383 int BC_FileBoxIcons::handle_event()
384 {
385         filebox->create_listbox(filebox->listbox->get_x(), filebox->listbox->get_y(), LISTBOX_ICONS);
386         filebox->listbox->show_window(1);
387         return 1;
388 }
389
390
391 BC_FileBoxNewfolder::BC_FileBoxNewfolder(int x, int y, BC_FileBox *filebox)
392  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_newfolder_images)
393 {
394         this->filebox = filebox;
395         set_tooltip(_("Create new folder"));
396 }
397 int BC_FileBoxNewfolder::handle_event()
398 {
399         filebox->newfolder_thread->start_new_folder();
400         return 1;
401 }
402
403
404 BC_FileBoxRename::BC_FileBoxRename(int x, int y, BC_FileBox *filebox)
405  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_rename_images)
406 {
407         this->filebox = filebox;
408         set_tooltip(_("Rename file"));
409 }
410 int BC_FileBoxRename::handle_event()
411 {
412         filebox->rename_thread->start_rename();
413         return 1;
414 }
415
416 BC_FileBoxUpdir::BC_FileBoxUpdir(int x, int y, BC_FileBox *filebox)
417  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_updir_images)
418 {
419         this->filebox = filebox;
420         set_tooltip(_("Up a directory"));
421 }
422 int BC_FileBoxUpdir::handle_event()
423 {
424 // Need a temp so submit_file can expand it
425         sprintf(string, "..");
426         filebox->submit_file(string);
427         return 1;
428 }
429
430 BC_FileBoxDelete::BC_FileBoxDelete(int x, int y, BC_FileBox *filebox)
431  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_delete_images)
432 {
433         this->filebox = filebox;
434         set_tooltip(_("Delete files"));
435 }
436 int BC_FileBoxDelete::handle_event()
437 {
438         filebox->unlock_window();
439         filebox->delete_thread->start();
440         filebox->lock_window("BC_FileBoxDelete::handle_event");
441         return 1;
442 }
443
444 BC_FileBoxReload::BC_FileBoxReload(int x, int y, BC_FileBox *filebox)
445  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_reload_images)
446 {
447         this->filebox = filebox;
448         set_tooltip(_("Refresh"));
449 }
450 int BC_FileBoxReload::handle_event()
451 {
452         filebox->refresh();
453         return 1;
454 }
455
456
457
458
459
460
461
462
463
464
465 BC_FileBox::BC_FileBox(int x, int y, const char *init_path,
466                 const char *title, const char *caption, int show_all_files,
467                 int want_directory, int multiple_files, int h_padding)
468  : BC_Window(title, x, y,
469         BC_WindowBase::get_resources()->filebox_w,
470         BC_WindowBase::get_resources()->filebox_h,
471         400, 300, 1, 0, 1)
472 {
473         fs = new FileSystem;
474 //      if(want_directory)
475 //      {
476 //              fs->set_want_directory();
477 //              columns = DIRBOX_COLUMNS;
478 //              columns = FILEBOX_COLUMNS;
479 //      }
480 //      else
481         {
482                 columns = FILEBOX_COLUMNS;
483         }
484
485         list_column = new ArrayList<BC_ListBoxItem*>[columns];
486         column_type = new int[columns];
487         column_width = new int[columns];
488
489         filter_text = 0;
490         filter_popup = 0;
491         usethis_button = 0;
492
493         strcpy(this->caption, caption);
494         strcpy(this->current_path, init_path);
495         strcpy(this->submitted_path, init_path);
496         select_multiple = multiple_files;
497         this->want_directory = want_directory;
498         if(show_all_files) fs->set_show_all();
499         fs->complete_path(this->current_path);
500         fs->complete_path(this->submitted_path);
501         fs->extract_dir(directory, this->current_path);
502         fs->extract_name(filename, this->current_path);
503
504 // printf("BC_FileBox::BC_FileBox %d '%s' '%s' '%s'\n",
505 // __LINE__,
506 // this->submitted_path,
507 // directory,
508 // filename);
509
510 //      if(want_directory)
511 //      {
512 //              for(int i = 0; i < columns; i++)
513 //              {
514 //                      column_type[i] = get_resources()->dirbox_columntype[i];
515 //                      column_width[i] = get_resources()->dirbox_columnwidth[i];
516 //                      column_titles[i] = BC_FileBox::columntype_to_text(column_type[i]);
517 //              }
518 //              sort_column = get_resources()->dirbox_sortcolumn;
519 //              sort_order = get_resources()->dirbox_sortorder;
520 //      }
521 //      else
522         {
523                 for(int i = 0; i < columns; i++)
524                 {
525                         column_type[i] = get_resources()->filebox_columntype[i];
526                         column_width[i] = get_resources()->filebox_columnwidth[i];
527                         column_titles[i] = (char*)BC_FileBox::columntype_to_text(column_type[i]);
528                 }
529                 sort_column = get_resources()->filebox_sortcolumn;
530                 sort_order = get_resources()->filebox_sortorder;
531         }
532
533
534
535 // Test if current directory exists
536         if(!fs->is_dir(directory))
537         {
538                 sprintf(this->current_path, "~");
539                 fs->complete_path(this->current_path);
540                 fs->set_current_dir(this->current_path);
541 //              fs->update(this->current_path);
542                 strcpy(directory, fs->get_current_dir());
543                 filename[0] = 0;
544         }
545         else
546                 fs->set_current_dir(this->directory);
547
548
549         if(h_padding == -1)
550         {
551                 h_padding = BC_WindowBase::get_resources()->ok_images[0]->get_h() -
552                         20;
553         }
554         this->h_padding = h_padding;
555         delete_thread = new BC_DeleteThread(this);
556         rename_thread = 0;
557 }
558
559 BC_FileBox::~BC_FileBox()
560 {
561 // this has to be destroyed before tables, because it can call for an update!
562         delete newfolder_thread;
563         delete fs;
564         delete_tables();
565         for(int i = 0; i < TOTAL_ICONS; i++)
566                 delete icons[i];
567         filter_list.remove_all_objects();
568         delete [] list_column;
569         delete [] column_type;
570         delete [] column_width;
571         delete delete_thread;
572         delete rename_thread;
573         recent_dirs.remove_all_objects();
574 }
575
576 void BC_FileBox::create_objects()
577 {
578         int x = 10, y = 10;
579         BC_Resources *resources = BC_WindowBase::get_resources();
580         int directory_title_margin = MAX(20,
581                 resources->filebox_text_images[0]->get_h());
582
583 // Create recent dir list
584         create_history();
585         update_history();
586
587 // Directories aren't filtered in FileSystem so skip this
588         if(!want_directory)
589         {
590                 filter_list.append(new BC_ListBoxItem("*"));
591                 filter_list.append(new BC_ListBoxItem("[*.mkv]"));
592                 filter_list.append(new BC_ListBoxItem("[*.mp4]"));
593                 filter_list.append(new BC_ListBoxItem("[*.mp2][*.mp3][*.wav]"));
594                 filter_list.append(new BC_ListBoxItem("[*.avi][*.mpg][*.m2v][*.m1v][*.mov]"));
595                 filter_list.append(new BC_ListBoxItem("*.xml"));
596                 fs->set_filter(get_resources()->filebox_filter);
597         }
598
599         fs->set_sort_order(sort_order);
600         fs->set_sort_field(column_type[sort_column]);
601         fs->update(directory);
602
603         create_icons();
604         create_tables();
605
606         add_subwindow(ok_button = new BC_FileBoxOK(this));
607         if(want_directory)
608                 add_subwindow(usethis_button = new BC_FileBoxUseThis(this));
609         add_subwindow(cancel_button = new BC_FileBoxCancel(this));
610
611         add_subwindow(new BC_Title(x, y, caption));
612
613         x = get_w() - resources->filebox_icons_images[0]->get_w() - 10;
614
615         add_subwindow(icon_button = new BC_FileBoxIcons(x, y, this));
616         x -= resources->filebox_text_images[0]->get_w() + 5;
617
618         add_subwindow(text_button = new BC_FileBoxText(x, y, this));
619         x -= resources->filebox_newfolder_images[0]->get_w() + 5;
620
621         add_subwindow(folder_button = new BC_FileBoxNewfolder(x, y, this));
622         x -= resources->filebox_delete_images[0]->get_w() + 5;
623
624         add_subwindow(rename_button = new BC_FileBoxRename(x, y, this));
625         x -= resources->filebox_delete_images[0]->get_w() + 5;
626
627         add_subwindow(delete_button = new BC_FileBoxDelete(x, y, this));
628         x -= resources->filebox_reload_images[0]->get_w() + 5;
629
630         add_subwindow(reload_button = new BC_FileBoxReload(x, y, this));
631         x -= resources->filebox_updir_images[0]->get_w() + 5;
632
633         add_subwindow(updir_button = new BC_FileBoxUpdir(x, y, this));
634
635         x = 10;
636         y += directory_title_margin + 3;
637
638         add_subwindow(recent_popup = new BC_FileBoxRecent(this, x, y));
639         add_subwindow(directory_title = new BC_FileBoxDirectoryText(x, y, this));
640         directory_title->reposition_window(x, y,
641                 get_w() - recent_popup->get_w() -  20, 1);
642         recent_popup->reposition_window(
643                 x + directory_title->get_w(), y,
644                 directory_title->get_w(), 200);
645
646         x = 10;
647         y += directory_title->get_h() + 5;
648
649         int newest_id = 0, newest = -1;
650         for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++) {
651                 if( !resources->filebox_history[i].path[0] ) continue;
652                 if( resources->filebox_history[i].id > newest_id ) {
653                         newest_id = resources->filebox_history[i].id;
654                         newest = i;
655                 }
656         }
657         if( newest >= 0 ) {
658                 strcpy(directory, resources->filebox_history[newest].path);
659                 fs->change_dir(directory, 0);
660                 directory_title->update(fs->get_current_dir());
661         }
662
663         listbox = 0;
664         create_listbox(x, y, get_display_mode());
665         y += listbox->get_h() + 10;
666         add_subwindow(textbox = new BC_FileBoxTextBox(x, y, this));
667         y += textbox->get_h() + 10;
668
669         if(!want_directory) {
670                 add_subwindow(filter_text = new BC_FileBoxFilterText(x, y, this));
671                 add_subwindow(filter_popup =
672                         new BC_FileBoxFilterMenu(x + filter_text->get_w(), y, this));;
673         }
674
675 // listbox has to be active because refresh might be called from newfolder_thread
676         listbox->activate();
677         newfolder_thread = new BC_NewFolderThread(this);
678
679         rename_thread = new BC_RenameThread(this);
680
681
682         show_window();
683 }
684
685 int BC_FileBox::get_listbox_w()
686 {
687         return get_w() - 20;
688 }
689
690 int BC_FileBox::get_listbox_h(int y)
691 {
692         int result = get_h() -
693                 y -
694                 h_padding;
695         if(want_directory)
696                 result -= BC_WindowBase::get_resources()->dirbox_margin;
697         else
698                 result -= BC_WindowBase::get_resources()->filebox_margin;
699
700         return result;
701 }
702
703 int BC_FileBox::create_icons()
704 {
705         for(int i = 0; i < TOTAL_ICONS; i++)
706         {
707                 icons[i] = new BC_Pixmap(this,
708                         BC_WindowBase::get_resources()->type_to_icon[i],
709                         PIXMAP_ALPHA);
710         }
711         return 0;
712 }
713
714 int BC_FileBox::resize_event(int w, int h)
715 {
716         draw_background(0, 0, w, h);
717         flash(0);
718
719 // OK button handles resize event itself
720 //      ok_button->reposition_window(ok_button->get_x(),
721 //              h - (get_h() - ok_button->get_y()));
722 //      cancel_button->reposition_window(w - (get_w() - cancel_button->get_x()),
723 //              h - (get_h() - cancel_button->get_y()));
724         if(usethis_button)
725                 usethis_button->reposition_window(w / 2 - 50,
726                         h - (get_h() - usethis_button->get_y()));
727
728
729         if(filter_popup) filter_popup->reposition_window(w - (get_w() - filter_popup->get_x()),
730                 h - (get_h() - filter_popup->get_y()),
731                 w - 30,
732                 0);
733
734
735         if(filter_text) filter_text->reposition_window(filter_text->get_x(),
736                 h - (get_h() - filter_text->get_y()),
737                 w - (get_w() - filter_text->get_w()),
738                 1);
739         directory_title->reposition_window(
740                 directory_title->get_x(),
741                 directory_title->get_y(),
742                 get_w() - recent_popup->get_w() -  20,
743                 1);
744         recent_popup->reposition_window(
745                 directory_title->get_x() + directory_title->get_w(),
746                 directory_title->get_y(),
747                 directory_title->get_w() + recent_popup->get_w(),
748                 recent_popup->get_h());
749         textbox->reposition_window(textbox->get_x(),
750                 h - (get_h() - textbox->get_y()),
751                 w - (get_w() - textbox->get_w()),
752                 1);
753         listbox->reposition_window(listbox->get_x(),
754                 listbox->get_y(),
755                 w - (get_w() - listbox->get_w()),
756                 h - (get_h() - listbox->get_h()),
757                 0);
758         int dx = w - get_w();
759         icon_button->reposition_window(icon_button->get_x()+dx, icon_button->get_y());
760         text_button->reposition_window(text_button->get_x()+dx, text_button->get_y());
761         folder_button->reposition_window(folder_button->get_x()+dx, folder_button->get_y());
762         rename_button->reposition_window(rename_button->get_x()+dx, rename_button->get_y());
763         reload_button->reposition_window(reload_button->get_x()+dx, reload_button->get_y());
764         delete_button->reposition_window(delete_button->get_x()+dx, delete_button->get_y());
765         updir_button->reposition_window(updir_button->get_x()+dx, updir_button->get_y());
766         set_w(w);  set_h(h);
767         get_resources()->filebox_w = get_w();
768         get_resources()->filebox_h = get_h();
769         flush();
770         return 1;
771 }
772
773 int BC_FileBox::keypress_event()
774 {
775         switch(get_keypress())
776         {
777                 case 'w':
778                         if(ctrl_down()) set_done(1);
779                         return 1;
780                         break;
781         }
782         return 0;
783 }
784
785 int BC_FileBox::close_event()
786 {
787         set_done(1);
788         return 1;
789 }
790
791 int BC_FileBox::handle_event()
792 {
793         return 0;
794 }
795
796 int BC_FileBox::extract_extension(char *out, const char *in)
797 {
798         *out = 0;
799         int i = strlen(in);
800         while( --i>=0 && in[i]!='.' );
801         if( i >= 0 ) strcpy(out, &in[++i]);
802         return 0;
803 }
804
805 int BC_FileBox::create_tables()
806 {
807         delete_tables();
808         char string[BCTEXTLEN];
809         BC_ListBoxItem *new_item;
810
811         fs->set_sort_order(sort_order);
812         fs->set_sort_field(column_type[sort_column]);
813
814         for(int i = 0; i < fs->total_files(); i++)
815         {
816                 FileItem *file_item = fs->get_entry(i);
817                 int is_dir = file_item->is_dir;
818                 BC_Pixmap* icon = get_icon(file_item->name, is_dir);
819
820 // Name entry
821                 new_item = new BC_ListBoxItem(file_item->name,
822                         icon,
823                         is_dir ? get_resources()->directory_color : get_resources()->file_color);
824                 if(is_dir) new_item->set_searchable(0);
825                 list_column[column_of_type(FILEBOX_NAME)].append(new_item);
826
827 // Size entry
828 //              if(!want_directory)
829 //              {
830                         if(!is_dir)
831                         {
832                                 sprintf(string, "%jd", file_item->size);
833                                 new_item = new BC_ListBoxItem(string, get_resources()->file_color);
834                         }
835                         else
836                         {
837                                 new_item = new BC_ListBoxItem("", get_resources()->directory_color);
838                         }
839
840                         list_column[column_of_type(FILEBOX_SIZE)].append(new_item);
841 //              }
842
843 // Date entry
844                 if(!is_dir || 1)
845                 {
846                         static const char *month_text[13] = { "Nul",
847                                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
848                                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
849                         };
850                         sprintf(string,
851                                 "%s %d, %d",
852                                 month_text[file_item->month],
853                                 file_item->day,
854                                 file_item->year);
855                         new_item = new BC_ListBoxItem(string, get_resources()->file_color);
856                 }
857                 else
858                 {
859                         new_item = new BC_ListBoxItem("", get_resources()->directory_color);
860                 }
861
862                 list_column[column_of_type(FILEBOX_DATE)].append(new_item);
863
864 // Extension entry
865 //              if(!want_directory)
866 //              {
867                         if(!is_dir) {
868                                 extract_extension(string, file_item->name);
869                                 new_item = new BC_ListBoxItem(string, get_resources()->file_color);
870                         }
871                         else {
872                                 new_item = new BC_ListBoxItem("", get_resources()->directory_color);
873                         }
874                         list_column[column_of_type(FILEBOX_EXTENSION)].append(new_item);
875 //              }
876         }
877
878         return 0;
879 }
880
881 int BC_FileBox::delete_tables()
882 {
883         for(int j = 0; j < columns; j++)
884         {
885                 list_column[j].remove_all_objects();
886         }
887         return 0;
888 }
889
890 BC_Pixmap* BC_FileBox::get_icon(char *path, int is_dir)
891 {
892         char *suffix = strrchr(path, '.');
893         int icon_type = ICON_UNKNOWN;
894
895         if(is_dir) return icons[ICON_FOLDER];
896
897         if(suffix)
898         {
899                 suffix++;
900                 if(*suffix != 0)
901                 {
902                         for(int i = 0; i < TOTAL_SUFFIXES; i++)
903                         {
904                                 if(!strcasecmp(suffix, BC_WindowBase::get_resources()->suffix_to_type[i].suffix))
905                                 {
906                                         icon_type = BC_WindowBase::get_resources()->suffix_to_type[i].icon_type;
907                                         break;
908                                 }
909                         }
910                 }
911         }
912
913         return icons[icon_type];
914 }
915
916 const char* BC_FileBox::columntype_to_text(int type)
917 {
918         switch(type) {
919                 case FILEBOX_NAME: return FILEBOX_NAME_TEXT;
920                 case FILEBOX_SIZE: return FILEBOX_SIZE_TEXT;
921                 case FILEBOX_DATE: return FILEBOX_DATE_TEXT;
922                 case FILEBOX_EXTENSION: return FILEBOX_EXTENSION_TEXT;
923         }
924         return "";
925 }
926
927 int BC_FileBox::column_of_type(int type)
928 {
929         for(int i = 0; i < columns; i++)
930                 if(column_type[i] == type) return i;
931         return 0;
932 }
933
934
935
936 int BC_FileBox::refresh(int reset)
937 {
938         fs->set_sort_order(sort_order);
939         fs->set_sort_field(column_type[sort_column]);
940         if( reset >= 0 )
941                 fs->update(0);
942         else
943                 fs->update_sort();
944         create_tables();
945         listbox->set_master_column(column_of_type(FILEBOX_NAME), 0);
946         listbox->update(list_column, column_titles, column_width, columns,
947                 reset>0 ? 0 : listbox->get_xposition(),
948                 reset>0 ? 0 : listbox->get_yposition(),
949                 -1, 1);
950         return 0;
951 }
952
953 int BC_FileBox::update_filter(const char *filter)
954 {
955         fs->set_filter(filter);
956 //      fs->update(0);
957         refresh();
958         strcpy(get_resources()->filebox_filter, filter);
959
960         return 0;
961 }
962
963
964 void BC_FileBox::move_column(int src, int dst)
965 {
966         if(src != dst)
967         {
968                 ArrayList<BC_ListBoxItem*> *new_columns =
969                         new ArrayList<BC_ListBoxItem*>[columns];
970                 int *new_types = new int[columns];
971                 int *new_widths = new int[columns];
972
973         // Fill in remaining columns with consecutive data
974                 for(int out_column = 0, in_column = 0;
975                         out_column < columns;
976                         out_column++,
977                         in_column++)
978                 {
979         // Copy destination column from src column
980                         if(out_column == dst)
981                         {
982                                 for(int i = 0; i < list_column[src].total; i++)
983                                 {
984                                         new_columns[out_column].append(list_column[src].values[i]);
985                                 }
986                                 new_types[out_column] = column_type[src];
987                                 new_widths[out_column] = column_width[src];
988                                 in_column--;
989                         }
990                         else
991                         {
992         // Skip source column
993                                 if(in_column == src) in_column++;
994                                 for(int i = 0; i < list_column[src].total; i++)
995                                 {
996                                         new_columns[out_column].append(list_column[in_column].values[i]);
997                                 }
998                                 new_types[out_column] = column_type[in_column];
999                                 new_widths[out_column] = column_width[in_column];
1000                         }
1001                 }
1002
1003         // Swap tables
1004                 delete [] list_column;
1005                 delete [] column_type;
1006                 delete [] column_width;
1007                 list_column = new_columns;
1008                 column_type = new_types;
1009                 column_width = new_widths;
1010
1011                 for(int i = 0; i < columns; i++)
1012                 {
1013                         get_resources()->filebox_columntype[i] = column_type[i];
1014                         get_resources()->filebox_columnwidth[i] = column_width[i];
1015                         column_titles[i] = (char*)BC_FileBox::columntype_to_text(column_type[i]);
1016                 }
1017         }
1018
1019         refresh();
1020 }
1021
1022
1023 int BC_FileBox::submit_dir(char *dir)
1024 {
1025         strcpy(directory, dir);
1026         fs->join_names(current_path, directory, filename);
1027
1028 // printf("BC_FileBox::submit_dir %d '%s' '%s' '%s'\n",
1029 // __LINE__,
1030 // current_path,
1031 // directory,
1032 // filename);
1033         strcpy(submitted_path, current_path);
1034         fs->change_dir(dir, 0);
1035         refresh(1);
1036         directory_title->update(fs->get_current_dir());
1037         if(want_directory)
1038                 textbox->update(fs->get_current_dir());
1039         else
1040                 textbox->update(filename);
1041         listbox->reset_query();
1042         return 0;
1043 }
1044
1045 int BC_FileBox::submit_file(const char *path, int use_this)
1046 {
1047 // Deactivate textbox to hide suggestions
1048         textbox->deactivate();
1049
1050 // If file wanted, take the current directory as the desired file.
1051 // If directory wanted, ignore it.
1052         if(!path[0] && !want_directory)
1053         {
1054 // save complete path
1055                 strcpy(this->current_path, directory);
1056 // save complete path
1057                 strcpy(this->submitted_path, directory);
1058                 update_history();
1059 // Zero out filename
1060                 filename[0] = 0;
1061                 set_done(0);
1062                 return 0;
1063         }
1064
1065 // is a directory, change directories
1066         if(fs->is_dir(path) && !use_this)
1067         {
1068                 fs->change_dir(path, 0);
1069                 refresh(1);
1070                 directory_title->update(fs->get_current_dir());
1071                 strcpy(this->current_path, fs->get_current_dir());
1072                 strcpy(this->submitted_path, fs->get_current_dir());
1073                 strcpy(this->directory, fs->get_current_dir());
1074                 update_history();
1075                 filename[0] = 0;
1076                 if(want_directory)
1077                         textbox->update(fs->get_current_dir());
1078                 else
1079                         textbox->update("");
1080                 listbox->reset_query();
1081                 return 1;
1082         }
1083         else
1084 // Is a file or desired directory.  Quit the operation.
1085         {
1086                 char path2[BCTEXTLEN];
1087                 strcpy(path2, path);
1088
1089 // save directory for defaults
1090                 fs->extract_dir(directory, path2);
1091
1092 // Just take the directory
1093                 if(want_directory)
1094                 {
1095                         filename[0] = 0;
1096                         strcpy(path2, directory);
1097                 }
1098                 else
1099 // Take the complete path
1100                 {
1101                         fs->extract_name(filename, path2);     // save filename
1102                 }
1103
1104                 fs->complete_path(path2);
1105                 strcpy(this->current_path, path2);          // save complete path
1106                 strcpy(this->submitted_path, path2);          // save complete path
1107                 update_history();
1108                 newfolder_thread->interrupt();
1109                 set_done(0);
1110                 return 0;
1111         }
1112         return 0;
1113 }
1114
1115 void BC_FileBox::update_history()
1116 {
1117 // Look for path already in history
1118         BC_Resources *resources = get_resources();
1119         char path[BCTEXTLEN];
1120         strcpy(path, directory);
1121 // enfore one trailing slash
1122         char *cp = path;
1123         while( *cp ) ++cp;
1124         while( cp > path && *(cp-1) == '/' ) --cp;
1125         *cp++ = '/';  *cp = 0;
1126
1127 //      int new_slot = FILEBOX_HISTORY_SIZE - 1;
1128
1129         for(int i = FILEBOX_HISTORY_SIZE - 1; i >= 0; i--)
1130         {
1131                 if(resources->filebox_history[i].path[0] &&
1132                         !strcmp(resources->filebox_history[i].path, path))
1133                 {
1134 // Got matching path.
1135 // Update ID.
1136                         resources->filebox_history[i].id = resources->get_filebox_id();
1137                         return;
1138                 }
1139 // // Shift down from this point.
1140 //                      while(i > 0)
1141 //                      {
1142 //                              strcpy(resources->filebox_history[i],
1143 //                                      resources->filebox_history[i - 1]);
1144 //                              if(resources->filebox_history[i][0]) new_slot--;
1145 //                              i--;
1146 //                      }
1147 //                      break;
1148 //              }
1149 //              else
1150 //                      if(resources->filebox_history[i][0])
1151 //                              new_slot--;
1152 //              else
1153 //                      break;
1154         }
1155
1156 // Remove oldest entry if full
1157         if(resources->filebox_history[FILEBOX_HISTORY_SIZE - 1].path[0])
1158         {
1159                 int oldest_id = 0x7fffffff;
1160                 int oldest = 0;
1161                 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
1162                 {
1163                         if(resources->filebox_history[i].path[0] &&
1164                                 resources->filebox_history[i].id < oldest_id)
1165                         {
1166                                 oldest_id = resources->filebox_history[i].id;
1167                                 oldest = i;
1168                         }
1169                 }
1170
1171                 for(int i = oldest; i < FILEBOX_HISTORY_SIZE - 1; i++)
1172                 {
1173                         strcpy(resources->filebox_history[i].path,
1174                                 resources->filebox_history[i + 1].path);
1175                         resources->filebox_history[i].id =
1176                                 resources->filebox_history[i + 1].id;
1177                 }
1178         }
1179
1180 // Create new entry
1181         strcpy(resources->filebox_history[FILEBOX_HISTORY_SIZE - 1].path, path);
1182         resources->filebox_history[FILEBOX_HISTORY_SIZE - 1].id = resources->get_filebox_id();
1183
1184 // Alphabetize
1185         int done = 0;
1186         while(!done)
1187         {
1188                 done = 1;
1189                 for(int i = 1; i < FILEBOX_HISTORY_SIZE; i++)
1190                 {
1191                         if( (resources->filebox_history[i - 1].path[0] &&
1192                                 resources->filebox_history[i].path[0] &&
1193                                 strcasecmp(resources->filebox_history[i - 1].path,
1194                                         resources->filebox_history[i].path) > 0) ||
1195                                 (resources->filebox_history[i - 1].path[0] == 0 &&
1196                                         resources->filebox_history[i].path[0]) )
1197                         {
1198                                 done = 0;
1199                                 char temp[BCTEXTLEN];
1200                                 int id_temp;
1201                                 strcpy(temp, resources->filebox_history[i - 1].path);
1202                                 id_temp = resources->filebox_history[i - 1].id;
1203                                 strcpy(resources->filebox_history[i - 1].path,
1204                                         resources->filebox_history[i].path);
1205                                 resources->filebox_history[i - 1].id =
1206                                         resources->filebox_history[i].id;
1207                                 strcpy(resources->filebox_history[i].path, temp);
1208                                 resources->filebox_history[i].id = id_temp;
1209                         }
1210                 }
1211         }
1212
1213 //      if(new_slot < 0)
1214 //      {
1215 //              for(int i = FILEBOX_HISTORY_SIZE - 1; i > 0; i--)
1216 //              {
1217 //                      strcpy(resources->filebox_history[i],
1218 //                                      resources->filebox_history[i - 1]);
1219 //              }
1220 //              new_slot = 0;
1221 //      }
1222 //
1223 //      strcpy(resources->filebox_history[new_slot], path);
1224
1225         create_history();
1226         recent_popup->update(&recent_dirs, 0, 0, 1);
1227 }
1228
1229 void BC_FileBox::create_history()
1230 {
1231         BC_Resources *resources = get_resources();
1232         recent_dirs.remove_all_objects();
1233         for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++) {
1234                 if(resources->filebox_history[i].path[0]) {
1235                         recent_dirs.append(new BC_ListBoxItem(resources->filebox_history[i].path));
1236                 }
1237         }
1238 }
1239
1240
1241 int BC_FileBox::get_display_mode()
1242 {
1243         return top_level->get_resources()->filebox_mode;
1244 }
1245
1246 void BC_FileBox::create_listbox(int x, int y, int mode)
1247 {
1248         if(listbox && listbox->get_display_mode() != mode)
1249         {
1250                 delete listbox;
1251                 listbox = 0;
1252                 top_level->get_resources()->filebox_mode = mode;
1253         }
1254
1255         if(!listbox)
1256                 add_subwindow(listbox = new BC_FileBoxListBox(x, y, this));
1257 }
1258
1259 char* BC_FileBox::get_path(int selection)
1260 {
1261         if(selection == 0)
1262         {
1263                 return get_submitted_path();
1264         }
1265         else
1266         {
1267                 BC_ListBoxItem *item = listbox->get_selection(
1268                         column_of_type(FILEBOX_NAME), selection - 1);
1269                 if(item)
1270                 {
1271                         fs->join_names(string, directory, item->get_text());
1272                         return string;
1273                 }
1274         }
1275         return 0;
1276 }
1277
1278 char* BC_FileBox::get_submitted_path()
1279 {
1280         return submitted_path;
1281 }
1282
1283 char* BC_FileBox::get_current_path()
1284 {
1285 //printf("BC_FileBox::get_current_path 1 %s\n", current_path);
1286         return current_path;
1287 }
1288
1289 char* BC_FileBox::get_newfolder_title()
1290 {
1291         char *letter2 = strchr(title, ':');
1292         new_folder_title[0] = 0;
1293         if(letter2)
1294         {
1295                 memcpy(new_folder_title, title, letter2 - title);
1296                 new_folder_title[letter2 - title] = 0;
1297         }
1298
1299         strcat(new_folder_title, _(": New folder"));
1300
1301         return new_folder_title;
1302 }
1303
1304 char* BC_FileBox::get_rename_title()
1305 {
1306         char *letter2 = strchr(title, ':');
1307         new_folder_title[0] = 0;
1308         if(letter2)
1309         {
1310                 memcpy(new_folder_title, title, letter2 - title);
1311                 new_folder_title[letter2 - title] = 0;
1312         }
1313
1314         strcat(new_folder_title, _(": Rename"));
1315
1316         return new_folder_title;
1317 }
1318
1319 char* BC_FileBox::get_delete_title()
1320 {
1321         char *letter2 = strchr(title, ':');
1322         new_folder_title[0] = 0;
1323         if(letter2)
1324         {
1325                 memcpy(new_folder_title, title, letter2 - title);
1326                 new_folder_title[letter2 - title] = 0;
1327         }
1328
1329         strcat(new_folder_title, _(": Delete"));
1330
1331         return new_folder_title;
1332 }
1333
1334 void BC_FileBox::delete_files()
1335 {
1336 // Starting at 1 causes it to ignore what's in the textbox.
1337         int i = 1;
1338         char *path;
1339         FileSystem fs;
1340         while((path = get_path(i)))
1341         {
1342 // Not directory.  Remove it.
1343                 if(!fs.is_dir(path))
1344                 {
1345 //printf("BC_FileBox::delete_files: removing \"%s\"\n", path);
1346                         remove(path);
1347                 }
1348                 i++;
1349         }
1350         refresh();
1351 }
1352
1353 BC_Button* BC_FileBox::get_ok_button()
1354 {
1355         return ok_button;
1356 }
1357
1358 BC_Button* BC_FileBox::get_cancel_button()
1359 {
1360         return cancel_button;
1361 }
1362