comparison FBGUI.cs @ 27:5bfc6c68591e

FBGUI: Added numeric up/down control, fix for transparent control background
author Ivo Smits <Ivo@UCIS.nl>
date Tue, 16 Apr 2013 01:16:45 +0200
parents 7f0b7a53a000
children
comparison
equal deleted inserted replaced
26:7f0b7a53a000 27:5bfc6c68591e
86 void IFBGControl.KeyUp(Keys g) { KeyUp(g); } 86 void IFBGControl.KeyUp(Keys g) { KeyUp(g); }
87 void IFBGControl.LostKeyboardCapture() { LostKeyboardCapture(); } 87 void IFBGControl.LostKeyboardCapture() { LostKeyboardCapture(); }
88 void IFBGControl.Orphaned() { Orphaned(); } 88 void IFBGControl.Orphaned() { Orphaned(); }
89 protected virtual void Paint(Graphics g) { 89 protected virtual void Paint(Graphics g) {
90 if (!visible) return; 90 if (!visible) return;
91 if (backColor.A != 0) g.Clear(backColor); 91 if (backColor.A == 0xff) g.Clear(backColor);
92 else if (backColor.A != 0) using (Brush brush = new SolidBrush(backColor)) g.FillRectangle(brush, g.ClipBounds);
92 RaiseEvent(OnPaint, new PaintEventArgs(g, Rectangle.Round(g.ClipBounds))); 93 RaiseEvent(OnPaint, new PaintEventArgs(g, Rectangle.Round(g.ClipBounds)));
93 } 94 }
94 protected virtual void MouseMove(Point position, MouseButtons buttons) { RaiseEvent(OnMouseMove, new MouseEventArgs(buttons, 0, position.X, position.Y, 0)); } 95 protected virtual void MouseMove(Point position, MouseButtons buttons) { RaiseEvent(OnMouseMove, new MouseEventArgs(buttons, 0, position.X, position.Y, 0)); }
95 protected virtual void MouseDown(Point position, MouseButtons buttons) { RaiseEvent(OnMouseDown, new MouseEventArgs(buttons, 1, position.X, position.Y, 0)); } 96 protected virtual void MouseDown(Point position, MouseButtons buttons) { RaiseEvent(OnMouseDown, new MouseEventArgs(buttons, 1, position.X, position.Y, 0)); }
96 protected virtual void MouseUp(Point position, MouseButtons buttons) { RaiseEvent(OnMouseUp, new MouseEventArgs(buttons, 1, position.X, position.Y, 0)); } 97 protected virtual void MouseUp(Point position, MouseButtons buttons) { RaiseEvent(OnMouseUp, new MouseEventArgs(buttons, 1, position.X, position.Y, 0)); }
551 private Rectangle DirtyRectangle; 552 private Rectangle DirtyRectangle;
552 553
553 public FBGCursor Cursor { 554 public FBGCursor Cursor {
554 get { return cursor; } 555 get { return cursor; }
555 set { 556 set {
557 if (cursor == value) return;
556 cursor = value; 558 cursor = value;
557 Invalidate(); 559 Invalidate();
558 } 560 }
559 } 561 }
560 public Point CursorPosition { 562 public Point CursorPosition {
1350 } 1352 }
1351 if (changed) Invalidate(); 1353 if (changed) Invalidate();
1352 } 1354 }
1353 } 1355 }
1354 } 1356 }
1355 public class FBGDomainUpDown : FBGControl { 1357 public abstract class FBGUpDownControlBase : FBGControl {
1358 private ButtonState buttonUpState = ButtonState.Normal;
1359 private ButtonState buttonDownState = ButtonState.Normal;
1360 public FBGUpDownControlBase(IFBGContainerControl parent) : base(parent) {
1361 BackColor = Color.White;
1362 Height = 25;
1363 }
1364 protected override void Paint(Graphics g) {
1365 base.Paint(g);
1366 g.DrawRectangle(Pens.DarkBlue, 0, 0, Bounds.Width - 1, Bounds.Height - 1);
1367 int lh = (int)Math.Ceiling(SystemFonts.DefaultFont.GetHeight());
1368 String text = SelectedText;
1369 if (text == null) {
1370 g.FillRectangle(Brushes.DarkGray, 2, 2, Bounds.Width - 4 - 16, Bounds.Height - 4);
1371 } else {
1372 using (StringFormat sf = new StringFormat(StringFormatFlags.NoWrap)) {
1373 sf.LineAlignment = StringAlignment.Center;
1374 g.FillRectangle(Brushes.LightGray, 2, 2, Bounds.Width - 4 - 16, Bounds.Height - 4);
1375 g.DrawString(text, SystemFonts.DefaultFont, SystemBrushes.WindowText, new Rectangle(3, 2, Bounds.Width - 6 - 16, Bounds.Height - 4), sf);
1376 }
1377 }
1378 int xoff = Bounds.Width - 17;
1379 int he = (Bounds.Height - 2) / 2;
1380 ControlPaint.DrawScrollButton(g, xoff, 1, 16, he, ScrollButton.Up, buttonUpState);
1381 ControlPaint.DrawScrollButton(g, xoff, Bounds.Height - he - 1, 16, he, ScrollButton.Down, buttonDownState);
1382 }
1383 protected abstract String SelectedText { get; }
1384 protected override void MouseDown(Point position, MouseButtons buttons) {
1385 CaptureKeyboard(true);
1386 if ((buttons & MouseButtons.Left) != 0) {
1387 CaptureMouse(true);
1388 if (position.X > Bounds.Width - 17) {
1389 if (position.Y < Bounds.Height / 2) {
1390 buttonUpState = ButtonState.Pushed;
1391 ButtonPressUp();
1392 } else {
1393 buttonDownState = ButtonState.Pushed;
1394 ButtonPressDown();
1395 }
1396 Invalidate(new Rectangle(Bounds.Width - 16, 0, 16, Bounds.Height));
1397 }
1398 }
1399 }
1400 protected override void MouseUp(Point position, MouseButtons buttons) {
1401 if ((buttons & MouseButtons.Left) != 0) {
1402 CaptureMouse(false);
1403 buttonUpState = buttonDownState = ButtonState.Normal;
1404 Invalidate(new Rectangle(Bounds.Width - 16, 0, 16, Bounds.Height));
1405 }
1406 }
1407 protected override void KeyDown(Keys key) {
1408 base.KeyDown(key);
1409 if (key == Keys.Down) ButtonPressDown();
1410 else if (key == Keys.Up) ButtonPressUp();
1411 }
1412 protected abstract void ButtonPressUp();
1413 protected abstract void ButtonPressDown();
1414 }
1415 public class FBGDomainUpDown : FBGUpDownControlBase {
1356 private List<Object> items = new List<object>(); 1416 private List<Object> items = new List<object>();
1357 private int selectedIndex = -1; 1417 private int selectedIndex = -1;
1358 private ButtonState buttonUpState = ButtonState.Normal;
1359 private ButtonState buttonDownState = ButtonState.Normal;
1360 private Converter<Object, String> itemFormatter = null; 1418 private Converter<Object, String> itemFormatter = null;
1361 public Boolean AllowSelectEmpty { get; set; } 1419 public Boolean AllowSelectEmpty { get; set; }
1362 public Converter<Object, String> ItemFormatter { get { return itemFormatter; } set { itemFormatter = value; Invalidate(); } } 1420 public Converter<Object, String> ItemFormatter { get { return itemFormatter; } set { itemFormatter = value; Invalidate(); } }
1363 public event EventHandler SelectedIndexChanged; 1421 public event EventHandler SelectedIndexChanged;
1364 public FBGDomainUpDown(IFBGContainerControl parent) 1422 public FBGDomainUpDown(IFBGContainerControl parent) : base(parent) { }
1365 : base(parent) {
1366 BackColor = Color.White;
1367 }
1368 public void AddItem(Object item) { 1423 public void AddItem(Object item) {
1369 items.Add(item); 1424 items.Add(item);
1370 Invalidate(); 1425 Invalidate();
1371 } 1426 }
1372 public void RemoveItem(Object item) { 1427 public void RemoveItem(Object item) {
1400 } 1455 }
1401 } 1456 }
1402 } 1457 }
1403 } 1458 }
1404 } 1459 }
1460 protected override string SelectedText {
1461 get {
1462 if (selectedIndex == -1) return null;
1463 Object item = items[selectedIndex];
1464 if (itemFormatter != null) return itemFormatter(item);
1465 if (item == null) return null;
1466 return item.ToString();
1467 }
1468 }
1405 private void FixSelectedIndex(int change) { 1469 private void FixSelectedIndex(int change) {
1406 int value = selectedIndex; 1470 int value = selectedIndex;
1407 if (value == 0 && change == -1 && !AllowSelectEmpty) change = 0; 1471 if (value == 0 && change == -1 && !AllowSelectEmpty) change = 0;
1408 value += change; 1472 value += change;
1409 if (value < -1) value = -1; 1473 if (value < -1) value = -1;
1410 if (value >= items.Count) value = items.Count - 1; 1474 if (value >= items.Count) value = items.Count - 1;
1411 SelectedIndex = value; 1475 SelectedIndex = value;
1412 } 1476 }
1413 protected override void Paint(Graphics g) { 1477 protected override void ButtonPressDown() {
1414 base.Paint(g); 1478 FixSelectedIndex(1);
1415 g.DrawRectangle(Pens.DarkBlue, 0, 0, Bounds.Width - 1, Bounds.Height - 1); 1479 }
1416 int lh = (int)Math.Ceiling(SystemFonts.DefaultFont.GetHeight()); 1480 protected override void ButtonPressUp() {
1417 if (selectedIndex == -1) { 1481 FixSelectedIndex(-1);
1418 g.FillRectangle(Brushes.DarkGray, 2, 2, Bounds.Width - 4 - 16, Bounds.Height - 4); 1482 }
1419 } else { 1483 }
1420 using (StringFormat sf = new StringFormat(StringFormatFlags.NoWrap)) { 1484 public class FBGNumericUpDown : FBGUpDownControlBase {
1421 sf.LineAlignment = StringAlignment.Center; 1485 private int minimum = 0;
1422 Object item = items[selectedIndex]; 1486 private int maximum = 0;
1423 String text = itemFormatter == null ? (item == null ? String.Empty : item.ToString()) : itemFormatter(item); 1487 private int value = 0;
1424 g.FillRectangle(Brushes.LightGray, 2, 2, Bounds.Width - 4 - 16, Bounds.Height - 4); 1488 public event EventHandler SelectedValueChanged;
1425 g.DrawString(text, SystemFonts.DefaultFont, SystemBrushes.WindowText, new Rectangle(3, 2, Bounds.Width - 6 - 16, Bounds.Height - 4), sf); 1489 public FBGNumericUpDown(IFBGContainerControl parent) : base(parent) { }
1426 } 1490 public int Value {
1427 } 1491 get { return value; }
1428 int xoff = Bounds.Width - 17; 1492 set { if (this.value == value) return; this.value = value; Invalidate(); RaiseEvent(SelectedValueChanged); }
1429 int he = (Bounds.Height - 2) / 2; 1493 }
1430 ControlPaint.DrawScrollButton(g, xoff, 1, 16, he, ScrollButton.Up, buttonUpState); 1494 public int Minimum {
1431 ControlPaint.DrawScrollButton(g, xoff, Bounds.Height - he - 1, 16, he, ScrollButton.Down, buttonDownState); 1495 get { return minimum; }
1432 } 1496 set { minimum = value; if (this.value < minimum) this.Value = minimum; }
1433 protected override void MouseDown(Point position, MouseButtons buttons) { 1497 }
1434 CaptureKeyboard(true); 1498 public int Maximum {
1435 if ((buttons & MouseButtons.Left) != 0) { 1499 get { return maximum; }
1436 CaptureMouse(true); 1500 set { maximum = value; if (this.value > maximum) this.Value = maximum; }
1437 if (position.X > Bounds.Width - 17) { 1501 }
1438 if (position.Y < Bounds.Height / 2) { 1502 public int Step { get; set; }
1439 buttonUpState = ButtonState.Pushed; 1503 protected override string SelectedText {
1440 FixSelectedIndex(-1); 1504 get { return value.ToString(); }
1441 } else { 1505 }
1442 buttonDownState = ButtonState.Pushed; 1506 protected override void ButtonPressDown() {
1443 FixSelectedIndex(1); 1507 Value = Math.Max(minimum, value - Step);
1444 } 1508 }
1445 Invalidate(new Rectangle(Bounds.Width - 16, 0, 16, Bounds.Height)); 1509 protected override void ButtonPressUp() {
1446 } 1510 Value = Math.Min(maximum, value + Step);
1447 }
1448 }
1449 protected override void MouseUp(Point position, MouseButtons buttons) {
1450 if ((buttons & MouseButtons.Left) != 0) {
1451 CaptureMouse(false);
1452 buttonUpState = buttonDownState = ButtonState.Normal;
1453 Invalidate(new Rectangle(Bounds.Width - 16, 0, 16, Bounds.Height));
1454 }
1455 }
1456 protected override void KeyDown(Keys key) {
1457 base.KeyDown(key);
1458 if (key == Keys.Down) {
1459 FixSelectedIndex(1);
1460 } else if (key == Keys.Up) {
1461 FixSelectedIndex(-1);
1462 }
1463 } 1511 }
1464 } 1512 }
1465 public interface IFBGTreeParent { 1513 public interface IFBGTreeParent {
1466 FBGTreeView TreeView { get; } 1514 FBGTreeView TreeView { get; }
1467 int Depth { get; } 1515 int Depth { get; }