1 /*****************************************************************************/
2 /* EyeClock - A clock application for X. The eyes follow the mouse pointer.  */
3 /*                                                                           */
4 /* EyeClock Copyright (C) 1998-1999 Sakai Hiroaki.                           */
5 /* All Rights Reserved.                                                      */
6 /*                                                                           */
7 /* EyeClock is a simple clock application for X11. You can put your favorite */
8 /* picture on the face of the clock. And you can move the eyes of the        */
9 /* picture following the mouse pointer.                                      */
10 /*                                                                           */
11 /* This file is a part of EyeClock.                                          */
12 /* EyeClock is FREE SOFTWARE. You can use, copy, modify, and/or redistribute */
13 /* it freely under the terms in the COPYRIGHT.                               */
14 /* See the COPYRIGHT for more details.                                       */
15 /*****************************************************************************/
16 
17 /*****************************************************************************/
18 /* Number ���������å�                                                       */
19 /* (����)                                                                    */
20 /*===========================================================================*/
21 /* ���Υե����������                                                        */
22 /*---------------------------------------------------------------------------*/
23 /* ���Υե�����ϡ����������åȤ����ΤǤ���                                  */
24 /*****************************************************************************/
25 
26 /*****************************************************************************/
27 /* ��������                                                                  */
28 /*****************************************************************************/
29 
30 #include "NumberP.h"
31 
32 /*****************************************************************************/
33 /* �إå��ե�����Υ����롼��                                              */
34 /*****************************************************************************/
35 
36 /* None */
37 
38 /*****************************************************************************/
39 /* ��������                                                                */
40 /*****************************************************************************/
41 
42 #ifndef M_PI
43 #define M_PI 3.1415926535
44 #endif
45 
46 /*****************************************************************************/
47 /* �������                                                                  */
48 /*****************************************************************************/
49 
50 /* None */
51 
52 /*****************************************************************************/
53 /* �����ѿ������                                                            */
54 /*****************************************************************************/
55 
56 /*===========================================================================*/
57 /* ���������åȤΥ��饹�����Τγ���                                          */
58 /*===========================================================================*/
59 
60 WidgetClass numberWidgetClass = (WidgetClass)(&numberClassRec);
61 
62 /*===========================================================================*/
63 /* �꥽���������                                                            */
64 /*===========================================================================*/
65 
66 static XtResource resources[] = {
67   {XtNgeometry, XtCGeometry, XtRWidgetCorePart, sizeof(CorePart),
68    XtOffsetOf(NumberRec, core), XtRString, "0x0+0+0"},
69   {XtNactive, XtCActive, XtRBoolean, sizeof(Boolean),
70    XtOffsetOf(NumberRec, number.active), XtRString, "True"},
71 
72   {XtNnumber, XtCNumber, XtRInt, sizeof(int),
73    XtOffsetOf(NumberRec, number.number), XtRString, "0"},
74 
75   {XtNlength, XtCLength, XtRDimension, sizeof(Dimension),
76    XtOffsetOf(NumberRec, number.length), XtRString, "85"},
77   {XtNfigure, XtCFigure, XtRPixmap, sizeof(Pixmap),
78    XtOffsetOf(NumberRec, number.figure), XtRString, "None"},
79   {XtNmask, XtCMask, XtRPixmap, sizeof(Pixmap),
80    XtOffsetOf(NumberRec, number.mask), XtRString, "None"},
81 
82   {XtNsize, XtCSize, XtRXPoint, sizeof(XPoint),
83    XtOffsetOf(NumberRec, number.size), XtRString, "0x0"},
84 
85   {XtNfaceSize, XtCFaceSize, XtRInt, sizeof(int),
86    XtOffsetOf(NumberRec, number.face_size), XtRString, "64"},
87   {XtNfaceWidth, XtCFaceWidth, XtRInt, sizeof(int),
88    XtOffsetOf(NumberRec, number.face_width), XtRString, "64"},
89   {XtNfaceHeight, XtCFaceHeight, XtRInt, sizeof(int),
90    XtOffsetOf(NumberRec, number.face_height), XtRString, "64"},
91   {XtNratio, XtCRatio, XtRInt, sizeof(int),
92    XtOffsetOf(NumberRec, number.ratio), XtRString, "100"},
93   {XtNratioX, XtCRatioX, XtRInt, sizeof(int),
94    XtOffsetOf(NumberRec, number.ratio_x), XtRString, "100"},
95   {XtNratioY, XtCRatioY, XtRInt, sizeof(int),
96    XtOffsetOf(NumberRec, number.ratio_y), XtRString, "100"}};
97 
98 /*===========================================================================*/
99 /* ���������åȤΥ��饹�����                                                */
100 /*===========================================================================*/
101 
102 static void ClassInitialize();
103 static void ClassPartInitialize(WidgetClass widgetClass);
104 
105 static void Initialize();
106 static void Realize(Widget w, XtValueMask * value_mask,
107 		    XSetWindowAttributes * attrs);
108 static void Destroy(Widget w);
109 static void Resize(Widget w);
110 static void Redisplay(Widget w, XEvent * event, Region region);
111 
112 NumberClassRec numberClassRec = {
113   { /* core �ե������ */
114     &widgetClassRec,        /* �����ѡ����饹 */
115     "Number",               /* ���饹̾ */
116     sizeof(NumberRec),      /* ������ */
117     ClassInitialize,        /* ���饹�ν���� */
118     NULL,                   /* class part �ν���� */
119     FALSE,                  /* class_inited */
120     Initialize,             /* ���������ν���� */
121     NULL,                   /* initialize_hook */
122     Realize,                /* realize */
123     NULL,                   /* actions */
124     0,                      /* actions �ο� */
125     resources,              /* �꥽���� */
126     XtNumber(resources),    /* �꥽�����ο� */
127     NULLQUARK,              /* xrm_class */
128     TRUE,                   /* motion ���� */
129     TRUE,                   /* exposure ���� */
130     TRUE,                   /* enterleave ���� */
131     FALSE,                  /* visible_interest */
132     Destroy,                /* ���������κ�� */
133     Resize,                 /* �ꥵ���� */
134     Redisplay,              /* �������ݡ��� */
135     NULL,                   /* set_values */
136     NULL,                   /* set_values_hook */
137     NULL,                   /* set_values_almost */
138     NULL,                   /* get_values_hook */
139     NULL,                   /* accept_focus */
140     XtVersion,              /* �С������ */
141     NULL,                   /* callback_offsets */
142     NULL,                   /* tm_table */
143     XtInheritQueryGeometry, /* query_geometry */
144     NULL,                   /* display_accelerator */
145     NULL                    /* extension */
146   },
147   { /* number �ե������ */
148     0                       /* dummy */
149   }
150 };
151 
152 /*****************************************************************************/
153 /* �ؿ�������                                                                */
154 /*****************************************************************************/
155 
ClassInitialize()156 static void ClassInitialize()
157 {
158   /* �꥽����������С�������Ͽ */
159   XtSetTypeConverter(XtRString, XtRXPoint, ConvertStringToPoint,
160 		     NULL, 0, XtCacheNone, NULL);
161   XtSetTypeConverter(XtRString, XtRBitmap, ConvertStringToPixmap,
162                      NULL, 0, XtCacheNone, NULL);
163   XtSetTypeConverter(XtRString, XtRPixmap, ConvertStringToPixmap,
164 		     NULL, 0, XtCacheNone, NULL);
165   XtSetTypeConverter(XtRPixmapData, XtRPixmap, ConvertPixmapDataToPixmap,
166 		     NULL, 0, XtCacheNone, NULL);
167   XtSetTypeConverter(XtRString, XtRWidgetCorePart,
168 		     ConvertGeometryStringToPoint, NULL, 0,
169 		     XtCacheNone, NULL);
170 }
171 
ClassPartInitialize(WidgetClass widgetClass)172 static void ClassPartInitialize(WidgetClass widgetClass) { /* None */ }
173 
Initialize(Widget req,Widget new,ArgList args,Cardinal num)174 static void Initialize(Widget req, Widget new, ArgList args, Cardinal num)
175 {
176   NumberWidget nw = (NumberWidget)new;
177   Display * d;
178   Window r;
179 
180   if (!nw->number.active) return;
181 
182   d = XtDisplay(new);
183   r = DefaultRootWindow(d);
184 
185   if (!nw->number.figure) {
186     nw->number.active = False;
187     return;
188   }
189 
190   if (nw->number.size.x <= 0)
191     nw->number.size.x =
192       new->core.width  ? new->core.width  : GetWidth( d, nw->number.figure);
193 
194   if (nw->number.size.y <= 0)
195     nw->number.size.y =
196       new->core.height ? new->core.height : GetHeight(d, nw->number.figure);
197 
198   new->core.width  = nw->number.size.x;
199   new->core.height = nw->number.size.y;
200 
201   if ((nw->number.size.x != GetWidth( d, nw->number.figure)) ||
202       (nw->number.size.y != GetHeight(d, nw->number.figure))) {
203     nw->number.figure = ResizePixmap(d, r, nw->number.figure,
204 				     nw->number.size.x, nw->number.size.y);
205   }
206 
207   /* GC �κ��� */
208   nw->number.gc_black = XCreateGC(d, DefaultRootWindow(d), 0, 0);
209   nw->number.gc_white = XCreateGC(d, DefaultRootWindow(d), 0, 0);
210   nw->number.gc_copy_and    = XCreateGC(d, DefaultRootWindow(d), 0, 0);
211   nw->number.gc_copy_andinv = XCreateGC(d, DefaultRootWindow(d), 0, 0);
212   nw->number.gc_copy_or     = XCreateGC(d, DefaultRootWindow(d), 0, 0);
213   XSetForeground(d, nw->number.gc_black, BlackPixel(d, DefaultScreen(d)));
214   XSetForeground(d, nw->number.gc_white, WhitePixel(d, DefaultScreen(d)));
215   XSetFunction(d, nw->number.gc_copy_and,    GXand);
216   XSetFunction(d, nw->number.gc_copy_andinv, GXandInverted);
217   XSetFunction(d, nw->number.gc_copy_or,     GXor);
218 
219   /* �ޥ���������̵�����ˤϡ���ưŪ�˺������� */
220   if (!nw->number.mask)
221     nw->number.mask = CreateMaskPixmap(d, r, nw->number.figure,
222 				       nw->number.gc_black,
223 				       nw->number.gc_white);
224   if (nw->number.mask) {
225     if ((GetWidth( d, nw->number.mask) != nw->number.size.x) ||
226 	(GetHeight(d, nw->number.mask) != nw->number.size.y))
227       nw->number.mask = ResizePixmap(d, r, nw->number.mask,
228 				     nw->number.size.x, nw->number.size.y);
229     XCopyArea(d, nw->number.mask, nw->number.figure,
230 	      nw->number.gc_copy_andinv,
231 	      0, 0, nw->number.size.x, nw->number.size.y, 0, 0);
232   }
233 
234   nw->number.resized_figure = 0;
235   nw->number.resized_mask   = 0;
236 }
237 
Realize(Widget w,XtValueMask * value_mask,XSetWindowAttributes * attrs)238 static void Realize(Widget w, XtValueMask * value_mask,
239 		    XSetWindowAttributes * attrs)
240 {
241   NumberWidget nw = (NumberWidget)w;
242 
243   if (!nw->number.active) return;
244 
245   /* if (!nw->number.resized_figure) Resize(w); */
246 }
247 
Destroy(Widget w)248 static void Destroy(Widget w)
249 {
250   NumberWidget nw = (NumberWidget)w;
251   Display * d;
252 
253   if (!nw->number.active) return;
254 
255   d = XtDisplay(w);
256 
257   if (nw->number.gc_black) {
258     XFreeGC(d, nw->number.gc_black);
259     nw->number.gc_black = 0;
260   }
261   if (nw->number.gc_white) {
262     XFreeGC(d, nw->number.gc_white);
263     nw->number.gc_white = 0;
264   }
265   if (nw->number.gc_copy_and) {
266     XFreeGC(d, nw->number.gc_copy_and);
267     nw->number.gc_copy_and = 0;
268   }
269   if (nw->number.gc_copy_andinv) {
270     XFreeGC(d, nw->number.gc_copy_andinv);
271     nw->number.gc_copy_andinv = 0;
272   }
273   if (nw->number.gc_copy_or) {
274     XFreeGC(d, nw->number.gc_copy_or);
275     nw->number.gc_copy_or = 0;
276   }
277 
278   if (nw->number.figure) {
279     XFreePixmap(d, nw->number.figure);
280     nw->number.figure = 0;
281   }
282   if (nw->number.mask) {
283     XFreePixmap(d, nw->number.mask);
284     nw->number.mask = 0;
285   }
286 
287   if (nw->number.resized_figure) {
288     XFreePixmap(d, nw->number.resized_figure);
289     nw->number.resized_figure = 0;
290   }
291   if (nw->number.resized_mask) {
292     XFreePixmap(d, nw->number.resized_mask);
293     nw->number.resized_mask = 0;
294   }
295 }
296 
Resize(Widget w)297 static void Resize(Widget w)
298 {
299   NumberWidget nw = (NumberWidget)w;
300   Display * d;
301   Window r;
302   int width, height;
303 
304   if (!nw->number.active) return;
305 
306   d = XtDisplay(w);
307   r = DefaultRootWindow(d);
308 
309   if (nw->number.resized_figure) {
310     XFreePixmap(d, nw->number.resized_figure);
311     nw->number.resized_figure = 0;
312   }
313   if (nw->number.resized_mask) {
314     XFreePixmap(d, nw->number.resized_mask);
315     nw->number.resized_mask = 0;
316   }
317 
318   width  = (int)nw->number.size.x * nw->number.ratio / 100;
319   height = (int)nw->number.size.y * nw->number.ratio / 100;
320 
321   nw->number.resized_figure = (width>0 && height>0 && nw->number.figure)
322     ? CreateResizedPixmap(d, r, nw->number.figure, width, height) : 0;
323   nw->number.resized_mask   = (width>0 && height>0 && nw->number.mask)
324     ? CreateResizedPixmap(d, r, nw->number.mask  , width, height) : 0;
325 }
326 
Redisplay(Widget w,XEvent * event,Region region)327 static void Redisplay(Widget w, XEvent * event, Region region)
328 {
329   NumberWidget nw = (NumberWidget)w;
330 
331   if (!nw->number.active) return;
332 
333   /* None */
334 }
335 
336 /*===========================================================================*/
337 /* ���饹�Υ᥽�åɤ����                                                    */
338 /*===========================================================================*/
339 
340 /*---------------------------------------------------------------------------*/
341 /* ����                                                                      */
342 /*---------------------------------------------------------------------------*/
343 
NumberDraw(Widget w,Pixmap pixmap,XPoint center)344 void NumberDraw(Widget w, Pixmap pixmap, XPoint center)
345 {
346   NumberWidget nw = (NumberWidget)w;
347   double c, s;
348   int x0, y0, x, y;
349   int width, height;
350 
351   if (!nw->number.active) return;
352 
353   if (!pixmap) return; /* �����褬�ʤ���� */
354 
355   c = cos((nw->number.number - 3) * (30.0 * M_PI / 180.0));
356   s = sin((nw->number.number - 3) * (30.0 * M_PI / 180.0));
357 
358   width  = (int)nw->number.size.x * nw->number.ratio / 100;
359   height = (int)nw->number.size.y * nw->number.ratio / 100;
360 
361   x0 = center.x +
362     (nw->number.face_width /2 * (int)w->core.x/100 * nw->number.ratio_x/100);
363 
364   y0 = center.y +
365     (nw->number.face_height/2 * (int)w->core.y/100 * nw->number.ratio_y/100);
366 
367   x = x0
368     + (int)(nw->number.face_size/2 * (int)nw->number.length / 100 * c)
369     * nw->number.ratio / 100 - (int)(width /2);
370   y = y0
371     + (int)(nw->number.face_size/2 * (int)nw->number.length / 100 * s)
372     * nw->number.ratio / 100 - (int)(height/2);
373 
374   if (nw->number.resized_mask)
375     XCopyArea(XtDisplay(w), nw->number.resized_mask  , pixmap,
376 	      nw->number.gc_copy_and, 0, 0, width, height, x, y);
377   if (nw->number.resized_figure)
378     XCopyArea(XtDisplay(w), nw->number.resized_figure, pixmap,
379 	      nw->number.gc_copy_or , 0, 0, width, height, x, y);
380 }
381 
382 /*****************************************************************************/
383 /* �����ޤ�                                                                  */
384 /*****************************************************************************/
385 
386 /*****************************************************************************/
387 /* End of File.                                                              */
388 /*****************************************************************************/
389