Turbo Vision Tutorial

Part 2 -- Windows and Clipboards




Welcome

Hi! Turbo Vision (TV) simplifies a lot of stuff in DOS programming right? Ready for the new stuff? Alright! Before going, please make sure that you totally understand what has happenned in the first part. Moreover, I'm going to use TMyApp defined in the last chapter as a context to move on.

Adding Window

Obviously, you want to add windows to your program. Adding window is simple. Look at the following code:

procedure TMyApp.NewWindow;
var R: TRect;
    MyWin: PWindow;
begin
   R.Assign(0, 0, 70, 22);
   New(MyWin, Init(R, 'My Window', wnNoNumber));
   InsertWindow(MyWin);
end;

Hmm... intuitively easy. Using R, you define the window's dimension. Then you instantiate the window MyWin specifying the dimension and the title: 'My Window'. The constant wnNoNumber specifies that the window is not selectable by Alt keystroke. If you replace wnNoNumber by 1, then your window is associated with Alt+1 shortcut keys. (Note that the number 1 is the one on the main keyboard, i.e. beside near the Tab key). If you use 2 instead, then the shortcut will be Alt+2. And so on. After creating the window, you have to insert the window into your application. This is done by invoking InsertWindow, a method that is defined inside TApplication.

 

Editor Window

Adding an editor window (i.e. the one used in TP7 or BP7) is similar. However, you need to add some extra steps. First of all, you need to inherit the constructor Init of TApplication. Then, you have to mention the maximum heap size. Then assign StdEditorDialog, the standard editor dialog, to EditorDialog. Then, invoke the parents' Init constructor. They are as follows:

constructor TMyApp.Init;
begin
  MaxHeapSize := 8192; { reserve 8,192 x 16 bytes for our editor }
  EditorDialog := StdEditorDialog;   { Use standard editor dialog }
  inherited init;   { Invoke TMyApplication's Init constructor }
  :
  :   { Do something else }
end;

Of course you still need to create the window and insert it into your application as done previously. However, instead of the normal PWindow instance as in MyWin done previously, we use PEditWindow.

 

File Editor

Well, if you'd like to build a text editor, you can just use TFileEditor. In TEditor, we don't have a save method, but here we have. How can we use this? First of all, you have do define two scroll bar object, one for vertical and the other is for horizontal. Then, you have to define an indicator object, i.e. the one for line number at the bottom. Sketch for this step is shown below:

var
  R: TRect;
  hScroll, vScroll: PScrollBar;
  indicator: PIndicator;
  fileEditor: TFileEditor;
begin
  R.Assign(0,0,50,1);  { One line thick for horiz. scroll bar }
  new(hScroll, Init(R));
  R.Assign(0,0,1,20);  { One line wide for vert. scroll bar }
  new(vScroll, Init(R));
  R.Assign(0,0,10,1);  { Small strip for indicator }
  new(indicator, Init(R));
  R.Assign(0,0,50,20); { Area for the file editor }
  new(fileEditor, Init(R, hScroll, vScroll, indicator, 'FILENAME.TXT');
end;

Voila! Easy, right? To save the file, you can just invoke fileEditor.Save;. How about creating a new file? Hmm... why don't you try it? :-) Hint: there is a field called FileName in TFileEditor that indicates the file name being edited.

 

Tiling And Cascading

Tiling and cascading is simple. In HandleEvent, respond the menu commands cmTile and cmCascade by calling Tile and Cascade respectively. Both Tile and Cascade are defined inside TApplication.

 

File Dialog Box

File dialog boxes are used when we want to open or save files. Example on how to use it is as follows:

procedure TMyApp.OpenWindow;   { You can name the method as you wish }
var  R: TRect;
     FileDlg: PFileDialog;
     filename: FNameStr;
const
     buttons : Word = fdOpenButton;  { Buttons available other than cancel }
begin
  filename := '*.*';  { show all files }
  New(FileDlg, Init(filename, 'Open File', '~F~ile name', buttons, 1));

  { Show the dialog, check if user doesn't cancel }
  if ExecuteDialog(FileDlg, @filename) <> cmCancel then
  begin
    { Do something }
    R.Assign(0, 0, 75, 20);
    InsertWindow(New(PEditWindow, Init(R, filename, wnNoNumber)));
  end;
end;

You can easily notice that if the user doesn't cancel (i.e. press Open button), then open that file. Openning the file to show it in the window, we assign the dimension in R, then do InsertWindow. Then create the edit window, by using new statement, feeding the dimension, filename, and the key binding as the parameters of the constructors.

Of course that you can specify more buttons in the file dialog. You can modify the constant buttons by ORing them with fd... constants. For example:

const
     buttons : Word = fdOpenButton or fdOKButton;

Of course, you can detect the return value of ExecuteDialog on this. For further information, please refer to Turbo Vision's Reference.

 

Clipboard

Well, actually clipboard is automatically there in Turbo Vision. So, you don't need to add anything. Cut, copy, and paste is automatically done. However, if you want to display the clipboard to investigate what is exactly the contents of the clipboard at a particular moment, you can follow these steps.

First of all, add ClipboardWin as a field to TMyApp (or your application object). So the class declaration will look like this:

type
   TMyApp = object(TMyApplication)
              ClipboardWin : PEditWindow;
              :

Then, look at the inherited Init constructor (see above). Modify it to:

constructor TMyApp.Init;
var R: TRect;
begin
  MaxHeapSize := 8192;
  EditorDialog := StdEditorDialog;
  inherited init;
  Desktop^.GetExtent(R);    { get desktop dimension }

  ClipboardWin := New(PEditWindow, Init(R, 'Clipboard', wnNoNumber));

  if ValidView(ClipboardWin) <> nil then { Do validity check }
  begin
    ClipboardWin^.Hide;              { Hide clipboard window }
    InsertWindow(ClipboardWin);      { Put it into our app }
    Clipboard := ClipboardWin^.Editor;  { Tell the TV system }
    Clipboard^.CanUndo := false;     { Of course, you can't undo in viewing clipboard }
  end;
  :
  :   { Do something else }
end;

Then, we get the desktop dimension and set it as clipboard window's dimension. After that, clipboard window is created as PEditWindow. Then, we check whether it is OK to add a clipboard window. Before putting it inside, we hide the clipboard initially. Then, tell the Turbo Vision editor system. Of course, you cannot undo clipboard editing, so set the undo property to false.

To show the clipboard later, you can do:

  ClipboardWin^.Select;
  ClipboardWin^.Show;

Well, why don't you add it to the menu and modify HandleEvent accordingly?

 

Closing

Phew! That's all for now. I know that you are eager to make some readme program and/or text editor. :-) See you.

 


Where to go

Chapter 9
News Page
Pascal Lesson 3 index
Contacting Me


Roby Joehanes © 2001