Ejemplo n.º 1
0
        private unsafe void SetIcon(AppIcon icon)
        {
            if (icon == null || icon.Icons.Length == 0)
            {
                Gtk.Window.SetIcon(Handle, IntPtr.Zero);
            }
            else
            {
                IntPtr iconList = IntPtr.Zero;
                var    icons    = new IntPtr[icon.Icons.Length];

                try
                {
                    for (int i = 0; i < icons.Length; i++)
                    {
                        IntPtr iconStream = IntPtr.Zero;
                        try
                        {
                            byte[] data = icon.GetIconData(icon.Icons[i]);
                            fixed(byte *iconDataPtr = data)
                            {
                                iconStream = GLib.CreateStreamFromData((IntPtr)iconDataPtr, data.Length, IntPtr.Zero);
                                icons[i]   = Gdk.Pixbuf.NewFromStream(iconStream, IntPtr.Zero, IntPtr.Zero);
                                iconList   = GLib.ListPrepend(iconList, icons[i]);
                            }
                        }
                        finally { if (iconStream != IntPtr.Zero)
                                  {
                                      GLib.UnrefObject(iconStream);
                                  }
                        }
                    }

                    Gtk.Window.SetIconList(Handle, iconList);
                }
                finally
                {
                    if (iconList != IntPtr.Zero)
                    {
                        GLib.FreeList(iconList);
                    }
                    foreach (var item in icons)
                    {
                        if (item != IntPtr.Zero)
                        {
                            GLib.UnrefObject(item);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void SetBackgroundColor(string color)
        {
            IntPtr provider = IntPtr.Zero;

            try
            {
                provider = Gtk.Css.Create();

                using (GLibString css = $"* {{background-color:{color}}}")
                {
                    Gtk.Css.LoadData(provider, css, new IntPtr(-1), IntPtr.Zero);
                }

                IntPtr context = Gtk.StyleContext.Get(Handle);
                Gtk.StyleContext.AddProvider(context, provider, GtkStyleProviderPriority.Application);
            }
            finally { if (provider != IntPtr.Zero)
                      {
                          GLib.UnrefObject(provider);
                      }
            }
        }
Ejemplo n.º 3
0
        private async void UriSchemeCallback(IntPtr request, IntPtr userdata)
        {
            try
            {
                var uri    = new Uri(GLibString.FromPointer(WebKit.UriScheme.GetRequestUri(request)));
                var scheme = uri.GetComponents(UriComponents.Scheme, UriFormat.Unescaped);
                if (scheme == CustomScheme)
                {
                    using (var contentStream = await Application.ContentProvider.GetStreamAsync(uri))
                    {
                        if (contentStream != null)
                        {
                            IntPtr stream = IntPtr.Zero;
                            try
                            {
                                if (contentStream is UnmanagedMemoryStream unmanagedMemoryStream)
                                {
                                    unsafe
                                    {
                                        long length = unmanagedMemoryStream.Length - unmanagedMemoryStream.Position;
                                        stream = GLib.CreateStreamFromData((IntPtr)unmanagedMemoryStream.PositionPointer, length, IntPtr.Zero);
                                        FinishUriSchemeCallback(request, stream, length, uri);
                                        return;
                                    }
                                }
                                else
                                {
                                    byte[] data;
                                    long   length;
                                    if (contentStream is MemoryStream memoryStream)
                                    {
                                        data   = memoryStream.GetBuffer();
                                        length = memoryStream.Length;
                                    }
                                    else
                                    {
                                        using (var copyStream = new MemoryStream())
                                        {
                                            await contentStream.CopyToAsync(copyStream);

                                            data   = copyStream.GetBuffer();
                                            length = copyStream.Length;
                                        }
                                    }

                                    unsafe
                                    {
                                        fixed(byte *dataPtr = data)
                                        {
                                            stream = GLib.CreateStreamFromData((IntPtr)dataPtr, length, IntPtr.Zero);
                                            FinishUriSchemeCallback(request, stream, length, uri);
                                            return;
                                        }
                                    }
                                }
                            }
                            finally { if (stream != IntPtr.Zero)
                                      {
                                          GLib.UnrefObject(stream);
                                      }
                            }
                        }
                    }
                }

                FinishUriSchemeCallbackWithError(request, UriCallbackFileNotFound);
            }
            catch { FinishUriSchemeCallbackWithError(request, UriCallbackUnspecifiedError); }
        }
Ejemplo n.º 4
0
        public DialogResult Show(IWindow?parent)
        {
            var    window    = NativeCast.To <GtkWindow>(parent);
            bool   useNative = false && Gtk.Version.IsAtLeast(3, 2, 0);
            IntPtr dialog    = IntPtr.Zero;

            try
            {
                using (GLibString gtitle = Title)
                {
                    if (useNative)
                    {
                        dialog = Gtk.Dialog.CreateNativeFileDialog(
                            gtitle.Pointer,
                            window?.Handle ?? IntPtr.Zero,
                            Type,
                            IntPtr.Zero,
                            IntPtr.Zero);
                    }
                    else
                    {
                        string acceptString = GetAcceptString(Type);
                        using GLibString acceptButton = acceptString;
                        using GLibString cancelButton = "_Cancel";
                        dialog = Gtk.Dialog.CreateFileDialog(
                            gtitle.Pointer,
                            window?.Handle ?? IntPtr.Zero,
                            Type,
                            cancelButton,
                            GtkResponseType.Cancel,
                            acceptButton,
                            GtkResponseType.Accept,
                            IntPtr.Zero);
                    }
                }

                Gtk.Dialog.SetCanCreateFolder(dialog, true);

                BeforeShow(dialog);

                GtkResponseType result;
                if (useNative)
                {
                    result = Gtk.Dialog.RunNative(dialog);
                }
                else
                {
                    result = Gtk.Dialog.Run(dialog);
                }

                var mappedResult = MapResult(result);
                BeforeReturn(dialog, mappedResult);

                return(mappedResult);
            }
            finally
            {
                if (dialog != IntPtr.Zero)
                {
                    if (useNative)
                    {
                        GLib.UnrefObject(dialog);
                    }
                    else
                    {
                        Gtk.Widget.Destroy(dialog);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public DialogResult Show(IWindow parent)
        {
            var window = parent as GtkWindow;

            if (parent != null && window == null)
            {
                throw new ArgumentException("Invalid window type.", nameof(parent));
            }

            bool   useNative = false && Gtk.Version.IsAtLeast(3, 2, 0);
            IntPtr dialog    = IntPtr.Zero;

            try
            {
                using (GLibString gtitle = Title)
                {
                    if (useNative)
                    {
                        dialog = Gtk.Dialog.CreateNativeFileDialog(
                            gtitle.Pointer,
                            window?.Handle ?? IntPtr.Zero,
                            Type,
                            IntPtr.Zero,
                            IntPtr.Zero);
                    }
                    else
                    {
                        string acceptString = GetAcceptString(Type);
                        using (GLibString acceptButton = acceptString)
                            using (GLibString cancelButton = "_Cancel")
                            {
                                dialog = Gtk.Dialog.CreateFileDialog(
                                    gtitle.Pointer,
                                    window?.Handle ?? IntPtr.Zero,
                                    Type,
                                    cancelButton,
                                    GtkResponseType.Cancel,
                                    acceptButton,
                                    GtkResponseType.Accept,
                                    IntPtr.Zero);
                            }
                    }
                }

                Gtk.Dialog.SetCanCreateFolder(dialog, true);
                if (!string.IsNullOrWhiteSpace(InitialDirectory))
                {
                    using (GLibString dir = InitialDirectory)
                    {
                        Gtk.Dialog.SetCurrentFolder(dialog, dir);
                    }
                }

                if (!string.IsNullOrWhiteSpace(FileName))
                {
                    using (GLibString name = FileName)
                    {
                        Gtk.Dialog.SetFileName(dialog, name);
                    }
                }

                SetFileFilters(dialog, FileFilters);

                BeforeShow(dialog);

                GtkResponseType result;
                if (useNative)
                {
                    result = Gtk.Dialog.RunNative(dialog);
                }
                else
                {
                    result = Gtk.Dialog.Run(dialog);
                }

                using (var fileName = new GLibString(Gtk.Dialog.GetFileName(dialog)))
                {
                    FileName = fileName.ToString();
                }

                BeforeReturn(dialog);

                return(MapResult(result));
            }
            finally
            {
                if (dialog != IntPtr.Zero)
                {
                    if (useNative)
                    {
                        GLib.UnrefObject(dialog);
                    }
                    else
                    {
                        Gtk.Widget.Destroy(dialog);
                    }
                }
            }
        }