loading ...
loading...

2006-12-05 | Using gtk Write a clock widget without cairo

分享

#include <gtk/gtk.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <gdk/gdk.h>
#include <linux/kd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
int beep(int sec)
{
 int frequency=1000+sec*25;
 int duration=160;
 if(sec==60)
 {
  frequency=1500;
  duration=320;
 }
 int fd=open("/dev/tty0",O_RDWR|O_NDELAY);

 if (fd<0)
 {
  perror("/dev/tty0");
  return 1;
 }
  /* 125 = 125 milliseconds, or 1/8th of a second;
      0x637 is the number of clock cycles in the standard kernel beep */
 if (ioctl(fd,KDMKTONE,(duration<<16)+1193180/frequency) == -1)
 {
  perror("/dev/tty0: ioctl");
  return 1;
 }

 close(fd);
 return 0;
}
static gboolean on_expose_event(gpointer data)
{
 GtkWidget *clock=GTK_WIDGET(data);
/*
 gcarc1--The filled arc's gc
 gcarc2---The arc edge's gc
 gcquar---The quarter's gc
*/
 GdkGC *gcarc1,*gcarc2,*gcquar;
/*
 gch--The hour hand's gc
 gcm--The minute hand's gc
 gcs--The second hand's gc
*/
 GdkGC *gch,*gcm,*gcs;
 GdkColor color;
 int  i;
 float radius=200;
 gcarc1 = gdk_gc_new (clock->window);
  color.red = 65535;
  color.green = 65535;
  color.blue = 65535;
 gdk_gc_set_rgb_fg_color (gcarc1, &color);
 gdk_draw_arc(clock->window,gcarc1,TRUE,0,0,radius*2,radius*2,0,360*64);

 gcarc2= gdk_gc_new (clock->window);
  color.red = 0;
  color.green = 0;
  color.blue = 0;
 gdk_gc_set_rgb_fg_color (gcarc1, &color);
 gdk_draw_arc(clock->window,gcarc1,FALSE,0,0,radius*2,radius*2,0,360*64);

 gcquar = gdk_gc_new (clock->window);
  color.red = 0;
  color.green =0;
  color.blue = 0;
 gdk_gc_set_rgb_fg_color (gcquar, &color);

 for(i=0;i<60;i++)
 {
  int inset;
  if (i % 15 == 0)
  {
    inset = 0.3 * radius;
  }
  else
  {
    if(i % 5 ==0)
     inset = 0.15 *radius;
    else inset = 0.05 * radius;
  }
 gdk_draw_line(clock->window,gcquar,
  radius+radius*sin(2*M_PI/60*i),
  radius-radius*cos(2*M_PI/60*i),
  radius+(radius-inset)*sin(2*M_PI/60*i),
  radius-(radius-inset)*cos(2*M_PI/60*i)
  );
 }

 gch = gdk_gc_new (clock->window);
  color.red = 65535;
  color.green = 0;
  color.blue = 0;
 gdk_gc_set_rgb_fg_color (gch, &color);

 gcm = gdk_gc_new (clock->window);
  color.red = 0;
  color.green = 65535;
  color.blue = 0;
 gdk_gc_set_rgb_fg_color (gcm, &color);

 gcs = gdk_gc_new (clock->window);
  color.red = 0;
  color.green = 0;
  color.blue = 0;
 gdk_gc_set_rgb_fg_color (gcs, &color);
 
 time_t timet;
 struct tm tm;
 time(&timet);
 localtime_r(&timet,&tm);

 //draw sec hand
 gdk_draw_line(clock->window,gcs,radius,radius,
  radius+(radius*4/5)*sin(2*M_PI*tm.tm_sec/60),
  radius-(radius*4/5)*cos(2*M_PI*tm.tm_sec/60)
  );

 //draw min hand

 gdk_draw_line(clock->window,gcm,radius,radius,
  radius+(radius*3/5)*sin(2*M_PI*tm.tm_min/60+(2*M_PI/60)*tm.tm_sec/60),
  radius-(radius*3/5)*cos(2*M_PI*tm.tm_min/60+(2*M_PI/60)*tm.tm_sec/60)
            );

 //draw hour hand
 gdk_draw_line(clock->window,gch,radius,radius,
  radius+(radius*2/5)*sin(2*M_PI*tm.tm_hour/12+(2*M_PI/12)*tm.tm_min/60),
  radius-(radius*2/5)*cos(2*M_PI*tm.tm_hour/12+(2*M_PI/12)*tm.tm_min/60)
            );

 if(tm.tm_min==59)
 {
  if(tm.tm_sec==0)
   beep(60);
  else
  {
   if(tm.tm_sec>55)
    beep(tm.tm_sec);
  }
 }
 g_object_unref(gch);
 g_object_unref(gcm);
 g_object_unref(gcs);
 g_object_unref(gcarc1);
 g_object_unref(gcarc2);
 g_object_unref(gcquar);
 return TRUE;
}
static gboolean updatetime(gpointer data)
{
 
 return  on_expose_event(data);
}
 int   main (int argc, char **argv)
{
 gtk_init(&argc,&argv);
 GtkWidget *window;
 GtkWidget *clock;

 gtk_init (&argc, &argv);
 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_MOUSE);
 
 GtkWidget *fixed=gtk_fixed_new();
 gtk_container_add (GTK_CONTAINER (window), fixed);
 gtk_widget_show(fixed);

 clock = gtk_drawing_area_new();
 gtk_fixed_put(GTK_FIXED(fixed),clock,0,0);
 gtk_widget_set_size_request(clock,600,480);
 g_signal_connect (window, "destroy",G_CALLBACK (gtk_main_quit), NULL);
  g_signal_connect (clock, "expose_event",
   G_CALLBACK (on_expose_event), NULL);

 g_timeout_add(1000,updatetime,clock);
  gtk_widget_show_all (window);
 gtk_main ();
}
和有cairo渲染的来相比,有明显的差距

效果图:

分享 分享 |  评论 (2) |  阅读 (?)  |  固定链接 |  类别 (gtk) |  发表于 17:11
搜狐博客温馨提示:警惕博客留言诈骗, 搜狐博客管理员的正确地址为http://admin.blog.sohu.com, 其他都是冒牌。搜狐博客官方不会要求参加活动的各位博友缴纳任何的手续费用。请勿轻信留言、评论中的中奖信息,更不要拨打陌生电话及向陌生帐户汇款,谨防受骗!识别更多网络骗术,请 点击查看详情
正在读取评论信息...
您还未登录,只能匿名发表评论。或者您可以 登录 后发表。
 
  一个单亲妈妈的心愿:治好7岁儿子的白血病
表  情:
加载中...
回复通知: 同时用小纸条通知对方该回复