Example #1
0
            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                if (TwoDADocument.BlankEntry.Equals(value))
                {
                    return(DBNull.Value);
                }

                var column = TwoDARowTypeDescriptor.GetColumn(context);

                if (column == null)
                {
                    return(value);
                }

                var schemaColumn = TwoDARowTypeDescriptor.GetSchemaColumn(context);

                if (schemaColumn != null)
                {
                    if (schemaColumn.RowSource != null ||
                        schemaColumn.DataType == TwoDASchema.DataType.StrRef ||
                        schemaColumn.Enum != null)
                    {
                        var s = value as string;
                        if (s != null)
                        {
                            int sepPos = s.IndexOf('\u2013');
                            if (sepPos > 0)
                            {
                                value = s.Substring(0, sepPos).TrimEnd('\u2002');
                            }
                        }
                    }
                    else if (schemaColumn.DataType == TwoDASchema.DataType.HexInteger)
                    {
                        var s = value as string;
                        if (s != null && s.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
                        {
                            int sepPos = s.IndexOf('\u2013');
                            if (sepPos > 0)
                            {
                                s = s.Substring(0, sepPos);
                            }
                            value = int.Parse(s.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
                        }
                    }
                }

                return(Convert.ChangeType(value, column.DataType));
            }
Example #2
0
            private static IEnumerable <DataRow> GetReferencedRows(ITypeDescriptorContext context, out DataColumn keyColumn)
            {
                var column       = TwoDARowTypeDescriptor.GetColumn(context);
                var schemaColumn = TwoDARowTypeDescriptor.GetSchemaColumn(context);

                var rowSource = schemaColumn.RowSource;

                if (rowSource != null)
                {
                    var doc          = (TwoDADocument)column.Table.ExtendedProperties[typeof(TwoDADocument)];
                    var rowSourceDoc = MainWindow.Instance.GetReferencedTwoDADocument(doc, rowSource);
                    if (rowSourceDoc != null && rowSourceDoc.Data.Columns.Contains(schemaColumn.RowSourceKeyField))
                    {
                        var keyColumn2 = keyColumn = rowSourceDoc.Data.Columns[schemaColumn.RowSourceKeyField];
                        return(from row in rowSourceDoc.Data.AsEnumerable()
                               where row.RowState != DataRowState.Deleted && row[keyColumn2] != DBNull.Value
                               select row);
                    }
                }

                keyColumn = null;
                return(null);
            }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (service == null)
            {
                return(value);
            }

            var column       = TwoDARowTypeDescriptor.GetColumn(context);
            var schemaColumn = TwoDARowTypeDescriptor.GetSchemaColumn(context);

            if (column == null || schemaColumn == null || schemaColumn.RowSource == null)
            {
                return(value);
            }

            var refDoc = (TwoDADocument)column.Table.ExtendedProperties[typeof(TwoDADocument)];
            var srcDoc = MainWindow.Instance.GetReferencedTwoDADocument(refDoc, schemaColumn.RowSource);

            if (srcDoc == null)
            {
                string text = string.Format(
                    "RowRef lookup is currently disabled for this property, because '{0}' is not opened.",
                    schemaColumn.RowSource);
                MessageBox.Show(text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(value);
            }

            if (!srcDoc.Data.Columns.Contains(schemaColumn.RowSourceKeyField))
            {
                return(value);
            }

            var keyColumn = srcDoc.Data.Columns[schemaColumn.RowSourceKeyField];

            object result          = value;
            var    view            = new DataView(srcDoc.Data);
            var    selectedRowView = (from rowView in view.Cast <DataRowView>()
                                      where object.Equals(rowView.Row[keyColumn], value)
                                      select rowView
                                      ).FirstOrDefault();

            var panel = new Panel {
                Width = 350, Height = 300
            };

            var searchTextBox = new TextBox
            {
                Dock = DockStyle.Top
            };

            var bindingSource = new BindingSource {
                DataSource = view
            };

            var grid = new DataGridView
            {
                AutoGenerateColumns = false,
                BorderStyle         = BorderStyle.None,
                Dock = DockStyle.Fill,
                ColumnHeadersVisible     = false,
                RowHeadersVisible        = false,
                SelectionMode            = DataGridViewSelectionMode.FullRowSelect,
                MultiSelect              = false,
                ReadOnly                 = true,
                ScrollBars               = ScrollBars.Vertical,
                AllowUserToResizeColumns = false,
                AllowUserToResizeRows    = false,
                AllowUserToAddRows       = false,
                AllowUserToDeleteRows    = false,
                DataSource               = bindingSource
            };

            var timer = new Timer {
                Interval = 800
            };

            grid.Columns.AddRange(
                new[]
            {
                new DataGridViewTextBoxColumn
                {
                    DataPropertyName = keyColumn.ColumnName,
                    AutoSizeMode     = DataGridViewAutoSizeColumnMode.None,
                    Width            = 75,
                    DefaultCellStyle = { WrapMode = DataGridViewTriState.True }
                },
                new DataGridViewTextBoxColumn
                {
                    DataPropertyName = srcDoc.Data.Columns[1].ColumnName,
                    AutoSizeMode     = DataGridViewAutoSizeColumnMode.Fill
                }
            });

            panel.Controls.Add(grid);
            panel.Controls.Add(searchTextBox);

            searchTextBox.TextChanged +=
                delegate
            {
                timer.Stop();
                timer.Start();
            };

            timer.Tick +=
                delegate
            {
                timer.Stop();

                var oldCursor = grid.Cursor;
                panel.Cursor = Cursors.AppStarting;

                if (string.IsNullOrEmpty(searchTextBox.Text))
                {
                    view.RowFilter = null;
                }
                else
                {
                    var pattern = new StringBuilder();
                    foreach (char c in searchTextBox.Text)
                    {
                        switch (c)
                        {
                        case '\'':
                            pattern.Append(c);
                            pattern.Append(c);
                            break;

                        case '*':
                        case '%':
                        case '[':
                        case ']':
                            pattern.Append('[');
                            pattern.Append(c);
                            pattern.Append(']');
                            break;

                        default:
                            pattern.Append(c);
                            break;
                        }

                        view.RowFilter = string.Format(
                            "[{0}] LIKE '*{1}*'",
                            srcDoc.Data.Columns[1].ColumnName,
                            pattern);
                    }
                }

                panel.Cursor = oldCursor;
            };

            bindingSource.CurrentChanged +=
                delegate
            {
                selectedRowView = (DataRowView)bindingSource.Current;
            };

            grid.CellDoubleClick +=
                delegate
            {
                result =
                    selectedRowView != null ?
                    selectedRowView.Row[keyColumn] :
                    value;
                service.CloseDropDown();
            };

            grid.KeyDown +=
                delegate(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    result =
                        selectedRowView != null ?
                        selectedRowView.Row[keyColumn] :
                        value;
                    service.CloseDropDown();
                    e.Handled = true;
                }
            };

            grid.KeyPress +=
                delegate(object sender, KeyPressEventArgs e)
            {
                if (!char.IsControl(e.KeyChar))
                {
                    searchTextBox.Focus();
                    searchTextBox.SelectAll();
                    SendKeys.Send(e.KeyChar.ToString());
                }
            };

            grid.DataBindingComplete +=
                delegate
            {
                var selectedIndex = bindingSource.IndexOf(selectedRowView);
                if (selectedIndex >= 0)
                {
                    grid.FirstDisplayedScrollingRowIndex = selectedIndex;
                    grid.CurrentCell = grid.Rows[selectedIndex].Cells[0];
                }
            };

            panel.GotFocus +=
                delegate
            {
                grid.Focus();
            };

            bindingSource.DataSource = view;
            service.DropDownControl(panel);

            return(result ?? (object)DBNull.Value);
        }
