OutlookBar.cs
Upload User: nnpulika
Upload Date: 2013-02-15
Package Size: 597k
Code Size: 71k
Category:

StatusBar

Development Platform:

C#

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Data;
  7. using System.Windows.Forms;
  8. using System.Diagnostics;
  9. using System.Threading;
  10. using System.Reflection;
  11. using System.IO;
  12. using System.Drawing.Design;
  13. using UtilityLibrary.Collections;
  14. using UtilityLibrary.Win32;
  15. using UtilityLibrary.General;
  16. using UtilityLibrary.Menus;
  17. namespace UtilityLibrary.WinControls
  18. {
  19. #region Delegates
  20. public delegate void OutlookBarPropertyChangedHandler(OutlookBarBand band, OutlookBarProperty property);
  21. public delegate void OutlookBarItemClickedHandler(OutlookBarBand band, OutlookBarItem item);
  22. public delegate void OutlookBarItemDroppedHandler(OutlookBarBand band, OutlookBarItem item);
  23. public delegate void OutlookBarIconViewChangedHandler(OutlookBarBand band);
  24. #endregion
  25. #region Enumerations
  26. public enum OutlookBarProperty
  27. {
  28. CurrentBandChanged,
  29. ShortcutNameChanged,
  30. GroupNameChanged
  31. }
  32. enum ItemSizeType
  33. {
  34. Icon,
  35. Label,
  36. All
  37. }
  38. public enum IconView
  39. {
  40. Small,
  41. Large
  42. }
  43. public enum HitTestType
  44. {
  45. UpScroll,
  46. DownScroll,
  47. Header,
  48. Item,
  49. DropLine,
  50. DropLineLastItem,
  51. Nothing
  52. }
  53. public enum ContextMenuType
  54. {
  55. Item,
  56. Header,
  57. Bar
  58. }
  59. public enum BorderType
  60. {
  61. None,
  62. FixedSingle,
  63. Fixed3D,
  64. Custom
  65. }
  66. #endregion
  67. #region OutlookBarBand class
  68. [ToolboxItem(false)]
  69. [Designer(typeof(UtilityLibrary.Designers.OutlookBarBandDesigner))]
  70. [TypeConverter(typeof(UtilityLibrary.Designers.OutlookBarBandConverter))]
  71. public class OutlookBarBand : Control
  72. {
  73. #region Class variables
  74. Control childControl = null;
  75.         ImageList smallImageList = null;
  76. ImageList largeImageList = null;
  77. OutlookBarItemCollection items = null;
  78. Color background = ColorUtil.VSNetControlColor;
  79. Color textColor = SystemColors.ControlText;
  80. IconView iconView = IconView.Large;
  81. #endregion
  82. #region Events
  83. public event OutlookBarIconViewChangedHandler IconViewChanged;
  84. #endregion
  85.         
  86. #region Constructors
  87. public OutlookBarBand(string text, Control childControl)
  88. {
  89. this.Text = text;
  90. this.childControl = childControl;
  91. }
  92. public OutlookBarBand(string text)
  93. {
  94. this.Text = text;
  95. // When using the constructor that just use the name,
  96. // it means that this band will have child items
  97. items = new OutlookBarItemCollection();
  98. }
  99. public OutlookBarBand()
  100. {
  101. // Constructor for designer support
  102. items = new OutlookBarItemCollection();
  103. if( this.Site!= null)
  104. Name = Site.Name;
  105. else
  106. Name = null;
  107. }
  108. #endregion
  109. #region Overrides
  110. protected override void Dispose( bool disposing )
  111. {
  112. if( items!= null)
  113. items.Clear();
  114. base.Dispose( disposing);
  115. }
  116. #endregion
  117. private void InitializeComponent()
  118. {
  119. }
  120.         
  121. #region Properties
  122. public Control ChildControl
  123. {
  124. get { return childControl; }
  125. }
  126. public ImageList SmallImageList
  127. {
  128. set { smallImageList = value; }
  129. get { return smallImageList; }
  130. }
  131. public ImageList LargeImageList
  132. {
  133. set { largeImageList = value; }
  134. get { return largeImageList; }
  135. }
  136. public Color Background
  137. {
  138. set { background = value; }
  139. get { return background; }
  140. }
  141. public Color TextColor
  142. {
  143. set { textColor = value; }
  144. get { return textColor; }
  145. }
  146. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  147. public OutlookBarItemCollection Items
  148. {
  149. get { return items; }
  150. }
  151. public IconView IconView 
  152. {
  153. set { iconView = value; }
  154. get { return iconView; }
  155. }
  156. #endregion
  157. #region Methods
  158. #endregion
  159. #region Implementation
  160. void FireIconViewChanged()
  161. {
  162. if ( IconViewChanged != null )
  163. {
  164. IconViewChanged(this);
  165. }
  166. }
  167. #endregion
  168. }
  169. #endregion
  170. #region OutlookBar Item class
  171. [ToolboxItem(false)]
  172. [TypeConverter(typeof(UtilityLibrary.Designers.OutlookBarItemConverter))]
  173. public class OutlookBarItem : Component
  174. {
  175. #region Class variables
  176. string text;
  177.         int imageIndex = -1;
  178. object tag;
  179. #endregion
  180. #region Events
  181. #endregion
  182. #region Constructors
  183. public OutlookBarItem(string text, int ImageIndex)
  184. {
  185. this.text = text;
  186. this.imageIndex = ImageIndex;
  187. }
  188. public OutlookBarItem(string text, int imageIndex, object tag)
  189. {
  190. this.text = text;
  191. this.imageIndex = imageIndex;
  192. this.tag = tag;
  193. }
  194. public OutlookBarItem()
  195. {
  196. // To support designer
  197. imageIndex = -1;
  198. }
  199. #endregion
  200. #region Overrides
  201. #endregion
  202. #region Methods 
  203. #endregion
  204. #region Properties
  205. public int ImageIndex
  206. {
  207. set { imageIndex = value; }
  208. get { return imageIndex; }
  209. }
  210. public string Text
  211. {
  212. set { text = value; }
  213. get { return text; }
  214. }
  215. public object Tag
  216. {
  217. set { tag = value; }
  218. get { return tag; }
  219. }
  220. #endregion
  221. #region Implementation
  222. #endregion
  223. }
  224. #endregion
  225.     #region OutlookBar class
  226. [ToolboxBitmap(typeof(UtilityLibrary.WinControls.OutlookBar), 
  227.     "UtilityLibrary.WinControls.OutlookBar.bmp")]
  228. [Designer(typeof(UtilityLibrary.Designers.OutlookBarDesigner))]
  229. public class OutlookBar : System.Windows.Forms.Control
  230. {
  231. #region Class variables
  232. const int BAND_HEADER_HEIGHT = 22;
  233. int currentBandIndex = -1;
  234. BorderType borderType = BorderType.None;
  235. int firstItem = 0;
  236. int lastHighlightedHeader = -1;
  237. int lastHighlightedItem = -1;
  238. Rectangle upArrowRect;
  239. Rectangle downArrowRect;
  240. DrawState upArrowDrawState = DrawState.Normal;
  241. DrawState downArrowDrawState = DrawState.Normal;
  242. bool upArrowVisible = false;
  243. bool downArrowVisible = false;
  244. bool upArrowPressed = false;
  245. bool downArrowPressed = false;
  246. bool upTimerTicking = false;
  247. bool downTimerTicking = false;
  248. bool doScrollingLoop = false;
  249. bool buttonPushed = false;
  250. Bitmap backgroundBitmap = null;
  251. bool needBackgroundBitmapResize = true;
  252. bool flatArrowButtons = false;
  253. // Custom Border colors
  254. Color leftTopColor = Color.Empty;
  255. Color rightBottomColor = Color.Empty;
  256. // Context Menus
  257. ContextMenu contextMenu = null;
  258. Point lastClickedPoint = Point.Empty;
  259. int selectedHeader = -1;
  260. System.Windows.Forms.Timer highlightTimer = new System.Windows.Forms.Timer();
  261. bool previousPressed = false;
  262. int animationSpeed = 20;
  263. Cursor dragCursor = null;
  264. int lastDrawnLineIndex = -1;
  265.         bool paintedDropLineLastItem = false;
  266. int droppedPosition = -1;
  267. bool forceHightlight = false;
  268. int forceHightlightIndex = -1;
  269. // For in place editing of the 
  270. TextBoxEx textBox = new TextBoxEx();
  271. bool editingAnItem = false;
  272.         
  273. const int X_SMALLICON_LABEL_OFFSET = 2;
  274. const int Y_LARGEICON_LABEL_OFFSET = 3;
  275. const int Y_SMALLICON_SPACING = 10;
  276. const int Y_LARGEICON_SPACING = 8;
  277. const int LEFT_MARGIN = 5;
  278. const int LARGE_TOP_MARGIN = 10;
  279. const int SMALL_TOP_MARGIN = 6;
  280. const int ARROW_BUTTON_MARGIN = 5;
  281. // Flat Arrow Buttons helpers
  282. DrawState upFlatArrowState = DrawState.Normal;
  283. DrawState downFlatArrowState = DrawState.Normal;
  284. // Bands
  285. OutlookBarBandCollection bands = null;
  286. #endregion
  287. #region Events
  288. public event OutlookBarPropertyChangedHandler PropertyChanged;
  289. public event OutlookBarItemClickedHandler ItemClicked;
  290. public event OutlookBarItemDroppedHandler ItemDropped;
  291. #endregion
  292. #region Constructors
  293. public OutlookBar()
  294. {
  295.             // Construct band collection
  296. bands = new OutlookBarBandCollection(this);
  297. Dock = DockStyle.Left;
  298. // We are going to do all of the painting so better setup the control
  299. // to use double buffering
  300. SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.ResizeRedraw
  301. |ControlStyles.Opaque|ControlStyles.UserPaint|ControlStyles.DoubleBuffer, true);
  302. Font = SystemInformation.MenuFont;
  303. CreateContextMenu();
  304. // Setup timer
  305. highlightTimer.Tick += new EventHandler(OnHighlightHeader);
  306. highlightTimer.Interval = 100;
  307. // Load drag cursor
  308. Assembly myAssembly = Assembly.GetAssembly(Type.GetType("UtilityLibrary.WinControls.OutlookBar"));
  309.             Stream cursorStream = myAssembly.GetManifestResourceStream("UtilityLibrary.Resources.DragCursor.cur");
  310. dragCursor = new Cursor(cursorStream);
  311.                
  312. // don't display it until we need to
  313. textBox.Visible = false;
  314. // Hook up notification for the Enter and LostFocus events
  315. textBox.KeyUp += new KeyEventHandler(TextBoxKeyDown);
  316. textBox.LostFocus += new EventHandler(TextBoxLostFocus);
  317. }
  318. #endregion
  319. #region Overrides
  320. protected override Size DefaultSize
  321. {
  322. get { return new Size(100, 10); }
  323. }
  324. protected override void OnPaint(PaintEventArgs pe)
  325. {
  326. base.OnPaint(pe);
  327. Graphics g = pe.Graphics;
  328. // Manipulate child window in case there is one
  329. if ( currentBandIndex == -1 && bands.Count > 0)
  330. SetCurrentBand(0);
  331. // Background if it needs to be painted
  332. DrawBackground(g);
  333. // If we have a border
  334. DrawBorder(g);
  335. // The little headers
  336. DrawHeaders(g);
  337. // If there is a child window
  338. // it will paint itself, otherwise we do the painting
  339. if ( !HasChild() )
  340. {
  341. // The items themselves
  342. DrawItems(g, Rectangle.Empty, null);
  343. // The buttons for scrolling items
  344. // Drawing second so that they don't get written over by the items
  345. DrawArrowButtons(g);
  346. }
  347. // Highlight last item clicked
  348. if ( forceHightlight )
  349. {
  350.                 ForceHighlightItem(g, forceHightlightIndex);
  351. forceHightlight = false;
  352. }
  353. }
  354. protected override void OnMouseDown(MouseEventArgs e)
  355. {
  356. base.OnMouseDown(e);
  357. // If the textbox is being displayed cancel editing
  358. if ( textBox.Visible )
  359. {
  360. ProcessTextBoxEditing();
  361. return;
  362. }
  363. int index;
  364. HitTestType ht = HitTest(new Point(e.X, e.Y), out index, false);
  365. if ( e.Button == MouseButtons.Left)
  366. {
  367. // Handle arrow button hit
  368. if( ht == HitTestType.UpScroll || ht == HitTestType.DownScroll)
  369. {
  370. if ( ht == HitTestType.UpScroll )
  371. {
  372. ProcessArrowScrolling(upArrowRect,   
  373. ref upArrowVisible, ref upArrowPressed, ref upTimerTicking);
  374. return;
  375. }
  376. else
  377. {
  378. ProcessArrowScrolling(downArrowRect, 
  379. ref downArrowVisible, ref downArrowPressed, ref downTimerTicking);
  380. return;
  381. }
  382. }
  383.                 
  384. // Handle header hit
  385. if ( ht == HitTestType.Header )
  386. {
  387. ProcessHeaderHit(index);
  388. return;
  389. }
  390. // Handle item hit
  391. if ( ht == HitTestType.Item )
  392. {
  393. ProcessItemHit(index, new Point(e.X, e.Y));
  394. return;
  395. }
  396. }
  397. }
  398. protected override void OnMouseMove(MouseEventArgs e)
  399. {
  400. base.OnMouseMove(e);
  401.            
  402. Point MousePos = new Point(e.X, e.Y);
  403. // Change cursor if over a header
  404. int index;
  405. HitTestType ht = HitTest(MousePos, out index, false);
  406. if ( ht != HitTestType.Header && lastHighlightedHeader >=0 )HighlightHeader(-1);
  407. if ( ht != HitTestType.Item  && lastHighlightedItem  >= 0 ) HighlightItem(-1, false);
  408. if ( upArrowDrawState == DrawState.Hot && ht != HitTestType.UpScroll
  409. || flatArrowButtons )
  410. if ( flatArrowButtons )
  411. {
  412. if ( upArrowVisible && UpFlatArrowState != DrawState.Normal)
  413. DrawArrowButton(upArrowRect, ButtonState.Normal);
  414. }
  415. else
  416. {
  417. DrawArrowButton(upArrowRect, ButtonState.Normal);
  418. }
  419. if ( downArrowDrawState == DrawState.Hot && ht == HitTestType.DownScroll
  420. || flatArrowButtons )
  421. if ( flatArrowButtons )
  422. {
  423. if ( downArrowVisible && DownFlatArrowState != DrawState.Normal )
  424. DrawArrowButton(downArrowRect, ButtonState.Normal);
  425. }
  426. else
  427. {
  428. DrawArrowButton(downArrowRect, ButtonState.Normal);
  429. }
  430. if ( ht == HitTestType.Header )
  431. {
  432. Cursor.Current = Cursors.Hand;
  433. HighlightHeader(index);
  434. highlightTimer.Start();
  435. }
  436. else if (ht == HitTestType.Item )
  437. {
  438. HighlightItem(index, false);
  439. highlightTimer.Start();
  440. }
  441. else if ( ht == HitTestType.UpScroll )
  442. {
  443. if ( flatArrowButtons 
  444. && upArrowVisible )
  445. {
  446. if ( UpFlatArrowState != DrawState.Hot )
  447. DrawArrowButton(upArrowRect, ButtonState.Pushed);
  448. }
  449. }
  450. else if ( ht == HitTestType.DownScroll )
  451. {
  452. if ( flatArrowButtons
  453. && downArrowVisible)
  454. {
  455. if ( DownFlatArrowState != DrawState.Hot )
  456. DrawArrowButton(downArrowRect, ButtonState.Pushed);
  457. }
  458. }
  459. }
  460. protected override void OnSizeChanged(EventArgs e)
  461. {
  462. base.OnSizeChanged(e);
  463. if ( HasChild() )
  464. {
  465. Rectangle rc = GetViewPortRect();
  466. OutlookBarBand newBand = bands[currentBandIndex];
  467. Control childControl = newBand.ChildControl;
  468. childControl.Bounds = rc;
  469. }
  470. // flag that we need to update the bitmap background
  471. // if there is one
  472. needBackgroundBitmapResize = true;
  473. }
  474. protected override void OnHandleCreated(EventArgs e)
  475. {
  476. base.OnHandleCreated(e);
  477. // Make text box a child of the outlookbar control
  478. WindowsAPI.SetParent(textBox.Handle, Handle);
  479. }
  480. protected override  void WndProc(ref Message m)
  481. {
  482. bool callBase = true;
  483. switch(m.Msg)
  484. {
  485. case ((int)Msg.WM_CONTEXTMENU):
  486. {
  487. Point pt = WindowsAPI.GetPointFromLPARAM((int)m.LParam);
  488. pt = PointToClient(pt);
  489. Rectangle viewPortRect = GetViewPortRect();
  490. // Save the point so that we can update our popup menu correctly
  491. lastClickedPoint = pt;
  492.                     
  493. // If the user click on the child window don't
  494. // show a context menu
  495. if ( HasChild() && viewPortRect.Contains(pt)) 
  496. {
  497. callBase = false;
  498. break;
  499. }
  500. }
  501. break;
  502. default:
  503. break;
  504. }
  505. if ( callBase )
  506. base.WndProc(ref m);
  507. }
  508. #endregion
  509.         #region Properties
  510. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  511. [Editor(typeof(UtilityLibrary.Designers.OutlookBarBandEditor),typeof(UITypeEditor))]
  512. public OutlookBarBandCollection Bands
  513. {
  514. get { return bands; }
  515. }
  516. public BorderType BorderType 
  517. {
  518. set { borderType = value; }
  519. get { return borderType; }
  520. }
  521. public int AnimationSpeed  
  522. {
  523. set { animationSpeed = value; }
  524. get { return animationSpeed; }
  525. }
  526. public Color LeftTopColor  
  527. {
  528. set { leftTopColor = value; }
  529. get { return leftTopColor; }
  530. }
  531. public Color RightBottomColor  
  532. {
  533. set { rightBottomColor = value; }
  534. get { return rightBottomColor; }
  535. }
  536. public Bitmap BackgroundBitmap
  537. {
  538. set 
  539. backgroundBitmap = value; 
  540. needBackgroundBitmapResize = true;
  541. }
  542. get { return backgroundBitmap; }
  543. }
  544. public bool FlatArrowButtons
  545. {
  546. set { flatArrowButtons = value; }
  547. get { return flatArrowButtons; }
  548. }
  549. internal DrawState UpFlatArrowState
  550. {
  551. set { upFlatArrowState = value; }
  552. get { return upFlatArrowState; }
  553. }
  554. internal DrawState DownFlatArrowState
  555. {
  556. set { downFlatArrowState = value; }
  557. get { return downFlatArrowState; }
  558. }
  559. #endregion
  560. #region Methods
  561. public HitTestType HitTest(Point point, out int index, bool doingDragging)
  562. {
  563. // If we don't have any bands just return
  564. index = 0;
  565. if ( bands.Count == 0 || currentBandIndex == -1 ) return HitTestType.Nothing;
  566. // Check if we hit the arrow buttons
  567. if (upArrowVisible && upArrowRect.Contains(point)) return HitTestType.UpScroll;
  568. if (downArrowVisible && downArrowRect.Contains(point)) return HitTestType.DownScroll;
  569. // Check to see if we hit a header
  570. for ( int i = 0; i < bands.Count; i++ )
  571. {
  572. Rectangle rc = GetHeaderRect(i);
  573. if ( rc.Contains(point) )
  574. {
  575. index = i;
  576. return HitTestType.Header;
  577. }
  578. }
  579. // Don't do any hit testing for items if
  580. // the current band has a child
  581. if ( HasChild() ) return HitTestType.Nothing;
  582. // Check to see if we hit an item
  583. int itemCount = bands[currentBandIndex].Items.Count;
  584. Rectangle viewPortRect = GetViewPortRect();
  585. Rectangle iconRect = Rectangle.Empty;
  586. Rectangle labelRect = Rectangle.Empty;
  587. for ( int i = firstItem; i < itemCount; i++ )
  588. {
  589. // Check if we hit the icon
  590. iconRect = GetIconRect(i);
  591. if ( iconRect.Contains(point) )
  592. {
  593. index = i;
  594. return HitTestType.Item;
  595. }
  596. else
  597. if ( iconRect.Bottom > viewPortRect.Bottom && !doingDragging) break;
  598. // Check to see if we hit the label
  599. labelRect = GetLabelRect(i);
  600. if ( labelRect.Contains(point) )
  601. {
  602. index = i;
  603. return HitTestType.Item;
  604. }
  605. else
  606. if ( labelRect.Bottom > viewPortRect.Bottom && !doingDragging) break;
  607. // If we are dragging, hit test for the drop line
  608. if ( doingDragging )
  609. {
  610. Rectangle dragRect = new Rectangle(viewPortRect.Left, iconRect.Top - Y_LARGEICON_SPACING,
  611. viewPortRect.Width, Y_LARGEICON_SPACING);
  612. if ( dragRect.Contains(point) )
  613. {
  614. index = i;
  615. return HitTestType.DropLine;
  616. }
  617. // if this is the last icon and the point is farther down the last item
  618. if ( (i == itemCount - 1) && point.Y > labelRect.Bottom )
  619. {
  620. index = itemCount - 1;
  621. return HitTestType.DropLineLastItem;
  622. }
  623. }
  624. }
  625. return HitTestType.Nothing;
  626. }
  627. public Rectangle GetItemRect(Graphics g, OutlookBarBand band, int index, Rectangle targetRect)
  628. {
  629. Rectangle rc = GetViewPortRect();
  630. if ( targetRect != Rectangle.Empty ) rc = targetRect;
  631. Size itemSize = new Size(0,0);
  632. int top = rc.Top;
  633. int y = 0;
  634. for ( int i = 0; i < index; i++ )
  635. {
  636. itemSize = GetItemSize(g, band, i, ItemSizeType.All);
  637. top += itemSize.Height;
  638. if ( band.IconView == IconView.Small )
  639. top += Y_SMALLICON_SPACING;
  640. else
  641. top += Y_LARGEICON_SPACING;
  642. if ( i == (firstItem - 1) )
  643. {
  644. // Subtract the hidden items height
  645. y = top - rc.Top;
  646. }
  647. }
  648. itemSize = GetItemSize(g, band, index, ItemSizeType.All);
  649. int margin = SMALL_TOP_MARGIN;
  650. if ( band.IconView == IconView.Large )
  651. margin = LARGE_TOP_MARGIN;
  652. // Work with Windows Rect is easier to change settings
  653. RECT rcItem = new RECT();
  654. rcItem.left = rc.Left;
  655. rcItem.top = top;
  656. rcItem.right = rc.Left + itemSize.Width;
  657. rcItem.bottom = top + itemSize.Height;
  658. // Adjust rectangle
  659. rcItem.top -= y;
  660. rcItem.bottom -= y;
  661. rcItem.top += margin;
  662. rcItem.bottom += margin;
  663. if ( band.IconView == IconView.Small )
  664. {
  665. rcItem.left  = rc.Left + LEFT_MARGIN;
  666. rcItem.right = rc.Right;
  667. }
  668. // Construct final rectangle
  669. Rectangle actualRect = new Rectangle(rcItem.left,  
  670. rcItem.top, rcItem.right - rcItem.left, rcItem.bottom - rcItem.top);
  671.        
  672. return actualRect;
  673. }
  674. public void ProcessOnMouseDown(MouseEventArgs e)
  675. {
  676. // To be used from the designer
  677. OnMouseDown(e);
  678. }
  679. public void OnContextMenu(object sender, EventArgs e)
  680. {
  681. OutlookBarBand band = bands[currentBandIndex];
  682. if (typeof(MenuItemEx).IsInstanceOfType(sender)) 
  683. {
  684. MenuItemEx item = (MenuItemEx)sender;
  685. if ( item.Text == "Large Icons")
  686. {
  687. band.IconView = IconView.Large;
  688. item.RadioCheck = true;
  689. contextMenu.MenuItems[1].RadioCheck = false;
  690. }
  691. else if ( item.Text == "Small Icons")
  692. {
  693. item.RadioCheck = true;
  694. band.IconView = IconView.Small;
  695. contextMenu.MenuItems[0].RadioCheck = false;
  696. }
  697. else if ( item.Text == "Rename Shortcut" )
  698. {
  699. RenameItem();
  700. }
  701. else if ( item.Text == "Rename Group" )
  702. {
  703. RenameHeader();
  704. }
  705. }
  706. Invalidate();
  707. }
  708. public int GetCurrentBand()
  709. {
  710. return currentBandIndex;
  711. }
  712. public void SetCurrentBand(int index)
  713. {
  714. // If in design mode and index equals -1 just don't do anything
  715. if ( DesignMode && index == -1 )
  716. return;
  717. // Make sure index is valid
  718. Debug.Assert(index >= 0 && index < bands.Count, "Invalid Band Index");
  719. // Don't do anything if requested to set it to the same one
  720. if ( currentBandIndex == index)
  721. return;
  722. // Hide current child control if any
  723. Control childControl;
  724. if ( currentBandIndex != -1 && bands.Count > 0)
  725. {
  726. OutlookBarBand oldBand = bands[currentBandIndex];
  727. childControl = oldBand.ChildControl;
  728. if ( childControl != null )
  729. childControl.Visible = false;
  730. }
  731. // Animate showing the new band
  732. if ( index != currentBandIndex )
  733. {
  734. AnimateScroll(currentBandIndex, index);
  735. }
  736. // Reset parameter
  737. currentBandIndex = index;
  738. firstItem = 0;
  739. lastHighlightedItem = -1;
  740. OutlookBarBand newBand = bands[currentBandIndex];
  741. childControl = newBand.ChildControl;
  742. if ( childControl != null )
  743. {
  744. // Make the outlookbar the parent of the child control
  745. // so that when we change the bounds of the control they
  746. // would be relative to the parent control
  747. // Don't do this every time since it causes flickering
  748. IntPtr hParent = WindowsAPI.GetParent(childControl.Handle);
  749. if ( hParent != Handle )
  750. {
  751. WindowsAPI.SetParent(childControl.Handle, Handle);
  752. // Hook up into control recreation
  753. childControl.HandleCreated += new EventHandler(HandleRecreated);
  754. }
  755. Rectangle rc = GetViewPortRect();
  756. childControl.Bounds = rc;
  757. childControl.Visible = true;
  758. childControl.Invalidate();
  759. childControl.Focus();
  760. }
  761. // update the bar
  762. Invalidate();
  763. // Fire property changed
  764. FirePropertyChanged(OutlookBarProperty.CurrentBandChanged);
  765. }
  766. #endregion
  767. #region Implementation
  768. void ProcessArrowScrolling(Rectangle arrowButton, ref bool arrowVisible, ref bool arrowPressed, ref bool timerTicking)
  769. {
  770. // Capture the mouse
  771. Capture = true;
  772. // Draw the arrow button pushed
  773. timerTicking = true;
  774. // Draw pushed button
  775. buttonPushed = true;
  776. DrawArrowButton(arrowButton, ButtonState.Pushed);
  777. // Start timer
  778. System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
  779. timer.Tick += new EventHandler(ScrollingTick);
  780. timer.Interval = 300;
  781. timer.Start();
  782.              
  783. doScrollingLoop = true;           
  784. while ( doScrollingLoop ) 
  785. {
  786. // Check messages until we find a condition to break out of the loop
  787. Win32.MSG msg = new Win32.MSG();
  788. WindowsAPI.GetMessage(ref msg, 0, 0, 0);
  789. Point point = new Point(0,0);
  790. if ( msg.message == (int)Msg.WM_MOUSEMOVE || msg.message == (int)Msg.WM_LBUTTONUP )
  791. {
  792. point = WindowsAPI.GetPointFromLPARAM((int)msg.lParam);
  793. }
  794. switch(msg.message)
  795. {
  796. case (int)Msg.WM_MOUSEMOVE:
  797. {
  798. if ( arrowButton.Contains(point) )
  799. {
  800. if ( !buttonPushed )
  801. {
  802. DrawArrowButton(arrowButton, ButtonState.Pushed);
  803. arrowVisible =  true;
  804. arrowPressed = true;
  805. buttonPushed =  true;
  806. }
  807. }
  808. else
  809. {
  810. if ( buttonPushed )
  811. {
  812. DrawArrowButton(arrowButton, ButtonState.Normal);
  813. arrowVisible =  false;
  814. arrowPressed = false;
  815. buttonPushed =  false;
  816. }
  817. }
  818. break;
  819. }
  820. case (int)Msg.WM_LBUTTONUP:
  821. {
  822. if ( buttonPushed )
  823. {
  824. if ( !flatArrowButtons )
  825. {
  826. // Only if using regular buttons
  827. DrawArrowButton(arrowButton, ButtonState.Normal);
  828. }
  829. buttonPushed =  false;
  830. }
  831. arrowVisible = false;
  832. if ( arrowButton.Contains(point) ) 
  833. {
  834. if ( arrowButton.Equals(upArrowRect) )
  835. {
  836. if ( firstItem > 0 )
  837. {
  838. firstItem--;
  839. Rectangle rc = GetViewPortRect();
  840. Invalidate(rc);
  841. }
  842. }
  843. else if ( arrowButton.Equals(downArrowRect) )
  844. {
  845. using ( Graphics g = Graphics.FromHwnd(Handle) )
  846. {
  847. // Get the last item rectangle
  848. OutlookBarBand band = bands[currentBandIndex];
  849. if ( band != null )
  850. {
  851. Rectangle rcItem = GetItemRect(g, band, band.Items.Count - 1, Rectangle.Empty);
  852. Rectangle rc = GetViewPortRect();
  853. if ( rcItem.Bottom > rc.Bottom )
  854. {
  855. firstItem++;
  856. Invalidate(rc);
  857. }
  858. }
  859. }
  860. }
  861. }
  862. doScrollingLoop = false;
  863. break;
  864. }
  865. case (int)Msg.WM_KEYDOWN:
  866. {
  867. if ( (int)msg.wParam == (int)VirtualKeys.VK_ESCAPE) 
  868. doScrollingLoop = false;
  869. break;
  870. }
  871. default:
  872. WindowsAPI.DispatchMessage(ref msg);
  873. break;
  874. }
  875. }
  876. // Release the capture
  877. Capture = false;
  878. // Stop timer
  879. timer.Stop();
  880. timer.Dispose();
  881. // Reset flags
  882. arrowVisible = false;
  883. arrowPressed = false;
  884. timerTicking = false;
  885. Rectangle viewPortRect = GetViewPortRect();
  886. Invalidate(viewPortRect);
  887. }
  888. void ScrollingTick(Object timeObject, EventArgs eventArgs) 
  889. {
  890. // Get mouse coordinates
  891. Point point = Control.MousePosition;
  892. point = PointToClient(point);
  893. if ( upTimerTicking )
  894. {
  895. if ( buttonPushed )
  896. {
  897. if ( upArrowRect.Contains(point))
  898. {
  899. upArrowPressed = true;
  900. if ( firstItem > 0 )
  901. {
  902. firstItem--;
  903. Rectangle rc = GetViewPortRect();
  904. Invalidate(rc);
  905. }
  906. else
  907. doScrollingLoop = false;
  908. }
  909. else
  910. upArrowPressed = false;
  911. }
  912. }
  913. else if ( downTimerTicking )
  914. {
  915. if ( buttonPushed )
  916. {
  917. if ( downArrowRect.Contains(point))
  918. {
  919. downArrowPressed = true;
  920. using ( Graphics g = Graphics.FromHwnd(Handle) )
  921. {
  922. // Get the last item rectangle
  923. OutlookBarBand band = bands[currentBandIndex];
  924. Rectangle rcItem = GetItemRect(g, band, band.Items.Count - 1, Rectangle.Empty);
  925. Rectangle rc = GetViewPortRect();
  926. if ( rcItem.Bottom > rc.Bottom )
  927. {
  928. firstItem++;
  929. Invalidate(rc);
  930. }
  931. }
  932. }
  933. else
  934. doScrollingLoop = false;
  935. }
  936. else
  937. downArrowPressed = false;
  938. }
  939. }
  940. void ProcessHeaderHit(int index)
  941. {
  942. Capture = true;
  943. Rectangle rc = GetHeaderRect(index);
  944. // Draw header pressed
  945. Graphics g = Graphics.FromHwnd(Handle);
  946. bool headerPressed = true;
  947. DrawHeader(g, index, rc, Border3DStyle.Sunken);
  948.             
  949. bool doLoop = true;
  950. while (doLoop)
  951. {
  952. // Check messages until we find a condition to break out of the loop
  953. Win32.MSG msg = new Win32.MSG();
  954. WindowsAPI.GetMessage(ref msg, 0, 0, 0);
  955. Point point = new Point(0,0);
  956. if ( msg.message == (int)Msg.WM_MOUSEMOVE || msg.message == (int)Msg.WM_LBUTTONUP )
  957. {
  958. point = WindowsAPI.GetPointFromLPARAM((int)msg.lParam);
  959. }
  960. switch(msg.message)
  961. {
  962. case (int)Msg.WM_MOUSEMOVE:
  963. {
  964. int currentIndex;
  965. HitTestType hit = HitTest(point, out currentIndex, false);
  966. if (hit == HitTestType.Header && currentIndex == index)
  967. {
  968. if (!headerPressed)
  969. {
  970. DrawHeader(g, index, rc, Border3DStyle.Sunken);
  971. headerPressed = true;
  972. }
  973. }
  974. else
  975. {
  976. if (headerPressed)
  977. {
  978. DrawHeader(g, index, rc, Border3DStyle.RaisedInner);
  979. headerPressed = false;
  980. }
  981. }
  982. break;
  983. }
  984. case (int)Msg.WM_LBUTTONUP:
  985. {
  986. if (headerPressed)
  987. {
  988. DrawHeader(g, index, rc, Border3DStyle.RaisedInner);
  989. headerPressed = false;
  990. }
  991. int newIndex;
  992. HitTestType ht = HitTest(point, out newIndex, false);
  993. if ( ht == HitTestType.Header && newIndex != selectedHeader )
  994. SetCurrentBand(newIndex);
  995. doLoop = false;
  996. break;
  997. }
  998. case (int)Msg.WM_KEYDOWN:
  999. {
  1000. if ( (int)msg.wParam == (int)VirtualKeys.VK_ESCAPE) 
  1001. doLoop = false;
  1002. break;
  1003. }
  1004. default:
  1005. WindowsAPI.DispatchMessage(ref msg);
  1006. break;
  1007. }
  1008. }
  1009. // Reset flags
  1010. Capture = false;
  1011. g.Dispose();
  1012. }
  1013. void ProcessItemHit(int index, Point pt)
  1014. {
  1015. bool itemPressed = true;
  1016. Capture = true;
  1017. Graphics g = Graphics.FromHwnd(Handle);
  1018.             Rectangle itemRect = GetItemRect(g, bands[currentBandIndex], index, Rectangle.Empty);
  1019. DrawItem(g, index, itemRect, true, true, Rectangle.Empty, null);
  1020. bool dragging = false;
  1021. int deltaX = 0;
  1022. int deltaY = 0;
  1023. bool itemHighlighted = false;
  1024. int dragItemIndex = -1;
  1025.                          
  1026. bool doLoop = true;
  1027. while ( doLoop )
  1028. {
  1029. // Check messages until we find a condition to break out of the loop
  1030. Win32.MSG msg = new Win32.MSG();
  1031. WindowsAPI.GetMessage(ref msg, 0, 0, 0);
  1032. Point point = new Point(0,0);
  1033. if ( msg.message == (int)Msg.WM_MOUSEMOVE || msg.message == (int)Msg.WM_LBUTTONUP )
  1034. {
  1035. point = WindowsAPI.GetPointFromLPARAM((int)msg.lParam);
  1036. if ( msg.message == (int)Msg.WM_MOUSEMOVE )
  1037. {
  1038. deltaX = pt.X - point.X;
  1039. deltaY = pt.Y - point.Y;
  1040. }
  1041. }
  1042. switch(msg.message)
  1043. {
  1044. case (int)Msg.WM_MOUSEMOVE:
  1045. {
  1046. int currentIndex;
  1047.                         HitTestType hit = HitTest(point, out currentIndex, dragging);
  1048. if ( dragging ) 
  1049. {
  1050. if ( hit == HitTestType.DropLine || hit == HitTestType.DropLineLastItem  )
  1051. {
  1052. Cursor.Current = dragCursor;
  1053. // Draw the Dragline
  1054.                                 DrawDropLine(g, currentIndex, true, hit);
  1055. }
  1056. else
  1057. {
  1058. Cursor.Current = Cursors.No;
  1059. // Erase the Dragline
  1060. DrawDropLine(g, index, false, hit);
  1061. }
  1062. if ( hit == HitTestType.Item && currentIndex == index)
  1063. {
  1064. if ( itemHighlighted == false )
  1065. {
  1066. DrawItem(g, index, itemRect, 
  1067. true, false, Rectangle.Empty, null);
  1068. itemHighlighted = true;                                
  1069. }
  1070. }
  1071. else 
  1072. {
  1073. if ( hit == HitTestType.Item )
  1074. {
  1075. // Erase previous highlighting first
  1076. if ( itemHighlighted ) 
  1077. DrawItem(g, index, itemRect, 
  1078. false, false, Rectangle.Empty, null);
  1079. }
  1080. // Highlight new item
  1081. itemRect = GetItemRect(g, bands[currentBandIndex], currentIndex, Rectangle.Empty);
  1082. index = currentIndex;
  1083. DrawItem(g, index, itemRect, 
  1084. true, false, Rectangle.Empty, null);
  1085. }
  1086. else 
  1087. {
  1088. // the mouse did not hit an item
  1089. if ( itemHighlighted ) 
  1090. {
  1091. DrawItem(g, index, itemRect, 
  1092. false, false, Rectangle.Empty, null);
  1093. itemHighlighted = false;
  1094. }
  1095. }
  1096. }
  1097. }
  1098. else 
  1099. {
  1100. if (hit == HitTestType.Item && currentIndex == index)
  1101. {
  1102. // Set no drag cursor if there have been at least
  1103. // a 5 pixel movement
  1104. int pixelmov = 5;
  1105. if ( bands[currentBandIndex].IconView == IconView.Small ) pixelmov = 2;
  1106. bool unpressed = false;
  1107. if ( Math.Abs(deltaX) >= pixelmov || Math.Abs(deltaY) >= pixelmov )
  1108. {
  1109. unpressed = true;
  1110. Cursor.Current = Cursors.No;
  1111. dragging = true;
  1112. dragItemIndex = index;
  1113. }
  1114. if (itemPressed && unpressed)
  1115. {
  1116. DrawItem(g, index, itemRect, 
  1117. true, false, Rectangle.Empty, null);
  1118. itemPressed = false;
  1119. itemHighlighted = true;
  1120. }
  1121. }
  1122. }
  1123. break;
  1124. }
  1125. case (int)Msg.WM_LBUTTONUP:
  1126. {
  1127. // Highlight the item
  1128. if (itemPressed)
  1129. {
  1130. DrawItem(g, index, itemRect, true, false, Rectangle.Empty, null);
  1131. itemPressed = false;
  1132. }
  1133. int newIndex;
  1134. HitTestType ht = HitTest(point, out newIndex, true);
  1135. bool doDrop = false;
  1136. if ( dragging && (ht == HitTestType.DropLine || ht == HitTestType.DropLineLastItem) )
  1137. {
  1138.                             // Delete dropline
  1139. Cursor.Current = Cursors.Default;
  1140. // Erase the Dragline
  1141. DrawDropLine(g, index, false, ht);
  1142. // Move the dragged item to the new location
  1143. // only if the new location is not contiguous to its
  1144. // own location
  1145. if ( dragItemIndex > droppedPosition 
  1146. && Math.Abs(dragItemIndex - droppedPosition) > 0 )
  1147. doDrop = true;
  1148. else if ( dragItemIndex < droppedPosition 
  1149. && Math.Abs(dragItemIndex - droppedPosition) > 1 )
  1150. doDrop = true;
  1151. if ( doDrop )
  1152. {
  1153.                                 // Remove item from its old location
  1154. OutlookBarItem dragItem = bands[currentBandIndex].Items[dragItemIndex];
  1155. bands[currentBandIndex].Items.RemoveAt(dragItemIndex);
  1156. // Insert item in its new location
  1157. if ( dragItemIndex < droppedPosition )
  1158. droppedPosition--;
  1159. bands[currentBandIndex].Items.Insert(droppedPosition, dragItem);
  1160. }
  1161. }
  1162. // Repaint the bar just in case we had a dropline painted
  1163. Invalidate();
  1164. // Highlight item
  1165. if ( !dragging )
  1166. {
  1167. // do not highlight if we are dropping
  1168. forceHightlight = true;
  1169. forceHightlightIndex = index;
  1170. // Fire item clicked property
  1171. FireItemClicked(index);
  1172. }
  1173. else
  1174. {
  1175. // Fire item dropped event
  1176. if ( droppedPosition != -1 && doDrop )
  1177. FireItemDropped(droppedPosition);
  1178. }
  1179. doLoop = false;
  1180. break;
  1181. }
  1182. case (int)Msg.WM_KEYDOWN:
  1183. {
  1184. if ( (int)msg.wParam == (int)VirtualKeys.VK_ESCAPE) 
  1185. doLoop = false;
  1186. break;
  1187. }
  1188. default:
  1189. WindowsAPI.DispatchMessage(ref msg);
  1190. break;
  1191. }
  1192. }
  1193. g.Dispose();
  1194. Capture = false;
  1195. }
  1196. void DrawDropLine(Graphics g, int index, bool drawLine, HitTestType hit)
  1197. {
  1198. Brush brush;
  1199. Pen pen;
  1200. if ( drawLine )
  1201. {
  1202. droppedPosition  = index;
  1203. lastDrawnLineIndex = index;
  1204. brush = SystemBrushes.ControlText;
  1205. pen = SystemPens.ControlText;
  1206. }
  1207. else
  1208. {
  1209. // If there is nothing painted, no need to erase
  1210. if ( lastDrawnLineIndex == -1) return;
  1211. index = lastDrawnLineIndex;
  1212. brush = new SolidBrush(bands[currentBandIndex].Background);
  1213. pen = new Pen(bands[currentBandIndex].Background);
  1214. lastDrawnLineIndex = -1;
  1215. }
  1216. int itemsCount = bands[currentBandIndex].Items.Count;
  1217.             Rectangle itemRect = GetItemRect(g, bands[currentBandIndex], index, Rectangle.Empty);
  1218. Rectangle viewPortRect = GetViewPortRect();
  1219. int y;
  1220. if  ( bands[currentBandIndex].IconView == IconView.Small)
  1221. y = itemRect.Top - Y_SMALLICON_SPACING/2;
  1222. else
  1223. y = itemRect.Top - Y_LARGEICON_SPACING;
  1224. if ( hit == HitTestType.DropLineLastItem )
  1225. {
  1226. // use the bottom of the label if dealing with the last item
  1227. Rectangle labelRect = GetLabelRect(itemsCount-1);
  1228. y = labelRect.Bottom + Y_LARGEICON_SPACING;
  1229. paintedDropLineLastItem = true;
  1230. // the none existing item index
  1231. droppedPosition = itemsCount;
  1232. }
  1233. else if ( paintedDropLineLastItem )
  1234. {
  1235. Rectangle labelRect = GetLabelRect(itemsCount-1);
  1236. y = labelRect.Bottom + Y_LARGEICON_SPACING;
  1237. paintedDropLineLastItem = false;
  1238. }
  1239. // If we are using a bitmap background, erase
  1240. // by painting the portion of the bitmap background
  1241. if ( backgroundBitmap != null && lastDrawnLineIndex == -1 )
  1242. {
  1243.                 Rectangle rcStrip = new Rectangle(viewPortRect.Left, y-6, viewPortRect.Width, 12);
  1244.                 g.DrawImage(backgroundBitmap, rcStrip, rcStrip, GraphicsUnit.Pixel);
  1245. return;
  1246. }
  1247. // Draw horizontal line
  1248. Point p1 = new Point(viewPortRect.Left+11, y);
  1249. Point p2 = new Point(viewPortRect.Right-11, y);
  1250. g.DrawLine(pen, p1, p2);
  1251.             
  1252. // Draw left triangle
  1253. Point top;
  1254. Point bottom;
  1255. if ( index == firstItem )
  1256. top = new Point(viewPortRect.Left+5, y);
  1257. else
  1258. top = new Point(viewPortRect.Left+5, y-6);
  1259. if ( hit == HitTestType.DropLineLastItem )
  1260. bottom =  new Point(viewPortRect.Left+5, y+1);
  1261. else
  1262. bottom = new Point(viewPortRect.Left+5, y+6);
  1263. Point middle = new Point(viewPortRect.Left+11, y);
  1264. Point[] points = new Point[3];
  1265. points.SetValue(top, 0);
  1266. points.SetValue(middle, 1);
  1267. points.SetValue(bottom, 2);
  1268. g.FillPolygon(brush, points);
  1269. // Draw right triangle
  1270. if ( index == firstItem )
  1271. top = new Point(viewPortRect.Right-5, y);
  1272. else
  1273. top = new Point(viewPortRect.Right-5, y - 6);
  1274. if ( hit == HitTestType.DropLineLastItem  )
  1275. bottom = new Point(viewPortRect.Right-5, y+1);
  1276. else
  1277. bottom = new Point(viewPortRect.Right-5, y + 6);
  1278.  
  1279. middle = new Point(viewPortRect.Right-11, y);
  1280. points.SetValue(top, 0);
  1281. points.SetValue(middle, 1);
  1282. points.SetValue(bottom, 2);
  1283. g.FillPolygon(brush, points);
  1284. if ( !drawLine )
  1285. {
  1286. brush.Dispose();
  1287. pen.Dispose();
  1288. }
  1289. }
  1290. void HighlightHeader(int index)
  1291. {
  1292. if ( lastHighlightedHeader == index ) return;
  1293.             
  1294. Graphics g = Graphics.FromHwnd(Handle);
  1295. Rectangle rc;
  1296. if ( lastHighlightedHeader >= 0 )
  1297. {
  1298. rc = GetHeaderRect(lastHighlightedHeader);
  1299. DrawHeader(g, lastHighlightedHeader, rc, Border3DStyle.RaisedInner);
  1300. }
  1301. lastHighlightedHeader = index;
  1302. if ( lastHighlightedHeader >= 0 )
  1303. {
  1304. rc = GetHeaderRect(lastHighlightedHeader);
  1305. DrawHeader(g, lastHighlightedHeader, rc, Border3DStyle.Raised);
  1306. }
  1307. void OnHighlightHeader(Object timeObject, EventArgs eventArgs) 
  1308. {
  1309. Point mousePos = Control.MousePosition;
  1310. mousePos = PointToClient(mousePos);
  1311. Rectangle rc = ClientRectangle;
  1312. if ( !rc.Contains(mousePos) )
  1313. {
  1314. HighlightHeader(-1);
  1315. HighlightItem(-1, false);
  1316. highlightTimer.Stop();
  1317. }
  1318. }
  1319. void HighlightItem(int index, bool pressed)
  1320. {
  1321. // Exit if item state has not changed
  1322. if ( lastHighlightedItem == index && previousPressed == pressed ) return;
  1323. // Remember if we were pressed or not
  1324. previousPressed = pressed;
  1325. if ( lastHighlightedItem >= 0 )
  1326. {
  1327. // Draw the previously highlighted item in normal state
  1328. using ( Graphics g = Graphics.FromHwnd(Handle) )
  1329. {
  1330. Rectangle itemRect = GetItemRect(g, bands[currentBandIndex], lastHighlightedItem, Rectangle.Empty);
  1331. DrawItem(g, lastHighlightedItem, itemRect, false, false, Rectangle.Empty, null);
  1332. }
  1333. }
  1334. lastHighlightedItem = index;
  1335. if ( lastHighlightedItem >= 0 )
  1336. {
  1337. // Draw this item hightlighed
  1338. using ( Graphics g = Graphics.FromHwnd(Handle) )
  1339. {
  1340. Rectangle itemRect = GetItemRect(g, bands[currentBandIndex], lastHighlightedItem, Rectangle.Empty);
  1341. DrawItem(g, lastHighlightedItem, itemRect, true, pressed, Rectangle.Empty, null);
  1342. }
  1343. }
  1344. }
  1345. void ForceHighlightItem(Graphics g, int index)
  1346. {
  1347. // Draw this item hightlighed
  1348. Rectangle itemRect = GetItemRect(g, bands[currentBandIndex], index, Rectangle.Empty);
  1349. DrawItem(g, index, itemRect, true, false, Rectangle.Empty, null);
  1350. }
  1351. public void PerformClick(int index)
  1352. {
  1353. FireItemClicked(index);
  1354.         }
  1355. void FirePropertyChanged(OutlookBarProperty property)
  1356. {
  1357. if ( PropertyChanged != null )
  1358. PropertyChanged(bands[currentBandIndex], property);
  1359. }
  1360. void FireItemClicked(int index)
  1361. {
  1362. if ( ItemClicked != null )
  1363. {
  1364. if ( currentBandIndex == -1)
  1365. SetCurrentBand(0);
  1366. if ( index >= 0 && index < bands[currentBandIndex].Items.Count )
  1367. ItemClicked(bands[currentBandIndex], bands[currentBandIndex].Items[index]);
  1368. }
  1369. }
  1370. void FireItemDropped(int index)
  1371. {
  1372. if ( ItemDropped != null )
  1373. {
  1374. if ( currentBandIndex == -1)
  1375. SetCurrentBand(0);
  1376. if ( index >= 0 && index < bands[currentBandIndex].Items.Count )
  1377. ItemDropped(bands[currentBandIndex], bands[currentBandIndex].Items[index]);
  1378. }
  1379. }
  1380. Rectangle GetWorkRect()
  1381. {
  1382. Rectangle rc = ClientRectangle;
  1383. if ( borderType != BorderType.None )
  1384. rc.Inflate(-2,-2);
  1385. return rc;
  1386. }
  1387. Rectangle GetViewPortRect()
  1388. {
  1389. // This is the area of the control minus
  1390. // the bands headers
  1391. Rectangle rect = GetWorkRect();
  1392. if ( bands.Count > 0 )
  1393. {
  1394. // Decrease the client area by the number of headers
  1395. int top = rect.Top + 1 + BAND_HEADER_HEIGHT * (currentBandIndex + 1);
  1396. int bottom = rect.Bottom - 1 - BAND_HEADER_HEIGHT * (bands.Count - currentBandIndex - 1);   
  1397. return new Rectangle(rect.Left, top, rect.Width, bottom - top);
  1398. }
  1399. return rect;
  1400. }
  1401. Rectangle GetHeaderRect(int index)
  1402. {
  1403. // Make sure we are within bounds
  1404. Debug.Assert((index >= 0 && index <= bands.Count-1), "Invalid Header Index");
  1405. Rectangle rect = GetWorkRect();
  1406. int top = rect.Top;
  1407. int bottom = rect.Bottom;
  1408. int max = bands.Count;
  1409. if ( index > currentBandIndex )
  1410. {
  1411. top = rect.Bottom - 1 - (max - index) * BAND_HEADER_HEIGHT;
  1412. bottom = rect.Bottom - 1 - (max - index - 1) * BAND_HEADER_HEIGHT;
  1413. }
  1414. else
  1415. {
  1416. top = rect.Top + 1 + index * BAND_HEADER_HEIGHT;
  1417. bottom = rect.Top + 1 + (index+1) * BAND_HEADER_HEIGHT;
  1418. }
  1419. return  new Rectangle(rect.Left, top, rect.Width, bottom-top);
  1420. }
  1421. void DrawBackground(Graphics g)
  1422. {
  1423. Rectangle rc = ClientRectangle;
  1424. // If we don't have any bands, just fill the rectangle
  1425. // with the System Control Color
  1426. if ( bands.Count == 0 || currentBandIndex == -1 )
  1427. {
  1428. g.FillRectangle(SystemBrushes.Control, rc);
  1429. return;
  1430. }
  1431. if ( backgroundBitmap == null )
  1432. {
  1433. if ( currentBandIndex >= 0 && bands[currentBandIndex] != null )
  1434. {
  1435. using ( SolidBrush b = new SolidBrush(bands[currentBandIndex].Background) )
  1436. {
  1437. // If there is a child control clip the area where the child
  1438. // control will be drawn to avoid flickering
  1439. if ( HasChild() )
  1440. {
  1441. Rectangle clipRect = GetViewPortRect();
  1442. g.ExcludeClip(clipRect);
  1443. }
  1444. g.FillRectangle(b, rc);
  1445. }
  1446. }
  1447. }
  1448. else 
  1449. {
  1450. // Draw custom background bitmap
  1451. // -- I don't know why but the bitmap is not painted properly if using the right size
  1452.     // I use the WindowsAPI to work around the problem
  1453. GDIUtil.StrechBitmap(g, rc, backgroundBitmap);
  1454. if ( needBackgroundBitmapResize )
  1455. {
  1456. needBackgroundBitmapResize = false;
  1457. backgroundBitmap = GDIUtil.GetStrechedBitmap(g, rc, backgroundBitmap);
  1458. }
  1459. }
  1460. }
  1461. void DrawBorder(Graphics g)
  1462. {
  1463. Rectangle rc = ClientRectangle;
  1464. if ( borderType == BorderType.FixedSingle )
  1465. {
  1466. g.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width-1, rc.Height-1);
  1467. }
  1468. else if ( borderType == BorderType.Fixed3D )
  1469. {
  1470. ControlPaint.DrawBorder3D(g, rc, Border3DStyle.Sunken);
  1471. }
  1472. else if ( borderType == BorderType.Custom )
  1473. {
  1474. Pen p1 = new Pen(leftTopColor);
  1475. Pen p2 = new Pen(RightBottomColor);
  1476. g.DrawRectangle(p1, rc.Left, rc.Top, rc.Width-1, rc.Height-1);
  1477. g.DrawRectangle(p2, rc.Left+1, rc.Top+1, rc.Width-3, rc.Height-3);
  1478. p1.Dispose();
  1479. p2.Dispose();
  1480. }
  1481. }
  1482. void DrawHeaders(Graphics g)
  1483. {
  1484. Rectangle rc;
  1485. for (int i = 0; i < bands.Count; i++)
  1486. {
  1487. rc = GetHeaderRect(i);
  1488. DrawHeader(g, i, rc, Border3DStyle.RaisedInner);
  1489. }
  1490. }
  1491. void DrawHeader(Graphics g, int index, Rectangle rc, Border3DStyle style)
  1492. {
  1493. string bandName = bands[index].Text;
  1494. using ( Brush b = new SolidBrush(ColorUtil.VSNetControlColor) ) 
  1495. {
  1496. g.FillRectangle(b, rc);
  1497. }
  1498. if ( ColorUtil.UsingCustomColor )
  1499. {
  1500. Pen p;
  1501. if ( style == Border3DStyle.RaisedInner )
  1502. {
  1503. p = new Pen(ColorUtil.VSNetBorderColor);
  1504. g.DrawRectangle(p, rc.Left, rc.Top, rc.Width-1, rc.Height-1);
  1505. }
  1506. else 
  1507. {
  1508. using ( Brush fillColor = new SolidBrush(ColorUtil.VSNetSelectionColor))
  1509. {
  1510. g.FillRectangle(fillColor, rc);
  1511. }
  1512. p = new Pen(ColorUtil.VSNetBorderColor);
  1513. g.DrawRectangle(p, rc.Left, rc.Top, rc.Width-1, rc.Height-1);
  1514. }
  1515. p.Dispose();
  1516. }
  1517. else 
  1518. {
  1519. ControlPaint.DrawBorder3D(g, rc, style);
  1520. }
  1521. StringFormat stringFormat = new StringFormat();
  1522. stringFormat.LineAlignment = StringAlignment.Center;
  1523. stringFormat.Alignment = StringAlignment.Center;
  1524. g.DrawString(bandName, Font, SystemBrushes.WindowText, rc, stringFormat);
  1525. }
  1526. void DrawArrowButtons(Graphics g)
  1527. {
  1528. // If we don't have any bands just return
  1529. if ( bands.Count == 0 ) return;
  1530. int first, last;
  1531. GetVisibleRange(g, out first, out last);
  1532. Rectangle rc = GetViewPortRect();
  1533. upArrowRect = new Rectangle(0, 0, SystemInformation.VerticalScrollBarWidth, 
  1534. SystemInformation.VerticalScrollBarWidth);
  1535. if ( flatArrowButtons )
  1536. // If using flat arrow buttons, make the rectangle
  1537. // slightly smaller --looks better--
  1538. upArrowRect = new Rectangle(upArrowRect.Left, upArrowRect.Top, SystemInformation.VerticalScrollBarWidth-1,
  1539. SystemInformation.VerticalScrollBarWidth-1);
  1540. }
  1541.                 
  1542. downArrowRect = upArrowRect;
  1543. upArrowRect.Offset(rc.Right - ARROW_BUTTON_MARGIN - SystemInformation.VerticalScrollBarWidth,
  1544. rc.Top + ARROW_BUTTON_MARGIN);
  1545. downArrowRect.Offset(rc.Right - ARROW_BUTTON_MARGIN - SystemInformation.VerticalScrollBarWidth,
  1546. rc.Bottom - ARROW_BUTTON_MARGIN - SystemInformation.VerticalScrollBarWidth);
  1547. // Do we need to show top scroll button
  1548. if ( first > 0 )
  1549. {
  1550. if ( flatArrowButtons )
  1551. DrawFlatArrowButton(g, upArrowRect, ArrowGlyph.Up, UpFlatArrowState);
  1552. else
  1553. {
  1554. if ( upArrowPressed )
  1555. ControlPaint.DrawScrollButton(g, upArrowRect, ScrollButton.Up, ButtonState.Pushed);
  1556. else
  1557. ControlPaint.DrawScrollButton(g, upArrowRect, ScrollButton.Up, ButtonState.Normal);
  1558. }
  1559. upArrowVisible = true;
  1560. }
  1561. else
  1562. {
  1563. upArrowVisible = false;
  1564. UpFlatArrowState = DrawState.Normal;
  1565. }
  1566. // Do we need to show bottom scroll button
  1567. if ( last < bands[currentBandIndex].Items.Count -1 )
  1568. {
  1569. if ( flatArrowButtons )
  1570. DrawFlatArrowButton(g, downArrowRect, ArrowGlyph.Down, DownFlatArrowState);
  1571. else
  1572. {
  1573. if ( downArrowPressed )
  1574. ControlPaint.DrawScrollButton(g, downArrowRect, ScrollButton.Down, ButtonState.Pushed);
  1575. else
  1576. ControlPaint.DrawScrollButton(g, downArrowRect, ScrollButton.Down, ButtonState.Normal);
  1577. }
  1578. downArrowVisible = true;
  1579. }
  1580. else
  1581. {
  1582. downArrowVisible = false;
  1583. DownFlatArrowState = DrawState.Normal;
  1584. }
  1585. }
  1586. void DrawFlatArrowButton(Graphics g, Rectangle rc, ArrowGlyph arrowGlyph, DrawState state)
  1587. {
  1588. if ( HasChild() )
  1589. {
  1590. // Dont' draw flat button if there is a band with a child control
  1591. return;
  1592. }
  1593. Brush b;
  1594. if ( state == DrawState.Hot )
  1595. {
  1596. b = new SolidBrush(ColorUtil.VSNetCheckedColor);
  1597. }
  1598. else
  1599. {
  1600. b = new SolidBrush(ColorUtil.VSNetControlColor);
  1601. }
  1602. g.FillRectangle(b, rc.Left, rc.Top, rc.Width-1, rc.Height-1);
  1603. b.Dispose();
  1604. using ( Pen p = new Pen(ColorUtil.VSNetBorderColor) )
  1605. {
  1606. if ( state == DrawState.Hot )
  1607. {
  1608. g.DrawRectangle(p, rc.Left, rc.Top, rc.Width-2, rc.Height-2);
  1609. }
  1610. GDIUtil.DrawArrowGlyph(g, rc, 7, 4, arrowGlyph, Brushes.Black);
  1611. }
  1612. // Remember last state of the flat arrow button
  1613. if ( arrowGlyph == ArrowGlyph.Up )
  1614. {
  1615. UpFlatArrowState = state;
  1616. }
  1617. else
  1618. {
  1619. DownFlatArrowState = state;
  1620. }
  1621. }
  1622. void DrawArrowButton(Rectangle buttonRect, ButtonState state)
  1623. {
  1624. if ( HasChild() )
  1625. {
  1626. // No drawing buttons if there is a child control in the current band
  1627. return;
  1628. }
  1629. // Directly draw the arrow button
  1630. Graphics g = Graphics.FromHwnd(Handle);
  1631. ArrowGlyph arrowGlyph = ArrowGlyph.Up;
  1632. ScrollButton buttonType = ScrollButton.Up;
  1633. if ( buttonRect.Equals(downArrowRect ))
  1634. {
  1635. buttonType = ScrollButton.Down;
  1636. arrowGlyph = ArrowGlyph.Down;
  1637. }
  1638. if ( flatArrowButtons && !HasChild() )
  1639. {
  1640. int first;
  1641. int last;
  1642. GetVisibleRange(g, out first, out last);
  1643. if ( arrowGlyph == ArrowGlyph.Up && first == 0 ) 
  1644. {
  1645. // up arrow is not visible no need to pain it
  1646. return;
  1647. }
  1648. else if ( arrowGlyph == ArrowGlyph.Down && last == bands[currentBandIndex].Items.Count -1)
  1649. {
  1650. // down arrow is not visible no need to pain it
  1651. return;
  1652. }
  1653. // If using flat button and the mouse is still over the button
  1654. // then don't allow drawing it without the hightlight
  1655. Point pos = Control.MousePosition;
  1656. pos = PointToClient(pos);
  1657. if ( buttonRect.Contains(pos) )
  1658. {
  1659. if ( arrowGlyph == ArrowGlyph.Up )
  1660. UpFlatArrowState = DrawState.Hot;
  1661. else
  1662. DownFlatArrowState = DrawState.Hot;
  1663. DrawFlatArrowButton(g, buttonRect, arrowGlyph, DrawState.Hot);
  1664. }
  1665. else
  1666. {
  1667. if ( arrowGlyph == ArrowGlyph.Up )
  1668. UpFlatArrowState = DrawState.Normal;
  1669. else
  1670. DownFlatArrowState = DrawState.Normal;
  1671. DrawFlatArrowButton(g, buttonRect, arrowGlyph, DrawState.Normal);
  1672. }
  1673. }
  1674. else 
  1675. ControlPaint.DrawScrollButton(g, buttonRect, buttonType, state);
  1676. g.Dispose();
  1677. }
  1678. void DrawItems(Graphics g, Rectangle targetRect, OutlookBarBand drawBand)
  1679. {
  1680. // If we don't have any bands just return
  1681. if ( bands.Count == 0 ) return;
  1682. Rectangle rc = GetViewPortRect();
  1683. OutlookBarBand band = bands[currentBandIndex];
  1684. if ( drawBand != null ) band = drawBand;
  1685. Debug.Assert(band != null);
  1686. for ( int i = firstItem; i < band.Items.Count; i++ )
  1687. {
  1688. Rectangle itemRect = GetItemRect(g, band, i, targetRect);
  1689. if ( itemRect.Top > rc.Bottom )
  1690. break;
  1691. else
  1692. DrawItem(g, i, itemRect, false, false, targetRect, drawBand);
  1693. }
  1694. }
  1695. void DrawItem(Graphics g, int index, Rectangle itemRect, bool hot, bool pressed, Rectangle targetRect, OutlookBarBand drawingBand)
  1696. {
  1697. OutlookBarBand band = bands[currentBandIndex];
  1698. if ( drawingBand != null ) band = drawingBand;
  1699. Point pt = new Point(0,0);
  1700. // Set clip region so that we don't draw outside the viewport
  1701. Rectangle viewPortRect = GetViewPortRect();
  1702. if ( targetRect != Rectangle.Empty )
  1703. viewPortRect = targetRect;
  1704. Region clippingRegion = new Region(viewPortRect);
  1705. g.Clip = clippingRegion;
  1706. // Clip the arrow buttons
  1707. g.ExcludeClip(downArrowRect);
  1708. g.ExcludeClip(upArrowRect);
  1709. Color textColor = band.TextColor;
  1710. Color backColor = band.Background; 
  1711. Color highLight = band.Background;
  1712. if ( ColorUtil.UsingCustomColor )
  1713. {
  1714. backColor = ColorUtil.VSNetControlColor;
  1715. highLight = ColorUtil.VSNetControlColor;
  1716. }
  1717. if ( hot )
  1718. {
  1719. backColor = ColorUtil.VSNetSelectionColor;
  1720. highLight = ColorUtil.VSNetBorderColor;
  1721. }
  1722. if ( pressed ) backColor = ColorUtil.VSNetPressedColor;
  1723. if ( band.IconView == IconView.Large && band.LargeImageList != null ) 
  1724. {
  1725. Size largeImageSize = band.LargeImageList.ImageSize;
  1726. pt.X = itemRect.Left + (viewPortRect.Width - largeImageSize.Width) / 2;
  1727. pt.Y = itemRect.Top;
  1728. Rectangle iconRect = new Rectangle(pt, largeImageSize);
  1729. using ( Brush b = new SolidBrush(backColor) )
  1730. {
  1731. iconRect.Inflate(2,2);
  1732. if ( backgroundBitmap != null )
  1733. {
  1734. g.DrawImage(backgroundBitmap, iconRect, iconRect, GraphicsUnit.Pixel);
  1735. // If we have a background bitmap, draw the item background
  1736. // only during the hot state
  1737. if ( hot)
  1738. {
  1739. g.FillRectangle(b, iconRect.Left, iconRect.Top, 
  1740. iconRect.Width, iconRect.Height);
  1741. }
  1742. }
  1743. else 
  1744. {
  1745. // If we don't have a background, always draw the 
  1746. // item backgound
  1747. g.FillRectangle(b, iconRect.Left, iconRect.Top, 
  1748. iconRect.Width, iconRect.Height);
  1749. }
  1750. using ( Pen p = new Pen(highLight) )
  1751. {
  1752. if ( backgroundBitmap == null || hot == true )
  1753. {
  1754. g.DrawRectangle(p,  iconRect.Left, iconRect.Top, iconRect.Width-1, iconRect.Height-1);
  1755. }
  1756. }
  1757. }
  1758. // I dont' use the image list to do the drawing because cliping does not work
  1759. if ( band.Items[index].ImageIndex != -1 && band.LargeImageList != null )
  1760. {
  1761. // Only if we have a valid image index
  1762. g.SmoothingMode  = SmoothingMode.HighQuality;
  1763. g.DrawImage(band.LargeImageList.Images[band.Items[index].ImageIndex], pt);
  1764. g.SmoothingMode  = SmoothingMode.Default;
  1765. }
  1766. // Draw the label
  1767. int top = itemRect.Top + largeImageSize.Height + Y_LARGEICON_LABEL_OFFSET;
  1768. Size textSize = GetLabelSize(g, band, index);
  1769. int left = itemRect.Left + (viewPortRect.Width - textSize.Width) / 2;
  1770. using ( Brush b = new SolidBrush(textColor) )
  1771. {
  1772. g.DrawString(band.Items[index].Text, Font, b, new Point(left, top));
  1773. }
  1774. // Reset clip region to the whole rectangle so that the arrows can be painted
  1775. clippingRegion.Dispose();
  1776. g.Clip = new Region(viewPortRect);
  1777. }
  1778. else if ( band.IconView == IconView.Small && band.SmallImageList != null ) 
  1779. {
  1780. Size smallImageSize = band.SmallImageList.ImageSize;
  1781. pt.X = itemRect.Left;
  1782. pt.Y = itemRect.Top + (itemRect.Height - smallImageSize.Height)/2;
  1783. Rectangle iconRect = new Rectangle(pt, smallImageSize);
  1784. using ( Brush b = new SolidBrush(backColor) )
  1785. {
  1786. iconRect.Inflate(1,1);
  1787. if ( backgroundBitmap != null )
  1788. {
  1789. g.DrawImage(backgroundBitmap, iconRect, iconRect, GraphicsUnit.Pixel);
  1790. // If we have a background bitmap, draw the item background
  1791. // only during the hot state
  1792. if ( hot)
  1793. {
  1794. g.FillRectangle(b, iconRect.Left, iconRect.Top, 
  1795. iconRect.Width, iconRect.Height);
  1796. }
  1797. }
  1798. else 
  1799. {
  1800. // If we don't have a background, always draw the 
  1801. // item backgound
  1802. g.FillRectangle(b, iconRect.Left, iconRect.Top, 
  1803. iconRect.Width, iconRect.Height);
  1804. }
  1805. using ( Pen p = new Pen(highLight) )
  1806. {
  1807. if ( backgroundBitmap == null || hot == true )
  1808. {
  1809. g.DrawRectangle(p,  iconRect.Left, iconRect.Top, iconRect.Width-1, iconRect.Height-1);
  1810. }
  1811. }
  1812. }
  1813. // I dont' use the image list to do the drawing because cliping does not work
  1814. if ( band.Items[index].ImageIndex != -1 && band.SmallImageList != null )
  1815. {
  1816. // Only if we have a valid image index
  1817. g.DrawImage(band.SmallImageList.Images[band.Items[index].ImageIndex], pt);
  1818. }
  1819. // Draw the label
  1820. Size labelSize = GetLabelSize(g, band, index);
  1821. pt.X = pt.X + smallImageSize.Width + X_SMALLICON_LABEL_OFFSET;
  1822. pt.Y = itemRect.Top + (itemRect.Height - labelSize.Height)/2;
  1823. using ( Brush b = new SolidBrush(textColor) )
  1824. {
  1825. g.DrawString(band.Items[index].Text, Font, b, pt);
  1826. }
  1827. }
  1828. }
  1829. void AnimateScroll(int From, int To)
  1830. {
  1831. if ( currentBandIndex == -1 || bands.Count == 0 ) return;
  1832. OutlookBarBand band = bands[currentBandIndex];
  1833. // Make sure we are whithin bounds
  1834. Debug.Assert(From >= 0 && From < bands.Count);
  1835. Debug.Assert(To >= 0 &&  To < bands.Count);
  1836. // Get needed dimensions
  1837. Rectangle viewPortRect = GetViewPortRect();
  1838. Rectangle headerRect = new Rectangle(0, 0, viewPortRect.Width, BAND_HEADER_HEIGHT);
  1839. Rectangle drawingRect = new Rectangle(0, 0, viewPortRect.Width, viewPortRect.Height + headerRect.Height * 2);
  1840. // Use straight GDI to do the drawing
  1841. IntPtr hDC = WindowsAPI.GetDC(Handle);
  1842.             IntPtr hDCFrom = WindowsAPI.CreateCompatibleDC(hDC);
  1843. IntPtr hDCTo = WindowsAPI.CreateCompatibleDC(hDC);
  1844. IntPtr bmFrom = WindowsAPI.CreateCompatibleBitmap(hDC, drawingRect.Width, drawingRect.Height);
  1845. IntPtr bmTo = WindowsAPI.CreateCompatibleBitmap(hDC, drawingRect.Width, drawingRect.Height);
  1846. // Select in the drawing surface
  1847. IntPtr hOldFromBitmap = WindowsAPI.SelectObject(hDCFrom, bmFrom);
  1848. IntPtr hOldToBitmap = WindowsAPI.SelectObject(hDCTo, bmTo);
  1849.             
  1850. // Draw in the memory device context
  1851. Graphics gHeaderDC;
  1852. if ( To > From )
  1853. gHeaderDC = Graphics.FromHdc(hDCTo);
  1854. else
  1855.     gHeaderDC = Graphics.FromHdc(hDCFrom);
  1856. DrawBandBitmap(hDCFrom, bands[From], From, drawingRect);
  1857. DrawBandBitmap(hDCTo,  bands[To], To, drawingRect);
  1858. DrawHeader(gHeaderDC, To, new Rectangle(drawingRect.Left, drawingRect.Top, 
  1859. drawingRect.Width, drawingRect.Top + BAND_HEADER_HEIGHT), Border3DStyle.RaisedInner);
  1860. Rectangle rectFrom = GetHeaderRect(From);
  1861. Rectangle rectTo = GetHeaderRect(To);
  1862. int headerHeight = rectFrom.Height;
  1863. // Do the animation with the bitmaps
  1864. if ( To > From)
  1865. {
  1866. for (int y = rectTo.Top - headerHeight; y > rectFrom.Bottom; y -= headerHeight)
  1867. {
  1868. // Draw From bitmap
  1869. WindowsAPI.BitBlt(hDC, viewPortRect.Left ,rectFrom.Bottom + 1, 
  1870. viewPortRect.Width, y - rectFrom.Bottom - 1, hDCFrom, 0 , 0, PatBltTypes.SRCCOPY);
  1871. // Draw To Bitmap
  1872. WindowsAPI.BitBlt(hDC, viewPortRect.Left, y, viewPortRect.Width, 
  1873. viewPortRect.Bottom - y + headerHeight, hDCTo, 0, 0, PatBltTypes.SRCCOPY);
  1874. Thread.Sleep(animationSpeed);
  1875. }
  1876. }
  1877. else
  1878. {
  1879. Rectangle rcTo = new Rectangle(viewPortRect.Left, 
  1880. viewPortRect.Bottom, viewPortRect.Width, viewPortRect.Bottom - headerHeight);
  1881. for (int y = rectFrom.Top + 1; y < rcTo.Top - headerHeight; y += headerHeight)
  1882. {
  1883. // Draw To Bitmap
  1884. WindowsAPI.BitBlt(hDC, viewPortRect.Left, rectFrom.Top,  viewPortRect.Width, 
  1885. y - rectFrom.Top - 1, hDCTo, 0, 0, PatBltTypes.SRCCOPY);
  1886. // Draw From bitmap
  1887. WindowsAPI.BitBlt(hDC, viewPortRect.Left , y, 
  1888. viewPortRect.Width, viewPortRect.Bottom - y, hDCFrom, 0 , 0, PatBltTypes.SRCCOPY);
  1889. Thread.Sleep(animationSpeed);
  1890. }
  1891. }
  1892. // Cleanup
  1893. WindowsAPI.ReleaseDC(Handle, hDC);
  1894. WindowsAPI.DeleteDC(hDCFrom);
  1895. WindowsAPI.DeleteDC(hDCTo);
  1896. WindowsAPI.SelectObject(hDCFrom, bmFrom);
  1897. WindowsAPI.SelectObject(hDCTo, bmTo);
  1898. WindowsAPI.DeleteObject(bmFrom);
  1899. WindowsAPI.DeleteObject(bmTo);
  1900.             
  1901. }
  1902. void DrawBandBitmap(IntPtr hDC, OutlookBarBand band, int bandIndex, Rectangle drawingRect)
  1903. {
  1904. // Don't do GDI+ calls since you cannot mix them with GDI calls
  1905. if ( !HasChild(bandIndex))
  1906. {
  1907. Color cb = band.Background;
  1908. IntPtr brush = WindowsAPI.CreateSolidBrush(ColorUtil.RGB(cb.R, cb.G, cb.B));
  1909. RECT rect;
  1910. rect.left = drawingRect.Left;
  1911. rect.top = drawingRect.Top;
  1912. rect.right = drawingRect.Left + drawingRect.Width;
  1913. rect.bottom = drawingRect.Top + drawingRect.Height;
  1914. WindowsAPI.FillRect(hDC, ref rect, brush);
  1915. WindowsAPI.DeleteObject(brush);
  1916. }
  1917. if ( HasChild(bandIndex) )
  1918. {
  1919. // Paint child control into memory device context
  1920. Control child = bands[bandIndex].ChildControl;
  1921. bool visible = child.Visible;
  1922. child.Visible = true;
  1923. // Change viewport if needed
  1924. POINT pt = new POINT();
  1925. WindowsAPI.SendMessage( child.Handle, 
  1926. (int)Msg.WM_ERASEBKGND, (int)hDC, 0);
  1927. WindowsAPI.SendMessage( child.Handle, 
  1928. (int)Msg.WM_PAINT, (int)hDC, 0);
  1929. if ( !visible) child.Visible = false;
  1930. }
  1931. else 
  1932. {
  1933. DrawItems(Graphics.FromHdc(hDC), drawingRect, bands[bandIndex]);
  1934. }
  1935. }
  1936. bool HasChild()
  1937. {
  1938. // Flag that tell us if the current band 
  1939. // has a child window
  1940. Control childControl = null;
  1941. if ( currentBandIndex != -1 && bands.Count > 0)
  1942. {
  1943. OutlookBarBand band = bands[currentBandIndex];
  1944. childControl = band.ChildControl;
  1945. }
  1946. return childControl != null;
  1947. }
  1948. bool HasChild(int index)
  1949. {
  1950. // Flag that tell us if the current band 
  1951. // has a child window
  1952. Control childControl = null;
  1953. if ( index != -1 && bands.Count > 0 && index >= 0 && index < bands.Count)
  1954. {
  1955. OutlookBarBand band = bands[index];
  1956. childControl = band.ChildControl;
  1957. }
  1958. return childControl != null;
  1959. }
  1960. void GetVisibleRange(Graphics g, out int first, out int last)
  1961. {
  1962. first = firstItem;
  1963. last = 0;
  1964. OutlookBarBand band = bands[currentBandIndex];
  1965. Rectangle rc = GetViewPortRect();
  1966. Rectangle itemRect;
  1967. for ( int i = firstItem; i < band.Items.Count; i++ )
  1968. {
  1969.                 itemRect = GetItemRect(g, band, i, Rectangle.Empty);
  1970. if ( itemRect.Bottom > rc.Bottom )
  1971. {
  1972. last = i-1;
  1973. break;
  1974. }
  1975. else 
  1976. {
  1977. last = i;
  1978. }
  1979. }
  1980. }
  1981. Size GetItemSize(Graphics g, OutlookBarBand band, int itemIndex, ItemSizeType itemSizeType)
  1982. {
  1983. Size iconSize = new Size(0,0);
  1984. Size labelSize = new Size(0,0);
  1985. if ( itemSizeType == ItemSizeType.Icon || itemSizeType == ItemSizeType.All )
  1986. {
  1987. iconSize = GetIconSize(band);
  1988. if (itemSizeType == ItemSizeType.Icon)
  1989. return iconSize;
  1990. }
  1991. if ( itemSizeType == ItemSizeType.Label || itemSizeType == ItemSizeType.All )
  1992. {
  1993. labelSize = GetLabelSize(g, band, itemIndex);
  1994. if ( itemSizeType == ItemSizeType.Label )
  1995. return labelSize;
  1996. }
  1997. if ( itemSizeType == ItemSizeType.All )
  1998. {
  1999. if ( band.IconView == IconView.Small )
  2000. return new Size(iconSize.Width + labelSize.Width + X_SMALLICON_LABEL_OFFSET, 
  2001. iconSize.Height > labelSize.Height?iconSize.Height:labelSize.Height);
  2002. else
  2003. return new Size(iconSize.Width>labelSize.Width?iconSize.Width:labelSize.Width, iconSize.Height +
  2004. labelSize.Height + Y_LARGEICON_LABEL_OFFSET + Y_LARGEICON_SPACING);
  2005. }
  2006. return new Size(0,0);
  2007. }
  2008. Size GetIconSize(OutlookBarBand band)
  2009. {
  2010. if ( band.IconView == IconView.Large && band.LargeImageList != null )
  2011. return band.LargeImageList.ImageSize;
  2012. else if ( band.IconView == IconView.Small && band.SmallImageList != null )
  2013. return band.SmallImageList.ImageSize;
  2014. return new Size(0,0);
  2015. }
  2016. Size GetLabelSize(Graphics g, OutlookBarBand band, int itemIndex)
  2017. {
  2018.             
  2019. Size textSize = new Size(0,0);
  2020. if ( band.IconView == IconView.Large )
  2021. {
  2022. // Calculate text rectangle including word breaks if needed
  2023. Rectangle rect = GetViewPortRect();
  2024. Rectangle textRect = Rectangle.FromLTRB(rect.Left, rect.Top, rect.Width, rect.Top);
  2025. // The TextUtil function is going to call GDI, but the Graphics object
  2026. // is already being used by GDI+. Pass a null reference so that the TextUtil
  2027. // function uses the Screen Device to calculate the text size
  2028. if ( band.Items[itemIndex].Text != null )
  2029. {
  2030. textSize = TextUtil.GetTextSize(null, band.Items[itemIndex].Text, 
  2031. Font, ref textRect, DrawTextFormatFlags.DT_CALCRECT | 
  2032. DrawTextFormatFlags.DT_CENTER|DrawTextFormatFlags.DT_WORDBREAK); 
  2033. }
  2034. return textSize;
  2035. }
  2036. else
  2037. {
  2038. // Same as above
  2039. // Calculate text rectangle single line
  2040. if ( band.Items[itemIndex].Text != null )
  2041. {
  2042. textSize = TextUtil.GetTextSize(null, band.Items[itemIndex].Text, Font); 
  2043. }
  2044. return textSize;
  2045. }
  2046. }
  2047. Rectangle GetIconRect(int index)
  2048. {
  2049. Rectangle viewPortRect = GetViewPortRect();
  2050. OutlookBarBand band = bands[currentBandIndex];
  2051.             Debug.Assert(band != null);
  2052.             Size imageSize = Size.Empty;
  2053. Rectangle itemRect = Rectangle.Empty;
  2054. Point pt = new Point(0,0);
  2055. using ( Graphics g = Graphics.FromHwnd(Handle) )
  2056. {
  2057. itemRect = GetItemRect(g, band, index, Rectangle.Empty);
  2058. }
  2059. if (  band.IconView == IconView.Small )
  2060. {
  2061. if ( band.SmallImageList != null )
  2062. {
  2063. imageSize = band.SmallImageList.ImageSize;
  2064. pt.X = itemRect.Left;
  2065. pt.Y = itemRect.Top + (itemRect.Height - imageSize.Height)/2;
  2066. }
  2067. return new Rectangle(pt, imageSize);
  2068. }
  2069. else
  2070. {
  2071. if ( band.LargeImageList != null )
  2072. {
  2073. imageSize = band.LargeImageList.ImageSize;
  2074. pt.X = itemRect.Left + (viewPortRect.Width - imageSize.Width) / 2;
  2075. pt.Y = itemRect.Top;
  2076. }
  2077. return new Rectangle(pt, imageSize);
  2078. }
  2079. }
  2080. Rectangle GetLabelRect(int index)
  2081. {
  2082. Rectangle viewPortRect = GetViewPortRect();
  2083. OutlookBarBand band = bands[currentBandIndex];
  2084. Debug.Assert(band != null);
  2085. Size imageSize = Size.Empty;
  2086. Rectangle itemRect = Rectangle.Empty;
  2087. Size labelSize = Size.Empty;
  2088. Point pt = new Point(0,0);
  2089. using ( Graphics g = Graphics.FromHwnd(Handle) )
  2090. {
  2091. itemRect = GetItemRect(g, band, index, Rectangle.Empty);
  2092. labelSize = GetLabelSize(g, band, index);
  2093. }
  2094. if (  band.IconView == IconView.Small )
  2095. {
  2096. if ( band.SmallImageList != null )
  2097. imageSize = band.SmallImageList.ImageSize;
  2098. // Don't include the offset between the icon and the label
  2099. // so that we don't leave a miss hit gap
  2100. pt.X = itemRect.Left + imageSize.Width;
  2101. pt.Y = itemRect.Top + (itemRect.Height - labelSize.Height)/2;
  2102. }
  2103. return new Rectangle(pt.X, pt.Y  , labelSize.Width + X_SMALLICON_LABEL_OFFSET+2 , labelSize.Height);
  2104. }
  2105. else
  2106. {
  2107. if ( band.LargeImageList != null )
  2108. {
  2109. imageSize = band.LargeImageList.ImageSize;
  2110. pt.X = itemRect.Left + (viewPortRect.Width - labelSize.Width) / 2;
  2111. // Don't include the offset between the icon and the label
  2112. // so that we don't leave a miss hit gap
  2113. pt.Y = itemRect.Top + imageSize.Height;
  2114. }
  2115. return new Rectangle(pt.X, pt.Y, labelSize.Width, labelSize.Height+ Y_LARGEICON_LABEL_OFFSET+2);
  2116. }
  2117. }
  2118. private void HandleRecreated(object sender, EventArgs e)
  2119. {
  2120. // If any of the child destroy itself and recreate
  2121. // reattach the Outlookbar as the parent
  2122. Control c = (Control)sender;
  2123. IntPtr hParent = WindowsAPI.GetParent(c.Handle);
  2124. if ( hParent != Handle )
  2125. {
  2126. WindowsAPI.SetParent(c.Handle, Handle);
  2127. }
  2128. }
  2129. void CreateContextMenu()
  2130. {
  2131. // context menu
  2132. MenuItemEx largeIconsMenu = new MenuItemEx("Large Icons", new EventHandler(OnContextMenu));
  2133. MenuItemEx smallIconsMenu = new MenuItemEx("Small Icons", new EventHandler(OnContextMenu));
  2134. MenuItemEx separator1 = new MenuItemEx("-", new EventHandler(OnContextMenu));
  2135. MenuItemEx renameGroup = new MenuItemEx("Rename Group", new EventHandler(OnContextMenu));
  2136. MenuItemEx separator2 = new MenuItemEx("-", new EventHandler(OnContextMenu));
  2137. MenuItemEx renameShortcut = new MenuItemEx("Rename Shortcut", new EventHandler(OnContextMenu));
  2138.             
  2139. contextMenu = new ContextMenu();
  2140. contextMenu.MenuItems.Add(0, largeIconsMenu);
  2141. contextMenu.MenuItems.Add(1, smallIconsMenu);
  2142. contextMenu.MenuItems.Add(2, separator1);
  2143. contextMenu.MenuItems.Add(3, renameGroup);
  2144.             contextMenu.MenuItems.Add(4, separator2);
  2145. contextMenu.MenuItems.Add(5, renameShortcut);
  2146.             
  2147. contextMenu.Popup += new EventHandler(ContextMenuPopup);
  2148. this.ContextMenu = contextMenu;
  2149. }
  2150. public void ContextMenuPopup(object sender, EventArgs e)
  2151. {
  2152. // Update the menu state before displaying it
  2153. OutlookBarBand band = bands[currentBandIndex];
  2154. MenuItemEx largeIconsMenu = (MenuItemEx)ContextMenu.MenuItems[0];
  2155. MenuItemEx smallIconsMenu = (MenuItemEx)ContextMenu.MenuItems[1];
  2156. MenuItemEx renameGroup = (MenuItemEx)ContextMenu.MenuItems[3];
  2157. MenuItemEx renameShortcut = (MenuItemEx)ContextMenu.MenuItems[5];
  2158.             
  2159. int index;
  2160. HitTestType hit = HitTest(lastClickedPoint, out index, false);
  2161. if ( hit == HitTestType.Header )
  2162. {
  2163. renameGroup.Enabled = true;
  2164. largeIconsMenu.Enabled = false;
  2165. smallIconsMenu.Enabled = false;
  2166. renameShortcut.Enabled = false;
  2167. }
  2168. else if ( hit == HitTestType.Item )
  2169. {
  2170. renameGroup.Enabled = false;
  2171. largeIconsMenu.Enabled = false;
  2172. smallIconsMenu.Enabled = false;
  2173. renameShortcut.Enabled = true;
  2174. }
  2175. else 
  2176. {
  2177. renameGroup.Enabled = false;
  2178. largeIconsMenu.Enabled = true;
  2179. smallIconsMenu.Enabled = true;
  2180. renameShortcut.Enabled = false;
  2181. }
  2182. if ( HasChild() )
  2183. {
  2184. largeIconsMenu.RadioCheck = false;
  2185. smallIconsMenu.RadioCheck = false;
  2186. }
  2187. else 
  2188. {
  2189. largeIconsMenu.RadioCheck = (band.IconView == IconView.Large);
  2190. smallIconsMenu.RadioCheck = (band.IconView == IconView.Small);
  2191. }
  2192. }
  2193. void RenameItem()
  2194. {
  2195.             // Display a edit control that will do the editing of  the item
  2196.            // Get the item index first
  2197. int index;
  2198. HitTestType hit = HitTest(lastClickedPoint, out index, false);
  2199. using ( Graphics g = Graphics.FromHwnd(Handle) )
  2200. {
  2201. Rectangle itemRect = GetLabelRect(index);
  2202. // Empty the text box first
  2203. textBox.Text = "";
  2204. // Move it to the top of the item rectangle
  2205. if ( bands[currentBandIndex].IconView == IconView.Large )
  2206.                     itemRect.Inflate(5, 0);
  2207. else
  2208. itemRect = new Rectangle(itemRect.Left, itemRect.Top, itemRect.Right, itemRect.Height + 5);
  2209. textBox.Bounds = itemRect;
  2210. // initialize the text box to the item text
  2211. textBox.Text = bands[currentBandIndex].Items[index].Text;
  2212.                 // Flag that we are editing an item and not a header
  2213. // --We'll use this on the textbox event handlers  
  2214. editingAnItem = true;
  2215. textBox.Visible = true;
  2216. textBox.Focus();
  2217. }
  2218. }
  2219. void RenameHeader()
  2220. {
  2221. // Display a edit control that will do the editing of the header
  2222. // Get the item index first
  2223. int index;
  2224. HitTestType hit = HitTest(lastClickedPoint, out index, false);
  2225. using ( Graphics g = Graphics.FromHwnd(Handle) )
  2226. {
  2227. Rectangle headerRect = GetHeaderRect(index);
  2228. // Empty the text box first
  2229. textBox.Text = "";
  2230. textBox.Bounds = headerRect;
  2231. // initialize the text box to the item text
  2232. textBox.Text = bands[index].Text;
  2233. // Flag that we are editing an header and not a item
  2234. // --We'll use this on the textbox event handlers 
  2235. editingAnItem = false;
  2236. textBox.Visible = true;
  2237. textBox.Focus();
  2238. }
  2239. }
  2240.         
  2241. void TextBoxKeyDown(object sender, KeyEventArgs e)
  2242. {
  2243.             if ( e.KeyCode == Keys.Enter )
  2244. ProcessTextBoxEditing();
  2245. }
  2246. void TextBoxLostFocus(object sender, EventArgs e)
  2247. {
  2248. ProcessTextBoxEditing();
  2249. }
  2250. void ProcessTextBoxEditing()
  2251. {
  2252. // Only if the textbox is actually visible
  2253. if ( textBox.Visible == false ) return;
  2254.             
  2255. string text = textBox.Text;
  2256. textBox.Visible = false;
  2257. int index;
  2258. HitTestType hit = HitTest(lastClickedPoint, out index, false);
  2259. if ( editingAnItem )
  2260. {
  2261. bands[currentBandIndex].Items[index].Text = text;
  2262. // Fire property changed
  2263. FirePropertyChanged(OutlookBarProperty.ShortcutNameChanged);
  2264. }
  2265. else
  2266. {
  2267. bands[index].Text = text;
  2268. // Fire property changed
  2269. FirePropertyChanged(OutlookBarProperty.GroupNameChanged);
  2270. }
  2271. // Invalidate control so that new label size
  2272. // is recalculated
  2273. Invalidate();
  2274. }
  2275. #endregion
  2276. }
  2277. #endregion
  2278. }