-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FrmMainApp.Startup.cs
384 lines (338 loc) · 15 KB
/
FrmMainApp.Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using GeoTagNinja.Helpers;
using GeoTagNinja.View.DialogAndMessageBoxes;
using static GeoTagNinja.Helpers.HelperControlAndMessageBoxHandling;
namespace GeoTagNinja;
public partial class FrmMainApp
{
/// <summary>
/// Calls the InitializeComponent
/// </summary>
private Task AppStartupInitializeComponentFrmMainApp()
{
// InitializeComponent();
Log.Info(message: "Starting");
try
{
InitializeComponent();
}
catch (Exception ex)
{
Log.Fatal(message: "Error: " + ex.Message);
CustomMessageBox customMessageBox = new(
text: ReturnControlText(
controlName: "mbx_FrmMainApp_ErrorInitializeComponent",
fakeControlType: FakeControlTypes.MessageBox) +
ex.Message,
caption: ReturnControlText(
controlName: MessageBoxCaption.Error.ToString(),
fakeControlType: FakeControlTypes.MessageBoxCaption),
buttons: MessageBoxButtons.OK,
icon: MessageBoxIcon.Error);
customMessageBox.ShowDialog();
}
return Task.CompletedTask;
}
/// <summary>
/// Enables double-buffering so that the listview doesn't flicker
/// </summary>
private Task AppStartupEnableDoubleBuffering()
{
Log.Info(message: "Starting");
try
{
lvw_FileList.DoubleBuffered(enable: true);
}
catch (Exception ex)
{
Log.Fatal(message: "Error: " + ex.Message);
CustomMessageBox customMessageBox = new(
text: ReturnControlText(
controlName: "mbx_FrmMainApp_ErrorDoubleBuffer",
fakeControlType: FakeControlTypes.MessageBox) +
ex.Message,
caption: ReturnControlText(
controlName: MessageBoxCaption.Error.ToString(),
fakeControlType: FakeControlTypes.MessageBoxCaption),
buttons: MessageBoxButtons.OK,
icon: MessageBoxIcon.Error);
customMessageBox.ShowDialog();
}
return Task.CompletedTask;
}
/// <summary>
/// Assigns labels to various objects in the application during startup. This includes buttons, labels, checkboxes, and
/// other UI elements.
/// It also sets up tooltips for specific controls. The labels and tooltips are fetched from a data source using the
/// HelperControlAndMessageBoxHandling.ReturnControlText method.
/// </summary>
private void AppStartupAssignLabelsToObjects()
{
Log.Info(message: "Starting");
HelperNonStatic helperNonstatic = new();
IEnumerable<Control> c = helperNonstatic.GetAllControls(control: this);
string objectName;
string objectText;
foreach (Control cItem in c)
{
if (
cItem.GetType() == typeof(MenuStrip) ||
cItem.GetType() == typeof(ToolStrip) ||
cItem.GetType() == typeof(Label) ||
cItem.GetType() == typeof(Button) ||
cItem.GetType() == typeof(CheckBox) ||
cItem.GetType() == typeof(TabPage) ||
cItem.GetType() == typeof(ToolStripButton) ||
(cItem.GetType() == typeof(ListView) && cItem.Name != "lvw_FileView")
// cItem.GetType() == typeof(ToolTip) // tooltips are not controls.
)
{
if (cItem.Name == "lbl_ParseProgress")
{
objectName = cItem.Name;
objectText = ReturnControlText(controlName: "lbl_ParseProgress_Normal",
fakeControlType: FakeControlTypes.Label)
;
cItem.Text = objectText;
Log.Trace(message: "" + objectName + ": " + objectText);
}
else if (cItem is ToolStrip ts)
{
// https://www.codeproject.com/Messages/3329190/How-to-convert-a-Control-into-a-ToolStripButton.aspx
foreach (ToolStripItem tsi in ts.Items)
{
if (tsi is ToolStripButton tsb)
{
objectName = tsb.Name;
objectText = ReturnControlText(controlName: objectName,
fakeControlType: FakeControlTypes.ToolStripButton);
tsb.ToolTipText = objectText;
Log.Trace(message: "" + objectName + ": " + objectText);
}
}
}
else if (cItem is ListView lvw)
{
foreach (ColumnHeader columnHeader in lvw.Columns)
{
// this is entirely stupid but .Name in this case returns nothing of use even though it's hard-coded in the Designer.
// alas .Text works -- fml.
objectName = columnHeader.Text;
objectText = ReturnControlText(controlName: objectName,
fakeControlType: FakeControlTypes.ColumnHeader);
columnHeader.Text = objectText;
columnHeader.Width = 120; // arbitrary
Log.Trace(message: "" + objectName + ": " + objectText);
}
}
else
{
objectName = cItem.Name;
ReturnControlText(cItem: cItem, senderForm: this);
Log.Trace(message: "" + objectName + ": " + cItem.Text);
}
}
}
// Text for ImagePreview
pbx_imagePreview.EmptyText = ReturnControlText(controlName: "pbx_imagePreviewEmptyText",
fakeControlType: FakeControlTypes.PictureBox);
// The MenuStrip logic above is not working
List<ToolStripItem> allMenuItems = new();
foreach (ToolStripItem toolItem in mns_MenuStrip.Items)
{
allMenuItems.Add(item: toolItem);
Log.Trace(message: "Menu: " + toolItem.Name);
//add sub items - not logging this.
allMenuItems.AddRange(collection: helperNonstatic.GetMenuItems(item: toolItem));
}
foreach (ToolStripItem toolItem in cms_FileListView.Items)
{
allMenuItems.Add(item: toolItem);
//add sub items
allMenuItems.AddRange(collection: helperNonstatic.GetMenuItems(item: toolItem));
}
foreach (ToolStripMenuItem cItem in allMenuItems.OfType<ToolStripMenuItem>())
{
objectName = cItem.Name;
objectText = ReturnControlText(controlName: objectName,
fakeControlType: FakeControlTypes.ToolStripMenuItem);
cItem.Text = objectText;
Log.Trace(message: objectName + ": " + objectText);
}
Log.Trace(message: "Setting Tooltips");
List<(ToolTip, Control, string)> ttpLabelsList = new()
{
(ttp_loctToFile, btn_loctToFile, "ttp_loctToFile"),
(ttp_loctToFileDestination, btn_loctToFileDestination, "ttp_loctToFileDestination"),
(ttp_NavigateMapGo, btn_NavigateMapGo, "ttp_NavigateMapGo"),
(ttp_SaveFavourite, btn_SaveFavourite, "ttp_SaveFavourite"),
(ttp_LoadFavourite, btn_LoadFavourite, "ttp_LoadFavourite"),
(ttp_ManageFavourites, btn_ManageFavourites, "ttp_ManageFavourites")
};
foreach ((ToolTip, Control, string) valueTuple in ttpLabelsList)
{
ToolTip ttp = valueTuple.Item1;
ttp.SetToolTip(control: valueTuple.Item2,
caption: ReturnControlText(controlName: valueTuple.Item3,
fakeControlType: FakeControlTypes.ToolTip));
}
}
/// <summary>
/// Pulls the last lat/lng combo from Settings if available, otherwise points to NASA's HQ
/// </summary>
private void AppStartupGetLastLatLngFromSettings()
{
Log.Info(message: "Starting");
try
{
nud_lat.Text = HelperDataApplicationSettings.DataReadSQLiteSettings(
dataTable: HelperVariables.DtHelperDataApplicationSettings,
settingTabPage: "generic",
settingId: "lastLat"
);
if (nud_lat.Text != null)
{
nud_lat.Value = Convert.ToDecimal(value: nud_lat.Text, provider: CultureInfo.CurrentCulture);
}
nud_lng.Text = HelperDataApplicationSettings.DataReadSQLiteSettings(
dataTable: HelperVariables.DtHelperDataApplicationSettings,
settingTabPage: "generic",
settingId: "lastLng"
);
if (nud_lng.Text != null)
{
nud_lng.Value = Convert.ToDecimal(value: nud_lng.Text, provider: CultureInfo.CurrentCulture);
}
}
catch
{
// ignored
}
if (nud_lat.Text == "" || nud_lat.Text == "0")
{
// NASA HQ
string defaultLat = "38.883056";
string defaultLng = "-77.016389";
nud_lat.Text = defaultLat;
nud_lng.Text = defaultLng;
nud_lat.Value = Convert.ToDecimal(value: defaultLat, provider: CultureInfo.InvariantCulture);
nud_lng.Value = Convert.ToDecimal(value: defaultLng, provider: CultureInfo.InvariantCulture);
}
HelperVariables.HsMapMarkers.Clear();
HelperVariables.HsMapMarkers.Add(item: (nud_lat.Text.Replace(oldChar: ',', newChar: '.'),
nud_lng.Text.Replace(oldChar: ',', newChar: '.')));
HelperVariables.LastLat = double.Parse(s: nud_lat.Text.Replace(oldChar: ',', newChar: '.'),
provider: CultureInfo.InvariantCulture);
HelperVariables.LastLng = double.Parse(s: nud_lng.Text.Replace(oldChar: ',', newChar: '.'),
provider: CultureInfo.InvariantCulture);
}
/// <summary>
/// Sets the application theme at startup based on the user's settings.
/// </summary>
/// <remarks>
/// If the user has chosen to use dark mode, the method sets the theme color to dark and applies a custom renderer to
/// the menu strip.
/// If the user has not chosen to use dark mode, the method sets the theme color to light and uses the default
/// rendering for the controls.
/// </remarks>
private Task AppStartupSetAppTheme()
{
// the custom logic is ugly af so no need to be pushy about it in light mode.
if (!HelperVariables.UserSettingUseDarkMode)
{
tcr_Main.DrawMode = TabDrawMode.Normal;
lvw_FileList.OwnerDraw = false;
lvw_ExifData.OwnerDraw = false;
}
else
{
mns_MenuStrip.Renderer = new DarkMenuStripRenderer();
}
// adds colour/theme
HelperControlThemeManager.SetThemeColour(
themeColour: HelperVariables.UserSettingUseDarkMode
? ThemeColour.Dark
: ThemeColour.Light, parentControl: this);
return Task.CompletedTask;
}
/// <summary>
/// Reads the data in SQLite for panel widths/heights/sizes and applies them if available.
/// </summary>
[SuppressMessage(category: "ReSharper", checkId: "InconsistentNaming")]
private void AppStartupApplyVisualStyleDefaults()
{
Log.Info(message: "Starting");
// there should be a better way of doing this.
// reflections could do it and i asked GPT on the how-part but it only gave options for storing and retrieving _all_ the controls and _all_ their details, which isn't something i'd like.
Dictionary<string, int> settingsApplicationDesignValuesDict = new()
{
{ "splitContainerMainSplitterDistance", 0 },
{ "splitContainerLeftTopSplitterDistance", 0 }
};
// need to make it into a list else the foreach complains that the collection has been modfied.
List<string> settingsApplicationDesignValuesKeysList = settingsApplicationDesignValuesDict.Keys.ToList();
foreach (string settingsApplicationDesignValue
in settingsApplicationDesignValuesKeysList)
{
string dataInSQL =
HelperDataApplicationSettings.DataReadSQLiteSettings(
dataTable: HelperVariables.DtHelperDataApplicationSettings,
settingTabPage: "generic",
settingId: settingsApplicationDesignValue, returnBlankIfNull: true);
Log.Debug(
message:
$"Reading settingsApplicationDesignValue {settingsApplicationDesignValue}, dataInSQL {dataInSQL}.");
bool parsedDataInSQLSuccessfully = int.TryParse(s: dataInSQL,
style: NumberStyles.Any,
provider: CultureInfo.InvariantCulture,
result: out int parsedSQLValueInt);
if (!string.IsNullOrWhiteSpace(value: dataInSQL) && parsedDataInSQLSuccessfully)
{
settingsApplicationDesignValuesDict[key: settingsApplicationDesignValue] = parsedSQLValueInt;
}
}
foreach (KeyValuePair<string, int> settingsApplicationDesignValue in settingsApplicationDesignValuesDict)
{
checkAssignSingleValues(dictValueKey: settingsApplicationDesignValue.Key);
}
void checkAssignSingleValues(string dictValueKey)
{
int valToAssign = settingsApplicationDesignValuesDict[key: dictValueKey];
Log.Debug(
message:
$"Assinging value {valToAssign} to {dictValueKey}.");
if (valToAssign > 0)
{
switch (dictValueKey)
{
case "splitContainerMainSplitterDistance":
splitContainerMain.SplitterDistance = valToAssign;
break;
case "splitContainerLeftTopSplitterDistance":
splitContainerLeftTop.SplitterDistance = valToAssign;
break;
}
}
}
string TrimEnd(string source, string value)
{
return !source.EndsWith(value: value)
? source
: source.Remove(startIndex: source.LastIndexOf(value: value));
}
Log.Debug(message: "Done");
}
private void splitContainerControl_Paint(object sender, PaintEventArgs e)
{
// https://stackoverflow.com/a/16006968
splitContainerMain.Paint -= splitContainerControl_Paint;
// Handle restoration here
AppStartupApplyVisualStyleDefaults();
}
}