<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Gtk;
using Colours;

public partial class MainWindow: Gtk.Window
{
	public AppController app;
	public AppPaletteController appPal;

	private static Gdk.Atom clipAtom = Gdk.Atom.Intern("CLIPBOARD", false);
	Clipboard clipboard = Clipboard.Get (clipAtom);

	// TODO: should this be just PaletteColor, and use render funcs?
	ListStore ls = new ListStore (typeof(Gdk.Pixbuf), typeof(string), typeof(string), typeof(PaletteColor));

	public MainWindow () : base (Gtk.WindowType.Toplevel)
	{
		Build ();
		// don't use this for app init, only for base init
		treeview1.Model = ls;

		var pcIconRender = new CellRendererPixbuf ();
		var pcIconCol = new TreeViewColumn ("Icon", pcIconRender);
		pcIconCol.AddAttribute (pcIconRender, "pixbuf", 0);

		var pcNameRender = new CellRendererText ();
		pcNameRender.Editable = true;
		pcNameRender.Edited += pcNameRender_Edited;
		var pcNameCol = new TreeViewColumn ("Name", pcNameRender);
		pcNameCol.AddAttribute (pcNameRender, "text", 1);

		var pcColorRender = new CellRendererText ();
		var pcColorCol = new TreeViewColumn ("Color", pcColorRender);
		pcColorCol.AddAttribute (pcColorRender, "text", 2);

		treeview1.AppendColumn (pcIconCol);
		treeview1.AppendColumn (pcNameCol);
		treeview1.AppendColumn (pcColorCol);

	}

	public MainWindow(InitialAppState state) : this()
	{
		app = new AppController (state);
		appPal = new AppPaletteController ();
		if (!String.IsNullOrWhiteSpace (state.PaletteFileName)) {

		}
		app.ResultChanged += SyncAppViewState;
		appPal.PaletteChanged += SyncAppPalViewState;
		treeview1.Selection.Changed += (o, e) => {
			UpdateUI();
		};
		SyncAppViewState (this, new EventArgs());
		SyncAppPalViewState (this, new EventArgs ());
	}

	// replaces var pc = (PaletteColor)treeview1.Model.GetValue (i, 2);
	public PaletteColor GetItemFromIter(TreeIter i)
	{
		const int colColumn = 3;
		return (PaletteColor)treeview1.Model.GetValue (i, colColumn);
	}

	public void SyncAppViewState(object sender, EventArgs e)
	{
		schemeBox.Active = (int)app.SchemeType;

		foreach (ColorButton cb in colorBox.Children)
			colorBox.Remove (cb);
		foreach (HsvColor c in app.Results) {
			ColorButton cb = new ColorButton (c.ToGdkColor ());
			cb.UseAlpha = false;
			cb.ColorSet += OnColorChooserColorChanged;
			cb.TooltipText = String.Format("{0}\r\n{1}\r\n{2}",
				c.ToRgb().ToHtml(),
				c.ToRgb().ToHslString(),
				c.ToString());

			// bind the menu too
			cb.ButtonPressEvent += HandleButtonPopupMenu;

			colorBox.PackStart(cb, true, true, 0);
		}

		this.ShowAll ();
		UpdateUI ();
	}

	public void SyncAppPalViewState(object sender, EventArgs e) 
	{
		ls.Clear ();

		foreach (PaletteColor pc in appPal.Palette.Colors) {
			Gdk.Pixbuf buf = new Gdk.Pixbuf (Gdk.Colorspace.Rgb, false, 8, 16, 16);

			buf.Fill (pc.Color.ToGdkPixel());

			ls.AppendValues (buf, pc.Name, pc.Color.ToHtml(), pc);
		}

		UpdateUI ();
	}

