home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / guicast / bcfilebox.C < prev    next >
C/C++ Source or Header  |  2000-11-29  |  14KB  |  663 lines

  1. #include "bcfilebox.h"
  2. #include "bcpixmap.h"
  3. #include "bcresources.h"
  4. #include "bctitle.h"
  5. #include "filesystem.h"
  6.  
  7. #include <string.h>
  8. #include <sys/stat.h>
  9.  
  10. BC_NewFolder::BC_NewFolder(int x, int y, BC_FileBox *filebox)
  11.  : BC_Window(filebox->get_newfolder_title(), 
  12.      x, 
  13.     y, 
  14.     320, 
  15.     120, 
  16.     0, 
  17.     0, 
  18.     0, 
  19.     0, 
  20.     1)
  21. {
  22. }
  23.  
  24. BC_NewFolder::~BC_NewFolder()
  25. {
  26. }
  27.  
  28.  
  29. int BC_NewFolder::create_objects()
  30. {
  31.     int x = 10, y = 10;
  32.     add_tool(new BC_Title(x, y, "Enter the name of the folder:"));
  33.     y += 20;
  34.     add_subwindow(textbox = new BC_TextBox(x, y, 300, 1, "Untitled"));
  35.     y += 30;
  36.     add_subwindow(new BC_OKButton(x, y));
  37.     x = get_w() - 100;
  38.     add_subwindow(new BC_CancelButton(x, y));
  39.     show_window();
  40.     return 0;
  41. }
  42.  
  43. char* BC_NewFolder::get_text()
  44. {
  45.     return textbox->get_text();
  46. }
  47.  
  48.  
  49. BC_NewFolderThread::BC_NewFolderThread(BC_FileBox *filebox)
  50. {
  51.     this->filebox = filebox;
  52.     active = 0;
  53.     Thread::synchronous = 0;
  54. }
  55.  
  56. BC_NewFolderThread::~BC_NewFolderThread() 
  57. {
  58. }
  59.  
  60. void BC_NewFolderThread::run()
  61. {
  62.     int result = window->run_window();
  63.  
  64.     if(!result)
  65.     {
  66.         char new_folder[BCTEXTLEN];
  67.         filebox->fs->join_names(new_folder, filebox->fs->get_current_dir(), window->get_text());
  68.         mkdir(new_folder, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  69.     }
  70.  
  71.     change_lock.lock();
  72.     active = 0;
  73.     change_lock.unlock();
  74.     delete window;
  75.     completion_lock.unlock();
  76. }
  77.  
  78. int BC_NewFolderThread::interrupt()
  79. {
  80.     change_lock.lock();
  81.     if(active)
  82.     {
  83.         window->lock_window();
  84.         window->set_done(1);
  85.         window->unlock_window();
  86.     }
  87.  
  88.     change_lock.unlock();
  89.  
  90.     completion_lock.lock();
  91.     completion_lock.unlock();
  92.     return 0;
  93. }
  94.  
  95. int BC_NewFolderThread::start_new_folder()
  96. {
  97.     window = new BC_NewFolder(filebox->get_abs_cursor_x(), filebox->get_abs_cursor_y(), filebox);
  98.     window->create_objects();
  99.  
  100.     change_lock.lock();
  101.     active = 1;
  102.     change_lock.unlock();
  103.  
  104.     Thread::start();
  105.  
  106.     completion_lock.lock();
  107.     return 0;
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. BC_FileBoxListBox::BC_FileBoxListBox(int x, int y, BC_FileBox *filebox)
  117.  : BC_ListBox(x, 
  118.              y, 
  119.              filebox->get_listbox_w(), 
  120.             filebox->get_listbox_h(), 
  121.              filebox->get_display_mode(), 
  122.              filebox->list_column, 
  123.             filebox->column_titles,
  124.             filebox->column_width,
  125.             FILEBOX_COLUMNS,
  126.             0,
  127.             0,
  128.             filebox->select_multiple ? LISTBOX_MULTIPLE : LISTBOX_SINGLE,
  129.             ICON_LEFT,
  130.             1,
  131.             0)
  132.     this->filebox = filebox; 
  133. }
  134.  
  135. BC_FileBoxListBox::~BC_FileBoxListBox() {}
  136.  
  137. int BC_FileBoxListBox::handle_event()
  138. {
  139.     filebox->submit_file(filebox->textbox->get_text(), 0);
  140.     return 1;
  141. }
  142.  
  143. int BC_FileBoxListBox::selection_changed()
  144. {
  145.     BC_ListBoxItem *item = get_selection(0, 0);
  146.     if(item)
  147.         filebox->textbox->update(item->get_text());
  148.     return 0;
  149. }
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156. BC_FileBoxTextBox::BC_FileBoxTextBox(int x, int y, BC_FileBox *filebox)
  157.  : BC_TextBox(x, y, filebox->get_w() - 20, 1, filebox->filename)
  158. {
  159.     this->filebox = filebox; 
  160. }
  161.  
  162. BC_FileBoxTextBox::~BC_FileBoxTextBox()
  163. {
  164. }
  165.  
  166. int BC_FileBoxTextBox::handle_event()
  167. {
  168.     
  169.     return 1;
  170. }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. BC_FileBoxFilterText::BC_FileBoxFilterText(int x, int y, BC_FileBox *filebox)
  179.  : BC_TextBox(x, y, filebox->get_w() - 50, 1, filebox->get_resources()->filebox_filter)
  180. {
  181.     this->filebox = filebox;
  182. }
  183.  
  184. int BC_FileBoxFilterText::handle_event()
  185. {
  186.     filebox->update_filter(get_text());
  187.     return 0;
  188. }
  189.  
  190.  
  191.  
  192.  
  193. BC_FileBoxFilterMenu::BC_FileBoxFilterMenu(int x, int y, BC_FileBox *filebox)
  194.  : BC_ListBox(x, y, filebox->get_w() - 30, 100, LISTBOX_TEXT, &filebox->filter_list, 0, 0, 1, 0, 1)
  195. {
  196.     this->filebox = filebox;
  197.     set_tooltip("Change the filter");
  198. }
  199.  
  200. int BC_FileBoxFilterMenu::handle_event()
  201. {
  202.     filebox->filter_text->update(get_selection(0, 0)->get_text());
  203.     filebox->update_filter(get_selection(0, 0)->get_text());
  204.     return 0;
  205. }
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. BC_FileBoxCancel::BC_FileBoxCancel(BC_FileBox *filebox)
  217.  : BC_CancelButton(filebox)
  218. {
  219.     this->filebox = filebox;
  220.     set_tooltip("Cancel the operation");
  221. }
  222.  
  223. BC_FileBoxCancel::~BC_FileBoxCancel()
  224. {
  225. }
  226.  
  227. int BC_FileBoxCancel::handle_event()
  228. {
  229.     filebox->submit_file(filebox->textbox->get_text(), 0);
  230.     filebox->newfolder_thread->interrupt();
  231.     filebox->set_done(1);
  232.     return 1;
  233. }
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241. BC_FileBoxOK::BC_FileBoxOK(BC_FileBox *filebox)
  242.  : BC_OKButton(filebox)
  243. {
  244.     this->filebox = filebox; 
  245.     set_tooltip("Submit the file");
  246. }
  247.  
  248. BC_FileBoxOK::~BC_FileBoxOK()
  249. {
  250. }
  251.  
  252. int BC_FileBoxOK::handle_event()
  253. {
  254.     filebox->submit_file(filebox->textbox->get_text(), 0);
  255.     return 1;
  256. }
  257.  
  258.  
  259.  
  260.  
  261.  
  262. BC_FileBoxText::BC_FileBoxText(int x, int y, BC_FileBox *filebox)
  263.  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_text_images)
  264. {
  265.     this->filebox = filebox; 
  266.     set_tooltip("Display text");
  267. }
  268. int BC_FileBoxText::handle_event()
  269. {
  270.     filebox->create_listbox(filebox->listbox->get_x(), filebox->listbox->get_y(), LISTBOX_TEXT);
  271.     return 1;
  272. }
  273.  
  274.  
  275. BC_FileBoxIcons::BC_FileBoxIcons(int x, int y, BC_FileBox *filebox)
  276.  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_icons_images)
  277. {
  278.     this->filebox = filebox; 
  279.     set_tooltip("Display icons");
  280. }
  281. int BC_FileBoxIcons::handle_event()
  282. {
  283.     filebox->create_listbox(filebox->listbox->get_x(), filebox->listbox->get_y(), LISTBOX_ICONS);
  284.     return 1;
  285. }
  286.  
  287.  
  288. BC_FileBoxNewfolder::BC_FileBoxNewfolder(int x, int y, BC_FileBox *filebox)
  289.  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_newfolder_images)
  290. {
  291.     this->filebox = filebox; 
  292.     set_tooltip("Create new folder");
  293. }
  294. int BC_FileBoxNewfolder::handle_event()
  295. {
  296.     filebox->newfolder_thread->start_new_folder();
  297.     return 1;
  298. }
  299.  
  300. BC_FileBoxUpdir::BC_FileBoxUpdir(int x, int y, BC_FileBox *filebox)
  301.  : BC_Button(x, y, BC_WindowBase::get_resources()->filebox_updir_images)
  302. {
  303.     this->filebox = filebox; 
  304.     set_tooltip("Up a directory");
  305. }
  306. int BC_FileBoxUpdir::handle_event()
  307. {
  308.     filebox->submit_file("..", 0);
  309.     return 1;
  310. }
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319. char* BC_FileBox::column_titles[FILEBOX_COLUMNS] = 
  320. {
  321.     "File",
  322.     "Size"
  323. };
  324.  
  325. BC_FileBox::BC_FileBox(int x, int y, 
  326.         char *init_path,
  327.         char *title,
  328.         char *caption,
  329. // Set to 1 to get hidden files. 
  330.         int show_all_files,
  331. // Want only directories
  332.         int want_directoryx,
  333.         int multiple_files,
  334.         int h_padding)
  335.  : BC_Window(title, 
  336.      x,
  337.     y,
  338.      BC_WindowBase::get_resources()->filebox_w, 
  339.     BC_WindowBase::get_resources()->filebox_h, 
  340.     10, 
  341.     10,
  342.     1,
  343.     0,
  344.     1)
  345. {
  346.     fs = new FileSystem;
  347.     strcpy(this->caption, caption);
  348.  
  349.     strcpy(this->path, init_path);
  350.     if(show_all_files) fs->set_show_all();
  351.     if(want_directory) fs->set_want_directory();
  352.     this->want_directory = want_directory;
  353.     select_multiple = multiple_files;
  354.     fs->complete_path(this->path);
  355.     fs->extract_dir(directory, this->path);
  356.     fs->extract_name(filename, this->path);
  357.  
  358. // Test directory
  359.     if(fs->update(directory))
  360.     {
  361.         sprintf(this->path, "~");
  362.         fs->complete_path(this->path);
  363.         fs->update(this->path);
  364.         strcpy(directory, fs->get_current_dir());
  365.         sprintf(filename, "");
  366.     }
  367.     this->h_padding = h_padding;
  368. }
  369.  
  370. BC_FileBox::~BC_FileBox()
  371. {
  372.     delete fs;
  373.     delete_tables();
  374.     for(int i = 0; i < TOTAL_ICONS; i++)
  375.         delete icons[i];
  376.     delete newfolder_thread;
  377. }
  378.  
  379. int BC_FileBox::create_objects()
  380. {
  381.     int x = 10, y = 10;
  382.  
  383.     column_width[0] = get_listbox_w() - 120;
  384.     column_width[1] = 120;
  385.     filter_list.append(new BC_ListBoxItem("*"));
  386.     filter_list.append(new BC_ListBoxItem("*.htal"));
  387.     filter_list.append(new BC_ListBoxItem("[*.mp2][*.mp3]"));
  388.     filter_list.append(new BC_ListBoxItem("[*.mpg][*.m2v][*.m1v][*.mov]"));
  389.     filter_list.append(new BC_ListBoxItem("[*.ifo][*.vob]"));
  390.     filter_list.append(new BC_ListBoxItem("heroine*"));
  391.     fs->set_filter(get_resources()->filebox_filter);
  392.     fs->update(directory);
  393.  
  394.     create_icons();
  395.     create_tables();
  396.  
  397.     add_subwindow(new BC_Title(x, y, caption));
  398.  
  399.     x = get_w() - 50;
  400.     add_subwindow(icon_button = new BC_FileBoxIcons(x, y, this));
  401.     x -= icon_button->get_w();
  402.     add_subwindow(text_button = new BC_FileBoxText(x, y, this));
  403.     x -= text_button->get_w();
  404.     add_subwindow(folder_button = new BC_FileBoxNewfolder(x, y, this));
  405.     x -= folder_button->get_w();
  406.     add_subwindow(updir_button = new BC_FileBoxUpdir(x, y, this));
  407.  
  408.     x = 10;
  409.     y += updir_button->get_h() + 10;
  410.     add_subwindow(directory_title = new BC_Title(x, y - 20, fs->get_current_dir()));
  411.     listbox = 0;
  412.  
  413.     create_listbox(x, y, get_display_mode());
  414.     y += listbox->get_h() + 10;
  415.     add_subwindow(textbox = new BC_FileBoxTextBox(x, y, this));
  416.     y += textbox->get_h() + 10;
  417.     add_subwindow(filter_text = new BC_FileBoxFilterText(x, y, this));
  418.     add_subwindow(filter_popup = new BC_FileBoxFilterMenu(x + filter_text->get_w(), y, this));
  419.  
  420.     add_subwindow(ok_button = new BC_FileBoxOK(this));
  421.     add_subwindow(cancel_button = new BC_FileBoxCancel(this));
  422.     
  423.     newfolder_thread = new BC_NewFolderThread(this);
  424.     show_window();
  425.     return 0;
  426. }
  427.  
  428. int BC_FileBox::get_listbox_w()
  429. {
  430.     return get_w() - 20;
  431. }
  432.  
  433. int BC_FileBox::get_listbox_h()
  434. {
  435.     return get_h() - 200 - h_padding;
  436. }
  437.  
  438. int BC_FileBox::create_icons()
  439. {
  440.     for(int i = 0; i < TOTAL_ICONS; i++)
  441.     {
  442.         icons[i] = new BC_Pixmap(this, 
  443.             BC_WindowBase::get_resources()->type_to_icon[i],
  444.             PIXMAP_ALPHA);
  445.     }
  446.     return 0;
  447. }
  448.  
  449. int BC_FileBox::resize_event(int w, int h)
  450. {
  451.     ok_button->reposition_window(ok_button->get_x(), h - (get_h() - ok_button->get_y()));
  452.     cancel_button->reposition_window(w - (get_w() - cancel_button->get_x()), h - (get_h() - cancel_button->get_y()));
  453.     filter_popup->reposition_window(w - (get_w() - filter_popup->get_x()), h - (get_h() - filter_popup->get_y()));
  454.     filter_text->reposition_window(filter_text->get_x(), 
  455.         h - (get_h() - filter_text->get_y()),
  456.         w - (get_w() - filter_text->get_w()),
  457.         1);
  458.     textbox->reposition_window(textbox->get_x(), 
  459.         h - (get_h() - textbox->get_y()),
  460.         w - (get_w() - textbox->get_w()),
  461.         1);
  462.     listbox->reposition_window(listbox->get_x(),
  463.         listbox->get_y(),
  464.         w - (get_w() - listbox->get_w()),
  465.         h - (get_h() - listbox->get_h()));
  466.     icon_button->reposition_window(w - (get_w() - icon_button->get_x()), 
  467.         icon_button->get_y());
  468.     text_button->reposition_window(w - (get_w() - text_button->get_x()), 
  469.         text_button->get_y());
  470.     folder_button->reposition_window(w - (get_w() - folder_button->get_x()), 
  471.         folder_button->get_y());
  472.     updir_button->reposition_window(w - (get_w() - updir_button->get_x()), 
  473.         updir_button->get_y());
  474.     set_w(w);
  475.     set_h(h);
  476.     get_resources()->filebox_w = get_w();
  477.     get_resources()->filebox_h = get_h();
  478.     return 1;
  479. }
  480.  
  481.  
  482. int BC_FileBox::create_tables()
  483. {
  484.     delete_tables();
  485.     char string[BCTEXTLEN];
  486.     BC_ListBoxItem *new_item;
  487.  
  488.     fs->update(0);
  489.     for(int i = 0; i < fs->total_files(); i++)
  490.     {
  491.         int is_dir = fs->get_entry(i)->is_dir;
  492.         BC_Pixmap* icon = get_icon(fs->get_entry(i)->name, is_dir);
  493.  
  494. // Name entry
  495.         new_item = new BC_ListBoxItem(0, 0, fs->get_entry(i)->name,
  496.             icon, is_dir ? BLUE : BLACK);
  497.         list_column[0].append(new_item);
  498.  
  499. // Size entry
  500.         if(!is_dir)
  501.         {
  502.             sprintf(string, "%d", fs->get_entry(i)->size);
  503.             new_item = new BC_ListBoxItem(string, BLACK);
  504.         }
  505.         else
  506.         {
  507.             new_item = new BC_ListBoxItem("", BLACK);
  508.         }
  509.  
  510.          list_column[1].append(new_item);
  511.     }
  512.     return 0;
  513. }
  514.  
  515. int BC_FileBox::delete_tables()
  516. {
  517.     for(int j = 0; j < FILEBOX_COLUMNS; j++)
  518.     {
  519.         for(int i = 0; i < list_column[0].total; i++)
  520.             delete list_column[j].values[i];
  521.         list_column[j].remove_all();
  522.     }
  523.     return 0;
  524. }
  525.  
  526. BC_Pixmap* BC_FileBox::get_icon(char *path, int is_dir)
  527. {
  528.     char *suffix = strrchr(path, '.');
  529.     int icon_type = ICON_UNKNOWN;
  530.  
  531.     if(is_dir) return icons[ICON_FOLDER];
  532.  
  533.     if(suffix)
  534.     {
  535.         suffix++;
  536.         if(*suffix != 0)
  537.         {
  538.             for(int i = 0; i < TOTAL_SUFFIXES; i++)
  539.             {
  540.                 if(!strcasecmp(suffix, BC_WindowBase::get_resources()->suffix_to_type[i].suffix)) 
  541.                 {
  542.                     icon_type = BC_WindowBase::get_resources()->suffix_to_type[i].icon_type;
  543.                     break;
  544.                 }
  545.             }
  546.         }
  547.     }
  548.  
  549.     return icons[icon_type];
  550. }
  551.  
  552. int BC_FileBox::update_filter(char *filter)
  553. {
  554.     fs->set_filter(filter);
  555.     fs->update();
  556.     create_tables();
  557.     listbox->update(list_column, 
  558.         column_titles, 
  559.         FILEBOX_COLUMNS, 
  560.         0, 
  561.         0,
  562.         -1, 
  563.         1);
  564.     strcpy(get_resources()->filebox_filter, filter);
  565.  
  566.     return 0;
  567. }
  568.  
  569. int BC_FileBox::submit_file(char *path, int return_value)
  570. {
  571.     if(!path[0]) return 1;   // blank
  572.  
  573. // is a directory, change directories
  574.     if(!fs->is_dir(path))
  575.     {
  576.         fs->change_dir(path);
  577.         create_tables();
  578.         listbox->update(list_column, column_titles, FILEBOX_COLUMNS, 0, 0, -1, 1);
  579.         directory_title->update(fs->get_current_dir());
  580.         strcpy(this->path, fs->get_current_dir());
  581.         strcpy(this->directory, fs->get_current_dir());
  582.         filename[0] = 0;
  583.         if(want_directory)
  584.             textbox->update(fs->get_current_dir());
  585.         else
  586.             textbox->update("");
  587.         listbox->reset_query();
  588.         return 1;
  589.     }
  590.     else
  591. // Is a file.  Quit the operation.
  592.     {
  593.         fs->extract_dir(directory, path);     // save directory for defaults
  594.         fs->extract_name(filename, path);     // save filename
  595.         fs->complete_path(path);
  596.         strcpy(this->path, path);          // save complete path
  597.         newfolder_thread->interrupt();
  598.         set_done(return_value);
  599.         return 0;
  600.     }
  601.     return 0;
  602. }
  603.  
  604. int BC_FileBox::get_display_mode()
  605. {
  606.     return top_level->get_resources()->filebox_mode;
  607. }
  608.  
  609. void BC_FileBox::create_listbox(int x, int y, int mode)
  610. {
  611.     if(listbox && listbox->get_display_mode() != mode)
  612.     {
  613.         delete listbox;
  614.         listbox = 0;
  615.         top_level->get_resources()->filebox_mode = mode;
  616.     }
  617.  
  618.     if(!listbox)
  619.         add_subwindow(listbox = new BC_FileBoxListBox(x, y, this));
  620. }
  621.  
  622. char* BC_FileBox::get_path(int selection)
  623. {
  624.     if(selection == 0)
  625.     {
  626.         return get_path();
  627.     }
  628.     else
  629.     {
  630.         BC_ListBoxItem *item = listbox->get_selection(0, selection - 1);
  631.         if(item) 
  632.         {
  633.             fs->join_names(string, directory, item->get_text());
  634.             return string;
  635.         }
  636.     }
  637.     return 0;
  638. }
  639.  
  640. char* BC_FileBox::get_path()
  641. {
  642.     return path;
  643. }
  644.  
  645. char* BC_FileBox::get_newfolder_title()
  646. {
  647.     char *letter2 = strchr(title, ':');
  648.     new_folder_title[0] = 0;
  649.     if(letter2)
  650.     {
  651.         memcpy(new_folder_title, title, letter2 - title);
  652.         new_folder_title[letter2 - title] = 0;
  653.     }
  654.  
  655.     strcat(new_folder_title, ": New folder");
  656.  
  657.     return new_folder_title;
  658. }
  659.  
  660.  
  661.  
  662.