Example #1
0
        void ConnectChild(Gtk.Widget widget)
        {
            if (widget == null)
            {
                return;
            }

            string widgetName = widget.Name;

            if (widget is Gtk.Label && !string.IsNullOrEmpty(widgetName))
            {
                var widgetNameParts = widgetName.Split(":".ToCharArray(), 2);

                if (widgetNameParts.Length != 2)
                {
                    return;
                }

                Gtk.Widget valueWidget = null;
                Gtk.Grid   grid        = widget.Parent as Gtk.Grid;

                var leftAttach = (int)grid.ChildGetProperty(widget, "left-attach").Val;
                var topAttach  = (int)grid.ChildGetProperty(widget, "top-attach").Val;
                var width      = 4;

                if (widgetNameParts [0] == "path")
                {
                    valueWidget = ConfigValueFolderWidget.Create(widgetNameParts [1]);
                }
                else if (widgetNameParts [0] == "string")
                {
                    valueWidget = ConfigValueStringWidget.Create(widgetNameParts [1]);
                }
                else if (widgetNameParts [0] == "int")
                {
                    widgetNameParts = widgetName.Split(":".ToCharArray(), 4);
                    valueWidget     = ConfigValueIntWidget.Create(widgetNameParts [3], int.Parse(widgetNameParts[1]), int.Parse(widgetNameParts[2]));
                }
                else if (widgetNameParts [0] == "bool")
                {
                    valueWidget        = ConfigValueBoolWidget.Create(widgetNameParts [1]);
                    valueWidget.Expand = false;
                    width = 1;
                }

                if (valueWidget != null)
                {
                    valueWidget.TooltipMarkup = widget.TooltipMarkup;
                    grid.AttachNextTo(valueWidget, widget, Gtk.PositionType.Right, width, 1);
                }
            }

            if (widget is Gtk.Container)
            {
                this.ConnectAllChildren(widget as Gtk.Container);
            }
        }
Example #2
0
        Gtk.Widget GenerateButtonsForData(PaletteHeaderData data)
        {
            Gtk.Grid grid = new Gtk.Grid();

            if (data.IsResolvable)
            {
                System.Drawing.Color[][] colors = data.GetPalettes();

                int row = 0;
                int col = 0;

                int numCols = 8; // Reduce this to sort colors into separate rows

                for (int i = 0; i < data.NumPalettes; i++)
                {
                    Gtk.Box box = new Gtk.VBox();

                    for (int j = 0; j < 4; j++)
                    {
                        int paletteIndex = i;
                        int colorIndex   = j;

                        Cairo.Color     color  = CairoHelper.ConvertColor(colors[i][j]);
                        Gtk.ColorButton button = new Gtk.ColorButton(color.ToRGBA());
                        button.ColorSet += (sender, args) => {
                            data.SetColor(paletteIndex, colorIndex, button.Rgba.ToDrawingColor());
                        };
                        box.Add(button);
                    }

                    Gtk.Frame frame = new Gtk.Frame();
                    frame.Label       = (i + data.FirstPalette).ToString();
                    frame.LabelXalign = 0.5f;
                    frame.Add(box);
                    grid.Attach(frame, col, row, 1, 1);
                    col++;
                    if (col == numCols)
                    {
                        col = 0;
                        row++;
                    }
                }
            }

            Gtk.Frame outsideFrame = new Gtk.Frame();
            outsideFrame.Add(grid);
            outsideFrame.Label = data.PointerName
                                 + " [" + (data.PaletteType == PaletteType.Background ? "BG" : "OBJ") + "]";
            outsideFrame.LabelXalign = 0.5f;

            return(outsideFrame);
        }
