gshortcut 程序是一个给xfce desktop environment for NIX创建桌面快捷方式的一个工具。
开发人:antan <antantan2007@yahoo.cn>
代码解析:
该程序共包括的源程序文件有:main.c interface.h interface.c support.h support.c callbacks.h callbacks.c
其中:
interface.h interface.c为界面设计的程序模块
support.c support.h 为多国语言支持的模块
main.c是主程序
callbacks.c callbacks.h为回调函数的模块
主函数
函数名称:main.c
/*
* Initial main.c file generated by Glade. Edit as required.
* Glade will not overwrite this file.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gtk/gtk.h>
#include "interface.h"
#include "support.h"
int
main (int argc, char *argv[])
{
GtkWidget *mainWindow;
#ifdef ENABLE_NLS
bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
#endif
gtk_set_locale ();
gtk_init (&argc, &argv);
add_pixmap_directory (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps");
/*
* The following code was added by Glade to create one of each component
* (except popup menus), just so that you see something after building
* the project. Delete any components that you don't want shown initially.
*/
mainWindow = create_mainWindow ();
gtk_widget_show (mainWindow);
g_signal_connect(GTK_WINDOW(mainWindow),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
gtk_main ();
return 0;
}
callbacks.h内容:
#ifndef CALLBACKS_H
#define CALLBACKS_H
#include <gtk/gtk.h>
#endif
callbacks.c内容:
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gtk/gtk.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
interface.h
/**************************************************************************************************************
*filename:interface.h
*create time:2008-01-15 by antan<antantan@linpus.com>
*description:The file description the more information about the gshortcut
*program. It can be created a ico and a new programer easily
****************************************************************************************************************/
GtkWidget *entryName;
GtkWidget *entryICON;
GtkWidget *entryHttp;
GtkWidget *entryPath;
GtkWidget *iconview1;
GtkWidget *imageButton;
char *str_button_select_path; //The string variable has to rember the ico's path.
//GtkWindow *aliasmainWindow;
GtkWidget* create_mainWindow (void);
GtkWidget* create_SelectIconWindow (void);
void info_clear(void);
void str_button_select_clicked(GtkWidget *win,gpointer data);
void button_ok_clicked(void);
void destroy_window(GtkWidget *win, gpointer data);
void display_icon(GtkWidget *button, gpointer data);
interface.c内容
/**************************************************************************************************************
*filename:interface.c
*create time:2008-01-15 by antan<antantan@linpus.com>
*description:The file description the more information about the gshortcut
*program. It can be created a ico and a new programer easily
****************************************************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
#define GLADE_HOOKUP_OBJECT(component,widget,name) /
g_object_set_data_full (G_OBJECT (component), name, /
gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)
#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) /
g_object_set_data (G_OBJECT (component), name, widget)
/***************************************************************************************************************
*function name:creat_mainWindow
*input argument:none
*output argument:none
*description:the function create the main Window about the program.
*create time:2008-01-15 by antan<antantan@linpus.com>
*******************************************************************************************************************/
GtkWidget* create_mainWindow (void)
{
GtkWidget *mainWindow;
GdkPixbuf *mainWindow_icon_pixbuf;
GtkWidget *fixed;
GtkWidget *hseparator1;
GtkWidget *imageHelp;
GtkWidget *labelHelp;
// GtkWidget *entryName;
// GtkWidget *entryICON;
GtkWidget *labelName;
GtkWidget *labelIcon;
//GtkWidget *imageButton;
GtkWidget *filechooserbutton;
GtkWidget *buttonExit;
GtkWidget *buttonClear;
GtkWidget *OK;
GtkWidget *labelHttp;
// GtkWidget *entryHttp;
GtkWidget *hseparator2;
GtkWidget *buttonSelect;
mainWindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request (mainWindow, 400, 350);
gtk_window_set_title (GTK_WINDOW (mainWindow), _("gshortcut"));
mainWindow_icon_pixbuf = create_pixbuf ("gshortcut.png");
if (mainWindow_icon_pixbuf)
{
gtk_window_set_icon (GTK_WINDOW (mainWindow), mainWindow_icon_pixbuf);
gdk_pixbuf_unref (mainWindow_icon_pixbuf);
}
fixed = gtk_fixed_new ();
gtk_widget_show (fixed);
gtk_container_add (GTK_CONTAINER (mainWindow), fixed);
gtk_widget_set_size_request (fixed, 400, 300);
hseparator1 = gtk_hseparator_new ();
gtk_widget_show (hseparator1);
gtk_fixed_put (GTK_FIXED (fixed), hseparator1, 0, 100);
gtk_widget_set_size_request (hseparator1, 400, 10);
imageHelp = create_pixmap (mainWindow, "help.png");
gtk_widget_show (imageHelp);
gtk_fixed_put (GTK_FIXED (fixed), imageHelp, 10, 10);
gtk_widget_set_size_request (imageHelp, 80, 80);
labelHelp = gtk_label_new (_(" Gshortcut Software/n can create a Icon at xfce /n desktop environment/n"));
gtk_widget_show (labelHelp);
gtk_fixed_put (GTK_FIXED (fixed), labelHelp, 112, 16);
gtk_widget_set_size_request (labelHelp, 280, 80);
entryName = gtk_entry_new ();
gtk_widget_show (entryName);
gtk_fixed_put (GTK_FIXED (fixed), entryName, 60, 120);
gtk_widget_set_size_request (entryName, 200, 25);
gtk_entry_set_invisible_char (GTK_ENTRY (entryName), 8226);
entryICON = gtk_entry_new ();
gtk_widget_show (entryICON);
gtk_fixed_put (GTK_FIXED (fixed), entryICON, 60, 160);
gtk_widget_set_size_request (entryICON, 200, 25);
gtk_entry_set_invisible_char (GTK_ENTRY (entryICON), 8226);
labelName = gtk_label_new (_("Name:"));
gtk_widget_show (labelName);
gtk_fixed_put (GTK_FIXED (fixed), labelName, 10, 120);
gtk_widget_set_size_request (labelName, 50, 25);
labelIcon = gtk_label_new (_("ICON:"));
gtk_widget_show (labelIcon);
gtk_fixed_put (GTK_FIXED (fixed), labelIcon, 10, 160);
gtk_widget_set_size_request (labelIcon, 50, 25);
gtk_entry_set_text(GTK_ENTRY(entryICON),"/usr/share/pixmaps/default.png");
buttonSelect = gtk_button_new ();
gtk_widget_show (buttonSelect);
gtk_fixed_put (GTK_FIXED (fixed), buttonSelect, 280, 130);
gtk_widget_set_size_request (buttonSelect, 80, 80);
imageButton = create_pixmap (mainWindow, "help.png");
gtk_widget_show (imageButton);
gtk_image_set_from_file(imageButton,"/usr/share/pixmaps/default.png");
gtk_container_add (GTK_CONTAINER (buttonSelect), imageButton);
g_signal_connect(GTK_BUTTON(buttonSelect),"clicked",G_CALLBACK(str_button_select_clicked ),NULL);
// gtk_button_set_image(buttonSelect,imageButton);
// gtk_fixed_put (GTK_FIXED (fixed), buttonSelect, 280, 160);
// gtk_widget_set_size_request (buttonSelect, 63, 28);
/*
filechooserbutton = gtk_file_chooser_button_new (_("select"), GTK_FILE_CHOOSER_ACTION_OPEN);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(filechooserbutton),"/usr/share/pixmaps");
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filechooserbutton), "/usr/share/pixmaps/gshortcut/default.png");
gtk_widget_show (filechooserbutton);
gtk_fixed_put (GTK_FIXED (fixed), filechooserbutton, 280, 160);
gtk_widget_set_size_request (filechooserbutton, 63, 28);
g_signal_connect(GTK_FILE_CHOOSER_BUTTON(filechooserbutton),"selection_changed",G_CALLBACK(str_button_select_clicked),NULL);
*/
buttonExit = gtk_button_new_with_mnemonic (_("exit"));
gtk_widget_show (buttonExit);
gtk_fixed_put (GTK_FIXED (fixed), buttonExit, 328, 288);
gtk_widget_set_size_request (buttonExit, 66, 29);
g_signal_connect(GTK_BUTTON(buttonExit),"clicked",G_CALLBACK(gtk_main_quit),NULL);
buttonClear = gtk_button_new_with_mnemonic (_("clear"));
gtk_widget_show (buttonClear);
gtk_fixed_put (GTK_FIXED (fixed), buttonClear, 240, 288);
gtk_widget_set_size_request (buttonClear, 66, 29);
g_signal_connect(GTK_BUTTON(buttonClear),"clicked",G_CALLBACK(info_clear),NULL);
OK = gtk_button_new_with_mnemonic (_("OK"));
gtk_widget_show (OK);
gtk_fixed_put (GTK_FIXED (fixed), OK, 160, 288);
gtk_widget_set_size_request (OK, 66, 29);
g_signal_connect(GTK_BUTTON(OK),"clicked",G_CALLBACK(button_ok_clicked),NULL);
labelHttp = gtk_label_new (_("Http://"));
gtk_widget_show (labelHttp);
gtk_fixed_put (GTK_FIXED (fixed), labelHttp, 10, 210);
gtk_widget_set_size_request (labelHttp, 50, 25);
entryHttp = gtk_entry_new ();
gtk_widget_show (entryHttp);
gtk_fixed_put (GTK_FIXED (fixed), entryHttp, 64, 208);
gtk_widget_set_size_request (entryHttp, 250, 25);
gtk_entry_set_invisible_char (GTK_ENTRY (entryHttp), 8226);
hseparator2 = gtk_hseparator_new ();
gtk_widget_show (hseparator2);
gtk_fixed_put (GTK_FIXED (fixed), hseparator2, 0, 264);
gtk_widget_set_size_request (hseparator2, 400, 10);
/* Store pointers to all widgets, for use by lookup_widget(). */
GLADE_HOOKUP_OBJECT_NO_REF (mainWindow, mainWindow, "mainWindow");
GLADE_HOOKUP_OBJECT (mainWindow, fixed, "fixed");
GLADE_HOOKUP_OBJECT (mainWindow, hseparator1, "hseparator1");
GLADE_HOOKUP_OBJECT (mainWindow, imageHelp, "imageHelp");
GLADE_HOOKUP_OBJECT (mainWindow, labelHelp, "labelHelp");
GLADE_HOOKUP_OBJECT (mainWindow, entryName, "entryName");
GLADE_HOOKUP_OBJECT (mainWindow, entryICON, "entryICON");
GLADE_HOOKUP_OBJECT (mainWindow, labelName, "labelName");
GLADE_HOOKUP_OBJECT (mainWindow, labelIcon, "labelIcon");
GLADE_HOOKUP_OBJECT (mainWindow, filechooserbutton, "filechooserbutton");
GLADE_HOOKUP_OBJECT (mainWindow, buttonExit, "buttonExit");
GLADE_HOOKUP_OBJECT (mainWindow, buttonClear, "buttonClear");
GLADE_HOOKUP_OBJECT (mainWindow, OK, "OK");
GLADE_HOOKUP_OBJECT (mainWindow, labelHttp, "labelHttp");
GLADE_HOOKUP_OBJECT (mainWindow, entryHttp, "entryHttp");
GLADE_HOOKUP_OBJECT (mainWindow, hseparator2, "hseparator2");
return mainWindow;
}
/**************************************************************************************************************************************
*function name:create_SelectIconWindow
*return value:select Icon Window
****************************************************************************************************************************************/
GtkWidget* create_SelectIconWindow (void)
{
GtkWidget *SelectIconWindow;
GtkWidget *fixed;
//GtkWidget *entryPath;
GtkWidget *buttonView;
GtkWidget *hseparator1;
GtkWidget *scrolledwindow1;
GtkWidget *viewport1;
GtkWidget *fixed3;
GtkTreeModel *model;
//GtkTreeModel *file_store_ico;
GtkWidget *iconview1;
SelectIconWindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (SelectIconWindow), _("SelectIconWindow"));
fixed = gtk_fixed_new ();
gtk_widget_show (fixed);
gtk_container_add (GTK_CONTAINER (SelectIconWindow), fixed);
gtk_widget_set_size_request (fixed, 400, 400);
entryPath = gtk_entry_new ();
gtk_widget_show (entryPath);
gtk_fixed_put (GTK_FIXED (fixed), entryPath, 10, 10);
gtk_widget_set_size_request (entryPath, 280, 25);
gtk_entry_set_invisible_char (GTK_ENTRY (entryPath), 8226);
gtk_entry_set_text(GTK_ENTRY(entryPath),"/usr/share/pixmaps");
buttonView = gtk_button_new_with_mnemonic (_("View"));
gtk_widget_show (buttonView);
gtk_fixed_put (GTK_FIXED (fixed), buttonView, 300, 10);
gtk_widget_set_size_request (buttonView, 80, 25);
hseparator1 = gtk_hseparator_new ();
gtk_widget_show (hseparator1);
gtk_fixed_put (GTK_FIXED (fixed), hseparator1, 0, 40);
gtk_widget_set_size_request (hseparator1, 400, 10);
scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_show (scrolledwindow1);
gtk_fixed_put (GTK_FIXED (fixed), scrolledwindow1, 8, 48);
gtk_widget_set_size_request (scrolledwindow1, 380, 340);
viewport1 = gtk_viewport_new (NULL, NULL);
gtk_widget_show (viewport1);
gtk_container_add (GTK_CONTAINER (scrolledwindow1), viewport1);
fixed3 = gtk_fixed_new ();
gtk_widget_show (fixed3);
gtk_container_add (GTK_CONTAINER (viewport1), fixed3);
iconview1 = gtk_icon_view_new_with_model(model);
gtk_widget_show (iconview1);
gtk_fixed_put (GTK_FIXED (fixed3), iconview1, 0, 0);
gtk_widget_set_size_request (iconview1, 380, 340);
// gtk_icon_view_selected_foreach(GTK_ICON_VIEW (iconview1),);
gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (iconview1), GTK_SELECTION_MULTIPLE);
gtk_icon_view_set_item_width(GTK_ICON_VIEW (iconview1),10);
gtk_icon_view_create_drag_icon(GTK_ICON_VIEW (iconview1),"/usr/share/pixmaps");
g_signal_connect(GTK_BUTTON(buttonView),"clicked",G_CALLBACK(display_icon),iconview1);
/* Store pointers to all widgets, for use by lookup_widget(). */
GLADE_HOOKUP_OBJECT_NO_REF (SelectIconWindow, SelectIconWindow, "SelectIconWindow");
GLADE_HOOKUP_OBJECT (SelectIconWindow, fixed, "fixed");
GLADE_HOOKUP_OBJECT (SelectIconWindow, entryPath, "entryPath");
GLADE_HOOKUP_OBJECT (SelectIconWindow, buttonView, "buttonView");
GLADE_HOOKUP_OBJECT (SelectIconWindow, hseparator1, "hseparator1");
GLADE_HOOKUP_OBJECT (SelectIconWindow, scrolledwindow1, "scrolledwindow1");
GLADE_HOOKUP_OBJECT (SelectIconWindow, viewport1, "viewport1");
GLADE_HOOKUP_OBJECT (SelectIconWindow, fixed3, "fixed3");
GLADE_HOOKUP_OBJECT (SelectIconWindow, iconview1, "iconview1");
return SelectIconWindow;
}
/***********************************************************************************************************
*function name:info_clear
*input argument:none
*output argument:none
*description:The program's function clears the main window's contrals and other inforamtion
*create time:2008-01-15 by antan<antantan@linpus.com>
*************************************************************************************************************/
void info_clear(void)
{
gtk_entry_set_text(GTK_ENTRY(entryName),"");
gtk_entry_set_text(GTK_ENTRY(entryICON),"/usr/share/pixmaps/default.png");
gtk_image_set_from_file(imageButton,"/usr/share/pixmaps/default.png");
gtk_entry_set_text(GTK_ENTRY(entryHttp),"");
}
/********************************************************************************************************************
*function name:str_button_select_clicked
*input argument:none
*output argument:none
*description:the function can access the ico file's name and path.
*create time:2008-01-16 antan<antantan@linpus.com>
************************************************************************************************************************/
void str_button_select_clicked(GtkWidget* win, gpointer data)
{
/*
GtkWidget *winSelect = create_SelectIconWindow();
gtk_widget_show(winSelect);
g_signal_connect(GTK_WINDOW(winSelect),"delete_event",G_CALLBACK(destroy_window),NULL);
*/
char *filename;
GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Select ICONS Window",
win,
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),"/usr/share/pixmaps");
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
{
//char *filename;
//filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
gtk_entry_set_text(GTK_ENTRY(entryICON),filename);
gtk_image_set_from_file(imageButton,filename);
//open_file (filename);
g_free (filename);
}
gtk_widget_destroy (dialog);
}
/**********************************************************************************************************************
*function name:button_ok_clicked
*input argument:none
*output argument:none
*description:the function can be executed creating a shortcut for a new program,
*It can been used easily.
*create time:2008-01-16 by antan<antantan@linpus.com>
************************************************************************************************************************/
void button_ok_clicked(void)
{
FILE *fd_desktop; //*.desktop file
FILE *fd_dmrc_file; //
char *version_str=NULL; //version string
char *encoding_str=NULL; //encode_str
char *icopath=NULL; //save the ico path
char *name=NULL; //save the app_name
char *name_zh_CN=NULL;
char *name_zh_TW=NULL;
char *genericName=NULL;
char *genericName_zh_CN=NULL;
char *genericName_zh_TW=NULL;
char *http_str=NULL; //save http path
char *comment_str=NULL;
char *exec_str=NULL; //firefox execute path
char *ico_str=NULL; //icon file save path
char *terminal_str=NULL; //terminal string
char *type_str=NULL; //type string
char *start_up_wm_class=NULL; //start up wm class
char *categories=NULL; //categories
char *x_desktop_file_install_version = NULL; //x desktop file install version
char *fd_desktop_default_name=NULL; //for testing.
//char *fd_desktop_default_name="/usr/share/applications/edesktop/"; //desktop path and name
char *tempfile=""; /*temp file name variable*/
char *tempstr=""; /*temp string variable*/
//g_printf("I have entry the create document/n");
/* tempstr = gtk_entry_get_text(GTK_ENTRY(entry_name));
g_printf(tempstr);
g_printf("testing0...../n");
*/
version_str = malloc(sizeof(char)*100);
memset(version_str,0x00,sizeof(char)*100);
strcat(version_str,"Version = 1.0/n");
encoding_str = malloc(sizeof(char)*100);
memset(encoding_str,0x00,sizeof(char)*100);
strcat(encoding_str,"Encoding=UTF-8/n");
name = malloc(sizeof(char)*100);
memset(name,0x00,sizeof(char)*100);
strcat(name,"Name=");
strcat(name,gtk_entry_get_text(GTK_ENTRY(entryName))); //get the new app name
//g_printf("testing1...../n");
strcat(name,"/n");
name_zh_CN = malloc(sizeof(char)*100);
memset(name_zh_CN,0x00,sizeof(char)*100);
strcat(name_zh_CN,"Name[zh_CN]=");
strcat(name_zh_CN,gtk_entry_get_text(GTK_ENTRY(entryName)));
strcat(name_zh_CN,"/n");
name_zh_TW = malloc(sizeof(char)*100);
memset(name_zh_TW,0x00,sizeof(char)*100);
strcat(name_zh_TW,"Name[zh_TW]=");
strcat(name_zh_TW,gtk_entry_get_text(GTK_ENTRY(entryName)));
strcat(name_zh_TW,"/n");
genericName = malloc(sizeof(char)*100);
memset(genericName,0x00,sizeof(char)*100);
strcat(genericName,"GenericName=");
strcat(genericName,gtk_entry_get_text(GTK_ENTRY(entryName)));
strcat(genericName,"/n");
genericName_zh_CN = malloc(sizeof(char)*100);
memset(genericName_zh_CN,0x00,sizeof(char)*100);
strcat(genericName_zh_CN,"GenericName[zh_CN]=");
strcat(genericName_zh_CN,gtk_entry_get_text(GTK_ENTRY(entryName)));
strcat(genericName_zh_CN,"/n");
genericName_zh_TW = malloc(sizeof(char)*100);
memset(genericName_zh_TW,0x00,sizeof(char)*100);
strcat(genericName_zh_TW,"GenericName[zh_TW]=");
strcat(genericName_zh_TW,gtk_entry_get_text(GTK_ENTRY(entryName)));
strcat(genericName_zh_TW,"/n");
comment_str = malloc(sizeof(char)*1000);
memset(comment_str,0x00,sizeof(char)*1000);
strcat(comment_str,"Comment= Internet shortcut/n");
exec_str = malloc(sizeof(char)*1000);
memset(exec_str,0x00,sizeof(char)*1000);
strcat(exec_str,"Exec=firefox http://");
strcat(exec_str,gtk_entry_get_text(GTK_ENTRY(entryHttp)));
strcat(exec_str,"/n");
ico_str = malloc(sizeof(char)*1000);
memset(ico_str,0x00,sizeof(char)*1000);
strcat(ico_str,"Icon=");
strcat(ico_str,gtk_entry_get_text(GTK_ENTRY(entryICON)));
strcat(ico_str,"/n");
terminal_str = malloc(sizeof(char)*100);
memset(terminal_str,0x00,sizeof(char)*100);
strcat(terminal_str,"Terminal=false/n");
type_str = malloc(sizeof(char)*100);
memset(type_str,0x00,sizeof(type_str));
strcat(type_str,"Type=Application/n");
start_up_wm_class = malloc(sizeof(char)*100);
memset(start_up_wm_class,0x00,sizeof(start_up_wm_class));
strcat(start_up_wm_class,"StartupWMClass=Firefox-bin/n");
categories = malloc(sizeof(char)*100);
memset(categories,0x00,sizeof(categories));
strcat(categories,"Categories=Network;WebBrowser;/n");
x_desktop_file_install_version = malloc(sizeof(char)*100);
memset(x_desktop_file_install_version,0x00,sizeof(x_desktop_file_install_version));
strcat(x_desktop_file_install_version,"X-Desktop-File-Install-Version=0.10/n");
fd_desktop_default_name = malloc(sizeof(char)*1000);
memset(fd_desktop_default_name,0x00,sizeof(char)*1000);
strcat(fd_desktop_default_name,"/home/linpus/.config/");
strcat(fd_desktop_default_name,gtk_entry_get_text(GTK_ENTRY(entryName)));
//strcat(fd_desktop_default_name,"/root/Desktop/test.desktop");
strcat(fd_desktop_default_name,".desktop");
// fd_desktop_default_name[sizeof(fd_desktop_default_name)] = 0x00;
g_printf("testing...../n");
//fd_desktop = malloc(sizeof(FILE*));
//memset(fd_desktop_default_name,0x00,sizeof(fd_desktop_default_name));
//strcat(fd_desktop_default_name,"/root/Desktop/test.desktop");
if ((fd_desktop = fopen(fd_desktop_default_name,"w+")) == NULL)
{
g_printf("open file error/n");
}
fputs("[Desktop Entry]/n",fd_desktop);
fputs(version_str,fd_desktop);
fputs(encoding_str,fd_desktop);
fputs(name,fd_desktop);
fputs(name_zh_CN,fd_desktop);
fputs(name_zh_TW,fd_desktop);
fputs(genericName,fd_desktop);
fputs(genericName_zh_CN,fd_desktop);
fputs(genericName_zh_TW,fd_desktop);
fputs(comment_str,fd_desktop);
fputs(exec_str,fd_desktop);
fputs(ico_str,fd_desktop);
fputs(terminal_str,fd_desktop);
fputs(type_str,fd_desktop);
fputs(start_up_wm_class,fd_desktop);
fputs(x_desktop_file_install_version,fd_desktop);
fputs(categories,fd_desktop);
fclose(fd_desktop);
tempstr = malloc(sizeof(char)*100);
memset(tempstr,0x00,sizeof(char)*100);
strcat(tempstr,"xfdesktop-simple --add_app=");
strcat(tempstr,fd_desktop_default_name);
system(tempstr);
free(tempstr);
free(version_str);
free(encoding_str);
free(name);
free(name_zh_CN);
free(name_zh_TW);
free(genericName);
free(genericName_zh_CN);
free(genericName_zh_TW);
free(comment_str);
free(exec_str);
free(ico_str);
free(terminal_str);
free(type_str);
free(start_up_wm_class);
free(x_desktop_file_install_version);
free(categories);
free(fd_desktop_default_name);
//free(fd_desktop);
gtk_entry_set_text(GTK_ENTRY(entryName),"");
gtk_entry_set_text(GTK_ENTRY(entryICON),"/usr/share/pixmaps/default.png");
gtk_image_set_from_file(imageButton,"/usr/share/pixmaps/default.png");
gtk_entry_set_text(GTK_ENTRY(entryHttp),"");
}
/***************************************************************************************************************************************/
void destroy_window(GtkWidget *win, gpointer data)
{
gtk_widget_destroy(win);
}
/*****************************************************************************************************************************************/
void display_icon(GtkWidget *button, gpointer data)
{
GtkTreeModel *model = gtk_icon_view_get_model(data);
//GtkTreeIter it;
char *file_name;
char *file_path = "/usr/share/pixmaps";
const char *dir_path=gtk_entry_get_text(GTK_ENTRY(entryPath));
GtkTreePath *path;
GtkTreeIter iter;
GtkTreeIter parent_iter;
gtk_tree_model_get_iter_from_string(model,&iter,"/usr/share/pixmaps");
// gtk_tree_model_get_iter( model, &iter, file_path );
/* get the iterator from a string */
//gtk_tree_model_get_iter_from_string (model, &iter, "3:2:5");
/* get the iterator from a path */
path = gtk_tree_path_new_from_string ("3:2:5");
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_path_free (path);
/* walk the tree to find the iterator */
gtk_tree_model_iter_nth_child (model, &iter, NULL, 3);
parent_iter = iter;
gtk_tree_model_iter_nth_child (model, &iter, &parent_iter, 2);
parent_iter = iter;
gtk_tree_model_iter_nth_child (model, &iter, &parent_iter, 5);
//gtk_icons_view_set_model(GTK_ICON_VIEW(data),list);
}
support.h内容
#ifndef SUPPORT_H
#define SUPPORT_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gtk/gtk.h>
/*
* Standard gettext macros.
*/
#ifdef ENABLE_NLS
# include <libintl.h>
# undef _
# define _(String) dgettext (PACKAGE, String)
# define Q_(String) g_strip_context ((String), gettext (String))
# ifdef gettext_noop
# define N_(String) gettext_noop (String)
# else
# define N_(String) (String)
# endif
#else
# define textdomain(String) (String)
# define gettext(String) (String)
# define dgettext(Domain,Message) (Message)
# define dcgettext(Domain,Message,Type) (Message)
# define bindtextdomain(Domain,Directory) (Domain)
# define _(String) (String)
# define Q_(String) g_strip_context ((String), (String))
# define N_(String) (String)
#endif
/*
* Public Functions.
*/
/*
* This function returns a widget in a component created by Glade.
* Call it with the toplevel widget in the component (i.e. a window/dialog),
* or alternatively any widget in the component, and the name of the widget
* you want returned.
*/
GtkWidget* lookup_widget (GtkWidget *widget,
const gchar *widget_name);
/* Use this function to set the directory containing installed pixmaps. */
void add_pixmap_directory (const gchar *directory);
/*
* Private Functions.
*/
/* This is used to create the pixmaps used in the interface. */
GtkWidget* create_pixmap (GtkWidget *widget,
const gchar *filename);
/* This is used to create the pixbufs used in the interface. */
GdkPixbuf* create_pixbuf (const gchar *filename);
/* This is used to set ATK action descriptions. */
void glade_set_atk_action_description (AtkAction *action,
const gchar *action_name,
const gchar *description);
#endif
support.c
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include "support.h"
GtkWidget*
lookup_widget (GtkWidget *widget,
const gchar *widget_name)
{
GtkWidget *parent, *found_widget;
for (;;)
{
if (GTK_IS_MENU (widget))
parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
else
parent = widget->parent;
if (!parent)
parent = (GtkWidget*) g_object_get_data (G_OBJECT (widget), "GladeParentKey");
if (parent == NULL)
break;
widget = parent;
}
found_widget = (GtkWidget*) g_object_get_data (G_OBJECT (widget),
widget_name);
if (!found_widget)
g_warning ("Widget not found: %s", widget_name);
return found_widget;
}
static GList *pixmaps_directories = NULL;
/* Use this function to set the directory containing installed pixmaps. */
void
add_pixmap_directory (const gchar *directory)
{
pixmaps_directories = g_list_prepend (pixmaps_directories,
g_strdup (directory));
}
/* This is an internally used function to find pixmap files. */
static gchar*
find_pixmap_file (const gchar *filename)
{
GList *elem;
/* We step through each of the pixmaps directory to find it. */
elem = pixmaps_directories;
while (elem)
{
gchar *pathname = g_strdup_printf ("%s%s%s", (gchar*)elem->data,
G_DIR_SEPARATOR_S, filename);
if (g_file_test (pathname, G_FILE_TEST_EXISTS))
return pathname;
g_free (pathname);
elem = elem->next;
}
return NULL;
}
/* This is an internally used function to create pixmaps. */
GtkWidget*
create_pixmap (GtkWidget *widget,
const gchar *filename)
{
gchar *pathname = NULL;
GtkWidget *pixmap;
if (!filename || !filename[0])
return gtk_image_new ();
pathname = find_pixmap_file (filename);
if (!pathname)
{
g_warning (_("Couldn't find pixmap file: %s"), filename);
return gtk_image_new ();
}
pixmap = gtk_image_new_from_file (pathname);
g_free (pathname);
return pixmap;
}
/* This is an internally used function to create pixmaps. */
GdkPixbuf*
create_pixbuf (const gchar *filename)
{
gchar *pathname = NULL;
GdkPixbuf *pixbuf;
GError *error = NULL;
if (!filename || !filename[0])
return NULL;
pathname = find_pixmap_file (filename);
if (!pathname)
{
g_warning (_("Couldn't find pixmap file: %s"), filename);
return NULL;
}
pixbuf = gdk_pixbuf_new_from_file (pathname, &error);
if (!pixbuf)
{
fprintf (stderr, "Failed to load pixbuf file: %s: %s/n",
pathname, error->message);
g_error_free (error);
}
g_free (pathname);
return pixbuf;
}
/* This is used to set ATK action descriptions. */
void
glade_set_atk_action_description (AtkAction *action,
const gchar *action_name,
const gchar *description)
{
gint n_actions, i;
n_actions = atk_action_get_n_actions (action);
for (i = 0; i < n_actions; i++)
{
if (!strcmp (atk_action_get_name (action, i), action_name))
atk_action_set_description (action, i, description);
}
}
以上程序是最近写的一个小APP,希望对有帮助的人一个帮助
开发人:antan <antantan2007@yahoo.cn>
代码解析:
该程序共包括的源程序文件有:main.c interface.h interface.c support.h support.c callbacks.h callbacks.c
其中:
interface.h interface.c为界面设计的程序模块
support.c support.h 为多国语言支持的模块
main.c是主程序
callbacks.c callbacks.h为回调函数的模块
主函数
函数名称:main.c
/*
* Initial main.c file generated by Glade. Edit as required.
* Glade will not overwrite this file.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gtk/gtk.h>
#include "interface.h"
#include "support.h"
int
main (int argc, char *argv[])
{
GtkWidget *mainWindow;
#ifdef ENABLE_NLS
bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
#endif
gtk_set_locale ();
gtk_init (&argc, &argv);
add_pixmap_directory (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps");
/*
* The following code was added by Glade to create one of each component
* (except popup menus), just so that you see something after building
* the project. Delete any components that you don't want shown initially.
*/
mainWindow = create_mainWindow ();
gtk_widget_show (mainWindow);
g_signal_connect(GTK_WINDOW(mainWindow),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
gtk_main ();
return 0;
}
callbacks.h内容:
#ifndef CALLBACKS_H
#define CALLBACKS_H
#include <gtk/gtk.h>
#endif
callbacks.c内容:
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gtk/gtk.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
interface.h
/**************************************************************************************************************
*filename:interface.h
*create time:2008-01-15 by antan<antantan@linpus.com>
*description:The file description the more information about the gshortcut
*program. It can be created a ico and a new programer easily
****************************************************************************************************************/
GtkWidget *entryName;
GtkWidget *entryICON;
GtkWidget *entryHttp;
GtkWidget *entryPath;
GtkWidget *iconview1;
GtkWidget *imageButton;
char *str_button_select_path; //The string variable has to rember the ico's path.
//GtkWindow *aliasmainWindow;
GtkWidget* create_mainWindow (void);
GtkWidget* create_SelectIconWindow (void);
void info_clear(void);
void str_button_select_clicked(GtkWidget *win,gpointer data);
void button_ok_clicked(void);
void destroy_window(GtkWidget *win, gpointer data);
void display_icon(GtkWidget *button, gpointer data);
interface.c内容
/**************************************************************************************************************
*filename:interface.c
*create time:2008-01-15 by antan<antantan@linpus.com>
*description:The file description the more information about the gshortcut
*program. It can be created a ico and a new programer easily
****************************************************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
#define GLADE_HOOKUP_OBJECT(component,widget,name) /
g_object_set_data_full (G_OBJECT (component), name, /
gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)
#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) /
g_object_set_data (G_OBJECT (component), name, widget)
/***************************************************************************************************************
*function name:creat_mainWindow
*input argument:none
*output argument:none
*description:the function create the main Window about the program.
*create time:2008-01-15 by antan<antantan@linpus.com>
*******************************************************************************************************************/
GtkWidget* create_mainWindow (void)
{
GtkWidget *mainWindow;
GdkPixbuf *mainWindow_icon_pixbuf;
GtkWidget *fixed;
GtkWidget *hseparator1;
GtkWidget *imageHelp;
GtkWidget *labelHelp;
// GtkWidget *entryName;
// GtkWidget *entryICON;
GtkWidget *labelName;
GtkWidget *labelIcon;
//GtkWidget *imageButton;
GtkWidget *filechooserbutton;
GtkWidget *buttonExit;
GtkWidget *buttonClear;
GtkWidget *OK;
GtkWidget *labelHttp;
// GtkWidget *entryHttp;
GtkWidget *hseparator2;
GtkWidget *buttonSelect;
mainWindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request (mainWindow, 400, 350);
gtk_window_set_title (GTK_WINDOW (mainWindow), _("gshortcut"));
mainWindow_icon_pixbuf = create_pixbuf ("gshortcut.png");
if (mainWindow_icon_pixbuf)
{
gtk_window_set_icon (GTK_WINDOW (mainWindow), mainWindow_icon_pixbuf);
gdk_pixbuf_unref (mainWindow_icon_pixbuf);
}
fixed = gtk_fixed_new ();
gtk_widget_show (fixed);
gtk_container_add (GTK_CONTAINER (mainWindow), fixed);
gtk_widget_set_size_request (fixed, 400, 300);
hseparator1 = gtk_hseparator_new ();
gtk_widget_show (hseparator1);
gtk_fixed_put (GTK_FIXED (fixed), hseparator1, 0, 100);
gtk_widget_set_size_request (hseparator1, 400, 10);
imageHelp = create_pixmap (mainWindow, "help.png");
gtk_widget_show (imageHelp);
gtk_fixed_put (GTK_FIXED (fixed), imageHelp, 10, 10);
gtk_widget_set_size_request (imageHelp, 80, 80);
labelHelp = gtk_label_new (_(" Gshortcut Software/n can create a Icon at xfce /n desktop environment/n"));
gtk_widget_show (labelHelp);
gtk_fixed_put (GTK_FIXED (fixed), labelHelp, 112, 16);
gtk_widget_set_size_request (labelHelp, 280, 80);
entryName = gtk_entry_new ();
gtk_widget_show (entryName);
gtk_fixed_put (GTK_FIXED (fixed), entryName, 60, 120);
gtk_widget_set_size_request (entryName, 200, 25);
gtk_entry_set_invisible_char (GTK_ENTRY (entryName), 8226);
entryICON = gtk_entry_new ();
gtk_widget_show (entryICON);
gtk_fixed_put (GTK_FIXED (fixed), entryICON, 60, 160);
gtk_widget_set_size_request (entryICON, 200, 25);
gtk_entry_set_invisible_char (GTK_ENTRY (entryICON), 8226);
labelName = gtk_label_new (_("Name:"));
gtk_widget_show (labelName);
gtk_fixed_put (GTK_FIXED (fixed), labelName, 10, 120);
gtk_widget_set_size_request (labelName, 50, 25);
labelIcon = gtk_label_new (_("ICON:"));
gtk_widget_show (labelIcon);
gtk_fixed_put (GTK_FIXED (fixed), labelIcon, 10, 160);
gtk_widget_set_size_request (labelIcon, 50, 25);
gtk_entry_set_text(GTK_ENTRY(entryICON),"/usr/share/pixmaps/default.png");
buttonSelect = gtk_button_new ();
gtk_widget_show (buttonSelect);
gtk_fixed_put (GTK_FIXED (fixed), buttonSelect, 280, 130);
gtk_widget_set_size_request (buttonSelect, 80, 80);
imageButton = create_pixmap (mainWindow, "help.png");
gtk_widget_show (imageButton);
gtk_image_set_from_file(imageButton,"/usr/share/pixmaps/default.png");
gtk_container_add (GTK_CONTAINER (buttonSelect), imageButton);
g_signal_connect(GTK_BUTTON(buttonSelect),"clicked",G_CALLBACK(str_button_select_clicked ),NULL);
// gtk_button_set_image(buttonSelect,imageButton);
// gtk_fixed_put (GTK_FIXED (fixed), buttonSelect, 280, 160);
// gtk_widget_set_size_request (buttonSelect, 63, 28);
/*
filechooserbutton = gtk_file_chooser_button_new (_("select"), GTK_FILE_CHOOSER_ACTION_OPEN);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(filechooserbutton),"/usr/share/pixmaps");
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filechooserbutton), "/usr/share/pixmaps/gshortcut/default.png");
gtk_widget_show (filechooserbutton);
gtk_fixed_put (GTK_FIXED (fixed), filechooserbutton, 280, 160);
gtk_widget_set_size_request (filechooserbutton, 63, 28);
g_signal_connect(GTK_FILE_CHOOSER_BUTTON(filechooserbutton),"selection_changed",G_CALLBACK(str_button_select_clicked),NULL);
*/
buttonExit = gtk_button_new_with_mnemonic (_("exit"));
gtk_widget_show (buttonExit);
gtk_fixed_put (GTK_FIXED (fixed), buttonExit, 328, 288);
gtk_widget_set_size_request (buttonExit, 66, 29);
g_signal_connect(GTK_BUTTON(buttonExit),"clicked",G_CALLBACK(gtk_main_quit),NULL);
buttonClear = gtk_button_new_with_mnemonic (_("clear"));
gtk_widget_show (buttonClear);
gtk_fixed_put (GTK_FIXED (fixed), buttonClear, 240, 288);
gtk_widget_set_size_request (buttonClear, 66, 29);
g_signal_connect(GTK_BUTTON(buttonClear),"clicked",G_CALLBACK(info_clear),NULL);
OK = gtk_button_new_with_mnemonic (_("OK"));
gtk_widget_show (OK);
gtk_fixed_put (GTK_FIXED (fixed), OK, 160, 288);
gtk_widget_set_size_request (OK, 66, 29);
g_signal_connect(GTK_BUTTON(OK),"clicked",G_CALLBACK(button_ok_clicked),NULL);
labelHttp = gtk_label_new (_("Http://"));
gtk_widget_show (labelHttp);
gtk_fixed_put (GTK_FIXED (fixed), labelHttp, 10, 210);
gtk_widget_set_size_request (labelHttp, 50, 25);
entryHttp = gtk_entry_new ();
gtk_widget_show (entryHttp);
gtk_fixed_put (GTK_FIXED (fixed), entryHttp, 64, 208);
gtk_widget_set_size_request (entryHttp, 250, 25);
gtk_entry_set_invisible_char (GTK_ENTRY (entryHttp), 8226);
hseparator2 = gtk_hseparator_new ();
gtk_widget_show (hseparator2);
gtk_fixed_put (GTK_FIXED (fixed), hseparator2, 0, 264);
gtk_widget_set_size_request (hseparator2, 400, 10);
/* Store pointers to all widgets, for use by lookup_widget(). */
GLADE_HOOKUP_OBJECT_NO_REF (mainWindow, mainWindow, "mainWindow");
GLADE_HOOKUP_OBJECT (mainWindow, fixed, "fixed");
GLADE_HOOKUP_OBJECT (mainWindow, hseparator1, "hseparator1");
GLADE_HOOKUP_OBJECT (mainWindow, imageHelp, "imageHelp");
GLADE_HOOKUP_OBJECT (mainWindow, labelHelp, "labelHelp");
GLADE_HOOKUP_OBJECT (mainWindow, entryName, "entryName");
GLADE_HOOKUP_OBJECT (mainWindow, entryICON, "entryICON");
GLADE_HOOKUP_OBJECT (mainWindow, labelName, "labelName");
GLADE_HOOKUP_OBJECT (mainWindow, labelIcon, "labelIcon");
GLADE_HOOKUP_OBJECT (mainWindow, filechooserbutton, "filechooserbutton");
GLADE_HOOKUP_OBJECT (mainWindow, buttonExit, "buttonExit");
GLADE_HOOKUP_OBJECT (mainWindow, buttonClear, "buttonClear");
GLADE_HOOKUP_OBJECT (mainWindow, OK, "OK");
GLADE_HOOKUP_OBJECT (mainWindow, labelHttp, "labelHttp");
GLADE_HOOKUP_OBJECT (mainWindow, entryHttp, "entryHttp");
GLADE_HOOKUP_OBJECT (mainWindow, hseparator2, "hseparator2");
return mainWindow;
}
/**************************************************************************************************************************************
*function name:create_SelectIconWindow
*return value:select Icon Window
****************************************************************************************************************************************/
GtkWidget* create_SelectIconWindow (void)
{
GtkWidget *SelectIconWindow;
GtkWidget *fixed;
//GtkWidget *entryPath;
GtkWidget *buttonView;
GtkWidget *hseparator1;
GtkWidget *scrolledwindow1;
GtkWidget *viewport1;
GtkWidget *fixed3;
GtkTreeModel *model;
//GtkTreeModel *file_store_ico;
GtkWidget *iconview1;
SelectIconWindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (SelectIconWindow), _("SelectIconWindow"));
fixed = gtk_fixed_new ();
gtk_widget_show (fixed);
gtk_container_add (GTK_CONTAINER (SelectIconWindow), fixed);
gtk_widget_set_size_request (fixed, 400, 400);
entryPath = gtk_entry_new ();
gtk_widget_show (entryPath);
gtk_fixed_put (GTK_FIXED (fixed), entryPath, 10, 10);
gtk_widget_set_size_request (entryPath, 280, 25);
gtk_entry_set_invisible_char (GTK_ENTRY (entryPath), 8226);
gtk_entry_set_text(GTK_ENTRY(entryPath),"/usr/share/pixmaps");
buttonView = gtk_button_new_with_mnemonic (_("View"));
gtk_widget_show (buttonView);
gtk_fixed_put (GTK_FIXED (fixed), buttonView, 300, 10);
gtk_widget_set_size_request (buttonView, 80, 25);
hseparator1 = gtk_hseparator_new ();
gtk_widget_show (hseparator1);
gtk_fixed_put (GTK_FIXED (fixed), hseparator1, 0, 40);
gtk_widget_set_size_request (hseparator1, 400, 10);
scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_show (scrolledwindow1);
gtk_fixed_put (GTK_FIXED (fixed), scrolledwindow1, 8, 48);
gtk_widget_set_size_request (scrolledwindow1, 380, 340);
viewport1 = gtk_viewport_new (NULL, NULL);
gtk_widget_show (viewport1);
gtk_container_add (GTK_CONTAINER (scrolledwindow1), viewport1);
fixed3 = gtk_fixed_new ();
gtk_widget_show (fixed3);
gtk_container_add (GTK_CONTAINER (viewport1), fixed3);
iconview1 = gtk_icon_view_new_with_model(model);
gtk_widget_show (iconview1);
gtk_fixed_put (GTK_FIXED (fixed3), iconview1, 0, 0);
gtk_widget_set_size_request (iconview1, 380, 340);
// gtk_icon_view_selected_foreach(GTK_ICON_VIEW (iconview1),);
gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (iconview1), GTK_SELECTION_MULTIPLE);
gtk_icon_view_set_item_width(GTK_ICON_VIEW (iconview1),10);
gtk_icon_view_create_drag_icon(GTK_ICON_VIEW (iconview1),"/usr/share/pixmaps");
g_signal_connect(GTK_BUTTON(buttonView),"clicked",G_CALLBACK(display_icon),iconview1);
/* Store pointers to all widgets, for use by lookup_widget(). */
GLADE_HOOKUP_OBJECT_NO_REF (SelectIconWindow, SelectIconWindow, "SelectIconWindow");
GLADE_HOOKUP_OBJECT (SelectIconWindow, fixed, "fixed");
GLADE_HOOKUP_OBJECT (SelectIconWindow, entryPath, "entryPath");
GLADE_HOOKUP_OBJECT (SelectIconWindow, buttonView, "buttonView");
GLADE_HOOKUP_OBJECT (SelectIconWindow, hseparator1, "hseparator1");
GLADE_HOOKUP_OBJECT (SelectIconWindow, scrolledwindow1, "scrolledwindow1");
GLADE_HOOKUP_OBJECT (SelectIconWindow, viewport1, "viewport1");
GLADE_HOOKUP_OBJECT (SelectIconWindow, fixed3, "fixed3");
GLADE_HOOKUP_OBJECT (SelectIconWindow, iconview1, "iconview1");
return SelectIconWindow;
}
/***********************************************************************************************************
*function name:info_clear
*input argument:none
*output argument:none
*description:The program's function clears the main window's contrals and other inforamtion
*create time:2008-01-15 by antan<antantan@linpus.com>
*************************************************************************************************************/
void info_clear(void)
{
gtk_entry_set_text(GTK_ENTRY(entryName),"");
gtk_entry_set_text(GTK_ENTRY(entryICON),"/usr/share/pixmaps/default.png");
gtk_image_set_from_file(imageButton,"/usr/share/pixmaps/default.png");
gtk_entry_set_text(GTK_ENTRY(entryHttp),"");
}
/********************************************************************************************************************
*function name:str_button_select_clicked
*input argument:none
*output argument:none
*description:the function can access the ico file's name and path.
*create time:2008-01-16 antan<antantan@linpus.com>
************************************************************************************************************************/
void str_button_select_clicked(GtkWidget* win, gpointer data)
{
/*
GtkWidget *winSelect = create_SelectIconWindow();
gtk_widget_show(winSelect);
g_signal_connect(GTK_WINDOW(winSelect),"delete_event",G_CALLBACK(destroy_window),NULL);
*/
char *filename;
GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Select ICONS Window",
win,
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),"/usr/share/pixmaps");
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
{
//char *filename;
//filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
gtk_entry_set_text(GTK_ENTRY(entryICON),filename);
gtk_image_set_from_file(imageButton,filename);
//open_file (filename);
g_free (filename);
}
gtk_widget_destroy (dialog);
}
/**********************************************************************************************************************
*function name:button_ok_clicked
*input argument:none
*output argument:none
*description:the function can be executed creating a shortcut for a new program,
*It can been used easily.
*create time:2008-01-16 by antan<antantan@linpus.com>
************************************************************************************************************************/
void button_ok_clicked(void)
{
FILE *fd_desktop; //*.desktop file
FILE *fd_dmrc_file; //
char *version_str=NULL; //version string
char *encoding_str=NULL; //encode_str
char *icopath=NULL; //save the ico path
char *name=NULL; //save the app_name
char *name_zh_CN=NULL;
char *name_zh_TW=NULL;
char *genericName=NULL;
char *genericName_zh_CN=NULL;
char *genericName_zh_TW=NULL;
char *http_str=NULL; //save http path
char *comment_str=NULL;
char *exec_str=NULL; //firefox execute path
char *ico_str=NULL; //icon file save path
char *terminal_str=NULL; //terminal string
char *type_str=NULL; //type string
char *start_up_wm_class=NULL; //start up wm class
char *categories=NULL; //categories
char *x_desktop_file_install_version = NULL; //x desktop file install version
char *fd_desktop_default_name=NULL; //for testing.
//char *fd_desktop_default_name="/usr/share/applications/edesktop/"; //desktop path and name
char *tempfile=""; /*temp file name variable*/
char *tempstr=""; /*temp string variable*/
//g_printf("I have entry the create document/n");
/* tempstr = gtk_entry_get_text(GTK_ENTRY(entry_name));
g_printf(tempstr);
g_printf("testing0...../n");
*/
version_str = malloc(sizeof(char)*100);
memset(version_str,0x00,sizeof(char)*100);
strcat(version_str,"Version = 1.0/n");
encoding_str = malloc(sizeof(char)*100);
memset(encoding_str,0x00,sizeof(char)*100);
strcat(encoding_str,"Encoding=UTF-8/n");
name = malloc(sizeof(char)*100);
memset(name,0x00,sizeof(char)*100);
strcat(name,"Name=");
strcat(name,gtk_entry_get_text(GTK_ENTRY(entryName))); //get the new app name
//g_printf("testing1...../n");
strcat(name,"/n");
name_zh_CN = malloc(sizeof(char)*100);
memset(name_zh_CN,0x00,sizeof(char)*100);
strcat(name_zh_CN,"Name[zh_CN]=");
strcat(name_zh_CN,gtk_entry_get_text(GTK_ENTRY(entryName)));
strcat(name_zh_CN,"/n");
name_zh_TW = malloc(sizeof(char)*100);
memset(name_zh_TW,0x00,sizeof(char)*100);
strcat(name_zh_TW,"Name[zh_TW]=");
strcat(name_zh_TW,gtk_entry_get_text(GTK_ENTRY(entryName)));
strcat(name_zh_TW,"/n");
genericName = malloc(sizeof(char)*100);
memset(genericName,0x00,sizeof(char)*100);
strcat(genericName,"GenericName=");
strcat(genericName,gtk_entry_get_text(GTK_ENTRY(entryName)));
strcat(genericName,"/n");
genericName_zh_CN = malloc(sizeof(char)*100);
memset(genericName_zh_CN,0x00,sizeof(char)*100);
strcat(genericName_zh_CN,"GenericName[zh_CN]=");
strcat(genericName_zh_CN,gtk_entry_get_text(GTK_ENTRY(entryName)));
strcat(genericName_zh_CN,"/n");
genericName_zh_TW = malloc(sizeof(char)*100);
memset(genericName_zh_TW,0x00,sizeof(char)*100);
strcat(genericName_zh_TW,"GenericName[zh_TW]=");
strcat(genericName_zh_TW,gtk_entry_get_text(GTK_ENTRY(entryName)));
strcat(genericName_zh_TW,"/n");
comment_str = malloc(sizeof(char)*1000);
memset(comment_str,0x00,sizeof(char)*1000);
strcat(comment_str,"Comment= Internet shortcut/n");
exec_str = malloc(sizeof(char)*1000);
memset(exec_str,0x00,sizeof(char)*1000);
strcat(exec_str,"Exec=firefox http://");
strcat(exec_str,gtk_entry_get_text(GTK_ENTRY(entryHttp)));
strcat(exec_str,"/n");
ico_str = malloc(sizeof(char)*1000);
memset(ico_str,0x00,sizeof(char)*1000);
strcat(ico_str,"Icon=");
strcat(ico_str,gtk_entry_get_text(GTK_ENTRY(entryICON)));
strcat(ico_str,"/n");
terminal_str = malloc(sizeof(char)*100);
memset(terminal_str,0x00,sizeof(char)*100);
strcat(terminal_str,"Terminal=false/n");
type_str = malloc(sizeof(char)*100);
memset(type_str,0x00,sizeof(type_str));
strcat(type_str,"Type=Application/n");
start_up_wm_class = malloc(sizeof(char)*100);
memset(start_up_wm_class,0x00,sizeof(start_up_wm_class));
strcat(start_up_wm_class,"StartupWMClass=Firefox-bin/n");
categories = malloc(sizeof(char)*100);
memset(categories,0x00,sizeof(categories));
strcat(categories,"Categories=Network;WebBrowser;/n");
x_desktop_file_install_version = malloc(sizeof(char)*100);
memset(x_desktop_file_install_version,0x00,sizeof(x_desktop_file_install_version));
strcat(x_desktop_file_install_version,"X-Desktop-File-Install-Version=0.10/n");
fd_desktop_default_name = malloc(sizeof(char)*1000);
memset(fd_desktop_default_name,0x00,sizeof(char)*1000);
strcat(fd_desktop_default_name,"/home/linpus/.config/");
strcat(fd_desktop_default_name,gtk_entry_get_text(GTK_ENTRY(entryName)));
//strcat(fd_desktop_default_name,"/root/Desktop/test.desktop");
strcat(fd_desktop_default_name,".desktop");
// fd_desktop_default_name[sizeof(fd_desktop_default_name)] = 0x00;
g_printf("testing...../n");
//fd_desktop = malloc(sizeof(FILE*));
//memset(fd_desktop_default_name,0x00,sizeof(fd_desktop_default_name));
//strcat(fd_desktop_default_name,"/root/Desktop/test.desktop");
if ((fd_desktop = fopen(fd_desktop_default_name,"w+")) == NULL)
{
g_printf("open file error/n");
}
fputs("[Desktop Entry]/n",fd_desktop);
fputs(version_str,fd_desktop);
fputs(encoding_str,fd_desktop);
fputs(name,fd_desktop);
fputs(name_zh_CN,fd_desktop);
fputs(name_zh_TW,fd_desktop);
fputs(genericName,fd_desktop);
fputs(genericName_zh_CN,fd_desktop);
fputs(genericName_zh_TW,fd_desktop);
fputs(comment_str,fd_desktop);
fputs(exec_str,fd_desktop);
fputs(ico_str,fd_desktop);
fputs(terminal_str,fd_desktop);
fputs(type_str,fd_desktop);
fputs(start_up_wm_class,fd_desktop);
fputs(x_desktop_file_install_version,fd_desktop);
fputs(categories,fd_desktop);
fclose(fd_desktop);
tempstr = malloc(sizeof(char)*100);
memset(tempstr,0x00,sizeof(char)*100);
strcat(tempstr,"xfdesktop-simple --add_app=");
strcat(tempstr,fd_desktop_default_name);
system(tempstr);
free(tempstr);
free(version_str);
free(encoding_str);
free(name);
free(name_zh_CN);
free(name_zh_TW);
free(genericName);
free(genericName_zh_CN);
free(genericName_zh_TW);
free(comment_str);
free(exec_str);
free(ico_str);
free(terminal_str);
free(type_str);
free(start_up_wm_class);
free(x_desktop_file_install_version);
free(categories);
free(fd_desktop_default_name);
//free(fd_desktop);
gtk_entry_set_text(GTK_ENTRY(entryName),"");
gtk_entry_set_text(GTK_ENTRY(entryICON),"/usr/share/pixmaps/default.png");
gtk_image_set_from_file(imageButton,"/usr/share/pixmaps/default.png");
gtk_entry_set_text(GTK_ENTRY(entryHttp),"");
}
/***************************************************************************************************************************************/
void destroy_window(GtkWidget *win, gpointer data)
{
gtk_widget_destroy(win);
}
/*****************************************************************************************************************************************/
void display_icon(GtkWidget *button, gpointer data)
{
GtkTreeModel *model = gtk_icon_view_get_model(data);
//GtkTreeIter it;
char *file_name;
char *file_path = "/usr/share/pixmaps";
const char *dir_path=gtk_entry_get_text(GTK_ENTRY(entryPath));
GtkTreePath *path;
GtkTreeIter iter;
GtkTreeIter parent_iter;
gtk_tree_model_get_iter_from_string(model,&iter,"/usr/share/pixmaps");
// gtk_tree_model_get_iter( model, &iter, file_path );
/* get the iterator from a string */
//gtk_tree_model_get_iter_from_string (model, &iter, "3:2:5");
/* get the iterator from a path */
path = gtk_tree_path_new_from_string ("3:2:5");
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_path_free (path);
/* walk the tree to find the iterator */
gtk_tree_model_iter_nth_child (model, &iter, NULL, 3);
parent_iter = iter;
gtk_tree_model_iter_nth_child (model, &iter, &parent_iter, 2);
parent_iter = iter;
gtk_tree_model_iter_nth_child (model, &iter, &parent_iter, 5);
//gtk_icons_view_set_model(GTK_ICON_VIEW(data),list);
}
support.h内容
#ifndef SUPPORT_H
#define SUPPORT_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gtk/gtk.h>
/*
* Standard gettext macros.
*/
#ifdef ENABLE_NLS
# include <libintl.h>
# undef _
# define _(String) dgettext (PACKAGE, String)
# define Q_(String) g_strip_context ((String), gettext (String))
# ifdef gettext_noop
# define N_(String) gettext_noop (String)
# else
# define N_(String) (String)
# endif
#else
# define textdomain(String) (String)
# define gettext(String) (String)
# define dgettext(Domain,Message) (Message)
# define dcgettext(Domain,Message,Type) (Message)
# define bindtextdomain(Domain,Directory) (Domain)
# define _(String) (String)
# define Q_(String) g_strip_context ((String), (String))
# define N_(String) (String)
#endif
/*
* Public Functions.
*/
/*
* This function returns a widget in a component created by Glade.
* Call it with the toplevel widget in the component (i.e. a window/dialog),
* or alternatively any widget in the component, and the name of the widget
* you want returned.
*/
GtkWidget* lookup_widget (GtkWidget *widget,
const gchar *widget_name);
/* Use this function to set the directory containing installed pixmaps. */
void add_pixmap_directory (const gchar *directory);
/*
* Private Functions.
*/
/* This is used to create the pixmaps used in the interface. */
GtkWidget* create_pixmap (GtkWidget *widget,
const gchar *filename);
/* This is used to create the pixbufs used in the interface. */
GdkPixbuf* create_pixbuf (const gchar *filename);
/* This is used to set ATK action descriptions. */
void glade_set_atk_action_description (AtkAction *action,
const gchar *action_name,
const gchar *description);
#endif
support.c
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include "support.h"
GtkWidget*
lookup_widget (GtkWidget *widget,
const gchar *widget_name)
{
GtkWidget *parent, *found_widget;
for (;;)
{
if (GTK_IS_MENU (widget))
parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
else
parent = widget->parent;
if (!parent)
parent = (GtkWidget*) g_object_get_data (G_OBJECT (widget), "GladeParentKey");
if (parent == NULL)
break;
widget = parent;
}
found_widget = (GtkWidget*) g_object_get_data (G_OBJECT (widget),
widget_name);
if (!found_widget)
g_warning ("Widget not found: %s", widget_name);
return found_widget;
}
static GList *pixmaps_directories = NULL;
/* Use this function to set the directory containing installed pixmaps. */
void
add_pixmap_directory (const gchar *directory)
{
pixmaps_directories = g_list_prepend (pixmaps_directories,
g_strdup (directory));
}
/* This is an internally used function to find pixmap files. */
static gchar*
find_pixmap_file (const gchar *filename)
{
GList *elem;
/* We step through each of the pixmaps directory to find it. */
elem = pixmaps_directories;
while (elem)
{
gchar *pathname = g_strdup_printf ("%s%s%s", (gchar*)elem->data,
G_DIR_SEPARATOR_S, filename);
if (g_file_test (pathname, G_FILE_TEST_EXISTS))
return pathname;
g_free (pathname);
elem = elem->next;
}
return NULL;
}
/* This is an internally used function to create pixmaps. */
GtkWidget*
create_pixmap (GtkWidget *widget,
const gchar *filename)
{
gchar *pathname = NULL;
GtkWidget *pixmap;
if (!filename || !filename[0])
return gtk_image_new ();
pathname = find_pixmap_file (filename);
if (!pathname)
{
g_warning (_("Couldn't find pixmap file: %s"), filename);
return gtk_image_new ();
}
pixmap = gtk_image_new_from_file (pathname);
g_free (pathname);
return pixmap;
}
/* This is an internally used function to create pixmaps. */
GdkPixbuf*
create_pixbuf (const gchar *filename)
{
gchar *pathname = NULL;
GdkPixbuf *pixbuf;
GError *error = NULL;
if (!filename || !filename[0])
return NULL;
pathname = find_pixmap_file (filename);
if (!pathname)
{
g_warning (_("Couldn't find pixmap file: %s"), filename);
return NULL;
}
pixbuf = gdk_pixbuf_new_from_file (pathname, &error);
if (!pixbuf)
{
fprintf (stderr, "Failed to load pixbuf file: %s: %s/n",
pathname, error->message);
g_error_free (error);
}
g_free (pathname);
return pixbuf;
}
/* This is used to set ATK action descriptions. */
void
glade_set_atk_action_description (AtkAction *action,
const gchar *action_name,
const gchar *description)
{
gint n_actions, i;
n_actions = atk_action_get_n_actions (action);
for (i = 0; i < n_actions; i++)
{
if (!strcmp (atk_action_get_name (action, i), action_name))
atk_action_set_description (action, i, description);
}
}
以上程序是最近写的一个小APP,希望对有帮助的人一个帮助