-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_ex08.c
75 lines (62 loc) · 2.25 KB
/
git_ex08.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <gtk/gtk.h>
static void app_activate (GApplication *app, gpointer user_data){
g_print("Please specify a filename to open.\n");
}
static void
app_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer user_data) {
GtkWidget *win;
GtkWidget *nb;
GtkWidget *lab;
GtkNotebookPage *nbp;
GtkWidget *scr;
GtkWidget *tv;
GtkTextBuffer *tb;
char *contents;
gsize length;
char *filename;
int i;
win = gtk_application_window_new(GTK_APPLICATION(app));
gtk_window_set_title(GTK_WINDOW(win), "File Viewer");
gtk_window_set_default_size(GTK_WINDOW(win), 400, 300);
gtk_window_maximize(GTK_WINDOW(win));
nb = gtk_notebook_new();
gtk_window_set_child(GTK_WINDOW(win), nb);
for (i = 0; i < n_files; i++){
if (g_file_load_contents(files[i], NULL, &contents, &length, NULL, NULL)){
scr = gtk_scrolled_window_new();
tv = gtk_text_view_new();
tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(tv), GTK_WRAP_WORD_CHAR);
gtk_text_view_set_editable(GTK_TEXT_VIEW(tv), FALSE);
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scr), tv);
gtk_text_buffer_set_text(tb, contents, length);
g_free(contents);
if ((filename = g_file_get_basename(files[i])) != NULL){
lab = gtk_label_new(filename);
g_free(filename);
}else
lab = gtk_label_new("");
gtk_notebook_append_page(GTK_NOTEBOOK(nb), scr, lab);
nbp = gtk_notebook_get_page(GTK_NOTEBOOK(nb), scr);
g_object_set(nbp, "tab-expand", TRUE, NULL);
}else if((filename = g_file_get_path(files[i])) != NULL){
g_print("No such file: %s.\n", filename);
g_free(filename);
}else
g_print("No valid file is given.\n");
}
if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(nb)) > 0)
gtk_widget_show(win);
else
gtk_window_destroy(GTK_WINDOW(win));
}
int main(int argc, char **argv){
GtkApplication *app;
int stat;
app = gtk_application_new("com.example.www", G_APPLICATION_HANDLES_OPEN);
g_signal_connect(app, "activate", G_CALLBACK(app_activate), NULL);
g_signal_connect(app, "open", G_CALLBACK(app_open), NULL);
stat = g_application_run(G_APPLICATION(app), argc, argv);
g_object_ref(app);
return stat;
}