/* Copyright (C) 1999 Southern Gold Development * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "gview.h" void getGVComment(sgview *view, const gchar *fname) { gchar *tmpW, *comment; FILE *fHnd; struct stat buff; gint err; tmpW = g_strconcat(view->currPath, ".gvcomment", fname, NULL); err = stat(tmpW, &buff); if (err == -1) { comment = NULL; } fHnd = fopen(tmpW, "rb"); if (!(fHnd)) { comment = NULL; } else { comment = g_malloc(sizeof(gchar) * buff.st_size); err = fread((void *) comment, sizeof(gchar), buff.st_size, fHnd); if (err != buff.st_size) { g_free(comment); comment = NULL; } else { comment[buff.st_size - 1] = '\0'; } fclose(fHnd); } g_free(tmpW); gtk_tooltips_set_tip(view->tips, GTK_WIDGET (view->disp), comment, NULL); if (comment) g_free(comment); } void commentDestroy(GtkWidget *widget, gpointer data) { ren *comm; gchar *tmpW; comm = (ren *) data; if (comm->view->currPic) { tmpW = g_strconcat(comm->view->currPath, ".gvcomment", rindex(comm->view->currPic->filename, '/'), NULL); if (strcmp(tmpW, (gchar *) comm->browser) == 0) { getGVComment(comm->view, rindex(comm->view->currPic->filename, '/')); } g_free(tmpW); } g_free((gchar *) comm->browser); g_free(comm); } void saveComment(GtkWidget *widget, gpointer data) { gchar *tmpW, *tmpD, *comment; ren *comm; struct stat buff; FILE *fHnd; gint err; comm = (ren *) data; tmpD = g_strconcat(comm->view->currPath, ".gvcomment", NULL); err = stat(tmpD, &buff); if (err == -1) { mkdir(tmpD, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); } g_free(tmpD); tmpW = (gchar *) comm->browser; comment = gtk_entry_get_text(GTK_ENTRY (comm->ent)); fHnd = fopen(tmpW, "wb"); err = fwrite((void *) comment, sizeof(gchar), strlen(comment), fHnd); err = fwrite((void *) "\n", sizeof(gchar), 1, fHnd); fclose(fHnd); gtk_widget_destroy(comm->rsp); } void deleComment(GtkWidget *widget, gpointer data) { gchar *tmpW; ren *comm; gint err; struct stat buff; comm = (ren *) data; tmpW = (gchar *) comm->browser; err = stat(tmpW, &buff); if (err != -1) { unlink(tmpW); } gtk_widget_destroy(comm->rsp); } void loseComment(GtkWidget *widget, gpointer data) { ren *comm; comm = (ren *) data; gtk_widget_destroy(comm->rsp); } gint commKeyPress(GtkWidget *widget, GdkEventKey *event, gpointer data) { switch (event->keyval) { case GDK_Return: case GDK_KP_Enter: saveComment(widget, data); break; case GDK_Escape: loseComment(widget, data); break; default: break; } return FALSE; } void setGVComment(sgview *view, const gchar *fname) { GtkWidget *win, *fix, *ent, *btn, *lbl; gchar *tmpW, *comment, *label, *fnameh; FILE *fHnd; gint err; struct stat buff; ren *comm; fnameh = (gchar *) fname; if (fname[0] == '/') fnameh++; comm = g_malloc(sizeof(ren)); comm->view = view; tmpW = g_strconcat(view->currPath, ".gvcomment/", fnameh, NULL); err = stat(tmpW, &buff); if (err == -1) { comment = NULL; } fHnd = fopen(tmpW, "rb"); if (!(fHnd)) { comment = NULL; } else { comment = g_malloc(sizeof(gchar) * buff.st_size); err = fread((void *) comment, sizeof(gchar), buff.st_size, fHnd); if (err != buff.st_size) { g_free(comment); comment = NULL; } else { comment[buff.st_size - 1] = '\0'; } fclose(fHnd); } comm->browser = (GtkWidget *) tmpW; win = gtk_window_new(GTK_WINDOW_DIALOG); comm->rsp = win; gtk_window_set_title(GTK_WINDOW (win), _("Set the gvComment")); gtk_window_set_position(GTK_WINDOW (win), GTK_WIN_POS_MOUSE); gtk_widget_set_usize(GTK_WIDGET (win), 300, 100); gtk_window_set_policy(GTK_WINDOW (win), FALSE, FALSE, FALSE); gtk_signal_connect(GTK_OBJECT (win), "destroy", (GtkSignalFunc) commentDestroy, (gpointer) comm); gtk_container_border_width(GTK_CONTAINER (win), 5); fix = gtk_fixed_new(); gtk_container_add(GTK_CONTAINER (win), fix); label = g_strdup_printf(_("gvComment for %s"), fnameh); lbl = gtk_label_new(label); g_free(label); gtk_fixed_put(GTK_FIXED (fix), lbl, 5, 5); gtk_widget_show(lbl); ent = gtk_entry_new(); comm->ent = ent; if (comment) { gtk_entry_set_text(GTK_ENTRY (ent), comment); g_free(comment); } gtk_widget_set_usize(GTK_WIDGET (ent), 280, 24); gtk_signal_connect(GTK_OBJECT (ent), "key_press_event", (GtkSignalFunc) commKeyPress, (gpointer) comm); gtk_fixed_put(GTK_FIXED (fix), ent, 5, 30); gtk_widget_show(ent); btn = gtk_button_new_with_label(_("Save")); gtk_widget_set_usize(GTK_WIDGET (btn), 90, 25); gtk_signal_connect(GTK_OBJECT (btn), "clicked", (GtkSignalFunc) saveComment, (gpointer) comm); gtk_fixed_put(GTK_FIXED (fix), btn, 5, 60); gtk_widget_show(btn); btn = gtk_button_new_with_label(_("Delete")); gtk_widget_set_usize(GTK_WIDGET (btn), 90, 25); gtk_signal_connect(GTK_OBJECT (btn), "clicked", (GtkSignalFunc) deleComment, (gpointer) comm); gtk_fixed_put(GTK_FIXED (fix), btn, 105, 60); gtk_widget_show(btn); btn = gtk_button_new_with_label(_("Cancel")); gtk_widget_set_usize(GTK_WIDGET (btn), 90, 25); gtk_signal_connect(GTK_OBJECT (btn), "clicked", (GtkSignalFunc) loseComment, (gpointer) comm); gtk_fixed_put(GTK_FIXED (fix), btn, 200, 60); gtk_widget_show(btn); gtk_widget_show(fix); gtk_widget_show(win); gtk_window_set_focus(GTK_WINDOW (win), ent); }