public IEnumerator <object> LoadConfiguration()
        {
            using (new ControlWaitCursor(this)) {
                List.BeginUpdate();

                string oldSelection = null;
                {
                    var oldItem = List.SelectedItem as ColorEntry;
                    if (oldItem != null)
                    {
                        oldSelection = oldItem.Key;
                    }
                }

                List.Items.Clear();

                ColorEntry[] entries = null;
                yield return(Program.Database.ExecuteArray <ColorEntry>(
                                 "SELECT key, red, green, blue FROM targetColors ORDER BY key ASC"
                                 ).Bind(() => entries));

                var entryDict = entries.ToDictionary((e) => e.Key);
                var keys      = Script.DefinedColors.Keys.ToArray();
                Array.Sort(keys);

                ColorEntry item;
                foreach (var key in keys)
                {
                    if (!entryDict.TryGetValue(key, out item))
                    {
                        var def = Script.DefinedColors[key];
                        item = new ColorEntry {
                            Key   = key,
                            Color = def
                        };
                    }

                    List.Items.Add(item);

                    if (key.Equals(oldSelection, StringComparison.InvariantCultureIgnoreCase))
                    {
                        List.SelectedItem = item;
                    }
                }

                using (var g = List.CreateGraphics())
                    List.ItemHeight = (int)Math.Ceiling(g.MeasureString("AaBbYyZz", List.Font).Height) + 2;

                List.EndUpdate();
            }
        }
        public IEnumerator<object> LoadConfiguration()
        {
            using (new ControlWaitCursor(this)) {
                List.BeginUpdate();

                string oldSelection = null;
                {
                    var oldItem = List.SelectedItem as ColorEntry;
                    if (oldItem != null)
                        oldSelection = oldItem.Key;
                }

                List.Items.Clear();

                ColorEntry[] entries = null;
                yield return Program.Database.ExecuteArray<ColorEntry>(
                    "SELECT key, red, green, blue FROM targetColors ORDER BY key ASC"
                ).Bind(() => entries);

                var entryDict = entries.ToDictionary((e) => e.Key);
                var keys = Script.DefinedColors.Keys.ToArray();
                Array.Sort(keys);

                ColorEntry item;
                foreach (var key in keys) {
                    if (!entryDict.TryGetValue(key, out item)) {
                        var def = Script.DefinedColors[key];
                        item = new ColorEntry {
                            Key = key,
                            Color = def
                        };
                    }

                    List.Items.Add(item);

                    if (key.Equals(oldSelection, StringComparison.InvariantCultureIgnoreCase))
                        List.SelectedItem = item;
                }

                using (var g = List.CreateGraphics())
                    List.ItemHeight = (int)Math.Ceiling(g.MeasureString("AaBbYyZz", List.Font).Height) + 2;

                List.EndUpdate();
            }
        }
        public IEnumerator<object> SetItemColor(ColorEntry item, Color? newColor)
        {
            if (newColor.HasValue) {
                item.Color = newColor.Value;

                using (var q = Program.Database.BuildQuery(
                    "REPLACE INTO targetColors (key, red, green, blue) VALUES (?, ?, ?, ?)"
                ))
                    yield return q.ExecuteNonQuery(item.Key, item.Red, item.Green, item.Blue);
            } else {
                using (var q = Program.Database.BuildQuery(
                    "DELETE FROM targetColors WHERE key = ?"
                ))
                    yield return q.ExecuteNonQuery(item.Key);
            }

            Program.EventBus.Broadcast(Script, "PreferenceChanged", "*");

            yield return LoadConfiguration();
        }
        public IEnumerator <object> SetItemColor(ColorEntry item, Color?newColor)
        {
            if (newColor.HasValue)
            {
                item.Color = newColor.Value;

                using (var q = Program.Database.BuildQuery(
                           "REPLACE INTO targetColors (key, red, green, blue) VALUES (?, ?, ?, ?)"
                           ))
                    yield return(q.ExecuteNonQuery(item.Key, item.Red, item.Green, item.Blue));
            }
            else
            {
                using (var q = Program.Database.BuildQuery(
                           "DELETE FROM targetColors WHERE key = ?"
                           ))
                    yield return(q.ExecuteNonQuery(item.Key));
            }

            Program.EventBus.Broadcast(Script, "PreferenceChanged", "*");

            yield return(LoadConfiguration());
        }