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;
56 int BC_FileBoxRecent::handle_event()
58 BC_ListBoxItem *selection = get_selection(0, 0);
59 if( selection != 0 ) {
60 char *path = selection->get_text();
61 filebox->submit_dir(path);
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,
84 this->filebox = filebox;
85 set_sort_column(filebox->sort_column);
86 set_sort_order(filebox->sort_order);
87 set_allow_drag_column(1);
90 BC_FileBoxListBox::~BC_FileBoxListBox()
94 int BC_FileBoxListBox::handle_event()
96 filebox->submit_file(filebox->textbox->get_text());
100 int BC_FileBoxListBox::selection_changed()
102 BC_ListBoxItem *item = get_selection(
103 filebox->column_of_type(FILEBOX_NAME), 0);
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);
118 int BC_FileBoxListBox::column_resize_event()
120 for(int i = 0; i < filebox->columns; i++)
121 BC_WindowBase::get_resources()->filebox_columnwidth[i] =
122 filebox->column_width[i] =
127 int BC_FileBoxListBox::sort_order_event()
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);
135 int BC_FileBoxListBox::move_column_event()
137 filebox->move_column(get_from_column(), get_to_column());
141 int BC_FileBoxListBox::evaluate_query(char *string)
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);
149 // Get best score in remaining items
150 int lowest_score = 0x7fffffff;
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++)
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(),
161 //printf(" %d i=%d %d %s %s\n", __LINE__, i, current_score, string, column->get(i)->get_text());
163 if(abs(current_score) < lowest_score)
165 lowest_score = abs(current_score);
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)
179 this->filebox = filebox;
182 BC_FileBoxTextBox::~BC_FileBoxTextBox()
186 int BC_FileBoxTextBox::handle_event()
189 if(get_keypress() != RETURN)
191 result = calculate_suggestions(&filebox->list_column[0]);
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())
200 this->filebox = filebox;
203 int BC_FileBoxDirectoryText::handle_event()
205 const char *path = get_text();
206 if( !filebox->fs->is_dir(path) ) return 0;
207 const char *cp = path;
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;
214 strcpy(filebox->directory, file_path);
215 filebox->update_history();
216 filebox->fs->change_dir(file_path);
225 BC_FileBoxFilterText::BC_FileBoxFilterText(int x, int y, BC_FileBox *filebox)
226 : BC_TextBox(x, y, filebox->get_w() - x - 50, 1, filebox->get_resources()->filebox_filter)
228 this->filebox = filebox;
231 int BC_FileBoxFilterText::handle_event()
233 filebox->update_filter(get_text());
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)
242 this->filebox = filebox;
243 set_tooltip(_("Change the filter"));
246 int BC_FileBoxFilterMenu::handle_event()
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());
256 BC_FileBoxSizeFormat::BC_FileBoxSizeFormat(int x, int y, BC_FileBox *file_box)
257 : BC_Button(x, y, &BC_WindowBase::get_resources()->
258 filebox_szfmt_images[3*file_box->size_format])
260 this->file_box = file_box;
261 set_tooltip(_("Size numeric format"));
263 BC_FileBoxSizeFormat::~BC_FileBoxSizeFormat()
267 int BC_FileBoxSizeFormat::handle_event()
269 if( ++file_box->size_format > FILEBOX_SIZE_THOU )
270 file_box->size_format = FILEBOX_SIZE_RAW;
271 BC_WindowBase::get_resources()->filebox_size_format = file_box->size_format;
272 set_images(&BC_WindowBase::get_resources()->
273 filebox_szfmt_images[3*file_box->size_format]);
275 file_box->refresh(0);
280 BC_FileBoxUseThis::BC_FileBoxUseThis(BC_FileBox *filebox)
281 : BC_Button(filebox->get_w() / 2 -
282 BC_WindowBase::get_resources()->usethis_button_images[0]->get_w() / 2,
283 filebox->ok_button->get_y(),
284 BC_WindowBase::get_resources()->usethis_button_images)
286 this->filebox = filebox;
287 set_tooltip(_("Submit the directory"));
290 BC_FileBoxUseThis::~BC_FileBoxUseThis()
294 int BC_FileBoxUseThis::handle_event()
296 // printf("BC_FileBoxUseThis::handle_event %d '%s'\n",
298 // filebox->textbox->get_text());
299 filebox->submit_file(filebox->textbox->get_text(), 1);
304 BC_FileBoxOK::BC_FileBoxOK(BC_FileBox *filebox)
305 : BC_OKButton(filebox,
306 !filebox->want_directory ?
307 BC_WindowBase::get_resources()->ok_images :
308 BC_WindowBase::get_resources()->filebox_descend_images)
310 this->filebox = filebox;
311 if(filebox->want_directory)
312 set_tooltip(_("Descend directory"));
314 set_tooltip(_("Submit the file"));
317 BC_FileBoxOK::~BC_FileBoxOK()
321 int BC_FileBoxOK::handle_event()
323 //printf("BC_FileBoxOK::handle_event %d\n", __LINE__);
324 const char *path = filebox->textbox->get_text();
325 if( *path ) filebox->submit_file(path);
329 BC_FileBoxCancel::BC_FileBoxCancel(BC_FileBox *filebox)
330 : BC_CancelButton(filebox)
332 this->filebox = filebox;
333 set_tooltip(_("Cancel the operation"));
336 BC_FileBoxCancel::~BC_FileBoxCancel()
340 int BC_FileBoxCancel::handle_event()
342 // filebox->submit_file(filebox->textbox->get_text());
343 filebox->newfolder_thread->interrupt();
344 filebox->set_done(1);
350 BC_FileBoxText::BC_FileBoxText(int x, int y, BC_FileBox *filebox)
351 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_text_images)
353 this->filebox = filebox;
354 set_tooltip(_("Display text"));
356 int BC_FileBoxText::handle_event()
358 filebox->create_listbox(filebox->listbox->get_x(), filebox->listbox->get_y(), LISTBOX_TEXT);
359 filebox->listbox->show_window(1);
364 BC_FileBoxIcons::BC_FileBoxIcons(int x, int y, BC_FileBox *filebox)
365 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_icons_images)
367 this->filebox = filebox;
368 set_tooltip(_("Display icons"));
370 int BC_FileBoxIcons::handle_event()
372 filebox->create_listbox(filebox->listbox->get_x(), filebox->listbox->get_y(), LISTBOX_ICONS);
373 filebox->listbox->show_window(1);
378 BC_FileBoxNewfolder::BC_FileBoxNewfolder(int x, int y, BC_FileBox *filebox)
379 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_newfolder_images)
381 this->filebox = filebox;
382 set_tooltip(_("Create new folder"));
384 int BC_FileBoxNewfolder::handle_event()
386 filebox->newfolder_thread->start_new_folder();
391 BC_FileBoxRename::BC_FileBoxRename(int x, int y, BC_FileBox *filebox)
392 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_rename_images)
394 this->filebox = filebox;
395 set_tooltip(_("Rename file"));
397 int BC_FileBoxRename::handle_event()
399 filebox->rename_thread->start_rename();
403 BC_FileBoxUpdir::BC_FileBoxUpdir(int x, int y, BC_FileBox *filebox)
404 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_updir_images)
406 this->filebox = filebox;
407 set_tooltip(_("Up a directory"));
409 int BC_FileBoxUpdir::handle_event()
411 // Need a temp so submit_file can expand it
412 sprintf(string, "..");
413 filebox->submit_file(string);
417 BC_FileBoxDelete::BC_FileBoxDelete(int x, int y, BC_FileBox *filebox)
418 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_delete_images)
420 this->filebox = filebox;
421 set_tooltip(_("Delete files"));
423 int BC_FileBoxDelete::handle_event()
425 filebox->unlock_window();
426 filebox->delete_thread->start();
427 filebox->lock_window("BC_FileBoxDelete::handle_event");
431 BC_FileBoxReload::BC_FileBoxReload(int x, int y, BC_FileBox *filebox)
432 : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_reload_images)
434 this->filebox = filebox;
435 set_tooltip(_("Refresh"));
437 int BC_FileBoxReload::handle_event()
445 BC_FileBox::BC_FileBox(int x, int y, const char *init_path,
446 const char *title, const char *caption, int show_all_files,
447 int want_directory, int multiple_files, int h_padding)
448 : BC_Window(title, x, y,
449 BC_WindowBase::get_resources()->filebox_w,
450 BC_WindowBase::get_resources()->filebox_h,
454 // if(want_directory)
456 // fs->set_want_directory();
457 // columns = DIRBOX_COLUMNS;
458 // columns = FILEBOX_COLUMNS;
462 columns = FILEBOX_COLUMNS;
465 list_column = new ArrayList<BC_ListBoxItem*>[columns];
466 column_type = new int[columns];
467 column_width = new int[columns];
473 size_format = BC_WindowBase::get_resources()->filebox_size_format;
475 strcpy(this->caption, caption);
476 strcpy(this->current_path, init_path);
477 strcpy(this->submitted_path, init_path);
478 select_multiple = multiple_files;
479 this->want_directory = want_directory;
480 if(show_all_files) fs->set_show_all();
481 fs->complete_path(this->current_path);
482 strcpy(this->submitted_path, this->current_path);
483 fs->extract_dir(directory, this->current_path);
484 fs->extract_name(filename, this->current_path);
486 // printf("BC_FileBox::BC_FileBox %d '%s' '%s' '%s'\n",
488 // this->submitted_path,
492 // if(want_directory)
494 // for(int i = 0; i < columns; i++)
496 // column_type[i] = get_resources()->dirbox_columntype[i];
497 // column_width[i] = get_resources()->dirbox_columnwidth[i];
498 // column_titles[i] = BC_FileBox::columntype_to_text(column_type[i]);
500 // sort_column = get_resources()->dirbox_sortcolumn;
501 // sort_order = get_resources()->dirbox_sortorder;
505 for(int i = 0; i < columns; i++)
507 column_type[i] = get_resources()->filebox_columntype[i];
508 column_width[i] = get_resources()->filebox_columnwidth[i];
509 column_titles[i] = (char*)BC_FileBox::columntype_to_text(column_type[i]);
511 sort_column = get_resources()->filebox_sortcolumn;
512 sort_order = get_resources()->filebox_sortorder;
517 // Test if current directory exists
518 if(!fs->is_dir(directory))
520 sprintf(directory, "~");
521 fs->complete_path(directory);
522 strcpy(current_path,directory);
525 fs->set_current_dir(directory);
529 h_padding = BC_WindowBase::get_resources()->ok_images[0]->get_h() -
532 this->h_padding = h_padding;
533 delete_thread = new BC_DeleteThread(this);
537 BC_FileBox::~BC_FileBox()
539 // this has to be destroyed before tables, because it can call for an update!
540 delete newfolder_thread;
543 for(int i = 0; i < TOTAL_ICONS; i++)
545 filter_list.remove_all_objects();
546 delete [] list_column;
547 delete [] column_type;
548 delete [] column_width;
549 delete delete_thread;
550 delete rename_thread;
551 recent_dirs.remove_all_objects();
554 void BC_FileBox::create_objects()
556 lock_window("BC_FileBox::create_objects");
558 BC_Resources *resources = BC_WindowBase::get_resources();
559 int directory_title_margin = MAX(20,
560 resources->filebox_text_images[0]->get_h());
562 // Directories aren't filtered in FileSystem so skip this
565 filter_list.append(new BC_ListBoxItem("*"));
566 filter_list.append(new BC_ListBoxItem("[*.mkv][*.webm]"));
567 filter_list.append(new BC_ListBoxItem("[*.mp4][*.MP4]"));
568 filter_list.append(new BC_ListBoxItem("[*.mp2][*.mp3][*.wav]"));
569 filter_list.append(new BC_ListBoxItem("[*.avi][*.mpg][*.m2v][*.m1v][*.mov]"));
570 filter_list.append(new BC_ListBoxItem("[*.jpg][*.JPG][*.png][*.gif][*.tiff]"));
571 filter_list.append(new BC_ListBoxItem("*.xml"));
572 fs->set_filter(get_resources()->filebox_filter);
578 add_subwindow(ok_button = new BC_FileBoxOK(this));
580 add_subwindow(usethis_button = new BC_FileBoxUseThis(this));
581 add_subwindow(cancel_button = new BC_FileBoxCancel(this));
583 add_subwindow(new BC_Title(x, y, caption));
585 x = get_w() - resources->filebox_icons_images[0]->get_w() - 10;
587 add_subwindow(icon_button = new BC_FileBoxIcons(x, y, this));
588 x -= resources->filebox_text_images[0]->get_w() + 5;
590 add_subwindow(text_button = new BC_FileBoxText(x, y, this));
591 x -= resources->filebox_newfolder_images[0]->get_w() + 5;
593 add_subwindow(folder_button = new BC_FileBoxNewfolder(x, y, this));
594 x -= resources->filebox_delete_images[0]->get_w() + 5;
596 add_subwindow(rename_button = new BC_FileBoxRename(x, y, this));
597 x -= resources->filebox_delete_images[0]->get_w() + 5;
599 add_subwindow(delete_button = new BC_FileBoxDelete(x, y, this));
600 x -= resources->filebox_reload_images[0]->get_w() + 5;
602 add_subwindow(reload_button = new BC_FileBoxReload(x, y, this));
603 x -= resources->filebox_updir_images[0]->get_w() + 5;
605 add_subwindow(updir_button = new BC_FileBoxUpdir(x, y, this));
606 x -= resources->filebox_szfmt_images[0]->get_w() + 5;
608 add_subwindow(szfmt_button = new BC_FileBoxSizeFormat(x, y, this));
611 y += directory_title_margin + 3;
613 add_subwindow(recent_popup = new BC_FileBoxRecent(this, x, y));
614 add_subwindow(directory_title = new BC_FileBoxDirectoryText(x, y, this));
615 directory_title->reposition_window(x, y, get_w() - recent_popup->get_w() - 20, 1);
616 x += directory_title->get_w() + 8;
617 recent_popup->reposition_window(x, y, directory_title->get_w(), 200);
620 y += directory_title->get_h() + 5;
622 int newest_id = 0, newest = -1;
623 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++) {
624 if( !resources->filebox_history[i].path[0] ) continue;
625 if( resources->filebox_history[i].id > newest_id ) {
626 newest_id = resources->filebox_history[i].id;
631 strcpy(directory, resources->filebox_history[newest].path);
632 fs->change_dir(directory, 0);
633 strcpy(directory, fs->get_current_dir());
634 directory_title->update(fs->get_current_dir());
636 fs->set_sort_order(sort_order);
637 fs->set_sort_field(column_type[sort_column]);
638 fs->update(directory);
640 // Create recent dir list
645 create_listbox(x, y, get_display_mode());
646 y += listbox->get_h() + 10;
647 add_subwindow(textbox = new BC_FileBoxTextBox(x, y, this));
648 y += textbox->get_h() + 10;
650 if(!want_directory) {
651 add_subwindow(filter_title = new BC_Title(x, y, _("Specify filter:")));
652 int x1 = x + filter_title->get_w() + 10;
653 add_subwindow(filter_text = new BC_FileBoxFilterText(x1, y, this));
654 add_subwindow(filter_popup =
655 new BC_FileBoxFilterMenu(x1 + filter_text->get_w(), y, this));
658 // listbox has to be active because refresh might be called from newfolder_thread
660 newfolder_thread = new BC_NewFolderThread(this);
662 rename_thread = new BC_RenameThread(this);
669 int BC_FileBox::get_listbox_w()
674 int BC_FileBox::get_listbox_h(int y)
676 int result = get_h() - y - h_padding - 10;
678 result -= BC_WindowBase::get_resources()->dirbox_margin;
680 result -= BC_WindowBase::get_resources()->filebox_margin;
685 int BC_FileBox::create_icons()
687 for(int i = 0; i < TOTAL_ICONS; i++)
689 icons[i] = new BC_Pixmap(this,
690 BC_WindowBase::get_resources()->type_to_icon[i],
696 int BC_FileBox::resize_event(int w, int h)
698 draw_background(0, 0, w, h);
701 // OK button handles resize event itself
702 // ok_button->reposition_window(ok_button->get_x(),
703 // h - (get_h() - ok_button->get_y()));
704 // cancel_button->reposition_window(w - (get_w() - cancel_button->get_x()),
705 // h - (get_h() - cancel_button->get_y()));
707 usethis_button->reposition_window(w / 2 - 50,
708 h - (get_h() - usethis_button->get_y()));
711 if(filter_popup) filter_popup->reposition_window(w - (get_w() - filter_popup->get_x()),
712 h - (get_h() - filter_popup->get_y()),
716 if(filter_title) filter_title->reposition_window(filter_title->get_x(),
717 h - (get_h() - filter_title->get_y()));
718 if(filter_text) filter_text->reposition_window(filter_text->get_x(),
719 h - (get_h() - filter_text->get_y()),
720 w - (get_w() - filter_text->get_w()),
722 directory_title->reposition_window(
723 directory_title->get_x(),
724 directory_title->get_y(),
725 get_w() - recent_popup->get_w() - 20,
727 recent_popup->reposition_window(
728 directory_title->get_x() + directory_title->get_w() + 8,
729 directory_title->get_y(),
730 directory_title->get_w() + recent_popup->get_w(),
731 recent_popup->get_h());
732 textbox->reposition_window(textbox->get_x(),
733 h - (get_h() - textbox->get_y()),
734 w - (get_w() - textbox->get_w()),
736 listbox->reposition_window(listbox->get_x(),
738 w - (get_w() - listbox->get_w()),
739 h - (get_h() - listbox->get_h()),
741 int dx = w - get_w();
742 icon_button->reposition_window(icon_button->get_x()+dx, icon_button->get_y());
743 text_button->reposition_window(text_button->get_x()+dx, text_button->get_y());
744 folder_button->reposition_window(folder_button->get_x()+dx, folder_button->get_y());
745 rename_button->reposition_window(rename_button->get_x()+dx, rename_button->get_y());
746 reload_button->reposition_window(reload_button->get_x()+dx, reload_button->get_y());
747 delete_button->reposition_window(delete_button->get_x()+dx, delete_button->get_y());
748 updir_button->reposition_window(updir_button->get_x()+dx, updir_button->get_y());
749 szfmt_button->reposition_window(szfmt_button->get_x()+dx, szfmt_button->get_y());
751 get_resources()->filebox_w = get_w();
752 get_resources()->filebox_h = get_h();
757 int BC_FileBox::keypress_event()
759 switch(get_keypress()) {
761 if( !ctrl_down() ) break;
765 if( !ctrl_down() ) break;
769 if( !ctrl_down() ) break;
776 int BC_FileBox::close_event()
782 int BC_FileBox::handle_event()
787 int BC_FileBox::extract_extension(char *out, const char *in)
791 while( --i>=0 && in[i]!='.' );
792 if( i >= 0 ) strcpy(out, &in[++i]);
796 static inline int64_t ipow(int m, int n)
799 for( int64_t vv=m; n>0; vv*=vv,n>>=1 ) if( n & 1 ) v *= vv;
802 static inline int ilen(int64_t v)
805 while( len<16 && (v/=10)>0 ) ++len;
809 int BC_FileBox::create_tables(int select_all)
812 char string[BCTEXTLEN];
813 BC_ListBoxItem *new_item;
815 fs->set_sort_order(sort_order);
816 fs->set_sort_field(column_type[sort_column]);
818 for(int i = 0; i < fs->total_files(); i++)
820 FileItem *file_item = fs->get_entry(i);
821 int is_dir = file_item->is_dir;
822 BC_Pixmap* icon = get_icon(file_item->name, is_dir);
825 new_item = new BC_ListBoxItem(file_item->name,
827 is_dir ? get_resources()->directory_color : get_resources()->file_color);
828 if(is_dir) new_item->set_searchable(0);
829 list_column[column_of_type(FILEBOX_NAME)].append(new_item);
832 // if(!want_directory)
836 int64_t size = file_item->size;
837 if( (size_format == FILEBOX_SIZE_1000 && size >= 1000) ||
838 (size_format == FILEBOX_SIZE_1024 && size >= 1024) ) {
839 static const char *suffix[] = { "", "K", "M", "G", "T", "P" };
840 if( size_format == FILEBOX_SIZE_1024 ) {
841 static const long double kk = logl(1000.)/logl(1024.);
842 size = expl(kk*logl((long double)size)) + 0.5;
844 int len = ilen(size), drop = len-3, round = 1;
845 if( round && drop > 0 ) { //round
846 size += ipow(10,drop)/2;
847 len = ilen(size); drop = len-3;
849 size /= ipow(10,drop);
851 int digits = (sfx+1)*3 - len;
852 int64_t frac = ipow(10,digits);
853 int mant = size / frac;
854 int fraction = size - mant*frac;
856 if( sfx && size_format == FILEBOX_SIZE_1000 ) sfx += 'a'-'A';
858 sprintf(string, "%d.%0*d%c", mant, digits, fraction, sfx);
860 sprintf(string, "%d%c", mant, sfx);
863 sprintf(string, "%jd", size);
864 if( size_format == FILEBOX_SIZE_THOU )
865 Units::punctuate(string);
867 new_item = new BC_ListBoxItem(string, get_resources()->file_color);
871 new_item = new BC_ListBoxItem("", get_resources()->directory_color);
874 list_column[column_of_type(FILEBOX_SIZE)].append(new_item);
881 localtime_r(&file_item->mtime, &mod_time);
882 sprintf(string, "%04d.%02d.%02d %02d:%02d:%02d",
883 mod_time.tm_year+1900, mod_time.tm_mon+1, mod_time.tm_mday,
884 mod_time.tm_hour, mod_time.tm_min, mod_time.tm_sec);
885 new_item = new BC_ListBoxItem(string, get_resources()->file_color);
889 new_item = new BC_ListBoxItem("", get_resources()->directory_color);
892 list_column[column_of_type(FILEBOX_DATE)].append(new_item);
895 // if(!want_directory)
898 extract_extension(string, file_item->name);
899 new_item = new BC_ListBoxItem(string, get_resources()->file_color);
902 new_item = new BC_ListBoxItem("", get_resources()->directory_color);
904 list_column[column_of_type(FILEBOX_EXTENSION)].append(new_item);
907 if( !is_dir && select_all ) {
908 int k = list_column[0].size()-1;
909 for( int j=0; j<columns; ++j )
910 list_column[j][k]->set_selected(1);
917 int BC_FileBox::delete_tables()
919 for(int j = 0; j < columns; j++)
921 list_column[j].remove_all_objects();
926 BC_Pixmap* BC_FileBox::get_icon(char *path, int is_dir)
928 if( is_dir ) return icons[ICON_FOLDER];
929 int icon_type = ICON_UNKNOWN;
930 char *suffix = strrchr(path, '.');
931 if( suffix && *++suffix ) {
932 suffix_to_type_t *stp = &BC_WindowBase::get_resources()->suffix_to_type[0];
933 while( stp->suffix && strcasecmp(stp->suffix, suffix) ) ++stp;
934 if( stp->icon_type ) icon_type = stp->icon_type;
937 return icons[icon_type];
940 const char* BC_FileBox::columntype_to_text(int type)
943 case FILEBOX_NAME: return FILEBOX_NAME_TEXT;
944 case FILEBOX_SIZE: return FILEBOX_SIZE_TEXT;
945 case FILEBOX_DATE: return FILEBOX_DATE_TEXT;
946 case FILEBOX_EXTENSION: return FILEBOX_EXTENSION_TEXT;
951 int BC_FileBox::column_of_type(int type)
953 for(int i = 0; i < columns; i++)
954 if(column_type[i] == type) return i;
960 int BC_FileBox::refresh(int reset, int select_all)
962 fs->set_sort_order(sort_order);
963 fs->set_sort_field(column_type[sort_column]);
968 create_tables(select_all);
969 listbox->set_master_column(column_of_type(FILEBOX_NAME), 0);
970 listbox->update(list_column, column_titles, column_width, columns,
971 reset>0 ? 0 : listbox->get_xposition(),
972 reset>0 ? 0 : listbox->get_yposition(),
977 int BC_FileBox::update_filter(const char *filter)
979 fs->set_filter(filter);
982 strcpy(get_resources()->filebox_filter, filter);
988 void BC_FileBox::move_column(int src, int dst)
992 ArrayList<BC_ListBoxItem*> *new_columns =
993 new ArrayList<BC_ListBoxItem*>[columns];
994 int *new_types = new int[columns];
995 int *new_widths = new int[columns];
997 // Fill in remaining columns with consecutive data
998 for(int out_column = 0, in_column = 0;
999 out_column < columns;
1003 // Copy destination column from src column
1004 if(out_column == dst)
1006 for(int i = 0; i < list_column[src].total; i++)
1008 new_columns[out_column].append(list_column[src].values[i]);
1010 new_types[out_column] = column_type[src];
1011 new_widths[out_column] = column_width[src];
1016 // Skip source column
1017 if(in_column == src) in_column++;
1018 for(int i = 0; i < list_column[src].total; i++)
1020 new_columns[out_column].append(list_column[in_column].values[i]);
1022 new_types[out_column] = column_type[in_column];
1023 new_widths[out_column] = column_width[in_column];
1028 delete [] list_column;
1029 delete [] column_type;
1030 delete [] column_width;
1031 list_column = new_columns;
1032 column_type = new_types;
1033 column_width = new_widths;
1035 for(int i = 0; i < columns; i++)
1037 get_resources()->filebox_columntype[i] = column_type[i];
1038 get_resources()->filebox_columnwidth[i] = column_width[i];
1039 column_titles[i] = (char*)BC_FileBox::columntype_to_text(column_type[i]);
1047 int BC_FileBox::submit_dir(char *dir)
1049 strcpy(directory, dir);
1050 fs->join_names(current_path, directory, filename);
1052 // printf("BC_FileBox::submit_dir %d '%s' '%s' '%s'\n",
1057 strcpy(submitted_path, current_path);
1058 fs->change_dir(dir, 0);
1060 directory_title->update(fs->get_current_dir());
1062 textbox->update(fs->get_current_dir());
1064 textbox->update(filename);
1065 listbox->reset_query();
1069 int BC_FileBox::submit_file(const char *path, int use_this)
1071 char path1[BCTEXTLEN];
1072 strcpy(path1, path);
1073 char *cp = strchr(path1,'\n');
1076 // Deactivate textbox to hide suggestions
1077 textbox->deactivate();
1079 // If file wanted, take the current directory as the desired file.
1080 // If directory wanted, ignore it.
1081 if(!path1[0] && !want_directory)
1083 // save complete path
1084 strcpy(this->current_path, directory);
1085 // save complete path
1086 strcpy(this->submitted_path, directory);
1088 // Zero out filename
1094 // is a directory, change directories
1095 if(fs->is_dir(path1) && !use_this)
1097 fs->change_dir(path1, 0);
1099 directory_title->update(fs->get_current_dir());
1100 strcpy(this->current_path, fs->get_current_dir());
1101 strcpy(this->submitted_path, fs->get_current_dir());
1102 strcpy(this->directory, fs->get_current_dir());
1106 textbox->update(fs->get_current_dir());
1108 textbox->update("");
1109 listbox->reset_query();
1113 // Is a file or desired directory. Quit the operation.
1115 char path2[BCTEXTLEN];
1116 strcpy(path2, path1);
1118 // save directory for defaults
1119 fs->extract_dir(directory, path2);
1121 // Just take the directory
1125 strcpy(path2, directory);
1128 // Take the complete path
1130 fs->extract_name(filename, path2); // save filename
1133 fs->complete_path(path2);
1134 strcpy(this->current_path, path2); // save complete path
1135 strcpy(this->submitted_path, path2); // save complete path
1137 newfolder_thread->interrupt();
1144 void BC_FileBox::update_history()
1146 // Look for path already in history
1147 BC_Resources *resources = get_resources();
1148 char path[BCTEXTLEN];
1149 strcpy(path, directory);
1150 // enfore one trailing slash
1152 while( *cp && *cp != '\n' ) ++cp;
1153 while( cp > path && *(cp-1) == '/' ) --cp;
1154 *cp++ = '/'; *cp = 0;
1156 // int new_slot = FILEBOX_HISTORY_SIZE - 1;
1158 for(int i = FILEBOX_HISTORY_SIZE - 1; i >= 0; i--)
1160 if(resources->filebox_history[i].path[0] &&
1161 !strcmp(resources->filebox_history[i].path, path))
1163 // Got matching path.
1165 resources->filebox_history[i].id = resources->get_filebox_id();
1168 // // Shift down from this point.
1171 // strcpy(resources->filebox_history[i],
1172 // resources->filebox_history[i - 1]);
1173 // if(resources->filebox_history[i][0]) new_slot--;
1179 // if(resources->filebox_history[i][0])
1185 // Remove oldest entry if full
1186 if(resources->filebox_history[FILEBOX_HISTORY_SIZE - 1].path[0])
1188 int oldest_id = 0x7fffffff;
1190 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
1192 if(resources->filebox_history[i].path[0] &&
1193 resources->filebox_history[i].id < oldest_id)
1195 oldest_id = resources->filebox_history[i].id;
1200 for(int i = oldest; i < FILEBOX_HISTORY_SIZE - 1; i++)
1202 strcpy(resources->filebox_history[i].path,
1203 resources->filebox_history[i + 1].path);
1204 resources->filebox_history[i].id =
1205 resources->filebox_history[i + 1].id;
1210 strcpy(resources->filebox_history[FILEBOX_HISTORY_SIZE - 1].path, path);
1211 resources->filebox_history[FILEBOX_HISTORY_SIZE - 1].id = resources->get_filebox_id();
1218 for(int i = 1; i < FILEBOX_HISTORY_SIZE; i++)
1220 if( (resources->filebox_history[i - 1].path[0] &&
1221 resources->filebox_history[i].path[0] &&
1222 strcasecmp(resources->filebox_history[i - 1].path,
1223 resources->filebox_history[i].path) > 0) ||
1224 (resources->filebox_history[i - 1].path[0] == 0 &&
1225 resources->filebox_history[i].path[0]) )
1228 char temp[BCTEXTLEN];
1230 strcpy(temp, resources->filebox_history[i - 1].path);
1231 id_temp = resources->filebox_history[i - 1].id;
1232 strcpy(resources->filebox_history[i - 1].path,
1233 resources->filebox_history[i].path);
1234 resources->filebox_history[i - 1].id =
1235 resources->filebox_history[i].id;
1236 strcpy(resources->filebox_history[i].path, temp);
1237 resources->filebox_history[i].id = id_temp;
1244 // for(int i = FILEBOX_HISTORY_SIZE - 1; i > 0; i--)
1246 // strcpy(resources->filebox_history[i],
1247 // resources->filebox_history[i - 1]);
1252 // strcpy(resources->filebox_history[new_slot], path);
1255 recent_popup->update(&recent_dirs, 0, 0, 1);
1258 void BC_FileBox::create_history()
1260 BC_Resources *resources = get_resources();
1261 recent_dirs.remove_all_objects();
1262 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++) {
1263 if(resources->filebox_history[i].path[0]) {
1264 recent_dirs.append(new BC_ListBoxItem(resources->filebox_history[i].path));
1270 int BC_FileBox::get_display_mode()
1272 return top_level->get_resources()->filebox_mode;
1275 void BC_FileBox::create_listbox(int x, int y, int mode)
1277 if(listbox && listbox->get_display_mode() != mode)
1281 top_level->get_resources()->filebox_mode = mode;
1285 add_subwindow(listbox = new BC_FileBoxListBox(x, y, this));
1288 char* BC_FileBox::get_path(int selection)
1292 return get_submitted_path();
1296 BC_ListBoxItem *item = listbox->get_selection(
1297 column_of_type(FILEBOX_NAME), selection - 1);
1300 fs->join_names(string, directory, item->get_text());
1307 char* BC_FileBox::get_submitted_path()
1309 return submitted_path;
1312 char* BC_FileBox::get_current_path()
1314 //printf("BC_FileBox::get_current_path 1 %s\n", current_path);
1315 return current_path;
1318 char* BC_FileBox::get_newfolder_title()
1320 char *letter2 = strchr(title, ':');
1321 new_folder_title[0] = 0;
1324 memcpy(new_folder_title, title, letter2 - title);
1325 new_folder_title[letter2 - title] = 0;
1328 strcat(new_folder_title, _(": New folder"));
1330 return new_folder_title;
1333 char* BC_FileBox::get_rename_title()
1335 char *letter2 = strchr(title, ':');
1336 new_folder_title[0] = 0;
1339 memcpy(new_folder_title, title, letter2 - title);
1340 new_folder_title[letter2 - title] = 0;
1343 strcat(new_folder_title, _(": Rename"));
1345 return new_folder_title;
1348 char* BC_FileBox::get_delete_title()
1350 char *letter2 = strchr(title, ':');
1351 new_folder_title[0] = 0;
1354 memcpy(new_folder_title, title, letter2 - title);
1355 new_folder_title[letter2 - title] = 0;
1358 strcat(new_folder_title, _(": Delete"));
1360 return new_folder_title;
1363 void BC_FileBox::delete_files()
1365 // Starting at 1 causes it to ignore what's in the textbox.
1369 while((path = get_path(i)))
1371 // Not directory. Remove it.
1372 if(!fs.is_dir(path))
1374 //printf("BC_FileBox::delete_files: removing \"%s\"\n", path);
1382 BC_Button* BC_FileBox::get_ok_button()
1387 BC_Button* BC_FileBox::get_cancel_button()
1389 return cancel_button;