tangxu
2024-05-06 f01d639477141399fd3e092f3f0acb3ced7e02bf
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using DevExpress.XtraEditors;
using DevExpress.XtraBars.Ribbon;
using System.Collections;
using System.Windows.Forms;
using System.IO;
 
namespace Hydro.WinfrmUI.Viewer {
    public class MRUArrayList : ArrayList {
        public static string MRUFileName = "RibbonMRUFiles.ini";
        public static string MRUFolderName = "RibbonMRUFolders.ini";
 
        Control container;
        int maxRecentFiles = 9;
        Image imgChecked, imgUncheked, glyph;
        public event EventHandler LabelClicked;
        bool indexedList;
        bool showDescription;
        public MRUArrayList(Control cont, Image iChecked, Image iUnchecked, Image glyph, bool indexedList, bool showDescription) : this(cont, iChecked, iUnchecked, glyph) {
            this.indexedList = indexedList;
            this.showDescription = showDescription;
        }
        public MRUArrayList(Control cont, Image iChecked, Image iUnchecked, Image glyph) : this(cont, iChecked, iUnchecked) {
            this.glyph = glyph;
        }
        public MRUArrayList(Control cont, Image iChecked, Image iUnchecked)
            : base() {
            this.indexedList = true;
            this.imgChecked = iChecked;
            this.imgUncheked = iUnchecked;
            this.container = cont;
        }
        int GetLastAppMenuFileLabelIndex() {
            for(int i = container.Controls.Count - 1; i >= 0; i--) {
                if(container.Controls[i] is AppMenuFileLabel)
                    return i + 1;
            }
            return 0;
        }
        public void InsertElement(object value) {
            string[] names = value.ToString().Split(',');
            string _name = names[0];
            bool checkedLabel = false;
            if(names.Length > 1) checkedLabel = names[1].ToLower().Equals("true");
            foreach(Control c in container.Controls) {
                AppMenuFileLabel ml = c as AppMenuFileLabel;
                if(ml == null)
                    continue;
                if(ml.Tag.Equals(_name)) {
                    checkedLabel = ml.Checked;
                    base.Remove(_name);
                    ml.LabelClick -= new EventHandler(OnLabelClick);
                    ml.Dispose();
                    break;
                }
            }
            bool access = true;
            if(base.Count >= maxRecentFiles)
                access = RemoveLastElement();
            if(access) {
                base.Insert(0, _name);
                AppMenuFileLabel ml = new AppMenuFileLabel();
                int index = GetLastAppMenuFileLabelIndex();
                container.Controls.Add(ml);
                container.Controls.SetChildIndex(ml, index);
                ml.Tag = _name;
                if(showDescription) {
                    ml.Text = GetFileName(_name);
                    ml.Description = _name;
                }
                else 
                    ml.Text = GetFileName(_name);
                ml.Glyph = glyph;
                ml.Checked = checkedLabel;
                ml.AutoHeight = true;
                ml.Dock = DockStyle.Top;
                ml.Image = imgUncheked;
                ml.SelectedImage = imgChecked;
                ml.LabelClick += new EventHandler(OnLabelClick);
                if(indexedList)
                    SetElementsRange();
            }
        }
        void OnLabelClick(object sender, EventArgs e) {
            if(LabelClicked != null)
                LabelClicked(((AppMenuFileLabel)sender).Tag.ToString(), e);
        }
        public bool RemoveLastElement() {
            for(int i = 0; i < container.Controls.Count; i++) {
                AppMenuFileLabel ml = container.Controls[i] as AppMenuFileLabel;
                if(ml != null && !ml.Checked) {
                    base.Remove(ml.Tag);
                    ml.LabelClick -= new EventHandler(OnLabelClick);
                    ml.Dispose();
                    return true;
                }
            }
            return false;
        }
        string GetFileName(object obj) {
            FileInfo fi = new FileInfo(obj.ToString());
            return fi.Name;
        }
        void SetElementsRange() {
            int i = 0;
            foreach(Control  c in container.Controls) {
                AppMenuFileLabel ml = c as AppMenuFileLabel;
                if(ml == null)
                    continue;
                ml.Caption = string.Format("&{0}", container.Controls.Count - i);
                i++;
            }
        }
        public bool GetLabelChecked(string name) {
            foreach(Control c in container.Controls) {
                AppMenuFileLabel ml = c as AppMenuFileLabel;
                if(ml == null)
                    continue;
                if(ml.Tag.Equals(name)) return ml.Checked;
            }
            return false;
        }
        public void Init(string fileName, string defaultItem) {
            if(!System.IO.File.Exists(fileName)) {
                InsertElement(defaultItem);
                return;
            }
            System.IO.StreamReader sr = System.IO.File.OpenText(fileName);
            container.SuspendLayout();
            List<string> list = new List<string>();
            for(string s = sr.ReadLine(); s != null; s = sr.ReadLine())
                list.Add(s);
            for(int i = list.Count - 1; i >= 0; i--) {
                InsertElement(list[i]);
            }
            sr.Close();
            container.ResumeLayout();
        }
    }
}