Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/WinForms/CS/BarTutorials/Gallery/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/WinForms/CS/BarTutorials/Gallery/Gallery.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraBars.Ribbon;
using DevExpress.Utils;

namespace DevExpress.XtraBars.Demos.Tutorials {
	/// <summary>
	/// Summary description for Gallery.
	/// </summary>
	public partial class Gallery : TutorialControl {
		public Gallery() {
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
			// TODO: Add any initialization after the InitForm call

		}
        public override bool SetNewWhatsThisPadding { get { return true; } }
        bool initValues = false;
        GalleryItem fCurrentFont;
		private void Gallery_Load(object sender, System.EventArgs e) {
			InitFont(gddFont.Gallery.Groups[0], rgbiFont.Gallery.Groups[0]);
			for(int i = 8; i <= 50; i+=2)
				repositoryItemComboBox1.Items.Add(i);
			InitFontValues();
		}

		void InitFontValues() {
			initValues = true;
			foreach(GalleryItem item in rgbiFont.Gallery.Groups[0].Items)
				if(item.Caption == labelControl1.Font.Name) CurrentFont = item;
			beiFontSize.EditValue = (int)labelControl1.Font.Size;
			biBold.Down = labelControl1.Font.Bold;
			biItalic.Down = labelControl1.Font.Italic;
			biUnderline.Down = labelControl1.Font.Underline;
			initValues = false;
		}

		private FontStyle CurrentFontStyle {
			get {
				FontStyle fs = new FontStyle();
				if(biBold.Down) fs |= FontStyle.Bold;
				if(biItalic.Down) fs |= FontStyle.Italic;
				if(biUnderline.Down) fs |= FontStyle.Underline;
				return fs;
			}
		}

        //<ribbonControl1>
		void InitFont(GalleryItemGroup groupDropDown, GalleryItemGroup galleryGroup) {
			for(int i = 0; i < FontFamily.Families.Length; i++) {
				string fontName = FontFamily.Families[i].Name;
				GalleryItem item = new GalleryItem();
				item.Caption = fontName;
				item.Image = GetFontImage(32, 28, fontName, 12);
				item.HoverImage = item.Image;
				item.Description = fontName;
				item.Hint = fontName;
				try {
					item.Tag = new Font(fontName, 9);
					if(DevExpress.Utils.ControlUtils.IsSymbolFont((Font)item.Tag)) {
						item.Tag = new Font(DevExpress.Utils.AppearanceObject.DefaultFont.FontFamily, 9);
						item.Description += " (Symbol Font)";
					}
				}
				catch {
					continue;
				}
				groupDropDown.Items.Add(item);
				galleryGroup.Items.Add(item);
			}
		}
        //</ribbonControl1>
        
		Image GetFontImage(int width, int height, string fontName, int fontSize) {
			Rectangle rect = new Rectangle(0, 0, width, height);
			Image fontImage = new Bitmap(width, height);
			try {
				using(Font fontSample = new Font(fontName, fontSize)) {
					Graphics g = Graphics.FromImage(fontImage);
					g.FillRectangle(Brushes.White, rect);
					using(StringFormat fs = new StringFormat()) {
						fs.Alignment = StringAlignment.Center;
						fs.LineAlignment = StringAlignment.Center;
						g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
						g.DrawString("Aa", fontSample, Brushes.Black, rect, fs);
						g.Dispose();
					}
				}
			}
			catch { }
			return fontImage;
		}

		private void gddFont_Gallery_CustomDrawItemText(object sender, DevExpress.XtraBars.Ribbon.GalleryItemCustomDrawEventArgs e) {
			DevExpress.XtraBars.Ribbon.ViewInfo.GalleryItemViewInfo itemInfo = e.ItemInfo as DevExpress.XtraBars.Ribbon.ViewInfo.GalleryItemViewInfo;
			itemInfo.PaintAppearance.ItemDescriptionAppearance.Normal.DrawString(e.Cache, e.Item.Description, itemInfo.DescriptionBounds);
			AppearanceObject app = itemInfo.PaintAppearance.ItemCaptionAppearance.Normal.Clone() as AppearanceObject;
			app.Font = (Font)e.Item.Tag;
            try {
                e.Cache.Graphics.DrawString(e.Item.Caption, app.Font, app.GetForeBrush(e.Cache), itemInfo.CaptionBounds);
            }
            catch { }
			e.Handled = true;
		}

		private void rgbiFont_Gallery_ItemClick(object sender, DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs e) {
			SetFont(e.Item);
		}

		private void gddFont_Gallery_ItemClick(object sender, DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs e) {
			SetFont(e.Item);
		}

        
		GalleryItem CurrentFont {
			get { return fCurrentFont; }
			set {
				if(CurrentFont == value) return;
				if(CurrentFont != null) CurrentFont.Checked = false;
				fCurrentFont = value;
				if(CurrentFont != null) {
					CurrentFont.Checked = true;
					SetMakeVisible();
				}
			}
		}	
		void SetFont(GalleryItem item) {
			if(initValues) return;
			if(item != null)
				CurrentFont = item;
			if(CurrentFont == null) return;
			labelControl1.Font = new Font(CurrentFont.Caption, Convert.ToInt32(beiFontSize.EditValue), CurrentFontStyle);
		}

		private void beiFontSize_EditValueChanged(object sender, System.EventArgs e) {
			SetFont(null);
		}

		private void bi_DownChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
			SetFont(null);
		}

		private void rpgFont_CaptionButtonClick(object sender, DevExpress.XtraBars.Ribbon.RibbonPageGroupEventArgs e) {
			DevExpress.Tutorials.Controls.XtraFontDialog dlg = new DevExpress.Tutorials.Controls.XtraFontDialog(labelControl1.Font);
			if(dlg.ShowDialog() == DialogResult.OK) { 
				labelControl1.Font = dlg.ResultFont;
				InitFontValues();
			}
		}

		private void gddFont_Popup(object sender, System.EventArgs e) {
			SetMakeVisible();
		}

		void SetMakeVisible() {
			if(CurrentFont == null) return; 
			gddFont.Gallery.MakeVisible(CurrentFont);
			rgbiFont.Gallery.MakeVisible(CurrentFont);
		}
	}
}