	public void UpdateUI()
	{
		Title = String.Format ("{0}{1} ({2} for {3})", appPal.Palette.Name,
			appPal.Dirty ? "*" : "", schemeBox.ActiveText,
			app.Color.ToHtml());

		var selected = treeview1.Selection.CountSelectedRows() > 0;
		var hasItems = appPal.Palette.Colors.Count > 0;

		goBackAction.Sensitive = app.CanUndo ();
		goForwardAction.Sensitive = app.CanRedo ();
		BrightenAction.Sensitive = app.CanBrighten ();
		DarkenAction.Sensitive = app.CanDarken ();
		SaturateAction.Sensitive = app.CanSaturate ();
		DesaturateAction.Sensitive = app.CanDesaturate ();

		undoAction.Sensitive = appPal.CanUndo ();
		redoAction.Sensitive = appPal.CanRedo ();

		cutAction.Sensitive = selected;
		copyAction.Sensitive = selected;
		deleteAction.Sensitive = selected;

		saveAction.Sensitive = hasItems;
		saveAsAction.Sensitive = hasItems;
	}

	/// <summary>
	/// Asks to save changes
	/// </summary>
	/// <returns>If it's OK to close.</returns>
	public bool DirtyPrompt()
	{
		if (appPal.Dirty) {
			MessageDialog md = new MessageDialog (this, DialogFlags.Modal,
				MessageType.Question, ButtonsType.None,
				"There are unsaved changed. Do you want to save before you close this palette?");
			md.AddButton ("Save", ResponseType.Yes);
			md.AddButton ("Discard", ResponseType.No);
			md.AddButton ("Cancel", ResponseType.Cancel);
			var retVal = (ResponseType)md.Run ();
			md.Destroy ();
			switch (retVal) {
			case ResponseType.Yes:
				return SavePalette (false);
			case ResponseType.No:
				return true;
			default:
				return false;
			};
		} else
			return true;
	}

	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		if (DirtyPrompt ()) {
			ConfigParser.SaveConfig (app.Color, app.SchemeType);
			Application.Quit ();
		}
		a.RetVal = true;
	}

	[GLib.ConnectBeforeAttribute]<commit_msg>fix typo in GTK dirty prompt<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Gtk;
using Colours;

public partial class MainWindow: Gtk.Window
{
	public AppController app;
	public AppPaletteController appPal;

	private static Gdk.Atom clipAtom = Gdk.Atom.Intern("CLIPBOARD", false);
	Clipboard clipboard = Clipboard.Get (clipAtom);

	// TODO: should this be just PaletteColor, and use render funcs?
	ListStore ls = new ListStore (typeof(Gdk.Pixbuf), typeof(string), typeof(string), typeof(PaletteColor));

	public MainWindow () : base (Gtk.WindowType.Toplevel)
	{
		Build ();
		// don't use this for app init, only for base init
		treeview1.Model = ls;

		var pcIconRender = new CellRendererPixbuf ();
		var pcIconCol = new TreeViewColumn ("Icon", pcIconRender);
		pcIconCol.AddAttribute (pcIconRender, "pixbuf", 0);

		var pcNameRender = new CellRendererText ();
		pcNameRender.Editable = true;
		pcNameRender.Edited += pcNameRender_Edited;
		var pcNameCol = new TreeViewColumn ("Name", pcNameRender);
		pcNameCol.AddAttribute (pcNameRender, "text", 1);

		var pcColorRender = new CellRendererText ();
		var pcColorCol = new TreeViewColumn ("Color", pcColorRender);
		pcColorCol.AddAttribute (pcColorRender, "text", 2);

		treeview1.AppendColumn (pcIconCol);
		treeview1.AppendColumn (pcNameCol);
		treeview1.AppendColumn (pcColorCol);

	}

	public MainWindow(InitialAppState state) : this()
	{
		app = new AppController (state);
		appPal = new AppPaletteController ();
		if (!String.IsNullOrWhiteSpace (state.PaletteFileName)) {

		}
		app.ResultChanged += SyncAppViewState;
		appPal.PaletteChanged += SyncAppPalViewState;
		treeview1.Selection.Changed += (o, e) => {
			UpdateUI();
		};
		SyncAppViewState (this, new EventArgs());
		SyncAppPalViewState (this, new EventArgs ());
	}

