⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.c

📁 一个linux下的时钟软件
💻 C
字号:
#include <gtk/gtk.h>#include <time.h>#include <math.h>#include <string.h>#include <stdio.h>#include <unistd.h>#include "clock.h"#include "support.h"#include "function.h"#ifndef M_PI#define M_PI 3.141592654#endifMyClock ClockData;MyClock *pclock = &ClockData;gboolean	isclear = FALSE;extern GdkPixmap *gdkxmp[11];extern GdkPixmap *RingPixmap;////////////////////////////////////////////////////////User's  function define///////////////////////////////////////////////////////////////////void clock_init ( GtkWidget *widget){  time_t ticks;  struct tm *tm;  gint h, m, s;  gint	  i;
  GdkColor   newcolor;
  GtkStyle   *newstyle;
  GtkWidget *drawingarea;  ticks = time (0);  tm = localtime (&ticks);  h = tm->tm_hour;  m = tm->tm_min;  s = tm->tm_sec;  pclock->hrs_angle = 2.5 * M_PI - (h % 12) * M_PI / 6 - m * M_PI / 360;  pclock->min_angle = 2.5 * M_PI - m * M_PI / 30;  pclock->sec_angle = 2.5 * M_PI - s * M_PI / 30;  pclock->timer = 0;  pclock->mode = CLOCK_ANALOG;//  pclock->shadow_type = GTK_SHADOW_IN;
