Beispiel #1
0
        public unsafe int Show()
        {
            SDL_MessageBoxData boxdata = new SDL_MessageBoxData();

            if (this.IconType == 1)
            {
                boxdata.flags = SDL_MessageBoxFlags.SDL_MESSAGEBOX_INFORMATION;
            }
            else if (this.IconType == 2)
            {
                boxdata.flags = SDL_MessageBoxFlags.SDL_MESSAGEBOX_WARNING;
            }
            else if (this.IconType == 3)
            {
                boxdata.flags = SDL_MessageBoxFlags.SDL_MESSAGEBOX_ERROR;
            }
            SDL_MessageBoxButtonData[] buttons = new SDL_MessageBoxButtonData[Buttons.Count];
            for (int i = 0; i < Buttons.Count; i++)
            {
                buttons[i]          = new SDL_MessageBoxButtonData();
                buttons[i].text     = StrToPtr(Buttons[i]);
                buttons[i].buttonid = 0;
                if (i == 0)
                {
                    buttons[i].flags = SDL_MessageBoxButtonFlags.SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
                }
                else if (i == Buttons.Count - 1)
                {
                    buttons[i].flags = SDL_MessageBoxButtonFlags.SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
                }
            }
            boxdata.message    = this.Message;
            boxdata.numbuttons = Buttons.Count;
            boxdata.title      = this.Title;
            boxdata.window     = this.Parent == null ? IntPtr.Zero : this.Parent.SDL_Window;
            int result = -1;

            fixed(SDL_MessageBoxButtonData *buttonptr = &buttons[0])
            {
                boxdata.buttons = (IntPtr)buttonptr;
            }

            SDL_ShowMessageBox(ref boxdata, out result);
            return(result);
        }
Beispiel #2
0
        private static unsafe int Show(
            MessageBoxFlags flags,
            string title,
            string message,
            IEnumerable <MessageBoxButton> buttons,
            MessageBoxColors?colorScheme = null,
            Window?parent = null
            )
        {
            int clicked;

            Span <byte> tbuf   = stackalloc byte[SL(title)];
            Span <byte> msgbuf = stackalloc byte[SL(message)];

            StringToUTF8(title, tbuf);
            StringToUTF8(message, msgbuf);
            fixed(byte *t = tbuf)
            fixed(byte *msg = msgbuf)
            {
                Span <SDL_MessageBoxButtonData> btns = new SDL_MessageBoxButtonData[16];
                SDL_MessageBoxData data;

                data.flags       = (uint)flags;
                data.window      = IntPtr.Zero;
                data.title       = t;
                data.message     = msg;
                data.numbuttons  = 0;
                data.buttons     = null;
                data.colorScheme = null;
                try
                {
                    SDL_MessageBoxColorScheme scheme;
                    if (colorScheme != null)
                    {
                        scheme           = MarshalColors(colorScheme);
                        data.colorScheme = &scheme;
                    }
                    if (parent != null)
                    {
                        bool ok = false;
                        parent.DangerousAddRef(ref ok);
                        if (ok)
                        {
                            data.window = parent.DangerousGetHandle();
                        }
                    }
                    data.numbuttons = MarshalButtons(buttons, btns);
                    fixed(SDL_MessageBoxButtonData *bptr = &MemoryMarshal.GetReference(btns))
                    {
                        data.buttons = bptr;
                        ErrorIfNegative(SDL_ShowMessageBox(data, out clicked));
                    }
                }
                finally
                {
                    if (parent != null && data.window != IntPtr.Zero)
                    {
                        parent.DangerousRelease();
                    }
                    if (data.numbuttons > 0)
                    {
                        for (var i = 0; i < data.numbuttons; ++i)
                        {
                            Marshal.FreeHGlobal((IntPtr)btns[i].text);
                        }
                    }
                }
            }
            return(clicked);
        }