	// replaces var pc = (PaletteColor)treeview1.Model.GetValue (i, 2);
	public PaletteColor GetItemFromIter(TreeIter i)
	{
		const int colColumn = 3;
		return (PaletteColor)treeview1.Model.GetValue (i, colColumn);
	}

	public void SyncAppViewState(object sender, EventArgs e)
	{
		schemeBox.Active = (int)app.SchemeType;

		foreach (ColorButton cb in colorBox.Children)
			colorBox.Remove (cb);
		foreach (HsvColor c in app.Results) {
			ColorButton cb = new ColorButton (c.ToGdkColor ());
			cb.UseAlpha = false;
			cb.ColorSet += OnColorChooserColorChanged;
			cb.TooltipText = String.Format("{0}\r\n{1}\r\n{2}",
				c.ToRgb().ToHtml(),
				c.ToRgb().ToHslString(),
				c.ToString());

			// bind the menu too
			cb.ButtonPressEvent += HandleButtonPopupMenu;

			colorBox.PackStart(cb, true, true, 0);
		}

		this.ShowAll ();
		UpdateUI ();
	}

	public void SyncAppPalViewState(object sender, EventArgs e) 
	{
		ls.Clear ();

		foreach (PaletteColor pc in appPal.Palette.Colors) {
			Gdk.Pixbuf buf = new Gdk.Pixbuf (Gdk.Colorspace.Rgb, false, 8, 16, 16);

			buf.Fill (pc.Color.ToGdkPixel());

			ls.AppendValues (buf, pc.Name, pc.Color.ToHtml(), pc);
		}

		UpdateUI ();
	}

	public void UpdateUI()
	{
		Title = String.Format ("{0}{1} ({2} for {3})", appPal.Palette.Name,
			appPal.Dirty ? "*" : "", schemeBox.ActiveText,
			app.Color.ToHtml());

		var selected = treeview1.Selection.CountSelectedRows() > 0;
		var hasItems = appPal.Palette.Colors.Count > 0;

		goBackAction.Sensitive = app.CanUndo ();
		goForwardAction.Sensitive = app.CanRedo ();
		BrightenAction.Sensitive = app.CanBrighten ();
		DarkenAction.Sensitive = app.CanDarken ();
		SaturateAction.Sensitive = app.CanSaturate ();
		DesaturateAction.Sensitive = app.CanDesaturate ();

		undoAction.Sensitive = appPal.CanUndo ();
		redoAction.Sensitive = appPal.CanRedo ();

		cutAction.Sensitive = selected;
		copyAction.Sensitive = selected;
		deleteAction.Sensitive = selected;

		saveAction.Sensitive = hasItems;
		saveAsAction.Sensitive = hasItems;
	}

	/// <summary>
	/// Asks to save changes
	/// </summary>
	/// <returns>If it's OK to close.</returns>
	public bool DirtyPrompt()
	{
		if (appPal.Dirty) {
			MessageDialog md = new MessageDialog (this, DialogFlags.Modal,
				MessageType.Question, ButtonsType.None,
				"There are unsaved changes. Do you want to save before you close this palette?");
			md.AddButton ("Save", ResponseType.Yes);
			md.AddButton ("Discard", ResponseType.No);
			md.AddButton ("Cancel", ResponseType.Cancel);
			var retVal = (ResponseType)md.Run ();
			md.Destroy ();
			switch (retVal) {
			case ResponseType.Yes:
				return SavePalette (false);
			case ResponseType.No:
				return true;
			default:
				return false;
			};
		} else
			return true;
	}

	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		if (DirtyPrompt ()) {
			ConfigParser.SaveConfig (app.Color, app.SchemeType);
			Application.Quit ();
		}
		a.RetVal = true;
	}

	[GLib.ConnectBeforeAttribute]
{'fraction_non_alphanumeric': 0.12799836684699398, 'fraction_numerical': 0.005716035521077881, 'mean_word_length': 4.151419558359621, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '739797', 'n_tokens_mistral': 3645, 'n_tokens_neox': 3266, 'n_words': 764}