Use of gtk progressbar progress bar Control

Source: Internet
Author: User
# Include <gtk/gtk. h> typedef struct _ ProgressData {GtkWidget * window; GtkWidget * pbar; int timer; gboolean activity_mode;} ProgressData;/* update the progress bar, in this way, you can see the movement of the progress bar */gint progress_timeout (gpointer data) {ProgressData * pdata = (ProgressData *) data; gdouble new_val; if (pdata-> activity_mode) gtk_progress_bar_pulse (GTK_PROGRESS_BAR (pdata-> pbar); else {/* calculate the progress bar value using the value range set in the adjustment object */new_val = gtk_progress_bar_get _ Fraction (GTK_PROGRESS_BAR (pdata-> pbar) + 0.01; if (new_val> 1.0) new_val = 0.0; /* set the new value of the progress bar */gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pdata-> pbar), new_val);}/* This is a timeout function and returns TRUE, in this way, it can continue to be called */return TRUE;}/* callback function, switch to the text display on your chute on the progress bar */void toggle_show_text (GtkWidget * widget, progressData * pdata) {const gchar * text; text = gtk_progress_bar_get_text (GTK_PROGRESS_BAR (pdata-> pbar); if (tex T & * text) gtk_progress_bar_set_text (GTK_PROGRESS_BAR (pdata-> pbar), ""); Progress (GTK_PROGRESS_BAR (pdata-> pbar), "some text ");} /* callback function, switch the activity mode of the progress bar */void toggle_activity_mode (GtkWidget * widget, ProgressData * pdata) {pdata-> activity_mode =! Pdata-> activity_mode; if (pdata-> activity_mode) gtk_progress_bar_pulse (GTK_PROGRESS_BAR (pdata-> pbar); Progress (GTK_PROGRESS_BAR (pdata-> pbar), 0.0 );} /* callback function, switch the direction of the progress bar */void toggle_orientation (GtkWidget * widget, ProgressData * pdata) {switch (gtk_progress_bar_get_orientation (GTK_PROGRESS_BAR (pdata-> pbar ))) {case GTK_PROGRESS_LEFT_TO_RIGHT: gtk_progress_bar_set_orientation (GTK_PROGRESS_BAR (pdata-> pbar), GTK_PROGRESS_RIGHT_TO_LEFT); break; case when: Then (GTK_PROGRESS_BAR (pdata-> pbar), then); break; default: break; // do nothing}/* clear allocated memory, delete timer (timer) */void destroy_progress (GtkWidget * widget, ProgressData * pdata) {gtk_timeout_remove (pdata-> timer); pdata-> timer = 0; pdata-> window = NULL; g_free (pdata); gt K_main_quit ();} int main (int argc, char * argv []) {ProgressData * pdata; GtkWidget * align; GtkWidget * separator; GtkWidget * table; GtkWidget * button; gtkWidget * check; GtkWidget * vbox; gtk_init (& argc, & argv);/* allocate memory for data transmitted to the callback function */pdata = g_malloc (sizeof (ProgressData )); pdata-> window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_resizable (GTK_WINDOW (pdata-> window), TRUE); g_signal_connect (G_OBJE CT (pdata-> window), "destroy", G_CALLBACK (destroy_progress), pdata); gtk_window_set_title (GTK_WINDOW (pdata-> window), "GtkProgressBar "); gtk_container_set_border_width (GTK_CONTAINER (pdata-> window), 0); vbox = gtk_vbox_new (FALSE, 5); gtk_container_set_border_width (GTK_CONTAINER (vbox), 10 ); gtk_container_add (GTK_CONTAINER (pdata-> window), vbox); gtk_widget_show (vbox);/* create a center-aligned object */align = gtk_ali Gnment_new (0.5, 0.5, 0, 0); gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 5); gtk_widget_show (align ); /* create progress bar */pdata-> pbar = gtk_progress_bar_new (); gtk_container_add (GTK_CONTAINER (align), pdata-> pbar); gtk_widget_show (pdata-> pbar ); /* Add a timer (timer) to update the progress bar value */pdata-> timer = gtk_timeout_add (100, progress_timeout, pdata); separator = gtk_hseparator_new (); gtk_box_pack_start (GTK_BOX (v Box), separator, FALSE, FALSE, 0); gtk_widget_show (separator);/* number of rows, number of columns, homogeneity (homogeneous) */table = gtk_table_new (2, 2, FALSE ); gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, TRUE, 0); gtk_widget_show (table);/* Add a check button, to select whether the text is displayed in the chute */check = gtk_check_button_new_with_label ("Show text"); gtk_table_attach (GTK_TABLE (table), check, 0, 1, 0, 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 5); g _ Signal_connect (G_OBJECT (check), "clicked", G_CALLBACK (toggle_show_text), pdata); gtk_widget_show (check);/* Add a check button, switch Activity status */check = activities ("Activity mode"); gtk_table_attach (GTK_TABLE (table), check, 0, 1, 1, 2, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 5); g_signal_connect (G_OBJECT (check), "clicked", G_CALLBACK (toggle_activity_mode), pdata); gtk_widget_show (check) ;/* Add a check button to switch the moving direction */check = gtk_check_button_new_with_label ("Right to Left"); gtk_table_attach (GTK_TABLE (table), check, 0, 1, 2, 3, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 5); g_signal_connect (G_OBJECT (check), "clicked", G_CALLBACK (callback), pdata); gtk_widget_show (check ); /* Add a button to exit the application */button = gtk_button_new_with_label ("close"); g_signal_connect_swapped (G_OBJECT (Ton), "clicked", G_CALLBACK (gtk_widget_destroy), pdata-> window); gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0 ); /* set the button as a default component */GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);/* set the default focus to this button to make it the default button, you only need to press the Enter key *, which is equivalent to clicking this button * // note: the default component can become the default component after obtaining the focus, and you can switch the focus by pressing the direction key. Gtk_widget_grab_default (button); gtk_widget_show (pdata-> window); gtk_main (); return 0 ;}

# Target, compiler and objects.EXEC    =  testCC      =  gccOBJ     =  main.o# Use  for  compile.CFLAGS  =  `pkg-config  --cflags gtk+-2.0 --cflags glib-2.0`# Use  for  link.CLIBS   =  `pkg-config  --libs gtk+-2.0 --cflags glib-2.0`$(EXEC):$(OBJ)$(CC) -o $@ $^ $(CLIBS)main.o:main.c$(CC) -c $< $(CFLAGS)clean:rm $(EXEC)  * .o

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.