/* GNOME Font Installer Author: Yash Goswami website:http://gfontinstall.sourceforge.net License: General Public License version 3 (GPL v3) */ #include #include #include #include "fontlist.c" /* Callback for close button */ GtkWidget *list; enum { LIST_ITEM = 0, N_COLUMNS, COLUMN_CHECK, COLUMN_FONT, }; static void init_list(GtkWidget *list) { GtkCellRenderer *renderer; GtkTreeViewColumn *column; GtkListStore *store; renderer = gtk_cell_renderer_text_new(); column = gtk_tree_view_column_new_with_attributes("List Items", renderer, "text", LIST_ITEM, NULL); gtk_tree_view_append_column(GTK_TREE_VIEW(list), column); store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING); gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store)); g_object_unref(store); } static void add_to_list(GtkWidget *list, const gchar *str) { GtkListStore *store; GtkTreeIter iter; store = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(list))); gtk_list_store_append(store, &iter); gtk_list_store_set(store, &iter, LIST_ITEM, str, -1); } void on_button_clicked (GtkWidget *button, gpointer data) { gtk_main_quit (); } void on_add_button_clicked (GtkWidget *button, GtkWindow *window) { int copypid,cpid,num_of_files=0; char command[100],data[20]; GtkWidget *dialog; GSList *filenames; dialog = gtk_file_chooser_dialog_new ("Open Font File(s) ...", window, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,NULL); /* Allow the user to choose more than one file at a time. */ gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), TRUE); gint result = gtk_dialog_run (GTK_DIALOG (dialog)); if (result == GTK_RESPONSE_ACCEPT) { copypid=vfork(); if(copypid==0) { filenames = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (dialog)); while (filenames != NULL) { num_of_files++; //system("sudo chmod 777 /usr/share/fonts/truetype"); //give permissions for truetype folder gchar *file = (gchar*) filenames->data; g_print ("%s was selected.\n", file); sprintf(command, "gksu cp %s /usr/share/fonts/truetype/", file); /*popups a dialog asking password*/ system(command); /* This line will not be printed untill the cp command has completed. */ g_print("file copied\n"); filenames = filenames->next; } } } gtk_widget_destroy (dialog); //sprintf(data," Font(s) Installed : %d\n",num_of_files); } void on_refresh_button_clicked (GtkWidget *button, gpointer data) { //g_print("This will Refresh the Fonts List\n"); char *b; GtkListStore *store; GtkTreeIter iter; //gtk_list_store_clear(store); init_list(list); while(i<7) { b=fontlist(); add_to_list(list, b); i++; } } void on_apply_button_clicked (GtkWidget *button, gpointer data) { g_print("By clicking this fonts will be installed\n"); } void on_delete_button_clicked (GtkWidget *button, gpointer data) { g_print("The selected fonts will be removed\n"); } void on_help_button_clicked (GtkWidget *button, gpointer data) { char *cmd[] = { "firefox", "http://gfontinstall.sourceforge.net", (char *)0 }; int ret,pid=vfork(); /* vfork() resumes the parent when child terminated*/ if(pid==0) ret=execv("/usr/bin/firefox",cmd); /*execv on success does not return so to run a simultaneous process we forked*/ else g_print("This helped\n"); } void on_about_button_clicked (GtkWidget *button, gpointer data) { //g_print("This will Refresh the Fonts List\n"); //GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file("battery.png", NULL); GtkWidget *dialog = gtk_about_dialog_new(); gtk_about_dialog_set_name(GTK_ABOUT_DIALOG(dialog), "GNOME Font Installer"); gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), "0.1"); gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(dialog),"(c) Yash Goswami"); gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(dialog),"GNOME Font Installer is a simple tool for font installation."); gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(dialog),"http://gfontinstall.sourceforge.net"); //gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(dialog), pixbuf); //g_object_unref(pixbuf), pixbuf = NULL; gtk_dialog_run(GTK_DIALOG (dialog)); gtk_widget_destroy(dialog); } void button_add_clicked( gpointer data ) { int indx; /* Something silly to add to the list. 4 rows of 1 columns each */ gchar *flist[4][1]={"FreeSans","Serif","Lucida Console","Gentium"}; /* Here we do the actual adding of the text. It's done once for * each row. */ for ( indx=0 ; indx < 4 ; indx++ ) gtk_clist_append( (GtkCList *) data, flist[indx]); return; } void on_changed(GtkWidget *widget, gpointer label) { GtkTreeIter iter; GtkTreeModel *model; char *value; if (gtk_tree_selection_get_selected( GTK_TREE_SELECTION(widget), &model, &iter)) { gtk_tree_model_get(model, &iter, LIST_ITEM, &value, -1); gtk_label_set_text(GTK_LABEL(label), value); g_free(value); } } static void remove_item(GtkWidget *widget, gpointer selection) { GtkListStore *store; GtkTreeModel *model; GtkTreeIter iter; store = GTK_LIST_STORE(gtk_tree_view_get_model( GTK_TREE_VIEW (list))); model = gtk_tree_view_get_model (GTK_TREE_VIEW (list)); if (gtk_tree_model_get_iter_first(model, &iter) == FALSE) return; if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection), &model, &iter)) { gtk_list_store_remove(store, &iter); } } /*void your_callback(GtkCellRendererToggle* renderer, gchar* pathStr, gpointer data) { GtkTreeIter iter; gboolean enabled; GtkTreePath* path = gtk_tree_path_new_from_string(pathStr); gtk_tree_model_get_iter(GTK_TREE_MODEL (data), &iter, path); gtk_tree_model_get(GTK_TREE_MODEL (data), &iter, 0, &enabled, -1); enabled = !enabled; gtk_tree_store_set(GTK_TREE_STORE (data), &iter, 0, enabled, -1); }*/ int main(int argc, char *argv[]) { GtkWidget *window, *table, *label,*name, *toolbar, *scrolled_win,*scrolled_win2, *button,*buttonclose,*buttonhelp, *sample, *cancel; GtkWidget *frameclist, *framesample, *label2; GtkToolItem *add; GtkWidget *hbox1, *hbox2, *vbox; GtkCellRenderer *renderer; GtkTreeViewColumn *column; GtkTreeModel *model; char *b; // to add to list //gchar *title[1]={"Font"}; /*GtkToolItem *open;*/ GtkToolItem *delete; GtkToolItem *apply; GtkToolItem *sep; GtkToolItem *exit; GtkToolItem *refresh; GtkToolItem *about; GtkToolItem *help; GtkTooltips *tooltips; GtkTextBuffer *buffer; GtkTreeSelection *selection; /*initialze the GTK+ */ gtk_init(&argc,&argv); window=gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window),"GNOME Font Installer"); /* gtk_window_set_title()requires a GtkWindow object as it’s the first parameter, we must cast our window using the GTK_WINDOW() function.*/ gtk_container_set_border_width(GTK_CONTAINER(window),5); gtk_window_set_position(GTK_WINDOW (window),GTK_WIN_POS_CENTER); gtk_widget_set_size_request(window,560,350); gtk_window_set_resizable(GTK_WINDOW(window), FALSE); list = gtk_tree_view_new(); //TreeView gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list),TRUE); vbox = gtk_vbox_new(FALSE, 0); label2 = gtk_label_new(""); gtk_label_set_justify(GTK_LABEL(label2), GTK_JUSTIFY_CENTER); table=gtk_table_new(7,3,TRUE); sample=gtk_text_view_new(); gtk_text_view_set_editable(GTK_TEXT_VIEW(sample), FALSE); gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(sample), FALSE); /* Obtaining the buffer associated with the widget. */ buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (sample)); buttonhelp = gtk_button_new_from_stock(GTK_STOCK_HELP); buttonclose=gtk_button_new_from_stock(GTK_STOCK_CLOSE); gtk_text_buffer_set_text (buffer,"A quick brown fox jumps over the lazy dog",-1); frameclist=gtk_frame_new("Font List"); framesample=gtk_frame_new("Details"); /* Add shadow to frame */ gtk_frame_set_shadow_type(GTK_FRAME(frameclist), GTK_SHADOW_OUT); gtk_frame_set_shadow_type(GTK_FRAME(framesample), GTK_SHADOW_OUT); scrolled_win = gtk_scrolled_window_new (NULL, NULL); /* Have to add CList widget */ scrolled_win2 = gtk_scrolled_window_new (NULL, NULL); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win2), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); /* Add shadow type */ gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_ETCHED_OUT); gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win2), GTK_SHADOW_ETCHED_OUT); //vbox=gtk_vbox_new(FALSE,0); gtk_container_add (GTK_CONTAINER (scrolled_win),list); gtk_container_add (GTK_CONTAINER (scrolled_win2),sample); gtk_box_pack_start(GTK_BOX(vbox),scrolled_win2, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox),label2, TRUE, TRUE, 5); /* adding scrolled_win to frameclist */ gtk_container_add(GTK_CONTAINER(frameclist),scrolled_win); gtk_container_add(GTK_CONTAINER(framesample),vbox); /* Set border width for the widgets */ gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 10); gtk_container_set_border_width (GTK_CONTAINER (scrolled_win2), 10); /* Toolbar coding starts here */ toolbar = gtk_toolbar_new(); gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS); gtk_container_set_border_width(GTK_CONTAINER(toolbar), 2); refresh = gtk_tool_button_new_from_stock(GTK_STOCK_REFRESH); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), refresh, -1); add = gtk_tool_button_new_from_stock(GTK_STOCK_ADD); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), add, -1); /*open = gtk_tool_button_new_from_stock(GTK_STOCK_OPEN); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), open, -1);*/ delete = gtk_tool_button_new_from_stock(GTK_STOCK_DELETE); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), delete, -1); apply = gtk_tool_button_new_from_stock(GTK_STOCK_APPLY); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), apply, -1); sep = gtk_separator_tool_item_new(); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), sep, -1); exit = gtk_tool_button_new_from_stock(GTK_STOCK_QUIT); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), exit, -1); about = gtk_tool_button_new_from_stock(GTK_STOCK_ABOUT); gtk_toolbar_insert(GTK_TOOLBAR(toolbar), about, -1); help = gtk_tool_button_new_from_stock(GTK_STOCK_HELP); gtk_toolbar_insert(GTK_TOOLBAR(toolbar),help, -1); //Add tooltips to Tool Items new function previously :gtk_tool_item_set_tooltip() gtk_tool_item_set_tooltip_text(refresh,"Update the List"); gtk_tool_item_set_tooltip_text(add,"Install local font files"); gtk_tool_item_set_tooltip_text(delete,"Remove the selected fonts"); gtk_tool_item_set_tooltip_text(apply,"Install the selected fonts"); gtk_tool_item_set_tooltip_text(exit,"Exit"); gtk_tool_item_set_tooltip_text(about,"About"); gtk_tool_item_set_tooltip_text(help,"Help"); /*Attach these to parent container */ gtk_table_attach(GTK_TABLE(table),toolbar,0,3,0,1,GTK_FILL,GTK_SHRINK,0,0); //gtk_table_attach(GTK_TABLE(table),hbox1,1,2,1,2,GTK_EXPAND,GTK_SHRINK,0,0); gtk_table_attach(GTK_TABLE(table),frameclist,0,2,1,6,GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND,0,0); gtk_table_attach(GTK_TABLE(table),framesample,2,3,1,6,GTK_EXPAND | GTK_FILL, GTK_FILL | GTK_EXPAND,0,0); //gtk_table_attach(GTK_TABLE(table),label2,2,3,5,6,GTK_EXPAND,GTK_SHRINK,0,0); gtk_table_attach(GTK_TABLE(table),buttonhelp,0,1,6,7,GTK_EXPAND,GTK_SHRINK,0,0); gtk_table_attach(GTK_TABLE(table),buttonclose,2,3,6,7,GTK_EXPAND,GTK_SHRINK,0,0); /* Add five pixels of spacing between every row and column */ gtk_table_set_row_spacings(GTK_TABLE(table),15); gtk_table_set_col_spacings(GTK_TABLE(table),5); gtk_container_add(GTK_CONTAINER(window),table); gtk_widget_show_all(window); /* ---------- */ /* ----------- */ /* init_list(list); while(i<7) { b=fontlist(); add_to_list(list, b); i++; }*/ /*add_to_list(list, "Leon"); add_to_list(list, "Capote"); add_to_list(list, "Saving private Ryan"); add_to_list(list, "Der Untergang"); add_to_list(list, "Aliens"); add_to_list(list, "Leon"); add_to_list(list, "Capote"); add_to_list(list, "Saving private Ryan"); add_to_list(list, "Der Untergang");*/ selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list)); /* scatching the signals and callback functions */ //g_signal_connect (G_OBJECT(renderer), "toggled", G_CALLBACK(your_callback), NULL); g_signal_connect(selection,"changed",G_CALLBACK(on_changed), label2); g_signal_connect(G_OBJECT(window) ,"destroy",G_CALLBACK(gtk_main_quit),NULL); g_signal_connect(G_OBJECT(buttonclose), "clicked",G_CALLBACK(on_button_clicked),NULL); g_signal_connect(G_OBJECT(exit), "clicked",G_CALLBACK(on_button_clicked),NULL); g_signal_connect(G_OBJECT(add), "clicked",G_CALLBACK(on_add_button_clicked),(gpointer)window); g_signal_connect(G_OBJECT(refresh), "clicked",G_CALLBACK(on_refresh_button_clicked),NULL); g_signal_connect(G_OBJECT(about), "clicked",G_CALLBACK(on_about_button_clicked),NULL); g_signal_connect(G_OBJECT(help), "clicked",G_CALLBACK(on_help_button_clicked),NULL); g_signal_connect(G_OBJECT(apply), "clicked",G_CALLBACK(on_apply_button_clicked),NULL); //g_signal_connect(G_OBJECT(delete), "clicked",G_CALLBACK(on_delete_button_clicked),NULL); gtk_signal_connect_object(GTK_OBJECT(buttonhelp), "clicked",GTK_SIGNAL_FUNC(on_help_button_clicked),NULL); g_signal_connect(G_OBJECT(delete), "clicked", G_CALLBACK(remove_item), selection); gtk_main(); return 0; }