4 * Copyright (C) 1997-2017 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "bcfilebox.h"
24 #include "bclistboxitem.h"
25 #include "bcnewfolder.h"
28 #include "bcresources.h"
29 #include "bcsignals.h"
32 #include "condition.h"
33 #include "filesystem.h"
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,
53 this->filebox = filebox;
54 set_tooltip(_("Recent paths"));
57 int BC_FileBoxRecent::handle_event()
59 BC_ListBoxItem *selection = get_selection(0, 0);
60 if( selection != 0 ) {
61 char *path = selection->get_text();
62 filebox->submit_dir(path);
77 BC_FileBoxListBox::BC_FileBoxListBox(int x, int y, BC_FileBox *filebox)
78 : BC_ListBox(x, y, filebox->get_listbox_w(), filebox->get_listbox_h(y),
79 filebox->get_display_mode(), filebox->list_column,
80 filebox->column_titles, filebox->column_width,
81 filebox->columns, 0, 0,
82 filebox->select_multiple ? LISTBOX_MULTIPLE : LISTBOX_SINGLE,
85 this->filebox = filebox;
86 set_sort_column(filebox->sort_column);
87 set_sort_order(filebox->sort_order);
88 set_allow_drag_column(1);
91 BC_FileBoxListBox::~BC_FileBoxListBox()
95 int BC_FileBoxListBox::handle_event()
97 filebox->submit_file(filebox->textbox->get_text());
101 int BC_FileBoxListBox::selection_changed()
103 BC_ListBoxItem *item = get_selection(
104 filebox->column_of_type(FILEBOX_NAME), 0);
107 char path[BCTEXTLEN];
108 strcpy(path, item->get_text());
109 filebox->textbox->update(path);
110 filebox->fs->extract_dir(filebox->directory, path);
111 filebox->fs->extract_name(filebox->filename, path);
112 filebox->fs->complete_path(path);
113 strcpy(filebox->current_path, path);
114 strcpy(filebox->submitted_path, path);
119 int BC_FileBoxListBox::column_resize_event()
121 for(int i = 0; i < filebox->columns; i++)
122 BC_WindowBase::get_resources()->filebox_columnwidth[i] =
123 filebox->column_width[i] =
128 int BC_FileBoxListBox::sort_order_event()
130 get_resources()->filebox_sortcolumn = filebox->sort_column = get_sort_column();
131 get_resources()->filebox_sortorder = filebox->sort_order = get_sort_order();
132 filebox->refresh(-1);
136 int BC_FileBoxListBox::move_column_event()
138 filebox->move_column(get_from_column(), get_to_column());
142 int BC_FileBoxListBox::evaluate_query(char *string)
144 // Search name column
145 ArrayList<BC_ListBoxItem*> *column =
146 &filebox->list_column[filebox->column_of_type(FILEBOX_NAME)];
147 // Get current selection
148 int current_selection = get_selection_number(0, 0);
150 // Get best score in remaining items
151 int lowest_score = 0x7fffffff;
153 if(current_selection < 0) current_selection = 0;
154 // for(int i = current_selection + 1; i < column->size(); i++)
155 for(int i = current_selection; i < column->size(); i++)
157 int len1 = strlen(string);
158 int len2 = strlen(column->get(i)->get_text());
159 int current_score = strncasecmp(string,
160 column->get(i)->get_text(),
162 //printf(" %d i=%d %d %s %s\n", __LINE__, i, current_score, string, column->get(i)->get_text());
164 if(abs(current_score) < lowest_score)
166 lowest_score = abs(current_score);
176 BC_FileBoxTextBox::BC_FileBoxTextBox(int x, int y, BC_FileBox *filebox)
177 : BC_TextBox(x, y, filebox->get_w() - x - 20, 1,
178 filebox->want_directory ? filebox->directory : filebox->filename)
180 this->filebox = filebox;
183 BC_FileBoxTextBox::~BC_FileBoxTextBox()
187 int BC_FileBoxTextBox::handle_event()
190 if(get_keypress() != RETURN)
192 result = calculate_suggestions(&filebox->list_column[0]);
198 BC_FileBoxDirectoryText::BC_FileBoxDirectoryText(int x, int y, int w, BC_FileBox *filebox)
199 : BC_TextBox(x, y, w, 1, filebox->fs->get_current_dir())
201 this->filebox = filebox;
204 int BC_FileBoxDirectoryText::handle_event()
206 const char *path = get_text();
207 if( !filebox->fs->is_dir(path) ) return 0;
208 const char *cp = path;
210 if( cp > path && *--cp != '/' ) return 0;
211 char *file_path = FileSystem::basepath(path);
212 char *dir_path = FileSystem::basepath(filebox->directory);
213 int ret = !strcmp(file_path, dir_path) ? 0 : 1;
215 strcpy(filebox->directory, file_path);
216 filebox->update_history();
217 filebox->fs->change_dir(file_path);
226 BC_FileBoxSearchText::BC_FileBoxSearchText(int x, int y, BC_FileBox *filebox)
227 : BC_TextBox(x, y, filebox->get_w() - x - 40, 1, "")
229 this->filebox = filebox;
232 int BC_FileBoxSearchText::handle_event()
239 BC_FileBoxFilterText::BC_FileBoxFilterText(int x, int y, BC_FileBox *filebox)
240 : BC_TextBox(x, y, filebox->get_w() - x - 50, 1, filebox->get_resources()->filebox_filter)
242 this->filebox = filebox;
245 int BC_FileBoxFilterText::handle_event()
247 filebox->update_filter(get_text());
252 BC_FileBoxFilterMenu::BC_FileBoxFilterMenu(int x, int y, BC_FileBox *filebox)
253 : BC_ListBox(x, y, filebox->get_w() - 30, 120,
254 LISTBOX_TEXT, &filebox->filter_list, 0, 0, 1, 0, 1)
256 this->filebox = filebox;
257 set_tooltip(_("Change the filter"));
260 int BC_FileBoxFilterMenu::handle_event()
262 filebox->filter_text->update(
263 get_selection(filebox->column_of_type(FILEBOX_NAME), 0)->get_text());
264 filebox->update_filter(
265 get_selection(filebox->column_of_type(FILEBOX_NAME), 0)->get_text());
270 BC_FileBoxSizeFormat::BC_FileBoxSizeFormat(int x, int y, BC_FileBox *file_box)
271 : BC_Button(x, y, &BC_WindowBase::get_resources()->
272 filebox_szfmt_images[3*file_box->size_format])
274 this->file_box = file_box;
275 set_tooltip(_("Size numeric format"));
277 BC_FileBoxSizeFormat::~BC_FileBoxSizeFormat()
281 int BC_FileBoxSizeFormat::handle_event()
283 if( ++file_box->size_format > FILEBOX_SIZE_THOU )
284 file_box->size_format = FILEBOX_SIZE_RAW;
285 BC_WindowBase::get_resources()->filebox_size_format = file_box->size_format;
286 set_images(&BC_WindowBase::get_resources()->
287 filebox_szfmt_images[3*file_box->size_format]);
289 file_box->refresh(0);
294 BC_FileBoxUseThis::BC_FileBoxUseThis(BC_FileBox *filebox)
295 : BC_Button(filebox->get_w() / 2 -
296 BC_WindowBase::get_resources()->usethis_button_images[0]->get_w() / 2,
297 filebox->ok_button->get_y(),
298 BC_WindowBase::get_resources()->usethis_button_images)
300 this->filebox = filebox;
301 set_tooltip(_("Submit the directory"));
304 BC_FileBoxUseThis::~BC_FileBoxUseThis()
308 int BC_FileBoxUseThis::handle_event()
310 // printf("BC_FileBoxUseThis::handle_event %d '%s'\n",
312 // filebox->textbox->get_text());
313 filebox->submit_file(filebox->textbox->get_text(), 1);
318 BC_FileBoxOK::BC_FileBoxOK(BC_FileBox *filebox)
319 : BC_OKButton(filebox,
320 !filebox->want_directory ?
321 BC_WindowBase::get_resources()->ok_images :
322 BC_WindowBase::get_resources()->filebox_descend_images)
324 this->filebox = filebox;
325 if(filebox->want_directory)
326 set_tooltip(_("Descend directory"));
328 set_tooltip(_("Submit the file"));
331 BC_FileBoxOK::~BC_FileBoxOK()
335 int BC_FileBoxOK::handle_event()
337 //printf("BC_FileBoxOK::handle_event %d\n", __LINE__);
338 const char *path = filebox->textbox->get_text();
339 if( *path ) filebox->submit_file(path);
343 BC_FileBoxCancel::BC_FileBoxCancel(BC_FileBox *filebox)
344 : BC_CancelButton(filebox)
346 this->filebox = filebox;
347 set_tooltip(_("Cancel the operation"));
350 BC_FileBoxCancel::~BC_FileBoxCancel()
354 int BC_FileBoxCancel::handle_event()
356 // filebox->submit_file(filebox->textbox->get_text());
357 filebox->newfolder_thread->interrupt();
358 filebox->set_done(1);
364 BC_FileBoxText::BC_FileBoxText(int x, int y, BC_FileBox *filebox)
365 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_text_images)
367 this->filebox = filebox;
368 set_tooltip(_("Display text"));
370 int BC_FileBoxText::handle_event()
372 filebox->create_listbox(filebox->listbox->get_x(), filebox->listbox->get_y(), LISTBOX_TEXT);
373 filebox->listbox->show_window(1);
378 BC_FileBoxIcons::BC_FileBoxIcons(int x, int y, BC_FileBox *filebox)
379 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_icons_images)
381 this->filebox = filebox;
382 set_tooltip(_("Display icons"));
384 int BC_FileBoxIcons::handle_event()
386 filebox->create_listbox(filebox->listbox->get_x(), filebox->listbox->get_y(), LISTBOX_ICONS);
387 filebox->listbox->show_window(1);
392 BC_FileBoxNewfolder::BC_FileBoxNewfolder(int x, int y, BC_FileBox *filebox)
393 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_newfolder_images)
395 this->filebox = filebox;
396 set_tooltip(_("Create new folder"));
398 int BC_FileBoxNewfolder::handle_event()
400 filebox->newfolder_thread->start_new_folder();
405 BC_FileBoxRename::BC_FileBoxRename(int x, int y, BC_FileBox *filebox)
406 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_rename_images)
408 this->filebox = filebox;
409 set_tooltip(_("Rename file"));
411 int BC_FileBoxRename::handle_event()
413 filebox->rename_thread->start_rename();
417 BC_FileBoxUpdir::BC_FileBoxUpdir(int x, int y, BC_FileBox *filebox)
418 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_updir_images)
420 this->filebox = filebox;
421 set_tooltip(_("Up a directory"));
423 int BC_FileBoxUpdir::handle_event()
425 // Need a temp so submit_file can expand it
426 sprintf(string, "..");
427 filebox->submit_file(string);
431 BC_FileBoxDelete::BC_FileBoxDelete(int x, int y, BC_FileBox *filebox)
432 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_delete_images)
434 this->filebox = filebox;
435 set_tooltip(_("Delete files"));
437 int BC_FileBoxDelete::handle_event()
439 filebox->unlock_window();
440 filebox->delete_thread->start();
441 filebox->lock_window("BC_FileBoxDelete::handle_event");
445 BC_FileBoxReload::BC_FileBoxReload(int x, int y, BC_FileBox *filebox)
446 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_reload_images)
448 this->filebox = filebox;
449 set_tooltip(_("Refresh"));
451 int BC_FileBoxReload::handle_event()
459 BC_FileBox::BC_FileBox(int x, int y, const char *init_path,
460 const char *title, const char *caption, int show_all_files,
461 int want_directory, int multiple_files, int h_padding)
462 : BC_Window(title, x, y,
463 BC_WindowBase::get_resources()->filebox_w,
464 BC_WindowBase::get_resources()->filebox_h,
468 // if(want_directory)
470 // fs->set_want_directory();
471 // columns = DIRBOX_COLUMNS;
472 // columns = FILEBOX_COLUMNS;
476 columns = FILEBOX_COLUMNS;
479 list_column = new ArrayList<BC_ListBoxItem*>[columns];
480 column_type = new int[columns];
481 column_width = new int[columns];
487 size_format = BC_WindowBase::get_resources()->filebox_size_format;
489 strcpy(this->caption, caption);
490 strcpy(this->current_path, init_path);
491 strcpy(this->submitted_path, init_path);
492 select_multiple = multiple_files;
493 this->want_directory = want_directory;
494 if(show_all_files) fs->set_show_all();
495 fs->complete_path(this->current_path);
496 strcpy(this->submitted_path, this->current_path);
497 fs->extract_dir(directory, this->current_path);
498 fs->extract_name(filename, this->current_path);
500 // printf("BC_FileBox::BC_FileBox %d '%s' '%s' '%s'\n",
502 // this->submitted_path,
506 // if(want_directory)
508 // for(int i = 0; i < columns; i++)
510 // column_type[i] = get_resources()->dirbox_columntype[i];
511 // column_width[i] = get_resources()->dirbox_columnwidth[i];
512 // column_titles[i] = BC_FileBox::columntype_to_text(column_type[i]);
514 // sort_column = get_resources()->dirbox_sortcolumn;
515 // sort_order = get_resources()->dirbox_sortorder;
519 for(int i = 0; i < columns; i++)
521 column_type[i] = get_resources()->filebox_columntype[i];
522 column_width[i] = get_resources()->filebox_columnwidth[i];
523 column_titles[i] = (char*)BC_FileBox::columntype_to_text(column_type[i]);
525 sort_column = get_resources()->filebox_sortcolumn;
526 sort_order = get_resources()->filebox_sortorder;
531 // Test if current directory exists
532 if(!fs->is_dir(directory))
534 sprintf(directory, "~");
535 fs->complete_path(directory);
536 strcpy(current_path,directory);
539 fs->set_current_dir(directory);
543 h_padding = BC_WindowBase::get_resources()->ok_images[0]->get_h() -
546 this->h_padding = h_padding;
547 delete_thread = new BC_DeleteThread(this);
551 BC_FileBox::~BC_FileBox()
553 // this has to be destroyed before tables, because it can call for an update!
554 delete newfolder_thread;
557 for(int i = 0; i < TOTAL_ICONS; i++)
559 filter_list.remove_all_objects();
560 delete [] list_column;
561 delete [] column_type;
562 delete [] column_width;
563 delete delete_thread;
564 delete rename_thread;
565 recent_dirs.remove_all_objects();
568 void BC_FileBox::create_objects()
570 lock_window("BC_FileBox::create_objects");
572 BC_Resources *resources = BC_WindowBase::get_resources();
573 int directory_title_margin = MAX(20,
574 resources->filebox_text_images[0]->get_h());
576 // Directories aren't filtered in FileSystem so skip this
579 filter_list.append(new BC_ListBoxItem("*"));
580 filter_list.append(new BC_ListBoxItem("[*.mkv][*.webm]"));
581 filter_list.append(new BC_ListBoxItem("[*.mp4][*.MP4]"));
582 filter_list.append(new BC_ListBoxItem("[*.mp2][*.mp3][*.wav]"));
583 filter_list.append(new BC_ListBoxItem("[*.avi][*.mpg][*.m2v][*.m1v][*.mov]"));
584 filter_list.append(new BC_ListBoxItem("[*.jpg][*.JPG][*.png][*.gif][*.tiff]"));
585 filter_list.append(new BC_ListBoxItem("*.xml"));
586 fs->set_filter(get_resources()->filebox_filter);
592 add_subwindow(ok_button = new BC_FileBoxOK(this));
594 add_subwindow(usethis_button = new BC_FileBoxUseThis(this));
595 add_subwindow(cancel_button = new BC_FileBoxCancel(this));
597 add_subwindow(new BC_Title(x, y, caption));
599 x = get_w() - resources->filebox_icons_images[0]->get_w() - 10;
601 add_subwindow(icon_button = new BC_FileBoxIcons(x, y, this));
602 x -= resources->filebox_text_images[0]->get_w() + 5;
604 add_subwindow(text_button = new BC_FileBoxText(x, y, this));
605 x -= resources->filebox_newfolder_images[0]->get_w() + 5;
607 add_subwindow(folder_button = new BC_FileBoxNewfolder(x, y, this));
608 x -= resources->filebox_delete_images[0]->get_w() + 5;
610 add_subwindow(rename_button = new BC_FileBoxRename(x, y, this));
611 x -= resources->filebox_delete_images[0]->get_w() + 5;
613 add_subwindow(delete_button = new BC_FileBoxDelete(x, y, this));
614 x -= resources->filebox_reload_images[0]->get_w() + 5;
616 add_subwindow(reload_button = new BC_FileBoxReload(x, y, this));
617 x -= resources->filebox_updir_images[0]->get_w() + 5;
619 add_subwindow(updir_button = new BC_FileBoxUpdir(x, y, this));
620 x -= resources->filebox_szfmt_images[0]->get_w() + 5;
622 add_subwindow(szfmt_button = new BC_FileBoxSizeFormat(x, y, this));
625 y += directory_title_margin + 3;
627 add_subwindow(recent_popup = new BC_FileBoxRecent(this, x, y));
629 add_subwindow(dir_title = new BC_Title(x, y, _("Directory:")));
630 int x1 = x + dir_title->get_w() + 10, w1 = get_w()-x1 - recent_popup->get_w()-20;
631 add_subwindow(directory_title = new BC_FileBoxDirectoryText(x1, y, w1, this));
632 x1 += directory_title->get_w() + 8;
633 recent_popup->reposition_window(x1, y, directory_title->get_w(), 200);
636 y += directory_title->get_h() + 5;
638 BC_Title *search_title;
639 add_subwindow(search_title = new BC_Title(x, y, _("Search:")));
640 x += search_title->get_w() + 10;
641 add_subwindow(search_text = new BC_FileBoxSearchText(x, y, this));
644 y += search_text->get_h() + 5;
646 int newest_id = 0, newest = -1;
647 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++) {
648 if( !resources->filebox_history[i].path[0] ) continue;
649 if( resources->filebox_history[i].id > newest_id ) {
650 newest_id = resources->filebox_history[i].id;
655 strcpy(directory, resources->filebox_history[newest].path);
656 fs->change_dir(directory, 0);
657 strcpy(directory, fs->get_current_dir());
658 directory_title->update(fs->get_current_dir());
660 fs->set_sort_order(sort_order);
661 fs->set_sort_field(column_type[sort_column]);
662 fs->update(directory);
664 // Create recent dir list
669 create_listbox(x, y, get_display_mode());
670 y += listbox->get_h() + 10;
671 add_subwindow(file_title = new BC_Title(x, y, _("File:")));
672 x1 = x + file_title->get_w() + 10;
673 add_subwindow(textbox = new BC_FileBoxTextBox(x1, y, this));
674 y += textbox->get_h() + 10;
676 if(!want_directory) {
677 add_subwindow(filter_title = new BC_Title(x, y, _("Specify filter:")));
678 int x1 = x + filter_title->get_w() + 10;
679 add_subwindow(filter_text = new BC_FileBoxFilterText(x1, y, this));
680 add_subwindow(filter_popup =
681 new BC_FileBoxFilterMenu(x1 + filter_text->get_w(), y, this));
684 // listbox has to be active because refresh might be called from newfolder_thread
686 newfolder_thread = new BC_NewFolderThread(this);
688 rename_thread = new BC_RenameThread(this);
695 int BC_FileBox::get_listbox_w()
700 int BC_FileBox::get_listbox_h(int y)
702 int result = get_h() - y - h_padding - 10;
704 result -= BC_WindowBase::get_resources()->dirbox_margin;
706 result -= BC_WindowBase::get_resources()->filebox_margin;
711 int BC_FileBox::create_icons()
713 for(int i = 0; i < TOTAL_ICONS; i++)
715 icons[i] = new BC_Pixmap(this,
716 BC_WindowBase::get_resources()->type_to_icon[i],
722 int BC_FileBox::resize_event(int w, int h)
724 draw_background(0, 0, w, h);
727 // OK button handles resize event itself
728 // ok_button->reposition_window(ok_button->get_x(),
729 // h - (get_h() - ok_button->get_y()));
730 // cancel_button->reposition_window(w - (get_w() - cancel_button->get_x()),
731 // h - (get_h() - cancel_button->get_y()));
733 usethis_button->reposition_window(w / 2 - 50,
734 h - (get_h() - usethis_button->get_y()));
737 if(filter_popup) filter_popup->reposition_window(w - (get_w() - filter_popup->get_x()),
738 h - (get_h() - filter_popup->get_y()),
742 if(filter_title) filter_title->reposition_window(filter_title->get_x(),
743 h - (get_h() - filter_title->get_y()));
744 if(filter_text) filter_text->reposition_window(filter_text->get_x(),
745 h - (get_h() - filter_text->get_y()),
746 w - (get_w() - filter_text->get_w()),
748 directory_title->reposition_window(
749 directory_title->get_x(), directory_title->get_y(),
750 get_w()-directory_title->get_x() - recent_popup->get_w()-20, 1);
751 recent_popup->reposition_window(
752 directory_title->get_x() + directory_title->get_w() + 8,
753 directory_title->get_y(),
754 directory_title->get_w() + recent_popup->get_w(), 200);
755 search_text->reposition_window(
756 search_text->get_x(),
757 search_text->get_y(),
758 get_w() - search_text->get_x() - 40,
760 file_title->reposition_window(file_title->get_x(),
761 h - (get_h() - file_title->get_y()));
762 textbox->reposition_window(textbox->get_x(),
763 h - (get_h() - textbox->get_y()),
764 w - (get_w() - textbox->get_w()),
766 listbox->reposition_window(listbox->get_x(),
768 w - (get_w() - listbox->get_w()),
769 h - (get_h() - listbox->get_h()),
771 int dx = w - get_w();
772 icon_button->reposition_window(icon_button->get_x()+dx, icon_button->get_y());
773 text_button->reposition_window(text_button->get_x()+dx, text_button->get_y());
774 folder_button->reposition_window(folder_button->get_x()+dx, folder_button->get_y());
775 rename_button->reposition_window(rename_button->get_x()+dx, rename_button->get_y());
776 reload_button->reposition_window(reload_button->get_x()+dx, reload_button->get_y());
777 delete_button->reposition_window(delete_button->get_x()+dx, delete_button->get_y());
778 updir_button->reposition_window(updir_button->get_x()+dx, updir_button->get_y());
779 szfmt_button->reposition_window(szfmt_button->get_x()+dx, szfmt_button->get_y());
781 get_resources()->filebox_w = get_w();
782 get_resources()->filebox_h = get_h();
787 int BC_FileBox::keypress_event()
789 switch(get_keypress()) {
791 if( !ctrl_down() ) break;
795 if( !ctrl_down() ) break;
799 if( !ctrl_down() ) break;
806 int BC_FileBox::close_event()
812 int BC_FileBox::handle_event()
817 int BC_FileBox::extract_extension(char *out, const char *in)
821 while( --i>=0 && in[i]!='.' );
822 if( i >= 0 ) strcpy(out, &in[++i]);
826 static inline int64_t ipow(int m, int n)
829 for( int64_t vv=m; n>0; vv*=vv,n>>=1 ) if( n & 1 ) v *= vv;
832 static inline int ilen(int64_t v)
835 while( len<16 && (v/=10)>0 ) ++len;
839 int BC_FileBox::create_tables(int select_all)
841 int preload_textbox = select_all;
843 char string[BCTEXTLEN];
844 BC_ListBoxItem *new_item;
846 fs->set_sort_order(sort_order);
847 fs->set_sort_field(column_type[sort_column]);
849 for(int i = 0; i < fs->total_files(); i++)
851 FileItem *file_item = fs->get_entry(i);
852 const char *text = search_text->get_text();
853 if( text && text[0] && !bstrcasestr(file_item->name, text) )
855 int is_dir = file_item->is_dir;
856 BC_Pixmap* icon = get_icon(file_item->name, is_dir);
859 new_item = new BC_ListBoxItem(file_item->name,
861 is_dir ? get_resources()->directory_color : get_resources()->file_color);
862 if(is_dir) new_item->set_searchable(0);
863 list_column[column_of_type(FILEBOX_NAME)].append(new_item);
866 // if(!want_directory)
870 if( preload_textbox ) {
872 textbox->update(new_item->get_text());
874 int64_t size = file_item->size;
875 if( (size_format == FILEBOX_SIZE_1000 && size >= 1000) ||
876 (size_format == FILEBOX_SIZE_1024 && size >= 1024) ) {
877 static const char *suffix[] = { "", "K", "M", "G", "T", "P" };
878 if( size_format == FILEBOX_SIZE_1024 ) {
879 static const long double kk = logl(1000.)/logl(1024.);
880 size = expl(kk*logl((long double)size)) + 0.5;
882 int len = ilen(size), drop = len-3, round = 1;
883 if( round && drop > 0 ) { //round
884 size += ipow(10,drop)/2;
885 len = ilen(size); drop = len-3;
887 size /= ipow(10,drop);
889 int digits = (sfx+1)*3 - len;
890 int64_t frac = ipow(10,digits);
891 int mant = size / frac;
892 int fraction = size - mant*frac;
894 if( sfx && size_format == FILEBOX_SIZE_1000 ) sfx += 'a'-'A';
896 sprintf(string, "%d.%0*d%c", mant, digits, fraction, sfx);
898 sprintf(string, "%d%c", mant, sfx);
901 sprintf(string, "%jd", size);
902 if( size_format == FILEBOX_SIZE_THOU )
903 Units::punctuate(string);
905 new_item = new BC_ListBoxItem(string, get_resources()->file_color);
909 new_item = new BC_ListBoxItem("", get_resources()->directory_color);
912 list_column[column_of_type(FILEBOX_SIZE)].append(new_item);
919 localtime_r(&file_item->mtime, &mod_time);
920 sprintf(string, "%04d.%02d.%02d %02d:%02d:%02d",
921 mod_time.tm_year+1900, mod_time.tm_mon+1, mod_time.tm_mday,
922 mod_time.tm_hour, mod_time.tm_min, mod_time.tm_sec);
923 new_item = new BC_ListBoxItem(string, get_resources()->file_color);
927 new_item = new BC_ListBoxItem("", get_resources()->directory_color);
930 list_column[column_of_type(FILEBOX_DATE)].append(new_item);
933 // if(!want_directory)
936 extract_extension(string, file_item->name);
937 new_item = new BC_ListBoxItem(string, get_resources()->file_color);
940 new_item = new BC_ListBoxItem("", get_resources()->directory_color);
942 list_column[column_of_type(FILEBOX_EXTENSION)].append(new_item);
945 if( !is_dir && select_all ) {
946 int k = list_column[0].size()-1;
947 for( int j=0; j<columns; ++j )
948 list_column[j][k]->set_selected(1);
955 int BC_FileBox::delete_tables()
957 for(int j = 0; j < columns; j++)
959 list_column[j].remove_all_objects();
964 BC_Pixmap* BC_FileBox::get_icon(char *path, int is_dir)
966 if( is_dir ) return icons[ICON_FOLDER];
967 int icon_type = ICON_UNKNOWN;
968 char *suffix = strrchr(path, '.');
969 if( suffix && *++suffix ) {
970 suffix_to_type_t *stp = &BC_WindowBase::get_resources()->suffix_to_type[0];
971 while( stp->suffix && strcasecmp(stp->suffix, suffix) ) ++stp;
972 if( stp->icon_type ) icon_type = stp->icon_type;
975 return icons[icon_type];
978 const char* BC_FileBox::columntype_to_text(int type)
981 case FILEBOX_NAME: return FILEBOX_NAME_TEXT;
982 case FILEBOX_SIZE: return FILEBOX_SIZE_TEXT;
983 case FILEBOX_DATE: return FILEBOX_DATE_TEXT;
984 case FILEBOX_EXTENSION: return FILEBOX_EXTENSION_TEXT;
989 int BC_FileBox::column_of_type(int type)
991 for(int i = 0; i < columns; i++)
992 if(column_type[i] == type) return i;
998 int BC_FileBox::refresh(int reset, int select_all)
1000 fs->set_sort_order(sort_order);
1001 fs->set_sort_field(column_type[sort_column]);
1006 create_tables(select_all);
1007 listbox->set_master_column(column_of_type(FILEBOX_NAME), 0);
1008 listbox->update(list_column, column_titles, column_width, columns,
1009 reset>0 ? 0 : listbox->get_xposition(),
1010 reset>0 ? 0 : listbox->get_yposition(),
1015 int BC_FileBox::update_filter(const char *filter)
1017 fs->set_filter(filter);
1020 strcpy(get_resources()->filebox_filter, filter);
1026 void BC_FileBox::move_column(int src, int dst)
1030 ArrayList<BC_ListBoxItem*> *new_columns =
1031 new ArrayList<BC_ListBoxItem*>[columns];
1032 int *new_types = new int[columns];
1033 int *new_widths = new int[columns];
1035 // Fill in remaining columns with consecutive data
1036 for(int out_column = 0, in_column = 0;
1037 out_column < columns;
1041 // Copy destination column from src column
1042 if(out_column == dst)
1044 for(int i = 0; i < list_column[src].total; i++)
1046 new_columns[out_column].append(list_column[src].values[i]);
1048 new_types[out_column] = column_type[src];
1049 new_widths[out_column] = column_width[src];
1054 // Skip source column
1055 if(in_column == src) in_column++;
1056 for(int i = 0; i < list_column[src].total; i++)
1058 new_columns[out_column].append(list_column[in_column].values[i]);
1060 new_types[out_column] = column_type[in_column];
1061 new_widths[out_column] = column_width[in_column];
1066 delete [] list_column;
1067 delete [] column_type;
1068 delete [] column_width;
1069 list_column = new_columns;
1070 column_type = new_types;
1071 column_width = new_widths;
1073 for(int i = 0; i < columns; i++)
1075 get_resources()->filebox_columntype[i] = column_type[i];
1076 get_resources()->filebox_columnwidth[i] = column_width[i];
1077 column_titles[i] = (char*)BC_FileBox::columntype_to_text(column_type[i]);
1085 int BC_FileBox::submit_dir(char *dir)
1087 strcpy(directory, dir);
1088 fs->join_names(current_path, directory, filename);
1090 // printf("BC_FileBox::submit_dir %d '%s' '%s' '%s'\n",
1095 strcpy(submitted_path, current_path);
1096 fs->change_dir(dir, 0);
1098 directory_title->update(fs->get_current_dir());
1100 textbox->update(fs->get_current_dir());
1102 textbox->update(filename);
1103 listbox->reset_query();
1107 int BC_FileBox::submit_file(const char *path, int use_this)
1109 char path1[BCTEXTLEN];
1110 strcpy(path1, path);
1111 char *cp = strchr(path1,'\n');
1114 // Deactivate textbox to hide suggestions
1115 textbox->deactivate();
1117 // If file wanted, take the current directory as the desired file.
1118 // If directory wanted, ignore it.
1119 if(!path1[0] && !want_directory)
1121 // save complete path
1122 strcpy(this->current_path, directory);
1123 // save complete path
1124 strcpy(this->submitted_path, directory);
1126 // Zero out filename
1132 // is a directory, change directories
1133 if(fs->is_dir(path1) && !use_this)
1135 fs->change_dir(path1, 0);
1137 directory_title->update(fs->get_current_dir());
1138 strcpy(this->current_path, fs->get_current_dir());
1139 strcpy(this->submitted_path, fs->get_current_dir());
1140 strcpy(this->directory, fs->get_current_dir());
1144 textbox->update(fs->get_current_dir());
1146 textbox->update("");
1147 listbox->reset_query();
1151 // Is a file or desired directory. Quit the operation.
1153 char path2[BCTEXTLEN];
1154 strcpy(path2, path1);
1156 // save directory for defaults
1157 fs->extract_dir(directory, path2);
1159 // Just take the directory
1163 strcpy(path2, directory);
1166 // Take the complete path
1168 fs->extract_name(filename, path2); // save filename
1171 fs->complete_path(path2);
1172 strcpy(this->current_path, path2); // save complete path
1173 strcpy(this->submitted_path, path2); // save complete path
1175 newfolder_thread->interrupt();
1182 void BC_FileBox::update_history()
1184 // Look for path already in history
1185 BC_Resources *resources = get_resources();
1186 char path[BCTEXTLEN];
1187 strcpy(path, directory);
1188 // enfore one trailing slash
1190 while( *cp && *cp != '\n' ) ++cp;
1191 while( cp > path && *(cp-1) == '/' ) --cp;
1192 *cp++ = '/'; *cp = 0;
1194 // int new_slot = FILEBOX_HISTORY_SIZE - 1;
1196 for(int i = FILEBOX_HISTORY_SIZE - 1; i >= 0; i--)
1198 if(resources->filebox_history[i].path[0] &&
1199 !strcmp(resources->filebox_history[i].path, path))
1201 // Got matching path.
1203 resources->filebox_history[i].id = resources->get_filebox_id();
1206 // // Shift down from this point.
1209 // strcpy(resources->filebox_history[i],
1210 // resources->filebox_history[i - 1]);
1211 // if(resources->filebox_history[i][0]) new_slot--;
1217 // if(resources->filebox_history[i][0])
1223 // Remove oldest entry if full
1224 if(resources->filebox_history[FILEBOX_HISTORY_SIZE - 1].path[0])
1226 int oldest_id = 0x7fffffff;
1228 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
1230 if(resources->filebox_history[i].path[0] &&
1231 resources->filebox_history[i].id < oldest_id)
1233 oldest_id = resources->filebox_history[i].id;
1238 for(int i = oldest; i < FILEBOX_HISTORY_SIZE - 1; i++)
1240 strcpy(resources->filebox_history[i].path,
1241 resources->filebox_history[i + 1].path);
1242 resources->filebox_history[i].id =
1243 resources->filebox_history[i + 1].id;
1248 strcpy(resources->filebox_history[FILEBOX_HISTORY_SIZE - 1].path, path);
1249 resources->filebox_history[FILEBOX_HISTORY_SIZE - 1].id = resources->get_filebox_id();
1256 for(int i = 1; i < FILEBOX_HISTORY_SIZE; i++)
1258 if( (resources->filebox_history[i - 1].path[0] &&
1259 resources->filebox_history[i].path[0] &&
1260 strcasecmp(resources->filebox_history[i - 1].path,
1261 resources->filebox_history[i].path) > 0) ||
1262 (resources->filebox_history[i - 1].path[0] == 0 &&
1263 resources->filebox_history[i].path[0]) )
1266 char temp[BCTEXTLEN];
1268 strcpy(temp, resources->filebox_history[i - 1].path);
1269 id_temp = resources->filebox_history[i - 1].id;
1270 strcpy(resources->filebox_history[i - 1].path,
1271 resources->filebox_history[i].path);
1272 resources->filebox_history[i - 1].id =
1273 resources->filebox_history[i].id;
1274 strcpy(resources->filebox_history[i].path, temp);
1275 resources->filebox_history[i].id = id_temp;
1282 // for(int i = FILEBOX_HISTORY_SIZE - 1; i > 0; i--)
1284 // strcpy(resources->filebox_history[i],
1285 // resources->filebox_history[i - 1]);
1290 // strcpy(resources->filebox_history[new_slot], path);
1293 recent_popup->update(&recent_dirs, 0, 0, 1);
1296 void BC_FileBox::create_history()
1298 BC_Resources *resources = get_resources();
1299 recent_dirs.remove_all_objects();
1300 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++) {
1301 if(resources->filebox_history[i].path[0]) {
1302 recent_dirs.append(new BC_ListBoxItem(resources->filebox_history[i].path));
1308 int BC_FileBox::get_display_mode()
1310 return top_level->get_resources()->filebox_mode;
1313 void BC_FileBox::create_listbox(int x, int y, int mode)
1315 if(listbox && listbox->get_display_mode() != mode)
1319 top_level->get_resources()->filebox_mode = mode;
1323 add_subwindow(listbox = new BC_FileBoxListBox(x, y, this));
1326 char* BC_FileBox::get_path(int selection)
1330 return get_submitted_path();
1334 BC_ListBoxItem *item = listbox->get_selection(
1335 column_of_type(FILEBOX_NAME), selection - 1);
1338 fs->join_names(string, directory, item->get_text());
1345 char* BC_FileBox::get_submitted_path()
1347 return submitted_path;
1350 char* BC_FileBox::get_current_path()
1352 //printf("BC_FileBox::get_current_path 1 %s\n", current_path);
1353 return current_path;
1356 char* BC_FileBox::get_newfolder_title()
1358 char *letter2 = strchr(title, ':');
1359 new_folder_title[0] = 0;
1362 memcpy(new_folder_title, title, letter2 - title);
1363 new_folder_title[letter2 - title] = 0;
1366 strcat(new_folder_title, _(": New folder"));
1368 return new_folder_title;
1371 char* BC_FileBox::get_rename_title()
1373 char *letter2 = strchr(title, ':');
1374 new_folder_title[0] = 0;
1377 memcpy(new_folder_title, title, letter2 - title);
1378 new_folder_title[letter2 - title] = 0;
1381 strcat(new_folder_title, _(": Rename"));
1383 return new_folder_title;
1386 char* BC_FileBox::get_delete_title()
1388 char *letter2 = strchr(title, ':');
1389 new_folder_title[0] = 0;
1392 memcpy(new_folder_title, title, letter2 - title);
1393 new_folder_title[letter2 - title] = 0;
1396 strcat(new_folder_title, _(": Delete"));
1398 return new_folder_title;
1401 void BC_FileBox::delete_files()
1403 // Starting at 1 causes it to ignore what's in the textbox.
1407 while((path = get_path(i)))
1409 // Not directory. Remove it.
1410 if(!fs.is_dir(path))
1412 //printf("BC_FileBox::delete_files: removing \"%s\"\n", path);
1420 BC_Button* BC_FileBox::get_ok_button()
1425 BC_Button* BC_FileBox::get_cancel_button()
1427 return cancel_button;