home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / mac / programm / 20857 < prev    next >
Encoding:
Internet Message Format  |  1993-01-07  |  4.6 KB

  1. Path: sparky!uunet!olivea!charnel!sifon!CC.UMontreal.CA!casgrain
  2. From: casgrain@ERE.UMontreal.CA (Casgrain Philippe)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Default buttons in dialogs (R)
  5. Keywords: Default button, FilterProc
  6. Message-ID: <1993Jan7.053958.18390@cc.umontreal.ca>
  7. Date: 7 Jan 93 05:39:58 GMT
  8. Sender: news@cc.umontreal.ca (Administration de Cnews)
  9. Organization: Universite de Montreal
  10. Lines: 130
  11. Rganization: Universite de Montreal
  12.  
  13. In article <880840m.29.726366713@axe.acadiau.ca> 880840m@axe.acadiau.ca (MICHAEL
  14.  ALEXANDER MCKAY) writes:
  15. >
  16. >Whenever I make a dialog box with ResEdit, my "OK" buttons, which are always
  17. >the number 1 button, fail to show up in my app with the heavy outline around
  18. >them. What am I doing wrong?
  19. >
  20. >
  21. The #1 button is drawn with a heavy outline only in alert boxes.  If you want
  22. to implement that behaviour in a dialog box, you have two options:
  23.  
  24. 1) You can have an additional item in your dialog box, a 'PICT' you drew in
  25. MacDraw or something, which looks like a heavy outline.  Note that its
  26. number must be _lower_ than the button you want to be outlined, since the
  27. Dialog Manager draws overimposed items with the latest on top.  So the 'default'
  28. button won't be #1.
  29.         So that's easy to do, but not a very nice programming practice.
  30.  
  31. 2) You can design your own FilterProc to use when calling ModalDialog (instead
  32. of nil).  I personally favor this approach, as knowledge of how a FilterProc
  33. works is quite valuable indeed.  Here's how it's done:
  34.  
  35. { I presume you have a valid DialogPtr in myDlg }
  36. ...
  37. { Somewhere in your program, you call ModalDialog: }
  38.     ModalDialog(myDlg, @MyFilterProc);
  39. ...
  40. { MyFilterProc is a top-level (i.e. not internal to another procedure or
  41. function, but it's fine in a unit) function which has the following structure:}
  42.  
  43.  function MyFilterProc (dlg: DialogPtr; 
  44.              var event: EventRecord; 
  45.             var itemHit: Integer): Boolean;
  46. { We must implement the standard ModalDialog "features": the default
  47. (#1) button gets activated when the user presses Enter or Return, and
  48. the #2 when he/she presses Escape or, as a nice touch, Command-Period}
  49.  
  50.   const
  51.    kPeriod = 46;
  52.    kEnter = 3;
  53.    kReturn = 13;
  54.    kEscape = 27;
  55. { Note that I also filter for when the cursor enters item # 3, an EditText
  56. item, it gets changed into an I-Beam cursor }
  57.    kEditTextItem = 3;
  58.   var
  59.    iKind, radius: Integer;
  60.    iHandle: Handle;
  61.    iRect: Rect;
  62.    key: Char;
  63.    dumBool: Boolean;
  64.    mouseLocation: Point;
  65.    theWindow: WindowPtr;
  66.    part: Integer;
  67.  begin
  68.   dumBool := False; { By default, we don't handle the event, thus leaving
  69.             it to ModalDialog.  We could process the event and
  70.             delete it, or leave it for further processing by
  71.             ModalDialog}
  72.  
  73.   case event.what of
  74.    nullEvent: 
  75. { This code handles the cursor-changing behaviour.  Extend as needed! }
  76.     begin
  77.      GetDItem(dlg, iValueItem, iKind, iHandle, iRect);
  78.      GetMouse(mouseLocation);
  79.      if (PtinRect(mouseLocation, iRect)) then
  80.       SetCursor(GetCursor(IBeamCursor)^^)
  81.      else
  82.       SetCursor(arrow);
  83.     end;{nullEvent}
  84.  
  85.    keyDown, autoKey:
  86. { This filters for key presses in the dialog box } 
  87.     begin
  88.      key := Char(BAnd(event.message, charCodeMask));
  89.      if (BAnd(event.modifiers, cmdKey) <> 0) then
  90. { a command-key combination was created and sent }
  91.       begin
  92.        if Ord(key) = kPeriod then
  93.        begin
  94.        itemHit := Cancel;
  95. { FlashDialogItem is a generic procedure that hilites (sets its CtlValue to 1)
  96. a control for exactly 1/6th of a second :-) }
  97.        FlashDialogItem(dlg, itemHit);
  98.        dumBool := True;
  99.        end;{if key}
  100.       end{if BAnd(event.modifiers, cmdKey)}
  101.      else
  102.       begin
  103.        if (Ord(key) = kReturn) or (Ord(key) = kEnter) then
  104.        begin
  105.        itemHit := OK;
  106.        FlashDialogItem(dlg, itemHit);
  107.        dumBool := True;
  108.        end;{if}
  109.        if (Ord(key) = kEscape) then
  110.        begin
  111.        itemHit := Cancel;
  112.        FlashDialogItem(dlg, itemHit);
  113.        dumBool := True;
  114.        end;{if}
  115.       end;{else}
  116.     end;{keyDown, autoKey}
  117.  
  118.    updateEvt:
  119. { Here's the part you've been waiting for! }
  120. { Thanks to Keith Rollin and Scott Knaster for their book, "Macintosh
  121. Programming Secrets" for all the pointers I found there! } 
  122.     begin
  123.      SetPort(dlg);
  124.      GetDItem(dlg, OK, iKind, iHandle, iRect);
  125.      InsetRect(iRect, -4, -4);
  126.      radius := (iRect.bottom - iRect.top) div 2;
  127.      if (radius > 16) then
  128.       radius := 16;
  129.      PenNormal;
  130.      PenSize(3, 3);
  131.      FrameRoundRect(iRect, radius, radius);
  132.  
  133.      dumBool := False;
  134.     end;{updateEvt}
  135.  
  136.   end;{case}
  137.   MyFilterProc := dumBool;
  138.  end;{function MyFilterProc}
  139.  
  140.  
  141. Share and enjoy!
  142. Philippe        Casgrain@ere.umontreal.ca 
  143.