⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 callbacks.c

📁 又一开源的解压缩chm格式文件的代码
💻 C
字号:
/*************************************************************************** *   Copyright (C) 2003 by zhong                                           * *   zhongz@163.com                                                        * *                                                                         * *   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.                                   * ***************************************************************************/#include <time.h>#include <stdio.h>#include <string.h>#include <gtk/gtk.h>#include <gdk/gdkx.h>#include "interface.h"#include "callbacks.h"#include "html.h"#include "exchm.h"#include "hhc.h"#include "strings.h"static GtkWidget *chmfilew = NULL;static const gchar *MainWindowKey = "MainWindowKey";static gchar *chmsee_tmpdir_new(void);static void on_open_chmfile_ok (GtkButton *button, gpointer user_data);static void on_open_chmfile_cancel (GtkButton *button, gpointer user_data);static gboolean on_open_filesel_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data);static void create_errord();static void load_file( ChmSee *window, const gchar *str_uri );void command_delete_tmpdir (char *s_path){	if (!g_file_test (s_path, G_FILE_TEST_EXISTS)) {		return;	}	if (s_path != NULL) {		char *argv[4];				argv[0] = "rm";		argv[1] = "-rf";		argv[2] = s_path;		argv[3] = NULL;		g_spawn_async (g_get_tmp_dir (), argv, NULL, 			      G_SPAWN_SEARCH_PATH, 			      NULL, NULL, NULL,				  NULL);/*		g_spawn_sync (g_get_tmp_dir (), argv, NULL, 			      G_SPAWN_SEARCH_PATH, 			      NULL, NULL, 			      NULL, NULL, NULL,			      NULL);*/	}}static void create_errord(){	GtkWidget *dialog;	dialog = gtk_message_dialog_new (NULL,                                  GTK_DIALOG_DESTROY_WITH_PARENT,                                  GTK_MESSAGE_ERROR,                                  GTK_BUTTONS_CLOSE,                                  "打开文件失败");	g_signal_connect_swapped (GTK_OBJECT (dialog), "response",                           G_CALLBACK (gtk_widget_destroy),                           GTK_OBJECT (dialog));	gtk_widget_show(dialog);}static gchar *chmsee_tmpdir_new(void) {	gchar	*get_tmpdir;	struct tm *ptr;	time_t lt;		lt = time(NULL);	ptr = localtime(&lt);	get_tmpdir = g_strdup_printf("%s/chmsee_tmp-%s/%d%d%d%d%d%d", 								g_get_tmp_dir(),								g_get_user_name(),								ptr->tm_year,								ptr->tm_mon,								ptr->tm_mday,								ptr->tm_hour,								ptr->tm_min,								ptr->tm_sec);	return (get_tmpdir);}void chmsee_init (ChmSee *window) {	gtk_window_set_title (GTK_WINDOW (window->chm_window), "ChmSee");	gtk_widget_set_sensitive (window->chm_back, FALSE);	gtk_widget_set_sensitive (window->chm_forward, FALSE);	gtk_widget_set_sensitive (window->chm_small, FALSE);	gtk_widget_set_sensitive (window->chm_reset, FALSE);	gtk_widget_set_sensitive (window->chm_big, FALSE);	gtk_widget_set_sensitive (window->chm_search, FALSE);	gtk_widget_hide (window->chm_home);	window->link = NULL;	window->link_pos = 0;	window->current_link = NULL;	window->current_codeset = "GB18030";	window->home_url = NULL;	window->tmpdir = g_strdup_printf("%s", chmsee_tmpdir_new());	window->hhc_name = NULL;	chmsee_html_load_empty(window);	}voidgo_home(GtkWidget *widget, gpointer data){	ChmSee *window = (ChmSee *) data;	load_file(window, window->home_url);}/*static gchar *chmsee_link_split_uri (const gchar *uri, gchar **ms_its){//	gchar *ret_val;	gchar *ch;		if ((ch = strchr (uri, '::'))) {		if (ms_its) {			*ms_its = g_strdup (ch + 1);		}		//		ret_val = g_strndup (uri, ch - uri);//	} else {//		ret_val = g_strdup (uri);	}//	return ret_val;}*/static void load_file( ChmSee *window, const gchar *str_uri ) {	gchar 	*full_link;//	gchar	*ms_its;	gchar	*ch;	if ( g_strcasecmp("nolink", str_uri) == 0 ) {		return;	} else {		// MS-ITS -- MS-ITS:this->chm::/link		if ((ch = g_strrstr (str_uri, ":"))) {				str_uri = g_strdup (ch + 1);		}		if (str_uri[0] == '/') {			full_link = g_strdup_printf("%s%s", window->tmpdir, str_uri);		} else {			full_link = g_strdup_printf("%s/%s", window->tmpdir, str_uri);		}		chm_html_open_uri(window, full_link, 0);	}		if (full_link != str_uri) {		g_free (full_link);	}}voidselection_cb (GtkTreeSelection *selection, gpointer user_data){	GtkTreeIter		iter;	ChmSee			*window = (ChmSee *) user_data;	GtkTreeView		*tree = GTK_TREE_VIEW(window->chm_tree);	GtkTreeModel	*model = gtk_tree_view_get_model (tree);	GValue		value = {0,};	if (gtk_tree_selection_get_selected (selection, NULL, &iter)) {		gtk_tree_model_get_value (model, &iter, FILENAME_COLUMN, &value);		load_file (window, g_value_get_string (&value));		g_value_unset (&value);	}}void chmsee_go_ex_chm_file(ChmSee *window, const gchar *filename){	gchar *chmfile;	gchar *title;	gchar *title_utf8;	chmfile = g_strdup_printf( "%s", g_strrstr ( g_path_get_basename( filename ), "." ) );		if (!chmfile) {		chmfile = g_strdup_printf( "%s", g_strrstr ( filename , "." ) );	}	if ( g_strcasecmp(".chm", g_strdown(chmfile)) == 0 ) {		ex_chm_file ( window, filename );				if (window->hhc_name != NULL) {			open_hhc_file (window);		} else {			window->hhc_name = get_topurl(window);			if (window->hhc_name) {				load_file (window, window->hhc_name );				window->home_url = g_strdup(window->hhc_name);				gtk_widget_show (window->chm_home);			} else {				create_errord();				return;			}		}			title = g_convert(filename, -1, "UTF-8", "GB18030", NULL, NULL, NULL);		if (title) {			title_utf8 = g_strdup_printf( "%s", title );		} else {			title_utf8 = g_strdup_printf( "%s", filename );		}		title_utf8 = g_strdup_printf( "%s", g_path_get_basename( title_utf8 ));				if (title_utf8) {			title_utf8 = g_strdup_printf( "ChmSee - %s", title_utf8 );	    	gtk_window_set_title( GTK_WINDOW (window->chm_window), title_utf8 );		} else {			title_utf8 = g_strdup_printf( "ChmSee - %s", filename );			gtk_window_set_title( GTK_WINDOW (window->chm_window), title_utf8 );		}		g_free( title );		g_free(title_utf8);		g_free(chmfile);		gtk_widget_grab_focus (window->chm_tree);		gtk_widget_set_sensitive (window->chm_small, TRUE);		gtk_widget_set_sensitive (window->chm_reset, TRUE);		gtk_widget_set_sensitive (window->chm_big, TRUE);		gtk_widget_set_sensitive (window->chm_search, TRUE);	}}/*************************************************************************** * Open File Selection Dialog. ***************************************************************************/static voidon_open_chmfile_ok (GtkButton *button, gpointer user_data){	ChmSee *window = (ChmSee *) user_data;	GtkTreeView		*tree = GTK_TREE_VIEW(window->chm_tree);	GtkTreeModel	*model = gtk_tree_view_get_model (tree);	GtkWidget *filesel;	const gchar *filename;	filesel = gtk_widget_get_toplevel (GTK_WIDGET (button));	filename = (gchar *) gtk_file_selection_get_filename (GTK_FILE_SELECTION (filesel));	filename = g_strdup_printf(filename);	gtk_widget_hide (filesel);	gtk_tree_store_clear (GTK_TREE_STORE (model));	command_delete_tmpdir( window->tmpdir );	chmsee_init(window);	chmsee_go_ex_chm_file(window, filename);}static voidon_open_chmfile_cancel  (GtkButton *button, gpointer user_data){	gtk_widget_hide (gtk_widget_get_toplevel (GTK_WIDGET(button)));}static gbooleanon_open_filesel_delete_event           (GtkWidget       *widget,                                        GdkEvent        *event,                                        gpointer         user_data){  gtk_widget_hide (gtk_widget_get_toplevel (widget));  return TRUE;}voidon_open_chmfile_filesel( GtkWidget *widget, gpointer user_data ){	ChmSee *chmfile = (ChmSee *) user_data;	GtkWidget	*w = chmfile->chm_window;		if (chmfilew == NULL) {  		GtkWidget *ok_button;  		GtkWidget *cancel_button;		chmfilew = gtk_file_selection_new ("选择要打开的CHM文件");		gtk_container_set_border_width (GTK_CONTAINER (chmfilew), 10);//		gchar *filename;//		filename = g_strdup_printf("%s", "/f/book/");//		gtk_file_selection_set_filename (GTK_FILE_SELECTION (chmfilew), filename);		ok_button = GTK_FILE_SELECTION (chmfilew)->ok_button;		gtk_widget_show (ok_button);		GTK_WIDGET_SET_FLAGS (ok_button, GTK_CAN_DEFAULT);		cancel_button = GTK_FILE_SELECTION (chmfilew)->cancel_button;		gtk_widget_show (cancel_button);		GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT);		g_signal_connect ((gpointer) chmfilew, "delete_event",                    	G_CALLBACK (on_open_filesel_delete_event),                    	NULL);		g_signal_connect ((gpointer) ok_button, "clicked",                    	G_CALLBACK (on_open_chmfile_ok),                    	chmfile);		g_signal_connect ((gpointer) cancel_button, "clicked",                    	G_CALLBACK (on_open_chmfile_cancel),                    	NULL);		gtk_widget_show(chmfilew);		gtk_window_set_transient_for (GTK_WINDOW (chmfilew), 							GTK_WINDOW (w));	}			gtk_object_set_data (GTK_OBJECT (chmfilew), MainWindowKey, w);	gtk_window_present (GTK_WINDOW (chmfilew));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -