home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / macintosh / programming-faq / text0000.txt < prev   
Encoding:
Text File  |  1995-07-05  |  89.3 KB  |  2,187 lines

  1. Archive-name: macintosh/programming-faq
  2.  
  3. Mac Programming Frequently Asked Questions Answer Sheet
  4. Last update: 10 May 95 - sharp dressed FAQ
  5.  
  6. Please download a copy of this answer sheet and search it before you 
  7. post to the 'net, to help reduce bandwidth.
  8.  
  9. Please send all correspondence regarding content directly to the current 
  10. caretaker and content editor, Chris Thomas, <thunderone@delphi.com>.  
  11. All submissions sent will be considered to be in the public domain 
  12. unless stated otherwise (in which case they will not be included in this 
  13. FAQ sheet).  When writing, be sure that you include something 
  14. appropriate in the subject line.  I'm now automatically deleting mail 
  15. with blank or whitespace subjects unread due to the growing nasty 
  16. practice of sending junk email without a subject.  The idea, apparently, 
  17. is to be cute and get you to read the mail without deleting it.  This 
  18. sheet was started and is distributed by Jon Watte, whom you may reach as 
  19. <h+@austin.metrowerks.com>.
  20.  
  21. This sheet is currently archived on nada.kth.se where you can reach it 
  22. using afs as /afs/nada.kth.se/public/ftp/pub/hacks/mac-faq/CSMP_PD_FAQ 
  23. or using anonymous FTP as pub/hacks/mac-faq/CSMP_PD_FAQ You can also 
  24. find it on rtfm.mit.edu unedr the name macintosh/programming-faq.  WWW 
  25. version is available as <http://www.nada.kth.se/~d88-jwa/mac-faq.html>.
  26.  
  27. No FAQ can substitute for real documentation (some of which is 
  28. mentioned in this FAQ) If you ask a question in comp.sys.mac.programmer 
  29. which has a good answer in one of the important sources, you will probably 
  30. not get an answer.  (Inside Macintosh, Macintosh Technical Notes being 
  31. important sources).
  32.  
  33. There is NO or VERY LIMITED error checking in the code examples, FOR 
  34. BREVITY ONLY.  You should make sure you ALWAYS check ALL return codes, 
  35. and handle any that you are not prepared to deal with appropriately.  
  36. Needless to say, do not use the code as is.
  37.  
  38. Exciting new stuff:  More general dev tools update.  See especially 
  39. question 1.2.  List Manager replacements.  The usual tweaking.
  40.  
  41. Topics:
  42. [search for *number* to find a topic quickly]
  43. [topics changed since last FAQ are marked with "+", new topics with ">"]
  44.  
  45.  1. +Development Tools
  46.      getting started, tool-specific issues
  47.  2. Memory
  48.      handles, large arrays, resource handles
  49.  3. User Interfacing
  50.      menus, windows, events, multitasking
  51.  4. Files
  52.      Mac fopen, wdrefnums, getting full pathnames
  53.  5. Imaging
  54.      QuickDraw and the means to avoid it
  55.  6. Text
  56.      Text editing packages, string conversion
  57.  7. Communications and Networking
  58.      Serial ports, TCP/IP, sockets
  59.  8. Interapplication Communication
  60.      AppleEvents, OSA, Scripting, and You
  61.  9. Dynamic Linking & Code Resources
  62.      the dynamics of code resources & trap patches
  63. 10. Compatibility
  64.      gestalt & glue
  65. 11. Optional System Software
  66.    11.1. QuickTime
  67.        codec details and the lack thereof
  68. 12. Third-Party Solutions
  69.    12.1. Databases
  70.        client/server solutions for the masses
  71.    12.2. >Circumventing Toolbox Limitations
  72.        foul baggage begone- List Manager replacements
  73. 13. Dessert
  74.      yummies the Macintosh Way
  75. 14. Contributors
  76.      whodunnit
  77.  
  78. *1* Development and debugging tools for the Macintosh 
  79.  
  80. 1.1) Q: What do I need to start writing Macintosh software? 
  81.  
  82. A: A Mac, a lot of time, and a few hundred $.  Although you can develop 
  83. software on a Classic-type machine, it is not to be attempted by the 
  84. weak of heart or stressed of time.  If you're doing paid work and/or 
  85. work for a company, a Quadra-class machine is a must; remember that your 
  86. time costs your employer much more than just your salary.  A PowerMac is 
  87. highly preferable.  16 MB is a minimum to run at all comfortably (40 MB 
  88. recommended), and Virtual Memory (including RamDoubler, unfortunately) 
  89. is not suited for development work.  Similarly, if you don't have at 
  90. least 80 MB free on your hard disk you need to buy more space.  You will 
  91. also need a CD-ROM drive.
  92.  
  93. You need a development system such as CodeWarrior, MPW Pro, Symantec C++ 
  94. 8.0 or Prograph, you need at least some of the New Inside Mac books 
  95. (Toolbox Essentials, Files, Memory come to mind) and a good entry-level 
  96. third-party book may help.
  97.  
  98. Once you are up to speed on the general layout of the Mac and its 
  99. toolboxes, you should call APDA and order the monthly developer mailing, 
  100. which will give you a CD chock full of documentation, utilities and 
  101. system software once a month.  You will also, obviously, need a CD 
  102. player; Apple's own CD600 is a very good buy at the time of this 
  103. writing.  If you don't have the dough for the monthly mailing 
  104. ($250/year) you can order a _develop_ subscription; this quarterly 
  105. magazine ($30-$50/year) comes with a CD containing most Inside Mac 
  106. documentation.  Another good product to order is the MacOS SDK, which 
  107. for $99 gives you a CD with every API in existence up to and including 
  108. the 7.5 Mac Toolbox additions.  It's somewhat redundant if you already 
  109. have the Developer CD subscription.  <apda@appplelink.apple.com> Apple's 
  110. Developer Web has almost all of the contents of the Developer CD online.  
  111. <http://www.info.apple.com/dev>
  112.  
  113. If you don't know how to program, go learn your language of choice 
  114. BEFORE attempting a "real" Mac application.  Programming is a discipline 
  115. often requiring different thought processes than your normal day job.  A 
  116. beginning book, like Lippman: The C++ Primer, one of the Teach Yourself 
  117. C++ books, or the primers available on the CodeWarrior CD, might help.
  118.  
  119. An indispensable Mac programming tool is the Macintosh Programmer's 
  120. Toolbox Reference (MPTA), an up-to-date hypertext reference guide 
  121. containing reference material on the New Inside Mac-documented portions 
  122. of the Toolbox with lightning-fast look-up and mostly correct usage 
  123. hints and code snippets.  MPTA can be found on the Developer CD, and is 
  124. also offered on a seperate $99 CD.  <apda@applelink.apple.com>
  125.  
  126. Think Reference version 2.0.1, precursor to MPTA, contains reference 
  127. material on many parts of the Mac toolbox with lightning-fast look-up 
  128. and mostly correct usage hints and code snippets.  While it does not 
  129. cover any post-System 7 system additions, nor the modern "universal" 
  130. headers format, it does include information on the standard C/C++ 
  131. libraries.  Available wherever fine Symantec products are sold - 
  132. <apda@applelink.apple.com> for one.
  133.  
  134. 1.2) Q: What is the most used Macintosh development system? 
  135.  
  136. A: Currently, the three most widely used are CodeWarrior (CW), MPW, and 
  137. Symantec C++, probably in that order, though I don't have any 
  138. statistics.  The latest version of any one of these is adequate for Mac 
  139. development.
  140.  
  141. CodeWarrior: In early 1994, CodeWarrior came out of nowhere and grabbed 
  142. a large share of the market visibility because they had the fastest 
  143. compiler and they generated PowerPC code as well as 68K code.  Today, 
  144. CodeWarrior has the smoothest development environment and most complete 
  145. C++ implementation, supporting both templates and exceptions.  CW/6 
  146. includes C, C++, and Object Pascal support, and can generate x86 
  147. binaries.  Among hobbyists, CW seems to be the most popular because of 
  148. it's low price, ultra-fast compile time, and support that no other 
  149. company on the planet can match.  See comp.sys.mac.programmer.codewarrior
  150. for more information and CodeWarrior-related praise.  Contact 
  151. <sales@metrowerks.com>.
  152.  
  153. MPW: The grandaddy of all Mac development environments, descended from 
  154. the original Mac development environment based on a Lisa.  MPW is an 
  155. extremely flexible, powerful, Unix-like command line environment with 
  156. makefile, multiple windows and split-pane support.  Many development 
  157. tools are MPW-based.  MPW Pro comes with C, C++, Pascal, assemblers for 
  158. both 68k and PowerMac, various other useful tools, and the C++-based 
  159. framework MacApp for a reasonable price.  MPW has in the past been 
  160. extremely slow, but shows signs of redemption.  Contact 
  161. <apda@applelink.apple.com>.
  162.  
  163. Symantec C++: This is the eighth-generation descendant of the C
  164. environment favored by Mac developers for over five years.  Symantec C++ 
  165. 8.0 is a complete, scriptable, modular development environment including 
  166. C, C++, and soon, a PowerPC assembler.  The C++ implementation supports 
  167. templates.  A development version of a PowerPC C++ compiler supporting 
  168. exceptions and RTTI is available at Symantec's devtools site.  SC++ 8.0 
  169. doesn't at this time support 68k Mac development.  For that purpose, 
  170. you're required to use the old, decrepit TPM development environment, 
  171. which is included.  Contact <sales@devtools.symantec.com>.
  172.  
  173. There are also at least two Fortran compilers, at least three SmallTalk 
  174. implementations (ObjectWorks, SmallTalk/V and SmallTalkAgents) and 
  175. others.  There are ways of stripping SmallTalk apps so they're smaller 
  176. and faster as standalone apps than in the environment.
  177.  
  178. Languge Systems has Object Pascal and Fortran for PowerMac.  Absoft has 
  179. Fortran and C++ for PowerMac.  These all require MPW.
  180.  
  181. There's also a world-class LISP/CLOS implementation from Apple called 
  182. Macintosh Common Lisp.  Recently, Apple announced that DigiTool has 
  183. licensed MCL with the intent (among other things) to provide a PowerMac 
  184. version and other updates.
  185.  
  186. Zedcor has FutureBasic, which seems to be a very popular...  It also 
  187. seems to be the only well-supported implementation of BASIC on 
  188. Macintosh.
  189.  
  190. CSI has MacForth, of which I only know the name and someone who says 
  191. it's pretty good.
  192.  
  193. There is another good Common Lisp implementation: Procyon Common Lisp.  
  194. I don't know if it is actively supported, but Procyon CL is also 
  195. available for DOS, OS/2 and Windows (as Allegro CL/PC) and actively 
  196. developed.
  197.  
  198. A new possible up-and-coming languge is Apple's Dylan, which is 
  199. something of a cross between BASIC, Pascal, and C.  C-based code can be 
  200. used directly from Dylan, but Dylan can't yet be used directly from C.  
  201. The Apple Dylan environment is rumored to be as far beyond MPW as MPW is 
  202. beyond thick bundles of FORTRAN punch cards.
  203.  
  204. 1.3) Q: Okay, which is the most used Mac programming languge?
  205.  
  206. A: The existing Macintosh code base is mostly C, with C++ second, and 
  207. Pascal finding it's niche in third.  Few people are writing mainstream 
  208. software in Pascal anymore, probably because (a) it's rather hard to 
  209. move to non-Mac platforms (b) Pascal is only rarely being taught past 
  210. the first year in Computer Science.
  211.  
  212. 1.4) Q: Where do I find a free/share/copyleftware C compiler for the Mac? 
  213. Is there a GCC for the mac? What about the FSF boycott of Apple products?
  214.  
  215. A: There is no really good solution for a "for-free" C development 
  216. system for the Mac.  GCC has been ported, but requires the MPW shell and 
  217. MPW assembler to run; these have to be bought from APDA.  There is a 
  218. standalone port of GCC 1.37 on nic.switch.ch:software/mac/src/think_c.  
  219.  
  220. gcc-1.37r14 V1.1 standalone is available for ftp at nic.switch.ch: 
  221. software/mac/src/think_c.
  222.  
  223. A not-entirely-stable port of GCC 2.3.3 to MPW is available for ftp at 
  224. atg.apple.com [anyone know the directory?].  A much more solid port of 
  225. GCC 1.37 is available for MPW as well.
  226.  
  227. Stan Shebs <shebs@cygnus.com>, the driving force behind all of the MPW 
  228. GCC ports, is working on a new port of GCC 2.5.8.
  229.  
  230. For those whose main interest is in developing only text-based C/C++ 
  231. programs, using GCC under MacMiNT might be appropriate.  MacMiNT is a 
  232. UNIX like operating system ported from the Atari ST which supports many 
  233. freely available UNIX utilities like GCC, GDB, make, tcsh, byacc, perl, 
  234. and more.  MacMiNT stuff can be found at nic.switch.ch in 
  235. 'software/mac/src/macmint'.
  236.  
  237. The FSF/LPF boycott of Apple products is over as of January 1995, which 
  238. means they will now incorporate changes made for Macintosh into their 
  239. main code base, if such changes are easily incorporated, and they 
  240. won't be any more antagonistic to Mac programmers than they would be 
  241. to any other micro-to-workstation-class programmers.
  242.  
  243. So what are you waiting for? Go out and port something from GNU! Send in 
  244. the changes!  We still lack decent free development tools!
  245.  
  246. 1.5) Q: Are there any other free Mac development platforms? 
  247.  
  248. A: The best source for information on free compilers/interpreters is the 
  249. Free Compilers FAQ which is written by Brian Connors 
  250. <connorbd@cleo.bc.edu>.  Watch for it in c.s.m.p.info.
  251.  
  252. 1.6) Q: What's the difference between the MPW, Think and CodeWarrior 
  253. environments?
  254.  
  255. A: As of CodeWarrior/6, MPW 3.3, and Symantec C++ 8.0.1 (SC++), 
  256. CodeWarrior will allow faster turnaround times, MPW will provide the 
  257. most flexibility and overall power, and Symantec C++ has the edge with 
  258. regard to helpful project browsing features.
  259.  
  260. CW C++ supports templates and exceptions, Symantec C++ supports only 
  261. templates, MPW's new compilers are based on Symantec C++.
  262.  
  263. SC++ 8 doesn't generate 68k code, while pre-CW/6 CodeWarrior requires 
  264. you to use seperate (virtually identical) environments, and builds are 
  265. controlled from a Makefile in MPW, of course.
  266.  
  267. All three need much hard disk space.  SC++ requires the most RAM.  MPW 
  268. requires the most disk space.
  269.  
  270. MPW is the slowest, followed distantly by SC++, which is followed 
  271. closely by CodeWarrior.
  272.  
  273. The best thing about MPW is that you can write scripts and make files to 
  274. do anything you want in the way you want it.  SC++ and CodeWarrior can 
  275. be AppleScripted to do builds that require more than one link operation, 
  276. but the process is more involved, and CW doesn't currently support 
  277. scripting in full.  SC++ 8 can do builds which require more than one 
  278. link operation.
  279.  
  280. For the MPW and CodeWarrior environments, there are four source level 
  281. debuggers; Metrowerks, SADE, SourceBug, Voodoo Monkey.  The latter is an 
  282. experimental debugger with support for threads debugging; the middle is 
  283. bundled with MPW while SADE has to be bought separately (but is fully 
  284. scriptable in its own scripting language).  Metrowerks Debugger is 
  285. included with CodeWarrior.
  286.  
  287. The Think environments have their own integrated debuggers; the Think 
  288. Pascal one has a lot of useful features while the Think C/C++ one is a 
  289. little more basic (but is gaining in functionality with each release).
  290.  
  291. Metrowerks has their own debugger which works like the MPW debuggers; i e 
  292. it runs the application standalone and pokes at it from the outside, 
  293. while the Think debuggers run the application "wrapped" in a special 
  294. environment, making for some subtle interferences with your heap (which 
  295. you usually don't notice).  The Metrowerks Debugger is Thread 
  296. Manager-savvy on the 68k side.
  297.  
  298. BOTTOM LINE:
  299.  
  300. If you're developing for both Power and regular 68k Macs, you need 
  301. CodeWarrior.  MPW is an option which makes sense if you need to develop 
  302. code for non-CFM OpenTransport or if you have a ridiculously large 
  303. number of independent code modules to compile, or if you're a Unix 
  304. person.  CodeWarrior, in addition to it's own integrated environment, 
  305. includes the non-compiler parts of MPW and MPW-hosted Metrowerks 
  306. compilers/linkers.  SC++ is an option only for PowerMac development.
  307.  
  308. 1.7) Q: What is a good low-level debugger for the Mac? 
  309.  
  310. A: MacsBug is freely available for ftp from <ftp://ftp.apple.com>; log 
  311. in as user anonymous and give your FULL e-mail address as password.  
  312. MacsBug is your basic monitor-type debugger that takes a few hundred Ks 
  313. of memory, and lets you break, step, disassemble, look at the stack etc 
  314. of most anything running on your Mac.  Since it's free (it's also on the 
  315. developer CDs) and provides most of the functionality you need, this is 
  316. a popular choice.  As of 6.5d10, Macsbug supports PowerPC debugging.
  317.  
  318. Jasik Designs has a debugger called The Debugger which can do both low- 
  319. and high-level debugging, with or without source and for all types of 
  320. code, application, code resources, everything.  This is the debugger of 
  321. choice for many large developers because of its high power and many 
  322. features not found anywhere else.  However; newcomers beware! This is 
  323. the Lamborghini of debuggers; if you know how to drive it, it is the 
  324. fastest way from A to B; if you don't, you'll just end up in the ditch.  
  325. The Debugger is PowerMac native and supports PowerPC disassembly.  It 
  326. includes an excellent code coverage tool and MacNosy, a general 
  327. disassembler.  Support is direct from the author and generally great.
  328. <macnosy@jasik.com>
  329.  
  330. 1.8) Q: Are there any visual developments environments for the 
  331. Mac (comparable to Visual C++)?
  332.  
  333. A: There is no Visual C++ as such.  However, there is a C++ parser/editor 
  334. called ObjectMaster which provides good browsing and editing capabilities 
  335. if you already have a C++ compiler.  A demo is available on the CodeWarrior 
  336. CD.  Think C++ comes with a browser built-in, and you can edit 
  337. dialogs/windows using plain old ResEdit, even for your custom view types.
  338.  
  339. Symantec C++ 7.0 also bundles a view editor/code generator called Visual 
  340. Architect; it is fairly complete and has a good level of integration 
  341. into the Think Project Manager.
  342.  
  343. AppMaker is a GUI builder/code generator.  Granted, it's not as nice as 
  344. VC++, but it's quite a product in any case.
  345.  
  346. MarksMan version 3.0 has totally revised TCL templates, and now 
  347. generates well-thought-out TCL code.  It can also generate ANSI C code 
  348. etc.
  349.  
  350. Also, Neuron Data has their UI tool called Open Interface, which is 
  351. better than VC++ and creates code portable across 35 platforms.  
  352. Unfortunately it's $2500 per developer per platform.  There's also two 
  353. other cross-platform products called XVT and Galaxy, the former has 
  354. gotten flak on UseNet while the latter reportedly is the premier 
  355. cross-platform application builder framework; with everything from 
  356. styled text to network support.
  357.  
  358. There is a fully visual, dynamic, object oriented data-flow-driven
  359. programming language for the Mac called Prograph CPX.  It features a
  360. full-featured class library, a powerful, user-extensible GUI Builder, full
  361. access to the entire Mac toolbox, a database engine, high-level interfaces
  362. to SQL, Oracle, etc.  But the coolest thing about Prograph is its
  363. interpretative debugger, fully integrated with the visual code editor,
  364. which lets you write your code _while it's running_.  Execution
  365. automatically rolls back to where changes you make have relevance.  A
  366. PowerMac-native compiler and a Windows version are expected in '95.  A
  367. complete demo version is available from <sales@prograph.com>.  Cost is 
  368. $695 ($395 for students).
  369.  
  370. SmalltalkAgents comes with a GUI builder, which lets you draw your
  371. interface, and then outputs the code for you.
  372.  
  373. If you'd rather do Common Lisp, Macintosh Common Lisp offers a Common 
  374. Lisp Object System with support for most Mac interface items; you can 
  375. edit code while it is running and build stand-alone applications.
  376.  
  377. However, all of these tools generate rather larger binaries with larger 
  378. system demands than a program written in C.  On the other hand; C++ 
  379. programs require more memory and disk space than programs written in 
  380. assembly.  It's a trade-off, and I believe this type of tools is the 
  381. wave of the near future.
  382.  
  383. 1.9) Q: What class libraries are there for the Mac? 
  384.  
  385. A: Apart from the libraries mentioned above, there are three contenders: 
  386. MacApp, TCL, and PowerPlant.  "Bedrock" will never be released as a 
  387. product, although parts of it surface in TCL 2.0 and other parts will be 
  388. the base for the OpenDoc Parts Framework.
  389.  
  390. MacApp is a heavy-duty class library that has tons of features and a 
  391. steep learning curve; it runs under MPW with Pascal or C++, and also 
  392. under Think Pascal 4.0 A major application written in MacApp is 
  393. PhotoShop.
  394.  
  395. TCL stands for Think Class Library and comes with Think Pascal, C or 
  396. C++.  It is a smaller library that still fills most peoples needs; since 
  397. Think C implements a subset of C++ (the most important OO concepts such 
  398. as virtual functions and inheritance) and the TCL is carefully written 
  399. not to take advantage of any C++ features not in Think C, you can use it 
  400. with Think C.  A major application written in TCL is Lotus 1-2-3.  (TCL 
  401. 1.1.3) Starting with Symantec C++ 7.0, Think Class Library 2.0 using 
  402. templates and "real" C++ objects is shipping.
  403.  
  404. The C++ Standard Template Library (STL) compiles under Symantec C++ 
  405. and CodeWarrior, and versions are available for both.
  406.  
  407. PowerPlant is the Metrowerks CodeWarrior offering; it's written by the 
  408. guy who designed the Think Class Library, but it has a lot of 
  409. differences from the original TCL; for one, it's not a monolithic one 
  410. base class framework.  On the other hand, it has some catching up to do 
  411. before it reaches the level of MacApp.  It is gaining quite fast on TCL, 
  412. but isn't all there yet.  It has the most complete support for 
  413. AppleEvents & scripting & drag & drop other modern features.
  414.  
  415. 1.10) Q: How should I debug and test my software? 
  416.  
  417. A: Get ahold of, and install, the extensions DoubleTrouble, 
  418. DisposeResource and EvenBetterBusError.  They will catch 80% of any 
  419. memory related bugs you may have, including many bugs that follow NULL 
  420. handles or pointers.  (Jasik's Debugger (see above) obviates the need 
  421. for these.)
  422.  
  423. A low-level debugger is required, and while you install it, install the 
  424. "leaks" dcmd which will help you catch memory leaks in your application.  
  425. All of these tools are available from <ftp.apple.com>.
  426.  
  427. 1.11) Q: Are there any good Mac programming magazines?
  428.  
  429. A: One Mac programming magazine I know of is MacTech Magazine (formerly 
  430. MacTutor).  It covers a variety of Mac programming topics on various 
  431. levels.  Operating independently from Apple, it has a lot of stuff for 
  432. the beginning Mac programmer, as well as occasional nuggets for the more 
  433. experienced of us.  <custservice@xplain.com>
  434.  
  435. Another VERY GOOD Magazine is _develop_ which is put out by Apple four 
  436. times a year; it comes with a CD containing code for all articles ever 
  437. published in _develop_, and a lot of documentation and system software 
  438. freebies as well.  $30/year in the US.  <dev.subs@applelink.apple.com>
  439.  
  440. 1.12) Q: What about protected memory? I'm sick and tired of re-booting 
  441. when my application crashes.
  442.  
  443. A: Write better software! 
  444.  
  445. Or install The Debugger from Jasik Designs, which can provide your 
  446. application with write-protection of critical parts of memory, if you have 
  447. a 68030-equipped Mac.
  448.  
  449. Making the Mac OS memory-protected is tricky, because applications expect 
  450. to be able to write to low memory, the system heap, temporary memory, 
  451. window lists, and even each other's heaps in some interapplication 
  452. communication solutions that date back to before AppleEvents and the PPC 
  453. Toolbox.  To add to the burden, Apple's own software tends to be the 
  454. worst offender in these cases.
  455.  
  456. But fear not, Mac fans! Jonathan Kimmitt has written Patmos, the 
  457. "Protected address translation mode operating system".  It is an 
  458. application that brings the advantages of protected mode programs to 
  459. your Quadra class Macintosh by the simple expedient of taking over the 
  460. memory management unit of the 68040 in a very simple kernel (<100K in 
  461. size), we immediately gain compatibility with the BSD unix program 
  462. environment.  The advantages of this are as follows:
  463.  
  464. (a) You can run certain programs (such as /bin/sh) designed for MacBSD
  465. (b) You can compile almost all GNU software including C and C++ without.
  466. modifying the source code in any way
  467. (c) All programs run with a flat 32-meg address space, with no worries
  468. about 32K segments or the other mac paraphernalia.
  469. (d) The majority of program bugs can be caught cleanly without crashing
  470.  your mac
  471. (e) All your files are shared between Patmos and MacOS so you can edit 
  472. using your favourite mac editor, then immediately compile in Patmos 
  473. without having to reboot or copy files around.
  474.  
  475. The downside is that not all macs use the memory management unit in the 
  476. same way, or even have the same kind of MMU, so Patmos may not run on 
  477. your particular mac model.  However, since the kernel source code is 
  478. very small, the task of adapting it to a new environment is very simple, 
  479. and once achieved, all application programs running in user mode are 
  480. enabled to run without even recompiling.
  481.  
  482. <ftp://nic.switch.ch in /software/mac/src/patmos>
  483.  
  484. 1.13) Q: I have this library written in (Think) Pascal that I want to use 
  485. from Think C/Symantec C, but I get link errors/don't know how to do it.  
  486. What should I do?
  487.  
  488. A: Start by writing a .h file describing the interface.  Remember to 
  489. declare the Pascal functions "pascal".  Build a library with Think Pascal 
  490. and convert it with oConv.
  491.  
  492. Do you get link errors on symbols defined in your Pascal lib? Check the 
  493. capitalisation used.
  494.  
  495. Do you get errors on symbols like LMUL and LDIV? Those functions are 
  496. defined in the Think Pascal library Runtime.lib or uRuntime.lib.  Include 
  497. uRuntime.lib and try again.
  498.  
  499. Do you get link errors on standard symbols like thePort? This is due to 
  500. bad capitalization in Symantec's libs.  Run oConv with .v checked.  This 
  501. will create a TEXT file with a .v extension.  Open that with a text 
  502. editor and correct the capitalization.  Run oConv again, with .v checked 
  503. this time too.
  504.  
  505. Do you still get errors on standard symbols? Are you using Think 
  506. C/Symantec C++ version 6 or higher? Then you must open the library (after 
  507. converting it) from Think C version 5, and remove the unit named 
  508. %_TOOLBOX.  (If I'm not mistaken, this is the toolbox init unit, which 
  509. you won't need anyway.)
  510.  
  511. 1.14) Q: CodeWarrior vs. Think/Symantec C++:  Which is better?
  512.  
  513. A:  See the above discussion on CodeWarrior, Think, and MPW for a full 
  514. understanding of the issues involved.
  515.  
  516. 1.15) Q: Can CodeWarrior read Think libraries?
  517.  
  518. A: Yes, in a way.  Here are the steps required.
  519.  
  520. 1) Secure a copy of Think Pascal and a machine that can run it
  521. 2) Import the Think C library into Think Pascal
  522. 3) Build a Think Pascal library (in MPW format)
  523. 4) Import MPW format library into CodeWarrior
  524.  
  525. 1.16) Q: What are some good books on the subject of learning the Mac 
  526. Toolbox?
  527.  
  528. A: Any of Dan Parks Sydow's numerous books on the subject.  Recommended 
  529. also is Dave Mark's _Ultimate Mac Programming_ (a Macworld book, for 
  530. some odd reason).  Stay away from Dave Mark's _Learn C on the Mac_ and 
  531. _Learn C++ on the Mac_.
  532.  
  533. See also Nick DeMello's books review, posted in c.s.m.p.info from time 
  534. to time.
  535.  
  536. 1.17) Q:  Source code!  I want source code!  Where can I find some?
  537.  
  538. A: Celestin Company, Inc. sells the Apprentice 2 CD-ROM.  Apprentice 
  539. contains over 600 megabytes of programmer utilities and up-to-date 
  540. source code in CodeWarrior, Symantec, and MPW projects for C, C++, and 
  541. Pascal.  <ftp://ftp.teleport.com/vendors/cci/apprentice/apprentice.hqx>
  542. for an index and info.  <celestin@olympus.net>
  543.  
  544. Also, the alt.sources.mac archive at <ftp://ftpbio.bgsu.edu> contains a 
  545. lot of misc. source code and snippets not found elsewhere.
  546.  
  547. <ftp://nic.switch.ch> is another good source for unique source code.
  548.  
  549. Info-mac is a good source for source, info-mac/dev/src.
  550.  
  551. 1.18) Q: I'm trying to use a largish array in Think C, but get a "code 
  552. overflow" error.  This is valid C, why doesn't it work?
  553.  
  554. A: The ANSI standard does not guarantee that any structure larger than 
  555. 32767 bytes be correctly handled.  Because of historical constraints, 
  556. the Mac memory model is built around several small blocks of size 32K or 
  557. less; these are used both for code and global/static data.  If you want 
  558. to use more code or data, you have to turn on "far code" or "far data" - 
  559. you still will not get around the restriction of 32K code or data per 
  560. compiled file, though.
  561.  
  562. This is one area where CodeWarrior's 68k support shines; it works around 
  563. most such limitations and it doesn't cost much in performance either!
  564.  
  565. As opposed to, say, DOS or Windows, however, you can allocate as much 
  566. memory as you want (and there is in the machine) and step through it 
  567. using ordinary pointers; it's just that global and static data space is 
  568. addressed off the A5 register using a 16bit displacement addressing mode 
  569. in the 68000 processor.
  570.  
  571. On the PowerPC, everything is 32bit from the start; that runtime model 
  572. is much more like UNIX.  The horizon is the limit.
  573.  
  574. *2* Memory
  575.  
  576. 2.1) Q: What is a handle? 
  577.  
  578. A: A handle is a pointer to a pointer to something.  However, it is more 
  579. than that; creating a handle by taking the address of one of your own 
  580. pointers does NOT create a Handle; the Memory Manager will only deal 
  581. properly with Handles that are created using NewHandle or something that 
  582. calls it (such as NewRgn or GetResource).
  583.  
  584. 2.2) Q: When do I have to lock a Handle? 
  585.  
  586. A: The contents of a Handle may move, and when it does, the pointer your 
  587. handle is pointing to is changed to point to the new address so your 
  588. handle is always valid.  The toolbox may call the memory manager to 
  589. allocate more memory pretty much anytime you call it (the toolbox) and 
  590. when memory is allocated, your handle may move in memory.  Don't 
  591. dereference a handle into a pointer (or take the address of a field in a 
  592. record a handle is double-pointing to) and then call the toolbox and 
  593. expect the pointer to still be valid.  The only way to ensure that the 
  594. pointer will still be valid is to call HLock on the handle to lock it.
  595.  
  596. Use HGetState and HSetState to save & restore the "locked" state of a 
  597. handle when you lock it.
  598.  
  599. 2.3) Q: How do I dispose of Handles? 
  600.  
  601. A: DisposeHandle (formerly called DisposHandle) once and ONLY once will 
  602. do the trick.  Trying to dispose of an already disposed Handle is an 
  603. error.  DoubleTrouble (see above) will catch such bugs when they do 
  604. occur.
  605.  
  606. 2.4) Q: What about resources? 
  607.  
  608. A: Calling GetResource returns NULL if the resource is not found or 
  609. there is not enough memory, else it returns a handle to the resource.  
  610. This handle may be moved or locked like any other handle, but DO NOT 
  611. call DisposeHandle to get rid of a resource handle - call 
  612. ReleaseResource.  DisposeResource (see above) will catch this kind of 
  613. bug.
  614.  
  615. Remember that AddResource makes a resource handle out of an ordinary 
  616. handle, and RemoveResource or DetachResource makes an ordinary handle 
  617. out of a resource handle.  You cannot call AddResource with a resource 
  618. handle; you have to DetachResource it first.
  619.  
  620. Resource handles are automagically disposed when the resource file they 
  621. belong to is closed.
  622.  
  623. *3* User / Machine interaction
  624.  
  625. 3.1) Q: How do I read the modifier keys of the keyboard? 
  626.  
  627. A: Just call EventAvail and check the event.modifiers field. 
  628. Only works when you are in the foreground. You can also use 
  629. GetKeys(), or (as a last resort) check the lo-mem global KeyMap 
  630. directly. 
  631.  
  632. 3.2) Q: How do I move the mouse cursor to a specific position?
  633.  
  634. A: Wait! Don't do it! There has to be a better way!
  635.  
  636. If you feel you HAVE to do it (for a game or VERY special simulation 
  637. situation) you can use the Cursor Device Manager documented in the tech 
  638. notes on <ftp.apple.com>.  If that manager is not installed, as it's not 
  639. on older Macs, you can use the following code:
  640.  
  641. You need to have some low-memory globals defined.  they may be defined 
  642. in SysEqu.h.
  643.  
  644. #define MTemp 0x828
  645. #define RawMouse 0x82c
  646. #define CrsrNewCouple 0x8ce
  647.  
  648. Note that CrsrNewCouple is actually a combination of two globals, just 
  649. to make our life slightly easier.
  650.  
  651. The code I use to move the mouse is:
  652.  
  653. *code*
  654. void
  655. MoveMouseTo ( Point where ) {
  656.  
  657.     HideCursor ( ) ;
  658.     * ( Point * ) RawMouse = where ;
  659.     * ( Point * ) MTemp = where ;
  660.     * ( short * ) CrsrNewCouple = -1 ;
  661.     ShowCursor ( ) ;
  662. }
  663. *end*
  664.  
  665. You need to hit a couple more global variables if you want this to work 
  666. properly in a multiple-monitor system, but i forget what they are 
  667. offhand.  poke through SysEqu.h, and you should be able to figure it out 
  668. without a problem.
  669.  
  670. On the PowerPC, these lo-mem globals may not be available for native 
  671. applications; however, all Power Macintoshes implement the Cursor Device 
  672. Manager.  All Macs made after March '93 (including Centris 650 and 610) 
  673. implement the Cursor Device Manager, in fact.
  674.  
  675. There is also a file on nada.kth.se:pub/hacks/mac-faq/MoveMouse.c which 
  676. shows how to use the Cursor Device Manager, written by an excellent 
  677. Apple engineer.  Grab!
  678.  
  679. Careful, version 1.0 of the Universal Header "CursorDevices.h" file was 
  680. completely incorrect.  Use 2.0a3 or later.
  681.  
  682. 3.3) Q: My menus don't show up in the menu bar
  683.  
  684. A: If your menus are hiearchical, you'll have to install them manually; 
  685. GetNewMBar won't do it for you.  See also 3.5.
  686.  
  687. 3.4) Q: When the user selects my menus, I get strange results back; they 
  688. seem to have different menu IDs than my menus?
  689.  
  690. A: The Menu ID as used by the menu manager is NOT the same thing as the 
  691. MENU resource ID (used in the MBAR resource and with GetMenu()) When you 
  692. create a MENU, ResEdit sets the menu ID to the MENU resource ID, but if 
  693. you re-number the resource, you will have to open the menu in ResEdit 
  694. and change the menu ID using the "Edit MENU ID" menu item.
  695.  
  696. 3.5) Q: I use GetMenu() to find a menu in the menu bar, and then change 
  697. it, but it seems I have a memory leak OR my changes don't "punch 
  698. through"
  699.  
  700. A: GetMenu() is only intended if you don't already have the menu "in 
  701. memory." The call you should use almost all the time is GetMHandle() 
  702. which gets the handle to a menu in the current menu bar by its menu ID 
  703. (not resource id).
  704.  
  705. 3.6) Q: What about pre-emptive multitasking? 
  706.  
  707. A: To the user, the Mac multitasking method, which builds upon each 
  708. application calling WaitNextEvent, GetNextEvent or EventAvail every so 
  709. often and the Process Manager/MultiFinder switching applications only at 
  710. such calls, is at least as good as preemtive multitasking, because the 
  711. present system priotitizes user interface responsiveness over everything 
  712. else.  The only shortfall about this is formatting floppies, which locks 
  713. up the Mac CPU.  This is because the Mac floppy controller is really 
  714. stupid, and would happen even if the Mac multitasked preemptively.
  715.  
  716. There IS "real" pre-emptive multitasking available for use in Mac 
  717. applications; the expensive way is buying A/UX 3.0 which can have Mac 
  718. applications written as UNIX processes; the cheap way is installing the 
  719. Thread Manager which will allow you to create pre-emptive threads.  
  720. However, the restrictions on those threads are the same as those on Time 
  721. Manager tasks: don't call any function in an unloaded segment, and don't 
  722. call QuickDraw or any toolbox call which may move memory (which are most 
  723. ToolBox calls; paradoxally, BlockMove is safe :-) as are, surprisingly, 
  724. FSRead and FSWrite).  The latest word from Apple is that this 
  725. preemptive support is going away, to be replaced by something else in 
  726. Copland.
  727.  
  728. There are several problems with making the Mac OS preemptive; including 
  729. apps that draw outside their windows or directly to screen, user 
  730. dragging and other issues.  The system is being reimplemented for 8.0 
  731. (Copland) to solve these problems.
  732.  
  733. *4* Files
  734.  
  735. 4.1) Q: How do I tell fopen() to open a file the user has selected using 
  736. StandardGetFile?
  737.  
  738. A: The "standard" ANSI C file functions are less than well suited for 
  739. the Macintosh way of doing things.  However, if you are doing a port for 
  740. your own enjoyment and benefit (or maybe for in-house work) you can use 
  741. the following function: (see below about converting a wdRefNum into a 
  742. vRefNum/parID pair)
  743.  
  744. *code*
  745. FILE *
  746. fopen_mac ( short vRefNum , long parID , char * fileName , char * mode ) {
  747.  
  748. short oldVol ;
  749. short aVol ;
  750. long aDir , aProc ;
  751. FILE * ret = NULL ;
  752.  
  753.     if ( GetVol ( NULL , & oldVol ) ) {
  754.         return NULL ;
  755.     }
  756.     if ( GetWDInfo ( oldVol , & aVol , & aDir , & aProc ) ) {
  757.         return NULL  ;
  758.     }
  759.     if ( HSetVol ( NULL , vRefNum , parID ) ) {
  760.         return NULL ;
  761.     }
  762.     ret = fopen ( fileName , mode ) ;
  763.     if ( HSetVol ( NULL, aVol , aDir ) ) {
  764.         /* an error we can't currently handle */
  765.     }
  766.     if ( SetVol ( NULL, oldVol ) ) {
  767.         /* an error we can't currently handle */
  768.     }
  769.     return ret ;
  770. }
  771. *end*
  772.  
  773. All of the above is necessary for one reason or another - if you are 
  774. interested, by all means look HSetVol up in Think Reference 2.0 or New 
  775. Inside Mac: Files.
  776.  
  777. In older versions of MPW; this wouldn't work since the MPW libraries 
  778. used to do a GetVol and explicitly use that value by itself.
  779.  
  780. 4.2) Q: When can I use the HOpen, HCreate etc file calls? Are they only 
  781. System 7 calls?
  782.  
  783. A: All the HXxx calls that take a vRefNum and parID as well as the file 
  784. name are implemented in glue that works on any system that has HFS 
  785. (meaning 3.2 and up with the HD20 INIT, and all systems from System 6 
  786. and up)
  787.  
  788. The glue is available in MPW 3.2 and up, and Think C 5.0 and up.  This 
  789. goes for all HXxx calls except HOpenDF; therefore, if you are interested 
  790. in System 6 compatibility, use HOpen instead and make sure you don't 
  791. allow file names beginning with a period.
  792.  
  793. 4.3) Q: Why do you say wdRefNum sometimes and vRefNum sometimes? 
  794. Why do you say parID sometimes and dirID sometimes? 
  795.  
  796. A: When the Mac first made an appearance in 1984, it identified files by 
  797. using a vRefNum (volume reference number meaning a floppy disk or later 
  798. hard disk) and a name.  Once HFS saw the light of day, folders within 
  799. folders became a reality, and you needed a dirID as well to point out 
  800. what folder you really meant on the volume.  However, older programs 
  801. that weren't being rewritten still knew nothing about directory IDs, so 
  802. Apple had SFGetFile make up "fake" vRefNums that didn't just specify a 
  803. volume, but also a parent folder.  These are called wdRefNums (for 
  804. working directory) and were a necessary evil invented in 1985.  You 
  805. should not create (or, indeed, use) wdRefNums yourself.
  806.  
  807. There is a system-wide table that maps wdRefNums onto vRefNum/parID 
  808. pairs.  There is a limit to the size of this table.  A dirID and a parID 
  809. is almost the same thing; you say "parID" when you mean the folder 
  810. something is in, while you say a "dirID" when you mean the folder 
  811. itself.  If you for instance have a folder called "Foo" with a folder 
  812. called "Bar" in it, the parID for "Bar" would be the dirID for "Foo."
  813.  
  814. 4.4) Q: How do I convert a wdRefNum as returned by SFGetFile 
  815. into a vRefNum/parID pair to use with the HXxx calls?
  816.  
  817. A: Use GetWDInfo, which is declared as: 
  818.  
  819. Pascal OSErr GetWDInfo ( short wdRefNum , short * vRefNum , long * parID 
  820. , OSType * procID ) ;
  821.  
  822. The procID parameter must be non-NULL and point to an OSType variable, 
  823. but the value of that variable can and should be ignored.
  824.  
  825. It is recommended that, as soon as you get your hands on a wdRefNum, for 
  826. instance from SFGetFile, you directly convert it into a vRefNum/parID 
  827. pair and always use the latter to reference the folder.
  828.  
  829. 4.5) Q: How do I select a folder using SFGetFile? 
  830.  
  831. A: This requires a custom dialog with a filter proc.  It is too 
  832. complicated to show here, but not totally impossible to comprehend.  
  833. There is sample code on ftp.apple.com, in the directory dts/snippets, on 
  834. how to do this.
  835.  
  836. 4.6) Q: How do I get the full path of a file referenced by a vRefNum, 
  837. parID and name?
  838.  
  839. A: You don't. 
  840.  
  841. OK, I cheated you.  There is exactly ONE valid reason to get the full 
  842. path of a file (or folder, for that matter) and that is to display its 
  843. location to the user in, say, a settings dialog.  To actually save the 
  844. location of the file you should do this: (assuming the file is in an 
  845. FSSpec called theFile - you can use FSSpecs in your program even if you 
  846. don't run under System 7; just make your own MyFSMakeFSSpec that fills 
  847. in the FSSpec manually if it's not implemented)
  848.  
  849. *code*
  850. if ( ! aliasManagerAvailable ) { /* System 6 ? */
  851.     GetVolumeName ( theFile -> vRefNum , vName ) ;
  852.     GetVolumeModDate ( vRefNum , & date ) ;
  853.     Save ( vName , date , parID , fileName ) ;
  854. } else {
  855.     NewAlias ( NULL , theFile , & theAlias ) ;
  856.     Save ( theAlias ) ;
  857.     DisposeHandle ( ( Handle ) theAlias ) ;
  858. }
  859. *end*
  860.  
  861. If you are really concerned about these issues (of course you are!) you 
  862. should save BOTH of these methods when available, and load back whatever 
  863. is there that you can handle; since users may be using your application 
  864. in a mixed System 6/System 7 environment.
  865.  
  866. To get back to the file is left as an exercise for the reader. 
  867.  
  868. To open a file using fopen() or the Pascal equivalent, see above about 
  869. using and not using HSetVol.
  870.  
  871. 4.7) Q: What about actually getting the full path for a file? I promise 
  872. I will only use it to show the location of a file to the user!
  873.  
  874. A: Enter PBGetCatInfo, the Vegimatic of the Mac file system.  Any Mac 
  875. hacker of knowledge has taken this system call to his heart.  Note that 
  876. this sample code isn't all there, but should point you in the right 
  877. direction:
  878.  
  879. *code*
  880. Boolean IsFolder(FSSpec *fs) // this function is called later
  881. {
  882.         CInfoPBRec rec;
  883.  
  884.         rec.hFileInfo.ioNamePtr = fs -> name;
  885.         rec.hFileInfo.ioVRefNum = fs -> vRefNum;
  886.         rec.hFileInfo.ioDirID = fs -> parID;
  887.  
  888.  
  889.         if ( !fs -> name [ 0 ] )
  890.         {
  891.                 rec . hFileInfo . ioFDirIndex = 0 ;
  892.         } else
  893.         {
  894.                 rec . hFileInfo . ioFDirIndex = -1 ;
  895.         }
  896.  
  897.         rec . hFileInfo . ioFVersNum = 0 ;
  898.         PBGetCatInfoSync (&rec);
  899.  
  900.         return(!rec.hFileInfo.ioFlAttrib & 0x10);
  901. }
  902. *end*
  903.  
  904. *code*
  905. OSErr GetFolderParent(FSSpec *fss, FSSpec *parent)
  906. {
  907.     CInfoPBRec rec;
  908.     short err;
  909.  
  910.     *parent = *fss;
  911.     rec.hFileInfo.ioNamePtr = parent -> name ;
  912.     rec.hFileInfo.ioVRefNum = parent -> vRefNum ;
  913.     rec.hFileInfo.ioDirID = parent -> parID ;
  914.  
  915.     if ( !parent -> name [ 0 ] )  // dougw -- neg of FAQ
  916.     {
  917.         rec . hFileInfo . ioFDirIndex = 0 ;
  918.     }
  919.     else
  920.     {
  921.         rec . hFileInfo . ioFDirIndex = -1 ;
  922.     }
  923.  
  924.     rec . hFileInfo . ioFVersNum = 0 ;
  925.     err = PBGetCatInfoSync ( & rec ) ;
  926.  
  927.     if ( ! ( rec . hFileInfo . ioFlAttrib & 0x10 ) ) { /* Not a folder */
  928.         if ( ! err ) {
  929.             err = dirNFErr ;
  930.         }
  931.     } else {
  932.         parent -> parID = rec . dirInfo . ioDrParID ;
  933.         BlockMove(rec.dirInfo.ioNamePtr, parent->name, rec.dirInfo.ioNamePtr[0]);
  934.     }
  935.     return err ;
  936. }
  937. *end*
  938.  
  939. *code*
  940. OSErr GetFullPathHandle ( FSSpec * fss , Handle * h )
  941. {
  942.     Handle  tempH = NULL;
  943.     FSSpec fs = * fss ;
  944.     FSSpec sSpec;
  945.  
  946.     if(*h == NULL) // allocate a handle if needed
  947.     {
  948.         *h = NewHandle(0);
  949.     }
  950.  
  951.     while ( fs . parID > 1 )
  952.     {
  953.         tempH = NULL ;
  954.         PtrToHand ( & fs . name [ 1 ] , & tempH , fs . name [ 0 ] ) ;
  955.         PtrAndHand ( ( void * ) ":" , tempH , 1 ) ;
  956.         HandAndHand ( * h , tempH ) ;
  957.         SetHandleSize ( * h , 0L ) ;
  958.         HandAndHand ( tempH , * h ) ;
  959.         DisposeHandle ( tempH ) ;
  960.  
  961.         tempH = NULL ;
  962.         GetFolderParent ( & fs , & sSpec ) ;
  963.         fs = sSpec ;
  964.     }
  965.     
  966.     // fs should now contain info about the volume itself
  967.     PtrToHand ( & fs . name [ 1 ] , & tempH , fs . name [ 0 ] ) ;
  968.     PtrAndHand ( ( void * ) ":" , tempH , 1 ) ;
  969.     HandAndHand ( * h , tempH ) ;
  970.     SetHandleSize ( * h , 0L ) ;
  971.     HandAndHand ( tempH , * h ) ;
  972.     DisposeHandle ( tempH ) ;
  973.     tempH = NULL ;
  974.  
  975.     if (!IsFolder ( fss ) )
  976.     {
  977.         SetHandleSize ( * h , GetHandleSize ( * h ) - 1 ) ;
  978.     }
  979.  
  980.     DisposeHandle(tempH);
  981.     return 0 ;
  982. }
  983. *end*
  984.  
  985. 4.8) Q: So how do I get the names of the files in a directory? 
  986.  
  987. A: You use PBGetCatInfo again, but this time you set ioFDirIndex to 1 or 
  988. more (you need to know the dirID and vRefNum of the folder you're 
  989. interested in) You then call PBGetCatInfoSync for values of ioFDirIndex 
  990. from 1 and up, until you get an fnfErr.  Any other err means you are not 
  991. allowed to get info about THAT item, but you may be for the next.  Then 
  992. collect the names in the string you made ioNamePtr point to as you go 
  993. along.  Note that you need to fill in the ioDirID field for each 
  994. iteration through the loop, and preferably clear the ioFVersNum as well.
  995.  
  996. Note that the contents of a directory may very well change while you are 
  997. iterating over it; this is most likely on a file server that more than 
  998. one user uses, or under System 7 where you run Personal File Share.
  999.  
  1000. 4.9) Q: How do I find the name of a folder for which I only know the 
  1001. dirID and vRefNum?
  1002.  
  1003. A: You call (surprise!) PBGetCatInfo! Make ioNamePtr point to an empty 
  1004. string (but NOT NULL) of length 63 (like, an Str63) and ioFDirIndex 
  1005. negative (-1 is a given winner) - this makes PBGetCatInfo return 
  1006. information about the vRefNum/dirID folder instead of the file/folder 
  1007. specified by vRefNum, parID and name.
  1008.  
  1009. 4.10) Q: How do I make the Finder see a new file that I created? Or if I 
  1010. changed the type of it; how do I display a new icon for it?
  1011.  
  1012. A: You call (surprise!) PBGetCatInfo followed by PBSetCatInfo for the 
  1013. FOLDER the file is in.  Inbetween, you should set ioDrMdDat to the 
  1014. current date&time.  Code:
  1015.  
  1016. *code*
  1017. OSErr
  1018. TouchFolder ( short vRefNum , long parID ) {
  1019.  
  1020. CInfoPBRec rec ;
  1021. Str63 name ;
  1022. short err ;
  1023.  
  1024.     rec . hFileInfo . ioNamePtr = name ;
  1025.     name [ 0 ] = 0 ;
  1026.     rec . hFileInfo . ioVRefNum = vRefNum ;
  1027.     rec . hFileInfo . ioDirID = parID ;
  1028.     rec . hFileInfo . ioFDirIndex = -1 ;
  1029.     rec . hFileInfo . ioFVersNum = 0 ;
  1030.     err = PBGetCatInfoSync ( & rec ) ;
  1031.     if ( err ) {
  1032.         return err ;
  1033.     }
  1034.     GetDateTime ( & rec . dirInfo . ioDrMdDat ) ;
  1035.     rec . hFileInfo . ioVRefNum = vRefNum ;
  1036.     rec . hFileInfo . ioDirID = parID ;
  1037.     rec . hFileInfo . ioFDirIndex = -1 ;
  1038.     rec . hFileInfo . ioFVersNum = 0 ;
  1039.     rec . hFileInfo . ioNamePtr [ 0 ] = 0 ;
  1040.     err = PBSetCatInfoSync ( & rec ) ;
  1041.     return err ;
  1042. }
  1043. *end*
  1044.  
  1045. 4.11) Q: Aren't we done with PBGetCatInfo soon? 
  1046.  
  1047. A: Well, it turns out that you can also find out whether an FSSpec is a 
  1048. file or a folder by calling PBGetCatInfo and check bit 4 (0x10) of 
  1049. ioFlAttr to see whether it is a folder.  You may prefer to call 
  1050. ResolveAliasFile for this instead.
  1051. You can also check the script of the file's title using PBGetCatInfo and 
  1052. check the ioFlFndrXInfo field if you want to work with other script 
  1053. systems than the Roman system.
  1054.  
  1055. Another common use is to find out how many items are in a folder; the 
  1056. modification date of something or the correct capitalization of its name 
  1057. (since the Mac file system is case independent BUT preserves the case 
  1058. the user uses)
  1059.  
  1060. 4.12) Q: How do I set what folder should initially be shown in the 
  1061. SFGetFile boxes?
  1062.  
  1063. A: You stuff the dirID you want to show into the lo-mem global 
  1064. CurDirStore, and the NEGATIVE of the vRefNum you want into the lo-mem 
  1065. global SFSaveDisk.
  1066.  
  1067. If you are using CustomGetFile and return sfSelectionChanged from an 
  1068. "init" message handler, you must remember to clear the script code, else 
  1069. the selection will not change.
  1070.  
  1071. 4.13) Q: How do I find the folder my application started from? How do I 
  1072. find the application file that's running?
  1073.  
  1074. A: Under System 7, you call GetProcessInformation using the 
  1075. ProcessSerialNumber kCurrentProcess with a pointer to an existing FSSpec 
  1076. in the parameter block.  This will give you your file, and, by using the 
  1077. vRefNum and parID, the folder the application is in.
  1078.  
  1079. *code*
  1080. OSErr   CurrentProcessLocation(FSSpec *applicationSpec)
  1081. {
  1082.     ProcessSerialNumber currentPSN;
  1083.     ProcessInfoRec info;
  1084.     
  1085.     currentPSN.highLongOfPSN = 0;
  1086.     currentPSN.lowLongOfPSN = kCurrentProcess;
  1087.     info.processInfoLength = sizeof(ProcessInfoRec);
  1088.     info.processName = NULL;
  1089.     info.processAppSpec = applicationSpec;
  1090.     return ( GetProcessInformation(¤tPSN, &info) );
  1091. }
  1092. *end*
  1093.  
  1094. Beware from writing to your applications resource or data forks; the 
  1095. former breaks on CDs/write protected floppies/file servers/virus 
  1096. checkers, the latter fails on PowerPC as well as in the above cases.
  1097.  
  1098. 4.14) Q: When can I use those nifty, easy to use FSSpec calls?
  1099.  
  1100. A: In Systems >= 7, in System 6 with QuickTime installed, in any system 
  1101. if you use the FSpCompat functions provided by MoreFiles [see below].
  1102.  
  1103. 4.15) Q: I hate dealing with the low-level file manager stuff;  why 
  1104. didn't Apple provide a complete high-level interface using FSSpecs?
  1105.  
  1106. A: Good question.  Apple, in the guise of Jim Luther of Mac Developer 
  1107. Technical Support, has written a library called MoreFiles, which not 
  1108. only provides a high-level interface to low-level file stuff, but 
  1109. provides FSSpec glue for Systems below 7.  MoreFiles is available on the 
  1110. Developer CD's (see above) and also at Appple's developer ftp site.
  1111.  
  1112. *5* Imaging with QuickDraw
  1113.  
  1114. 5.1) Q: Why is CopyBits so slow? 
  1115.  
  1116. A: It is not.  It just requires some hand-holding to get good results.  
  1117. The main rules are: Make sure the source and destination pixMaps are of 
  1118. the same depth.
  1119.  
  1120. Make sure the front color is black and the back color is white.
  1121.  
  1122. Use srcCopy and don't use a masking region, unless it's rectangular. 
  1123.  
  1124. Copy to an unclipped window (the frontmost window).  Make sure the 
  1125. ctSeed values of the source pixMap and dest pixMap match. 
  1126.  
  1127. Copying few and large pixMaps is faster than copying many and small 
  1128. ones.  Icon-sized sprites count as small ones.
  1129.  
  1130. Make sure your source bitmap or pixelMap has the same alignment, when 
  1131. adjusted for the source and destination rect expressed in global screen 
  1132. coordinates.  The necessary alignment is 32 bits (4 bytes), although 128 
  1133. bit (16 byte) alignment is probably even better on 68040 macs and won't 
  1134. hurt on other macs.
  1135.  
  1136. Example of global alignment: 
  1137.  
  1138. Your window is positioned at (42,100) (H,V) 
  1139.  
  1140. Your destination rectangle is (10,20)-(74,52) 
  1141.  
  1142. The alignment coefficient of the rectangle in global coordinates is 
  1143. (42+10)*bitDepth where bitDepth is one of 1,2,4,8,16 or 32.
  1144.  
  1145. Make sure your source pixmap rect has the same coeffecient modulo your 
  1146. alignment factor (in bits) For black&white macs, this is still true, 
  1147. although bitDepth is fix at 1.  Offscreen pixMaps can calculate with a 
  1148. "global posistion" of 0,0 and get correct results.
  1149.  
  1150. 5.2) Q: Why is CopyBits still too slow? 
  1151.  
  1152. A: Because there is always some overhead involved in calling QuickDraw; 
  1153. you have the trap dispatcher, clipping checks, and checking whether the 
  1154. CopyBits call is being recorded in a PICT handle (if you called 
  1155. OpenPicture)
  1156.  
  1157. If you can't live with this, look at 4.8 below, but PLEASE try and make 
  1158. CopyBits work, and retain the CopyBits code in your application, so 
  1159. users with special monitors (accellerator cards, PowerBook color 
  1160. screens, Radius Pivot screens) can still play your game.  (non-game 
  1161. applications don't need more speed than CopyBits can give at its max.  
  1162. Promise!)
  1163.  
  1164. 5.3) Q: What is the fastest way to set one pixel? 
  1165.  
  1166. A: On 68k Macs, NOT SetCPixel()! Assuming you have the correct ForeColor() 
  1167. set, you can set the pen size to (1,0) and call Line (0,1)
  1168.  
  1169. I have heard PaintRect is good for this but requires slightly more code.  
  1170. Using PaintRect eliminates a trap call.
  1171.  
  1172. 5.4) Q: Why do pictures I record suddenly draw as empty space or not 
  1173. draw at all?
  1174.  
  1175. A: When recording pictures, you have to set the clipping area to exactly 
  1176. the frame of the picture you are recording.  This is because it is 
  1177. initally set at -32768,32727 in both directions, and offsetting the 
  1178. picture even one pixel when drawing it will result in the region 
  1179. wrapping around and becoming empty.
  1180.  
  1181. When recording pictures, do this: 
  1182.  
  1183. *code*
  1184. PicHandle h = OpenPicture ( & theRect ) ;
  1185. ClipRect ( & theRect ) ;
  1186.     /* draw the picture */
  1187. ClosePicture ( ) ;
  1188. *end*
  1189.  
  1190. 5.5) Q: Where can I find the format of picture files and 
  1191. resources? 
  1192.  
  1193. A: The format of a picture resource version 1 is defined in a technical 
  1194. note.  This format is obsolete.
  1195.  
  1196. The format of a picture resource version 2 is defined in Old Inside Mac 
  1197. vol V, with addenda in Old Inside Mac vol VI.
  1198.  
  1199. Some things happen with QuickTime compressed pictures; try the Inside 
  1200. Mac: QuickTime book or turn to Inside Mac: Imaging with QuickDraw which 
  1201. is the definite reference on QuickDraw.
  1202.  
  1203. The format of a picture file is the same as that of a picture resource 
  1204. with 512 added 0 bytes in front.
  1205.  
  1206. 5.6) Q: GWorlds? 
  1207.  
  1208. A: What about them?  They're great.  Look them up in IM: Imaging With 
  1209. QuickDraw.  Don't forget to SetGWorld back to what it was before calling 
  1210. WaitNextEvent.
  1211.  
  1212. 5.7) Q: How do I find the current depth of the screen? 
  1213.  
  1214. A: My question to you is: What screen? Many macs have more than one 
  1215. screen attached.  You can use GetDeviceList and walk the devices to find 
  1216. the screen you're looking for (use TestDeviceAttribute to see whether 
  1217. it's a screen) or you can call GetMaxDevice() to find the deepest device 
  1218. your window intersects.
  1219.  
  1220. Once you have the device handle, finding the depth is just a 
  1221. matter of looking at the gdPMap pixMapHandle, and dereference it 
  1222. to the pmSize field. Done. 
  1223.  
  1224. 5.8) Q: Why is it a bad idea to draw directly to screen? 
  1225.  
  1226. A: Because of several reasons: 
  1227.  
  1228. - You will be incompatible with future display hardware. 
  1229.  
  1230. - You will be incompatible with some present-day display 
  1231. hardware, such as Radius Pivots and PowerBook color screens. 
  1232.  
  1233. - You have to think about a lot of things; testing it all on 
  1234. your own machine is not possible and the chances of crashing are 
  1235. great. 
  1236.  
  1237. - You will be incompatible with future hardware where devices 
  1238. may live in some unaccessible I/O space. 
  1239.  
  1240. 5.9) Q: But I really need to do it.  I can't make my animation into a 
  1241. QuickTime movie, and CopyBits is too slow, even when syncing to the 
  1242. screen retrace and with my source GWorld aligned properly.
  1243.  
  1244. A: You have to prepare yourself, and ask these questions: 
  1245.  
  1246. - Do I want to support all screens, or just 8-bit devices?
  1247.  
  1248. - Do I have a few weeks of free time to make it work? 
  1249.  
  1250. - Do I want to get nasty mail when I break on some hardware and 
  1251.    have to rev the application - even if I may not be able to get 
  1252.    ahold of the hardware that makes it break? 
  1253.  
  1254. If all you're doing is rendering an image pixel-by-pixel or 
  1255. line-by-line, maybe you can draw directly into an offscreen 
  1256. pixMap/GWorld and then CopyBits the entire GWorld to screen? 
  1257. That will be more compatible, especially if you use the 
  1258. keepLocal flag when creating the GWorld. 
  1259.  
  1260. 5.10) Q: Okay, so how do I get the base address of the screen? 
  1261.  
  1262. A: "The" screen? Which screen? There may be several. The base 
  1263. address may be on an accellerated screen card. There may be more 
  1264. than one screen covering the same desktop area. 
  1265.  
  1266. Due to unfortunate circumstances, there is a bug in 
  1267. GetPixBaseAddr() that causes it to return incorrect results for 
  1268. early versions of System 7. Instead, get the baseAddr directly 
  1269. from the gdPMap handle of the GDHandle for the screen you draw 
  1270. to. This address may need switching to 32bit mode to be valid. 
  1271.  
  1272. 5.11) Q: Quit stalling and give me code! 
  1273.  
  1274. A: Okay, but I'll let you sweat over Inside Mac to figure out 
  1275. what it does. All of it is important; believe me! To make this 
  1276. code run faster, a lot of the things it does can be done once 
  1277. before starting to draw. 
  1278.  
  1279. Make sure that you have a window that covers the area where you 
  1280. are drawing, so other windows will not be overdrawn. Also make 
  1281. sure that you do not do direct-to-screen-drawing while you are 
  1282. in the background. 
  1283.  
  1284. *code*
  1285. /* This is presently untested code */
  1286. /* Value is dependent on what depth the screen has */
  1287. /* This code doesn't work on non-color-quickdraw Macs (i e the MacClassic) */
  1288. /* "where" is in GLOBAL coordinates */
  1289. void
  1290. SetPixel ( Point where , unsigned long value ) {
  1291.  
  1292. Rect r ;
  1293. GDHandle theGD ;
  1294. char * ptr ;
  1295. long rowBytes ;
  1296. short bitsPerPixel ;
  1297. PixMapHandle pmh ;
  1298. Boolean oldMode ;
  1299.  
  1300.     r . left = where . h ;
  1301.     r . top = where . v ;
  1302.     r . right = r . left + 1 ;
  1303.     r . bottom = r . top + 1 ;
  1304.     theGD = GetMaxDevice ( & r ) ;
  1305.     if ( theGD ) {
  1306.         where . v -= ( * theGD ) -> gdRect . left ;
  1307.         where . h -= ( * theGD ) -> gdRect . top ;
  1308.         pmh = ( * theGD ) -> gdPMap ;
  1309.         rowBytes = ( ( * pmh ) -> rowBytes ) & 0x3fff ;
  1310.         ptr = ( char * ) ( * pmh ) -> baseAddr ;
  1311.         bitsPerPixel = ( * pmh ) -> pixelSize ;
  1312.         oldMode = true32b ;
  1313.         ptr += where . v * rowBytes ;
  1314.         SwapMMUMode ( & oldMode ) ;
  1315.         switch ( bitsPerPixel ) {
  1316.         case 1 :
  1317.             if ( value & 1 ) { 
  1318.                 ptr [ where . h >> 3 ] |= ( 128 >> ( where . h & 7 ) ) ;
  1319.             } else {
  1320.                 ptr [ where . h >> 3 ] &= ~( 128 >> ( where . h & 7 ) ) ;
  1321.             }
  1322.             break ;
  1323.         case 2 :
  1324.             ptr [ where . h >> 2 ] &= ( 192 >> 2 * ( where . h & 3 ) ) ;
  1325.             ptr [ where . h >> 2 ] |= ( value & 3 ) << 2 * ( 3 - ( where . h & 3 ) ) ;
  1326.             break ;
  1327.         case 4 :
  1328.             ptr [ where . h >> 1 ] &= ( where . h & 1 ) ? 0xf : 0xf0 ;
  1329.             ptr [ where . h >> 1 ] |= ( value & 15 ) << 4 * ( 1 - ( where . h & 1 ) ) ;
  1330.             break ;
  1331.         case 8 :
  1332.             ptr [ where . h ] = value ;
  1333.             break ;
  1334.         case 16 :
  1335.             ( ( unsigned short * ) ptr ) [ where . h ] = value ;
  1336.             break ;
  1337.         case 32 :
  1338.             ( ( unsigned long * ) ptr ) [ where . h ] = value ;
  1339.             break ;
  1340.         default :
  1341.             abort ( ) ; /* Should never get here */
  1342.         }
  1343.         SwapMMUMode ( & oldMode ) ;
  1344.     }
  1345. }
  1346. *end*
  1347.  
  1348. *6* Text
  1349.  
  1350. 6.1) Q: How do I get TextEdit to display more than 32k of text?
  1351.  
  1352. A: You don't.  Truly, it's not worth it.  There's a call-for-call 
  1353. TextEdit replacement called TE32k which does > 32k text, and is 
  1354. available from any recent Info-Mac mirror.  It doesn't do styled text, 
  1355. though.
  1356.  
  1357. 6.2) Q: How do I get TextEdit to display more than 32k of __styled__ 
  1358. text *and* embedded objects in the text (such as pictures)?
  1359.  
  1360. WASTE, available at ftp://ghost.dsi.unimi.it/pub2/papers/piovanel, is a 
  1361. vast improvement over TextEdit.  Version 1.0 does >32k styled text 
  1362. retains compatibility with the TextEdit style scrap (which is used to 
  1363. store styled text in files such as SimpleText's, as well as in the 
  1364. clipboard), includes source code and is freeware.  Really worth the 
  1365. download.  Version 1.1 adds embedded objects within the text, such as 
  1366. pictures, intelligent cut-and-paste, built-in Drag Manager support, 
  1367. built-in Undo support.  1.1 is currently in alpha, but seems to be very 
  1368. stable.
  1369.  
  1370. "But," you nervously stutter, "WASTE is in Pascal!  And it's munged so 
  1371. that it won't work as an imported library w/Metrowerks C!  What now?"
  1372.  
  1373. Dan Crevier is maintaining a C port of WASTE, and the current version 
  1374. along with other WASTE goodies is at <ftp://rhino.harvard.edu/pub/dan/>.
  1375.  
  1376. 6.3) Q: I'm too back-asswards to use WASTE 1.1.  How do I include 
  1377. pictures in text using TextEdit?
  1378.  
  1379. A: There's no really easy way (such as a TEAddPict() call), and it will 
  1380. be a nasty kludge if you do get it working, but if you can live with 
  1381. that, here's how to do it.  I do recommend that you take a look at Q 
  1382. 6.2, above.
  1383.  
  1384. Write an algorithm to get the position of a special marker character 
  1385. [Teach/SimpleText uses option-space] or text attribute that the user 
  1386. will insert wherever he wants a picture.  Pass this position to a 
  1387. function similar to the one below.
  1388.  
  1389. *code* 
  1390. /*
  1391.     TEDrawPicture
  1392.     Draw a picture within TextEdit's text.
  1393.     
  1394.     input:
  1395.         pos - the position of the special character in the text where the user 
  1396.                 wants a picture.
  1397.         r   - size of picture
  1398.     
  1399.     output:
  1400.         r   - frame in which picture was drawn
  1401. */
  1402.  
  1403. void TEDrawPicture(short pos,PicHandle pic,Rect &r,TEHandle theTE)
  1404. {
  1405.     Point   bottomLeft;  //I think TT/ST uses topleft
  1406.     
  1407.     bottomLeft = TEGetPoint(pos, theTE);
  1408.     
  1409.     r.right = bottomLeft.h + (r.right - r.left);
  1410.     r.top = bottomLeft.v - (r.bottom - r.top);
  1411.     r.left = bottomLeft.h;
  1412.     r.bottom = bottomLeft.v;
  1413.     
  1414.     DrawPicture(pic, &r);
  1415. }
  1416.  
  1417. *end*
  1418.  
  1419. I'll leave selection and hiliting as an exercise for the reader (don't 
  1420. ya love it when people say that?).
  1421.  
  1422. Thereotically, it should be possible to kludge up TextEdit to the point 
  1423. where it would treat pictures as if they were actually letters (rather big 
  1424. letters, but letters just the same).  That's what the width and word break 
  1425. hooks are for, after all.  However, this would be a lot of Work, and I've 
  1426. never seen it done.  One is lead to believe that it's less work to create 
  1427. an improved version of TextEdit from the ground up with picture support.  
  1428. WASTE 1.1, in fact, does this rather nicely.
  1429.  
  1430. 6.4) Q: I have all this money, and I want to get rid of it. How do I
  1431. edit more than 32k of styled text now?
  1432.  
  1433. A: There are at least three solutions.
  1434.  
  1435. 1) The Galaxy application framework comes with a styled text editting
  1436. engine which does more than 32k of text. It also happens to support
  1437. cross-platform application development. Pricing starts at $10000.
  1438. <galaxy@visix.com>
  1439.  
  1440. 2) DataPak is selling a cross-platform library called "PAIGE".  It is 
  1441. written to be customizable to any extent, and you can do _anything_ in 
  1442. it (want quicktime movies that play and flow with the text while 
  1443. editting? Three pages of code; you can adapt their picture sample code.) 
  1444. Available for Mac, Windows & Never Trusted and Power Mac.  Pricing at 
  1445. $5000 ($25000 for source code) - it might be cheaper if you talk to 
  1446. them.  Customer support is reputed to be "lousy".
  1447.  
  1448. 3) Or you could use WASTE 1.1 with it's embedded objects implementation 
  1449. (want quicktime movies that play and flow with the text while editing? 
  1450. One page of code, if that much.)
  1451.  
  1452. 6.5) Q: How do I convert from a string to a floating point type?
  1453.  
  1454. A: Once you've got an Str255, you want to call the routine 
  1455. StringToExtended to change the number into type extended80, which is the 
  1456. 80-bit floating point type.  The nice thing about StringToExtended is 
  1457. that it even works in funky foreign language scripts like Chinese.  To 
  1458. use the routine, you must pass it not only the Str255 that you want 
  1459. converted, but also the results of the StringToFormatRec routine and the 
  1460. GetIntlResourceTable routine.  The documentation is generally difficult, 
  1461. so I'll describe the parameters and give you a code example below.  
  1462. Here's what you pass to StringToExtended:
  1463.  
  1464. - source:
  1465. Obviously the string representation of the number.
  1466.  
  1467. - myFormatRec:
  1468. This is simply the format that you expect the number to be.  For 
  1469. example, if you wanted a positive integer with up to 3 decimal places, 
  1470. you would want the format to be "###".  (The "#" symbol means a non-zero 
  1471. filled digit, whereas a "0" would mean zero filled.) If you wanted an 
  1472. floating point exponential notation with up to 2 digit integer portion, 
  1473. exactly 2 digit decimal portion, and exactly 1 digit after the "E", your 
  1474. format would be "##.00E+0".  (Actually, in Canada, you folks might use a 
  1475. "," instead of a "." for the decimal point; if you do, then you would 
  1476. put the locally correct symbol in there.) In a format string, you can 
  1477. put the format for a positive value, a negative value, and 0 seperated 
  1478. by semicolons (e.g.  "##.00E+0;-##.00E+0;0.00E+0").
  1479.  
  1480. But myFormatRec is not the format string itself, but is an internal 
  1481. format that you get from calling StringToFormatRec.  This is so you can 
  1482. store the format returned to you by StringToFormatRec and use it on 
  1483. different international systems.  There is a nice editor for ResEdit for 
  1484. 'FMAT' resources that lets you type the format string and will call 
  1485. StringToFormatRec for you and create an 'FMAT' resource out of the 
  1486. result.  Then you can load the 'FMAT' at run time and pass it to 
  1487. myFormatRec.
  1488.  
  1489. - partsTable:
  1490. The number parts table from the 'itl4' resource.  If you are using 
  1491. System 7 or later, you call GetIntlResourceTable, asking it for the 
  1492. number parts table.  You don't need to worry about HLock'ing itlHandle 
  1493. because StringToExtended doesn't move memory; just don't do anything 
  1494. between your call to GetIntlResourceTable and the StringToFormatRec 
  1495. routine.  If you are using System 6, you need to perform the 
  1496. GetIntlResourceTable by hand.  It's a few lines of code; left as an 
  1497. exercise to the reader.
  1498.  
  1499. - x:
  1500. The extended number to put the thing into.
  1501.  
  1502. So, let's say you expect the user to enter a string containing up to 5 
  1503. digits in the integer with a thousand marker between the 3rd and fourth 
  1504. digit, exactly two digits after the decimal, put it in parenthesis if 
  1505. it's negative, and have it just be "0.00" if it's 0.  (I will use "." 
  1506. for the decimal and "," for the thousand marker.  Here's the C code 
  1507. (passing in the source string):
  1508.  
  1509. *code*
  1510. Handle itlHandle;               /* The 'itl4' resource handle */
  1511. long offset, length;            /* Offset-length of parts table */
  1512. NumFormatStringRec myFormatRec; /* Canonical format record */
  1513. extended80 x;                   /* The number to put it into */
  1514.  
  1515. GetIntlResourceTable (smCurrentScript, smNumberPartsTable,
  1516.                       &itlHandle, &offset, &length);
  1517. StringToFormatRec ("\p##,###.00;(##,###.00);0.00",
  1518.                    (NumberParts *)(*itlHandle + offset),
  1519.                    &myFormatRec);
  1520. StringToExtended (source, &myFormatRec,
  1521.                   (NumberParts *)(*itlHandle + offset),
  1522.                   &x);
  1523. *end*
  1524.  
  1525. If you saved a format record in a resource using the 'FMAT' editor 
  1526. (which is really the preferred way to do it), you would change the code 
  1527. to look like this:
  1528.  
  1529. *code*
  1530. Handle itlHandle;                 /* The 'itl4' resource handle */
  1531. long offset, length;              /* Offset-length of parts table */
  1532. NumFormatStringRec **myFormatRec; /* Canonical format resource */
  1533. extended80 x;                     /* The number to put it into */
  1534.  
  1535. myFormatRec = (NumFormatStringRec **)GetResource('FMAT', MY_FMAT);
  1536. GetIntlResourceTable (smCurrentScript, smNumberPartsTable,
  1537.                       &itlHandle, &offset, &length);
  1538. StringToExtended (source, *myFormatRec,
  1539.                   (NumberParts *)(*itlHandle + offset),
  1540.                   &x);
  1541. *end*
  1542.  
  1543. The StringToExtended routines will return a result that will tell you if 
  1544. there are any parsing errors.  Error checking is also left as an 
  1545. exercise to the reader.
  1546.  
  1547. Of course, the reverse of the process (using ExtendedToString) works 
  1548. very much the same way, passing the same sorts of parameters.
  1549.  
  1550. *7* Communications and Networking
  1551.  
  1552. 7.1) Q: How do I get at the serial ports? 
  1553.  
  1554. A: You call OpenDriver for the names "\p.AOut" and "\p.AIn" to get at 
  1555. the modem port, and "\p.BOut" and "\p.BIn" for the printer port.  The 
  1556. function RAMSDOpen was designed for the original Mac with 128 kB of 
  1557. memory and 64 kB of ROM, and has been extinct for several years.
  1558.  
  1559. However, many users use their serial ports for MIDI, LocalTalk, graphic 
  1560. tablets, or what have you and have installed an additional serial port 
  1561. card to get more ports.  What you SHOULD do as a good application is to 
  1562. use the Comm Toolbox Resource Manager to search for serial resources; 
  1563. this requires that the Comms Toolbox is present (true on earlier System 
  1564. 6 with an INIT, on later System 6 and System 7 always, as well as on 
  1565. A/UX) and that you have initialized the comms resource manager.  The 
  1566. exact code follows (adapted from Inside Mac Comms Toolbox):
  1567.  
  1568. *code*
  1569. #include "CommResources.h"
  1570. OSErr
  1571. FindPorts ( Handle * portOutNames , Handle * portInNames , Handle * names , Handle * iconHandles ) {
  1572.  
  1573. OSErr ret = noErr ;
  1574. short old = 0 ;
  1575. CRMRec theCRMRec , * found ;
  1576. CRMSerialRecord * serial ;
  1577.  
  1578.     * portOutNames = NewHandle ( 0L ) ;
  1579.     * portInNames = NewHandle ( 0L ) ;
  1580.     * names = NewHandle ( 0L ) ;
  1581.     * iconHandles = NewHandle ( 0L ) ;
  1582.     while ( ! ret ) {
  1583.         theCRMRec . crmDeviceType = crmSerialDevice ;
  1584.         theCRMRec . crmDeviceID = old ;
  1585.         found = ( CRMRec * ) CRMSearch ( ( QElementPtr ) & theCRMRec ) ;
  1586.         if ( found ) {
  1587.             serial = ( CRMSerialRecord * ) found -> crmAttributes ;
  1588.             old = found -> crmDeviceID ;
  1589.             PtrAndHand ( & serial -> outputDriverName , * portOutNames ,
  1590.                 sizeof ( serial -> outputDriverName ) ) ;
  1591.             PtrAndHand ( & serial -> inputDriverName , * portInNames ,
  1592.                 sizeof ( serial -> inputDriverName ) ) ;
  1593.             PtrAndHand ( & serial -> name , * names , 
  1594.                 sizeof ( serial -> name ) ) ;
  1595.             PtrAndHand ( & serial -> deviceIcon , * iconHandles ,
  1596.                 sizeof ( serial -> deviceIcon ) ) ;
  1597.         } else {
  1598.             break ;
  1599.         }
  1600.     }
  1601.     return err ;
  1602. }
  1603. *end*
  1604.  
  1605. This will create four handles with the driver names, device names and 
  1606. driver icon handles for all of the available serial devices.  Then let 
  1607. the user choose with a pop-up menu or scrolling list, and save the 
  1608. choice in your settings file.
  1609.  
  1610. You can use OpenDriver, SetReset, SetHShake, SetSetBuf, SerGetBuf and 
  1611. the other Serial Manager functions on these drivers.  To write to the 
  1612. serial port, use FSWrite for synchronous writes that wait until all is 
  1613. written, or PBWrite asynchronously for queuing up data that is supposed 
  1614. to go out but you don't want to wait for it.  At least once each time 
  1615. through your event loop, you should call SerGetBuf on the in driver 
  1616. reference number you got from OpenDriver, and call FSRead for that many 
  1617. bytes - neither more nor less.
  1618.  
  1619. If you are REALLY interested in doing the right thing, you will use the 
  1620. Communications Toolbox Connection Manager instead; this will give you 
  1621. access to modems, direct lines, and networks of various kinds using the 
  1622. same API! Great for stuff like BBSes that may be on a network as well 
  1623. etc.  The Comms Toolbox also provides modularized terminal emulation 
  1624. and file transfer tools, although the Apple-suplied VT102 tool is pretty 
  1625. lame, as is the VT102 mode of the VT320 tool.  And it seems as though 
  1626. it was designed by people who'd never used a Mac before... more in the 
  1627. Unix style, right down to the lack of documentation and "magicCookie."
  1628.  
  1629. 7.2) Q: Where is a Berkley sockets library for the Mac? 
  1630.  
  1631. A: There are some problems with that.  MacTCP, the Mac Toolbox 
  1632. implementation of TCP/IP, doesn't have an API that looks at all like 
  1633. Berkley sockets.  For instance, there is ONE paramater-block call to do 
  1634. a combined listen()/accept()/bind() - sort of.  I have heard that there 
  1635. may be a socket library available by ftp from MIT but haven't seen it 
  1636. myself.
  1637.  
  1638. There is also a pretty good C++ TCP implementation called GUSI which is 
  1639. easily handled, and it also is callable from C using the Berkley socket 
  1640. API.  Apart from TCP, it also handles "standard" Mac network protocols 
  1641. such as ADSP.  The big disadvantage is that it is currently only 
  1642. implemented for MPW.  The ftp site is nic.switch.ch, 
  1643. software/mac/src/mpw_c.
  1644.  
  1645. I can also recommend the Communications Toolbox; for the price of using 
  1646. an API that is a bigger pain in the ass than configuring Windoze to use 
  1647. a new peripheral device, you get the benefit of being able to use any 
  1648. kind of connection (TCP tools are available).
  1649.  
  1650. Instead of the Comm Toolbox, one might use Apple's much improved Open 
  1651. Transport architecture.  Preliminary docs are available for ftp at 
  1652. seeding.apple.com.  Apple's new networking products team seems to have 
  1653. it's head screwed on tight.  A refreshing change.
  1654.  
  1655. Novell and Wollogong offer commercial socket-like libraries. 
  1656.  
  1657. 7.3) Q: Where do I find MacTCP? 
  1658.  
  1659. A: You can buy the MacTCP developers kit from APDA.  It is also 
  1660. available on E T O, and if you want saner headers than those, try ftp to 
  1661. seeding.apple.com.  MacTCP (the control panel) is included with System 
  1662. 7.5 and above.
  1663.  
  1664. 7.4) Q: I'm trying to write to the serial port.  It works fine on the 
  1665. following Machines, Quadra700, IIFX, Powerbook 170, Quadra 840av, but 
  1666. freezes on the Duo 230 and 280c.
  1667.  
  1668. A: You need to call SerHShake.  Otherwise you get screwed on some 
  1669. machines without a hardware handshaking cable because the port default to 
  1670. hardware handshaking.
  1671.  
  1672. *8* IAC
  1673.  
  1674. 8.1) Q: What are AppleEvents? 
  1675.  
  1676. A: AppleEvents are a level-5 network protocol.  If you are not familiar 
  1677. with the ISO network stack, this means it's a way of structuring sessions 
  1678. between network entities (programs) that is not dependent on the 
  1679. underlying protocol (such as PPC or TCP/IP) Despite being a network 
  1680. protocol, they can be very useful on Macs that are not on a network.  In 
  1681. short, they provide applications with a comprehensive way to send 
  1682. arbitrary structured data to other applications (or themselves) which 
  1683. receive the events through their main event loop.
  1684.  
  1685. The AppleEvent Object Model is a way of looking at applications and the 
  1686. data they contain, and also a level-6 network protocol.  You _can_ send 
  1687. AppleEvent Object Model data through AppleEvents (and the standard 
  1688. AppleEvents defined in the AppleEvent Registry use it) but you don't have 
  1689. to - unless you want to talk with other applications, of course, then the 
  1690. AEOM is a lingua franca.
  1691.  
  1692. 8.2) Q: What are the four required AppleEvents? 
  1693.  
  1694. A: There are four events your application really must implement if you 
  1695. want to sell it: the kCoreEventClass class, kAEOpenApplication, 
  1696. kAEQuitApplication, kAEOpenDocuments and kAEPrintDocuments events IDs.  
  1697. When you support these events (or any AppleEvents) you will not get 
  1698. startup info through GetAppParams() anymore, unless you run under System 
  1699. 6 of course.  The kAEOpenApplication event will be sent to you when the 
  1700. user double-clicks your app and it's not started yet.  When receiving it, 
  1701. you can put up a new untitled window.
  1702.  
  1703. kAEOpenDocuments is sent when the user double-clicks your apps documents.  
  1704. Note that if the first AppleEvent you receive is a kAEOpenDocuments 
  1705. event, the user started your app by double-clicking its documents.
  1706.  
  1707. kAEPrintDocuments is sent when the user selects your documents and 
  1708. chooses "Print" in the Finder menu.  If this is the first AppleEvent you 
  1709. receive, you should print the documents and then quit the application 
  1710. again; if you received a kAEOpenApplication or kAEOpenDocuments event 
  1711. before this, you should just print the documents and close them when 
  1712. you're done.
  1713.  
  1714. kAEQuitApplication is sent to you when the user chooses "Shutdown" or 
  1715. "Restart" from the Apple Menu.  You should ask the user whether he wants 
  1716. to save any unsaved changed documents, and then quit unless the user 
  1717. presses Cancel.
  1718.  
  1719. Interestingly enough, you can use these four AppleEvents to send even to 
  1720. non-AE-aware applications, and the system will translate these events 
  1721. into fake menu selections for you.
  1722.  
  1723. A good way of shutting down the Finder is to send it a Quit AppleEvent.  
  1724. You should send a Quit AppleEvent to File Sharing Extension before you 
  1725. shut down the Finder, though; the FSE is found by looking for a process 
  1726. with the creator 'hhgg'.
  1727.  
  1728. 8.3) Q: Are there any limits or tradeoffs with AppleEvents? 
  1729.  
  1730. A: As always, more power means more responsibility. 
  1731.  
  1732. AppleEvents sent to applications on other Macs require authentification 
  1733. the first time they are sent.  If the remote Mac allows Guests to link to 
  1734. programs, the INIT AutoGuest 2.0 might help (or the code solution that 
  1735. comes with it and you can build into your application)
  1736.  
  1737. In the first version of the AppleEvent manager, there was a total 64K 
  1738. limit on the size of data and overhead.  This limit has been lifted with 
  1739. the version of the AppleEvent manager that comes with AppleScript.
  1740.  
  1741. AppleEvents require a lot of memory copying and handle resizing in their 
  1742. construction; this means that large AppleEvents may be slow in 
  1743. construction, especially when compared to a pure PPC Toolbox or 
  1744. ADSP/ASDSP link.  The way around this is to use Jens Alfke's AEGizmos, 
  1745. available on the Developer CD, to create AppleEvents.  AEGizmos are 
  1746. being integrated into System Software, so You Will be doing the right 
  1747. thing.
  1748.  
  1749. You should use your own application signature as event class for 
  1750. AppleEvents you make up, in order not to collide with anybody else.  
  1751. Other than that, you are free to make your own events for your own 
  1752. needs, though supporting the required events and at least a subset of 
  1753. the Core event suite will buy you a lot of functinality from within 
  1754. AppleScript.  Especially important are the Get Current Selection and Set 
  1755. Current Selection events (which are really Get/Set Data on the contents 
  1756. of the current selection of the application)
  1757.  
  1758. The signature for your application SHOULD be registered with DTS to 
  1759. avoid conflicts; this is done through e-mail to 
  1760. DEVSUPPORT@AppleLink.Apple.Com and the form you use is located on the 
  1761. developer CDs and found on ftp.apple.com.
  1762.  
  1763. 8.4) Q: How do AppleEvents interface with AppleScript? 
  1764.  
  1765. A: AppleEvents are the meat and potatoes of AppleScript.  If you support 
  1766. the AppleEvent Object Model from within your application, users can 
  1767. control you through AppleScript.
  1768.  
  1769. The first thing you should do is get ahold of Inside Mac: 
  1770. Interapplication Communication, and a copy of the AppleEvents Registry.  
  1771. The former tells you all you ever need to know about AppleEvents, while 
  1772. the latter is paramount for implementing the right standard events.  If 
  1773. everybody use the standard events, dynamic data interchange between any 
  1774. applications will become sweet reality!
  1775.  
  1776. Then there is the 'aete' resource which lets you put names on the events 
  1777. you support, so that users can "Open Terminology" on your application 
  1778. from within the Apple Script Editor and use the proper AppleScript 
  1779. commands in their scripts.  The format of an aete resource is defined in 
  1780. Inside Macintosh: Interapplication Communication.
  1781.  
  1782. 8.5) Q: Can I compile and run scripts from within my application?
  1783.  
  1784. A: Yes, this is very simple.  There are toolbox calls for reading 
  1785. scripts, compiling scripts, and executing scripts.  (OSACompile, 
  1786. OSAExecute) These are all documented in Inside Mac: Interapplication 
  1787. Communication.
  1788.  
  1789. 8.6) Q: Is this a good way of getting a macro language almost 
  1790. for free? 
  1791.  
  1792. A: "Good" is an understatement.  Just let users write scripts, load them 
  1793. into menu items and go.  Total systems integration in under a week, 
  1794. including adding support for the AEOM to your application.
  1795.  
  1796. There is source code for an application called "MenuScripter" on the 
  1797. developer CD which shows you how to do an application with all of the 
  1798. menus being AppleScript scripts.  
  1799.  
  1800. 8.7) Q: Why do I get error -903 (noPortErr) when I try to send an Apple 
  1801. Event?
  1802.  
  1803. A: Make sure the isHighLevelEventAware flag in your application's SIZE 
  1804. resource is set.  The AE Manager won't allow you to send events if you're 
  1805. not able to receive them.
  1806.  
  1807. *9* Standalone Code & Trap Patching
  1808.  
  1809. 9.1) Q: How do I do code resources/extensions/external functions, much 
  1810. like HyperCard XCMDs only for my 68k app/extension?
  1811.  
  1812. A: Here is what you need to do:
  1813.  
  1814. Define a storage location for the plug-ins.  such as, for an application 
  1815. called Foo, a folder called "Foo Pouch/ PlugIns/ Modules/ Tools/ Etc", 
  1816. which your installer creates, and which you search for in the same 
  1817. folder as your application, the preferences folder, and the System 
  1818. Folder (for System 6 (no preferences)) the name of the pouch folder 
  1819. should be in a 'STR ' or 'STR#' resource for easy internationalization.
  1820.  
  1821. Next, define a resource type to hold your plug-in exectuable code.  You 
  1822. should allow any resID number, since some development systems have 
  1823. restrictions on which id numbers can be used for multi-segment code 
  1824. resources.
  1825.  
  1826. Decide whether you will allow multiple code resources per file.  For 
  1827. example, Photoshop lets you bundle an import resource, an export 
  1828. resource, and a filter into the same file, each is its own resource 
  1829. type.  It uses the resource name to tell the user, so each has its own 
  1830. name.
  1831.  
  1832. Decide on the user interface to plug-ins.  Commonly, at program 
  1833. start-up, you scan the pouch and collect all the files of the correct 
  1834. types.  Then you scan for appropriate code resources, then put the names 
  1835. of the resources in the menus.  You can also use the file names, or run 
  1836. the code resources and ask them for the correct name.
  1837.  
  1838. Decide on the calling convention.  MPW likes to pass parameters as 
  1839. longs, and pascal development systems usually can't generate C 
  1840. interfaces, so your best bet is to have the entry point of your code 
  1841. resource be something like:
  1842.  
  1843. extern pascal long PlugIn(long selector, CallbackPtr callback,
  1844.         long param1, long param2, long param3, long refcon);
  1845.  
  1846. where params 1 through 3 will have specific meanings depending upon
  1847. which function selector is asking to be performed.
  1848.  
  1849. CallbackPtr is something like this:
  1850. typedef struct CallbackRec{
  1851.         DrawFunc        erase;
  1852.         DrawFunc        paint;
  1853. }CallbackRec, *CallbackPtr;
  1854.  
  1855. where DrawFunc is something like:
  1856. typedef pascal void (*DrawFunc)(Rect *);
  1857.  
  1858. This is the place where you define services that your application will 
  1859. perform when asked to by the code resource of the plug-in.
  1860.  
  1861. You'll need to define a bunch of selectors for what you want the plug-in
  1862. to do.
  1863.  
  1864. You should definately give the plug-in an Init selector, and a dispose 
  1865. selector, and have Init return an error or a refcon, which will be 
  1866. passed back to the plug-in on each call, so the plug-in can have some 
  1867. persistent storage.  You should definately set the CurResFile to the 
  1868. plug-in's file while it is running, so it can easily get at any 
  1869. resources it needs.
  1870.  
  1871. The Init selector should pass the interface version in param1 and the 
  1872. program version in param2, so that the plug-in can cope with different 
  1873. versions of your program.
  1874.  
  1875. The actual call to the plug-in looks like:
  1876.  
  1877. *code*
  1878.     h = GetResource('PLUG', 1);
  1879.     HLock(h);
  1880.     xh = StripAddress(h);
  1881.     ((PlugInFunc) *xh)(kInit, callbacks, kInterfaceVer1, kProgVer1, 0, 0);
  1882.     HUnlock(h);
  1883. *end*
  1884.  
  1885. The StripAddress is important; if your app is running in 24bit mode, the 
  1886. resource handle may contain tag bits and you don't want strange things 
  1887. to happen if the code resource switches into 32bit mode (which QuickDraw 
  1888. may do, incidentally)
  1889.  
  1890. Exactly how you structure your calling conventions is up to you; there 
  1891. is no accepted standard (except for HyperCard XCMDs, but that is 
  1892. probably overkill for you).
  1893.  
  1894. 9.2) Q: How do I fat-patch a trap (that is, how do I patch a trap with 
  1895. both 68k and PPC code)?
  1896.  
  1897. A: Create a normal fat routine descriptor and pass it to 
  1898. Set{Tool, OS}TrapAddress().
  1899.  
  1900. *10* Compatibility
  1901.  
  1902. 10.1) Q: I see all these people call Gestalt without first 
  1903. checking whether it's implemented. Isn't that bad? 
  1904.  
  1905. A: No; Gestalt and a few other traps (the HXxx file manager 
  1906. traps, and FindFolder) are implemented using glue so they do the 
  1907. right thing even if the trap is not implemented. 
  1908.  
  1909. If you want to get rid of the glue, you can #define 
  1910. SystemSevenOrLater (and, using Think C/C++, re-pre-compile 
  1911. MacHeaders) However, then you will be responsible for checking 
  1912. for these features before you use them. 
  1913.  
  1914. 10.2) Q: What more functions are implemented in glue? 
  1915.  
  1916. A: Wake Up and Smell the Glue!
  1917.  
  1918. How often have you wished you could use that cool new ToolBox 
  1919. call, but didn't want to make your application System 7 
  1920. dependent? Well, it might be that you *could* in fact have used 
  1921. the call. Several traps are implemented in glue, that is, much 
  1922. of their functionality is linked into your application and thus 
  1923. available even if you are running under an old System. 
  1924.  
  1925. This list applies to MPW 3.2 and should also be valid for the 
  1926. current version of Think C. If you find any inaccuracies, please 
  1927. report them to me. (neeri@iis.ee.ethz.ch) 
  1928.  
  1929. FSOpen: Tries first OpenDF, then Open. 
  1930.  
  1931. HOpenResFile: Full functionality emulated if trap not available 
  1932.  
  1933. HCreateResFile: Full functionality emulated if trap not 
  1934. available 
  1935.  
  1936. FindFolder: Under System 6, understands the following values for 
  1937. folderType and returns the System Folder for all of them: 
  1938.  
  1939. kAppleMenuFolderType 
  1940.  
  1941. kControlPanelFolderType 
  1942.  
  1943. kExtensionFolderType 
  1944.  
  1945. kPreferencesFolderType 
  1946.  
  1947. kPrintMonitorDocsFolderType 
  1948.  
  1949. kStartupFolderType 
  1950.  
  1951. kSystemFolderType 
  1952.  
  1953. kTemporaryFolderType 
  1954.  
  1955. SysEnvirons: Full functionality emulated if trap not available 
  1956.  
  1957. NewGestalt: Returns an error if not implemented 
  1958.  
  1959. ReplaceGestalt: Returns an error if not implemented 
  1960.  
  1961. Gestalt: The following selectors are always implemented: 
  1962.  
  1963. vers    mach    sysv    proc    fpu     
  1964.  
  1965. qd      kbd     atlk    ram     lram    
  1966.  
  1967. 10.3) Q: I have to support System 6, don't I? 
  1968.  
  1969. A: It would be foolish to lock yourself out of the many benefits 
  1970. the System 7 API provides for software that you start to write 
  1971. now. Some of the System 6 and older things (likely SFGetFile and 
  1972. wdRefNums among others) will be phased out of the interfaces and 
  1973. lose support; especially on future platforms. 
  1974.  
  1975. The installed base of System 7 is larger than that of System 6; 
  1976. this is not surprising because Apple has been shipping System 7 
  1977. for several years with all new machines, including the LCII, 
  1978. Classic II, Performas and Color Classic. Another argument is 
  1979. that newer computer owners (having System 7) are much more 
  1980. likely to buy new software than old computer owners who have 
  1981. systems that already do what they want them to. 
  1982.  
  1983. The added work to support both System 6 and System 7 is 
  1984. significant; if you have the time and money you may want to do 
  1985. it, but only supporting System 6 and not System 7 is doomed to 
  1986. fail in the market of today. 
  1987.  
  1988. Some may call this position subjective; I call it business sense 
  1989. based on market demographics. A rule of thumb may be that if you 
  1990. target color machines only, you can just as well demand System 7 
  1991. as well. 
  1992.  
  1993. 10.4) Q: Why does my application work on an SE with accellerator 
  1994. (or a Mac II or Quadra), but not on one without? 
  1995.  
  1996. A: Assuming you're not calling Color QuickDraw (which is not 
  1997. available on accellerated SEs), you most probably have an 
  1998. odd-aligned word access somewhere. 
  1999.  
  2000. The 68000 does not allow words or longwords to be read from odd 
  2001. addresses, while the 68020 and up relaxes this restriction (it 
  2002. still is slower than aligned-word access though) 
  2003.  
  2004. This may or may not crash depending on your compiler: 
  2005.  
  2006. *code*
  2007. struct foo {
  2008.     char c1 ;
  2009.     char c2 ;
  2010.     char c3 ;
  2011.     char c4 ;
  2012.     char c5 ;
  2013. } bar ;
  2014.  
  2015.     long * x = ( long * ) & bar . c2 ;
  2016.     * x = 0x12345678 ; /* X is odd if compiler doesn't pad */
  2017.  
  2018. This WILL crash on an SE/Plus/Classic/PB100:
  2019.  
  2020. char foo [ 10 ] ;
  2021.  
  2022.     long * x = ( long * ) & foo [ 1 ] ;
  2023.     * x = 0x12345678 ;
  2024.  
  2025. *end*
  2026.  
  2027. 10.5) Q: Why does my application work on a IIci and a pre-1995 PowerMac 
  2028. but not on a Quadra?
  2029.  
  2030. A: Two reasons: 
  2031.  
  2032. 1) The Quadras 900 and 950 have special processors that handle the serial 
  2033. ports; if you write directly to the serial chips, you will crash (this 
  2034. goes for the IIfx as well)
  2035.  
  2036. 2) The Quadras have 68040 processors, as have the Centrises.  These 
  2037. processors have separate instruction and data caches (like the 68030) but 
  2038. they are larger (4K each) and unlike the 68030 which is write-through 
  2039. data cached, the 68040 is copy-back data cached.  This means that changes 
  2040. you make to "your code" aren't really changed all the time, since the 
  2041. changes may still be in the data cache and not written to memory when the 
  2042. CPU reads that part of memory into its I-cache.  Even worse; that part 
  2043. might already have been read into the I-cache before you change it in the 
  2044. D-cache, meaning that writing out the D-cache will still not be enough.  
  2045. You need to flush both the caches when writing self-modifying code.
  2046.  
  2047. Self-modifying code includes code that builds its own jump tables and 
  2048. code that decrypts itself and code that "stubs" MDEFs or WDEFs to jump 
  2049. back into the application code.
  2050.  
  2051. You flush the cache using FlushDataCache() which is implemented if 
  2052. Gestalt says you have a 68020 or better processor (or if the _HwDispatch 
  2053. trap is implemented)
  2054.  
  2055. The current PowerMac 68k emulator doesn't have the self-modifying cache 
  2056. problem, but a future faster version will.
  2057.  
  2058. 10.6) Q: Why does my application work on my Quadra but not on my 
  2059. accellerated SE?
  2060.  
  2061. A: You're probably calling Color QuickDraw without first checking if it's 
  2062. available.  The following machines do not have color QuickDraw in ROM nor 
  2063. RAM:
  2064.  
  2065. Mac Plus, Mac SE, Mac Classic, Mac Luggable, PowerBook 100, Outbound
  2066.  
  2067. 10.7) Q: I do check for color quickdraw, but crash nevertheless. 
  2068.  
  2069. A: _Gestalt lies under some versions of System 7; it says that non-color 
  2070. machines HAVE color QuickDraw when you test using the 
  2071. gestaltQuickdrawFeatures selector.
  2072.  
  2073. Instead, check the gestaltQuickdrawVersion selector, if it returns >= 
  2074. gestalt8BitQuickdraw then you can safely use gestaltQuickdrawFeatures, 
  2075. else you only have b/w QuickDraw.
  2076.  
  2077. *11* Optional System Software
  2078.  
  2079.   *11.1* QuickTime
  2080.  
  2081. 11.1.1) Q: I want to write a Amiga QuickTime player and need the 
  2082. CODEC format details. 
  2083.  
  2084. A: Although the structure of QuickTime movies is well documented in 
  2085. Inside Mac: QuickTime, the inner workings of the Apple compression 
  2086. modules is a trade secret that Apple will only license to you at great 
  2087. cost.  Perhaps it's time for a freeware, cross-platform QuickTime codec?
  2088.  
  2089. *12* Third-Party Solutions
  2090.  
  2091.   *12.1* Databases
  2092.  
  2093. 12.1.1) Q: What are some royalty-free databases available for the 
  2094. Macintosh?
  2095.  
  2096. A: These are commercial with no run-time licenses:
  2097.  
  2098. - Prograph CPX comes with a database that can do indexes and tables.  
  2099. <sales@prograph.com>
  2100.  
  2101. - CXBase Pro is an engine that comes with source, does tables and indexes 
  2102. in one compound file.  <tse.int@applelink.apple.com>
  2103.  
  2104. - dtF is a relational database library.  <DTF.AMERICA@applelink.apple.com>
  2105.  
  2106. NeoAccess is an object-oriented database, where you can store objects and 
  2107. retrieve by field value; it handles inheritance as well.  Comes as a 
  2108. demo on the CodeWarrior CD with an unlockable archive of the real thing.
  2109.  
  2110. 12.1.2) Q: I need a robust Client/Server database library.  What do I do?
  2111.  
  2112. A: Get the Macintosh Client/Server Database Development Summary, by Liam 
  2113. Breck.  It covers software applicable to Macintosh (and cross-platform) 
  2114. client/server database development in three categories:
  2115.  
  2116.   + Client application development tools
  2117.   + Data access layers
  2118.   + Database servers
  2119.  
  2120. The eight page (20 Kbytes ASCII text) document includes explanations of the
  2121. three categories and describes over 30 products. Its sources are vendors'
  2122. product literature, industry periodicals, and discussions with users and
  2123. vendors' tech support staff. It is purely informational and contains no
  2124. propaganda, as the author is a neutral party.
  2125.  
  2126. To receive a copy electronically, email a request to: 
  2127. <maccsdb@external.umass.edu>.  Tell 'em the Mac Programming FAQ sent 
  2128. you!
  2129.  
  2130.    *12.2* Circumvention of Toolbox Limitations
  2131.  
  2132. 12.2.1) Q: Help!  The List Manager is driving me right UP THE WALL!  I 
  2133. want to throw it out and start over.  What'r my options?
  2134.  
  2135. A: The only non-framework List Manager replacement of which I'm aware is 
  2136. StoneTable, a commerical replacement library for the List Manager which 
  2137. provides many extensions including variable size columns/rows, 
  2138. column/row titles, different style/font/size/color per cell, sorting, 
  2139. edit in cell, and much more. <http://www.teleport.com/~stack>
  2140.  
  2141. TCL, PowerPlant, and MacApp all have their own integrated list 
  2142. management solutions.
  2143.  
  2144. *13* Dessert
  2145.  
  2146. 13.1) Q: Dessert? 
  2147.  
  2148. A: Honey Hill Farms Cookie Jar Frozen Yoghurt or Haagen-Dazs Raspberry & 
  2149. Cream Ice Cream.
  2150.  
  2151. Hokey-Pokey icecream with chocolate sauce and (for those who like their 
  2152. brain food firmer) Almond and Double Chocolate CookieTime cookies!?
  2153.  
  2154. *14* Contributors
  2155.  
  2156. [do let me know if I've forgotten you]
  2157.  
  2158. Denis Birnie
  2159. Liam Breck
  2160. Scott Bronson <urge@mcl.mcl.ucsb.edu>
  2161. Tim Converse <converse@cs.uchicago.edu>
  2162. Bob Hablutzel
  2163. Julian Harris
  2164. Bruce Hoult <Bruce@hoult.actrix.gen.nz>
  2165. Peter Jensen <pjensen@netcom.com>
  2166. Steve Jasik <macnosy@jasik.com>
  2167. Jonathan Kimmitt <jrrk@camcon.co.uk>
  2168. Matthias Neeracher <neeri@iis.ee.ethz.ch>
  2169. David P. Oster <oster@netcom.com>
  2170. Louis M. Pecora
  2171. Ingemar Ragnemalm <ingemar@lysator.liu.se>
  2172. Pete Resnick <resnick@uiuc.edu>
  2173. Phil Shapiro <phils@bedford.symantec.com>
  2174. eric slosser <slosser@apple.com>
  2175. M Sooriarachchi
  2176. Bill <stack@teleport.com>
  2177. Chris Thomas <thunderone@delphi.com>
  2178. Doug W. <dougw@as.arizona.edu>
  2179. Jon Watte <h+@austin.metrowerks.com>
  2180. Rick Zaccone <zaccone@bucknell.edu>
  2181.  
  2182. -- 
  2183.  -- Jon W{tte, h+@nada.kth.se, Mac Software Engineer Deluxe --
  2184.  
  2185.    There's no problem that can't be solved using brute-force algorithms
  2186.    and a sufficiently fast computer. Ergo, buy more hardware.    (NOT!)
  2187.