Skip to content

Commit

Permalink
Merge pull request #100 from sillsdev/added-optional-owner-when-showi…
Browse files Browse the repository at this point in the history
…ng-dialogs

Added optional owner parameter to methods that show dialog boxes
  • Loading branch information
tombogle authored Jul 8, 2022
2 parents d4a2464 + dcdaaaf commit 7bc8ee4
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 60 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ file like Bloom.es.xlf or .../es/Bloom.xlf).
when constructing an XLIFF-based LocalizationManager.
- Changed the way the "original" attribute is set in XLIFF files. It used to be based on the
Name, but changed it to use Id instead.
- Added optional owner parameter to methods that show dialog boxes so that they can be
displayed centered on a parent window (and not appear off-screen).

### Removed

Expand Down
1 change: 1 addition & 0 deletions L10NSharp.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Filenames/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Palaso/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Xliff/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
3 changes: 2 additions & 1 deletion src/L10NSharp/ILocalizationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace L10NSharp
{
Expand Down Expand Up @@ -91,7 +92,7 @@ public interface ILocalizationManager: IDisposable
void RefreshToolTips();

void PrepareToCustomizeLocalizations();
void ShowLocalizationDialogBox(bool runInReadonlyMode);
void ShowLocalizationDialogBox(bool runInReadonlyMode, IWin32Window owner = null);

/// ------------------------------------------------------------------------------------
/// <summary>
Expand Down
15 changes: 9 additions & 6 deletions src/L10NSharp/LocalizationManagerInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,21 @@ internal static void RemoveManager(string id)
}
}

internal static void ShowLocalizationDialogBox(IComponent component)
internal static void ShowLocalizationDialogBox(IComponent component,
IWin32Window owner = null)
{
TipDialog.Show("If you click on an item while you hold alt and shift keys down, this tool will open up with that item already selected.");
if (owner == null)
owner = (component as Control)?.FindForm();
TipDialog.ShowAltShiftClickTip(owner);
LocalizeItemDlg<T>.ShowDialog(GetLocalizationManagerForComponent(component),
component, false);
component, false, owner);
}

public static void ShowLocalizationDialogBox(string id)
public static void ShowLocalizationDialogBox(string id, IWin32Window owner = null)
{
TipDialog.Show("If you click on an item while you hold alt and shift keys down, this tool will open up with that item already selected.");
TipDialog.ShowAltShiftClickTip(owner);
LocalizeItemDlg<T>.ShowDialog(GetLocalizationManagerForString(id),
id, false);
id, false, owner);
}

/// ------------------------------------------------------------------------------------
Expand Down
22 changes: 13 additions & 9 deletions src/L10NSharp/TMXUtils/TMXLocalizationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ public void Dispose()

#region Methods for showing localization dialog box
/// ------------------------------------------------------------------------------------
public void ShowLocalizationDialogBox(bool runInReadonlyMode)
public void ShowLocalizationDialogBox(bool runInReadonlyMode, IWin32Window owner = null)
{
LocalizeItemDlg<TMXDocument>.ShowDialog(this, "", runInReadonlyMode);
LocalizeItemDlg<TMXDocument>.ShowDialog(this, "", runInReadonlyMode, owner);
}