Example #4
0
            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                if (context != null && destinationType == typeof(string))
                {
                    if (value is DBNull)
                    {
                        return(TwoDADocument.BlankEntry);
                    }

                    var column = TwoDARowTypeDescriptor.GetColumn(context);
                    if (column == null)
                    {
                        return(value);
                    }

                    var schemaColumn = TwoDARowTypeDescriptor.GetSchemaColumn(context);
                    if (schemaColumn != null)
                    {
                        if (schemaColumn.Enum != null && value != null)
                        {
                            var msv = schemaColumn.Enum.FirstOrDefault(
                                sv =>
                            {
                                try
                                {
                                    var o = Convert.ChangeType(sv.Value, value.GetType());
                                    return(object.Equals(value, o));
                                }
                                catch
                                {
                                    return(false);
                                }
                            });
                            if (msv != null && msv.Name != null)
                            {
                                return(string.Format("{0}\u2002\u2013\u2002{1}", value, msv.Name));
                            }
                        }
                        else if (schemaColumn.RowSource != null)
                        {
                            DataColumn keyColumn;
                            var        referencedRows = GetReferencedRows(context, out keyColumn);
                            if (referencedRows != null)
                            {
                                var referencedRow = referencedRows.FirstOrDefault(row => object.Equals(row[keyColumn], value));
                                if (referencedRow != null)
                                {
                                    return(RowReferenceToString(referencedRow, keyColumn));
                                }
                            }
                        }
                        else if (schemaColumn.DataType == TwoDASchema.DataType.StrRef)
                        {
                            if (value is int)
                            {
                                int strref = (int)value;

                                var twoDA = (TwoDADocument)column.Table.ExtendedProperties[typeof(TwoDADocument)];
                                var tlk   = MainWindow.Instance.GetReferencedTlkDocument(twoDA, (strref & 0x01000000) != 0);
                                if (tlk != null)
                                {
                                    var referencedEntry = tlk.Entries[strref & 0x00FFFFFF];
                                    if (referencedEntry != null)
                                    {
                                        return(StrReferenceToString(strref, referencedEntry));
                                    }
                                }
                            }
                        }
                        else if (schemaColumn.DataType == TwoDASchema.DataType.HexInteger)
                        {
                            if (value is int)
                            {
                                int    i    = (int)value;
                                string desc = "";
                                if (schemaColumn.Flags != null)
                                {
                                    foreach (var flag in schemaColumn.Flags)
                                    {
                                        if ((i & flag.Bits) != 0)
                                        {
                                            if (desc.Length == 0)
                                            {
                                                desc = " \u2013 ";
                                            }
                                            else
                                            {
                                                desc += ", ";
                                            }
                                            desc += flag.Name;
                                        }
                                    }
                                }

                                return("0x" + ((int)value).ToString("X" + schemaColumn.Digits, CultureInfo.InvariantCulture) + desc);
                            }
                        }
                    }
                }

                return(base.ConvertTo(context, culture, value, destinationType));
            }
