hanging dialogs



Well, I've since fixed that problem with the bizarre
window behavior I was having trouble with, but now
I'm having trouble with something else. The problem
comes along when I want to update a progress bar
and label in the dialog from the function I'm calling
on every member in a GList (using g_list_foreach()).
The problem is this, when I click on the button, the
function starts to execute, but my dialog hangs and
doesn't get completely drawn until the g_list_foreach()
is completely finished, and then it says 100% for
500 milliseconds, and then closes. So, I don't really
get to see the progress. My busy indicator comes up
and disappears just fine, however. I've even tried adding
a usleep(1) to usleep(10) and it does the same thing, it
just takes longer to finish. :) I've attached some
code below. I was thinking about maybe forking a process
or something , but I don't have a lot of experience
doing this so I'm not sure if it would be the best idea.
Something like this is probably very trivial to many
of you so if I could get a shove in the right direction,
I'd really appreciate it. Thanks a bunch!

Jeff "Shippy" Shipman     E-Mail: shippy nmt edu
Computer Science Major    ICQ: 1786493
New Mexico Institute of Mining and Technology
Homepage: http://www.nmt.edu/~shippy
/* find_meals_start - Begins the process of meal searching. */
void find_meals_start(GtkWidget *widget, gpointer data)
{
   GList *sel = GTK_CLIST(ing_list)->selection,
         *namesel = NULL;
   gchar *text;
   ingredient *ing;

   if(meals == NULL)
   {
      PrintError("No Meals to Search!",
                 "You cannot perform a meal search because\n"
                 "you do not currently have any meals entered.\n"
                 "Please enter meals via the Meal Control tab\n"
                 "and then try your search again.");
      return;
   }
   
   /* Build the name list of ingredients. */
   while(sel != NULL)
   {
      ing = g_malloc(sizeof(ingredient));
      gtk_clist_get_text(GTK_CLIST(ing_list), (int)sel->data, 0, &text);
      ing->name = g_strdup(text);
      namesel = g_list_append(namesel, ing);
      sel = g_list_next(sel);
   }
   gtk_clist_clear(GTK_CLIST(meal_list));

   gdk_window_set_cursor(main_win->window, gdk_cursor_new(GDK_WATCH));
   g_list_foreach(meals, conduct_search, (gpointer)namesel);
   gtk_timeout_add(500, dialog_hide, widget);
   gdk_window_set_cursor(main_win->window, gdk_cursor_new(GDK_TOP_LEFT_ARROW));
}


/* conduct_search - Does the actual meal search. */
void conduct_search(gpointer data, gpointer names)
{
   GtkWidget *pbar = gtk_object_get_data(GTK_OBJECT(FindMealsDlg), "pbar"),
             *slabel = gtk_object_get_data(GTK_OBJECT(FindMealsDlg), "slabel");
   gchar *entry[1], *slabel_text;
   GList *namesel = names;
   meal *m = (meal *)data;
   ingredient *ing;
   gint cur_meal = g_list_index(meals, m)+1,
        length = g_list_length(meals);
   gint i;
   
   /* Update the progress label */
   slabel_text = g_strdup_printf("Searching meal %d of %d.", cur_meal, length);
   gtk_label_set_text(GTK_LABEL(slabel), (gchar *)slabel_text);
   
   /* Search to make sure each ingredient in the meal *
    * is in the selection list.                       */
   for(i=0;i<g_list_length(m->ings);i++)
   {
      ing = g_list_nth_data(m->ings, i);
      
      if(ing->opt) continue; /* We skip optional ingredients */
      if(g_list_find_custom(namesel, ing, ing_cmp) == NULL)
      {
         break;
      }
   }
   
   /* If all ingredients were found, add it to the list! */
   if(i == g_list_length(m->ings))
   {
      entry[0] = g_strdup(m->name);
      gtk_clist_append(GTK_CLIST(meal_list), entry);
   }

   /* Update the progress percentage. */
   gtk_progress_bar_update(GTK_PROGRESS_BAR(pbar), (gfloat)cur_meal/(gfloat)length);
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]