The GtkProgressBar and GtkStatusBar Widgets

only for RuBoard - do not distribute or recompile

The GtkProgressBar and GtkStatusBar Widgets

The GtkProgressBar and GtkStatusBar widgets are exactly what you d expect: a progress bar and a status bar. The GtkProgressBar widget indicates a sliding value from 0 to 100, normally. However, this can be customized in GTK+. Also, the progress can be set to move visually from left to right, right to left, top to bottom, or bottom to top. The GtkStatusBar widget allows the program to communicate short text messages to the user. Normally, both would be found at the bottom of a user interface window. Keeping with the previous listing, Figure 3.2 shows the program from Listing 3.2, compiled and running.

Figure 3.2. The GtkProgressBar and GtkStatusBar demo program.
graphics/03fig02.gif

A Note About the Status Bar Widget

The status bar widget is for handling text, and there doesn t appear to be an easy way to put other widgets into it (such as a progress bar). I am reluctant to say it can t be done, because about that time, someone will do it. At this point, though, there are no built-in functions for doing that sort of thing.

Listing 3.2 GtkCombo GtkProgressBar and GtkStatusBar Demo Program
 #include <gtk/gtk.h>  GtkWidget *frm_bars;  GtkWidget *top_progress_bar, *bottom_status_bar;  /* The timeout variable below is like a timer object   * or a timer control; every XXX milliseconds it will   * call another function, which actually   * updates the progress bar. Here it's at   * the global level so that you can access it in   * order to stop the motion of the progress bar.   */  gint timeout_1 = 0;  void destroy_main();  void cmd_default_progress_clicked();  void cmd_zero_clicked();  void cmd_stop_clicked();  void cmd_lr_clicked();  void cmd_rl_clicked();  void cmd_bt_clicked();  void cmd_tb_clicked();  void cmd_activity_true_clicked();  void cmd_activity_false_clicked();  void cmd_string_clicked();  /* Below is the only status bar callback. */  void cmd_pop_clicked();  gint main(gint argc, gchar *argv[])  {    GtkWidget *vbox_main;     GtkWidget *cmd_zero;    GtkWidget *cmd_default_progress;  GtkWidget *cmd_stop;  GtkWidget *cmd_lr, *cmd_rl, *cmd_bt, *cmd_tb;  GtkWidget *cmd_activity_true, *cmd_activity_false;  GtkWidget *cmd_string;  GtkWidget *cmd_pop;  gtk_init(&argc, &argv);  frm_bars = gtk_window_new(GTK_WINDOW_TOPLEVEL);  gtk_window_set_title(GTK_WINDOW(frm_bars),                       "Progress and Status Bar(s) Demo");  gtk_signal_connect(GTK_OBJECT(frm_bars),       "destroy",       GTK_SIGNAL_FUNC(destroy_main),       NULL);  vbox_main = gtk_vbox_new(FALSE, 0);  top_progress_bar = gtk_progress_bar_new();  /* You can also create a progress bar with an adjustment widget,   * like you did for the spin buttons in   Listing 2.2   . In that case,   * the instantiation call would be   *   *  gtk_progress_bar_new_with_adjustment(GTK_ADJUSTMENT(adj_widget))  *   * This allows you to set the min, max, and so   * on to be independent of the widgetin   * this case, the progress bar. Because most applications will   * use a 0-100 percent progress bar, you will accept the defaults   * in this example.   */  cmd_zero = gtk_button_new_with_label("Set Progress to 0");  gtk_signal_connect(GTK_OBJECT(cmd_zero),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_zero_clicked),                     NULL);  cmd_default_progress = gtk_button_new_with_label("Default Progress");  gtk_signal_connect(GTK_OBJECT(cmd_default_progress),                     "released",                     GTK_SIGNAL_FUNC(cmd_default_progress_clicked),                     NULL);  cmd_stop = gtk_button_new_with_label("Stop Progress");  gtk_signal_connect(GTK_OBJECT(cmd_stop),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_stop_clicked),                     NULL);  /* The following command buttons control how the progress bar fills,   * from the left, right, top, or bottom.   */  cmd_lr = gtk_button_new_with_label("Progress L to R");  gtk_signal_connect(GTK_OBJECT(cmd_lr),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_lr_clicked),                     NULL);  cmd_rl = gtk_button_new_with_label("Progress R to L");  gtk_signal_connect(GTK_OBJECT(cmd_rl),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_rl_clicked),                     NULL);  cmd_bt = gtk_button_new_with_label("Progress B to T");  gtk_signal_connect(GTK_OBJECT(cmd_bt),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_bt_clicked),                     NULL);  cmd_tb = gtk_button_new_with_label("Progress T to B");  gtk_signal_connect(GTK_OBJECT(cmd_tb),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_tb_clicked),                     NULL);  cmd_activity_true = gtk_button_new_with_label("Activity Mode TRUE");  gtk_signal_connect(GTK_OBJECT(cmd_activity_true),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_activity_true_clicked),                     NULL);  cmd_activity_false = gtk_button_new_with_label("Activity Mode FALSE");  gtk_signal_connect(GTK_OBJECT(cmd_activity_false),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_activity_false_clicked),                     NULL);  cmd_string = gtk_button_new_with_label("Insert String");  gtk_signal_connect(GTK_OBJECT(cmd_string),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_string_clicked),                     NULL);  cmd_pop = gtk_button_new_with_label("Pop Last Status Msg");  gtk_signal_connect(GTK_OBJECT(cmd_pop),                     "clicked",                     GTK_SIGNAL_FUNC(cmd_pop_clicked),                     NULL);    bottom_status_bar = gtk_statusbar_new();  /* Now assemble the interface */  gtk_box_pack_start(GTK_BOX(vbox_main), top_progress_bar, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_zero, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_default_progress, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_stop, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_lr, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_rl, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_bt, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_tb, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_activity_true, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_activity_false, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_string, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), cmd_pop, TRUE, TRUE, 0);     gtk_box_pack_start(GTK_BOX(vbox_main), bottom_status_bar, TRUE, TRUE, 0);     gtk_container_add(GTK_CONTAINER(frm_bars), vbox_main);     gtk_widget_show_all (frm_bars);     gtk_main ();     return 0;  }  void destroy_main()  {   gtk_main_quit(); }  gint update_progress()  {  /* Notice that in the *  _get_value()  call below, the number returned   * is a whole number, not a float between 0 and 1. However, when   * setting the progress bar, the *  _set_value()  function expects   * a number between 0 and 1, which is declared as gfloat if   * a variable is used. Thus, use the divide-by-100 calculation.   */  if (gtk_progress_get_value(GTK_PROGRESS(top_progress_bar)) <= 98)    {      gtk_progress_bar_update(GTK_PROGRESS_BAR(top_progress_bar),                               gtk_progress_get_value                                   (GTK_PROGRESS(top_progress_bar))/100 + .01);     return(TRUE);    }    else    {      gtk_progress_bar_update(GTK_PROGRESS_BAR(top_progress_bar), 1);  /* When a function called by the  gtk_timeout_add  function   * returns false, the timeout is removed.   */  gtk_statusbar_push(GTK_STATUSBAR(bottom_status_bar),                         1, "Done with progress bar update...");       return(FALSE);    }  }  void cmd_default_progress_clicked()  {  /* This is the "basic" action of the progress barupdating   * from 0 to 100% in increments of one.   *   * If you press the "default progress" button multiple times,   * the progress bar will speed up. The reason for this is that   * each time, you create multiple timeout objects, each   * of which operates on the same progress bar. This also   * means you lose your tag to the previous timeout objects because   * only one variable holds the most recent tag:   * the timeout_1 variable.   */  g_print("Starting progress bar update...\n");     gtk_statusbar_push(GTK_STATUSBAR(bottom_status_bar),                         1, "Starting progress bar update...");  /* The code above demonstrates the same thing in two ways.   * (The corresponding "finished" message is in the  update__progress()  * function listed just prior to this one.)   */  timeout_1 = gtk_timeout_add(50, update_progress, NULL);  }  void cmd_stop_clicked()  {    gtk_timeout_remove(timeout_1);  }  void cmd_zero_clicked()  {    gtk_progress_bar_update(GTK_PROGRESS_BAR(top_progress_bar), 0);  }  void cmd_lr_clicked()  {      gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(top_progress_bar),                                      GTK_PROGRESS_LEFT_TO_RIGHT);  }  void cmd_rl_clicked()  {    gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(top_progress_bar),                                      GTK_PROGRESS_RIGHT_TO_LEFT);  }  void cmd_bt_clicked()  {  /* Notice that the shape of the progress bar changes when the   * orientation is set to top-to-bottom or bottom-to-top  .  */  gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(top_progress_bar),                                      GTK_PROGRESS_BOTTOM_TO_TOP);  }  void cmd_tb_clicked()  {    gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(top_progress_bar),                                      GTK_PROGRESS_TOP_TO_BOTTOM);  }  void cmd_activity_true_clicked()  {  /* In activity mode, the progress bar bounces a bar back and   * forth inside the progress bar window. In this demo, this   * must be used with the "Default Progress" button. The activity   * mode also stops when the value of the progress exceeds 1. So,   * to run in progress mode for as long as needed, keep the value   * for the progress bar between 0 and 1.   */  gtk_progress_set_activity_mode(GTK_PROGRESS(top_progress_bar), TRUE);     gtk_statusbar_push(GTK_STATUSBAR(bottom_status_bar),                         1, "Going into Activity Mode...");  }  void cmd_activity_false_clicked()  {    gtk_progress_set_activity_mode(GTK_PROGRESS(top_progress_bar), FALSE);     gtk_statusbar_push(GTK_STATUSBAR(bottom_status_bar),                         1, "Coming out of Activity Mode...");  }  void cmd_string_clicked()  {  /* This places a formatted text string into the progress bar   * window. This string is similar to a C printf string except   * that the  %  -formatter can only be used with the following   * values:   *  %u  - maximum of the progress bar   *  %l  - minimum   *  %p  - percentage of range   *  %v  - value of range   * The example below shows all four, along with some   * plain text.   */  gtk_progress_set_format_string(GTK_PROGRESS(top_progress_bar),                                    "Pct:%p Val:%v Min:%l Max:%u");     gtk_progress_set_show_text(GTK_PROGRESS(top_progress_bar), TRUE);  /* Setting and showing text in a progress bar is a two-step   * process, as shown above. Note that when the progress bar   * is in activity mode, text is not allowed in the progress   * bar; it will disappear until the progress bar is taken   * out of activity mode.   *   * The call below sets the position of the text. The last two   * parameters can range between 0 and 1. 0, 0 is the upper-left   * corner of the progress bar, and 1, 1 is the lower-right corner.   * The first controls the horizontal positioning, and the second   * controls the vertical. Thus, 0, 1 would be the lower-left corner,   * and 1, 0 would be the upper-right corner. The values allowed for   * each are floats between 0 and 1.   */  gtk_progress_set_text_alignment(GTK_PROGRESS(top_progress_bar), 0.5, 0.5);  }  void cmd_pop_clicked()  {  /* The status bar object operates as a stack with corresponding   * push/pop calls. When you want to display a message, you push it   * onto the stack with the  gtk_status_bar_push()  call. It takes three   * parameters: the first is the target status bar, the second is a   * context identifier, and the third is the message to be displayed.   * In this example, all the context identifiers are 1, but if you   * need to access the advanced functionality of the status bar, you   * may want to investigate this further. For example, you could remove   * a message from the stack. For this example, a standard   * last-in-first-out (LIFO) operation will suffice (and from my   * experience, that would fit most business applications also).   *   * Note that the context identifier cannot be zero. In this case, "1"   * is easy to remember because you are either pushing or popping   * one message.   *   * The  gtk_status_bar_pop()  function removes the message highest   * in the stack with the given context identifier. In this example,   * since all context identifiers are 1, the stack operates as pure   * LIFO. However, you could have two or more classes of messages   * and pop a message of either class. If you are not popping   * the context identifier of the top message, though, you are not   * going to see any change in the status bar.   */  gtk_statusbar_pop(GTK_STATUSBAR(bottom_status_bar), 1);  } 
only for RuBoard - do not distribute or recompile


MySQL Building User Interfaces
MySQL: Building User Interfaces (Landmark)
ISBN: 073571049X
EAN: 2147483647
Year: 2001
Pages: 119

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net