Example #5
0
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (service == null)
            {
                return(value);
            }

            var column       = TwoDARowTypeDescriptor.GetColumn(context);
            var schemaColumn = TwoDARowTypeDescriptor.GetSchemaColumn(context);

            if (column == null || schemaColumn == null)
            {
                return(value);
            }

            var twoDA  = (TwoDADocument)column.Table.ExtendedProperties[typeof(TwoDADocument)];
            var tlk    = MainWindow.Instance.GetReferencedTlkDocument(twoDA, false);
            var tlkAlt = MainWindow.Instance.GetReferencedTlkDocument(twoDA, true);

            if (tlk == null && tlkAlt == null)
            {
                string text =
                    "StrRef lookup is currently disabled, because there are no TLK files opened.";
                MessageBox.Show(text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(value);
            }

            var tlkEntries =
                tlk != null?
                tlk.Entries.Select(entry => new TlkEntryView(entry, false)).ToArray() :
                    new TlkEntryView[0];

            var tlkAltEntries =
                tlkAlt != null?
                tlkAlt.Entries.Select(entry => new TlkEntryView(entry, true)).ToArray() :
                    new TlkEntryView[0];

            var entries = tlkEntries.Concat(tlkAltEntries).ToArray();

            int?strref = value as int?;
            int?result = strref;

            TlkEntryView selectedEntry = null;

            if (strref != null)
            {
                int i = (int)strref & 0x00FFFFFF;
                if ((strref & 0x01000000) == 0)
                {
                    selectedEntry = tlkEntries.ElementAtOrDefault(i);
                }
                else
                {
                    selectedEntry = tlkAltEntries.ElementAtOrDefault(i);
                }
            }

            var panel = new Panel {
                Width = 350, Height = 300
            };

            var searchTextBox = new TextBox
            {
                Dock = DockStyle.Top
            };

            var bindingSource = new BindingSource {
                DataSource = entries
            };

            var grid = new DataGridView
            {
                AutoGenerateColumns = false,
                BorderStyle         = BorderStyle.None,
                Dock = DockStyle.Fill,
                ColumnHeadersVisible     = false,
                RowHeadersVisible        = false,
                SelectionMode            = DataGridViewSelectionMode.FullRowSelect,
                MultiSelect              = false,
                ReadOnly                 = true,
                ScrollBars               = ScrollBars.Vertical,
                AllowUserToResizeColumns = false,
                AllowUserToResizeRows    = false,
                AllowUserToAddRows       = false,
                AllowUserToDeleteRows    = false,
                DataSource               = bindingSource
            };

            var timer = new Timer {
                Interval = 800
            };

            grid.Columns.AddRange(
                new[]
            {
                new DataGridViewTextBoxColumn
                {
                    DataPropertyName = "StrRef",
                    AutoSizeMode     = DataGridViewAutoSizeColumnMode.None,
                    Width            = 75,
                    DefaultCellStyle = { WrapMode = DataGridViewTriState.True }
                },
                new DataGridViewTextBoxColumn
                {
                    DataPropertyName = "Text",
                    AutoSizeMode     = DataGridViewAutoSizeColumnMode.Fill
                }
            });

            panel.Controls.Add(grid);
            panel.Controls.Add(searchTextBox);

            searchTextBox.TextChanged +=
                delegate
            {
                timer.Stop();
                timer.Start();
            };

            timer.Tick +=
                delegate
            {
                timer.Stop();

                var oldCursor = grid.Cursor;
                panel.Cursor = Cursors.AppStarting;

                bindingSource.DataSource = (from entry in entries
                                            where entry.Text != null &&
                                            entry.Text.IndexOf(searchTextBox.Text, StringComparison.CurrentCultureIgnoreCase) >= 0
                                            select entry).ToArray();

                panel.Cursor = oldCursor;
            };

            bindingSource.CurrentChanged +=
                delegate
            {
                selectedEntry = (TlkEntryView)bindingSource.Current;
            };

            grid.CellDoubleClick +=
                delegate
            {
                result = selectedEntry != null ? selectedEntry.StrRef : strref;
                service.CloseDropDown();
            };

            grid.KeyDown +=
                delegate(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    result = selectedEntry != null ? selectedEntry.StrRef : strref;
                    service.CloseDropDown();
                    e.Handled = true;
                }
            };

            grid.KeyPress +=
                delegate(object sender, KeyPressEventArgs e)
            {
                if (!char.IsControl(e.KeyChar))
                {
                    searchTextBox.Focus();
                    searchTextBox.SelectAll();
                    SendKeys.Send(e.KeyChar.ToString());
                }
            };

            grid.DataBindingComplete +=
                delegate
            {
                var selectedIndex = bindingSource.IndexOf(selectedEntry);
                if (selectedIndex >= 0)
                {
                    grid.FirstDisplayedScrollingRowIndex = selectedIndex;
                    grid.CurrentCell = grid.Rows[selectedIndex].Cells[0];
                }
            };

            panel.GotFocus +=
                delegate
            {
                grid.Focus();
            };

            bindingSource.DataSource = entries;
            service.DropDownControl(panel);

            return(result ?? (object)DBNull.Value);
        }