"Fossies" - the Fresh Open Source Software Archive

Member "gtklp-1.3.4/gtklp/file.c" (1 Nov 2019, 13730 Bytes) of package /linux/privat/gtklp-1.3.4.src.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 "file.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.3.1.src_vs_1.3.3.src.

    1 /*
    2  GtkLP
    3   graphical Frontends for non-administrative CUPS users.
    4  
    5  Copyright (C) 1998-2004 Tobias Mueller
    6 
    7  This program is free software; you can redistribute it and/or
    8  modify it under the terms of the GNU General Public License
    9  as published by the Free Software Foundation; either version 2
   10  of the License, or (at your option) any later version.
   11 
   12  This program is distributed in the hope that it will be useful,
   13  but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  GNU General Public License for more details.
   16 
   17  You should have received a copy of the GNU General Public License
   18  along with this program; if not, write to the Free Software 
   19  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
   20  Boston, MA 02110-1301, USA
   21  
   22 */
   23 
   24 #include <config.h>
   25 #include <stdlib.h>
   26 #include <stdio.h>
   27 #include <string.h>
   28 #include <gtk/gtk.h>
   29 
   30 #include <sys/types.h>
   31 #include <dirent.h>
   32 
   33 #include <gettext.h>
   34 #include <libgtklp.h>
   35 
   36 #include "gtklp.h"
   37 #include "file.h"
   38 
   39 GtkWidget *fileTab, *fileRemove, *fileRemoveAll, *fileAdd;
   40 GtkWidget *fileFrame, *fileList;
   41 GtkWidget *fileRequ;
   42 int fileSelectedEntry;
   43 
   44 int fileSelectDontSelect;
   45 
   46 void fileTabSetDefaults(void)
   47 {
   48     if (DEBUG)
   49         printf("Get fileDefaults\n");
   50     /* Buttons */
   51     gtk_widget_set_sensitive(fileRemove, FALSE);
   52     gtk_widget_set_sensitive(fileRemoveAll, FALSE);
   53     if (filesToPrintAnz >= MAXPRINTFILES)
   54         gtk_widget_set_sensitive(fileAdd, FALSE);
   55 }
   56 
   57 void fileSelected(GtkWidget * widget, gint row, gint column,
   58           GdkEventButton * event, gpointer data)
   59 {
   60     if (DEBUG)
   61         printf("file-entry selected: %i->%s\n", row, filesToPrint[row]);
   62     fileSelectedEntry = row;
   63     gtk_widget_set_sensitive(fileRemove, TRUE);
   64 }
   65 
   66 void fileListEmpty(GtkWidget * widget, gpointer data)
   67 {
   68     if (DEBUG)
   69         printf("file-list empty again !\n");
   70     gtk_widget_set_sensitive(fileRemoveAll, FALSE);
   71 }
   72 
   73 #ifdef OLDDIALOG
   74 void fileSelDialogOk(GtkWidget * widget, GtkFileSelection * fs)
   75 {
   76     gtk_widget_destroy(fileRequ);
   77 }
   78 #else
   79 void fileSelDialogOk(GtkWidget * widget)
   80 {
   81     GSList *z1, *z2;
   82     gchar *entry[1][1];
   83 
   84     z1 = gtk_file_chooser_get_filenames((GtkFileChooser *) widget);
   85     z2 = z1;
   86     while (z1 != (GSList *) NULL) {
   87         if (filesToPrintAnz >= MAXPRINTFILES) {
   88             gtk_widget_set_sensitive(fileAdd, FALSE);
   89         } else {
   90             strncpy(filesToPrint[(unsigned char)filesToPrintAnz],
   91                 (char *)z1->data, (size_t) MAXPATH);
   92             entry[0][0] =
   93                 g_filename_to_utf8(filesToPrint
   94                            [(unsigned char)filesToPrintAnz],
   95                            -1, NULL, NULL, NULL);
   96             filesToPrintAnz++;
   97             gtk_clist_append(GTK_CLIST(fileList), entry[0]);
   98 
   99             g_free(entry[0][0]);
  100             if (filesToPrintAnz >= MAXPRINTFILES)
  101                 gtk_widget_set_sensitive(fileAdd, FALSE);
  102             else
  103                 gtk_widget_set_sensitive(fileRemoveAll, TRUE);
  104         }
  105         g_free(z1->data);
  106         z1 = z1->next;
  107     }
  108     g_slist_free(z2);
  109 
  110     strncpy(fileOpenPath,
  111         gtk_file_chooser_get_current_folder((GtkFileChooser *) widget),
  112         (size_t) MAXPATH);
  113 
  114     gtk_widget_destroy(widget);
  115 }
  116 #endif
  117 
  118 #ifdef OLDDIALOG
  119 void fileSelDialogDestroyed(GtkWidget * widget, gpointer data)
  120 {
  121 #if GTK_MAJOR_VERSION != 1
  122     gchar **z1;
  123     int i1;
  124     gchar *entry[1][1];
  125     DIR *dir;
  126 #endif
  127     if (DEBUG)
  128         printf("File-Requester destroyed !\n");
  129 
  130 #if GTK_MAJOR_VERSION != 1
  131     z1 = gtk_file_selection_get_selections((GtkFileSelection *) widget);
  132     i1 = 0;
  133     do {
  134         if (DEBUG)
  135             printf("File selected: %s\n", z1[i1]);
  136 
  137         dir = opendir(z1[i1]);
  138         if (dir != (DIR *) NULL) {
  139             if (DEBUG)
  140                 printf("Try to select whole DIRECTORY!!!\n");
  141             closedir(dir);
  142         } else {
  143             if (filesToPrintAnz >= MAXPRINTFILES) {
  144                 gtk_widget_set_sensitive(fileAdd, FALSE);
  145             } else {
  146                 strncpy(filesToPrint[filesToPrintAnz], z1[i1],
  147                     (size_t) MAXPATH);
  148                 filesToPrintAnz++;
  149                 entry[0][0] =
  150                     g_filename_to_utf8(z1[i1], -1, NULL, NULL,
  151                                NULL);
  152                 gtk_clist_append(GTK_CLIST(fileList), entry[0]);
  153                 g_free(entry[0][0]);
  154                 gtk_widget_set_sensitive(fileRemoveAll, TRUE);
  155             }
  156         }
  157         i1++;
  158     } while (z1[i1] != (char *)NULL);
  159     g_strfreev(z1);
  160 #endif
  161 
  162     gtk_window_set_modal(GTK_WINDOW(widget), FALSE);
  163     gtk_window_set_modal(GTK_WINDOW(mainWindow), TRUE);
  164 
  165 }
  166 #endif
  167 
  168 void filesel(GtkCList * clist, gint row, gint column, GdkEventButton * event,
  169          gpointer user_data)
  170 {
  171 #if GTK_MAJOR_VERSION == 1
  172     char tmp[MAXPATH + 1];
  173     char *z1, *z2;
  174     gchar *entry[1][1];
  175     int i1, pos;
  176 
  177     if (DEBUG)
  178         printf("filesel\n");
  179 
  180     if (fileSelectDontSelect == 1) {    /* Event was initiated from myself... */
  181         fileSelectDontSelect = 2;
  182         return;
  183     }
  184 
  185     /* Build the right path */
  186     gtk_clist_get_text(GTK_CLIST
  187                (((GtkFileSelection *) fileRequ)->file_list), row, 0,
  188                &z2);
  189     strncpy(tmp,
  190         gtk_file_selection_get_filename(GTK_FILE_SELECTION(fileRequ)),
  191         (size_t) MAXPATH);
  192     i1 = strlen(gtk_entry_get_text
  193             (GTK_ENTRY
  194              ((((GtkFileSelection *) fileRequ)->selection_entry))));
  195     z1 = tmp + strlen(tmp);
  196     z1 -= i1;
  197     *z1 = (uintptr_t) NULL;
  198     strncat(tmp, z2, (size_t) MAXPATH);
  199 
  200     pos = -1;
  201 
  202     if (DEBUG)
  203         printf("File selected: %s\n", tmp);
  204 
  205     if (strcmp((char *)user_data, "SEL") == 0) {
  206         strncpy(filesToPrint[filesToPrintAnz], tmp, (size_t) MAXPATH);
  207         filesToPrintAnz++;
  208         if (filesToPrintAnz >= MAXPRINTFILES)
  209             gtk_widget_set_sensitive(fileAdd, FALSE);
  210 
  211         entry[0][0] = tmp;
  212         gtk_clist_append(GTK_CLIST(fileList), entry[0]);
  213     } else {
  214         fileRemoveFromList(tmp);
  215     }
  216 
  217     if (DEBUG)
  218         printf("Path selected: %s\n", tmp);
  219     strncpy(fileOpenPath, tmp, (size_t) MAXPATH);   /* Remember Path for next time "Add" is pressed */
  220     if (DEBUG) {
  221         printf("FilesToPrint: \n");
  222         for (i1 = 0; i1 < filesToPrintAnz; i1++)
  223             printf("  %s\n", filesToPrint[i1]);
  224     }
  225 
  226     if (filesToPrintAnz > 0)
  227         gtk_widget_set_sensitive(fileRemoveAll, TRUE);
  228     else
  229         gtk_widget_set_sensitive(fileRemoveAll, FALSE);
  230 #else
  231     return;
  232 #endif
  233 }
  234 
  235 void dirChanged(GtkWidget * widget, gpointer data)
  236 {
  237 #if GTK_MAJOR_VERSION == 1
  238     char tmppath[MAXPATH + 1];
  239     char *z1;
  240     int i1, i2;
  241 
  242     if (fileSelectDontSelect == 2) {    /* This signal was initiated by myself... */
  243         fileSelectDontSelect = 0;
  244         return;
  245     }
  246 
  247     if (*gtk_entry_get_text(GTK_ENTRY(widget)) == 0) {
  248 
  249         for (i1 = 0; i1 < filesToPrintAnz; i1++) {
  250             i2 = 0;
  251             while (gtk_clist_get_text
  252                    (GTK_CLIST
  253                 (((GtkFileSelection *) fileRequ)->file_list),
  254                 i2, 0, &z1) == 1) {
  255                 snprintf(tmppath, (size_t) MAXPATH, "%s%s",
  256                      gtk_file_selection_get_filename
  257                      (GTK_FILE_SELECTION(fileRequ)), z1);
  258                 if (strcmp(tmppath, filesToPrint[i1]) == 0) {
  259                     fileSelectDontSelect = 1;
  260                     gtk_clist_select_row(GTK_CLIST
  261                                  (((GtkFileSelection
  262                                 *) fileRequ)->
  263                                   file_list), i2,
  264                                  0);
  265                     gtk_entry_set_text(GTK_ENTRY(widget),
  266                                "");
  267                 }
  268                 i2++;
  269             }
  270         }
  271     }
  272 #else
  273     return;
  274 #endif
  275 }
  276 
  277 void fileAddPressed(GtkWidget * widget, gpointer data)
  278 {
  279     if (DEBUG)
  280         printf("Add request\n");
  281 
  282     fileSelectDontSelect = 0;
  283 
  284 #ifdef OLDDIALOG
  285     fileRequ = gtk_file_selection_new(str2str(_("Print File...")));
  286 #else
  287     fileRequ =
  288         gtk_file_chooser_dialog_new(str2str(_("Print File...")),
  289                     GTK_WINDOW(mainWindow),
  290                     GTK_FILE_CHOOSER_ACTION_OPEN,
  291                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
  292                     GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
  293                     NULL);
  294 #endif
  295 
  296 #if GTK_MAJOR_VERSION == 1
  297     gtk_clist_set_selection_mode(GTK_CLIST
  298                      (((GtkFileSelection *) fileRequ)->
  299                       file_list), GTK_SELECTION_MULTIPLE);
  300 #else
  301 #ifdef OLDDIALOG
  302     gtk_file_selection_set_select_multiple((GtkFileSelection *) fileRequ,
  303                            TRUE);
  304 #else
  305     gtk_file_chooser_set_select_multiple((GtkFileChooser *) fileRequ, TRUE);
  306 #endif
  307 #endif
  308 
  309 #ifdef OLDDIALOG
  310     gtk_widget_set_sensitive((GtkWidget *) GTK_BUTTON(((GtkFileSelection *) fileRequ)->cancel_button), FALSE);  /* Problems with cancel on Multiselection */
  311 #endif
  312 #if GTK_MAJOR_VERSION == 1
  313     gtk_signal_connect(GTK_OBJECT
  314                (GTK_CLIST
  315                 (((GtkFileSelection *) fileRequ)->file_list)),
  316                "select-row", (GtkSignalFunc) filesel, "SEL");
  317     gtk_signal_connect(GTK_OBJECT
  318                (GTK_CLIST
  319                 (((GtkFileSelection *) fileRequ)->file_list)),
  320                "unselect-row", (GtkSignalFunc) filesel, "UNSEL");
  321 
  322     gtk_signal_connect(GTK_OBJECT
  323                (GTK_ENTRY
  324                 (((GtkFileSelection *) fileRequ)->selection_entry)),
  325                "changed", (GtkSignalFunc) dirChanged, NULL);
  326 #endif
  327 
  328 #ifdef OLDDIALOG
  329     gtk_file_selection_set_filename(GTK_FILE_SELECTION(fileRequ),
  330                     fileOpenPath);
  331     gtk_file_selection_hide_fileop_buttons(GTK_FILE_SELECTION(fileRequ));
  332     gtk_window_set_modal(GTK_WINDOW(fileRequ), TRUE);
  333     gtk_window_set_modal(GTK_WINDOW(mainWindow), FALSE);
  334     gtk_window_set_position(GTK_WINDOW(fileRequ), GTK_WIN_POS_CENTER);
  335 
  336     gtk_entry_set_editable(GTK_ENTRY
  337                    (((GtkFileSelection *) fileRequ)->
  338                 selection_entry), FALSE);
  339 
  340     gtk_signal_connect(GTK_OBJECT(fileRequ), "destroy",
  341                (GtkSignalFunc) fileSelDialogDestroyed, &fileRequ);
  342     gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(fileRequ)->ok_button),
  343                "clicked", (GtkSignalFunc) fileSelDialogOk,
  344                fileRequ);
  345 
  346     gtk_widget_show(fileRequ);
  347 #else
  348     gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fileRequ),
  349                         fileOpenPath);
  350     gtk_window_set_position(GTK_WINDOW(fileRequ), GTK_WIN_POS_CENTER);
  351     switch (gtk_dialog_run(GTK_DIALOG(fileRequ))) {
  352     case GTK_RESPONSE_ACCEPT:
  353         fileSelDialogOk(fileRequ);
  354         break;
  355     case GTK_RESPONSE_CANCEL:
  356         gtk_widget_destroy(fileRequ);
  357         break;
  358     default:        /* should be destroy dialog */
  359         gtk_widget_destroy(fileRequ);
  360         break;
  361     }
  362 #endif
  363 
  364 }
  365 
  366 void fileRemovePressed(GtkWidget * widget, gpointer data)
  367 {
  368     int i1;
  369 
  370     if (DEBUG)
  371         printf("Remove request for entry %i->%s\n", fileSelectedEntry,
  372                filesToPrint[fileSelectedEntry]);
  373 
  374     fileRemoveFromList(filesToPrint[fileSelectedEntry]);
  375 
  376     if (DEBUG) {
  377         printf("FilesToPrint: \n");
  378         for (i1 = 0; i1 < filesToPrintAnz; i1++)
  379             printf("  %s\n", filesToPrint[i1]);
  380     }
  381 }
  382 
  383 void fileRemoveAllPressed(GtkWidget * widget, gpointer data)
  384 {
  385     int i1;
  386 
  387     if (DEBUG)
  388         printf("Remove All request\n");
  389 
  390     for (i1 = filesToPrintAnz - 1; i1 >= 0; i1--) {
  391         fileRemoveFromList(filesToPrint[i1]);
  392     }
  393 
  394     gtk_widget_set_sensitive(fileRemoveAll, FALSE);
  395 
  396 }
  397 
  398 void fileRemoveFromList(char *file)
  399 {
  400     int i1, i2, i3;
  401 
  402     if (DEBUG)
  403         printf("Remove request for entry %s\n", file);
  404 
  405     i2 = filesToPrintAnz;   /* Better not use variables which were changed during "for" */
  406     for (i1 = 0; i1 < i2; i1++) {
  407         if (strcmp(filesToPrint[i1], file) == 0) {
  408             gtk_clist_remove(GTK_CLIST(fileList), i1);
  409             filesToPrintAnz--;
  410             for (i3 = i1; i3 < filesToPrintAnz; i3++)
  411                 strncpy(filesToPrint[i3], filesToPrint[i3 + 1],
  412                     (size_t) MAXPATH);
  413             i1 = i2;
  414         }
  415     }
  416 
  417     if (filesToPrintAnz < MAXPRINTFILES)
  418         gtk_widget_set_sensitive(fileAdd, TRUE);
  419     gtk_widget_set_sensitive(fileRemove, FALSE);
  420 
  421 }
  422 
  423 void FileFrame(GtkWidget * container)
  424 {
  425     GtkWidget *mainHBox, *vbox, *buttonBox, *scrolled;
  426     gchar *entry[1][1];
  427 
  428     int i1;
  429 
  430     mainHBox = gtk_hbox_new(FALSE, 0);
  431     gtk_box_pack_start(GTK_BOX(container), mainHBox, FALSE, FALSE,
  432                FRAME_SPACING_V);
  433     gtk_widget_show(mainHBox);
  434 
  435     fileFrame = gtk_frame_new(str2str(_("Files to print")));
  436     gtk_box_pack_start(GTK_BOX(mainHBox), fileFrame, TRUE, TRUE,
  437                FRAME_SPACING_V);
  438 
  439     vbox = gtk_vbox_new(FALSE, 8);
  440     gtk_container_add(GTK_CONTAINER(fileFrame), vbox);
  441     gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
  442     gtk_widget_show(vbox);
  443 
  444     scrolled = gtk_scrolled_window_new(NULL, NULL);
  445     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
  446                        GTK_POLICY_AUTOMATIC,
  447                        GTK_POLICY_AUTOMATIC);
  448     gtk_box_pack_start(GTK_BOX(vbox), scrolled, TRUE, TRUE, 0);
  449     gtk_widget_show(scrolled);
  450 
  451     fileList = gtk_clist_new(1);
  452     gtk_widget_set_size_request(fileList, -1, FILE_DIALOG_HEIGHT);
  453     gtk_container_add(GTK_CONTAINER(scrolled), fileList);
  454     gtk_clist_set_shadow_type(GTK_CLIST(fileList), GTK_SHADOW_OUT);
  455 
  456     for (i1 = 0; i1 < filesToPrintAnz; i1++) {
  457         entry[0][0] =
  458             g_filename_to_utf8((gchar *) filesToPrint[i1], -1, NULL,
  459                        NULL, NULL);
  460         gtk_clist_append(GTK_CLIST(fileList), entry[0]);
  461         g_free(entry[0][0]);
  462     }
  463     fileSelectedEntry = -1;
  464 
  465     buttonBox = gtk_hbox_new(FALSE, BUTTON_SPACING_H);
  466     gtk_box_pack_start(GTK_BOX(vbox), buttonBox, FALSE, FALSE,
  467                INFRAME_SPACING_V);
  468     gtk_widget_show(buttonBox);
  469 
  470 #if GTK_MAJOR_VERSION != 1
  471     fileAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
  472 #else
  473     fileAdd = gtk_button_new_with_label(str2str(_("Add")));
  474 #endif
  475     button_pad(fileAdd);
  476     gtk_box_pack_start(GTK_BOX(buttonBox), fileAdd, FALSE, FALSE, 0);
  477     gtk_widget_show(fileAdd);
  478 
  479 #if GTK_MAJOR_VERSION != 1
  480     fileRemove = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
  481 #else
  482     fileRemove = gtk_button_new_with_label(str2str(_("Remove")));
  483 #endif
  484     button_pad(fileRemove);
  485     gtk_box_pack_start(GTK_BOX(buttonBox), fileRemove, FALSE, FALSE, 0);
  486     gtk_widget_show(fileRemove);
  487 
  488 #if GTK_MAJOR_VERSION != 1
  489     fileRemoveAll = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
  490 #else
  491     fileRemoveAll = gtk_button_new_with_label(str2str(_("Remove All")));
  492 #endif
  493     button_pad(fileRemoveAll);
  494     gtk_box_pack_start(GTK_BOX(buttonBox), fileRemoveAll, FALSE, FALSE, 0);
  495     gtk_widget_show(fileRemoveAll);
  496 
  497     gtk_signal_connect(GTK_OBJECT(fileAdd), "released",
  498                GTK_SIGNAL_FUNC(fileAddPressed), NULL);
  499 
  500     gtk_signal_connect(GTK_OBJECT(fileRemove), "released",
  501                GTK_SIGNAL_FUNC(fileRemovePressed), NULL);
  502     gtk_signal_connect(GTK_OBJECT(fileRemoveAll), "released",
  503                GTK_SIGNAL_FUNC(fileRemoveAllPressed), NULL);
  504     gtk_signal_connect(GTK_OBJECT(fileList), "select_row",
  505                GTK_SIGNAL_FUNC(fileSelected), "NULL");
  506 
  507     gtk_widget_show(fileList);
  508 
  509 }
  510 
  511 void FileTab(void)
  512 {
  513     fileTab = gtk_vbox_new(FALSE, 0);
  514     gtk_container_set_border_width(GTK_CONTAINER(fileTab), FRAME_BORDER);
  515     gtk_notebook_append_page(GTK_NOTEBOOK(tabs), fileTab,
  516                  gtk_label_new(str2str(_("File"))));
  517 
  518     FileFrame(fileTab);
  519 
  520 }