[comp.lang.c] ThinkC program--why wont it work?

EAO102@psuvm.psu.edu (Ernie Oporto) (03/06/91)

I have entered this program into Think C for compilation.  It appeared in the C
 Users Journal in the August 1990 issue.  When I try to compile it, I get
"t_window ..." and "t_windinfo not defined".  What should I do.


Each  ; char with a - over it indicates a tab.

/*** generic main event loop ***/


#include "WindowMgr.h"
#include "EventMgr.h"
#include "QuickDraw.h"

void mainevent()
{
   EventRecord event;

   while(1)  /* forever */
   {
      GetNextEvent(everyEvent,&event);

      switch (event.what)
      {
         case mouseDown:
            do_mousedown(event);
            break;
         case keyDown:
         case autoKey:
            do_keydown(event);
            break;
         case activateEvt:
            do_activate(event);
            break;
         case updateEvt:
            do_update(event);
            break;
         default:
            do_idle();
            break;
       }
   }
}

/*** window info structure ***/

typedef struct wind
{
   char dirty; /* contents saved? */
   char zoom;  /* window "zoomed"? */
   void **data; /* window contents */

/* window messages/methods */

   void(*activateproc)();
   void (*updateproc)();
   void (*keydownproc)();
   void (*contentproc)();
   void (*goawayproc)();
   void (*growproc)();
   void (*zoomproc)();
   void (*idleproc)();
   void (*disposeproc)();
   void (*cutproc)();
   void (*copyproc)();
   void (*pasteproc)();
   void (*clearproc)();
   void (*findproc)();
} WIND;

/*** text window creation function ***/

void create_t_window(title,wrect,closebox)
char *title;
Rect wrect;
int closebox;

{
/*    ...     */

/* allocate new window info */

   t_windinfo =
      (WIND *)NewPtr(sizeof(WIND));

/* allocate new window */

   t_window =
      NewWindow(NIL,&wrect,title,TRUE,8,FRONT,closebox,NIL);
/*     ...     */

/* insert window methods */

   t_windinfo->activateproc = t_activate;
   t_windinfo->updateproc = t_update;
   t_window->keydown = t_keydown;
/* etc... */

/* insert info into window refCon */

   SetWRefCon(t_window,t_windinfo);
}

/***text window activate method ***/

void t_activate(window,windinfo)
WindowPtr window;
WIND      *windinfo;
{
   TEHandle  tehandle;

      /* get data handle */

   tehandle =
      (TEHandle)windinfo->data;
         /* activate event */
   if(event.modifiers & activeFlag)
   {
      TEActive(tehandle);
      HiliteControl(((WindowPeek) window)
        ->controlList,ENABLE);
      TEFromScrap();
      enable_edit((**tehandle).selEnd
        - (**tehandle).selStart);
      enable_find();
      }
      else /* deactivate event */
      {
         TEDeactivate (tehandle);
         HiliteControl(((WindowPeek) window)
           ->controlList,DISABLE);
         ZeroScrap();
         TEToScrap();
         disable_edit():
         disable_find();
      }
}

/*** activate message dispatcher ***/

void do_activate(event)
EventRecord event;
{
   WindowPtr window;
   WIND   *windinfo;

   window = (WindowPtr)event.message;

   windinfo = (WIND *)GetWRefCon(window);

   (*windinfo->activateproc)(window,windinfo);
}

/*** Macintosh application ***/

main()
{
/* Mac specific initialization */

   init_mac();

/* set up application menus */

   setup_menus();

/*   etc...   */

/* call main event loop */

   mainevent();
}

EAO102@psuvm.psu.edu (Ernie Oporto) (03/07/91)

OK......here's more of a problem.
I inserted the following lines inside of the create_t_window function:
WIND *t_windinfo;
WindowPtr t_window;
That seemed to have cleared up my problems with their being declared, but
now ThinkC is telling me that my #included files can't be openned.
Now what am I doing wrong?  Below is the source code:

/*** generic main event loop ***/


#include "WindowMgr.h"
#include "EventMgr.h"
#include "QuickDraw.h"

void mainevent()
{
   EventRecord event;

   while(1)  /* forever */
   {
      GetNextEvent(everyEvent,&event);

      switch (event.what)
      {
         case mouseDown:
            do_mousedown(event);
            break;
         case keyDown:
         case autoKey:
            do_keydown(event);
            break;
         case activateEvt:
            do_activate(event);
            break;
         case updateEvt:
            do_update(event);
            break;
         default:
            do_idle();
            break;
      }
   }
}

/*** window info structure ***/

typedef struct wind
{
   char dirty; /* contents saved? */
   char zoom;  /* window "zoomed"? */
   void **data; /* window contents */

/* window messages/methods */

   void (*activateproc)();
   void (*updateproc)();
   void (*keydownproc)();
   void (*contentproc)();
   void (*goawayproc)();
   void (*growproc)();
   void (*zoomproc)();
   void (*idleproc)();
   void (*disposeproc)();
   void (*cutproc)();
   void (*copyproc)();
   void (*pasteproc)();
   void (*clearproc)();
   void (*findproc)();
} WIND;

/*** text window creation function ***/

void create_t_window(title,wrect,closebox)
char *title;
Rect wrect;
int closebox;

{
/*    ...     */
WIND *t_windinfo;
WindowPtr t_window;

/* allocate new window info */

   t_windinfo =
      (WIND *)NewPtr(sizeof(WIND));

/* allocate new window */

   t_window =
      NewWindow(NIL,&wrect,title,TRUE,8,FRONT,closebox,NIL);
/*     ...     */

/* insert window methods */

   t_windinfo->activateproc = t_activate;
   t_windinfo->updateproc = t_update;
   t_window->keydown = t_keydown;
/* etc... */

/* insert info into window refCon */

   SetWRefCon(t_window,t_windinfo);
}

/***text window activate method ***/

void t_activate(window,windinfo)
WindowPtr window;
WIND      *windinfo;
{
   TEHandle  tehandle;

      /* get data handle */

   tehandle =
      (TEHandle)windinfo->data;
         /* activate event */
   if(event.modifiers & activeFlag)
   {
      TEActive(tehandle);
      HiliteControl(((WindowPeek) window)
        ->controlList,ENABLE);
      TEFromScrap();
      enable_edit((**tehandle).selEnd
        - (**tehandle).selStart);
      enable_find();
    }
    else /* deactivate event */
    {
         TEDeactivate (tehandle);
         HiliteControl(((WindowPeek) window)
           ->controlList,DISABLE);
         ZeroScrap();
         TEToScrap();
         disable_edit():
         disable_find();
    }
}

/*** activate message dispatcher ***/

void do_activate(event)
EventRecord event;
{
   WindowPtr window;
   WIND    *windinfo;

   window = (WindowPtr)event.message;

   windinfo = (WIND *)GetWRefCon(window);

   (*windinfo->activateproc)(window,windinfo);
}

/*** Macintosh application ***/

main()
{
/* Mac specific initialization */

   init_mac();

/* set up application menus */

   setup_menus();

/*   etc...   */

/* call main event loop */

   mainevent();
}

jbr0@cbnews.att.com (joseph.a.brownlee) (03/07/91)

In article <91064.120155EAO102@psuvm.psu.edu> EAO102@psuvm.psu.edu (Ernie
Oporto) writes:
> I have entered this program into Think C for compilation.  It appeared in the
> C Users Journal in the August 1990 issue.  When I try to compile it, I get
> "t_window ..." and "t_windinfo not defined".

That's because they aren't defined.

>                                              What should I do.

Define them.

> [...]
>
>/*** text window creation function ***/
>
>void create_t_window(title,wrect,closebox)
>char *title;
>Rect wrect;
>int closebox;
>{
>/*    ...     */

       ^^^ See that?  They are telling you they left stuff out.  You need to
define t_window and t_windinfo here.  You can easily determine the types from
the following code:

>
>/* allocate new window info */
>
>   t_windinfo = (WIND *)NewPtr(sizeof(WIND));
>
>/* allocate new window */
>
>   t_window = NewWindow(NIL,&wrect,title,TRUE,8,FRONT,closebox,NIL);

The correct definitions are:

	WIND *		t_windinfo;
	WindowPtr	t_window;

Note also that by using "NIL" as your first argument, you are not allocating
space to store the window record.  This will often work, but you then risk
heap problems.  It is very easy to allocate the storage yourself; see Inside
Macintosh I (Window Manager) for details.

In general, I hate examples that leave out definitions.  Novices are bound
to be confused by stuff like this, and they're the ones most likely to be
looking for examples.

-- 
   -      _   Joe Brownlee, Analysts International Corp. @ AT&T Network Systems
  /_\  @ / `  471 E Broad St, Suite 1610, Columbus, Ohio 43215   (614) 860-7461
 /   \ | \_,  E-mail: jbr@cblph.att.com     Who pays attention to what _I_ say?
 "Scotty, we need warp drive in 3 minutes or we're all dead!" --- James T. Kirk

thornley@cs.umn.edu (David H. Thornley) (03/08/91)

In article <91065.143433EAO102@psuvm.psu.edu> EAO102@psuvm.psu.edu (Ernie Oporto) writes:
>OK......here's more of a problem.
>I inserted the following lines inside of the create_t_window function:
>WIND *t_windinfo;
>WindowPtr t_window;
>That seemed to have cleared up my problems with their being declared, but
>now ThinkC is telling me that my #included files can't be openned.
>Now what am I doing wrong?  Below is the source code:
>
One thing you are doing wrong is using #include "WindowMgr.h" rather
than the more reliable #include <WindowMgr.h>.  You also have to make
sure that the #include files are in the folder that includes THINK C,
or in a subfolder of that.

You are also not following the usual guidelines for posting.

First, we use a rejoinder RTFM, which is an acronym for "Read The
Manual".  In this case, you don't seem to have studied the
documentation.  There is also a comment RTFAQ, which refers
to the "Frequently Asked Question" list that is posted here
monthly.  If you intend to use C much, and are not thoroughly
familiar with it (and you apparently aren't), I would suggest
reading the FAQ carefully.  There are a lot of good solutions
to common problems there.

Second, it is usually best not to post program fragments this long.
You should try to reproduce your problem in as few lines as possible.

The reason behind these two is that it makes things easier on
everyone.  When you post a question, it should be something
that is giving you trouble that can't easily be resolved by
yourself, and it should be easy to look at so people can try
to see what is going wrong.  There are plenty of really smart
people who post here, and who will generally take a crack at
problems if you give them the impression that it is worth it.
If you don't care enough to RTFM, or to trim the program causing
the problem down to manageable size, why should they care to
solve the problem?

Third, you should be more specific.  What version of THINK C are you
using?  How was it installed?  Is it all on a hard disk or are you
operating off floppies?  You might also want to mention the computer
and system being used, since it might make a difference.  (If you
try running a program compiled with 68020 and 68881 options on a
vanilla SE, it won't work.)

Fourth, you are in danger of being flamed for posting a question
on a specific C implementation; some will probably insist that
this be posted on comp.sys.mac.programmer, which isn't a bad group
to consider for THINK C problems.  On the other hand, lots of other
people do it for other systems (ms-dos, unix, whatever).  It is best
to post questions like "How do I manage Mac menus" on the system-
specific group, posting only questions about C (like this one) here.

This is not intended as a flame, but as suggestions for getting
the best use out of this group.  Happy computing!

DHT