[10042] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.MainForm.WindowsForms {
|
---|
| 30 | public partial class BreadcrumbControl : UserControl {
|
---|
| 31 | private LinkedList<IContent> breadcrumbs;
|
---|
| 32 | public IEnumerable<IContent> Breadcrumbs {
|
---|
| 33 | get { return breadcrumbs; }
|
---|
| 34 | set {
|
---|
| 35 | breadcrumbs = new LinkedList<IContent>(value);
|
---|
| 36 | BuildBreadcrumbTrail();
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | public ViewHost ViewHost { get; set; }
|
---|
| 41 |
|
---|
| 42 | public BreadcrumbControl() {
|
---|
| 43 | InitializeComponent();
|
---|
| 44 | breadcrumbs = new LinkedList<IContent>();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | private void BuildBreadcrumbTrail() {
|
---|
| 48 | var curLoc = new Point(3, 0);
|
---|
| 49 | int idx = 0;
|
---|
| 50 | var labels = new List<Label>();
|
---|
| 51 | foreach (var bc in breadcrumbs) {
|
---|
| 52 | Label label;
|
---|
| 53 | if (bc == breadcrumbs.Last.Value) {
|
---|
| 54 | label = new Label {
|
---|
| 55 | Location = curLoc,
|
---|
| 56 | AutoSize = true,
|
---|
| 57 | Font = new Font("Microsoft Sans Serif", 7f),
|
---|
| 58 | Text = bc.ToString()
|
---|
| 59 | };
|
---|
| 60 | } else {
|
---|
| 61 | label = new LinkLabel {
|
---|
| 62 | Location = curLoc,
|
---|
| 63 | AutoSize = true,
|
---|
| 64 | Font = new Font("Microsoft Sans Serif", 7f),
|
---|
| 65 | Text = bc.ToString(),
|
---|
| 66 | Tag = bc
|
---|
| 67 | };
|
---|
[10093] | 68 | ((LinkLabel)label).LinkClicked += label_LinkClicked;
|
---|
[10042] | 69 | }
|
---|
| 70 | labels.Add(label);
|
---|
| 71 | label.Size = label.GetPreferredSize(Size.Empty);
|
---|
| 72 | curLoc.X += label.Size.Width;
|
---|
| 73 | if (++idx < breadcrumbs.Count) {
|
---|
| 74 | var separator = new Label {
|
---|
| 75 | Location = curLoc,
|
---|
| 76 | AutoSize = true,
|
---|
| 77 | Font = new Font("Microsoft Sans Serif", 7f),
|
---|
| 78 | Text = ">>"
|
---|
| 79 | };
|
---|
| 80 | labels.Add(separator);
|
---|
| 81 | separator.Size = separator.GetPreferredSize(Size.Empty);
|
---|
| 82 | curLoc.X += separator.Size.Width;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | Controls.Clear();
|
---|
| 86 | if (curLoc.X <= Width)
|
---|
| 87 | Controls.AddRange(labels.ToArray());
|
---|
| 88 | else
|
---|
| 89 | AddLabelsRightToLeft(labels);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | private void AddLabelsRightToLeft(List<Label> labels) {
|
---|
| 93 | if (!labels.Any()) return;
|
---|
| 94 | var curLoc = new Point(3, 0);
|
---|
[10093] | 95 | var ellipsis = new LinkLabel {
|
---|
[10042] | 96 | Location = curLoc,
|
---|
| 97 | AutoSize = true,
|
---|
| 98 | Font = new Font("Microsoft Sans Serif", 7f),
|
---|
| 99 | Text = "..."
|
---|
| 100 | };
|
---|
| 101 | ellipsis.Location = curLoc;
|
---|
| 102 | Controls.Add(ellipsis);
|
---|
| 103 | curLoc.X += ellipsis.Size.Width;
|
---|
| 104 | int x = Width;
|
---|
| 105 | int i = labels.Count - 1;
|
---|
| 106 | while (i >= 0) {
|
---|
| 107 | x -= labels[i].Size.Width;
|
---|
| 108 | if (x < curLoc.X) break;
|
---|
| 109 | x -= labels[--i].Size.Width;
|
---|
| 110 | if (x < curLoc.X) break;
|
---|
| 111 | i--;
|
---|
| 112 | }
|
---|
| 113 | if (i == labels.Count - 1) return;
|
---|
| 114 | i += 1 + i % 2;
|
---|
[10093] | 115 | ellipsis.Tag = breadcrumbs.ElementAt(i / 2);
|
---|
| 116 | ellipsis.LinkClicked += label_LinkClicked;
|
---|
[10042] | 117 | while (i < labels.Count) {
|
---|
| 118 | var l = labels[i++];
|
---|
| 119 | l.Location = curLoc;
|
---|
| 120 | Controls.Add(l);
|
---|
| 121 | curLoc.X += l.Size.Width;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[10093] | 125 | private void label_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
|
---|
[10042] | 126 | var label = (LinkLabel)sender;
|
---|
| 127 | var content = (IContent)label.Tag;
|
---|
[10093] | 128 | while (breadcrumbs.Any() && breadcrumbs.Last.Value != content)
|
---|
[10042] | 129 | breadcrumbs.RemoveLast();
|
---|
[10093] | 130 | var mainForm = MainFormManager.GetMainForm<MainForm>();
|
---|
[10103] | 131 | var outermostViewHost = mainForm.GetOutermostControlOfType<ViewHost>(ViewHost);
|
---|
[10093] | 132 | bool showBreadcrumbs = outermostViewHost.ShowBreadcrumbs;
|
---|
[10107] | 133 | var oldCrumbs = outermostViewHost.Breadcrumbs;
|
---|
[10093] | 134 | outermostViewHost.Content = null;
|
---|
[10042] | 135 | var viewType = MainFormManager.GetDefaultViewType(content.GetType());
|
---|
[10093] | 136 | outermostViewHost.ViewType = viewType;
|
---|
| 137 | outermostViewHost.Content = content;
|
---|
[10107] | 138 | outermostViewHost.UpdateBreadcrumbTrail(oldCrumbs, breadcrumbs);
|
---|
[10093] | 139 | outermostViewHost.ShowBreadcrumbs = showBreadcrumbs;
|
---|
[10042] | 140 | }
|
---|
| 141 |
|
---|
| 142 | private void BreadcrumbControl_Resize(object sender, EventArgs e) {
|
---|
| 143 | BuildBreadcrumbTrail();
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | } |
---|