Example #3
0
        public ValueReferenceEditor(Project p, ValueReferenceGroup vrg, int rows, string frameText = null)
            : base(1.0F, 1.0F, 1.0F, 1.0F)
        {
            Project = p;

            valueReferenceGroup = vrg;
            maxBounds           = new int[valueReferenceGroup.GetNumValueReferences()];
            widgetGrids         = new List <Gtk.Grid>();
            widgetPositions     = new Tuple <int, int> [maxBounds.Count];
            widgetLists         = new List <IList <Gtk.Widget> >();

            Gtk.Box hbox = new Gtk.HBox();
            hbox.Spacing = 6;

            Func <Gtk.Grid> newGrid = () => {
                Gtk.Grid g = new Gtk.Grid();
                g.ColumnSpacing = 6;
                g.RowSpacing    = 2;
                hbox.Add(g);
                return(g);
            };

            Gtk.Grid grid = newGrid();
            int      x = 0, y = 0;


            // Do not use "foreach" here. The "valueReferenceGroup" may be changed. So, whenever we
            // access a ValueReference from within an event handler, we must do so though the
            // "valueReferenceGroup" class variable, and NOT though an alias (like with foreach).
            for (int tmpCounter = 0; tmpCounter < valueReferenceGroup.Count; tmpCounter++)
            {
                int i = tmpCounter; // Variable must be distinct within each closure

                if (y >= rows)
                {
                    y    = 0;
                    x    = 0;
                    grid = newGrid();
                }

                // Each ValueReference may use up to 3 widgets in the grid row
                Gtk.Widget[] widgetList = new Gtk.Widget[3];

                widgetPositions[i] = new Tuple <int, int>(x, y);

                Action <Gtk.SpinButton> setSpinButtonLimits = (spinButton) => {
                    if (valueReferenceGroup[i].MaxValue < 0x10)
                    {
                        spinButton.Digits = 1;
                    }
                    else if (valueReferenceGroup[i].MaxValue < 0x100)
                    {
                        spinButton.Digits = 2;
                    }
                    else if (valueReferenceGroup[i].MaxValue < 0x1000)
                    {
                        spinButton.Digits = 3;
                    }
                    else
                    {
                        spinButton.Digits = 4;
                    }
                    spinButton.Adjustment.Lower = valueReferenceGroup[i].MinValue;
                    spinButton.Adjustment.Upper = valueReferenceGroup[i].MaxValue;
                };

                int entryWidgetWidth = 1;

                // If it has a ConstantsMapping, use a combobox instead of anything else
                if (valueReferenceGroup[i].ConstantsMapping != null)
                {
                    ComboBoxFromConstants comboBox = new ComboBoxFromConstants(showHelp: true, vertical: true);
                    comboBox.SetConstantsMapping(valueReferenceGroup[i].ConstantsMapping);

                    // Must put this before the "Changed" handler below to avoid
                    // it being fired (for some reason?)
                    setSpinButtonLimits(comboBox.SpinButton);

                    comboBox.Changed += delegate(object sender, EventArgs e) {
                        valueReferenceGroup[i].SetValue(comboBox.ActiveValue);
                        OnDataModifiedInternal();
                    };

                    dataModifiedExternalEvent += delegate() {
                        comboBox.ActiveValue = valueReferenceGroup[i].GetIntValue();
                    };

                    /*
                     * comboBox.MarginTop = 4;
                     * comboBox.MarginBottom = 4;
                     */

                    widgetList[0] = new Gtk.Label(valueReferenceGroup[i].Name);
                    widgetList[1] = comboBox;

                    entryWidgetWidth = 2;

                    goto loopEnd;
                }
                // ConstantsMapping == null

                switch (valueReferenceGroup[i].ValueType)
                {
                case ValueReferenceType.String:
                {
                    widgetList[0] = new Gtk.Label(valueReferenceGroup[i].Name);

                    Gtk.Entry entry = new Gtk.Entry();
                    if (!valueReferenceGroup[i].Editable)
                    {
                        entry.Sensitive = false;
                    }
                    dataModifiedExternalEvent += delegate() {
                        entry.Text = valueReferenceGroup[i].GetStringValue();
                        OnDataModifiedInternal();
                    };

                    widgetList[1] = entry;
                    break;
                }

                case ValueReferenceType.Int:
                {
                    widgetList[0] = new Gtk.Label(valueReferenceGroup[i].Name);

                    SpinButtonHexadecimal spinButton =
                        new SpinButtonHexadecimal(valueReferenceGroup[i].MinValue, valueReferenceGroup[i].MaxValue);
                    if (!valueReferenceGroup[i].Editable)
                    {
                        spinButton.Sensitive = false;
                    }
                    setSpinButtonLimits(spinButton);
                    spinButton.ValueChanged += delegate(object sender, EventArgs e) {
                        Gtk.SpinButton button = sender as Gtk.SpinButton;
                        if (maxBounds[i] == 0 || button.ValueAsInt <= maxBounds[i])
                        {
                            valueReferenceGroup[i].SetValue(button.ValueAsInt);
                        }
                        else
                        {
                            button.Value = maxBounds[i];
                        }
                        OnDataModifiedInternal();
                    };
                    dataModifiedExternalEvent += delegate() {
                        spinButton.Value = valueReferenceGroup[i].GetIntValue();
                    };

                    widgetList[1] = spinButton;
                }
                break;

                case ValueReferenceType.Bool:
                {
                    widgetList[0] = new Gtk.Label(valueReferenceGroup[i].Name);

                    Gtk.CheckButton checkButton = new Gtk.CheckButton();
                    checkButton.FocusOnClick = false;
                    if (!valueReferenceGroup[i].Editable)
                    {
                        checkButton.Sensitive = false;
                    }
                    checkButton.Toggled += delegate(object sender, EventArgs e) {
                        Gtk.CheckButton button = sender as Gtk.CheckButton;
                        valueReferenceGroup[i].SetValue(button.Active ? 1 : 0);
                        OnDataModifiedInternal();
                    };
                    dataModifiedExternalEvent += delegate() {
                        checkButton.Active = valueReferenceGroup[i].GetIntValue() == 1;
                    };

                    widgetList[1] = checkButton;
                }
                break;
                }

loopEnd:
                grid.Attach(widgetList[0], x, y, 1, 1);
                grid.Attach(widgetList[1], x + 1, y, entryWidgetWidth, 1);

                widgetList[2]         = new Gtk.Alignment(0, 0.5f, 0, 0); // Container for help button
                widgetList[2].Hexpand = true;                             // Let this absorb any extra space
                grid.Attach(widgetList[2], x + 2, y, 1, 1);

                widgetGrids.Add(grid);
                widgetLists.Add(widgetList);

                if (valueReferenceGroup[i].Tooltip != null)
                {
                    SetTooltip(i, valueReferenceGroup[i].Tooltip);
                }
                y++;
            }

            if (frameText != null)
            {
                var frame = new Gtk.Frame(frameText);
                frame.Add(hbox);
                this.Add(frame);
            }
            else
            {
                this.Add(hbox);
            }

            this.ShowAll();

            // Initial values
            if (dataModifiedExternalEvent != null)
            {
                dataModifiedExternalEvent();
            }

            UpdateHelpButtons();

            AddModifiedHandlers();
            this.Destroyed += (sender, args) => RemoveModifiedHandlers();
        }
