g_ui_gtk.c
来自「Non-Networked File System在几个非网络的(或慢网络的)U」· C语言 代码 · 共 1,262 行 · 第 1/3 页
C
1,262 行
/* NNFS: Non-Networked File System. * Copyright (C) 1998-2002 Thierry EXCOFFIER (exco@ligim.univ-lyon1.fr) * * 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., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <gtk/gtk.h>#include "g_ui.h"#include "g_const.h"#include "g_debug.h"#include "g_config.h"#include "g_malloc.h"#include "g_nnfsrc.h"#include "g_filter.h"#include "g_update.h"#include "g_secure.h"#if HAVE_STRING_H#include <string.h>#endif#ifndef HAVE_SCROLLED_WINDOW_ADD_WITH_VIEWPORT#define gtk_scrolled_window_add_with_viewport gtk_container_add#endif#define NH 16 /* Number of hosts */#define HOST pages[search_page("Hosts")]static int ganswer, gerror ;static int exit_status ;static pid_t gfils ;static GtkWidget *notebook, *window, *continue_button ;static GtkWidget *skip_reading, *ok_medium, *wquit ;static GtkWidget *format_button, *configure_button ;static GtkWidget *add_nickname_but[NH] = { NULL } ;static GtkWidget *can_not_write = NULL ;static int id_error=0, id_main=0, id_medium=0 ;static GtkTooltips *tips ;static int nb_hosts = 0 ;static g_Config *gconfig ;static g_Options *goptions ;static g_Boolean global_there_is_error = g_False ;struct pages{ char *name ; char *full_name ; char *tip ; GtkWidget *(*create)(int i) ; GtkWidget *eb ; GtkWidget *frame ; GtkWidget *content ; /* Usualy vbox */ GtkWidget *table, *label, *cancel, *ok, *lrw, *ldc, *tables[10], *button ; GtkWidget *menu ; guint signal ;} ;extern struct pages pages[] ;int search_page(char *name) ;/* * */void generic_goto_page(char *name, int set){ int i ; static int old_i = -1 ; char buf[G_LINE_CHUNK] ; i = search_page(name) ; if ( set ) if ( old_i != i ) { if ( old_i != -1 ) { gtk_widget_set_name(pages[old_i].eb, "") ; } gtk_widget_set_name(pages[i].eb, "current") ; old_i = i ; sprintf(buf, "NNFS2: %s", pages[i].full_name) ; gtk_window_set_title(GTK_WINDOW(window), buf) ; } gtk_notebook_set_page( GTK_NOTEBOOK(notebook), i) ;}void user_goto_page(GtkWidget *w, void* x, int page){ gtk_notebook_set_page( GTK_NOTEBOOK(w->parent), page) ;}void goto_page(char *name){ generic_goto_page(name, 1) ;}/* * */void remove_end(int *id){ gdk_input_remove(*id) ; *id = 0 ; if ( id_main==0 && id_error==0 && id_medium==0 ) { waitpid(gfils, &exit_status, WUNTRACED); exit_status = WEXITSTATUS(exit_status) ; gtk_label_set(GTK_LABEL(pages[search_page("Bye Bye")].content), (exit_status || global_there_is_error) ? "*************************************************\n" "*************************************************\n" "*************************************************\n" "Errors !\n" "Look more closely the informations displayed.\n" "It could come from a full disk, a permission denied, ...\n" "*************************************************\n" "*************************************************\n" "*************************************************\n" : "Update completed without error.\n" "Thank you for using NNFS2\n" "You can click on `Quit' to terminate user interface") ; gtk_widget_set_name(wquit, "normal") ; goto_page("Bye Bye") ; }}/* * */void error_function(gpointer data, gint source, GdkInputCondition condition){ gchar *str ; GtkWidget *error ; int nb ; char buf[G_LINE_CHUNK] ; char *buf2 ; error = (GtkWidget*)data ; nb = read(source, buf, G_LINE_CHUNK-1) ; if ( nb<=0 ) { remove_end(&id_error) ; } else { buf[nb] = '\0' ;g_Printf("%s", buf) ; gtk_label_get (GTK_LABEL(error), &str) ; buf2 = g_Malloc( strlen(str) + nb + 1 ) ; sprintf(buf2, "%s%s", str, buf) ; gtk_label_set (GTK_LABEL(error), buf2) ; g_Free(buf2) ; }}GtkWidget *error_page(){ GtkWidget *frame, *scrolled_window, *werror ; frame = gtk_frame_new("Error Messages") ; scrolled_window = gtk_scrolled_window_new (NULL, NULL); gtk_container_border_width (GTK_CONTAINER (scrolled_window), 5); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); werror = gtk_label_new (""); gtk_label_set_justify(GTK_LABEL(werror), GTK_JUSTIFY_LEFT) ; /* gtk_widget_set_usize(werror, 1000,100) ; */ gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window), werror) ; gtk_widget_show (werror); id_error = gdk_input_add (gerror, GDK_INPUT_READ, error_function, (gpointer)werror) ; gtk_widget_show(scrolled_window) ; gtk_container_add (GTK_CONTAINER (frame), scrolled_window); gtk_widget_show(frame) ; return(frame) ;}/* * */void unsensitivize_all(){ int i ; gtk_widget_set_sensitive(continue_button, FALSE) ; if ( format_button ) { gtk_widget_destroy(format_button) ; format_button = NULL ; } if ( configure_button ) { gtk_widget_destroy(configure_button) ; configure_button = NULL ; } for(i=0;i<NH;i++) add_nickname_but[i] = NULL ; gtk_label_set( GTK_LABEL(HOST.label), "") ;}void empty_table(){ GList *l, *ll ; ll = gtk_container_children(GTK_CONTAINER(HOST.table)) ; for(l = ll ; l ; l = l->next ) gtk_widget_destroy( GTK_WIDGET(l->data) ) ; g_list_free(ll) ; nb_hosts = 0 ;}void add_nickname(GtkWidget *widget, gpointer data){ char c[3] ; c[0] = G_ADD_NICKNAME ; c[1] = (int)data+1 ; c[2] = 0 ; write(ganswer, c, 3) ; unsensitivize_all() ; empty_table() ;}/* * */#define VO ((GtkAttachOptions)0)void fill_td(GtkWidget *table, int l, int c, char *txt, int left, int nbc){ GtkWidget *label; GtkWidget *box; label = gtk_label_new(txt) ; box = gtk_alignment_new(left?0:1,0.5,0.,0.) ; gtk_container_add (GTK_CONTAINER (box), label); gtk_widget_show(label) ; gtk_widget_show(box) ; gtk_table_attach (GTK_TABLE (table), box, c,c+nbc,l,l+1,GTK_FILL,VO,5,0);}void fill_table(GtkWidget *table, int l, char *host, char *dir, char *nb, char *date, char *ch){ GtkWidget *but ; char tmp[G_LINE_CHUNK] ; fill_td(table, l,0,host, 0,1) ; fill_td(table, l,1,dir, 1,1) ; fill_td(table, l,2,nb, 0,1) ; fill_td(table, l,3,date, 1,1) ; fill_td(table, l,3,date, 1,1) ; if ( l<2 ) return ; but = gtk_button_new_with_label("+Nickname") ; sprintf(tmp, "To add the current host (%s) as a nickname " "of the host: %s.\n" "The two hosts must access the same files " "by the same directory name (%s).", ch, host, dir ) ; gtk_tooltips_set_tip(tips, but, tmp, NULL) ; gtk_signal_connect (GTK_OBJECT (but), "clicked", GTK_SIGNAL_FUNC (add_nickname), (gpointer)(l-2)); gtk_table_attach (GTK_TABLE (table), but, 4,5,l,l+1,GTK_FILL,VO,0,0); add_nickname_but[l-2] = but ;}void configure_function(){}void format_function(){ char c ; unsensitivize_all() ; c = G_FORMAT ; write(ganswer,&c,1) ;}GtkWidget *frame_label_button(char* frame_name, char *label_content, char *button_name, void (*fct)(), int text, char *widget_name){ GtkWidget *frame, *vbox, *label, *alignment, *button ; label = gtk_label_new(label_content) ; /* gtk_label_set_justify(label, GTK_JUSTIFY_FILL) ; */ gtk_widget_show(label) ; if ( fct ) { if ( text ) { button = gtk_entry_new() ; gtk_entry_set_text (GTK_ENTRY (button), button_name) ; gtk_signal_connect (GTK_OBJECT (button), "activate", GTK_SIGNAL_FUNC (fct), NULL); } else { button = gtk_button_new_with_label(button_name) ; gtk_widget_set_name(button, widget_name) ; gtk_signal_connect(GTK_OBJECT (button),"clicked", GTK_SIGNAL_FUNC(fct),NULL); gtk_widget_show(button) ; alignment = gtk_alignment_new(0.5,0.,0.,0.) ; gtk_container_add(GTK_CONTAINER(alignment), button) ; button = alignment ; } gtk_widget_show(button) ; vbox = gtk_vbox_new(0,0) ; gtk_box_pack_start(GTK_BOX(vbox), label, 0, 0, 0) ; gtk_box_pack_start(GTK_BOX(vbox), button, 0, 0, 5) ; gtk_widget_show(vbox) ; } frame = gtk_frame_new(frame_name) ; gtk_container_border_width(GTK_CONTAINER(frame),5) ; gtk_container_add(GTK_CONTAINER(frame), fct ? vbox : label) ; gtk_widget_show(frame) ; return(frame) ;}/* * */GtkWidget *notebook_label(const char *labeltext, int page, const char *tip){ GtkWidget *label, *eb ; label = gtk_label_new (labeltext); gtk_widget_show(label) ; eb = gtk_event_box_new() ; gtk_signal_connect (GTK_OBJECT (eb), "button_press_event", (GtkSignalFunc) user_goto_page, (gpointer)page); gtk_container_add( GTK_CONTAINER(eb), label); gtk_tooltips_set_tip(tips, eb, tip, NULL) ; return(eb) ;}/* * */GtkWidget *host_page(int i){ GtkWidget *vbox, *table, *label ; vbox = gtk_vbox_new(0,0) ; table = gtk_table_new (5, 2*NH+3, 0) ; /* 2 * nb_hosts */ label = gtk_label_new("=") ; gtk_widget_show(label) ; pages[i].label = label ; gtk_box_pack_start(GTK_BOX(vbox), table, 0,0,0) ; gtk_box_pack_start(GTK_BOX(vbox), label, 0,0,0) ; format_button = frame_label_button ("Do not read medium" , "If you press this button, " "NNFS2 will create a new file system on medium.\n" "It will overwrite existing data on medium." , "Continue without reading the medium" ,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?