Example #1
0
 public void AddRow(int address, Label alias)
 {
     if (locked)
     {
         return;
     }
     RawAdd(address, alias);
     dataGridView1.Invalidate();
 }
 public void AddRow(int address, Label alias)
 {
     if (Locked)
     {
         return;
     }
     RawAdd(address, alias);
     labelView.Invalidate();
 }
Example #3
0
    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        if (dataGridView1.Rows[e.RowIndex].IsNewRow)
        {
            return;
        }

        var existingSnesAddressStr = (string)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
        var existingName           = (string)dataGridView1.Rows[e.RowIndex].Cells[1].Value;
        var existingComment        = (string)dataGridView1.Rows[e.RowIndex].Cells[2].Value;

        int.TryParse(existingSnesAddressStr, NumberStyles.HexNumber, null, out var existingSnesAddress);

        var newLabel = new Label
        {
            Name    = existingName,
            Comment = existingComment
        };

        toolStripStatusLabel1.Text = "";
        var newSnesAddress = -1;

        switch (e.ColumnIndex)
        {
        case 0:     // label's address
        {
            if (!int.TryParse(e.FormattedValue.ToString(), NumberStyles.HexNumber, null, out newSnesAddress))
            {
                e.Cancel = true;
                toolStripStatusLabel1.Text = "Must enter a valid hex address.";
                break;
            }

            if (existingSnesAddress == -1 && Data.Labels.GetLabel(newSnesAddress) != null)
            {
                e.Cancel = true;
                toolStripStatusLabel1.Text = "This address already has a label.";
                break;
            }

            if (dataGridView1.EditingControl != null)
            {
                dataGridView1.EditingControl.Text = Util.ToHexString6(newSnesAddress);
            }
            break;
        }

        case 1:     // label name
        {
            newSnesAddress = existingSnesAddress;
            newLabel.Name  = e.FormattedValue.ToString();
            // todo (validate for valid label characters)
            break;
        }

        case 2:     // label comment
        {
            newSnesAddress   = existingSnesAddress;
            newLabel.Comment = e.FormattedValue.ToString();
            // todo (validate for valid comment characters, if any)
            break;
        }
        }

        locked = true;
        if (currentlyEditing >= 0)
        {
            if (newSnesAddress >= 0)
            {
                Data.Labels.RemoveLabel(existingSnesAddress);
            }

            Data.Labels.AddLabel(newSnesAddress, newLabel, true);
        }
        locked = false;

        currentlyEditing = -1;
    }
        private void ImportLabelsFromCsv(bool replaceAll)
        {
            OpenFileDialog open = new OpenFileDialog();

            open.Filter = "Comma Separated Value Files|*.csv|Text Files|*.txt|All Files|*.*";

            var result = open.ShowDialog();

            if (result != DialogResult.OK || open.FileName == "")
            {
                return;
            }

            var errLine = 0;

            try
            {
                var newValues = new Dictionary <int, Label>();
                var lines     = Util.ReadLines(open.FileName).ToArray();

                var validLabelChars = new Regex(@"^([a-zA-Z0-9_\-]*)$");

                // NOTE: this is kind of a risky way to parse CSV files, won't deal with weirdness in the comments
                // section.
                for (var i = 0; i < lines.Length; i++)
                {
                    var label = new Label();

                    errLine = i + 1;

                    SplitOnFirstComma(lines[i], out var labelAddress, out var remainder);
                    SplitOnFirstComma(remainder, out label.Name, out label.Comment);

                    label.CleanUp();

                    label.Name = label.Name.Trim();
                    if (!validLabelChars.Match(label.Name).Success)
                    {
                        throw new InvalidDataException("invalid label name: " + label.Name);
                    }

                    newValues.Add(int.Parse(labelAddress, NumberStyles.HexNumber, null), label);
                }

                // everything read OK, modify the existing list now. point of no return
                if (replaceAll)
                {
                    Project.Data.DeleteAllLabels();
                }

                ClearAndInvalidateDataGrid();

                // this will call AddRow() to add items back to the UI datagrid.
                foreach (var pair in newValues)
                {
                    Project.Data.AddLabel(pair.Key, pair.Value, true);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    "An error occurred while parsing the file.\n" + ex.Message +
                    (errLine > 0 ? $" (Check line {errLine}.)" : ""),
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 private void RawAdd(int address, Label alias)
 {
     labelView.Rows.Add(Util.NumberToBaseString(address, Util.NumberBase.Hexadecimal, 6), alias.Name, alias.Comment);
 }
        private void labelView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (labelView.Rows[e.RowIndex].IsNewRow)
            {
                return;
            }
            if (labelView.Rows[e.RowIndex].Cells[0].Value == "")
            {
                return;
            }

            int val = -1, oldAddress = -1;

            int.TryParse((string)labelView.Rows[e.RowIndex].Cells[0].Value, NumberStyles.HexNumber, null, out oldAddress);

            var label = new Label
            {
                Name    = (string)labelView.Rows[e.RowIndex].Cells[1].Value,
                Comment = (string)labelView.Rows[e.RowIndex].Cells[2].Value,
            };

            //toolStripStatusLabel1.Text = "";

            switch (e.ColumnIndex)
            {
            case 0:
                if (!int.TryParse(e.FormattedValue.ToString(), NumberStyles.HexNumber, null, out val))
                {
                    e.Cancel = true;
                    //toolStripStatusLabel1.Text = "Must enter a valid hex address.";
                }
                else if (oldAddress == -1 && Project.Data.Labels.ContainsKey(val))
                {
                    e.Cancel = true;
                    foreach (DataGridViewRow row in labelView.Rows)
                    {
                        if (row.Cells[0].Value.ToString().Equals(e.FormattedValue.ToString().ToUpper()))
                        {
                            labelView.CurrentCell = row.Cells[0];
                            break;
                        }
                    }
                    //toolStripStatusLabel1.Text = "This address already has a label.";

                    Console.WriteLine(Util.NumberToBaseString(val, Util.NumberBase.Hexadecimal));
                }
                else if (labelView.EditingControl != null)
                {
                    labelView.EditingControl.Text = Util.NumberToBaseString(val, Util.NumberBase.Hexadecimal, 6);
                }
                break;

            case 1:
                val        = oldAddress;
                label.Name = e.FormattedValue.ToString();
                // todo (validate for valid label characters)
                break;

            case 2:
                val           = oldAddress;
                label.Comment = e.FormattedValue.ToString();
                // todo (validate for valid comment characters, if any)
                break;
            }

            Locked = true;
            if (currentlyEditing >= 0)
            {
                if (val >= 0)
                {
                    Project.Data.AddLabel(oldAddress, null, true);
                }
                Project.Data.AddLabel(val, label, true);
            }
            Locked = false;

            currentlyEditing = -1;
            InvalidateTable();  // TODO: move to mainwindow, use notifychanged in mainwindow for this
        }