Example #4
0
        public static Gtk.Widget GetChildAt(this Gtk.Grid grid, int left, int top)
        {
            var ptr = NativeMethods.gtk_grid_get_child_at(grid.Handle, left, top);

            return(GLib.Object.GetObject(ptr) as Gtk.Widget);
        }
Example #5
0
        void SetImagePosition()
        {
            if (Control.Child != null)
            {
                Control.Remove(Control.Child);
            }
            (label.Parent as Gtk.Container)?.Remove(label);
            (gtkimage?.Parent as Gtk.Container)?.Remove(gtkimage);

            Gtk.Widget child     = null;
            var        showImage = Image != null;
            var        showLabel = !string.IsNullOrEmpty(label.Text);

            if (showImage && showLabel)
            {
                Gtk.VBox vbox;
                Gtk.HBox hbox;
                switch (ImagePosition)
                {
                case ButtonImagePosition.Above:
                    child = vbox = new Gtk.VBox(false, 2);
                    vbox.PackStart(gtkimage, true, true, 0);
                    vbox.PackEnd(label, false, true, 0);
                    break;

                case ButtonImagePosition.Below:
                    child = vbox = new Gtk.VBox(false, 2);
                    vbox.PackStart(label, false, true, 0);
                    vbox.PackEnd(gtkimage, true, true, 0);
                    break;

                case ButtonImagePosition.Left:
                    child = hbox = new Gtk.HBox(false, 2);
                    hbox.PackStart(gtkimage, false, true, 0);
                    hbox.PackStart(label, true, true, 0);
                    break;

                case ButtonImagePosition.Right:
                    child = hbox = new Gtk.HBox(false, 2);
                    hbox.PackStart(label, true, true, 0);
                    hbox.PackEnd(gtkimage, false, true, 0);
                    break;

                case ButtonImagePosition.Overlay:
#if GTK2
                    var table = new Gtk.Table(1, 1, false);
                    child = table;
                    table.Attach(label, 0, 0, 1, 1, Gtk.AttachOptions.Expand, Gtk.AttachOptions.Expand, 0, 0);
                    table.Attach(gtkimage, 0, 0, 1, 1, Gtk.AttachOptions.Expand, Gtk.AttachOptions.Expand, 0, 0);
#else
                    var grid = new Gtk.Grid();
                    child            = grid;
                    label.Hexpand    = label.Vexpand = true;
                    gtkimage.Hexpand = gtkimage.Vexpand = true;
                    grid.Attach(label, 0, 0, 1, 1);
                    grid.Attach(gtkimage, 0, 0, 1, 1);
#endif
                    break;

                default:
                    throw new NotSupportedException();
                }
            }
            else if (showLabel)
            {
                child = label;
            }
            else if (showImage)
            {
                child = gtkimage;
            }

            if (child != null)
            {
                child.Show();
                Control.Child = child;
            }

            Control.QueueResize();
        }