📄 gui.c
字号:
/* * Copyright (c) 2001-2005 Falk Feddersen * * 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#include <sys/time.h>#include <time.h>#include <unistd.h>#include <math.h>#include "bdefs.h"#if THREAD_YES==1 /* located after "bdefs.h" include */#include <pthread.h>#endif /* THREAD_YES */#include "gui.h"#if THREAD_YES==1 extern pthread_t gui_thread;extern pthread_t main_thread;#endif /* THREAD_YES */timewindow *TW;int fdivfalkg(gdouble num, gdouble denom){ gdouble t1; gdouble t2=0.0, t3=0.0; int tt3; t1 = num/denom; t2 = modf(t1,&t3); tt3 = (int ) t3; return tt3;}void convert_timing(timewindow *TW){ gdouble dt = TW->TT->dt; gint time_index = TW->TT->time_index; gdouble time; gdouble min, hour, day, sec; time = dt*time_index; min = fdivfalkg(time,60.0); sec = time - min*60.0; hour = fdivfalkg(min,60.0); min = (min - 60.0*hour); day = fdivfalkg(hour,24.0); hour = (hour - 24.0*day); TW->day = (gint ) day; TW->hour = (gint ) hour; TW->min = (gint ) min; TW->sec = (gint ) sec;}void full_time(timewindow *TW){ // gdouble dt = TW->TT->dt; // gint time_index = TW->TT->time_index; gdouble ftime; gdouble min, hour, day, sec; ftime = TW->TT->total_run_time; min = fdivfalkg(ftime,60.0); sec = ftime - min*60.0; hour = fdivfalkg(min,60.0); min = (min - 60.0*hour); day = fdivfalkg(hour,24.0); hour = (hour - 24.0*day); TW->dayf = (gint ) day; TW->hourf = (gint ) hour; TW->minf = (gint ) min; TW->secf = (gint ) sec;}timewindow* init_time_window(timing *TT){ timewindow *TW; TW = (timewindow * ) g_malloc(sizeof(timewindow)); TW->TT = TT; TW->day = 0; TW->hour = 0; TW->min = 0; TW->sec = 0; TW ->label1_text = g_string_new(" "); TW->msg = TT->msg; TW->message = TT->message; TW->initfilename = TT->initfilename; g_string_sprintf(TW->label1_text," Run Time: Day %02d Hour %02d Min %02d Sec %02d", TW->day,TW->hour,TW->min,TW->sec); full_time(TW); TW ->label2_text = g_string_new(" "); g_string_sprintf(TW->label2_text,"Full Time: Day %02d Hour %02d Min %02d Sec %05d", TW->dayf,TW->hourf,TW->minf,TW->secf); TW ->label0_text = g_string_new(" "); g_string_sprintf(TW->label0_text," Elapsed Time: Day %02d Hour %02d Min %02d Sec %02d",0,0,0,0); TW->TT = TT; return TW;}gint window_update_time(timewindow *TW){ gdouble new_val; gfloat new_val2; timing *TT; struct tm* ptm; time_t total_sec, now_sec; now_sec = time(NULL); total_sec = now_sec - TW->start_sec; ptm = gmtime(&total_sec);#ifdef DEBUG printf("now_sec=%d, start_sec=%d, total_sec = %d\n",now_sec,TW->start_sec,total_sec); printf("hour = %d, min=%d, sec=%d\n",ptm->tm_hour,ptm->tm_min,ptm->tm_sec);#endif /* DEBUG */ // day = ptm->tm_day; // hour = ptm->tm_hour-19; g_string_sprintf(TW->label0_text," Elapsed Time: Day %02d Hour %02d Min %02d Sec %02d", ptm->tm_yday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec); gtk_label_set_text(GTK_LABEL(TW->label0),TW->label0_text->str); TT = TW->TT; new_val = (TT->time_index)*(TT->dt)/(TT->total_run_time); new_val2 = new_val; convert_timing(TW); g_string_sprintf(TW->label1_text," Run Time: Day %02d Hour %02d Min %02d Sec %02d", TW->day,TW->hour,TW->min,TW->sec); gtk_label_set_text(GTK_LABEL(TW->label1),TW->label1_text->str); /* Calculate the value of the progress bar using the * value range set in the adjustment object */ /* adj = GTK_PROGRESS (TW->pbar)->adjustment; if (new_val > adj->upper) new_val = adj->upper; */ gtk_progress_set_value (GTK_PROGRESS (TW->pbar), new_val2); if (new_val2>=1.0) { gtk_main_quit(); } return(TRUE);}/*gint window_update_time(timewindow *TW){ gdouble new_val; gfloat new_val2; timing *TT; TT = TW->TT; new_val = (TT->time_index)*(TT->dt)/(TT->total_run_time); new_val2 = new_val; convert_timing(TW); g_string_sprintf(TW->label1_text," Run Time: Day %02d Hour %02d Min %02d Sec %02d", TW->day,TW->hour,TW->min,TW->sec); gtk_label_set_text(GTK_LABEL(TW->label1),TW->label1_text->str); gtk_progress_set_value (GTK_PROGRESS (TW->pbar), new_val2); if (TW->TT->finished==1) gtk_signal_emit_by_name(GTK_OBJECT(TW->window),"destroy"); return(TRUE);}*/gint delete_event( GtkWidget *widget, GdkEvent *event, gpointer data ){#if THREAD_YES==1 pthread_cancel(main_thread);#endif return(FALSE);}/* Another callback */void destroy( GtkWidget *widget, gpointer data ){#if THREAD_YES==1 pthread_cancel(main_thread);#endif gtk_main_quit();}void button_callback(GtkWidget *widget, gpointer data){#if THREAD_YES==1 pthread_cancel(main_thread);#endif gtk_main_quit();}void* make_window(timewindow *T){ GtkWidget *window; GtkWidget *vbox; GtkWidget *label_1; GtkWidget *label0; GtkWidget *label1; GtkWidget *label_file; GtkWidget *label2; GtkAdjustment *adj; GtkWidget *pbar; GtkWidget *button; GtkWidget *stopbox; GtkWidget *hsep; GtkWidget *hsep_1; GtkWidget *hsep_file; gchar title[] = "funwaveC timing window"; /* create a new window */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), title); T->window = window; /* When the window is given the "delete_event" signal (this is given * by the window manager, usually by the "close" option, or on the * titlebar), we ask it to call the delete_event () function * as defined above. The data passed to the callback * function is NULL and is ignored in the callback function. */ gtk_signal_connect (GTK_OBJECT (window), "delete_event", GTK_SIGNAL_FUNC (delete_event), NULL); /* Here we connect the "destroy" event to a signal handler. * This event occurs when we call gtk_widget_destroy() on the window, * or if we return FALSE in the "delete_event" callback. */ gtk_signal_connect (GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (destroy), NULL); /* Sets the border width of the window. */ gtk_container_set_border_width (GTK_CONTAINER (window), 10); vbox = gtk_vbox_new (FALSE, 5); gtk_container_set_border_width (GTK_CONTAINER(vbox), 10); gtk_container_add (GTK_CONTAINER(window), vbox); gtk_widget_show(vbox); label_file = gtk_label_new(T->initfilename->str); T->label_file = label_file; gtk_widget_show (label_file); gtk_box_pack_start (GTK_BOX (vbox), label_file, FALSE, FALSE, 5); /* Create a Horizontal Separator */ hsep_file = gtk_hseparator_new(); gtk_widget_show(hsep_file); gtk_box_pack_start(GTK_BOX(vbox),hsep_file,FALSE,TRUE,5); /* Creates the first-first label -1 */ if (TW->msg) { label_1 = gtk_label_new(T->message->str); T->label_1 = label_1; gtk_widget_show (label_1); gtk_box_pack_start (GTK_BOX (vbox), label_1, FALSE, FALSE, 5); /* Create a Horizontal Separator */ hsep_1 = gtk_hseparator_new(); gtk_widget_show(hsep_1); gtk_box_pack_start(GTK_BOX(vbox),hsep_1,FALSE,TRUE,5); } /* Creates the first label 0 */ label0 = gtk_label_new(T->label0_text->str); T->label0 = label0; gtk_widget_show (label0); gtk_box_pack_start (GTK_BOX (vbox), label0, FALSE, FALSE, 5); /* Creates the label 1 */ label1 = gtk_label_new(T->label1_text->str); T->label1 = label1; gtk_widget_show (label1); gtk_box_pack_start (GTK_BOX (vbox), label1, FALSE, FALSE, 5); /* Creates a new label */ label2 = gtk_label_new(T->label2_text->str); T->label2 = label2; gtk_widget_show (label2); gtk_box_pack_start (GTK_BOX (vbox), label2, FALSE, FALSE, 5); /* Create a Adjusment object to hold the range of the * progress bar */ adj = (GtkAdjustment *) gtk_adjustment_new (0, 0, 1, 0, 0, 0); /* Create the GtkProgressBar using the adjustment */ pbar = gtk_progress_bar_new_with_adjustment (adj); T->pbar = pbar; /* Set the format of the string that can be displayed in the * trough of the progress bar: * %p - percentage * %v - value * %l - lower range value * %u - upper range value */ gtk_progress_set_format_string (GTK_PROGRESS (pbar), "%v from [%l-%u] (=%p%%)"); gtk_container_add (GTK_CONTAINER (vbox), pbar); gtk_widget_show(pbar); /* Create a Horizontal Separator */ hsep = gtk_hseparator_new(); gtk_widget_show(hsep); gtk_box_pack_start(GTK_BOX(vbox),hsep,FALSE,TRUE,5); /* create the STOP button */ stopbox = gtk_hbox_new(FALSE,0); button = gtk_button_new_with_label("Stop"); gtk_signal_connect(GTK_OBJECT(button),"clicked", GTK_SIGNAL_FUNC (button_callback), NULL); gtk_box_pack_start(GTK_BOX(stopbox),button,TRUE,FALSE,3); gtk_widget_show(button); gtk_box_pack_start(GTK_BOX(vbox),stopbox,FALSE,FALSE,0); gtk_widget_show(stopbox); /* and the window */ gtk_widget_show (window);}int gui_start(timing *TT){ gint32 timer; TW = init_time_window(TT); make_window(TW); timer = gtk_timeout_add(300, (GtkFunction ) window_update_time,TW); TW->start_sec = time(NULL); gtk_main();#if THREAD_YES==1 pthread_exit(NULL);#endif /* THREAD_YES */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -