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

Resizable Dialog

Part of DflCategory

Description

Demonstrates DialogApp that adjusts the position and size of several controls when the overall application window is resized. (Caveat: Done by a DFL newbie so this may not be best way to accomplish the goal.)

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.treeview;


class ResizableForm: Form
{
this()
{
  text = "DflResizeTest";

  CreateControls();
}

const int DEFAULT_LEFT          = 2;
const int DEFAULT_TOP           = 2;

const int DEFAULT_HEIGHT_ADJUST = 45;
const int DEFAULT_WIDTH_ADJUST  = 10;
const int DEFAULT_RIGHT         = 504;

const int DEFAULT_BOTTOM        = 404;
const int CHAPTREE_WIDTH        = 122;
const int DEFAULT_BUTTON_HEIGHT = 25;

const int DEFAULT_BUTTON_WIDTH  = ((CHAPTREE_WIDTH / 2) - 3);
const int SEARCHBUTTON_HEIGHT   = DEFAULT_BUTTON_HEIGHT;
const int BOOKSBUTTON_WIDTH     = DEFAULT_BUTTON_WIDTH;

const int HELPBUTTON_WIDTH      = DEFAULT_BUTTON_WIDTH;
const int HELPBUTTON_LEFT       = (DEFAULT_RIGHT 
                                  - (DEFAULT_BUTTON_WIDTH + 5));
const int SEARCHBUTTON_WIDTH    = DEFAULT_BUTTON_WIDTH;

const int SEARCHBUTTON_LEFT     = (DEFAULT_BUTTON_WIDTH + 4);
const int SEARCHWORDS_LEFT      = (CHAPTREE_WIDTH + 0);
const int CHAPTREE_HEIGHT       = (DEFAULT_BOTTOM 
                                  - (DEFAULT_BUTTON_HEIGHT 
                                  + DEFAULT_HEIGHT_ADJUST));

const int CHAPTREE_TOP          = (DEFAULT_BUTTON_HEIGHT + 4);
const int OPTIONSBUTTON_WIDTH   = 60;
const int OPTIONSBUTTON_LEFT    = (DEFAULT_RIGHT 
                                  - (OPTIONSBUTTON_WIDTH 
                                  + DEFAULT_BUTTON_WIDTH + 8));

const int TEXTVIEWER_WIDTH      = (DEFAULT_RIGHT 
                                  - (CHAPTREE_WIDTH + 3));
const int SEARCHWORDS_WIDTH     = (DEFAULT_RIGHT 
                                  - (CHAPTREE_WIDTH 
                                  + OPTIONSBUTTON_WIDTH 
                                  + DEFAULT_BUTTON_WIDTH + 8));

private void CreateControls()
{
  size = Size((DEFAULT_RIGHT - DEFAULT_LEFT) + DEFAULT_WIDTH_ADJUST, 
              DEFAULT_BOTTOM - DEFAULT_TOP);  // Overall form size

  resize ~= &ResizeForm;

  booksButton = new Button;
  booksButton.parent = this;
  booksButton.text = "Books";
  booksButton.location = Point(DEFAULT_LEFT, DEFAULT_TOP);
  booksButton.size = Size(DEFAULT_BUTTON_WIDTH, 
                          DEFAULT_BUTTON_HEIGHT);

  helpButton = new Button;
  helpButton.parent = this;
  helpButton.text = "Help";
  helpButton.size = Size(DEFAULT_BUTTON_WIDTH, 
                         DEFAULT_BUTTON_HEIGHT);

  optionsButton = new Button;
  optionsButton.parent = this;
  optionsButton.text = "Options";
  optionsButton.size = Size(DEFAULT_BUTTON_WIDTH, 
                            DEFAULT_BUTTON_HEIGHT);

  searchButton = new Button;
  searchButton.parent = this;
  searchButton.text = "Search";
  searchButton.location = Point(SEARCHBUTTON_LEFT, DEFAULT_TOP);
  searchButton.size = Size(DEFAULT_BUTTON_WIDTH, 
                           DEFAULT_BUTTON_HEIGHT);

  searchWordsTextBox = new TextBox;
  searchWordsTextBox.parent = this;
  searchWordsTextBox.text = "searchWordsTextBox";
  searchWordsTextBox.location = Point(SEARCHWORDS_LEFT, 
                                      DEFAULT_TOP);
  searchWordsTextBox.multiline = false;
  searchWordsTextBox.readOnly = false;

  viewerTextBox = new TextBox;
  viewerTextBox.parent = this;
  viewerTextBox.text = "viewerTextBox";
  viewerTextBox.location = Point(CHAPTREE_WIDTH, CHAPTREE_TOP);
  viewerTextBox.multiline = true;
  viewerTextBox.readOnly = true;
  viewerTextBox.wordWrap = true;

  chapSelectorTree = new TreeView;
  chapSelectorTree.parent = this;
  chapSelectorTree.location = Point(DEFAULT_LEFT, CHAPTREE_TOP);

  ResizeForm(null, null);
}
  

private void ResizeForm(Object sender, EventArgs ea)
{
  Size formClientSize = clientSize();
  Rect formBounds     = bounds();
  int  formHeight     = formClientSize.height;
  int  formWidth      = formClientSize.width;
   
  int helpButtonLeft    = (formWidth  - (DEFAULT_BUTTON_WIDTH + 5));
  int chapTreeHeight    = (formHeight - (DEFAULT_BUTTON_HEIGHT + 10));
  int optionsButtonLeft = (formWidth  - (OPTIONSBUTTON_WIDTH 
                           + DEFAULT_BUTTON_WIDTH + 8));
  int textViewerWidth   = (formWidth  - (CHAPTREE_WIDTH + 3));
  int searchWordsWidth  = (formWidth  - (CHAPTREE_WIDTH 
                           + OPTIONSBUTTON_WIDTH 
                           + DEFAULT_BUTTON_WIDTH + 8));

  searchWordsTextBox.size = Size(searchWordsWidth, 
                                 DEFAULT_BUTTON_HEIGHT);
  viewerTextBox.size = Size(textViewerWidth, chapTreeHeight);
  chapSelectorTree.size = Size(CHAPTREE_WIDTH, chapTreeHeight);

  optionsButton.location = Point(optionsButtonLeft, DEFAULT_TOP);
  helpButton.location = Point(helpButtonLeft, DEFAULT_TOP);
}


private Button    booksButton;
private Button    helpButton;
private Button    optionsButton;
private Button    searchButton;

private TreeView  chapSelectorTree;
private TextBox   searchWordsTextBox;

private TextBox   viewerTextBox;
}

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

Source

Link http://www.dsource.org/tutorials/index.php?show_example=136
Posted by Lynn
Date/Time Wed Dec 22, 2004 4:04 pm