#endregion
Expand Down Expand Up @@ -273,7 +273,7 @@ public void MergeTranslationDocuments(string appId, TMXDocument newDoc, string o
/// ------------------------------------------------------------------------------------
/// <summary>
/// Enumerates a TMX file for each language. Prefer the custom localizations folder version
/// if it exists, otherwise the installed langauge folder.
/// if it exists, otherwise the installed language folder.
/// Exception: never return the English tmx, which is always handled separately and first.
/// Doing this serves to insert any new dynamic strings into the cache, thus validating
/// them as non-obsolete if we encounter them in other languages.
Expand Down Expand Up @@ -428,8 +428,7 @@ public void RegisterComponentForLocalizing(LocalizingInfo info,
/// ------------------------------------------------------------------------------------
private void PrepareComponentForRuntimeLocalization(IComponent component)
{
var toolStripItem = component as ToolStripItem;
if (toolStripItem != null)
if (component is ToolStripItem toolStripItem)
{
toolStripItem.MouseDown += HandleToolStripItemMouseDown;
toolStripItem.Disposed += HandleToolStripItemDisposed;
Expand Down Expand Up @@ -920,6 +919,8 @@ internal void HandleToolStripItemMouseDown(object sender, MouseEventArgs e)
// Make sure all drop-downs are closed that are in the
// chain of menu items for this item.
var tsddi = sender as ToolStripDropDownItem;
var owningForm = tsddi?.Owner?.FindForm();

while (tsddi != null)
{
tsddi.DropDown.Close();
Expand All @@ -930,7 +931,8 @@ internal void HandleToolStripItemMouseDown(object sender, MouseEventArgs e)
tsddi = tsddi.OwnerItem as ToolStripDropDownItem;
}

LocalizeItemDlg<TMXDocument>.ShowDialog(this, (IComponent)sender, false);
LocalizeItemDlg<TMXDocument>.ShowDialog(this, (IComponent)sender, false,
owningForm);
}

private static bool DoHandleMouseDown =>
Expand Down Expand Up @@ -982,7 +984,7 @@ internal void HandleControlMouseDown(object sender, MouseEventArgs e)
var lm = LocalizationManagerInternal<TMXDocument>.GetLocalizationManagerForComponent(ctrl);

LocalizationManager.OnLaunchingLocalizationDialog(lm);
LocalizeItemDlg<TMXDocument>.ShowDialog(lm, ctrl, false);
LocalizeItemDlg<TMXDocument>.ShowDialog(lm, ctrl, false, ctrl?.FindForm());
LocalizationManager.OnClosingLocalizationDialog(lm);
}

Expand Down Expand Up @@ -1048,7 +1050,8 @@ internal void HandleListViewColumnHeaderClicked(object sender, ColumnClickEventA
return;

if (sender is ListView lv && ComponentCache.ContainsKey(lv.Columns[e.Column]))
LocalizeItemDlg<TMXDocument>.ShowDialog(this, lv.Columns[e.Column], false);
LocalizeItemDlg<TMXDocument>.ShowDialog(this, lv.Columns[e.Column], false,
lv.FindForm());
}

/// ------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1089,7 +1092,8 @@ internal void HandleDataGridViewCellMouseDown(object sender, DataGridViewCellMou
return;

if (sender is DataGridView grid && e.RowIndex < 0 && ComponentCache.ContainsKey(grid.Columns[e.ColumnIndex]))
LocalizeItemDlg<TMXDocument>.ShowDialog(this, grid.Columns[e.ColumnIndex], false);
LocalizeItemDlg<TMXDocument>.ShowDialog(this, grid.Columns[e.ColumnIndex], false,
grid.FindForm());
}

/// ------------------------------------------------------------------------------------
Expand Down
16 changes: 12 additions & 4 deletions src/L10NSharp/UI/LocalizeItemDlg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public partial class LocalizeItemDlg<T> : Form

/// ------------------------------------------------------------------------------------
internal static DialogResult ShowDialog(ILocalizationManagerInternal<T> callingManager,
IComponent component, bool runInReadonlyMode)
IComponent component, bool runInReadonlyMode, IWin32Window owner = null)
{
if (callingManager != null && !callingManager.CanCustomizeLocalizations)
runInReadonlyMode = true;
Expand All @@ -47,20 +47,28 @@ internal static DialogResult ShowDialog(ILocalizationManagerInternal<T> callingM
callingManager.ComponentCache.FirstOrDefault(kvp => kvp.Key == component).Value);

using (var dlg = new LocalizeItemDlg<T>(viewModel, id, callingManager))
return dlg.ShowDialog();
{
if (owner != null)
dlg.StartPosition = FormStartPosition.CenterParent;
return dlg.ShowDialog(owner);
}
}

