Tag.cs

00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using System.Drawing;
00005 using System.Windows.Forms;
00006 
00007 namespace TagControls
00008 {
00019     class TagControle
00020     {
00025         List<Label> labels = new List<Label>();
00026 
00028         Pen myPen = new Pen(Color.Black);
00030         TextureBrush myBrush = new TextureBrush(global::TagControls.Properties.Resources.Tagbar);
00032         SolidBrush myBlackBrush = new SolidBrush(Color.Black);
00034         SolidBrush myWhiteBrush = new SolidBrush(Color.White);
00036         SolidBrush Background = new SolidBrush(System.Drawing.SystemColors.Control);
00038         Graphics gfx;
00039         
00041         private Control TegneFlade;
00043         private TagContainer TheTagContainer;
00044 
00046         private int conSize;
00047 
00051         public TagControle(Control f, TagContainer tagcontainer)
00052         {
00053             gfx = f.CreateGraphics();
00054             TegneFlade = f;
00055             TheTagContainer = tagcontainer;
00056             conSize = f.Width - 54;
00057         }
00058 
00067         public void draw(int X, int Y, int procent, string keyWord, Boolean plusminus)
00068         {
00069             int nyY, Procent;
00070             nyY = Y + 14;
00071 
00072             int size = this.conSize + 1;
00073 
00074             //calculate the length of the status bar from % to C# unites
00075             Procent = Convert.ToInt32((Convert.ToDouble(procent) / 100.0 ) * Convert.ToDouble(conSize));
00076 
00077             //Draws the background, this is done becaus we need to remove parst of old tags
00078             gfx.FillRectangle(Background, new Rectangle(X, Y, conSize + 5, 100));
00079 
00080             //This draws the black box around the status bar    
00081             gfx.DrawLine(myPen, X, nyY, (X + size), nyY);                 //top
00082             gfx.DrawLine(myPen, X, (nyY + 4), (X + size), (nyY + 4));     //bund
00083             gfx.DrawLine(myPen, X, nyY, X, (nyY + 4));                    //V. side
00084             gfx.DrawLine(myPen, (X + size), nyY, (X + size), (nyY + 4));  //H. side
00085 
00086             //This draws the status bar
00087             gfx.FillRectangle(myWhiteBrush, new Rectangle((X + 1), (nyY + 1), conSize, 3));
00088             gfx.FillRectangle(myBrush, new Rectangle((X + 1), (nyY + 1), Procent, 3));
00089 
00090             //This draws the text of tah tag
00091             gfx.DrawString(keyWord,
00092                            new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))),
00093                            myBlackBrush,
00094                            new Point(X, Y));
00095 
00096             //Adds the +/- lable
00097             Label m = new System.Windows.Forms.Label();
00098             m.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
00099             m.Location = new System.Drawing.Point(X + (this.conSize + 4), Y + 3);
00100             m.Tag = keyWord;
00101             m.Name = "m";
00102             m.Size = new System.Drawing.Size(16, 16);
00103             if (plusminus)
00104                 m.Text = "-";
00105             else
00106                 m.Text = "+";
00107             m.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
00108             m.Click += new EventHandler(m_Click);
00109 
00110             //Adds the +/- label the the drawing canvas
00111             TegneFlade.Controls.Add(m);
00112             
00113             //adds the label the a list of labels used, so we can remove is again
00114             labels.Add(m);
00115         }
00116 
00122         void m_Click(object sender, EventArgs e)
00123         {
00124             //finds the sender of the event 
00125             Label l = (Label)sender;
00126 
00127             if (l.Text == "-")
00128             {
00129                 //A use can only remove hes own tags,
00130                 //so we run through all the tags to find a tag with the text and username of the current user
00131                 TheTagContainer.RemovedTag = new Shared.TagInfo(l.Tag.ToString(), TheTagContainer.ID, TheTagContainer.Username);
00132 
00133                 foreach (Shared.TagInfo Tag in TheTagContainer.Tags)
00134                 {
00135                     if (Tag.UserName == TheTagContainer.Username && Tag.Name == l.Tag.ToString())
00136                     {
00137                         TheTagContainer.Tags.Remove(Tag);
00138                         break;
00139                     }
00140                 }
00141                 //We cant triger an event of the tagcontainer so we have made a function in the tagcontauner we can run.
00142                 TheTagContainer.FireRemoveTag();
00143             }
00144             else
00145             {
00146                 //if one of the other tags are pressed we add a new with the same text but with a new user.
00147                 TheTagContainer.AddedTag = new Shared.TagInfo(l.Tag.ToString(), TheTagContainer.ID, TheTagContainer.Username);
00148                 TheTagContainer.Tags.Add(TheTagContainer.AddedTag);
00149                 TheTagContainer.FireAddTag();
00150             }
00151         }
00152     
00158         public void TagCleaner(int X, int Y)
00159         {
00160             for (int i = 0; i < labels.Count; i++)
00161                 labels[i].Dispose();
00162             
00163             //empty the label list
00164             labels.Clear();
00165 
00166             //draw over any old tags
00167             gfx.FillRectangle(Background, new Rectangle(X, Y, conSize + 5, labels.Count * 20));
00168         }
00169     }
00170 }

Generated on Thu Dec 21 06:21:57 2006 for SCRAML by  doxygen 1.5.1-p1