Re: gtk_main() at level 0 and gtk_file_chooser_dialog_new



This is the attachment

/*-------------------------------------------------------------------------
 * Filename:      gtk-test.c
 * Version:       0.2
 * Author:        Alan Layec <alan layec cermics enpc fr>
 * Description:   GTK+ gtk_file_chooser_dialog_new example
 * Created at:    Thu Sep 13 16:14:32 2012
 * Modified by:   
 * Modified at:   
 *-----------------------------------------------------------------------*/
/*
 * Compile with:
 *
 * gcc -g -Wall -o gtk-test gtk-test.c `pkg-config gtk+-2.0 --cflags --libs`
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <gtk/gtk.h>

/* function to return in ms time elapsed betweean two time measurement.
 * found @
 * http://stackoverflow.com/questions/361363/how-to-measure-time-in-
milliseconds-using-ansi-c
 */
double timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)
{
  return (double) (((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -
                  ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec))/1e6;
}

/* callback for g_timeout_add */
gint timeout_pause(void *data)
{
  /* we leave the gtk main loop at
   * level 0 in the callback
   */
  gtk_main_quit();
  return TRUE;
}

/* quit_enter_elapsed : function which add a timeout source to the gtk main
 * event loop, run it and display time elapsed in ms between the call of
 * gtk_main and gtk_main_quit
 */
void enter_quit_elapsed(void)
{
  guint tid;
  int delay=5; /* in ms */
  struct timespec start, end;
  double elapsed;
  int i;
  
  for (i=0;i<30;i++) /* do 10 iterations */
  {
    /* add a timeout source to gtk main context */
    tid=g_timeout_add(delay,(GSourceFunc) timeout_pause, NULL);
    
    /* first time measurement*/
    clock_gettime(CLOCK_MONOTONIC, &start);
    
    /* running the gtk main loop
     * the timeout source will quit/stop the gtk event main loop
     * after the delay
     */
    gtk_main();
    
    /* secong time measurement */
    clock_gettime(CLOCK_MONOTONIC, &end);
    
    /* remove timeout source */
    g_source_remove (tid);
    
    /* compute and print time elapsed between gtk_main/gtk_main_quit */
    elapsed=timespecDiff(&end, &start);
    fprintf(stdout,"iter %d - "
                   "gtk_main_level : %d - "
                   "elapsed time : %f ms\n", \
                   i,gtk_main_level(),elapsed);
  }
}

/* main */
int main(int argc, char *argv[])
{
  GtkWidget *dialog;
  
  /* gtk_init */
  gtk_init(&argc, &argv);

  /* first test before the call of gtk_file_chooser_dialog_new */
  fprintf(stdout,"Test before the call of gtk_file_chooser_dialog_new\n");
  enter_quit_elapsed();
  
  /* set a gtk_file_chooser_dialog_new dialog box */
  dialog = gtk_file_chooser_dialog_new ("title",
                                        NULL,
                                        GTK_FILE_CHOOSER_ACTION_OPEN,
                                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                        GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                        NULL);
  
  /* here we show the dialog and we wait tht the user close the window
   * but in fact this is not needed for the purpose of the test
   * -only the first call of gtk_file_chooser_dialog_new is needed-
   */
  gtk_widget_show_all (dialog);
  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
  
  /* second test after the call of gtk_file_chooser_dialog_new */
  fprintf(stdout,"Test after the call of gtk_file_chooser_dialog_new\n");
  enter_quit_elapsed();
    
  exit(0);         
}



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