33 internal class FormHelper
35 public static void FillInFrom(
GPALControl controlEntry,
string useText, WriteMode writeMode)
39 case FormControlType.Input:
40 case FormControlType.TextArea:
48 public static void FillInWithTokens(
GPALForm myForm, List<IGPALGrid<string>> tokenList, WriteMode writeMode)
50 foreach (IGPALGrid<string> tokens
in tokenList)
53 for (
int tokenIdx = 0; tokenIdx < tokens.Count(); tokenIdx++)
56 List<string> currentRow = tokens[tokenIdx];
58 foreach (
string token
in currentRow)
60 bool textFilled =
false;
62 while (
false == textFilled && elementIdx < myForm.ControlList.Count)
64 switch (myForm.ControlList[elementIdx].ControlType)
66 case FormControlType.TextArea:
67 case FormControlType.Input:
68 myForm.Form.SwitchToTab(myForm.ControlList[elementIdx]);
69 FillInFrom(myForm.ControlList[elementIdx], token, writeMode);
80 if (elementIdx == myForm.ControlList.Count &&
false == textFilled)
81 GPAL.
PublishSimpleEvent(GPALEventType.WARNING, $
"Unused token: [{token}]", myForm, GPALObjectType.GPALForm);
85 while (elementIdx < myForm.ControlList.Count)
87 switch (myForm.ControlList[elementIdx].ControlType)
89 case FormControlType.TextArea:
90 case FormControlType.Input:
91 GPAL.
PublishSimpleEvent(GPALEventType.WARNING, $
"No token for control: [{myForm.ControlList[elementIdx].Name}]", myForm, GPALObjectType.GPALForm);
101 if (
null != myForm.Form.CallAfterFillIn)
103 CallIfStatus handled = myForm.Form.CallAfterFillIn(myForm, tokens, tokenIdx);
105 if (CallIfStatus.Terminate == handled)
107 GPAL.
PublishSimpleEvent(GPALEventType.INFO, $
"CallAfterFillIn handler [{myForm.Form.CallAfterFillIn.GetInvocationList()[0].Method.Name}] requested program termination.", myForm, GPALObjectType.GPALForm);
108 throw new GPALException($
"{GPAL.MyMethodName()}: CallAfterFillIn handler [{myForm.Form.CallAfterFillIn.GetInvocationList()[0].Method.Name}] requested program termination.");
114 public static HWND WaitForWindow(
string windowTitle,
int waitTimeInSeconds = 10)
116 return WaitForWindowGeneric(windowTitle,
false, waitTimeInSeconds);
119 public static HWND WaitForWindowRegex(
string windowTitleRegex,
int waitTimeInSeconds)
121 return WaitForWindowGeneric(windowTitleRegex,
true, waitTimeInSeconds);
124 public static HWND WaitForWindowGeneric(
string waitForTitle,
bool useRegex,
int waitTimeInSeconds)
127 HWND retHWND = IntPtr.Zero;
129 for (
int cnt = 0; cnt < waitTimeInSeconds &&
false == found; cnt++)
132 IDictionary<HWND, string> windows = GetOpenWindows();
133 foreach (var entry
in windows)
135 if (
true == useRegex)
137 MatchCollection mc = Regex.Matches(entry.Value, waitForTitle);
147 if (
true == waitForTitle.Equals(entry.Value))
163 public static IDictionary<HWND, string> GetOpenWindows()
165 HWND shellWindow = GetShellWindow();
166 Dictionary<HWND, string> windows =
new Dictionary<HWND, string>();
168 EnumWindows(delegate (HWND hWnd,
int lParam)
170 if (hWnd == shellWindow)
return true;
171 if (!IsWindowVisible(hWnd))
return true;
173 int length = GetWindowTextLength(hWnd);
174 if (length == 0)
return true;
176 StringBuilder builder =
new StringBuilder(length);
177 GetWindowText(hWnd, builder, length + 1);
179 windows[hWnd] = builder.ToString();
187 private delegate
bool EnumWindowsProc(HWND hWnd,
int lParam);
189 [DllImport(
"USER32.DLL")]
190 private static extern bool EnumWindows(EnumWindowsProc enumFunc,
int lParam);
192 [DllImport(
"USER32.DLL")]
193 private static extern int GetWindowText(HWND hWnd, StringBuilder lpString,
int nMaxCount);
195 [DllImport(
"USER32.DLL")]
196 private static extern int GetWindowTextLength(HWND hWnd);
198 [DllImport(
"USER32.DLL")]
199 private static extern bool IsWindowVisible(HWND hWnd);
201 [DllImport(
"USER32.DLL")]
202 private static extern IntPtr GetShellWindow();