/// ------------------------------------------------------------------------------------
internal static DialogResult ShowDialog(ILocalizationManagerInternal<T> callingManager,
string id, bool runInReadonlyMode)
string id, bool runInReadonlyMode, IWin32Window owner = null)
{
if (callingManager != null && !callingManager.CanCustomizeLocalizations)
runInReadonlyMode = true;

var viewModel = new LocalizeItemDlgViewModel<T>(runInReadonlyMode);

using (var dlg = new LocalizeItemDlg<T>(viewModel, id, callingManager))
return dlg.ShowDialog();
{
if (owner != null)
dlg.StartPosition = FormStartPosition.CenterParent;
return dlg.ShowDialog(owner);
}
}

#region Constrution/initialization
Expand Down
40 changes: 18 additions & 22 deletions src/L10NSharp/UI/TipDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,32 @@ namespace L10NSharp.UI
{
public partial class TipDialog : Form
{
public string DialogTitle
public string Message
{
get { return Text; }
set { Text = value; }
get => _message.Text;
set => _message.Text = value;
}

public string Message
public new Image Icon
{
get { return _message.Text; }
set { _message.Text = value; }
get => _icon.Image;
set => _icon.Image = value;
}

public new Image Icon
internal static void ShowAltShiftClickTip(IWin32Window owner = null)
{
get { return _icon.Image; }
set { _icon.Image = value; }
Show("If you click on an item while you hold alt and shift keys down, this tool will " +
"open up with that item already selected.", owner);
}

public static void Show(string message)
public static void Show(string message, IWin32Window owner)
{
using (var d = new TipDialog(message, "Tip", SystemIcons.Information.ToBitmap()))
d.ShowDialog();
{
if (owner != null)
d.StartPosition = FormStartPosition.CenterParent;
d.ShowDialog(owner);
}
}

private TipDialog()
Expand All @@ -43,16 +47,13 @@ private TipDialog()
dontShowThisAgainButton1.CloseIfShouldNotShow(Settings.Default, Message);
}

/// <summary>
/// Use this one if you need to customize the dialog, e.g. to setup an alternate button
/// </summary>
public TipDialog(string message, string dialogTitle, Image icon) : this()
{
if (icon != null)
_icon.Image = icon;
Icon = icon;

Text = dialogTitle;
_message.Text = message;
Message = message;
}

private void _acceptButton_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -104,10 +105,5 @@ private int GetDesiredTextBoxHeight()
}
}

private void TipDialog_Load(object sender, EventArgs e)
{

}

}
}
}
3 changes: 1 addition & 2 deletions src/L10NSharp/UI/TipDialog.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 24 additions & 16 deletions src/L10NSharp/XLiffUtils/XLiffLocalizationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,24 +269,27 @@ public void Dispose()

#region Methods for showing localization dialog box
/// ------------------------------------------------------------------------------------
public void ShowLocalizationDialogBox(bool runInReadonlyMode)
public void ShowLocalizationDialogBox(bool runInReadonlyMode, IWin32Window owner = null)
{
LocalizeItemDlg<XLiffDocument>.ShowDialog(this, "", runInReadonlyMode);
LocalizeItemDlg<XLiffDocument>.ShowDialog(this, "", runInReadonlyMode, owner);
}

/// ------------------------------------------------------------------------------------
public static void ShowLocalizationDialogBox(IComponent component)
public static void ShowLocalizationDialogBox(IComponent component,
IWin32Window owner = null)
{
TipDialog.Show("If you click on an item while you hold alt and shift keys down, this tool will open up with that item already selected.");
if (owner == null)
owner = (component as Control)?.FindForm();
TipDialog.ShowAltShiftClickTip(owner);
LocalizeItemDlg<XLiffDocument>.ShowDialog(LocalizationManagerInternal<XLiffDocument>.GetLocalizationManagerForComponent(component),
component, false);
component, false, owner);
}

