TweetFollow Us on Twitter

Ports
Volume Number:1
Issue Number:4
Column Tag:C Workshop

Ports

By Chris Derossi

When one thinks of the Macintosh, one of the first things to come to mind is the Mac’s frequent use of windows for multiple, independent areas or objects. Usually, one window is related to one task or concept, and tasks that have very different orientations are in separate windows.

There are several benefits for grouping items or tasks into separate windows. The most important is user interfacing. By using windows, the user can conceptually group similar or common things. This makes the use of sotware more intuitive because the user is shielded from modes in the software. In other words, instead of explicitly needing to have the computer switch to a different mode, all that is needed is to begin operations in a different window.

Obviously, the more intuitive for the user that your software is, the better. Indeed, one of the primary concepts of the Macintosh is that of using tools like icons, the mouse, and windows to decrease the need for special knowledge or special education. In this article, we will take a look at the concept behind windows, that of ports. (In a later article, we will examine windows more closely.)

In order to facilitate the creation of independent windows, the implementation of ports or graph ports provides for completely separate drawing envir- onments. That is, each port is an entire drawing environment, and operations with one port are not related to another.

At any given time, after QuickDraw is initialized, a single port is always active, and is considered the current graph port. QuickDraw operations always occur within the current graph port.

With each graph port are associated several characteristics and parameters. These include location of the port’s bit map, size, clipping region, visible region, background color, and several text mode values. The port’s bit map refers to an area of memory where the bit image of any drawing is put. Usually, this coincides with an area of the screen ram, so that drawing is displayed on the screen. However, this is not a requirement. The port’s bit map can be any area of memory on or off the screen. For example, a port may use an off-screen bit map to prepare an image, then move that image into the screen bit map.

The size of the port is determined by an enclosing rectangle. The rectangle encloses part of the bit map and indicates size and position within the bit map. For ports that are visible, the rectangle encloses an area of the screen.

Clipping region and visible region both restrict the available drawing area within a graph port. While the port itself is rectangular, the clipping and visible areas are regions, providing total flexibility with shape and configuration. The visible region acts like a clipping region, but is separate from it mainly for use by the window manager. (When one window overlaps another, the visible region of the bottom window is set to equal the part of the port that still shows.) There are two regions so that the user may have a region for clipping that is unrelated to the the region needed by the window manager for handling overlap.

Each port is its own drawing environment, and as such has its own drawing characteristics such as pen size, pattern, position, etc. Also, each port has its own text drawing characteristics that are used by the font manager. This way, you need only choose which port in which to draw, and the font, style, and text size associated with that port are automatically used.

In addition to having its own locations, size, and modes, each graph port may also have its own coordinate system. That is, regardless of where on the screen the port is displayed, the upper left corner of the port may be any coordinate. Usually, of course, the upper left coordinate is assigned the value 0,0. This is useful because neither the user nor the programmer need be concerned with the actual position of any port; drawing may be made as if the port were the entire universe. (You might begin to see why moving windows around is so easy; the coordinates inside the window remain constant.)

Since the available coordinate plane is much larger than any visible graph port, this feature allows ports to ‘look at’ different areas of the coordinate plane. For example, a program may draw on the entire plane, and different graph ports may be created as neccessary to view separate parts of the entire area. This can be done without the need for conversions either in the drawing or the displaying; QuickDraw handles all conversions.

There are two important programming practices to develop with regards to ports. The first has already been mentioned; it is a good idea to group similar things, and separate dissimilar things. The second idea is that of port independence. Programs should be written to not rely on any port size or location. (When changing the size of a window, for instance, the program should NOT need a certain size window to perform.)

The sample program this month illustrates the second concept, as the first one is much easier to practice. In this program, the user ‘creates’ television sets on which drawing is displayed. While drawing is occurring, the user may create more TVs, or change which one is active. All this is done by clicking the mouse. If the mouse is clicked in a TV, it becomes the active one, if not, a new TV is created.

Each TV, of course, is a graph port. A port is created, and then in that port a television is drawn. Then, the clipping region of that port is restricted to the ‘picture tube’ part of the picture.

Drawing continues, regardless of which port is active, or where it is. The placement of the port, and the selection of the current port is entirely up to the user. Notice, that the main drawing procedure has absolutely no idea which port is active.

In order to avoid running out of memory, the number of possible ports is restricted. You may vary the constant ‘MaxPorts’ as you like. An array is set up called ‘Screens’ which contains pointers to the graph ports. QuickDraw procedures and functions that act upon ports take a pointer to the port as the parameter, not the port itself. The port is left to be dynamically allocated for flexibility. The type ‘GrafPtr’ is the predefined pointer to a graph port.

The variable ScrnCnt keeps track of how many ports we have created. Since we want to draw only on our TV screens, drawing does not occur if the user has not created any ports yet. When the user clicks the mouse, the program checks to see if the position of the mouse is inside any graph port. The point must first be brought to global coordinates, which makes it independent of any port. Then, one by one, the point is referenced to each graph port and checked against that port’s rectangle. If the point is inside the rectangle, that port is selected as the current one.

When the point lies outside all ports, a new port is created. (Unless all possible ports have been created already; then the program terminates.) The port is initialized and its characteristics are set. Then, the TV is drawn in the port, and the clipping region set. The new port is also set as the current port. Notice that the program explicitly sets the port’s origin. Try playing with the origin values.

When the program ends, it closes each port one by one and frees the memory used by the ports. Then, it creates another temporary port that covers the entire screen. (The default values for the size and location of the port coincide with the screen.) This port is then filled with gray to eliminate the other graphics. Finally, this temporary port is also closed and its memory freed.

