Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

DFL Mini-Calc with RMB

Part of DflCategory

Description

Extension of DflMiniCalc with 'Plus' button having ContextMenu when RMB (right mouse button) clicked. There is a common handler for all 3 RMB menu-items, and argument "sender" is used to determine which menu-item was clicked.

Example

// To compile:
// dmd DflTest dfl.lib -L/exet:nt/su:windows:4.0

private import std.string;

private import dfl.form, dfl.event, dfl.drawing, dfl.application;
private import dfl.button, dfl.textbox, dfl.menu, dfl.control;


class MiniCalcForm: Form
{
  this()
  {
    text = "DflMiniCalc";

    CreateOtherControls();    

    CreatePlusButtonWithRmbMenu();    

    acceptButton = _plusButton;
  }
  
  private void CreateOtherControls()
  {
    size = Size(120, 150);  // Overall form size

    _minusButton = new Button;
    _minusButton.parent = this;
    _minusButton.text = "Minus";
    _minusButton.left = 60;
    _minusButton.size = Size(50, 25);
    _minusButton.click ~= &onMinusClick;

    _firstNumberText = new TextBox;
    _firstNumberText.parent = this;
    _firstNumberText.text = "170";
    _firstNumberText.location = Point(10, 30);
    _firstNumberText.size = Size(100, 20);

    _secondNumberText = new TextBox;
    _secondNumberText.parent = this;
    _secondNumberText.text = "252";
    _secondNumberText.location = Point(10, 55);
    _secondNumberText.size = Size(100, 20);

    _resultNumberText = new TextBox;
    _resultNumberText.parent = this;
    _resultNumberText.text = "";
    _resultNumberText.location = Point(10, 80);
    _resultNumberText.size = Size(100, 20);
  }
  
  private void CreatePlusButtonWithRmbMenu()
  {
    _plusButton = new Button;
    _plusButton.parent = this;
    _plusButton.text = "Plus";
    _plusButton.left = 10;
    _plusButton.size = Size(50, 25);
    _plusButton.click ~= &onPlusClick;

    _plusButton.contextMenu = new ContextMenu;

    MenuItem miDouble = new MenuItem;
    miDouble.text = "Double";
    miDouble.click ~= &onPbcmHandler;
    miDouble.index = 0;


    _plusButton.contextMenu.menuItems.add(miDouble);

    MenuItem miTriple = new MenuItem;
    miTriple.text = "Triple";
    miTriple.click ~= &onPbcmHandler;
    miTriple.index = 1;
    _plusButton.contextMenu.menuItems.add(miTriple);

    MenuItem miSeparator = new MenuItem;
    miSeparator.text = "-";
    miSeparator.index = 2;
    _plusButton.contextMenu.menuItems.add(miSeparator);

    MenuItem miHalf = new MenuItem;
    miHalf.text = "Half";
    miHalf.click ~= &onPbcmHandler;
    miHalf.index = 3;
    _plusButton.contextMenu.menuItems.add(miHalf);
  }
  
  void onPbcmHandler(Object sender, EventArgs ea)
  {
    GetNumbers();

    MenuItem mi = cast(MenuItem)sender;
    char[] label = mi.text;
    debug char* _dbgLabel = label;

    switch (label) 
    {
      case "Double":
           _num1 += _num1;
           break;
      
      case "Triple":
           _num1 += (_num1 + _num1);
           break;
      
      case "Half":
           _num1 /= 2;
           break;
    }
    _firstNumberText.text = std.string.toString(_num1);
  }
    
  private void onPlusClick(Object sender, EventArgs ea)
  {
    GetNumbers();
    _result = _num1 + _num2;
    _resultNumberText.text = std.string.toString(_result);
  }

  private void onMinusClick(Object sender, EventArgs ea)
  {
    GetNumbers();
    _result = _num1 - _num2;
    _resultNumberText.text = std.string.toString(_result);
  }

  private void GetNumbers()
  {
    char[] firstValue = _firstNumberText.text;
    char[] secondValue = _secondNumberText.text;
    _num1 = atoi(firstValue);
    _num2 = atoi(secondValue);
  }

  private TextBox _firstNumberText;
  private TextBox _secondNumberText;
  private TextBox _resultNumberText;
  private Button  _plusButton;
  private Button  _minusButton;
  private long    _num1;
  private long    _num2;
  private long    _result;
}


int main()
{
  Application.run(new MiniCalcForm);
  
  return 0;
}

Source

Link http://www.dsource.org/tutorials/index.php?show_example=137
Posted by Lynn
Date/Time Mon Dec 27, 2004 5:47 pm