/// ------------------------------------------------------------------------------------
public static void ShowLocalizationDialogBox(string id)
public static void ShowLocalizationDialogBox(string id, IWin32Window owner = null)
{
TipDialog.Show("If you click on an item while you hold alt and shift keys down, this tool will open up with that item already selected.");
LocalizeItemDlg<XLiffDocument>.ShowDialog(LocalizationManagerInternal<XLiffDocument>.GetLocalizationManagerForString(id), id, false);
TipDialog.ShowAltShiftClickTip(owner);
LocalizeItemDlg<XLiffDocument>.ShowDialog(LocalizationManagerInternal<XLiffDocument>.GetLocalizationManagerForString(id), id, false, owner);
}

#endregion
Expand Down Expand Up @@ -992,6 +995,7 @@ internal void HandleToolStripItemMouseDown(object sender, MouseEventArgs e)
// Make sure all drop-downs are closed that are in the
// chain of menu items for this item.
var tsddi = sender as ToolStripDropDownItem;
var owningForm = tsddi?.Owner?.FindForm();
while (tsddi != null)
{
tsddi.DropDown.Close();
Expand All @@ -1002,7 +1006,8 @@ internal void HandleToolStripItemMouseDown(object sender, MouseEventArgs e)
tsddi = tsddi.OwnerItem as ToolStripDropDownItem;
}

LocalizeItemDlg<XLiffDocument>.ShowDialog(this, (IComponent)sender, false);
LocalizeItemDlg<XLiffDocument>.ShowDialog(this, (IComponent)sender, false,
owningForm);
}

private static bool DoHandleMouseDown
Expand Down Expand Up @@ -1065,7 +1070,7 @@ internal void HandleControlMouseDown(object sender, MouseEventArgs e)
var lm = LocalizationManagerInternal<XLiffDocument>.GetLocalizationManagerForComponent(ctrl);

LocalizationManager.OnLaunchingLocalizationDialog(lm);
LocalizeItemDlg<XLiffDocument>.ShowDialog(lm, ctrl, false);
LocalizeItemDlg<XLiffDocument>.ShowDialog(lm, ctrl, false, ctrl?.FindForm());
LocalizationManager.OnClosingLocalizationDialog(lm);
}

Expand Down Expand Up @@ -1131,9 +1136,9 @@ internal void HandleListViewColumnHeaderClicked(object sender, ColumnClickEventA
if (!DoHandleMouseDown)
return;

var lv = sender as ListView;
if (lv != null && ComponentCache.ContainsKey(lv.Columns[e.Column]))
LocalizeItemDlg<XLiffDocument>.ShowDialog(this, lv.Columns[e.Column], false);
if (sender is ListView lv && ComponentCache.ContainsKey(lv.Columns[e.Column]))
LocalizeItemDlg<XLiffDocument>.ShowDialog(this, lv.Columns[e.Column], false,
lv.FindForm());
}

/// ------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1173,9 +1178,12 @@ internal void HandleDataGridViewCellMouseDown(object sender, DataGridViewCellMou
if (!DoHandleMouseDown)
return;

var grid = sender as DataGridView;
if (grid != null && e.RowIndex < 0 && ComponentCache.ContainsKey(grid.Columns[e.ColumnIndex]))
LocalizeItemDlg<XLiffDocument>.ShowDialog(this, grid.Columns[e.ColumnIndex], false);
if (sender is DataGridView grid && e.RowIndex < 0 &&
ComponentCache.ContainsKey(grid.Columns[e.ColumnIndex]))
{
LocalizeItemDlg<XLiffDocument>.ShowDialog(this, grid.Columns[e.ColumnIndex], false,
grid.FindForm());
}
}

/// ------------------------------------------------------------------------------------
Expand Down

0 comments on commit 7bc8ee4

Please sign in to comment.