Although this is a simple example, it demonstrates a very powerful principle; that of independence. The more independent you programs are, the more flexible they are, and the more control the user may have. This avoids placing pointless limitations on your programs and their users. In addition, programming of this nature helps keep programs functional when the environment changes. (Like moving from a Mac to the Lisa with MacWorks, or future, improved machines.)

This concludes our introduction to the basics of QuickDraw. With this foundation, we can move on into the rest of the Macintosh with a strong background. You might want to try and improve on this sample program as an excersize to increase your skill. For example, try to allow the user to select the size as well as the location of the port. Or, try to prevent the ports from overlapping. If you’re really enthusiastic, have all the TV sets going at once, and let the user change ‘channels’ on each of them, causing different graphics to appear on each channel.

In the next issue, we’ll extend the discussion of ports to windows. We’ll find out what the concept of windows adds to the already powerful idea of ports, and find out what features are provided to the user through the use of windows. Ciao.

program Ports_Demo;

{ Ports_Demo - for MacTutor}
{ -- by Chris Derossi}

 uses
  QuickDraw2;  {QuickDraw2 contains the stuff for regions.}

 const
  MaxScreens = 8;

 type
  TVList = array[1..MaxScreens] of GrafPtr;

 var
  Screens : TVList;
  ScrnCnt : INTEGER;
  done : BOOLEAN;

 procedure SetUp;

 begin
  HideAll;
  ScrnCnt := 0;
  done := FALSE;
 end;

 procedure ShutDown;

  var
   TempPort : GrafPtr;

 begin
  while ScrnCnt > 0 do
   begin
    ClosePort(Screens[ScrnCnt]);
    Dispose(Screens[ScrnCnt]);
    ScrnCnt := ScrnCnt - 1;
   end;

{ Create a new port and fill the screen with gray. }
  NEW(TempPort);
  OpenPort(TempPort);
  BackPat(Gray);
  EraseRect(TempPort^.PortRect);
  ClosePort(TempPort);
  Dispose(TempPort);
 end;

 function MakeTV (Left, Top : INTEGER) : GrafPtr;

  var
   TempPtr : GrafPtr;
   WorkRect : Rect;

 begin
{ Create a new port and set its characteristics. }
  NEW(TempPtr);
  OpenPort(TempPtr);
  PortSize(100, 80);
  MovePortTo(Left, Top);
  SetOrigin(0, 0);

{ Clear the port to white and draw our ‘television’ }
  BackPat(White);
  EraseRect(TempPtr^.PortRect);
  FrameRect(TempPtr^.PortRect);
  MoveTo(80, 0);
  LineTo(80, 80);
  SetRect(WorkRect, 85, 10, 95, 20);
  FrameOval(WorkRect);
  SetRect(WorkRect, 85, 30, 95, 40);
  FrameOval(WorkRect);

{ Restrict the clipping region to the TV ‘screen’ }
  WorkRect := TempPtr^.PortRect;
  WorkRect.right := WorkRect.Right - 20;
  InsetRect(WorkRect, 1, 1);
  ClipRect(WorkRect);
  MakeTV := TempPtr;
 end;

 procedure SetTV;

{ If the mouse is in one of our ports, select that port. If all}
{possible ports are created and the mouse is not in any port,}
{then we’re done. Otherwise, create a new port.}

  var
   MousePt, Pnt1 : Point;
   I, X, Y : INTEGER;
   TempPtr : GrafPtr;

 begin
  TempPtr := nil;
  GetMouse(X, Y); { Current port’s local coordinates }
  repeat { nothing }
  until not Button;

{ Convert to a point, the to global coordinates }
  MousePt.h := X;
  MousePt.v := Y;
  LocalToGlobal(MousePt);
  Pnt1 := MousePt;

{ Scan the existing ports. }
  if ScrnCnt > 0 then { we have some ports to scan }
   for I := 1 to ScrnCnt do
    begin
    SetPort(Screens[I]);
    MousePt := Pnt1;
    GlobalToLocal(MousePt);
    if PtInRect(MousePt, Screens[I]^.PortRect) then
    TempPtr := Screens[I];
    end; { for loop }

  if TempPtr <> nil then { the mouse is in a port; set it }
   SetPort(TempPtr)
  else if ScrnCnt = MaxScreens then { all ports used }
   done := TRUE
  else
   begin
    ScrnCnt := ScrnCnt + 1;
    Screens[ScrnCnt] := MakeTV(Pnt1.h, Pnt1.v);
   end;
 end;

 procedure MainLoop;

  var
   Figure : INTEGER;
   WorkRect : Rect;

 begin
  if Button then
   SetTV;

  if ScrnCnt > 0 then { do some drawing }
   begin
    if random mod 100 < 5 then { Clear the ‘screen’ }
    begin
    SetRect(WorkRect, 0, 0, 90, 90);
    EraseRect(WorkRect);
    end;

{ Create a random rectangle for drawing }
    WorkRect.top := random mod 80;
    WorkRect.left := random mod 80;
    WorkRect.right := WorkRect.left + (random mod 60);
    WorkRect.bottom := WorkRect.top + (random mod 60);
    Figure := random mod 6;
    case Figure of
    0 : 
    FrameRect(WorkRect);
    1 : 
    FrameRoundRect(WorkRect, 18, 18);
    2 : 
    FrameOval(WorkRect);
    3 : 
    PaintRect(WorkRect);
    4 : 
    PaintRoundRect(WorkRect, 18, 18);
    5 : 
    PaintOval(WorkRect);
    end;
   end;
 end;

begin   { Ports_Demo }
 SetUp;
 while not done do
  MainLoop;
 ShutDown;
end.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.