Skip to content

Commit

Permalink
Merge pull request #45 from don-mccomb/SelectNewImageWidthText
Browse files Browse the repository at this point in the history
Select width text when New Document dialog is shown.
  • Loading branch information
cameronwhite committed Dec 1, 2012
2 parents f446bcc + da09903 commit cafa78f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
31 changes: 21 additions & 10 deletions Pinta/Actions/File/NewDocumentAction.cs
Expand Up @@ -45,14 +45,21 @@ public void Uninitialize ()

private void Activated (object sender, EventArgs e)
{
NewImageDialog dialog = new NewImageDialog ();

if (!TryUseClipboardImageSize (dialog))
int imgWidth = 0;
int imgHeight = 0;

// Try to get the dimensions of an image on the clipboard
// for the initial width and height values on the NewImageDialog
if (!GetClipboardImageSize (out imgWidth, out imgHeight))
{
dialog.NewImageWidth = PintaCore.Settings.GetSetting<int> ("new-image-width", 800);
dialog.NewImageHeight = PintaCore.Settings.GetSetting<int> ("new-image-height", 600);
// An image was not on the clipboard,
// so use saved dimensions from settings
imgWidth = PintaCore.Settings.GetSetting<int> ("new-image-width", 800);
imgHeight = PintaCore.Settings.GetSetting<int> ("new-image-height", 600);
}

NewImageDialog dialog = new NewImageDialog (imgWidth, imgHeight);

dialog.WindowPosition = Gtk.WindowPosition.CenterOnParent;

int response = dialog.Run ();
Expand All @@ -69,12 +76,16 @@ private void Activated (object sender, EventArgs e)
}

/// <summary>
/// Sets the dialog to use the clipboard image's dimensions, if possible.
/// Gets the width and height of an image on the clipboard,
/// if available.
/// </summary>
/// <returns>True if an image was on the clipboard, false otherwise.</returns>
private static bool TryUseClipboardImageSize (NewImageDialog dialog)
/// <param name="width">Destination for the image width.</param>
/// <param name="height">Destination for the image height.</param>
/// <returns>True if dimensions were available, false otherwise.</returns>
private static bool GetClipboardImageSize (out int width, out int height)
{
bool clipboardUsed = false;
width = height = 0;

Gtk.Clipboard cb = Gtk.Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));
if (cb.WaitIsImageAvailable ())
Expand All @@ -83,8 +94,8 @@ private static bool TryUseClipboardImageSize (NewImageDialog dialog)
if (image != null)
{
clipboardUsed = true;
dialog.NewImageWidth = image.Width;
dialog.NewImageHeight = image.Height;
width = image.Width;
height = image.Height;
image.Dispose ();
}
}
Expand Down
23 changes: 14 additions & 9 deletions Pinta/Dialogs/NewImageDialog.cs
Expand Up @@ -32,7 +32,12 @@ namespace Pinta
{
public partial class NewImageDialog : Gtk.Dialog
{
public NewImageDialog () : base (string.Empty, PintaCore.Chrome.MainWindow, DialogFlags.Modal)
/// <summary>
/// Configures and builds a NewImageDialog object.
/// </summary>
/// <param name="imgWidth">Initial value of the width spin control.</param>
/// <param name="imgHeight">nitial value of the height spin control.</param>
public NewImageDialog (int imgWidth, int imgHeight) : base (string.Empty, PintaCore.Chrome.MainWindow, DialogFlags.Modal)
{
this.Build ();

Expand All @@ -42,17 +47,17 @@ public NewImageDialog () : base (string.Empty, PintaCore.Chrome.MainWindow, Dial

widthSpinner.ActivatesDefault = true;
heightSpinner.ActivatesDefault = true;
}

public int NewImageWidth {
get { return widthSpinner.ValueAsInt; }
set { widthSpinner.Value = value; }
}
// Initialize the spin control values
widthSpinner.Value = imgWidth;
heightSpinner.Value =imgHeight;

public int NewImageHeight {
get { return heightSpinner.ValueAsInt; }
set { heightSpinner.Value = value; }
// Set focus to the width spin control and select it's text
widthSpinner.GrabFocus();
}

public int NewImageWidth { get { return widthSpinner.ValueAsInt; } }
public int NewImageHeight { get { return heightSpinner.ValueAsInt; } }
}
}

0 comments on commit cafa78f

Please sign in to comment.