📄 misc.c
字号:
GtkWidget *i_label;// e_assert_widget(parent, "parent is not a GtkWidget"); i_label = gtk_label_new(text); if(GTK_IS_CONTAINER(parent)) gtk_container_add(GTK_CONTAINER(parent),i_label ); else gtk_box_pack_start(GTK_BOX(parent), i_label,FALSE,FALSE,0); gtk_widget_show(i_label); return i_label;}/* * --- CloseDialog * * Routine to close the about dialog window. */void closeShowMessage (GtkWidget *widget, gpointer data){ GtkWidget *dialog_widget = (GtkWidget *) data; /* --- Close the widget --- */ gtk_widget_destroy (dialog_widget);}/* * ClearShowMessage * * Release the window "grab" * Clear out the global dialog_window since that * is checked when the dialog is brought up. */void clearShowMessage (GtkWidget *widget, gpointer data){ gtk_grab_remove (widget);}/* * ShowMessage * * Show a popup message to the user. * * sztitle -- 对话框标题 * szmessage -- 对话框显示内容 */void showMessage (char *szTitle, char *szMessage){ GtkWidget *label; GtkWidget *button; GtkWidget *dialog_window; /* --- Create a dialog window --- */ dialog_window = gtk_dialog_new (); gtk_signal_connect (GTK_OBJECT (dialog_window), "destroy", GTK_SIGNAL_FUNC (clearShowMessage), NULL); /* --- Set the title and add a border --- */ gtk_window_set_title (GTK_WINDOW (dialog_window), szTitle); gtk_container_border_width (GTK_CONTAINER (dialog_window), 0); /* --- Create an "Ok" button with the focus --- */ button = gtk_button_new_with_label ("OK"); gtk_signal_connect (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (closeShowMessage), dialog_window); /* --- Default the "Ok" button --- */ GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT); gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog_window)->action_area), button, TRUE, TRUE, 0); gtk_widget_grab_default (button); gtk_widget_show (button); /* --- Create a descriptive label --- */ label = gtk_label_new (szMessage); /* --- Put some room around the label text --- */ gtk_misc_set_padding (GTK_MISC (label), 10, 10); /* --- Add label to designated area on dialog --- */ gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog_window)->vbox), label, TRUE, TRUE, 0); /* --- Show the label --- */ gtk_widget_show (label); /* --- Show the dialog --- */ gtk_widget_show (dialog_window); /* --- Only this window can have actions done. --- */ gtk_grab_add (dialog_window);}/*
* CanWindowClose
*
* Function that determines that if the dialog
* window can be closed.
*/
gint CanWindowClose (GtkWidget *widget)
{
/* --- TRUE => cannot close --- */
/* --- FALSE => can close --- */
return (pdata->bProgressUp);
}
/*
* UpdateProgress
*
* Update the progress window to reflect the state
* of the file that is being loaded.
*
* pos - how much of the file has been loaded.
* len - length of the file
* (pos / len) = % file has been loaded.
*/
void UpdateProgress (long pos, long len)
{
gfloat pvalue;
int pct;
/* --- Prevent divide by zero errors --- */
if (len > 0) {
/* --- Calculate the percentage --- */
pvalue = (gfloat) pos / (gfloat) len;
pct = pvalue * 100;
if (pdata->nLastPct != pct) {
/* --- Update the displayed value --- */
gtk_progress_set_percentage (GTK_PROGRESS (pdata->progressbar),
pvalue);
/* --- Repaint any windows - like the progress bar --- */
while (gtk_events_pending ()) {
gtk_main_iteration ();
}
pdata->nLastPct = pct;
}
}
}
/*
* StartProgress
*
* Create a window for the progress bar * * hint---进度条框提示内容
*/
void StartProgress (gchar *hint)
{
GtkWidget *label;
GtkWidget *table;
GtkWidget *window;
GtkAdjustment *adj;
pdata = (typProgressData *)g_malloc(sizeof(typProgressData));
pdata->nLastPct = -1;
pdata->bProgressUp = TRUE;
/*
* --- Create the top level window
*/
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
pdata->window = window;
/* --- Hook up the destroy --- */
gtk_signal_connect (GTK_OBJECT (window), "delete_event",
GTK_SIGNAL_FUNC (CanWindowClose), pdata);
gtk_container_border_width (GTK_CONTAINER (window), 10);
/* --- Create a table --- */
table = gtk_table_new (3, 2, TRUE);
gtk_container_add (GTK_CONTAINER (window), table);
/* --- Add a label to the table --- */
label = gtk_label_new (hint);
gtk_table_attach_defaults (GTK_TABLE (table), label, 0,2,0,1);
gtk_widget_show (label);
/* --- Add the progress bar to the table --- */
adj = (GtkAdjustment *) gtk_adjustment_new (0, 0, 400, 0, 0, 0);
pdata->progressbar = gtk_progress_bar_new_with_adjustment (adj);
gtk_table_attach_defaults (GTK_TABLE (table),
pdata->progressbar, 0,2,1,2);
gtk_widget_show (pdata->progressbar);
/* --- Show everything --- */
gtk_widget_show (table);
gtk_widget_show (window);
}
/*
* EndProgress
*
* Close down the progress bar.
*/
void EndProgress ()
{
/* --- Allow it to close --- */
pdata->bProgressUp = FALSE;
/* --- Destroy the window --- */
gtk_widget_destroy (pdata->window);
/* --- Free used memory. --- */
g_free (pdata);
pdata = NULL;
}
/*-----------------------------------------/ create Clist dialog/ parent --父窗口或容器/ cols -- list窗口列数/ item-selected -- 点击list行记录时回呼函数/-------------------------------------------*/ GtkWidget *createClist(GtkWidget *parent, gint cols, GtkSignalFunc item_selected){ GtkWidget *i_s, *i_list;// e_assert_widget(parent, "parent is not a GtkWidget"); i_s = gtk_scrolled_window_new(NULL, NULL); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(i_s), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); gtk_box_pack_start(GTK_BOX(parent), i_s, TRUE, TRUE, 0); gtk_widget_show(i_s); i_list = gtk_clist_new_with_titles(cols, NULL); gtk_signal_connect(GTK_OBJECT(i_list), "select_row", GTK_SIGNAL_FUNC(internalClistSelect), i_list); gtk_signal_connect(GTK_OBJECT(i_list), "select_row", GTK_SIGNAL_FUNC(item_selected), i_list); gtk_clist_set_shadow_type(GTK_CLIST(i_list), GTK_SHADOW_OUT); gtk_clist_set_selection_mode(GTK_CLIST(i_list), GTK_SELECTION_SINGLE); gtk_clist_column_titles_show(GTK_CLIST(i_list)); gtk_container_add(GTK_CONTAINER(i_s), i_list); gtk_widget_show(i_list); return i_list;}/*---------------------------------------/ 设置list构件列项/ list --list 构件/ title -- 列标题/ width -- 列宽度/-------------------------------------------*/void setupClist(GtkWidget *list, gint col, gchar *title, gint width){// e_assert_widget(list, "list is not a GtkWidget"); gtk_clist_set_column_title(GTK_CLIST(list), col, title); gtk_clist_set_column_width(GTK_CLIST(list), col, width);}void internalClistSelect(GtkWidget *list, gint c, gint r){ gchar *internal; int i;// e_assert_widget(list, "list is not a GtkWidget"); strcpy(current_list_selection, ""); current_list_row = c; for(i = 0; i < GTK_CLIST(list)->columns; i++) { gtk_clist_get_text(GTK_CLIST(list), c, i, &internal); sprintf(current_list_selection, "%s | %s",current_list_selection, internal); }}gint getClistRow(){ return current_list_row;}gchar *getClist(){ return current_list_selection;}/*-------------------------------------------------/ 获取GTKClist构件中指定的行,列的数据???????????????/ list -- GTKClist 构件/ c --行/ r -- 列/---------------------------------------------------*/ gchar *getClistText(GtkWidget *list, gint c, gint r){ gchar *internal;// e_assert_widget(list, "list is not a GtkWidget"); if(c>GTK_CLIST(list)->rows); return NULL; if(r>GTK_CLIST(list)->columns); return NULL; gtk_clist_get_text(GTK_CLIST(list), c, r, &internal); return internal;}/*-------------------------------------------------/ 设置GTKClist构件中指定的行,列的数据???????????????/ list -- GTKClist 构件/ c --行/ r -- 列/---------------------------------------------------*/ gint setClistText(GtkWidget *list, gint c, gint r,gchar *text){// e_assert_widget(list, "list is not a GtkWidget"); if(c>GTK_CLIST(list)->rows); return FAIL; if(r>GTK_CLIST(list)->columns); return FAIL; gtk_clist_set_text(GTK_CLIST(list), c, r, text); return SUCCESS;}/*--------------------------------------------/ 插入list纪录到最后/ list -- list 构件/ text -- 字符串数组/--------------------------------------------*/void insertClist(GtkWidget *list, gchar *text[]){// e_assert_widget(list, "list is not a GtkWidget"); gtk_clist_append(GTK_CLIST(list), text);}/*--------------------------------------------/ 插入list纪录到指定位址/ list -- list 构件/ text -- 字符串数组/--------------------------------------------*/void insertClistRow(GtkWidget *list, gchar *text[]){// e_assert_widget(list, "list is not a GtkWidget"); gtk_clist_insert(GTK_CLIST(list),current_list_row,text);}/*-------------------------------------------/ 删除list构件中当前记录/----------------------------------------*/void removeClist(GtkWidget *list){// e_assert_widget(list, "list is not a GtkWidget"); gtk_clist_remove(GTK_CLIST(list), current_list_row);}
/*
* Get the selected filename and print it to the console
*/
void file_ok_sel (GtkWidget *w, GtkFileSelection *fs)
{
char *sTempFile;
/* --- Get the name --- */
sTempFile = gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs));
// printf ("Getting %s\n", sTempFile);
/* --- Allocate space and save it. --- */
sFilename =(char *) malloc (sizeof (char) * (strlen (sTempFile) + 1));
strcpy (sFilename, sTempFile);
/* --- Destroy the file selection --- */
gtk_widget_destroy (filew);
}
void file_cancel_sel (GtkWidget *w, GtkFileSelection *fs)
{
/* --- Destroy the file selection --- */
sFilename = (char *)malloc (sizeof (char) * 10);
strcpy (sFilename, "");
gtk_widget_destroy (filew);
}
/*
* DestroyDialog
*
* Destroy the dialog (obvious, eh?) but also remove the
* grab and close the modal.
*/
int DestroyDialog (GtkWidget *widget, gpointer *data)
{
// printf ("widget destroyed\n");
// sFilename = malloc (sizeof (char) * 10);
// strcpy (sFilename, "");
gtk_grab_remove (widget);
gtk_main_quit ();
return (FALSE);
}
/*
* getFilename
返回选择文件
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -