-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomRibbon.cs
509 lines (442 loc) · 17.3 KB
/
CustomRibbon.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using Office = Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Linq;
using System.Drawing.Drawing2D;
namespace fieldMarker
{
[ComVisible(true)]
public class CustomRibbon : Office.IRibbonExtensibility
{
private Office.IRibbonUI ribbon;
private Word.WdColorIndex selectedColor = Word.WdColorIndex.wdNoHighlight;
private Dictionary<string, Word.WdColorIndex> colorMap = new Dictionary<string, Word.WdColorIndex>()
{
{ "btnNoColor", Word.WdColorIndex.wdNoHighlight },
{ "btnYellow", Word.WdColorIndex.wdYellow },
{ "btnGreen", Word.WdColorIndex.wdBrightGreen },
{ "btnTurquoise", Word.WdColorIndex.wdTurquoise },
{ "btnPink", Word.WdColorIndex.wdPink },
{ "btnRed", Word.WdColorIndex.wdRed },
{ "btnBlue", Word.WdColorIndex.wdBlue },
{ "btnDarkBlue", Word.WdColorIndex.wdDarkBlue },
{ "btnTeal", Word.WdColorIndex.wdTeal },
{ "btnGray", Word.WdColorIndex.wdGray25 }
};
public CustomRibbon()
{
}
#region IRibbonExtensibility Members
public string GetCustomUI(string ribbonID)
{
string resourceName = "fieldMarker.CustomRibbon.xml";
Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream != null)
{
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
return null;
}
#endregion
#region Ribbon Callbacks
public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
this.ribbon = ribbonUI;
}
public void OnColorButtonClick(Office.IRibbonControl control)
{
try
{
ApplyHighlight();
}
catch (Exception ex)
{
ShowNotification($"Error applying highlight: {ex.Message}", "Error", true);
}
}
public void OnColorSelected(Office.IRibbonControl control)
{
try
{
if (control != null && control.Id != null && colorMap.ContainsKey(control.Id))
{
selectedColor = colorMap[control.Id];
ApplyHighlight();
}
}
catch (Exception ex)
{
ShowNotification($"Error selecting color: {ex.Message}", "Error", true);
}
}
public void OnInfoButtonClick(Office.IRibbonControl control)
{
var result = MessageBox.Show(
"Developer: Burak Can KARA\nEmail: [email protected]\nGitHub: github.com/bcankara\n\nClick OK to visit GitHub profile",
"Field Marker Add-In",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
try
{
Process.Start(new ProcessStartInfo
{
FileName = "https://github.com/bcankara",
UseShellExecute = true
});
}
catch (Exception ex)
{
ShowNotification($"Error opening link: {ex.Message}", "Error", true);
}
}
}
#endregion
#region Image Getters
public Bitmap GetInfoImage(Office.IRibbonControl control)
{
return Properties.Resources.info;
}
public Bitmap GetColorPickerImage(Office.IRibbonControl control)
{
return Properties.Resources.color_picker;
}
public Bitmap GetNoColorImage(Office.IRibbonControl control)
{
return Properties.Resources.color_nocolor;
}
public Bitmap GetYellowImage(Office.IRibbonControl control)
{
return Properties.Resources.color_yellow;
}
public Bitmap GetGreenImage(Office.IRibbonControl control)
{
return Properties.Resources.color_green;
}
public Bitmap GetTurquoiseImage(Office.IRibbonControl control)
{
return Properties.Resources.color_turquoise;
}
public Bitmap GetPinkImage(Office.IRibbonControl control)
{
return Properties.Resources.color_pink;
}
public Bitmap GetRedImage(Office.IRibbonControl control)
{
return Properties.Resources.color_red;
}
public Bitmap GetBlueImage(Office.IRibbonControl control)
{
return Properties.Resources.color_blue;
}
public Bitmap GetDarkBlueImage(Office.IRibbonControl control)
{
return Properties.Resources.color_darkblue;
}
public Bitmap GetTealImage(Office.IRibbonControl control)
{
return Properties.Resources.color_teal;
}
public Bitmap GetGrayImage(Office.IRibbonControl control)
{
return Properties.Resources.color_gray;
}
#endregion
#region Helper Methods
private void ShowNotification(string message, string title, bool isError = false)
{
NotificationForm.Show(message, title, isError);
}
private class ModernProgressForm : Form
{
private readonly ProgressBar progressBar;
private readonly Label statusLabel;
private readonly Label percentLabel;
private readonly Label completionLabel;
private readonly Button okButton;
public ModernProgressForm()
{
this.Text = "Field Marker Progress";
this.Width = 400;
this.Height = 200;
this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPosition.CenterScreen;
this.BackColor = Color.White;
this.TopMost = true;
// Form gölgesi ve yuvarlak köşeler
this.Paint += (s, e) =>
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (var path = new GraphicsPath())
{
path.AddRoundedRectangle(this.ClientRectangle, 10);
e.Graphics.FillPath(new SolidBrush(this.BackColor), path);
}
};
// Form sürükleme için mouse events
bool isDragging = false;
Point dragStartPoint = Point.Empty;
this.MouseDown += (s, e) =>
{
if (e.Button == MouseButtons.Left)
{
isDragging = true;
dragStartPoint = new Point(e.X, e.Y);
}
};
this.MouseMove += (s, e) =>
{
if (isDragging)
{
Point p = PointToScreen(e.Location);
Location = new Point(p.X - dragStartPoint.X, p.Y - dragStartPoint.Y);
}
};
this.MouseUp += (s, e) =>
{
isDragging = false;
};
// Başlık etiketi
var titleLabel = new Label
{
Text = "Processing Fields",
Font = new Font("Segoe UI", 12, FontStyle.Bold),
ForeColor = Color.FromArgb(64, 64, 64),
AutoSize = false,
TextAlign = ContentAlignment.MiddleCenter,
Width = 360,
Height = 30,
Location = new Point(20, 15)
};
// Progress bar
progressBar = new ProgressBar
{
Width = 360,
Height = 23,
Location = new Point(20, 55),
Style = ProgressBarStyle.Continuous
};
// Durum etiketi
statusLabel = new Label
{
AutoSize = false,
TextAlign = ContentAlignment.MiddleLeft,
Width = 280,
Height = 23,
Location = new Point(20, 90),
Font = new Font("Segoe UI", 9),
ForeColor = Color.FromArgb(64, 64, 64)
};
// Yüzde etiketi
percentLabel = new Label
{
AutoSize = false,
TextAlign = ContentAlignment.MiddleRight,
Width = 80,
Height = 23,
Location = new Point(300, 90),
Font = new Font("Segoe UI", 9, FontStyle.Bold),
ForeColor = Color.FromArgb(64, 64, 64)
};
// Tamamlanma mesajı etiketi
completionLabel = new Label
{
AutoSize = false,
TextAlign = ContentAlignment.MiddleCenter,
Width = 360,
Height = 30,
Location = new Point(20, 120),
Font = new Font("Segoe UI", 9),
ForeColor = Color.FromArgb(64, 64, 64),
Visible = false
};
// OK Butonu
okButton = new Button
{
Text = "✓ OK",
Width = 120,
Height = 35,
Location = new Point((this.Width - 120) / 2, 155),
FlatStyle = FlatStyle.Flat,
Font = new Font("Segoe UI", 11, FontStyle.Bold),
Visible = false,
Name = "okButton",
BackColor = Color.FromArgb(0, 120, 212),
ForeColor = Color.White,
Cursor = Cursors.Hand
};
// Buton köşelerini yuvarla
okButton.Paint += (s, e) =>
{
var buttonPath = new GraphicsPath();
var rect = new Rectangle(0, 0, okButton.Width, okButton.Height);
buttonPath.AddRoundedRectangle(rect, 5);
okButton.Region = new Region(buttonPath);
};
// Buton efektleri
okButton.MouseEnter += (s, e) => okButton.BackColor = Color.FromArgb(0, 102, 204);
okButton.MouseLeave += (s, e) => okButton.BackColor = Color.FromArgb(0, 120, 212);
okButton.Click += (s, e) => this.Close();
this.Controls.AddRange(new Control[] { titleLabel, progressBar, statusLabel, percentLabel, completionLabel, okButton });
// ESC tuşu ile kapatma
this.KeyPreview = true;
this.KeyDown += (s, e) =>
{
if (e.KeyCode == Keys.Escape && okButton.Visible)
{
this.Close();
}
};
// Enter tuşu ile kapatma
this.AcceptButton = okButton;
}
public void UpdateProgress(int current, int total)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() => UpdateProgress(current, total)));
return;
}
int percentage = (int)((current / (double)total) * 100);
progressBar.Value = percentage;
statusLabel.Text = $"Processing field {current} of {total}";
percentLabel.Text = $"{percentage}%";
Application.DoEvents();
}
public void ShowCompletion(string message)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() => ShowCompletion(message)));
return;
}
completionLabel.Text = message;
completionLabel.Visible = true;
progressBar.Value = 100;
okButton.Visible = true;
okButton.Focus();
statusLabel.Text = "Operation completed successfully";
percentLabel.Text = "100%";
// Progress bar'ı yeşil yap
progressBar.ForeColor = Color.FromArgb(40, 167, 69);
this.TopMost = true;
Application.DoEvents();
// Başlık metnini güncelle
var titleLabel = this.Controls.OfType<Label>().FirstOrDefault(l => l.Font.Size == 12);
if (titleLabel != null)
{
titleLabel.Text = "Operation Complete";
}
}
protected override CreateParams CreateParams
{
get
{
const int CS_DROPSHADOW = 0x20000;
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
}
private void ApplyHighlight()
{
ModernProgressForm progressForm = null;
try
{
var app = Globals.ThisAddIn.Application;
if (app == null || app.ActiveDocument == null)
{
MessageBox.Show("Word document is not available.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var document = app.ActiveDocument;
var fields = document.Fields.Cast<Word.Field>().ToList();
int totalFields = fields.Count;
if (totalFields == 0)
{
MessageBox.Show("No fields found in the document.", "No Fields Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
progressForm = new ModernProgressForm();
progressForm.Show();
int fieldCount = 0;
// Screen updating'i kapat
app.ScreenUpdating = false;
foreach (Word.Field field in fields)
{
try
{
if (field?.Result != null)
{
// Alan kodunun tüm aralığını seç
var range = field.Result;
range.HighlightColorIndex = selectedColor;
fieldCount++;
// İlerleme çubuğunu güncelle
progressForm.UpdateProgress(fieldCount, totalFields);
}
}
catch (Exception)
{
continue;
}
}
// Screen updating'i geri aç
app.ScreenUpdating = true;
if (fieldCount > 0)
{
string colorName = colorMap.FirstOrDefault(x => x.Value == selectedColor).Key?.Replace("btn", "");
string message = $"{fieldCount} fields {(selectedColor == Word.WdColorIndex.wdNoHighlight ? "cleared" : $"highlighted with {colorName} color")}.";
progressForm.ShowCompletion(message);
}
else
{
progressForm.Close();
}
}
catch (Exception ex)
{
if (progressForm != null && !progressForm.IsDisposed)
{
progressForm.Close();
}
MessageBox.Show($"Error processing fields: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
// Screen updating'in açık olduğundan emin ol
if (Globals.ThisAddIn.Application != null)
{
Globals.ThisAddIn.Application.ScreenUpdating = true;
}
}
}
#endregion
}
public static class GraphicsExtensions
{
public static void AddRoundedRectangle(this GraphicsPath path, Rectangle bounds, int radius)
{
path.AddArc(bounds.X, bounds.Y, radius * 2, radius * 2, 180, 90);
path.AddArc(bounds.Right - radius * 2, bounds.Y, radius * 2, radius * 2, 270, 90);
path.AddArc(bounds.Right - radius * 2, bounds.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
path.AddArc(bounds.X, bounds.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
path.CloseFigure();
}
}
}