home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / MacStarter Pascal 1.0 / xWindows definition files / xFigureDecoration.p < prev    next >
Encoding:
Text File  |  1992-06-17  |  12.9 KB  |  420 lines  |  [TEXT/PJMM]

  1. unit xFigureDecoration;
  2.  
  3. { This units defines several "figure decorations", which are simply static graphical }
  4. { objects that can be added to xWindows.  Typical uses would be to display  a line }
  5. { across the window, as in :    new(anXRectangle); }
  6. {                                              anXRectangle.setUp(anXWindow,0,50,-1,2) }
  7. { which would draw a line two pixels thick across the window, 50 pixels down from }
  8. { the top.  Note how the use of -1 as the third parameter anchors the right end of the }
  9. { line to the right side of the widow. }
  10. {      Also defined in this unit is the class xDisplayString, which can display text in }
  11. { various sizes and fonts.  You can change the string displayed, as well as change its }
  12. { characteristics. }
  13. {       Finally, a classes for displaying PICTs and ICONs are provided. }
  14.  
  15. interface
  16.  
  17. uses
  18.     xWindow;
  19.  
  20. type
  21.  
  22.     xFigureDecoration = object(xWindowDecoration)
  23.           { An abstract class defininig the basic behaviour of several different simple }
  24.           { geometric figures.  The subclasses of this class simply override the method }
  25.           { drawIt.   Note that the procedures SetNib and SetPattern do not force a }
  26.           { redrawing of the figure.  You should call them immediately after calling }
  27.           { SetUp.  (If you do want to change the characteristics of an existing figure, }
  28.           { you will have to force a redraw operation yourself.) }
  29.             nib: point;   { the pen size for drawing the figure, by default 1 X 1 pixel. }
  30.             paintPattern: pattern;  { the pattern for filling the figure, by default solid black }
  31.             procedure setUp (win: xWindow;
  32.                                         theLeft, theTop, theWidth, theHeight: integer);
  33.           { Initialize the figure and install it into the xWindow win.  The other parameters }
  34.           { determine the placement and size of the figure.  Read about their meanings in }
  35.           { the description of the method xWindowDecoration.Install, which can be found in }
  36.           { the unit xWindow.p. }
  37.             procedure SetNib (dh, dv: integer);
  38.           { Set the pen size to be dh X dv pixels; this will not affect filled figures. }
  39.             procedure SetPattern (thePattern: pattern);
  40.           { Set the fill pattern to be used for filled figures.  This will only affect filled }
  41.           { figures.  Some predefined constants for use in this procedure are black, gray, }
  42.           { dkGray, ltGray, and white. }
  43.             procedure doDraw;
  44.             override;
  45.           { draws the figure; called by procedure xWindow.doRedraw }
  46.             procedure drawIt;
  47.           { called by doDraw to do the actual drawing }
  48.         end;
  49.  
  50.     xRectangle = object(xFigureDecoration)
  51.          { A rectangle, frame only; a narrow rectangle makes a horizontal or vertical line }
  52.             procedure drawIt;
  53.             override;
  54.         end;
  55.  
  56.     xRoundRect = object(xFigureDecoration)
  57.          { A rectangle with rounded corners; frame only }
  58.             procedure drawIt;
  59.             override;
  60.         end;
  61.  
  62.     xOval = object(xFigureDecoration)
  63.          { An oval that just fits into the enclosing rectangle of the figure; frame only }
  64.             procedure drawIt;
  65.             override;
  66.         end;
  67.  
  68.     xFilledRectangle = object(xFigureDecoration)
  69.           { A rectangle filled with a pattern (solid black by default) }
  70.             procedure drawIt;
  71.             override;
  72.         end;
  73.  
  74.     xFilledRoundRect = object(xFigureDecoration)
  75.           { A filled rectangle with rounded cornors }
  76.             procedure drawIt;
  77.             override;
  78.         end;
  79.  
  80.     xFilledOval = object(xFigureDecoration)
  81.          { a filled oval that just fits into its enclosing rectangle }
  82.             procedure drawIt;
  83.             override;
  84.         end;
  85.  
  86.  
  87.     xDisplayString = object(xWindowDecoration)
  88.           { Displays one or more lines of text, is a typeface, font, and size that can }
  89.           { be changed any time you want. }
  90.             theText: string;   { The displayed text }
  91.             fontNum, fontSize: integer;   { current font and font size }
  92.             fontFace: style;   { text style }
  93.             trueWidth, trueHeight: integer;  { The actual size of the displayed text }
  94.             procedure Setup (win: xWindow;
  95.                                         str: string;
  96.                                         theLeft, theTop: integer);
  97.            { Set up the text to be drawn and the place where it is to draw.  win is }
  98.            { the xWindow where the string is to be drawn.  Str specifies the string }
  99.            { to be drawn.  The character "\" is interpreted as a line feed in the text, }
  100.            { allowing you to have multiple line text displays.  (However, there is no }
  101.            { way to actually print out a \.  }
  102.            {     theLeft and theTop determine the position of the upper left corner of the }
  103.            { string.  Note the special interpretation of negative values, as discussed under }
  104.            { the Install procedure of class xWindowDecoration. }
  105.             procedure ChangeString (newStr: string);
  106.            { Change to a different displayed string; forces a redraw. }
  107.             procedure ChangeFont (newFont, newSize: integer);
  108.            { Change the font and/or size, and forces a redraw.  If the newFont is < 0 then }
  109.            { the current font is not changed. If the newSize is <= 0, then the size is not }
  110.            { changed. }
  111.             procedure ChangeStyle (newFace: style);
  112.            { Change the text style (bold, italic, etc), and forces a redraw.  The type }
  113.            { "style" is predefined to be set of (bold,italic,underline,outline,shadow, }
  114.            { condense,extend]).}
  115.             procedure adjustSize;   { not for user; react to change in size of window }
  116.             override;
  117.             procedure doDraw;   { not for user use; draws the string whenever necessary }
  118.             override;
  119.         end;
  120.  
  121.     xIcon = object(xWindowDecoration)
  122.           { A class for displaying an icon; the icon is loaded from an existing icon }
  123.           { resource. }
  124.             iconHandle: handle;
  125.             procedure SetUp (xWin: xWindow;
  126.                                         iconResourceNum: integer;
  127.                                         theLeft, theTop: integer);
  128.           { Loads an icon resourse, and install it at the specified point in the window. }
  129.           { NOTE: if you resize this decoration, the icon will be drawn in larger or }
  130.           { smaller size, as appropriate (and will probably look bad). }
  131.           {    The iconResourseNum specifes the resource of an existing icon resource. }
  132.           { If the resource is not found, then the icon is simply not installed in the }
  133.           { window. }
  134.           {     theLeft and theTop determine the position of the upper left corner of the }
  135.           { icon.  Note the special interpretation of negative values, as discussed under }
  136.           { the Install procedure of class xWindowDecoration. }
  137.             procedure doDraw;  { not for user; draw the icon. }
  138.             override;
  139.         end;
  140.  
  141.     xPict = object(xWindowDecoration)
  142.           { A class for displaying a PICT graphic.  The two SetUp procedures allow you }
  143.           { to load a PICT resource from the resource file, or to use a PICT that already }
  144.           { exists. }
  145.           {     Note: If you resize this decoration, the PICT will be scaled to fit the new }
  146.           { drawing rectangle. }
  147.             pic: picHandle;
  148.             procedure SetUp (xWin: xWindow;
  149.                                         thePict: PicHandle;
  150.                                         theLeft, theTop: integer);
  151.           { Installs a PICT to be displayed at the specified location in the given xWindow. }
  152.           {     theLeft and theTop determine the position of the upper left corner of the }
  153.           { pict.  Note the special interpretation of negative values, as discussed under }
  154.           { the Install procedure of class xWindowDecoration. }
  155.             procedure SetUpWithResource (xWin: xWindow;
  156.                                         pictResourceNum: integer;
  157.                                         theLeft, theTop: integer);
  158.           { Like SetUp, but loads a PICT resource to be displayed. }
  159.             procedure doDraw;  { not for user; actually draws PICTS }
  160.             override;
  161.         end;
  162.  
  163. implementation
  164.  
  165. procedure xFigureDecoration.setUp (win: xWindow;
  166.                                 theLeft, theTop, theWidth, theHeight: integer);
  167.     begin
  168.         inherited init;
  169.         nib.h := 1;
  170.         nib.v := 1;
  171.         paintPattern := black;
  172.         install(win, theLeft, theTop, theWidth, theHeight);
  173.     end;
  174.  
  175. procedure xFigureDecoration.SetNib (dh, dv: integer);
  176.     begin
  177.         nib.h := dh;
  178.         nib.v := dv;
  179.     end;
  180.  
  181. procedure xFigureDecoration.SetPattern (thePattern: pattern);
  182.     begin
  183.         paintPattern := thePattern;
  184.     end;
  185.  
  186. procedure xFigureDecoration.doDraw;
  187.     var
  188.         x1, y1, x2, y2: integer;
  189.         saveNib: point;
  190.         savePat: pattern;
  191.     begin
  192.         saveNib := itsWindow.theWindow^.pnSize;
  193.         PenSize(nib.h, nib.v);
  194.         savePat := itsWindow.theWindow^.pnPat;
  195.         PenPat(paintPattern);
  196.         drawIt;
  197.         PenSize(saveNib.h, saveNib.v);
  198.         PenPat(savePat);
  199.     end;
  200.  
  201. procedure xFigureDecoration.drawIt;
  202.     begin
  203.     end;
  204.  
  205. procedure xRectangle.drawIt;
  206.     begin
  207.         FrameRect(drawRect);
  208.     end;
  209.  
  210. procedure xRoundRect.drawIt;
  211.     begin
  212.         FrameRoundRect(drawRect, 16, 16);
  213.     end;
  214.  
  215. procedure xOval.drawIt;
  216.     begin
  217.         FrameOval(drawRect);
  218.     end;
  219.  
  220. procedure xFilledRectangle.drawIt;
  221.     begin
  222.         PaintRect(drawRect);
  223.     end;
  224.  
  225. procedure xFilledRoundRect.drawIt;
  226.     begin
  227.         PaintRoundRect(drawRect, 16, 16);
  228.     end;
  229.  
  230. procedure xFilledOval.drawIt;
  231.     begin
  232.         PaintOval(drawRect);
  233.     end;
  234.  
  235. procedure DoString (d: xDisplayString;
  236.                                 var width, height: integer;
  237.                                 drawIt: boolean);
  238.     var
  239.         savePort: GrafPTr;
  240.         saveFont: integer;
  241.         saveSize: integer;
  242.         saveFace: style;
  243.         win: windowPtr;
  244.         i, ct: integer;
  245.         info: fontInfo;
  246.         lineHeight: integer;
  247.         str: string;
  248.         w: integer;
  249.     begin
  250.         if (d.itsWindow = nil) | (d.itsWindow.theWindow = nil) then
  251.             EXIT(DoString);
  252.         if length(d.theText) = 0 then begin
  253.                 width := 0;
  254.                 height := 0;
  255.                 EXIT(DoString);
  256.             end;
  257.         GetPort(savePort);
  258.         win := d.itsWindow.theWindow;
  259.         SetPort(win);
  260.         saveFont := win^.txFont;
  261.         saveSize := win^.txSize;
  262.         saveFace := win^.txFace;
  263.         TextFont(d.fontNum);
  264.         TextSize(d.fontSize);
  265.         TextFace(d.fontFace);
  266.         ct := 0;
  267.         i := 1;
  268.         width := 0;
  269.         str := '';
  270.         GetFontInfo(info);
  271.         lineHeight := (info.ascent + info.descent + info.leading);
  272.         while i <= length(d.theText) do begin
  273.                 if d.theText[i] = '\' then begin
  274.                         w := StringWidth(str);
  275.                         if w > width then
  276.                             width := w;
  277.                         if drawIt then begin
  278.                                 MoveTo(d.drawRect.left + 1, d.drawRect.top + info.ascent + ct * lineHeight);
  279.                                 DrawString(str);
  280.                             end;
  281.                         ct := ct + 1;
  282.                         str := '';
  283.                     end
  284.                 else
  285.                     str := Concat(str, d.theText[i]);
  286.                 i := i + 1;
  287.             end;
  288.         if (str <> '') then begin
  289.                 w := StringWidth(str);
  290.                 if w > width then
  291.                     width := w;
  292.                 if drawIt then begin
  293.                         MoveTo(d.drawRect.left + 1, d.drawRect.top + info.ascent + ct * lineHeight);
  294.                         DrawString(str);
  295.                     end;
  296.                 ct := ct + 1;
  297.             end;
  298.         height := ct * lineHeight;
  299.         width := width + 1;
  300.         TextFace(saveFace);
  301.         TextFont(saveFont);
  302.         TextSize(saveSize);
  303.         SetPort(savePort);
  304.     end;
  305.  
  306. procedure xDisplayString.Setup (win: xWindow;
  307.                                 str: string;
  308.                                 theLeft, theTop: integer);
  309.     begin
  310.         if (win.theWindow = nil) then
  311.             EXIT(setup);
  312.         theText := str;
  313.         fontNum := systemFont;
  314.         fontSize := 12;
  315.         fontFace := [];
  316.         init;
  317.         Install(win, theLeft, theTop, 1, 1);
  318.         DoString(self, trueWidth, trueHeight, false);
  319.         SetSize(trueWidth, trueHeight);
  320.     end;
  321.  
  322. procedure xDisplayString.ChangeString (newStr: string);
  323.     begin
  324.         theText := newStr;
  325.         DoString(self, trueWidth, trueHeight, false);
  326.         if trueWidth = width then
  327.             width := width - 1;
  328.         SetSize(trueWidth, trueHeight);
  329.     end;
  330.  
  331. procedure xDisplayString.ChangeFont (newFont, newSize: integer);
  332.     begin
  333.         if newFont >= 0 then
  334.             fontNum := newFont;
  335.         if newSize > 0 then
  336.             fontSize := newSize;
  337.         DoString(self, trueWidth, trueHeight, false);
  338.         if trueWidth = width then
  339.             width := width - 1;
  340.         SetSize(trueWidth, trueHeight);
  341.     end;
  342.  
  343. procedure xDisplayString.ChangeStyle (newFace: style);
  344.     begin
  345.         fontFace := newFace;
  346.         DoString(self, trueWidth, trueHeight, false);
  347.         if trueWidth = width then
  348.             width := width - 1;
  349.         SetSize(trueWidth, trueHeight);
  350.     end;
  351.  
  352. procedure xDisplayString.adjustSize;
  353.     begin
  354.         inherited adjustSize;
  355.         clickRect.right := clickRect.left + trueWidth;
  356.         clickRect.bottom := clickRect.top + trueHeight;
  357.         drawRect := clickRect;
  358.     end;
  359.  
  360. procedure xDisplayString.doDraw;
  361.     var
  362.         junk1, junk2: integer;
  363.     begin
  364.         DoString(self, junk1, junk2, true);
  365.     end;
  366.  
  367. procedure xIcon.SetUp (xWin: xWindow;
  368.                                 iconResourceNum: integer;
  369.                                 theLeft, theTop: integer);
  370.     begin
  371.         iconHandle := GetIcon(iconResourceNum);
  372.         if iconHandle <> nil then begin
  373.                 init;
  374.                 install(xWin, theLeft, theTop, 32, 32);
  375.             end;
  376.     end;
  377.  
  378. procedure xIcon.doDraw;
  379.     begin
  380.         if iconHandle <> nil then
  381.             PlotIcon(drawRect, iconHandle);
  382.     end;
  383.  
  384. procedure xPict.SetUp (xWin: xWindow;
  385.                                 thePict: PicHandle;
  386.                                 theLeft, theTop: integer);
  387.     var
  388.         w, h: integer;
  389.     begin
  390.         pic := thePict;
  391.         if pic <> nil then begin
  392.                 init;
  393.                 w := pic^^.picFrame.right - pic^^.picFrame.left;
  394.                 h := pic^^.picFrame.bottom - pic^^.picFrame.top;
  395.                 install(xWin, theLeft, theTop, w, h);
  396.             end;
  397.     end;
  398.  
  399. procedure xPict.SetUpWithResource (xWin: xWindow;
  400.                                 pictResourceNum: integer;
  401.                                 theLeft, theTop: integer);
  402.     var
  403.         w, h: integer;
  404.     begin
  405.         pic := GetPicture(pictResourceNum);
  406.         if pic <> nil then begin
  407.                 init;
  408.                 w := pic^^.picFrame.right - pic^^.picFrame.left;
  409.                 h := pic^^.picFrame.bottom - pic^^.picFrame.top;
  410.                 install(xWin, theLeft, theTop, w, h);
  411.             end;
  412.     end;
  413.  
  414. procedure xPict.doDraw;
  415.     begin
  416.         if pic <> nil then
  417.             DrawPicture(pic, drawRect);
  418.     end;
  419.  
  420. end.