audio wave icons, viewer wave image, 7 lib updates, tweak file size
[goodguy/history.git] / cinelerra-5.1 / guicast / bcfilebox.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2017 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 BC_FileBoxTextBox::BC_FileBoxTextBox(int x, int y, BC_FileBox *filebox)
176  : BC_TextBox(x, y, filebox->get_w() - 20, 1,
177         filebox->want_directory ?  filebox->directory : filebox->filename)
178 {
179         this->filebox = filebox;
180 }
181
182 BC_FileBoxTextBox::~BC_FileBoxTextBox()
183 {
184 }
185
186 int BC_FileBoxTextBox::handle_event()
187 {
188         int result = 0;
189         if(get_keypress() != RETURN)
190         {
191                 result = calculate_suggestions(&filebox->list_column[0]);
192         }
193         return result;
194 }
195
196
197 BC_FileBoxDirectoryText::BC_FileBoxDirectoryText(int x, int y, BC_FileBox *filebox)
198  : BC_TextBox(x, y, filebox->get_w() - 40, 1, filebox->fs->get_current_dir())
199 {
200         this->filebox = filebox;
201 }
202
203 int BC_FileBoxDirectoryText::handle_event()
204 {
205         const char *path = get_text();
206         if( !filebox->fs->is_dir(path) ) return 0;
207         const char *cp = path;
208         while( *cp ) ++cp;
209         if( cp > path && *--cp != '/' ) return 0;
210         char *file_path = FileSystem::basepath(path);
211         char *dir_path = FileSystem::basepath(filebox->directory);
212         int ret = !strcmp(file_path, dir_path) ? 0 : 1;
213         if( ret ) {
214                 strcpy(filebox->directory, file_path);
215                 filebox->update_history();
216                 filebox->fs->change_dir(file_path);
217                 filebox->refresh(1);
218         }
219         delete [] dir_path;
220         delete [] file_path;
221         return ret;
222 }
223
224
225 BC_FileBoxFilterText::BC_FileBoxFilterText(int x, int y, BC_FileBox *filebox)
226  : BC_TextBox(x, y, filebox->get_w() - 50, 1, filebox->get_resources()->filebox_filter)
227 {
228         this->filebox = filebox;
229 }
230
231 int BC_FileBoxFilterText::handle_event()
232 {
233         filebox->update_filter(get_text());
234         return 1;
235 }
236
237
238 BC_FileBoxFilterMenu::BC_FileBoxFilterMenu(int x, int y, BC_FileBox *filebox)
239  : BC_ListBox(x, y, filebox->get_w() - 30, 120,
240         LISTBOX_TEXT, &filebox->filter_list, 0, 0, 1, 0, 1)
241 {
242         this->filebox = filebox;
243         set_tooltip(_("Change the filter"));
244 }
245
246 int BC_FileBoxFilterMenu::handle_event()
247 {
248         filebox->filter_text->update(
249                 get_selection(filebox->column_of_type(FILEBOX_NAME), 0)->get_text());
250         filebox->update_filter(
251                 get_selection(filebox->column_of_type(FILEBOX_NAME), 0)->get_text());
252         return 1;
253 }
254
255
256 BC_FileBoxSizeFormat::BC_FileBoxSizeFormat(int x, int y, BC_FileBox *file_box)
257  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_szfmt_images)
258 {
259         this->file_box = file_box;
260         set_tooltip(_("Size numeric format"));
261 }
262 BC_FileBoxSizeFormat::~BC_FileBoxSizeFormat()
263 {
264 }
265
266 int BC_FileBoxSizeFormat::handle_event()
267 {
268         if( ++file_box->size_format > FILEBOX_SIZE_THOU )
269                 file_box->size_format = FILEBOX_SIZE_RAW;
270         BC_WindowBase::get_resources()->filebox_size_format = file_box->size_format;
271         file_box->refresh(0);
272         return 1;
273 }
274
275
276 BC_FileBoxUseThis::BC_FileBoxUseThis(BC_FileBox *filebox)
277  : BC_Button(filebox->get_w() / 2 -
278                 BC_WindowBase::get_resources()->usethis_button_images[0]->get_w() / 2,
279         filebox->ok_button->get_y(),
280         BC_WindowBase::get_resources()->usethis_button_images)
281 {
282         this->filebox = filebox;
283         set_tooltip(_("Submit the directory"));
284 }
285
286 BC_FileBoxUseThis::~BC_FileBoxUseThis()
287 {
288 }
289
290 int BC_FileBoxUseThis::handle_event()
291 {
292 // printf("BC_FileBoxUseThis::handle_event %d '%s'\n",
293 // __LINE__,
294 // filebox->textbox->get_text());
295         filebox->submit_file(filebox->textbox->get_text(), 1);
296         return 1;
297 }
298
299
300 BC_FileBoxOK::BC_FileBoxOK(BC_FileBox *filebox)
301  : BC_OKButton(filebox,
302         !filebox->want_directory ?
303                 BC_WindowBase::get_resources()->ok_images :
304                 BC_WindowBase::get_resources()->filebox_descend_images)
305 {
306         this->filebox = filebox;
307         if(filebox->want_directory)
308                 set_tooltip(_("Descend directory"));
309         else
310                 set_tooltip(_("Submit the file"));
311 }
312
313 BC_FileBoxOK::~BC_FileBoxOK()
314 {
315 }
316
317 int BC_FileBoxOK::handle_event()
318 {
319 //printf("BC_FileBoxOK::handle_event %d\n", __LINE__);
320         const char *path = filebox->textbox->get_text();
321         if( *path ) filebox->submit_file(path);
322         return 1;
323 }
324
325 BC_FileBoxCancel::BC_FileBoxCancel(BC_FileBox *filebox)
326  : BC_CancelButton(filebox)
327 {
328         this->filebox = filebox;
329         set_tooltip(_("Cancel the operation"));
330 }
331
332 BC_FileBoxCancel::~BC_FileBoxCancel()
333 {
334 }
335
336 int BC_FileBoxCancel::handle_event()
337 {
338 //      filebox->submit_file(filebox->textbox->get_text());
339         filebox->newfolder_thread->interrupt();
340         filebox->set_done(1);
341         return 1;
342 }
343
344
345
346 BC_FileBoxText::BC_FileBoxText(int x, int y, BC_FileBox *filebox)
347  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_text_images)
348 {
349         this->filebox = filebox;
350         set_tooltip(_("Display text"));
351 }
352 int BC_FileBoxText::handle_event()
353 {
354         filebox->create_listbox(filebox->listbox->get_x(), filebox->listbox->get_y(), LISTBOX_TEXT);
355         filebox->listbox->show_window(1);
356         return 1;
357 }
358
359
360 BC_FileBoxIcons::BC_FileBoxIcons(int x, int y, BC_FileBox *filebox)
361  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_icons_images)
362 {
363         this->filebox = filebox;
364         set_tooltip(_("Display icons"));
365 }
366 int BC_FileBoxIcons::handle_event()
367 {
368         filebox->create_listbox(filebox->listbox->get_x(), filebox->listbox->get_y(), LISTBOX_ICONS);
369         filebox->listbox->show_window(1);
370         return 1;
371 }
372
373
374 BC_FileBoxNewfolder::BC_FileBoxNewfolder(int x, int y, BC_FileBox *filebox)
375  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_newfolder_images)
376 {
377         this->filebox = filebox;
378         set_tooltip(_("Create new folder"));
379 }
380 int BC_FileBoxNewfolder::handle_event()
381 {
382         filebox->newfolder_thread->start_new_folder();
383         return 1;
384 }
385
386
387 BC_FileBoxRename::BC_FileBoxRename(int x, int y, BC_FileBox *filebox)
388  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_rename_images)
389 {
390         this->filebox = filebox;
391         set_tooltip(_("Rename file"));
392 }
393 int BC_FileBoxRename::handle_event()
394 {
395         filebox->rename_thread->start_rename();
396         return 1;
397 }
398
399 BC_FileBoxUpdir::BC_FileBoxUpdir(int x, int y, BC_FileBox *filebox)
400  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_updir_images)
401 {
402         this->filebox = filebox;
403         set_tooltip(_("Up a directory"));
404 }
405 int BC_FileBoxUpdir::handle_event()
406 {
407 // Need a temp so submit_file can expand it
408         sprintf(string, "..");
409         filebox->submit_file(string);
410         return 1;
411 }
412
413 BC_FileBoxDelete::BC_FileBoxDelete(int x, int y, BC_FileBox *filebox)
414  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_delete_images)
415 {
416         this->filebox = filebox;
417         set_tooltip(_("Delete files"));
418 }
419 int BC_FileBoxDelete::handle_event()
420 {
421         filebox->unlock_window();
422         filebox->delete_thread->start();
423         filebox->lock_window("BC_FileBoxDelete::handle_event");
424         return 1;
425 }
426
427 BC_FileBoxReload::BC_FileBoxReload(int x, int y, BC_FileBox *filebox)
428  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_reload_images)
429 {
430         this->filebox = filebox;
431         set_tooltip(_("Refresh"));
432 }
433 int BC_FileBoxReload::handle_event()
434 {
435         filebox->refresh();
436         return 1;
437 }
438
439
440
441 BC_FileBox::BC_FileBox(int x, int y, const char *init_path,
442                 const char *title, const char *caption, int show_all_files,
443                 int want_directory, int multiple_files, int h_padding)
444  : BC_Window(title, x, y,
445         BC_WindowBase::get_resources()->filebox_w,
446         BC_WindowBase::get_resources()->filebox_h,
447         400, 300, 1, 0, 1)
448 {
449         fs = new FileSystem;
450 //      if(want_directory)
451 //      {
452 //              fs->set_want_directory();
453 //              columns = DIRBOX_COLUMNS;
454 //              columns = FILEBOX_COLUMNS;
455 //      }
456 //      else
457         {
458                 columns = FILEBOX_COLUMNS;
459         }
460
461         list_column = new ArrayList<BC_ListBoxItem*>[columns];
462         column_type = new int[columns];
463         column_width = new int[columns];
464
465         filter_text = 0;
466         filter_popup = 0;
467         usethis_button = 0;
468         size_format = BC_WindowBase::get_resources()->filebox_size_format;
469
470         strcpy(this->caption, caption);
471         strcpy(this->current_path, init_path);
472         strcpy(this->submitted_path, init_path);
473         select_multiple = multiple_files;
474         this->want_directory = want_directory;
475         if(show_all_files) fs->set_show_all();
476         fs->complete_path(this->current_path);
477         strcpy(this->submitted_path, this->current_path);
478         fs->extract_dir(directory, this->current_path);
479         fs->extract_name(filename, this->current_path);
480
481 // printf("BC_FileBox::BC_FileBox %d '%s' '%s' '%s'\n",
482 // __LINE__,
483 // this->submitted_path,
484 // directory,
485 // filename);
486
487 //      if(want_directory)
488 //      {
489 //              for(int i = 0; i < columns; i++)
490 //              {
491 //                      column_type[i] = get_resources()->dirbox_columntype[i];
492 //                      column_width[i] = get_resources()->dirbox_columnwidth[i];
493 //                      column_titles[i] = BC_FileBox::columntype_to_text(column_type[i]);
494 //              }
495 //              sort_column = get_resources()->dirbox_sortcolumn;
496 //              sort_order = get_resources()->dirbox_sortorder;
497 //      }
498 //      else
499         {
500                 for(int i = 0; i < columns; i++)
501                 {
502                         column_type[i] = get_resources()->filebox_columntype[i];
503                         column_width[i] = get_resources()->filebox_columnwidth[i];
504                         column_titles[i] = (char*)BC_FileBox::columntype_to_text(column_type[i]);
505                 }
506                 sort_column = get_resources()->filebox_sortcolumn;
507                 sort_order = get_resources()->filebox_sortorder;
508         }
509
510
511
512 // Test if current directory exists
513         if(!fs->is_dir(directory))
514         {
515                 sprintf(directory, "~");
516                 fs->complete_path(directory);
517                 strcpy(current_path,directory);
518                 filename[0] = 0;
519         }
520         fs->set_current_dir(directory);
521
522         if(h_padding == -1)
523         {
524                 h_padding = BC_WindowBase::get_resources()->ok_images[0]->get_h() -
525                         20;
526         }
527         this->h_padding = h_padding;
528         delete_thread = new BC_DeleteThread(this);
529         rename_thread = 0;
530 }
531
532 BC_FileBox::~BC_FileBox()
533 {
534 // this has to be destroyed before tables, because it can call for an update!
535         delete newfolder_thread;
536         delete fs;
537         delete_tables();
538         for(int i = 0; i < TOTAL_ICONS; i++)
539                 delete icons[i];
540         filter_list.remove_all_objects();
541         delete [] list_column;
542         delete [] column_type;
543         delete [] column_width;
544         delete delete_thread;
545         delete rename_thread;
546         recent_dirs.remove_all_objects();
547 }
548
549 void BC_FileBox::create_objects()
550 {
551         int x = 10, y = 10;
552         BC_Resources *resources = BC_WindowBase::get_resources();
553         int directory_title_margin = MAX(20,
554                 resources->filebox_text_images[0]->get_h());
555
556 // Directories aren't filtered in FileSystem so skip this
557         if(!want_directory)
558         {
559                 filter_list.append(new BC_ListBoxItem("*"));
560                 filter_list.append(new BC_ListBoxItem("[*.mkv]"));
561                 filter_list.append(new BC_ListBoxItem("[*.mp4]"));
562                 filter_list.append(new BC_ListBoxItem("[*.mp2][*.mp3][*.wav]"));
563                 filter_list.append(new BC_ListBoxItem("[*.avi][*.mpg][*.m2v][*.m1v][*.mov]"));
564                 filter_list.append(new BC_ListBoxItem("*.xml"));
565                 fs->set_filter(get_resources()->filebox_filter);
566         }
567
568         create_icons();
569         create_tables();
570
571         add_subwindow(ok_button = new BC_FileBoxOK(this));
572         if(want_directory)
573                 add_subwindow(usethis_button = new BC_FileBoxUseThis(this));
574         add_subwindow(cancel_button = new BC_FileBoxCancel(this));
575
576         add_subwindow(new BC_Title(x, y, caption));
577
578         x = get_w() - resources->filebox_icons_images[0]->get_w() - 10;
579
580         add_subwindow(icon_button = new BC_FileBoxIcons(x, y, this));
581         x -= resources->filebox_text_images[0]->get_w() + 5;
582
583         add_subwindow(text_button = new BC_FileBoxText(x, y, this));
584         x -= resources->filebox_newfolder_images[0]->get_w() + 5;
585
586         add_subwindow(folder_button = new BC_FileBoxNewfolder(x, y, this));
587         x -= resources->filebox_delete_images[0]->get_w() + 5;
588
589         add_subwindow(rename_button = new BC_FileBoxRename(x, y, this));
590         x -= resources->filebox_delete_images[0]->get_w() + 5;
591
592         add_subwindow(delete_button = new BC_FileBoxDelete(x, y, this));
593         x -= resources->filebox_reload_images[0]->get_w() + 5;
594
595         add_subwindow(reload_button = new BC_FileBoxReload(x, y, this));
596         x -= resources->filebox_updir_images[0]->get_w() + 5;
597
598         add_subwindow(updir_button = new BC_FileBoxUpdir(x, y, this));
599         x -= resources->filebox_szfmt_images[0]->get_w() + 5;
600
601         add_subwindow(szfmt_button = new BC_FileBoxSizeFormat(x, y, this));
602
603         x = 10;
604         y += directory_title_margin + 3;
605
606         add_subwindow(recent_popup = new BC_FileBoxRecent(this, x, y));
607         add_subwindow(directory_title = new BC_FileBoxDirectoryText(x, y, this));
608         directory_title->reposition_window(x, y, get_w() - recent_popup->get_w() -  20, 1);
609         x += directory_title->get_w() + 8;
610         recent_popup->reposition_window(x, y, directory_title->get_w(), 200);
611
612         x = 10;
613         y += directory_title->get_h() + 5;
614
615         int newest_id = 0, newest = -1;
616         for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++) {
617                 if( !resources->filebox_history[i].path[0] ) continue;
618                 if( resources->filebox_history[i].id > newest_id ) {
619                         newest_id = resources->filebox_history[i].id;
620                         newest = i;
621                 }
622         }
623         if( newest >= 0 ) {
624                 strcpy(directory, resources->filebox_history[newest].path);
625                 fs->change_dir(directory, 0);
626                 strcpy(directory, fs->get_current_dir());
627                 directory_title->update(fs->get_current_dir());
628         }
629         fs->set_sort_order(sort_order);
630         fs->set_sort_field(column_type[sort_column]);
631         fs->update(directory);
632
633 // Create recent dir list
634         create_history();
635         update_history();
636
637         listbox = 0;
638         create_listbox(x, y, get_display_mode());
639         y += listbox->get_h() + 10;
640         add_subwindow(textbox = new BC_FileBoxTextBox(x, y, this));
641         y += textbox->get_h() + 10;
642
643         if(!want_directory) {
644                 add_subwindow(filter_text = new BC_FileBoxFilterText(x, y, this));
645                 add_subwindow(filter_popup =
646                         new BC_FileBoxFilterMenu(x + filter_text->get_w(), y, this));;
647         }
648
649 // listbox has to be active because refresh might be called from newfolder_thread
650         listbox->activate();
651         newfolder_thread = new BC_NewFolderThread(this);
652
653         rename_thread = new BC_RenameThread(this);
654
655         refresh();
656         show_window();
657 }
658
659 int BC_FileBox::get_listbox_w()
660 {
661         return get_w() - 20;
662 }
663
664 int BC_FileBox::get_listbox_h(int y)
665 {
666         int result = get_h() - y - h_padding - 10;
667         if(want_directory)
668                 result -= BC_WindowBase::get_resources()->dirbox_margin;
669         else
670                 result -= BC_WindowBase::get_resources()->filebox_margin;
671
672         return result;
673 }
674
675 int BC_FileBox::create_icons()
676 {
677         for(int i = 0; i < TOTAL_ICONS; i++)
678         {
679                 icons[i] = new BC_Pixmap(this,
680                         BC_WindowBase::get_resources()->type_to_icon[i],
681                         PIXMAP_ALPHA);
682         }
683         return 0;
684 }
685
686 int BC_FileBox::resize_event(int w, int h)
687 {
688         draw_background(0, 0, w, h);
689         flash(0);
690
691 // OK button handles resize event itself
692 //      ok_button->reposition_window(ok_button->get_x(),
693 //              h - (get_h() - ok_button->get_y()));
694 //      cancel_button->reposition_window(w - (get_w() - cancel_button->get_x()),
695 //              h - (get_h() - cancel_button->get_y()));
696         if(usethis_button)
697                 usethis_button->reposition_window(w / 2 - 50,
698                         h - (get_h() - usethis_button->get_y()));
699
700
701         if(filter_popup) filter_popup->reposition_window(w - (get_w() - filter_popup->get_x()),
702                 h - (get_h() - filter_popup->get_y()),
703                 w - 30,
704                 0);
705
706
707         if(filter_text) filter_text->reposition_window(filter_text->get_x(),
708                 h - (get_h() - filter_text->get_y()),
709                 w - (get_w() - filter_text->get_w()),
710                 1);
711         directory_title->reposition_window(
712                 directory_title->get_x(),
713                 directory_title->get_y(),
714                 get_w() - recent_popup->get_w() -  20,
715                 1);
716         recent_popup->reposition_window(
717                 directory_title->get_x() + directory_title->get_w() + 8,
718                 directory_title->get_y(),
719                 directory_title->get_w() + recent_popup->get_w(),
720                 recent_popup->get_h());
721         textbox->reposition_window(textbox->get_x(),
722                 h - (get_h() - textbox->get_y()),
723                 w - (get_w() - textbox->get_w()),
724                 1);
725         listbox->reposition_window(listbox->get_x(),
726                 listbox->get_y(),
727                 w - (get_w() - listbox->get_w()),
728                 h - (get_h() - listbox->get_h()),
729                 0);
730         int dx = w - get_w();
731         icon_button->reposition_window(icon_button->get_x()+dx, icon_button->get_y());
732         text_button->reposition_window(text_button->get_x()+dx, text_button->get_y());
733         folder_button->reposition_window(folder_button->get_x()+dx, folder_button->get_y());
734         rename_button->reposition_window(rename_button->get_x()+dx, rename_button->get_y());
735         reload_button->reposition_window(reload_button->get_x()+dx, reload_button->get_y());
736         delete_button->reposition_window(delete_button->get_x()+dx, delete_button->get_y());
737         updir_button->reposition_window(updir_button->get_x()+dx, updir_button->get_y());
738         szfmt_button->reposition_window(szfmt_button->get_x()+dx, szfmt_button->get_y());
739         set_w(w);  set_h(h);
740         get_resources()->filebox_w = get_w();
741         get_resources()->filebox_h = get_h();
742         flush();
743         return 1;
744 }
745
746 int BC_FileBox::keypress_event()
747 {
748         switch(get_keypress())
749         {
750                 case 'w':
751                         if(ctrl_down()) set_done(1);
752                         return 1;
753                         break;
754         }
755         return 0;
756 }
757
758 int BC_FileBox::close_event()
759 {
760         set_done(1);
761         return 1;
762 }
763
764 int BC_FileBox::handle_event()
765 {
766         return 0;
767 }
768
769 int BC_FileBox::extract_extension(char *out, const char *in)
770 {
771         *out = 0;
772         int i = strlen(in);
773         while( --i>=0 && in[i]!='.' );
774         if( i >= 0 ) strcpy(out, &in[++i]);
775         return 0;
776 }
777
778 static inline int64_t ipow(int m, int n)
779 {
780         int64_t v = 1;
781         for( int64_t vv=m; n>0; vv*=vv,n>>=1 ) if( n & 1 ) v *= vv;
782         return v;
783 }
784 static inline int ilen(int64_t v)
785 {
786         int len = 1;
787         while( len<16 && (v/=10)>0 ) ++len;
788         return len;
789 }
790
791 int BC_FileBox::create_tables()
792 {
793         delete_tables();
794         char string[BCTEXTLEN];
795         BC_ListBoxItem *new_item;
796
797         fs->set_sort_order(sort_order);
798         fs->set_sort_field(column_type[sort_column]);
799
800         for(int i = 0; i < fs->total_files(); i++)
801         {
802                 FileItem *file_item = fs->get_entry(i);
803                 int is_dir = file_item->is_dir;
804                 BC_Pixmap* icon = get_icon(file_item->name, is_dir);
805
806 // Name entry
807                 new_item = new BC_ListBoxItem(file_item->name,
808                         icon,
809                         is_dir ? get_resources()->directory_color : get_resources()->file_color);
810                 if(is_dir) new_item->set_searchable(0);
811                 list_column[column_of_type(FILEBOX_NAME)].append(new_item);
812
813 // Size entry
814 //              if(!want_directory)
815 //              {
816                         if(!is_dir)
817                         {
818                                 int64_t size = file_item->size;
819                                 if( (size_format == FILEBOX_SIZE_1000 && size >= 1000) ||
820                                     (size_format == FILEBOX_SIZE_1024 && size >= 1024) ) {
821                                         static const char *suffix[] = { "", "K", "M", "G", "T", "P" };
822                                         if( size_format == FILEBOX_SIZE_1024 ) {
823                                                 static const long double kk = logl(1000.)/logl(1024.);
824                                                 size = expl(kk*logl((long double)size)) + 0.5;
825                                         }
826                                         int len = ilen(size), drop = len-3, round = 1;
827                                         if( round && drop > 0 ) { //round
828                                                 size += ipow(10,drop)/2;
829                                                 len = ilen(size);  drop = len-3;
830                                         }
831                                         size /= ipow(10,drop);
832                                         int sfx = (len-1)/3;
833                                         int digits = (sfx+1)*3 - len;
834                                         int64_t frac = ipow(10,digits);
835                                         int mant  = size / frac;
836                                         int fraction = size - mant*frac;
837                                         sfx = *suffix[sfx];
838                                         if( sfx && size_format == FILEBOX_SIZE_1000 ) sfx += 'a'-'A';
839                                         if( digits )
840                                                 sprintf(string, "%d.%0*d%c", mant, digits, fraction, sfx);
841                                         else
842                                                 sprintf(string, "%d%c", mant, sfx);
843                                 }
844                                 else {
845                                         sprintf(string, "%jd", size);
846                                         if( size_format == FILEBOX_SIZE_THOU )
847                                                 Units::punctuate(string);
848                                 }
849                                 new_item = new BC_ListBoxItem(string, get_resources()->file_color);
850                         }
851                         else
852                         {
853                                 new_item = new BC_ListBoxItem("", get_resources()->directory_color);
854                         }
855
856                         list_column[column_of_type(FILEBOX_SIZE)].append(new_item);
857 //              }
858
859 // Date entry
860                 if(!is_dir || 1)
861                 {
862                         struct tm mod_time;
863                         localtime_r(&file_item->mtime, &mod_time);
864                         sprintf(string, "%04d.%02d.%02d  %02d:%02d:%02d",
865                                 mod_time.tm_year+1900, mod_time.tm_mon+1, mod_time.tm_mday,
866                                 mod_time.tm_hour, mod_time.tm_min, mod_time.tm_sec);
867                         new_item = new BC_ListBoxItem(string, get_resources()->file_color);
868                 }
869                 else
870                 {
871                         new_item = new BC_ListBoxItem("", get_resources()->directory_color);
872                 }
873
874                 list_column[column_of_type(FILEBOX_DATE)].append(new_item);
875
876 // Extension entry
877 //              if(!want_directory)
878 //              {
879                         if(!is_dir) {
880                                 extract_extension(string, file_item->name);
881                                 new_item = new BC_ListBoxItem(string, get_resources()->file_color);
882                         }
883                         else {
884                                 new_item = new BC_ListBoxItem("", get_resources()->directory_color);
885                         }
886                         list_column[column_of_type(FILEBOX_EXTENSION)].append(new_item);
887 //              }
888         }
889
890         return 0;
891 }
892
893 int BC_FileBox::delete_tables()
894 {
895         for(int j = 0; j < columns; j++)
896         {
897                 list_column[j].remove_all_objects();
898         }
899         return 0;
900 }
901
902 BC_Pixmap* BC_FileBox::get_icon(char *path, int is_dir)
903 {
904         if( is_dir ) return icons[ICON_FOLDER];
905         int icon_type = ICON_UNKNOWN;
906         char *suffix = strrchr(path, '.');
907         if( suffix && *++suffix ) {
908                 suffix_to_type_t *stp = &BC_WindowBase::get_resources()->suffix_to_type[0];
909                 while( stp->suffix && strcasecmp(stp->suffix, suffix) ) ++stp;
910                 if( stp->icon_type ) icon_type = stp->icon_type;
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         char path1[BCTEXTLEN];
1048         strcpy(path1, path);
1049         char *cp = strchr(path1,'\n');
1050         if( cp ) *cp = 0;
1051
1052 // Deactivate textbox to hide suggestions
1053         textbox->deactivate();
1054
1055 // If file wanted, take the current directory as the desired file.
1056 // If directory wanted, ignore it.
1057         if(!path1[0] && !want_directory)
1058         {
1059 // save complete path
1060                 strcpy(this->current_path, directory);
1061 // save complete path
1062                 strcpy(this->submitted_path, directory);
1063                 update_history();
1064 // Zero out filename
1065                 filename[0] = 0;
1066                 set_done(0);
1067                 return 0;
1068         }
1069
1070 // is a directory, change directories
1071         if(fs->is_dir(path1) && !use_this)
1072         {
1073                 fs->change_dir(path1, 0);
1074                 refresh(1);
1075                 directory_title->update(fs->get_current_dir());
1076                 strcpy(this->current_path, fs->get_current_dir());
1077                 strcpy(this->submitted_path, fs->get_current_dir());
1078                 strcpy(this->directory, fs->get_current_dir());
1079                 update_history();
1080                 filename[0] = 0;
1081                 if(want_directory)
1082                         textbox->update(fs->get_current_dir());
1083                 else
1084                         textbox->update("");
1085                 listbox->reset_query();
1086                 return 1;
1087         }
1088         else
1089 // Is a file or desired directory.  Quit the operation.
1090         {
1091                 char path2[BCTEXTLEN];
1092                 strcpy(path2, path1);
1093
1094 // save directory for defaults
1095                 fs->extract_dir(directory, path2);
1096
1097 // Just take the directory
1098                 if(want_directory)
1099                 {
1100                         filename[0] = 0;
1101                         strcpy(path2, directory);
1102                 }
1103                 else
1104 // Take the complete path
1105                 {
1106                         fs->extract_name(filename, path2);     // save filename
1107                 }
1108
1109                 fs->complete_path(path2);
1110                 strcpy(this->current_path, path2);          // save complete path
1111                 strcpy(this->submitted_path, path2);          // save complete path
1112                 update_history();
1113                 newfolder_thread->interrupt();
1114                 set_done(0);
1115                 return 0;
1116         }
1117         return 0;
1118 }
1119
1120 void BC_FileBox::update_history()
1121 {
1122 // Look for path already in history
1123         BC_Resources *resources = get_resources();
1124         char path[BCTEXTLEN];
1125         strcpy(path, directory);
1126 // enfore one trailing slash
1127         char *cp = path;
1128         while( *cp && *cp != '\n' ) ++cp;
1129         while( cp > path && *(cp-1) == '/' ) --cp;
1130         *cp++ = '/';  *cp = 0;
1131
1132 //      int new_slot = FILEBOX_HISTORY_SIZE - 1;
1133
1134         for(int i = FILEBOX_HISTORY_SIZE - 1; i >= 0; i--)
1135         {
1136                 if(resources->filebox_history[i].path[0] &&
1137                         !strcmp(resources->filebox_history[i].path, path))
1138                 {
1139 // Got matching path.
1140 // Update ID.
1141                         resources->filebox_history[i].id = resources->get_filebox_id();
1142                         return;
1143                 }
1144 // // Shift down from this point.
1145 //                      while(i > 0)
1146 //                      {
1147 //                              strcpy(resources->filebox_history[i],
1148 //                                      resources->filebox_history[i - 1]);
1149 //                              if(resources->filebox_history[i][0]) new_slot--;
1150 //                              i--;
1151 //                      }
1152 //                      break;
1153 //              }
1154 //              else
1155 //                      if(resources->filebox_history[i][0])
1156 //                              new_slot--;
1157 //              else
1158 //                      break;
1159         }
1160
1161 // Remove oldest entry if full
1162         if(resources->filebox_history[FILEBOX_HISTORY_SIZE - 1].path[0])
1163         {
1164                 int oldest_id = 0x7fffffff;
1165                 int oldest = 0;
1166                 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
1167                 {
1168                         if(resources->filebox_history[i].path[0] &&
1169                                 resources->filebox_history[i].id < oldest_id)
1170                         {
1171                                 oldest_id = resources->filebox_history[i].id;
1172                                 oldest = i;
1173                         }
1174                 }
1175
1176                 for(int i = oldest; i < FILEBOX_HISTORY_SIZE - 1; i++)
1177                 {
1178                         strcpy(resources->filebox_history[i].path,
1179                                 resources->filebox_history[i + 1].path);
1180                         resources->filebox_history[i].id =
1181                                 resources->filebox_history[i + 1].id;
1182                 }
1183         }
1184
1185 // Create new entry
1186         strcpy(resources->filebox_history[FILEBOX_HISTORY_SIZE - 1].path, path);
1187         resources->filebox_history[FILEBOX_HISTORY_SIZE - 1].id = resources->get_filebox_id();
1188
1189 // Alphabetize
1190         int done = 0;
1191         while(!done)
1192         {
1193                 done = 1;
1194                 for(int i = 1; i < FILEBOX_HISTORY_SIZE; i++)
1195                 {
1196                         if( (resources->filebox_history[i - 1].path[0] &&
1197                                 resources->filebox_history[i].path[0] &&
1198                                 strcasecmp(resources->filebox_history[i - 1].path,
1199                                         resources->filebox_history[i].path) > 0) ||
1200                                 (resources->filebox_history[i - 1].path[0] == 0 &&
1201                                         resources->filebox_history[i].path[0]) )
1202                         {
1203                                 done = 0;
1204                                 char temp[BCTEXTLEN];
1205                                 int id_temp;
1206                                 strcpy(temp, resources->filebox_history[i - 1].path);
1207                                 id_temp = resources->filebox_history[i - 1].id;
1208                                 strcpy(resources->filebox_history[i - 1].path,
1209                                         resources->filebox_history[i].path);
1210                                 resources->filebox_history[i - 1].id =
1211                                         resources->filebox_history[i].id;
1212                                 strcpy(resources->filebox_history[i].path, temp);
1213                                 resources->filebox_history[i].id = id_temp;
1214                         }
1215                 }
1216         }
1217
1218 //      if(new_slot < 0)
1219 //      {
1220 //              for(int i = FILEBOX_HISTORY_SIZE - 1; i > 0; i--)
1221 //              {
1222 //                      strcpy(resources->filebox_history[i],
1223 //                                      resources->filebox_history[i - 1]);
1224 //              }
1225 //              new_slot = 0;
1226 //      }
1227 //
1228 //      strcpy(resources->filebox_history[new_slot], path);
1229
1230         create_history();
1231         recent_popup->update(&recent_dirs, 0, 0, 1);
1232 }
1233
1234 void BC_FileBox::create_history()
1235 {
1236         BC_Resources *resources = get_resources();
1237         recent_dirs.remove_all_objects();
1238         for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++) {
1239                 if(resources->filebox_history[i].path[0]) {
1240                         recent_dirs.append(new BC_ListBoxItem(resources->filebox_history[i].path));
1241                 }
1242         }
1243 }
1244
1245
1246 int BC_FileBox::get_display_mode()
1247 {
1248         return top_level->get_resources()->filebox_mode;
1249 }
1250
1251 void BC_FileBox::create_listbox(int x, int y, int mode)
1252 {
1253         if(listbox && listbox->get_display_mode() != mode)
1254         {
1255                 delete listbox;
1256                 listbox = 0;
1257                 top_level->get_resources()->filebox_mode = mode;
1258         }
1259
1260         if(!listbox)
1261                 add_subwindow(listbox = new BC_FileBoxListBox(x, y, this));
1262 }
1263
1264 char* BC_FileBox::get_path(int selection)
1265 {
1266         if(selection == 0)
1267         {
1268                 return get_submitted_path();
1269         }
1270         else
1271         {
1272                 BC_ListBoxItem *item = listbox->get_selection(
1273                         column_of_type(FILEBOX_NAME), selection - 1);
1274                 if(item)
1275                 {
1276                         fs->join_names(string, directory, item->get_text());
1277                         return string;
1278                 }
1279         }
1280         return 0;
1281 }
1282
1283 char* BC_FileBox::get_submitted_path()
1284 {
1285         return submitted_path;
1286 }
1287
1288 char* BC_FileBox::get_current_path()
1289 {
1290 //printf("BC_FileBox::get_current_path 1 %s\n", current_path);
1291         return current_path;
1292 }
1293
1294 char* BC_FileBox::get_newfolder_title()
1295 {
1296         char *letter2 = strchr(title, ':');
1297         new_folder_title[0] = 0;
1298         if(letter2)
1299         {
1300                 memcpy(new_folder_title, title, letter2 - title);
1301                 new_folder_title[letter2 - title] = 0;
1302         }
1303
1304         strcat(new_folder_title, _(": New folder"));
1305
1306         return new_folder_title;
1307 }
1308
1309 char* BC_FileBox::get_rename_title()
1310 {
1311         char *letter2 = strchr(title, ':');
1312         new_folder_title[0] = 0;
1313         if(letter2)
1314         {
1315                 memcpy(new_folder_title, title, letter2 - title);
1316                 new_folder_title[letter2 - title] = 0;
1317         }
1318
1319         strcat(new_folder_title, _(": Rename"));
1320
1321         return new_folder_title;
1322 }
1323
1324 char* BC_FileBox::get_delete_title()
1325 {
1326         char *letter2 = strchr(title, ':');
1327         new_folder_title[0] = 0;
1328         if(letter2)
1329         {
1330                 memcpy(new_folder_title, title, letter2 - title);
1331                 new_folder_title[letter2 - title] = 0;
1332         }
1333
1334         strcat(new_folder_title, _(": Delete"));
1335
1336         return new_folder_title;
1337 }
1338
1339 void BC_FileBox::delete_files()
1340 {
1341 // Starting at 1 causes it to ignore what's in the textbox.
1342         int i = 1;
1343         char *path;
1344         FileSystem fs;
1345         while((path = get_path(i)))
1346         {
1347 // Not directory.  Remove it.
1348                 if(!fs.is_dir(path))
1349                 {
1350 //printf("BC_FileBox::delete_files: removing \"%s\"\n", path);
1351                         remove(path);
1352                 }
1353                 i++;
1354         }
1355         refresh();
1356 }
1357
1358 BC_Button* BC_FileBox::get_ok_button()
1359 {
1360         return ok_button;
1361 }
1362
1363 BC_Button* BC_FileBox::get_cancel_button()
1364 {
1365         return cancel_button;
1366 }
1367