simple - draw with interval



  Hi All,
It's simple example below which draws arrows up and down
after klicked button. I want to rewrite this example:
after toggle-on button - arrows will apper up,down,up,down...
with 2 seconds interval. When I clicked toggle-of
there will be only one arrow(up or down) which was in that
time.

Thanks,
Michal Grabos
mailto: mgrabos student uci agh edu pl

//******example******
#include <gtk/gtk.h>

static GdkPixmap *pixmap = NULL;
static GtkWidget *drawing_area;

void delete_event(GtkWidget *widget, GdkEvent *event, gpointer data){
  gtk_main_quit();
}

static gint configure_event(GtkWidget *widget,
                            GdkEventConfigure *event){
  if (pixmap)
    gdk_pixmap_unref(pixmap);
  pixmap = gdk_pixmap_new (widget->window,
                           widget->allocation.width,
                           widget->allocation.height,
                           -1);
  gdk_draw_rectangle(pixmap,
                     widget->style->white_gc,
                     TRUE,
                     0,0,
                     widget->allocation.width,
                     widget->allocation.height);                            
  return TRUE;
}

static gint expose_event(GtkWidget *widget,
                         GdkEventExpose *event){
  gdk_draw_pixmap(widget->window,
                  widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
                  pixmap,
                  event->area.x, event->area.y,
                  event->area.x, event->area.y,
                  event->area.width, event->area.height);
  return FALSE;          
}

static void draw_up(GtkWidget *widget,
                       gdouble x,
                       gdouble y){
                       
  GdkRectangle update_rect;                    
  
  update_rect.x = x;
  update_rect.y = y;
  update_rect.width = 50;
  update_rect.height = 50;
  gdk_draw_rectangle(pixmap,
                     widget->style->black_gc,
                     TRUE,
                     update_rect.x, update_rect.y,
                     update_rect.width, update_rect.height);
                     
  gdk_draw_line (pixmap,widget->style->white_gc,x+25,y+2,x+10,y+25);
  gdk_draw_line (pixmap,widget->style->white_gc,x+25,y+2,x+40,y+25);
  gdk_draw_line (pixmap,widget->style->white_gc,x+25,y+2,x+25,y+48);
  
  gtk_widget_queue_draw_area (widget,
                              update_rect.x,update_rect.y,
                              update_rect.width,update_rect.height);
}

static void draw_down(GtkWidget *widget,
                       gdouble x,
                       gdouble y){
                       
  GdkRectangle update_rect;                    
  
  update_rect.x = x;
  update_rect.y = y;
  update_rect.width = 50;
  update_rect.height = 50;
  gdk_draw_rectangle(pixmap,
                     widget->style->white_gc,
                     TRUE,
                     update_rect.x, update_rect.y,
                     update_rect.width, update_rect.height);
                     
  gdk_draw_line (pixmap,widget->style->black_gc,x+25,y+48,x+10,y+25);
  gdk_draw_line (pixmap,widget->style->black_gc,x+25,y+48,x+40,y+25);
  gdk_draw_line (pixmap,widget->style->black_gc,x+25,y+48,x+25,y+2);
                                       
  gtk_widget_queue_draw_area (widget,
                              update_rect.x,update_rect.y,
                              update_rect.width,update_rect.height);
}

static gint toggle(GtkWidget *widget, GdkEvent *event){

  if (GTK_TOGGLE_BUTTON(widget)->active){
    draw_up(drawing_area,0,0);
  }
  else{
    draw_down(drawing_area,0,0);
  }
  return TRUE;
}



int main(int argc, char *argv[]){

  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *button;

  gint return_val;
  GdkEvent event;

  gtk_init (&argc, &argv);
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window),"Example.");
  gtk_window_set_default_size(GTK_WINDOW(window),67,90);
  gtk_signal_connect(GTK_OBJECT(window),"delete_event",
                     GTK_SIGNAL_FUNC(delete_event),NULL);
  gtk_window_set_policy(GTK_WINDOW(window),TRUE,TRUE,TRUE);
  gtk_container_set_border_width(GTK_CONTAINER(window),10);
  
  vbox = gtk_vbox_new(FALSE,0);
  
  button = gtk_toggle_button_new_with_label("press");
  gtk_box_pack_end(GTK_BOX(vbox),button,TRUE,FALSE,0);
  gtk_signal_connect(GTK_OBJECT(button),"clicked",
                     GTK_SIGNAL_FUNC(toggle),
                     NULL);
  gtk_widget_show(button);
  
  drawing_area = gtk_drawing_area_new();
  gtk_drawing_area_size(GTK_DRAWING_AREA(drawing_area),50,50);
  gtk_box_pack_end(GTK_BOX(vbox),drawing_area,TRUE,TRUE,0);
  gtk_signal_connect(GTK_OBJECT(drawing_area),"expose_event",
                     GTK_SIGNAL_FUNC(expose_event),NULL);
  gtk_signal_connect(GTK_OBJECT(drawing_area),"configure_event",
                     GTK_SIGNAL_FUNC(configure_event),NULL);
  gtk_widget_show(drawing_area);
  
  gtk_container_add(GTK_CONTAINER(window),vbox);
  
  gtk_widget_show(vbox);
  
  gtk_widget_show(window);
  
  gtk_signal_emit(GTK_OBJECT(button), 
                  gtk_signal_lookup("clicked",GTK_OBJECT_TYPE(button)),
                  &event, &return_val);
  
  gtk_main();

  return(0);
}




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