Пример #1
0
 TonNurako.X11.Color OptionToColor(Display dpy, string option, TonNurako.X11.Color def)
 {
     if (null == option)
     {
         return(def);
     }
     return(TonNurako.X11.Color.AllocNamedColor(dpy, dpy.DefaultColormap, option));
 }
Пример #2
0
        public Eyes(EyesOptions option, TonNurako.ApplicationContext app) : base()
        {
            unity = new TonNurako.Inutility.Unity();

            applicationContext = app.Handle.Context;

            this.ClassInited         = 0;
            this.CompressMotion      = true;
            this.CompressExposure    = 1;
            this.CompressEnterleave  = true;
            this.VisibleInterest     = false;
            this.SuperClass          = TonNurako.Xt.Core.CoreWidgetClass.GetDefaultSuperClass();
            this.ClassName           = "EyesTnk";
            this.ClassPartInitialize = (w) => Console.WriteLine("ClassPartInitialize");
            this.ClassInitialize     = () => Console.WriteLine("ClassInitialize");
            this.ClassPartInitialize = (w) => Console.WriteLine("ClassPartInitialize");
            this.SetValues           = (old, request, xnew, args) => false;
            this.SetValuesAlmost     = (old, xnew, geom) => null;

            this.Initialize = (request, xnew, args) => {
                widgetSelf = xnew;
                var win = xnew.Handle.Window;
                var dpy = xnew.Handle.Display;
                widgetRec = xnew.Handle.WidgetRecAccessor;
                //TonNurako.Inutility.Dumper.DumpProperty(widgetRec, (s) => Console.WriteLine($"WidgetRec: {s}"));

                Console.WriteLine("=== options ===");
                foreach (var prpr in typeof(EyesOptions).GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
                {
                    var v = prpr.GetValue(option);
                    if (null == v)
                    {
                        v = (object)"(default)";
                    }
                    Console.WriteLine($"{prpr.Name} = {v}");
                }
                Console.WriteLine("======");

                var dfg = new TonNurako.X11.Color((ulong)widgetSelf.ToolkitResources.GetLongValue(TonNurako.Motif.ResourceId.XmNforeground));
                var dbg = new TonNurako.X11.Color((ulong)widgetSelf.ToolkitResources.GetLongValue(TonNurako.Motif.ResourceId.XmNbackground));

                eyes.Pixel[(int)EyesPart.PART_PUPIL]   = OptionToColor(dpy, option.fg, dfg);
                eyes.Pixel[(int)EyesPart.PART_OUTLINE] = OptionToColor(dpy, option.outline, dfg);
                eyes.Pixel[(int)EyesPart.PART_CENTER]  = OptionToColor(dpy, option.center, dbg);
                eyes.Render      = option.render;
                eyes.ShapeWindow = option.shape;
                eyes.Distance    = option.distance;

                var values = new TonNurako.X11.XGCValues();

                if (eyes.ReverseVideo)
                {
                    var xfg = eyes.Pixel[(int)EyesPart.PART_PUPIL];
                    var xbg = dbg;

                    if (widgetRec.BorderPixel.PixelEquals(xfg))
                    {
                        widgetRec.BorderPixel = xbg;
                    }

                    if (eyes.Pixel[(int)EyesPart.PART_OUTLINE].PixelEquals(xfg))
                    {
                        eyes.Pixel[(int)EyesPart.PART_OUTLINE] = xbg;
                    }
                    if (eyes.Pixel[(int)EyesPart.PART_CENTER].PixelEquals(xbg))
                    {
                        eyes.Pixel[(int)EyesPart.PART_CENTER] = xfg;
                    }
                    eyes.Pixel[(int)EyesPart.PART_PUPIL] = xbg;
                    widgetRec.BackgroundPixel            = xfg;
                }

                values.Foreground = eyes.Pixel[(int)EyesPart.PART_PUPIL].Pixel;
                values.Background = widgetRec.BackgroundPixel.Pixel;
                eyes.GC[(int)EyesPart.PART_PUPIL] =
                    XtSports.XtGetGC(xnew, TonNurako.X11.GCMask.GCForeground | TonNurako.X11.GCMask.GCBackground, values);

                values.Foreground = eyes.Pixel[(int)EyesPart.PART_OUTLINE].Pixel;
                eyes.GC[(int)EyesPart.PART_OUTLINE] =
                    XtSports.XtGetGC(xnew, TonNurako.X11.GCMask.GCForeground | TonNurako.X11.GCMask.GCBackground, values);

                //PART_CENTER
                values.Foreground = eyes.Pixel[(int)EyesPart.PART_CENTER].Pixel;
                values.Background = eyes.Pixel[(int)EyesPart.PART_PUPIL].Pixel;
                eyes.GC[(int)EyesPart.PART_CENTER] =
                    XtSports.XtGetGC(xnew, TonNurako.X11.GCMask.GCForeground | TonNurako.X11.GCMask.GCBackground, values);

                eyes.Update     = 0;
                eyes.IntervalId = 0;

                eyes.Pupil[0] = new TPoint();
                eyes.Pupil[1] = new TPoint();

                eyes.Pupil[0].X = eyes.Pupil[1].X = TPOINT_NONE;
                eyes.Pupil[0].Y = eyes.Pupil[1].Y = TPOINT_NONE;

                eyes.Mouse.X = eyes.Mouse.Y = TPOINT_NONE;

                eyes.ShapeMask = null;

                eyes.GC[(int)EyesPart.PART_SHAPE] = null;

                unity.Store <TonNurako.X11.GC>(eyes.GC);

                if (eyes.Render)
                {
                    for (int i = 0; i < (int)EyesPart.PART_SHAPE; i++)
                    {
                        XColor c = new XColor();
                        c.Pixel = eyes.Pixel[i].Pixel;
                        var q = Color.QueryColor(dpy, widgetRec.Colormap, c);

                        eyes.Fill[i] = TonNurako.X11.Extension.XRender.CreateSolidFill(dpy, new TonNurako.X11.Extension.XRenderColor(q, 0xFFFF));
                    }
                    unity.Store <TonNurako.X11.Extension.Picture> (eyes.Fill);
                }

                Console.WriteLine("Initialize");
            };
            this.Realize = (widget, mask, attr) => {
                mask = TonNurako.X11.ChangeWindowAttributes.CWBackPixel |
                       TonNurako.X11.ChangeWindowAttributes.CWBorderPixel |
                       TonNurako.X11.ChangeWindowAttributes.CWEventMask |
                       TonNurako.X11.ChangeWindowAttributes.CWBackingStore;

                var dpy = widget.Handle.Display;
                attr.Colormap     = widgetRec.Colormap.Handle;
                attr.BackingStore = TonNurako.X11.BackingStoreHint.NotUseful;
                Console.WriteLine($"Realize: M={mask}");
                //TonNurako.Inutility.Dumper.DumpProperty(attr, (s) => Console.WriteLine($"Realize: {s}"));
                XtSports.XtCreateWindow(widget, TonNurako.X11.WindowClass.InputOutput, TonNurako.X11.Visual.CopyFromParent, mask, attr);

                this.Resize(widget);

                eyes.IntervalId = applicationContext.AddTimeOut(delays[eyes.Update], DrawIt);
                Console.WriteLine($"Realize timeoutId={eyes.IntervalId}");
            };

            this.Destroy = (self) => {
                applicationContext.RemoveTimeOut(eyes.IntervalId);
                if (null != eyes.Picture)
                {
                    eyes.Picture.Dispose();
                    eyes.Picture = null;
                }
                unity.Asset();
                Console.WriteLine("Destroy");
            };

            this.Resize = (self) => {
                if (!TonNurako.Xt.XtSports.XtIsRealized(self))
                {
                    return;
                }
                var win = self.Handle.Window;
                win.ClearWindow();
                var geom   = win.GetGeometry();
                var dpy    = self.Handle.Display;
                var screen = self.Handle.Screen;

                SetTransform(eyes.Transform,
                             0, geom.Width,
                             geom.Height, 0,
                             W_MIN_X, W_MAX_X,
                             W_MIN_Y, W_MAX_Y);

                if (null != eyes.Picture)
                {
                    eyes.Picture.Dispose();
                    eyes.Picture = null;
                }

                if (eyes.ShapeWindow)
                {
                    using (eyes.ShapeMask =
                               new TonNurako.X11.Pixmap(self.Handle.Display, self.Handle.Window, geom.Width, geom.Height, 1)) {
                        if (null == eyes.GC[(int)EyesPart.PART_SHAPE])
                        {
                            eyes.GC[(int)EyesPart.PART_SHAPE] =
                                unity.Store(TonNurako.X11.GC.Create(eyes.ShapeMask, GCMask.GCNone, new XGCValues()));
                        }

                        eyes.GC[(int)EyesPart.PART_SHAPE].SetForeground(0);
                        eyes.GC[(int)EyesPart.PART_SHAPE].FillRectangle(eyes.ShapeMask, 0, 0, geom.Width, geom.Height);
                        eyes.GC[(int)EyesPart.PART_SHAPE].SetForeground(1);
                        EyeLiner(false, 0);
                        EyeLiner(false, 1);
                        int     x      = 0;
                        int     y      = 0;
                        IWidget parent = self;
                        for (; null != XtSports.XtParent(parent); parent = XtSports.XtParent(parent))
                        {
                            var g = parent.Handle.Window.GetGeometry();
                            x += g.X + g.BorderWidth;
                            x += g.Y + g.BorderWidth;
                        }
                        TonNurako.X11.Extension.XShape.CombineMask(
                            parent.Handle.Display, parent.Handle.Window, TonNurako.X11.Extension.ShapeKind.ShapeBounding,
                            x, y, eyes.ShapeMask, TonNurako.X11.Extension.ShapeOp.ShapeSet);
                    }
                    eyes.ShapeMask = null;
                }

                if (eyes.Render)
                {
                    var pa = new TonNurako.X11.Extension.XRenderPictureAttributes();
                    var pf = TonNurako.X11.Extension.XRender.FindVisualFormat(dpy, screen.DefaultVisualOfScreen);
                    if (null != pf)
                    {
                        eyes.Picture =
                            TonNurako.X11.Extension.XRender.CreatePicture(
                                dpy, win, pf, TonNurako.X11.Extension.CreatePictureMask.None, pa);
                        if (null == eyes.Picture)
                        {
                            throw new Exception("XRender.CreatePicture Failed");
                        }
                    }
                    else
                    {
                        throw new Exception("FindVisualFormat Failed");
                    }
                }
                Console.WriteLine("Resize");
            };

            this.Expose = (self, xevent, region) => {
                eyes.Pupil[0].X = TPOINT_NONE;
                eyes.Pupil[0].Y = TPOINT_NONE;
                eyes.Pupil[1].X = TPOINT_NONE;
                eyes.Pupil[1].Y = TPOINT_NONE;
                RepaintWindow();
                Console.WriteLine("Expose");
            };
        }