//  pclock->display_am_pm = TRUE;	/* display am or pm by default. */  pclock->display_secs = TRUE;		/* display the seconde's pointer */  pclock->interval = 300;			/* 原来为1/10 seconds update interval by default */  drawingarea = lookup_widget ( GTK_WIDGET ( widget), "Drawingarea");  newstyle = gtk_style_copy (gtk_widget_get_style (drawingarea));
  newcolor.red = 1*0xffff;
  newcolor.green = 1*0xffff;
  newcolor.blue = 1*0xffff;
  for (i = 0; i < 5; i++)
	newstyle->bg[i] = newcolor;
  gtk_widget_set_style (drawingarea, newstyle);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////static void draw_hrs_pointer (GtkWidget * widget){  GdkPoint points[5];  gint xc, yc;  gdouble s, c;  g_return_if_fail (widget != NULL);  xc = widget->allocation.width / 2;  yc = widget->allocation.height / 2;  s = sin (pclock->hrs_angle);  c = cos (pclock->hrs_angle);  points[0].x = xc + s * pclock->pointer_width / 4;  points[0].y = yc + c * pclock->pointer_width / 4;  points[1].x = xc + 6 * c * pclock->radius / 16;  points[1].y = yc - 6 * s * pclock->radius / 16;  points[2].x = xc - s * pclock->pointer_width / 4;  points[2].y = yc - c * pclock->pointer_width / 4;  points[3].x = xc - c * pclock->pointer_width / 4;  points[3].y = yc + s * pclock->pointer_width / 4;  points[4].x = xc + s * pclock->pointer_width / 4;  points[4].y = yc + c * pclock->pointer_width / 4;  if( !isclear)       gdk_draw_polygon (widget->window, widget->style->text_gc[GTK_STATE_NORMAL], TRUE, points, 5);  else       gdk_draw_polygon (widget->window, widget->style->white_gc, TRUE, points, 5);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////static void draw_min_pointer (GtkWidget * widget){  GdkPoint points[5];  gint xc, yc;  gdouble s, c;  g_return_if_fail (widget != NULL);  xc = widget->allocation.width / 2;  yc = widget->allocation.height / 2;  s = sin (pclock->min_angle);  c = cos (pclock->min_angle);  points[0].x = xc + s * pclock->pointer_width / 4;  points[0].y = yc + c * pclock->pointer_width / 4;  points[1].x = xc + c *8*pclock->radius/16;  points[1].y = yc - s *8*pclock->radius/16;  points[2].x = xc - s * pclock->pointer_width / 4;  points[2].y = yc - c * pclock->pointer_width / 4;  points[3].x = xc - c * pclock->pointer_width / 4;  points[3].y = yc + s * pclock->pointer_width / 4;  points[4].x = xc + s * pclock->pointer_width / 4;  points[4].y = yc + c * pclock->pointer_width / 4;  if( !isclear)		gdk_draw_polygon (widget->window, widget->style->text_gc[GTK_STATE_NORMAL], TRUE, points, 5);  else		gdk_draw_polygon (widget->window, widget->style->white_gc, TRUE, points, 5);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////static void draw_sec_pointer (GtkWidget * widget){  GdkPoint points[5];  gint xc, yc;  gdouble s, c;  GdkColor color;  static GdkColormap *colormap = NULL;  static GdkGC	*gc = NULL;  g_return_if_fail (widget != NULL);  xc = widget->allocation.width / 2;  yc = widget->allocation.height / 2;  s = sin (pclock->sec_angle);  c = cos (pclock->sec_angle);  points[0].x = xc +  s*pclock->pointer_width / 6;  points[0].y = yc +  c*pclock->pointer_width / 6;  points[1].x = xc + (c *6* pclock->radius+4)/8;  points[1].y = yc - (s *6* pclock->radius+4)/8;  points[2].x = xc -  s*pclock->pointer_width / 6;  points[2].y = yc -  c*pclock->pointer_width / 6;  points[3].x = xc -  c*pclock->pointer_width / 6;  points[3].y = yc +  s*pclock->pointer_width / 6;  points[4].x = xc +  s*pclock->pointer_width / 6;  points[4].y = yc +  c*pclock->pointer_width / 6;
  if ( colormap == NULL)  {	colormap = gdk_colormap_get_system ();	gc = gdk_gc_new (widget->window);	color.red = 65535;	color.green = 13084;	color.blue = 24563;	gdk_color_alloc ( colormap, &color);	gdk_gc_set_foreground (gc, &color);  }  if ( isclear)	gdk_draw_polygon (widget->window, widget->style->white_gc,TRUE, points, 5);  else	gdk_draw_polygon (widget->window, gc,TRUE, points, 5);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////static void draw_digits (GtkWidget * widget, gint x, gint y, gchar c){  g_return_if_fail (widget != NULL);  switch (c)    {	case '0' : gdk_draw_pixmap (widget->window, widget->style->white_gc, gdkxmp[0],
								0, 0, x, y, digits_width, digits_height);				break;	case '1' : gdk_draw_pixmap (widget->window, widget->style->white_gc, gdkxmp[1],
								0, 0, x, y, digits_width, digits_height);				break;	case '2' : gdk_draw_pixmap (widget->window, widget->style->white_gc, gdkxmp[2],
								0, 0, x, y, digits_width, digits_height);				break;	case '3' : gdk_draw_pixmap (widget->window, widget->style->white_gc, gdkxmp[3],
								0, 0, x, y, digits_width, digits_height);				break;	case '4' : gdk_draw_pixmap (widget->window, widget->style->white_gc, gdkxmp[4],
								0, 0, x, y, digits_width, digits_height);				break;	case '5' : gdk_draw_pixmap (widget->window, widget->style->white_gc, gdkxmp[5],
								0, 0, x, y, digits_width, digits_height);				break;	case '6' : gdk_draw_pixmap (widget->window, widget->style->white_gc, gdkxmp[6],
								0, 0, x, y, digits_width, digits_height);				break;	case '7' : gdk_draw_pixmap (widget->window, widget->style->white_gc, gdkxmp[7],
								0, 0, x, y, digits_width, digits_height);				break;	case '8' : gdk_draw_pixmap (widget->window, widget->style->white_gc, gdkxmp[8],
								0, 0, x, y, digits_width, digits_height);				break;	case '9' : gdk_draw_pixmap (widget->window, widget->style->white_gc, gdkxmp[9],
								0, 0, x, y, digits_width, digits_height);				break;	case ':' : gdk_draw_pixmap (widget->window, widget->style->white_gc, gdkxmp[10],
								0, 0, x, y, maohao_width, maohao_height);    }}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////static void clock_draw_leds (GtkWidget * widget){  time_t ticks;  struct tm *tm;  gint x, y, h, m, s;  guint len, i;  gchar time_buf[8];  g_return_if_fail (widget != NULL);  ticks = time (0);  tm = localtime (&ticks);  h = tm->tm_hour;  m = tm->tm_min;  s = tm->tm_sec;  if (!(pclock->military_time))    {	if (h > 12)	  h -= 12;	if (h == 0)	  h = 12;    }  sprintf (time_buf, "%02d%c%02d%c", h, ':', m, '\0');  len = strlen (time_buf);  x = 15;		//(widget->allocation.width - (digits_width * (leng-1)+maohao_width)) / 2;  y = 60;		//(widget->allocation.height - digits_height) / 2;  if (time_buf[0] == '0')
  {    x = x + digits_width / 2;//三位数字时刷新四位数字的边框余点
	gdk_draw_rectangle (widget->window, widget->style->black_gc, TRUE,//数字的背景色原为白色现改为黑色		0, (widget->allocation.height - digits_height) / 2 - 20, widget->allocation.width, digits_height + 40);
  }  for (i = 0; i < len; i++)    {	gint j = i;	if (time_buf[0] == '0')	  j--;	if (((i == 0)&&(time_buf[0]!='0')) || (i==1))	  draw_digits (widget, x+j*digits_width, y, time_buf[i]);	if (i == 3 || i ==4)	  draw_digits (widget, ((x+j*digits_width)-(maohao_width/2)), y, time_buf[i]);    }  if (time_buf[0] == '0')    x = x + digits_width;  else    x = x + digits_width * 2;  if (s & 1)    draw_digits (widget, x, y, time_buf[2]);  else	gdk_draw_rectangle (widget->window, widget->style->black_gc, TRUE, x, y, maohao_width, maohao_height);//数字的背景色原为白色现改为黑色因此屏蔽此句}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////static void clock_draw_analog (GtkWidget * widget){	g_return_if_fail (widget != NULL);  	draw_hrs_pointer (widget);	draw_min_pointer (widget);	if (pclock->display_secs)		draw_sec_pointer (widget);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void clock_draw_internal (GtkWidget * widget){  g_return_if_fail (widget != NULL);  if (GTK_WIDGET_DRAWABLE (widget))  {    switch (pclock->mode)    {    case CLOCK_ANALOG:      clock_draw_analog (widget);      break;    case CLOCK_LEDS:      clock_draw_leds (widget);//用于刷新上午、下午、晚上等文字      gdk_draw_rectangle (widget->window, widget->style->black_gc, TRUE,						  0, (widget->allocation.height - digits_height) / 2 + digits_height + 20,
						  widget->allocation.width, 40);//数字的背景色原为白色现改为黑色      if (!pclock->military_time)		leds_clock_draw_string (widget, 5, 160);      gdk_draw_pixmap (widget->window, widget->style->white_gc, RingPixmap, 0, 0, 10, 10, 15, 18);      break;    default:      clock_draw_analog (widget);    }  }}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////gint clock_timer (GtkWidget * widget){  time_t ticks;  gint	 h, m, s;  struct tm *tm;  static gint oh = 0, om = 0, os = 0;  static gboolean sem = FALSE;  g_return_val_if_fail (widget != NULL, FALSE);  if (sem)    return TRUE;  sem = TRUE;  ticks = time (0);  tm = localtime (&ticks);  h = tm->tm_hour;  m = tm->tm_min;  s = tm->tm_sec;  if (!(((!((pclock->display_secs) || (pclock->mode==CLOCK_LEDS))) || (s==os)) && (m==om) && (h==oh)))  {    isclear = TRUE;    clock_draw_internal (widget);    os = s;    om = m;    oh = h;    pclock->hrs_angle = 2.5 * M_PI - (h % 12) * M_PI / 6 - m * M_PI / 360;    pclock->min_angle = 2.5 * M_PI - m * M_PI / 30;    pclock->sec_angle = 2.5 * M_PI - s * M_PI / 30;    isclear = FALSE;    clock_draw_internal (widget);  }  sem = FALSE;  return TRUE;}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -