Split Windows
Volume Number: | | 5
|
Issue Number: | | 6
|
Column Tag: | | Pascal Procedures
|
Related Info: Window Manager
Have Your Window Do The Splits
By Kirk Chase, Anaheim, CA
Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.
Split Personality
As I was looking in chapter two of Inside Macintosh, Vol. I, I saw a sweet little interface under the heading Splitting a Window. I thought about that interface and decided it would be nice to create a split bar in a window. So, I set out to write a control definition for the split bar and a short little demo to see what additional support was needed by the application.
The Split Bar Interface
A split bar is used to divide a window into two or more views of a document. The split bar creates and readjusts scroll bars on top and bottom of the split bar as it is moved. This allows the user to scroll each view, called a pane, to different areas (See Figure 1). Many word processing and spreadsheet applications use split bars.
For example, at program start up, there is one scroll bar and a black rectangle (the split bar) directly above it and beneath the drag bar of the window. The user then clicks on the split bar and drags it across the top of the scroll bar. As he releases the split bar, the scroll bars top is brought down on the screen just under the split bar again and another scroll bar above the split bar fills the space left by moving the split bar. As the user continues to move the split bar up or down, the top and bottom scroll bars are adjusted in height accordingly.
Figure 1.
The Split Bar CDEF
The split bar functions like a scroll bar with only a thumb. In some ways, it is easier than a normal scroll bar to create. For example, there is no concern for page up/down or line up/down-- only a thumb routine (which is not very easy until you know how). I include two variations of the CDEF for both horizontal (1) and vertical (2) split bars.
The CDEF for a split bar is somewhere between the complexity of a normal button and a scroll bar. If you read up on defining your own controls (Inside Macintosh, Vol. I, Chapter 10), there are nine routines that your control function may or may not need to handle. Most of the routines I, and others, have covered before in previous issues of MacTutor (Vol. 5, No. 1 and 4). I will only examine those parts that apply to the split bar.
calcCRgns Message
This message asks the CDEF to calculate the region of the control or indicators and store it in the region handle supplied in the param parameter. Upon entry, you test param to see if the high bit is set. If it is, you give the region for the indicator, and if not, you give the region of the control.
The region of the control is its enclosing rectangle. This rectangle extends the length of the window. This area is the same, except for a few areas, as the area the two scroll bars occupy. (See Figure 2).
Figure 2 Scroll and Split Bars
Stacking controls is not a good idea (how do you know which control you are in?). But, since the Split Bar CDEF only reports a mouse down in the indicator, which is not covered, we have no problem knowing which control was pressed.
Now if the message wants the region of the thumb indicator, it is because it is about to be dragged around the screen with DragGrayRgn. Rather than just the region of the thumb, I decided to give additional, visual interface to the user by dragging a rectangular gray region across the window along with the thumb. In the application, I draw a couple lines across the screen to separate the panes, and this gives the feel that these two lines are part of the control. Calculating this other region is easy since all points are known.
thumbCntl Message
This message is another routine used as a precursor to dragging an image of the thumb. The param parameter is a pointer to the following record:
thumbinfo = record;
limitRect : Rect;
trackRect : Rect;
axis : integer;
end;
This record has the same meaning as that of the DragControl procedure. limitRect confines the movement of the indicator itself; trackRect defines the rectangle where the indicator will track with the mouse (these two are just the controls rectangle); and axis determines tracking constraints-- 1 for horizontal, 2 for vertical, and 0 for none. Notice that the axis constraints are the same as the variation of the control (I did not implement a 0 variation).
posCntl Message
After the thumb has been dragged all over, the CDEF is called again with a posCntl message to reposition the thumb. You must erase the old position of the indicator which I do with a flag sent to my draw routine and invalidating the place where the indicator was.
The next step is to figure out the new value of the control. Our friend, param, is typecasted to a point and holds the relative offset both horizontal and vertical. The new value of the control is the old value plus the offset, minus any amount that goes out of bounds. Finally, the control is drawn at its new value.
Other Messages
The initCntl message just sets the contrlAction field of the control to nil. The drawCntl message holds nothing mysterious that hasnt been covered before. There is nothing done for the dispCntl, dragCntl, and autoTrack messages. The latter two messages are for further customizing your control.
The Split Bar Demo
I decided to write a simple Text Edit demo using a vertical split bar. The user could pull the split bar up and down, resize the window, scroll each pane independently, do editing in either pane, and see updates if applicable in both panes. This was certainly a mouthful.
I tried to think about how to handle text in the two panes. I could keep two Text Edit records identical except for view and destination rectangles, or I could use one record and just switch the view and destination rectangles when needed. I decided to implement the latter version. Although separate TE records would have made it simpler to develop and not taken too much memory, I thought that this demo could have easily been a spreadsheet or a drawing application where the document might be too much to duplicate (what if there were a possibility of four or even nine panes?).
The next step was to readjust the view rectangles and scroll bars when the split bar was moved. Since the value of the split bar control was in pixels, this presented no problems. I just adjusted the bottom of the top TE view rectangle by the split bar value and the lower TE view rectangle by split bar value and the indicator width. I also did the same with the scroll bars. In fact, I used the same routines to fix the view rectangles and scroll bar heights when the window was grown as when the split bar was moved.
There was the problem of when the height of the scroll bar was less than what was needed to draw the scroll bar properly. So if the height was smaller, I made the control invisible. I also restricted the growing of the window as to leave a minimum space from the split bar. Therefore, if the height did not allow a scroll bar to be drawn correctly, it was not drawn, and the user could not scroll that pane.
Besides responding to the grow and split bar, each pane must respond to editing such as cut, copy, paste, and keyboard input. The standard procedure is used in the active pane and then the other pane is invalidated to take care of updating in the update procedure.
The pane where the action such as scrolling and editing happens is quite easy to figure out. The scroll bars have a pane associated with it. They switch the TE record to the proper pane, scroll the TE record, and then reset the TE record back to the active pane. Mouse downs in the content region see which pane they are in and then switch the active pane. I also do automatic scrolling to the selection for the active pane only (the non active pane only moves when scrolled by its scroll bar or by major editing).
The updating of the window is easy to accomplish. Simply switch panes and do a TEUpdate call to both panes.
That is about it, the demo is pretty much your standard TE demo you have seen before. You simply switch the view and destination rectangles, duplicate or update the other, and then switch back to the active pane. You will also notice several TEActivates and TEDeactives throughout the source code. I found that the blinking insertion point would stay around if you switched between panes or typed while it was drawn. The solution was to turn it off, switch panes or edit, and then turn it back on.
Improvements
I limited the demo to a single, vertical split bar. To improve the demo, you could include a horizontal split bar making a possibility of four panes. You might even want to add multiple split bars to the horizontal and/or vertical dimensions. This could give you to nine panes if you have two horizontal and two vertical split bars. There would be the problem of keeping a split bar invisible if it was covered by another split bar.
There is one minor bug I could not track down. On starting the demo after the Mac has been restarted, the indicators outline is not drawn when it is dragged. However, if you click on the drag bar of the window, creating a window outline, then the indicator outline appears from then on, until the Mac is restarted. I tried fixing this but could not do so. If any of the readers have any insight, please write me at MacTutor, and Ill relay the information in the letters column.
The CDEF allows horizontal and vertical variations (1 and 2). These numbers follow the axis constraints for dragging the thumb. How about a combined horizontal and vertical split bar which would have a variation code of 0 (no constraint)? This could be used for maybe a new and improved combined scrolling. The CDEF could be used for other things such as T bars for drawing programs.
All this is left as an exercise to the reader. Good luck! Now your windows will have the same type of personality as the developer--split.
Listing: splitbar.pas
unit MyControl;
{Splitbar Code Definition Function - ID=17}
{This creates two types of splitbar controls - horizontal,}
{ variation code 1; and vertical, variation code 2}
{A Splitbar is essentially just an indicator (thumb) which}
{ can be moved by the mouse to set }
{up window panes. The control only moves the thumb. It is}
{ up to the application to create/resize normal}
{scrollbars, adjust the content region, and so forth. It }
{will only return the Indicator part code of}
{inThumb (129). There are no page/line up/down parts}
{valid min is 0 and max is screen width - indicatorwidth;}
{ control value is then in pixels}
{To get a horizontal splitbar ask for CDEF 273 ( 16*ID + }
{ variation), and 274 for vertical}
{History}
{3/15/89 Created by Kirk Chase}
interface
{ main entry into CDEF }
function main (varCode: integer; theControl: ControlHandle; message:
integer; param: longint): longint;
implementation
const
vSplitBar = 2; {Variation code for a vertical splitbar}
hSplitBar = 1; {Variation code for a horizontal splitbar}
IndicatorWidth = 6; {width of thumb}
PaneWidth = 4;
draw = 1;
erase = 0;
invisible = 0;
inactive = 255;
function main;
procedure doRect (varcode, value: integer; var theRect: rect);
{calculate indicator rectangle according to varcode}
begin
case varcode of
vSplitBar:
begin
theRect.top := value + theRect.top;
theRect.bottom := theRect.top + IndicatorWidth;
InsetRect(theRect, 1, 0);
end;
hSplitBar:
begin
theRect.left := value + theRect.left;
theRect.right := theRect.left + IndicatorWidth;
InsetRect(theRect, 0, 1);
end;
end;
end;
procedure doInit (myControl: ControlHandle);
{inits control by setting the action proc to nil}
begin
myControl^^.contrlAction := nil; {set action proc }
end; {of doInit}
procedure doDraw (varCode: integer; myControl: ControlHandle; flag:
integer);
{this will draw or erase thumb control according to flag}
var
aRect, iRect: Rect;
oldClip, controlRegion: RgnHandle;
oldPen: PenState;
begin
{only draw if visible}
if (myControl^^.contrlVis <> invisible) then
begin
{ Get controls region & set clip region to region.}
oldClip := NewRgn;
GetClip(oldClip);
{ Set the clip region to the controls rectangle }
aRect := myControl^^.contrlRect;
iRect := aRect;
controlRegion := NewRgn;
RectRgn(controlRegion, aRect);
MoveHHi(Handle(myControl));
HLock(Handle(myControl));
SetClip(controlRegion);
HUnlock(Handle(myControl));
{set pen to normal state}
GetPenState(oldPen);
PenNormal;
FrameRect(aRect); {draw control bounds}
doRect(varcode, myControl^^.contrlValue, iRect);
{either draw or erase indicator}
if flag = draw then
PaintRect(iRect)
else
EraseRect(iRect);
if (myControl^^.contrlHilite = inactive) then
EraseRect(iRect); {inactive controls}
SetClip(oldClip); {Clean up}
DisposeRgn(oldClip);
DisposeRgn(controlRegion);
SetPenState(oldPen);
end;
end; {of doDraw}
function doTest (varcode: integer; myControl: ControlHandle; theParam:
longint): longint;
{returns inThumb or 0 if mousedown in thumb or not}
var
CRect, IRect: Rect;
thePoint: point;
begin
CRect := myControl^^.contrlRect; {initialize values}
IRect := CRect;
thePoint := point(theParam);
doTest := 0;
{test point if active and visible}
if (myControl^^.contrlHilite <> inactive) and (myControl^^.contrlVis
<> invisible) then
begin
{in control?}
if PtInRect(thePoint, CRect) then
begin
{in thumb?}
doRect(varcode, myControl^^.contrlValue, IRect);
if PtInRect(thePoint, IRect) then
doTest := inThumb;
end;
end;
end; {of doTest}
procedure doCalc (varcode: integer; myControl: ControlHandle; theParam:
longint);
{calculate all or indicators region}
var
aRect: Rect;
thumbRgn: RgnHandle;
begin
{ CalcButtnRgn must first find out if high bit is set. }
{ High bit set indicates that region being calculated is}
{ for an indicator }
if not BitTst(Ptr(@theParam), 0) then
begin {whole region}
theParam := longint(BitAnd(theParam, $00FFFFFF));
aRect := myControl^^.contrlRect;
RectRgn(RgnHandle(theParam), aRect);
end
else
begin
aRect := myControl^^.contrlRect;
doRect(varcode, myControl^^.contrlValue, aRect);
thumbRgn := NewRgn;
RectRgn(thumbRgn, aRect);
if varcode = vSplitBar then {get rgn }
SetRect(aRect, 0, aRect.top + 1, aRect.right, aRect.bottom - 1) {vertical
splitbar}
else
SetRect(aRect, aRect.left + 1, 0, aRect.right - 1, aRect.top); {horizontal
splitbar}
RectRgn(RgnHandle(theParam), aRect);
UnionRgn(RgnHandle(theParam), thumbRgn, RgnHandle(theParam));
DisposeRgn(thumbRgn);
end;
end; {of doCalc}
procedure doThumb (myControl: ControlHandle; varcode: integer; theParam:
longint);
{this sets up dragging parameters for thumb}
type
thumbPtr = ^thumbinfo;
thumbinfo = record
limitRect: Rect;
trackRect: Rect;
axis: integer;
end;
begin
with thumbPtr(theParam)^ do
begin
limitRect := myControl^^.contrlRect;
trackRect := myControl^^.contrlRect;
axis := varcode;
end;
end; {of doThumb}
procedure doPosition (myControl: ControlHandle; varcode: integer; DeltaPoint:
longint);
{this routine is called to reposition the control }
{first erase old position of control and draw in new place}
var
thePoint: point;
value, delta, position: integer;
aRect: rect;
begin
aRect := myControl^^.contrlRect; {get thumb region}
doRect(varcode, myControl^^.contrlValue, aRect);
InvalRect(aRect);
doDraw(varCode, myControl, erase); {erase}
thePoint := point(DeltaPoint);
value := myControl^^.contrlValue;
if varcode = vSplitBar then {calculate delta offset}
begin
position := value + thePoint.v;
delta := thePoint.v;
end
else
begin
position := value + thePoint.h;
delta := thePoint.h;
end;
{recalculate delta offset if out of bounds}
if position < myControl^^.contrlMin then
delta := -(value - myControl^^.contrlMin);
if position > myControl^^.contrlMax then
delta := myControl^^.contrlMax - value;
myControl^^.contrlValue := myControl^^.contrlValue + delta; {reset control
value}
doDraw(varCode, myControl, draw); {redraw}
end; {of doPosition}
begin {main entry point}
main := 0; {initialize values}
case message of {switch to proper routine}
initCntl:
doInit(theControl);
drawCntl:
doDraw(varCode, theControl, draw);
testCntl:
main := doTest(varcode, theControl, param);
{ Calc the region for the button. }
calcCRgns:
doCalc(varcode, theControl, param);
thumbCntl:
doThumb(theControl, varcode, param);
posCntl:
doPosition(theControl, varcode, param);
{ Nothing to do for these messages... }
dragCntl, autoTrack, dispCntl:
;
otherwise
end;
end;
end. {of MyControl Unit}
Listing: MyGlobals.pas
unit MyGlobals;
interface
const
Splitbar = 10; {Scroll bar ID}
Scrollbar2 = 9; {Scroll bar ID}
Scrollbar1 = 8; {Scroll bar ID}
SBarWidth = 16; {ScrollBr Width}
AppleMenuID = 1; {Menu list}
About_Splitbar = 1;
FileMenuID = 2; {Menu list}
C_New = 1;
C_Close = 3;
C_Quit = 5;
EditMenuID = 3; {Menu list}
C_Undo = 1;
C_Cut = 3;
C_Copy = 4;
C_Paste = 5;
SBarMinLen = 55;{Min. Scroll bar hieght to be usable}
CR = Chr(13);
VSpiltbar = 274; {variation code for split bar}
IndicatorWidth = 6; {split bar width}
Margin = 4; {margin inset for TE}
AboutDialogID = 1; {about dialog ID}
WindowID = 2; {MyWindow ID}
About_OK = 1; {OK button in About Dialog}
invisible = 0;
visible = 255;
inactive = 255;
active = 0;
mBarHeightGlobal = $BAA; {location of menu bar height}
WNETrapNum = $60;
UnImplTrapNum = $9F;
MultiEvt = 15;
bit0 = 31;
GrayRgnLowMemGlobal = $9EE;
var
Scroll: array[1..2] of ControlHandle; {scrollbars}
Split: ControlHandle; {splitbar control}
MyWindow: WindowPtr;
viewRect: array[1..2] of rect; {TE viewRects switching}
destRect: array[1..2] of rect; {TE destRects switching}
activePane: integer; {active pane where insertion pt is}
EditText: TEHandle; {the TE record for the editing}
paneRect: rect; {the two lines that split the pane}
AppleMenuHandle, FileMenuHandle, EditMenuHandle: MenuHandle; {various
menu handles}
theWorld: SysEnvRec;
theErr: OSErr;
WNE: boolean;
mBarHeight: integer;
implementation
end.
Listing: MyScroll.pas
unit MyScroll;
interface
uses
MyGlobals;
procedure AdjustScrollBars; {reset max of scroll bars}
procedure AdjustText (pane: integer); {scroll to value}
procedure ScrollCharacter; {scroll selection into view}
procedure ScrollToSelection; { figure which way to scroll}
procedure SetEditText (Index: integer); {Switch TE pane}
procedure FixTextRects; {resize EditTexts rectangles}
procedure FixScrollbarRects;{resize & post scroll/split }
procedure UpdateOtherPane; {inval nonactivepanes update }
implementation
{ This is taken from Symantecs Thinks Pascal}
function LinesInText: integer;
var
lines: integer;
txt: CharsHandle;
begin
with EditText^^ do
begin
lines := nLines;
txt := CharsHandle(hText);
if teLength > 0 then
if txt^^[teLength - 1] = CR then {Return?}
lines := lines + 1;
LinesInText := lines
end
end; {of LinesInText}
procedure AdjustScrollBars; {reset max of scroll bars}
var
ctlMax, index, height: integer;
begin
for index := 1 to 2 do {for each pane}
begin
SetEditText(index); {set pane}
with EditText^^ do
begin
height := (viewRect.bottom - viewRect.top) div lineHeight; {lines of
text in viewRect}
ctlMax := LinesInText - height; {Control max}
if ctlMax < 0 then
ctlMax := 0;
SetCtlMax(Scroll[index], ctlMax)
end
end;
SetEditText(activePane); {reset back to active pane}
end; {of AdjustScrollBars}
procedure AdjustText; {scroll to scrollbar value}
var
oldScroll, newScroll, delta: integer;
begin
SetEditText(pane);
with EditText^^ do
begin
oldScroll := viewRect.top - destrect.top; {get old }
newScroll := GetCtlValue(Scroll[pane]) * lineHeight;
{get new scroll value}
delta := oldScroll - newScroll;
if delta <> 0 then
begin
TEScroll(0, delta, EditText); {scroll to new }
end;
end;
destRect[pane] := EditText^^.destRect; {reset destRect }
SetEditText(activepane); {reset to active pane}
end; {of AdjustText}
procedure ScrollCharacter; {scroll selection into view}
var
theLine, height: integer;
begin
HLock(Handle(EditText));
theLine := 0;
with EditText^^ do
begin
height := (viewRect.bottom - viewRect.top) div lineHeight;
while selStart >= lineStarts[theLine] do
theLine := theLine + 1;
SetCtlValue(Scroll[activePane], theLine - nLines div 2);
AdjustText(activePane);
end;
HUnLock(Handle(EditText));
end; {of scroll character}
procedure ScrollToSelection; {way to scroll to select}
var
topline, bottomline, height, max: integer;
begin
SetEditText(activePane);
HLock(Handle(EditText));
AdjustScrollBars;
AdjustText(activePane);
with EditText^^, viewRect do
begin
topline := GetCtlValue(Scroll[activePane]);
height := (bottom - top) div lineHeight;
bottomline := topline + height;
max := GetCtlMax(Scroll[activePane]);
if max = 0 then {all text within pane}
AdjustText(activePane)
else
ScrollCharacter; {need to scroll}
end;
HUnLock(Handle(EditText));
end; {of ScrollToSelection}
procedure SetEditText;
{Set EditText to indexed pane}
begin
EditText^^.viewRect := viewRect[Index];
EditText^^.destRect := destRect[Index];
end; {of SetEditText}
procedure FixTextRects; {adjust EditTexts two panes}
begin
HLock(Handle(EditText));
viewRect[1] := MyWindow^.portRect; {set lower pane}
viewRect[1].top := Split^^.contrlValue + IndicatorWidth;
viewRect[1].right := viewRect[1].right - SBarWidth;
viewRect[1].bottom := viewRect[1].bottom - SBarWidth;
InsetRect(viewRect[1], Margin, Margin); {Give TE margins}
{Make viewRect a Multiple of lineHeight}
viewRect[1].bottom := viewRect[1].bottom - ((viewRect[1].bottom - viewRect[1].top)
mod EditText^^.lineHeight);
destRect[1] := viewRect[1]; {Set destRect}
viewRect[2] := MyWindow^.portRect; {set upper pane}
viewRect[2].bottom := Scroll[2]^^.contrlRect.bottom;
viewRect[2].right := viewRect[2].right - SBarWidth;
InsetRect(viewRect[2], Margin, Margin); {Give TE margins}
{Make viewRect a multiple of lineHeight}
viewRect[2].bottom := viewRect[2].bottom - ((viewRect[2].bottom - viewRect[2].top)
mod EditText^^.lineHeight);
destRect[2] := viewRect[2];
SetEditText(activePane); {Reset back to active pane}
if EditText <> nil then {recalculate line starts}
begin
TECalText(EditText);
AdjustText(1);
AdjustText(2);
ScrollToSelection;
end;
HUnLock(Handle(EditText));
end; {of FixTextRects}
procedure FixScrollbarRects; {Fix scrollbars to splitbar}
var
width, hieght: integer;
temp: rect;
begin
with MyWindow^.portRect do {get window dimensions}
begin
hieght := bottom - top - SBarWidth + 1;
width := right - left - SBarWidth + 1;
end;
SetRect(temp, width, 0, width + SBarWidth, hieght);
Split^^.contrlRect := temp;
Scroll[1]^^.contrlRect := Split^^.contrlRect;
Scroll[2]^^.contrlRect := Split^^.contrlRect;
Scroll[1]^^.contrlRect.top := Split^^.contrlValue + IndicatorWidth;
Scroll[2]^^.contrlRect.bottom := Split^^.contrlValue;
{check to see if there is enough room to draw scroll bars}
if (Scroll[2]^^.contrlRect.bottom - Scroll[2]^^.contrlRect.top) < SBarMinLen
then
begin {not enough room, make invisible}
EraseRect(Scroll[2]^^.contrlRect);
Scroll[2]^^.contrlVis := invisible;
end
else {enough room, make visible}
Scroll[2]^^.contrlVis := visible;
if (Scroll[1]^^.contrlRect.bottom - Scroll[1]^^.contrlRect.top) < SBarMinLen
then
begin {not enough room, make invisible}
EraseRect(Scroll[1]^^.contrlRect);
Scroll[1]^^.contrlVis := invisible;
end
else {enough room, make visible}
Scroll[1]^^.contrlVis := visible;
if EditText <> nil then {fix TE rectangles}
begin
FixTextRects;
EraseRect(MyWindow^.portRect);
InvalRect(MyWindow^.portRect);
end;
end;{ of FixScrollbarRects}
procedure UpdateOtherPane; {invalidate non active pane}
var
tempRect: array[1..2] of rect;
delta: array[1..2] of integer;
i, index: integer;
pane: rect;
intersect: boolean;
begin
case activePane of {get non active pane}
1:
index := 2;
2:
index := 1;
end;
for i := 1 to 2 do {normalize viewRects}
begin
delta[i] := destRect[i].top;
tempRect[i] := viewRect[i];
OffsetRect(tempRect[i], 0, -delta[i]);
end;
intersect := SectRect(tempRect[1], tempRect[2], pane);
{get intersection}
OffsetRect(pane, 0, delta[index]); {place intersection pt}
if intersect then {invalidate intersecting rectangle}
InvalRect(pane);
end; {of UpdateOtherPane}
end.
Listing: Test_Window.pas
unit Test_Window;
{File name: Test_Window.Pas}
{Function: Handle a Window}
interface
uses
MyGlobals, MyScroll;
procedure Close_Test_Window (whichWindow: WindowPtr);
{Close our window}
procedure Open_Test_Window; {Open our window }
procedure Update_Test_Window; {Update our window }
procedure Do_Test_Window (myEvent: EventRecord);
{Handle action to our window, like controls}
implementation
{=================================}
procedure Close_Test_Window; {Close our window}
begin
if (MyWindow <> nil) and (MyWindow = whichWindow) then
begin
DisposeWindow(MyWindow); {Clear window and controls}
MyWindow := nil;
TEDispose(EditText); {dispose of TE record}
EditText := nil;
end; {End for if (MyWindow<>nil)}
end; {of Close_Test_Window}
procedure UpDate_Test_Window; {Update our window}
begin
PenNormal;
EraseRect(paneRect); {redraw dividing lines two panes }
SetRect(paneRect, 0, Split^^.contrlValue + 1, Split^^.contrlRect.left,
Split^^.contrlValue + 5);
if ((Split^^.contrlValue > 0) and (Split^^.contrlValue < Split^^.contrlMax))
then {is there enough room?}
begin
FrameRect(paneRect);
end;
if (EditText <> nil) then {update EditText to both panes}
begin
TEDeactivate(EditText);
SetEditText(1);
TEUpdate(MyWindow^.portRect, EditText); {Lower pane}
SetEditText(2);
TEUpdate(MyWindow^.portRect, EditText); {Upper pane}
SetEditText(activePane);
TEActivate(EditText);
end;
DrawControls(MyWindow); {Draw all the controls}
DrawGrowIcon(MyWindow); {Draw the Grow box}
end; {of Update_Test_Window}
procedure Open_Test_Window; {Open our window and draw}
var
sTemp: str255; { For initializing the text window}
begin
if (MyWindow = nil) then
begin
MyWindow := GetNewWindow(WindowID, nil, Pointer(-1));
SelectWindow(MyWindow); {Bring our window to front}
SetPort(MyWindow);
{ Make a scroll bars}
Scroll[2] := GetNewControl(Scrollbar2, MyWindow);
Scroll[1] := GetNewControl(Scrollbar1, MyWindow);
{ Make a splitbar}
Split := GetNewControl(Splitbar, MyWindow);
{Set max/min and initial values of scrollbars}
SetCtlMax(Scroll[1], 0);
SetCtlMin(Scroll[1], 0);
SetCtlValue(Scroll[1], 0);
SetCtlMax(Scroll[2], 0);
SetCtlMin(Scroll[2], 0);
SetCtlValue(Scroll[2], 0);
FixScrollbarRects; {redraw scroll/split bars done}
FixTextRects; {get TE records rects for switching}
activePane := 1; {Set active pane}
EditText := TENew(viewRect[1], destRect[1]);
HLock(Handle(EditText)); { setup attributes of TE }
with EditText^^ do
begin
txFont := applFont;
fontAscent := 12;
lineHeight := 12 + 3 + 1;
end;
HUnLock(Handle(EditText));
sTemp := Splitbar available for those with split personalities;
TESetText(Pointer(Ord4(@sTemp) + 1), length(sTemp), EditText);
FixTextRects;
TEActivate(EditText);
ShowWindow(MyWindow);
UpDate_Test_Window; {Do update to draw rest}
end
else
SelectWindow(MyWindow); {Already open, so show it}
end; {of Open_Test_Window}
procedure HandleScrollBars (theControl: ControlHandle; thePart: integer);
{scroll Text while in scrollbar}
var
oldValue, delta, pane: integer;
begin
case GetCRefCon(theControl) of {find which pane}
ScrollBar1:
pane := 1;
ScrollBar2:
pane := 2;
end;
case thePart of {get delta}
inUpButton:
delta := -1;
inDownButton:
delta := 1;
inPageUp:
delta := -(viewRect[pane].bottom - viewRect[pane].top) div EditText^^.lineHeight;
inPageDown:
delta := (viewRect[pane].bottom - viewRect[pane].top) div EditText^^.lineHeight;
end;
if thePart <> 0 then {set Controls value & adjust text}
begin
oldValue := GetCtlValue(theControl);
SetCtlValue(theControl, oldValue + delta);
AdjustText(pane);
end;
end; {of HandleScrollBars}
procedure Do_Test_Window; {Handle action to our window}
var
RefCon: integer;
code: integer; {Location of event in window or controls}
theValue: integer; {Current value of a control}
whichWindow: WindowPtr;
myPt: Point; {Point where event happened}
theControl: ControlHandle; {Handle for a control}
extend: boolean; {TE extending with Shift key modifier}
procedure Do_A_ScrollBar (code: integer); {do ScrollBar}
begin
RefCon := GetCRefCon(theControl); {get control refcon}
case RefCon of {Select correct scrollbar}
Splitbar: {Splitbar}
begin {start for this scroll bar}
TEDeactivate(EditText);
code := TrackControl(theControl, myPt, nil);
FixScrollbarRects;
TEActivate(EditText);
end;
Scrollbar1:
begin {start for this scroll bar}
if code <> inThumb then
code := TrackControl(theControl, myPt, @HandleScrollBars)
else
begin
code := TrackControl(theControl, myPt, nil);
AdjustText(1);
end;
end;
Scrollbar2:
begin {start for this scroll bar}
if code <> inThumb then
code := TrackControl(theControl, myPt, @HandleScrollBars)
else
begin
code := TrackControl(theControl, myPt, nil);
AdjustText(2);
end;
end;
end; {end of case}
end; {Handle a ScrollBar being pressed}
begin {Start of Window handler}
if (MyWindow <> nil) then
begin
code := FindWindow(myEvent.where, whichWindow);
if (myEvent.what = MouseDown) and (MyWindow = whichWindow) then { convert
coords }
begin
myPt := myEvent.where;
GlobalToLocal(myPt);
end;
if (MyWindow = whichWindow) and (code = inContent) then {for our window}
begin
TEDeactivate(EditText); {remove hilighting}
if PtInRect(MyPt, viewRect[1]) then
{determine which pane mouse down in}
begin
SetEditText(1);
activePane := 1;
end
else if PtInRect(MyPt, viewRect[2]) then
begin
SetEditText(2);
activePane := 2;
end;
TEActivate(EditText); {put highlighting back}
if PtInRect(MyPt, EditText^^.viewRect) then
begin
extend := (BitAnd(myEvent.modifiers, ShiftKey) <> 0);
TEClick(myPt, extend, EditText);
end;
code := FindControl(myPt, whichWindow, theControl); {Get type of control}
if (code <> 0) then {Check type of control}
code := TrackControl(theControl, myPt, nil);
if (code = inUpButton) or (code = inDownButton) or (code = inThumb)
or (code = inPageDown) or (code = inPageUp) then
Do_A_ScrollBar(code); {Do scrollbars}
end;
end;
end; {End of procedure}
end. {End of unit}
Listing: SplitbarTest.pas
program SplitbarTest;
{Program name:SplitbarTest.Pas }
{Function: demo application of the Splitbar CDEF 17}
{History: 3/14/89 Original by Prototyper. }
{Modified to work right: By Kirk Chase }
uses
Test_Window, MyGlobals, MyScroll;
var
myEvent: EventRecord; {Event record for all events}
doneFlag: boolean; {Exit program flag}
code: integer; {Determine event type}
SavePort, whichWindow: WindowPtr;
tempRect, GrowRect, DragRect: Rect; {Rect for dragging}
mResult: longint; {Menu list and item selected values}
theMenu, theItem: integer; {Menu list and item selected}
chCode: integer; {Key code}
ch: char; {Key pressed in Ascii}
IBeam: CursHandle; {IBeam Cursor}
sleep: integer; {MF sleep period}
DoIt: boolean;
ResumePeek: WindowPeek;
sysResult: boolean;
procedure D_About; {puts up About Box}
var
GetSelection: DialogPtr; {Name of dialog}
ItemHit: integer;
ExitDialog: boolean; {Flag used to exit the Dialog}
begin
GetSelection := GetNewDialog(AboutDialogID, nil, Pointer(-1)); {Bring
in the dialog resource}
ShowWindow(GetSelection);
SelectWindow(GetSelection);
SetPort(GetSelection);
ExitDialog := FALSE;
repeat
ModalDialog(nil, itemHit); {Wait until an item is hit}
if (ItemHit = About_OK) then
begin
ExitDialog := TRUE;
end;
until ExitDialog;
DisposDialog(GetSelection);
end; {of D_About}
procedure Init_My_Menus; {Initialize the menus}
begin
ClearMenuBar; {Clear any old menu bars}
AppleMenuHandle := GetMenu(AppleMenuID);
AddResMenu(AppleMenuHandle, DRVR); {Add in DAs}
InsertMenu(AppleMenuHandle, 0);
FileMenuHandle := GetMenu(FileMenuID);
InsertMenu(FileMenuHandle, 0);
EditMenuHandle := GetMenu(EditMenuID);
InsertMenu(EditMenuHandle, 0);
DrawMenuBar; {Draw the menu bar}
end; {of Init_My_Menus}
procedure FixMenus; {adjust menu items }
begin
if (FrontWindow <> nil) then {is there a front window?}
begin
if (FrontWindow <> MyWindow) then {window mine?}
begin {somebody elses window is in front}
DisableItem(FileMenuHandle, C_New);
DisableItem(FileMenuHandle, C_Close);
EnableItem(EditMenuHandle, C_Cut);
EnableItem(EditMenuHandle, C_Copy);
EnableItem(EditMenuHandle, C_Paste);
end
else {my window is up in front}
begin
EnableItem(FileMenuHandle, C_Close);
DisableItem(FileMenuHandle, C_New);
DisableItem(EditMenuHandle, C_Undo);
if (EditText^^.selStart <> EditText^^.selEnd) then {is there an non
empty selection?}
begin {non empty selection}
EnableItem(EditMenuHandle, C_Cut);
EnableItem(EditMenuHandle, C_Copy);
end
else
begin
DisableItem(EditMenuHandle, C_Cut);
DisableItem(EditMenuHandle, C_Copy);
end;
if (TEGetScrapLen <> 0) then {some TE Scrap?}
EnableItem(EditMenuHandle, C_Paste)
else
DisableItem(EditMenuHandle, C_Paste);
end;
end
else
begin {no front window}
DisableItem(EditMenuHandle, C_Cut);
DisableItem(EditMenuHandle, C_Copy);
DisableItem(EditMenuHandle, C_Paste);
DisableItem(FileMenuHandle, C_Close);
EnableItem(FileMenuHandle, C_New);
end;
end; {of FixMenus}
procedure FixCursor; {Non MF Cursor adjust}
var
mousept: Point;
ContentRect: Rect;
begin
if FrontWindow = nil then
InitCursor
else if FrontWindow = MyWindow then
begin
GetMouse(mousept);
ContentRect := MyWindow^.portRect;
ContentRect.right := Split^^.contrlRect.left;
ContentRect.bottom := ContentRect.bottom - SBarWidth;
if (PtInRect(mousePt, ContentRect)) then
SetCursor(IBeam^^)
else
InitCursor;
end;
end; {of FixCursor}
procedure Handle_My_Menu (var doneFlag: boolean; theMenu, theItem: integer);
{Handle menu selections}
var
DNA: integer;
BoolHolder: boolean;
DAName: Str255;
SavePort: GrafPtr;
begin {Start of procedure}
case theMenu of {Do selected menu list}
AppleMenuID:
begin
case theItem of {Handle all commands in menu}
About_Splitbar:
begin
D_About; {Call a dialog for About}
end;
otherwise {Handle the DAs}
begin {Start of Otherwise}
GetPort(SavePort); {Save current port}
GetItem(AppleMenuHandle, theItem, DAName);
DNA := OpenDeskAcc(DAName);
SetPort(SavePort);
end;
end; {End of item case}
end; {End for this list}
FileMenuID:
begin
case theItem of
C_New:
begin
Open_Test_Window;
end;
C_Close:
begin
Close_Test_Window(MyWindow);
end;
C_Quit:
begin
doneFlag := TRUE;
end;
end; {End of item case}
end; {End for this list}
EditMenuID:
begin
BoolHolder := SystemEdit(theItem - 1); {Do DA }
if not (BoolHolder) then {If not DA get it}
begin
case theItem of
C_Undo:
begin
end;
C_Cut:
begin
ScrollToSelection;
TECut(EditText);{a Cut in a TE area}
AdjustScrollBars;
AdjustText(1);
AdjustText(2);
UpdateOtherPane;
ScrollToSelection;
end;
C_Copy:
begin
ScrollToSelection;
TECopy(EditText); {a Copy in a TE area}
UpdateOtherPane;
ScrollToSelection;
end;
C_Paste:
begin
ScrollToSelection;
TEPaste(EditText);{a Paste in a TE area}
AdjustScrollBars;
AdjustText(1);
AdjustText(2);
UpdateOtherPane;
ScrollToSelection;
end;
end; {End of item case}
end;
end; {End for this list}
end; {End for lists}
HiliteMenu(0); {Turn menu selection off}
end; {of Handle_My_Menu}
procedure InitMac; {Initialize Mac Stuff}
var
MPtr: ^integer;
begin
MoreMasters;
InitGraf(@thePort);
InitFonts;
InitWindows;
InitMenus;
TEInit;
InitDialogs(nil);
FlushEvents(everyEvent, 0);
InitCursor;
MPtr := pointer(mbarHeightGlobal);
mBarHeight := MPtr^;
end; {of InitMac}
procedure InitApp; {Initialize Application Stuff}
begin
doneFlag := FALSE; {Do not exit program yet}
Init_My_Menus; {Initialize menu bar}
IBeam := GetCursor(IBeamCursor); {Get IBeam Cursor}
DragRect := screenbits.bounds; {set drag rect}
InsetRect(DragRect, 10, 10);
DragRect.top := DragRect.top + mBarHeight;
theErr := SysEnvirons(1, theWorld);
if (theWorld.machineType >= 0) and (NGetTrapAddress(WNETrapNum, ToolTrap)
= NGetTrapAddress(UnImplTrapNum, ToolTrap)) then
WNE := false
else
WNE := true;
sleep := 10;
if TEFromScrap <> noErr then {get TE Scrap from Scrap}
TESetScrapLen(0);
EditText := nil; {Init EditText TE Record}
MyWindow := nil; {Initialize the window}
end; {of InitApp}
{===================================================}
begin {of main program}
InitMac;
InitApp;
Open_Test_Window; {Open the window routines }
repeat {Start of main event loop}
FixCursor;
FixMenus;
if (EditText <> nil) then {See if a TE is active}
TEIdle(EditText); {Blink the cursor if everything is ok}
if WNE then
DoIt := WaitNextEvent(everyEvent, myEvent, sleep, nil)
else
begin
SystemTask;
DoIt := GetNextEvent(everyEvent, myEvent);
end;
if DoIt then {If event then...}
begin {Start handling the event}
code := FindWindow(myEvent.where, whichWindow);
case myEvent.what of {Decide type of event}
MouseDown: {Mouse button pressed}
begin
if (code = inMenuBar) then
begin
mResult := MenuSelect(myEvent.Where);
theMenu := HiWord(mResult);
theItem := LoWord(mResult);
Handle_My_Menu(doneFlag, theMenu, theItem); {Handle the menu}
end; {of inMenuBar}
if (code = InDrag) then
begin
DragWindow(whichWindow, myEvent.where, DragRect); {Drag the window}
end; {of InDrag}
if (code = inGrow) then
begin {Handle the growing}
EraseRect(Split^^.contrlRect);
SetRect(GrowRect, 55, 55 + Split^^.contrlValue, 1000, 1000);
mResult := GrowWindow(whichWindow, myEvent.where, GrowRect); {Grow it}
SizeWindow(whichWindow, LoWord(mResult), HiWord(mResult), TRUE); {Resize
to result}
FixScrollbarRects;
DrawGrowIcon(whichWindow);
AdjustScrollBars;
DrawControls(MyWindow);
AdjustText(1);
AdjustText(2);
ScrollToSelection;
end; {of doing the growing}
if (code = inGoAway) then
begin
if TrackGoAway(whichWindow, myEvent.where) then {See if mouse released
in GoAway box}
begin
Close_Test_Window(MyWindow);
end;
end; {of InGoAway}
if (code = inContent) then
begin
if (whichWindow <> FrontWindow) then
SelectWindow(whichWindow)
else
begin
SetPort(whichWindow);
Do_Test_Window(myEvent);
end; {End of else}
end; {End of inContent}
if (code = inSysWindow) then
SystemClick(myEvent, whichWindow);
end; {of MouseDown}
KeyDown, AutoKey: {Handle key inputs}
begin
with myevent do
begin
chCode := BitAnd(message, CharCodeMask);
ch := CHR(chCode); {Change to ASCII}
if (Odd(modifiers div CmdKey)) then
begin
mResult := MenuKey(ch);
theMenu := HiWord(mResult);
theItem := LoWord(mResult);
if (theMenu <> 0) then
Handle_My_Menu(doneFlag, theMenu, theItem); {Do the menu selection}
end
else if (EditText <> nil) then
begin
TEDeactivate(EditText);
TEKey(ch, EditText);
TEActivate(EditText);
ScrollToSelection;
UpdateOtherPane;
end;
end; {End for with}
end; {of KeyDown,AutoKey}
UpDateEvt: {Update event for a window}
begin
whichWindow := WindowPtr(myEvent.message);
if whichWindow = MyWindow then
begin
GetPort(SavePort); {Save current port}
SetPort(MyWindow); {Set port to my window}
BeginUpdate(whichWindow);
Update_Test_Window; {Update this window}
EndUpdate(whichWindow);
SetPort(SavePort);
end;
end; {of UpDateEvt}
ActivateEvt: {Window activated event}
begin {Handle the activation}
whichWindow := WindowPtr(myevent.message);
if odd(myEvent.modifiers) then
SelectWindow(whichWindow);
end; {of ActivateEvt}
MultiEvt:
begin
if Odd(myEvent.message) then
begin {resume event}
if FrontWindow = MyWindow then
begin
SetPort(MyWindow);
InvalRect(MyWindow^.portRect);
FixMenus;
end
else if FrontWindow <> nil then
begin
ResumePeek := WindowPeek(FrontWindow);
if ResumePeek^.windowKind < 0 then
begin
myEvent.what := activateEvt;
BitSet(@myEvent.modifiers, bit0);
sysResult := SystemEvent(myEvent);
end;
end;
end {of resume event}
else {suspend event}
begin
if FrontWindow = MyWindow then
begin
SetPort(MyWindow);
InvalRect(MyWindow^.portRect);
EnableItem(EditMenuHandle, C_Undo);
EnableItem(EditMenuHandle, C_Cut);
EnableItem(EditMenuHandle, C_Copy);
EnableItem(EditMenuHandle, C_Paste);
DrawGrowIcon(MyWindow);
end
else if FrontWindow <> nil then
begin
ResumePeek := WindowPeek(FrontWindow);
if ResumePeek^.windowKind < 0 then
begin
myEvent.what := activateEvt;
BitClr(@myEvent.modifiers, bit0);
sysResult := SystemEvent(myEvent);
end;
end;
end;
end; {of MultiEvt}
end; {End of case}
end; {end of GetNextEvent}
until doneFlag; {End of the event loop}
end. {End of the program}
Listing: SplitbarTest.r
*********************************************************
* RMaker resource file sources.
* File: SplitbarTest.R
*********************************************************
SplitbarTest.RSRC
????????
INCLUDE XP 40:LSP 2.0:splitbar :Splitbar Icon
INCLUDE XP 40:LSP 2.0:splitbar :Splitbar.CDEF 17
Type KCSB = STR
,0
© 1989 by Kirk Chase and MacTutor\0Dversion 1.0 March 1989
Type SIZE = GNRL
, -1
.H
4800
.L
128000
.L
80000
.I
Type BNDL
,128
KCSB 0
ICN#
0 128
FREF
0 128
Type FREF
, 128
APPL 0
Type DLOG
*
,1
About
50 120 189 380
Visible NoGoAway
1
1
1
* This is the DIALOG or ALERT item list.
*
Type DITL
*
,1
3
Button Enabled
86 83 112 163
OK
StaticText
51 24 68 241
© 1989 Kirk Chase and MacTutor
StaticText
26 37 43 218
Splitbar Test by Kirk Chase
Type WIND
*
,2
Test Window
41 3 338 508
InVisible GoAway
0
2
Type CNTL
*
,10
SplitBar
0 490 282 506
Visible
274
10
0 276 0
,9
Scrollbar2
0 490 0 506
Visible
16
9
1 100 1
,8
Scrollbar1
6 490 282 506
Visible
16
8
1 100 1
Type MENU
*
,1
\14 ;;APPLE menu title
About Splitbar...
(-
,2
File
(New
(-
Close
(-
Quit/Q
,3
Edit
(Undo/Z
(-
Cut/X
Copy/C
Paste/V