This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Ribbon.cs
674 lines (571 loc) · 17.4 KB
/
Ribbon.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
using System;
using System.Collections.Generic;
using Cairo;
using Gtk;
namespace Ribbons
{
/// <summary>Ribbon widget.</summary>
public class Ribbon : Container
{
private enum KeyTipLevel { None, TopLevel, Tab };
private static double borderWidth = 2.0;
private static double space = 2.0;
private static double pagePadding = 3.0;
private static double tabPadding = 4.0;
private static double minimalTabsHorizontalPosition = 8.0;
private static double lineWidth = 1.0;
private static double roundSize = 4.0;
protected ColorScheme colorScheme = new ColorScheme ();
protected Theme theme = Theme.DefaultTheme;
protected List<RibbonPage> pages;
protected int curPageIndex;
protected ApplicationButton appButton;
protected QuickAccessToolbar toolbar;
protected Widget shortcuts;
private Gdk.Rectangle bodyAllocation, pageAllocation;
private Gtk.Requisition appButtonRequisition;
private Gtk.Requisition toolbarRequisition;
private Gtk.Requisition shortcutsRequisition;
private Gtk.Requisition pageRequisition;
private double headerHeight;
private KeyTipLevel queuedKeyTipLevel = KeyTipLevel.None;
private List<KeyTip> tabKeyTips;
public event PageSelectedHandler PageSelected;
public event PageAddedHandler PageAdded;
public event PageMovedHandler PageMoved;
public event PageRemovedHandler PageRemoved;
public ApplicationButton ApplicationButton
{
set
{
if(appButton != null) appButton.Unparent ();
appButton = value;
if(appButton != null)
{
appButton.Visible = true;
appButton.Parent = this;
}
ShowAll ();
}
get
{
return appButton;
}
}
public QuickAccessToolbar QuickAccessToolbar
{
set
{
if(toolbar != null) toolbar.Unparent ();
toolbar = value;
if(toolbar != null)
{
toolbar.Visible = true;
toolbar.Parent = this;
}
ShowAll ();
}
get
{
return toolbar;
}
}
/// <summary>Indix of the currently selected page.</summary>
/// <remakrs>Returns -1 if no page is selected.</remarks>
public int CurrentPageIndex
{
set
{
if(curPageIndex != -1)
{
CurrentPage.Label.ModifyFg (StateType.Normal, theme.GetForecolorForRibbonTabs (false));
CurrentPage.Page.Unparent ();
}
curPageIndex = value;
if(curPageIndex != -1)
{
CurrentPage.Label.ModifyFg (StateType.Normal, theme.GetForecolorForRibbonTabs (true));
CurrentPage.Page.Parent = this;
}
ShowAll ();
QueueDraw ();
}
get
{
return curPageIndex;
}
}
/// <summary>Currently selected page.</summary>
public RibbonPage CurrentPage
{
get
{
int idx = curPageIndex;
return idx == -1 ? null : pages[idx];
}
}
/// <summary>Number of pages.</summary>
public int NPages
{
get { return pages.Count; }
}
/// <summary>Shortcuts widget.</summary>
/// <remarks>The shortcuts widget is displayed next to the tabs.</remarks>
public Widget Shortcuts
{
set
{
if(shortcuts != null) shortcuts.Unparent ();
shortcuts = value;
if(shortcuts != null)
{
shortcuts.Visible = true;
shortcuts.Parent = this;
}
QueueDraw ();
}
get { return shortcuts; }
}
/// <summary>Theme used to draw the widget.</summary>
public Theme Theme
{
set
{
theme = value;
QueueDraw ();
}
get { return theme; }
}
/// <summary>Default constructor.</summary>
public Ribbon()
{
this.SetFlag (WidgetFlags.NoWindow);
this.AddEvents ((int)(Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask | Gdk.EventMask.PointerMotionMask));
this.pages = new List<RibbonPage> ();
this.tabKeyTips = new List<KeyTip> ();
this.curPageIndex = -1;
}
/// <summary>Adds a new page after all existing pages.</summary>
/// <param name="Child">The widget to use as the content of the page.</param>
/// <param name="Label">The widget to use as the tab.</param>
public void AppendPage (Widget Child, Widget Label)
{
InsertPage (Child, Label, -1);
}
/// <summary>Adds a new page before all existing pages.</summary>
/// <param name="Child">The widget to use as the content of the page.</param>
/// <param name="Label">The widget to use as the tab.</param>
public void PrependPage (Widget Child, Widget Label)
{
InsertPage (Child, Label, 0);
}
/// <summary>Adds a new page at the specified position.</summary>
/// <param name="Child">The widget to use as the content of the page.</param>
/// <param name="Label">The widget to use as the tab.</param>
/// <param name="Position">The index (starting at 0) at which the page must be inserted, or -1 to insert the page after all existing pages.</param>
public void InsertPage (Widget Child, Widget Label, int Position)
{
RibbonPage p = new RibbonPage (this, Child, Label);
if(Position == -1)
{
pages.Add (p);
}
else
{
pages.Insert (Position, p);
if(curPageIndex != -1)
{
if(Position <= curPageIndex)
++curPageIndex;
}
}
if(pages.Count == 1)
{
CurrentPageIndex = 0;
}
else
{
Label.ModifyFg (StateType.Normal, theme.GetForecolorForRibbonTabs (false));
}
Label.ButtonPressEvent += delegate (object sender, ButtonPressEventArgs evnt)
{
this.SelectRibbonPage (p);
};
Label.EnterNotifyEvent += delegate (object sender, EnterNotifyEventArgs evnt)
{
};
Label.LeaveNotifyEvent += delegate (object sender, LeaveNotifyEventArgs evnt)
{
};
OnPageAdded (new PageEventArgs (p));
for(int idx = Position + 1 ; idx < pages.Count ; ++idx)
{
OnPageSelected (new PageEventArgs (pages[idx]));
}
}
/// <summary>Removes the specified page.</summary>
/// <param name="PageNumber">Index of the page to remove.</param>
public void RemovePage (int PageNumber)
{
if(curPageIndex != -1)
{
if(PageNumber < curPageIndex)
{
--curPageIndex;
}
else if(PageNumber == curPageIndex)
{
curPageIndex = -1;
}
}
RibbonPage p = pages[PageNumber];
if(curPageIndex == -1)
pages.RemoveAt (pages.Count - 1);
else
pages.RemoveAt (PageNumber);
OnPageRemoved (new PageEventArgs (p));
}
/// <summary>Returns the index of the specified page given its content widget.</summary>
/// <param name="Child">The content of the page whose index must be returned.</param>
/// <returns>The index.</returns>
public int PageNum (Widget Child)
{
// Since it is unlikely that the widget will containe more than
// a dozen pages, it is just fine to do a linear search.
for(int i = 0, i_up = pages.Count ; i < i_up ; ++i)
if(pages[i].Page == Child)
return i;
return -1;
}
/// <summary>Returns the index of the specified page.</summary>
/// <param name="Page">The page whose index must be returned.</param>
/// <returns>The RibbonPage.</returns>
public int RibbonPageNum (RibbonPage Page)
{
// Since it is unlikely that the widget will containe more than
// a dozen pages, it is just fine to do a linear search.
for(int i = 0, i_up = pages.Count ; i < i_up ; ++i)
if(pages[i] == Page)
return i;
return -1;
}
/// <summary>Sets the label widget of the specified page.</summary>
/// <param name="Page">The content of the page whose label must be modified.</param>
/// <param name="Label">The new label widget.</param>
public void SetPageLabel (Widget Child, Widget Label)
{
pages[PageNum (Child)].Label = Label;
}
/// <summary>Gets the label widget of the specified page.</summary>
/// <param name="Child">The content of the page whose label must be returned.</param>
/// <returns>The label widget.</returns>
public Widget GetPageLabel (Widget Child)
{
return pages[PageNum (Child)].Label;
}
/// <summary>Returns the content widget of the n-th page.</summary>
/// <param name="Position">Index of the page whose content has to be returned.</param>
/// <returns>The n-th page.</returns>
public Widget GetNthPage (int Position)
{
return pages[Position].Page;
}
/// <summary>Returns the n-th page.</summary>
/// <param name="Position">Index of the page to return.</param>
public RibbonPage GetNthRibbonPage (int Position)
{
return pages[Position];
}
/// <summary>Selects the specified page.</summary>
/// <param name="Page">The page to select.</param>
public void SelectRibbonPage (RibbonPage page)
{
int idx = RibbonPageNum (page);
if(idx != -1) CurrentPageIndex = idx;
OnPageSelected (new PageEventArgs (page));
}
/// <summary>Selects the previous page.</summary>
public void PrevPage ()
{
int i = CurrentPageIndex;
if(i > 0) CurrentPageIndex = i - 1;
}
/// <summary>Selects the next page.</summary>
public void NextPage ()
{
int i = CurrentPageIndex;
if(i < NPages - 1) CurrentPageIndex = i + 1;
}
public void AddTabKeyTip (KeyTip kt)
{
tabKeyTips.Add (kt);
}
public void RemoveTabKeyTip (KeyTip kt)
{
tabKeyTips.Remove (kt);
}
public void ClearTabKeyTips ()
{
tabKeyTips.Clear ();
}
protected override void ForAll (bool include_internals, Callback callback)
{
if(toolbar != null && toolbar.Visible)
{
callback (toolbar);
}
if(appButton != null && appButton.Visible)
{
callback (appButton);
}
if(Shortcuts != null && Shortcuts.Visible)
{
callback (Shortcuts);
}
foreach(RibbonPage p in pages) callback (p.Label);
if(CurrentPage != null)
{
callback (CurrentPage.Page);
}
}
protected override void OnSizeRequested (ref Requisition requisition)
{
base.OnSizeRequested (ref requisition);
RibbonPage page = CurrentPage;
double tabsWidth = 0, tabsHeight = 0;
foreach(RibbonPage p in pages)
{
Gtk.Requisition req = p.Label.SizeRequest ();
tabsWidth += req.Width;
tabsHeight = Math.Max (tabsHeight, req.Height);
p.LabelRequisition = req;
}
tabsWidth += pages.Count * 2 * tabPadding;
tabsHeight += 2 * tabPadding;
double headerWidth = tabsWidth;
if(shortcuts != null && shortcuts.Visible)
{
shortcutsRequisition = shortcuts.SizeRequest ();
double x = shortcutsRequisition.Width + space;
headerWidth += Math.Max (x, minimalTabsHorizontalPosition);
}
else
{
shortcutsRequisition = new Gtk.Requisition ();
headerWidth += minimalTabsHorizontalPosition;
}
headerHeight = Math.Max (tabsHeight, shortcutsRequisition.Height);
bool showToolbar = toolbar != null && toolbar.Visible;
bool showAppButton = appButton != null && appButton.Visible;
if(showToolbar) toolbarRequisition = toolbar.SizeRequest ();
if(showAppButton) appButtonRequisition = appButton.SizeRequest ();
if(showToolbar)
{
headerHeight += 2 * space + toolbarRequisition.Height;
}
if(showAppButton)
{
headerHeight = Math.Max (headerHeight, space + appButtonRequisition.Height);
}
double pageWidth = 0, pageHeight = 0;
if(page != null)
{
pageRequisition = page.Page.SizeRequest ();
pageWidth = pageRequisition.Width + 2 * pagePadding;
pageHeight = pageRequisition.Height + 2 * pagePadding;
}
else
{
pageRequisition = new Gtk.Requisition ();
}
double width = Math.Max (headerWidth, pageWidth);
width = borderWidth + width + borderWidth;
double height = borderWidth + headerHeight + pageHeight + borderWidth;
requisition.Width = (int)Math.Ceiling (width - double.Epsilon);
requisition.Height = (int)Math.Ceiling (height - double.Epsilon);
}
protected override void OnSizeAllocated (Gdk.Rectangle allocation)
{
base.OnSizeAllocated (allocation);
RibbonPage page = CurrentPage;
if(allocation.Height < headerHeight + borderWidth) return;
double headerBottom = allocation.Y + borderWidth + headerHeight;
double currentX = borderWidth;
bool showToolbar = toolbar != null && toolbar.Visible;
bool showAppButton = appButton != null && appButton.Visible;
if(showAppButton)
{
Gdk.Rectangle alloc;
alloc.X = (int)currentX;
alloc.Y = (int)(allocation.Y + borderWidth);
alloc.Width = Math.Min (appButtonRequisition.Width, (int)(allocation.Width - 2 * space));
alloc.Height = appButtonRequisition.Height;
appButton.SizeAllocate (alloc);
currentX += alloc.Width + space;
}
if(showToolbar)
{
Gdk.Rectangle alloc;
alloc.X = (int)currentX;
alloc.Y = (int)(allocation.Y + space);
alloc.Width = Math.Min (toolbarRequisition.Width, (int)(allocation.Width - (alloc.X - allocation.X) - space));
alloc.Height = toolbarRequisition.Height;
toolbar.SizeAllocate (alloc);
}
if(shortcuts != null && shortcuts.Visible)
{
Gdk.Rectangle alloc;
alloc.X = (int)currentX;
alloc.Y = (int)(headerBottom - shortcutsRequisition.Height);
alloc.Width = shortcutsRequisition.Width;
alloc.Height = shortcutsRequisition.Height;
shortcuts.SizeAllocate (alloc);
currentX += shortcutsRequisition.Width;
}
currentX += space;
currentX = Math.Max (currentX, minimalTabsHorizontalPosition);
foreach(RibbonPage p in pages)
{
Gdk.Rectangle alloc;
alloc.X = (int)(currentX + tabPadding);
alloc.Y = (int)(headerBottom - tabPadding - p.LabelRequisition.Height);
alloc.Width = p.LabelRequisition.Width;
alloc.Height = p.LabelRequisition.Height;
p.Label.SizeAllocate (alloc);
alloc.X = (int)currentX;
alloc.Y = (int)(headerBottom - tabPadding - p.LabelRequisition.Height - tabPadding);
alloc.Width = (int)(tabPadding + p.LabelRequisition.Width + tabPadding);
alloc.Height = (int)(tabPadding + p.LabelRequisition.Height + tabPadding);
p.SetLabelAllocation (alloc);
currentX += p.LabelRequisition.Width + 2 * tabPadding;
}
bodyAllocation.X = allocation.X + (int)borderWidth;
bodyAllocation.Y = (int)headerBottom;
bodyAllocation.Width = allocation.Width - bodyAllocation.X - (int)borderWidth;
bodyAllocation.Height = allocation.Height - bodyAllocation.Y - (int)borderWidth;
if(page != null)
{
pageAllocation = bodyAllocation;
int pad = (int)pagePadding;
pageAllocation.Inflate (-pad, -pad);
page.Page.SizeAllocate (pageAllocation);
}
else
{
pageAllocation = Gdk.Rectangle.Zero;
}
}
public void ShowTopLevelKeyTips ()
{
queuedKeyTipLevel = KeyTipLevel.TopLevel;
if(appButton != null) appButton.ShowKeyTips ();
if(toolbar != null) toolbar.ShowKeyTips ();
int x, y;
GdkWindow.GetOrigin(out x, out y);
int tabLineY = (int)(y + Allocation.Y + headerHeight);
foreach(KeyTip kt in tabKeyTips)
{
kt.ShowAt (x + kt.Target.Allocation.X + (kt.Target.Allocation.Width >> 1), tabLineY, 0.5, 0.0);
}
}
public void ShowTabLevelKeyTips ()
{
queuedKeyTipLevel = KeyTipLevel.Tab;
}
public void HideKeyTips ()
{
queuedKeyTipLevel = KeyTipLevel.None;
if(appButton != null) appButton.HideKeyTips ();
if(toolbar != null) toolbar.HideKeyTips ();
foreach(KeyTip kt in tabKeyTips)
{
kt.Hide ();
}
}
protected override bool OnExposeEvent (Gdk.EventExpose evnt)
{
Context cr = Gdk.CairoHelper.Create (this.GdkWindow);
cr.Rectangle (evnt.Area.X, evnt.Area.Y, evnt.Area.Width, evnt.Area.Height);
cr.Clip ();
Draw (cr);
((IDisposable)cr.Target).Dispose ();
((IDisposable)cr).Dispose ();
return base.OnExposeEvent (evnt);
}
protected void Draw (Context cr)
{
bool showToolbar = toolbar != null && toolbar.Visible;
Gdk.Rectangle menuBarAllocation;
menuBarAllocation.X = Allocation.X;
menuBarAllocation.Y = Allocation.Y;
menuBarAllocation.Width = Allocation.Width;
if(showToolbar)
menuBarAllocation.Height = (int)(toolbar.Allocation.Height + 2 * space);
else
menuBarAllocation.Height = 0;
theme.DrawRibbon (cr, menuBarAllocation, bodyAllocation, roundSize, lineWidth, this);
}
protected virtual void OnPageSelected (PageEventArgs args)
{
if(PageSelected != null) PageSelected (this, args);
}
protected virtual void OnPageAdded (PageEventArgs args)
{
if(PageAdded != null) PageAdded (this, args);
}
protected virtual void OnPageMoved (PageEventArgs args)
{
if(PageMoved != null) PageMoved (this, args);
}
protected virtual void OnPageRemoved (PageEventArgs args)
{
if(PageRemoved != null) PageRemoved (this, args);
}
/// <summary>Ribbon page.</summary>
public class RibbonPage
{
private Ribbon parent;
private Widget label, page;
private Requisition labelReq;
private Gdk.Rectangle labelAlloc;
private List<KeyTip> tabKeyTips;
/// <summary>Label widget of the page.</summary>
public Widget Label
{
set
{
if(label != null) label.Unparent ();
label = value;
if(label != null) label.Parent = parent;
}
get { return label; }
}
/// <summary>Widget used as the content of the page.</summary>
public Widget Page
{
set { page = value; }
get { return page; }
}
internal Requisition LabelRequisition
{
set { labelReq = value; }
get { return labelReq; }
}
public Gdk.Rectangle LabelAllocation
{
get { return labelAlloc; }
}
public RibbonPage (Ribbon Parent, Widget Page, Widget Label)
{
this.parent = Parent;
this.Label = Label;
this.Page = Page;
this.tabKeyTips = new List<KeyTip> ();
}
public void SetLabelAllocation (Gdk.Rectangle r)
{
labelAlloc = r;
}
}
}
}