"Fossies" - the Fresh Open Source Software Archive

Member "xarchive-0.2.8-6/src/myfc_gtk.c" (27 Feb 2006, 9967 Bytes) of package /linux/privat/old/xarchive-0.2.8-6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "myfc_gtk.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  *  myfc_gtk.c - multiple file chooser
    3  *  Copyright (C) 2005 Lee Bigelow <ligelowbee@yahoo.com> 
    4  *
    5  *  This program is free software; you can redistribute it and/or modify
    6  *  it under the terms of the GNU General Public License as published by
    7  *  the Free Software Foundation; either version 2 of the License, or
    8  *  (at your option) any later version.
    9  *
   10  *  This program is distributed in the hope that it will be useful,
   11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  *  GNU General Public License for more details.
   14  *
   15  *  You should have received a copy of the GNU General Public License
   16  *  along with this program; if not, write to the Free Software
   17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   18  */
   19 #include"myfc_gtk.h"
   20 
   21 void 
   22 myfc_add_foreach_func(gchar *fpath, GtkListStore *myfc_ls)
   23 {
   24   GtkTreeIter iter;
   25   gchar *icon = GTK_STOCK_FILE;
   26   gchar *fdisp;
   27 
   28   fdisp = g_filename_display_name(fpath);
   29   if ( g_file_test(fpath, G_FILE_TEST_IS_DIR) == TRUE ) 
   30   {
   31     icon = GTK_STOCK_DIRECTORY;
   32   }
   33 
   34   gtk_list_store_append(myfc_ls, &iter);
   35   gtk_list_store_set(myfc_ls, &iter, COL_ICON, icon, COL_FILE, 
   36                      fpath, COL_DFILE, fdisp, -1);
   37   g_free(fdisp);
   38 }
   39 
   40 void 
   41 myfc_add_cb(GtkWidget *widget, MyfcData *myfcd)
   42 {
   43   GSList *selfiles;
   44 
   45   selfiles = gtk_file_chooser_get_filenames(myfcd->fc);
   46   g_slist_foreach(selfiles, (GFunc) myfc_add_foreach_func, myfcd->ls);
   47 }
   48 
   49 void
   50 myfc_onDragDataReceived(GtkWidget *widget,
   51                GdkDragContext *context,
   52                int x, int y,
   53                GtkSelectionData *seldata,
   54                guint info, guint time,
   55                gpointer userdata)
   56 {
   57   GtkListStore *myfc_ls;
   58   gchar **uri_list;
   59   gchar *hostname;
   60   gchar *filename;
   61   gint i;
   62   
   63   myfc_ls = GTK_LIST_STORE(userdata);
   64   uri_list = gtk_selection_data_get_uris(seldata);
   65   for(i=0; uri_list[i] != NULL; i++)
   66   {
   67     filename = g_filename_from_uri(uri_list[i], &hostname, NULL);
   68     printf("\nhost: %s\npath: %s\n", hostname, filename);
   69     if ( filename != NULL && g_file_test(filename, G_FILE_TEST_EXISTS) )
   70      myfc_add_foreach_func(filename, myfc_ls);
   71     g_free(filename);
   72   }
   73   g_strfreev(uri_list);
   74 }
   75                
   76 void 
   77 myfc_remove_foreach_func(GtkTreeModel *model, GtkTreePath *path, 
   78                          GtkTreeIter *iter, GList **rowref_list)
   79 {
   80   GtkTreeRowReference *rowref;
   81 
   82   rowref = gtk_tree_row_reference_new(model, path);
   83   *rowref_list = g_list_append(*rowref_list, rowref);
   84 }
   85 
   86 void 
   87 myfc_remove_cb(GtkWidget *widget, MyfcData *myfcd)
   88 {
   89   GtkTreeSelection *sel;
   90   GtkTreePath *path;
   91   GtkTreeIter iter;
   92   GList *rr_list = NULL; /* row ref list of selected rows */
   93   GList *node;
   94     
   95   sel = gtk_tree_view_get_selection(myfcd->tv);
   96   gtk_tree_selection_selected_foreach(sel, 
   97                                       (GtkTreeSelectionForeachFunc) 
   98                                       myfc_remove_foreach_func, 
   99                                       &rr_list);
  100     
  101   for (node = rr_list; node != NULL; node = node->next) 
  102   {
  103     path = gtk_tree_row_reference_get_path((GtkTreeRowReference *)
  104                                            node->data);
  105     if (path) 
  106     {
  107       if ( gtk_tree_model_get_iter(GTK_TREE_MODEL(myfcd->ls), 
  108                                    &iter, path) ) 
  109       {
  110         gtk_list_store_remove(myfcd->ls, &iter);
  111       }
  112       gtk_tree_path_free(path);
  113     }
  114   }
  115 
  116   g_list_foreach(rr_list, (GFunc) gtk_tree_row_reference_free, NULL);
  117   g_list_free(rr_list);
  118 }
  119     
  120 GtkListStore *
  121 make_myfc_ls(void)
  122 {
  123   GtkListStore *liststore;
  124 
  125   /* store holds (icon, fpath, fdisp) */
  126   liststore = gtk_list_store_new(3, GTK_TYPE_STRING, GTK_TYPE_STRING, 
  127                                  GTK_TYPE_STRING); 
  128 
  129   return liststore;
  130 }
  131 
  132 GtkWidget *
  133 make_myfc_tree(GtkListStore *myfc_ls)
  134 {
  135   GtkTreeView *treeview;
  136   GtkTreeSelection *sel; 
  137   GtkTreeViewColumn   *tvcol; 
  138   GtkCellRenderer     *renderer;
  139   enum { TARGET_URI };
  140   GtkTargetEntry targetentries[] =
  141     {
  142       { "text/uri-list", 0, TARGET_URI },
  143     };
  144 
  145   treeview = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(myfc_ls)));
  146   gtk_tree_view_set_rules_hint(treeview, TRUE);
  147 
  148   /* make treeview use multiple selections */
  149   sel = gtk_tree_view_get_selection(treeview);
  150   gtk_tree_selection_set_mode(sel, GTK_SELECTION_MULTIPLE);
  151     
  152   /* create treeview column */
  153   tvcol = gtk_tree_view_column_new();
  154 
  155   /* filepath icon renderer*/
  156   renderer = gtk_cell_renderer_pixbuf_new();
  157   gtk_tree_view_column_pack_start(tvcol, renderer, FALSE);
  158   gtk_tree_view_column_add_attribute(tvcol, renderer, "stock-id", COL_ICON);
  159 
  160   /* filepath text renderer*/
  161   renderer = gtk_cell_renderer_text_new();
  162   gtk_tree_view_column_pack_start(tvcol, renderer, TRUE);
  163   gtk_tree_view_column_add_attribute(tvcol, renderer, "text", COL_DFILE);
  164 
  165   gtk_tree_view_column_set_title(tvcol, "File and directories to add:");
  166 
  167   /* append column to treeview */
  168   gtk_tree_view_append_column(treeview, tvcol);
  169 
  170   /* setup drag and drop attributes */
  171   gtk_drag_dest_set(GTK_WIDGET(treeview), 
  172             GTK_DEST_DEFAULT_ALL,
  173             targetentries, 1,
  174             GDK_ACTION_COPY|GDK_ACTION_MOVE);
  175   g_signal_connect(GTK_WIDGET(treeview),
  176            "drag_data_received",
  177            G_CALLBACK(myfc_onDragDataReceived),
  178            myfc_ls);
  179 
  180   return GTK_WIDGET(treeview);
  181 }
  182 
  183 gboolean
  184 view_popup_menu(GtkWidget *treeview, GdkEventButton *event, MyfcData *myfcd)
  185 {
  186   GtkWidget *menu, *menuitem;
  187 
  188   if (event->type == GDK_BUTTON_PRESS  &&  event->button == 3)
  189   {    
  190     menu = gtk_menu_new();
  191         
  192     menuitem = gtk_menu_item_new_with_label("Remove Selected From List");
  193     g_signal_connect(menuitem, "activate", (GCallback) myfc_remove_cb, myfcd);
  194 
  195     gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
  196     gtk_widget_show_all(menu);
  197     gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
  198                    (event != NULL) ? event->button : 0,
  199                    gdk_event_get_time((GdkEvent*)event));
  200     return TRUE;
  201   }
  202   return FALSE;
  203 }
  204     
  205 
  206 GtkDialog *
  207 make_myfc(gchar *archive, GtkListStore *myfc_ls)
  208 {
  209   static MyfcData myfcd;
  210   GtkDialog *dialog;
  211   GtkWidget *vpaned, *vbox, *hbox, *label, *button, *image, *scrollw;
  212   GtkTooltips *button_tips;
  213 
  214   myfcd.ls = myfc_ls;
  215   button_tips = gtk_tooltips_new();
  216 
  217   /* make general dialog box */
  218   dialog = GTK_DIALOG( gtk_dialog_new_with_buttons(
  219                                                    "Select Files or Directories To Add", NULL,
  220                                                    GTK_DIALOG_MODAL,
  221                                                    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
  222                                                    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
  223                                                    NULL) );
  224   gtk_dialog_set_alternative_button_order(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT,
  225                                           GTK_RESPONSE_CANCEL, -1);
  226   
  227   /* pack panes into dialog vbox */
  228   vpaned = gtk_vpaned_new();
  229   gtk_container_add(GTK_CONTAINER(dialog->vbox), vpaned);
  230   gtk_widget_show(vpaned);
  231 
  232   /* pack file chooser into top pane */
  233   myfcd.fc = GTK_FILE_CHOOSER(
  234                               gtk_file_chooser_widget_new(GTK_FILE_CHOOSER_ACTION_OPEN) );
  235   gtk_paned_pack1(GTK_PANED(vpaned), GTK_WIDGET(myfcd.fc), TRUE, TRUE);
  236   gtk_widget_show(GTK_WIDGET(myfcd.fc));
  237 
  238   gtk_widget_set_size_request(GTK_WIDGET(myfcd.fc), 450, 200);
  239   gtk_file_chooser_set_select_multiple(myfcd.fc, TRUE);
  240 
  241   /* pack vbox of treeview into bottom pane */
  242   vbox = gtk_vbox_new(FALSE, 0);
  243   gtk_widget_set_size_request(vbox, 450, 180);
  244   gtk_widget_show (vbox);
  245   gtk_paned_pack2(GTK_PANED(vpaned), GTK_WIDGET(vbox), TRUE, TRUE);
  246 
  247   /* hbox for buttons goes in top of vbox */
  248   hbox = gtk_hbox_new(FALSE, 0);
  249   gtk_widget_show(hbox);
  250   gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE,FALSE, 0);
  251 
  252   /* label packs into start of hbox */
  253   label = gtk_label_new(archive);
  254   gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 5);
  255   gtk_widget_show(label);
  256    
  257   gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_START);
  258     
  259   /* remove button packs into end of hbox */
  260   button = gtk_button_new_with_mnemonic("Remove _From list");
  261   gtk_box_pack_end(GTK_BOX(hbox), button, FALSE, FALSE, 0);
  262   gtk_widget_show(button);
  263   g_signal_connect(G_OBJECT(button), "clicked", 
  264                    G_CALLBACK(myfc_remove_cb), &myfcd);
  265   image = gtk_image_new_from_stock(GTK_STOCK_GO_UP, GTK_ICON_SIZE_BUTTON);
  266   gtk_button_set_image(GTK_BUTTON(button), image);
  267   gtk_tooltips_set_tip(button_tips, button, 
  268                        "(ALT-F) Remove selected files from the \"add to archive\" list below.", NULL);
  269 
  270   /* add button packs into end of hbox */
  271   button = gtk_button_new_with_mnemonic("Add _To list");
  272   gtk_box_pack_end(GTK_BOX(hbox), button, FALSE, FALSE, 0);
  273   gtk_widget_show(button);
  274   g_signal_connect(G_OBJECT(button), "clicked", 
  275                    G_CALLBACK(myfc_add_cb), &myfcd);
  276   image = gtk_image_new_from_stock(GTK_STOCK_GO_DOWN, GTK_ICON_SIZE_BUTTON);
  277   gtk_button_set_image(GTK_BUTTON(button), image);
  278   gtk_tooltips_set_tip(button_tips, button, 
  279                        "(ALT-T) Add selected files from the \"file chooser\" above to the \"add to archive\" list below.", NULL);
  280 
  281     
  282   /* next scroll window packs in vbox */
  283   scrollw = gtk_scrolled_window_new(NULL, NULL);
  284   gtk_box_pack_start(GTK_BOX(vbox), scrollw, TRUE, TRUE, 0);
  285   gtk_widget_show(scrollw);
  286 
  287   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollw),
  288                                  GTK_POLICY_AUTOMATIC,
  289                                  GTK_POLICY_AUTOMATIC);
  290 
  291   /* finally the treeview packs into the scrollw */
  292   myfcd.tv = GTK_TREE_VIEW(make_myfc_tree(myfc_ls)); 
  293   gtk_container_add(GTK_CONTAINER(scrollw), GTK_WIDGET(myfcd.tv));
  294   gtk_widget_show(GTK_WIDGET(myfcd.tv));
  295   g_signal_connect(GTK_WIDGET(myfcd.tv), "button_press_event", (GCallback) view_popup_menu, &myfcd);
  296     
  297   return dialog;
  298 }