/*-- AutoRev header do NOT edit!
*
*   Program         :   GenC.c
*   Copyright       :   © Copyright 1991 Jaba Development
*   Author          :   Jan van den Baard
*   Creation Date   :   20-Oct-91
*   Current version :   1.00
*   Translator      :   DICE v2.6
*
*   REVISION HISTORY
*
*   Date          Version         Comment
*   ---------     -------         ------------------------------------------
*   20-Oct-91     1.00            C Source Generator.
*
*-- REV_END --*/

#include "GTEd.h"
#include "Protos.h"

extern struct MemoryChain       *Chain;
extern UBYTE                     MainFontName[ 80 ];
extern struct TextAttr           MainFont;
extern struct Screen            *MainScreen;
extern struct Window            *MainWindow;
extern UBYTE                     MainFileName[ 512 ];
extern UWORD                     MainDriPen[ NUMDRIPENS + 1 ];
extern struct ColorSpec          MainColors[ 33 ];
extern UBYTE                     MainScreenTitle[ 80 ];
extern UBYTE                     MainWindowTitle[ 80 ];
extern struct TagItem            nwTags[];
extern struct TagItem            MainSTags[];
extern struct ExtGadgetList      Gadgets;
extern UWORD                     ActiveKind;
extern struct Prefs              MainPrefs;
extern BOOL                      Saved;
extern struct NewMenu            Menus[];
extern struct IntuiText         *WindowTxt;
extern BOOL                      ws_InnerW, ws_InnerH, ws_ZoomF, ws_MQueue;
extern BOOL                      ws_RQueue, ws_Adjust, cs_AutoScroll;
extern WORD                      ws_IWidth, ws_IHeight, ws_ZLeft, ws_ZTop;
extern WORD                      ws_ZWidth, ws_ZHeight, ws_MQue, ws_RQue;
extern UWORD                     cs_ScreenType;
extern ULONG                     WindowFlags, WindowIDCMP;
extern struct ExtMenuList        ExtMenus;

struct FileRequester            *gc_GenC = 0l;

UBYTE                            gc_CPatt[32]  = "#?.c";
UBYTE                            gc_CFile[32]  = "unnamed.c";
UBYTE                            gc_CPath[256];

UWORD                            gc_TagOffset, gc_GadOffset, gc_ScreenOffset;

struct TagItem                   gc_CTags[] = {
    ASL_Hail,                   "Save C Source As...",
    ASL_Window,                 0l,
    ASL_File,                   gc_CFile,
    ASL_Dir,                    gc_CPath,
    ASL_Pattern,                gc_CPatt,
    ASL_OKText,                 "Save",
    ASL_FuncFlags,              FILF_SAVE | FILF_PATGAD,
    TAG_DONE };

UBYTE                           *gc_Kinds[] = {
    "GENERIC_KIND", "BUTTON_KIND", "CHECKBOX_KIND",
    "INTEGER_KIND", "LISTVIEW_KIND", "MX_KIND", "NUMBER_KIND",
    "CYCLE_KIND", "PALETTE_KIND", "SCROLLER_KIND", "RESERVED",
    "SLIDER_KIND", "STRING_KIND", "TEXT_KIND" };

UBYTE                           *gc_Types[] = {
    "NM_END", "NM_TITLE", "NM_ITEM", "NM_SUB" };

#define STAT                    ( MainPrefs.pr_PrefFlags0 & PRF_STATIC ) == PRF_STATIC

/*
 * --- Write placement flags.
 */
void WritePlaceFlags( BPTR file, long flags, long mode )
{
    UBYTE   c;

    if ( mode ) c = '!';
    else        c = '|';

    if ( NOT flags )  {
        MyFPrintf( file, "0;\n" );
        return;
    }

    if (( flags & PLACETEXT_LEFT ) == PLACETEXT_LEFT )
        MyFPrintf( file, "PLACETEXT_LEFT%lc", c );
    else if (( flags & PLACETEXT_RIGHT ) == PLACETEXT_RIGHT )
        MyFPrintf( file, "PLACETEXT_RIGHT%lc", c );
    else if (( flags & PLACETEXT_ABOVE ) == PLACETEXT_ABOVE )
        MyFPrintf( file, "PLACETEXT_ABOVE%lc", c );
    else if (( flags & PLACETEXT_BELOW ) == PLACETEXT_BELOW )
        MyFPrintf( file, "PLACETEXT_BELOW%lc", c );
    else if (( flags & PLACETEXT_IN ) == PLACETEXT_IN )
        MyFPrintf( file, "PLACETEXT_IN%lc", c );
    if (( flags & NG_HIGHLABEL ) == NG_HIGHLABEL )
        MyFPrintf( file, "NG_HIGHLABEL%lc", c );

    Seek( file, -1l, OFFSET_CURRENT );
    MyFPrintf( file, ";\n" );
}

/*
 * --- Write DisplayID flags.
 */
void WriteIDFlags( BPTR file, long flags, long mode )
{
    UBYTE   c;

    if ( mode ) c = '!';
    else        c = '|';

    if (( flags & PAL_MONITOR_ID ) == PAL_MONITOR_ID )
        MyFPrintf( file, "PAL_MONITOR_ID%lc", c );
    else if (( flags & NTSC_MONITOR_ID ) == NTSC_MONITOR_ID )
        MyFPrintf( file, "NTSC_MONITOR_ID%lc", c );
    else
        MyFPrintf( file, "DEFAULT_MONITOR_ID%lc", c );

    if (( flags & SUPERLACE_KEY ) == SUPERLACE_KEY )
        MyFPrintf( file, "SUPERLACE_KEY%lc", c );
    else if (( flags & HIRESLACE_KEY ) == HIRESLACE_KEY )
        MyFPrintf( file, "HIRESLACE_KEY%lc", c );
    else if (( flags & LORESLACE_KEY ) == LORESLACE_KEY )
        MyFPrintf( file, "LORESLACE_KEY%lc", c );
    else if (( flags & SUPER_KEY ) == SUPER_KEY )
        MyFPrintf( file, "SUPER_KEY%lc", c );
    else if (( flags & HIRES_KEY ) == HIRES_KEY )
        MyFPrintf( file, "HIRES_KEY%lc", c );
    else
        MyFPrintf( file, "LORES_KEY%lc", c );

    Seek( file, -1l, OFFSET_CURRENT );
    MyFPrintf( file, ",\n" );
}

/*
 * --- Write the IntuiText drawmode flags.
 */
void WriteCDrMd( BPTR file, long drmd, long mode )
{
    if (( drmd & JAM2 ) == JAM2 ) {
        if ( mode ) MyFPrintf( file, "RP_JAM2!" );
        else        MyFPrintf( file, "JAM2|" );
    } else {
        if ( mode ) MyFPrintf( file, "RP_JAM1!" );
        else        MyFPrintf( file, "JAM1|" );
    }

    if (( drmd & COMPLEMENT ) == COMPLEMENT ) {
        if ( mode ) MyFPrintf( file, "RP_COMPLEMENT!" );
        else        MyFPrintf( file, "COMPLEMENT|" );
    }

    if (( drmd & INVERSVID ) == INVERSVID ) {
        if ( mode ) MyFPrintf( file, "RP_INVERSVID!" );
        else        MyFPrintf( file, "INVERSVID|" );
    }

    Seek( file, -1l, OFFSET_CURRENT );
    MyFPrintf( file, "," );
}

/*
 * --- Write IDCMP flags.
 */
void WriteIDCMPFlags( BPTR file, long idcmp, long mode )
{
    UBYTE   c;

    if ( mode ) c = '!';
    else        c = '|';

    if ( NOT idcmp ) {
        MyFPrintf( file, "NULL,\n" );
        return;
    }

    if (( idcmp & IDCMP_SIZEVERIFY ) == IDCMP_SIZEVERIFY )
        MyFPrintf( file, "IDCMP_SIZEVERIFY%lc", c );
    if (( idcmp & IDCMP_NEWSIZE ) == IDCMP_NEWSIZE )
        MyFPrintf( file, "IDCMP_NEWSIZE%lc", c );
    if (( idcmp & IDCMP_REFRESHWINDOW ) == IDCMP_REFRESHWINDOW )
        MyFPrintf( file, "IDCMP_REFRESHWINDOW%lc", c );
    if (( idcmp & IDCMP_MOUSEBUTTONS ) == IDCMP_MOUSEBUTTONS )
        MyFPrintf( file, "IDCMP_MOUSEBUTTONS%lc", c );
    if (( idcmp & IDCMP_MOUSEMOVE ) == IDCMP_MOUSEMOVE )
        MyFPrintf( file, "IDCMP_MOUSEMOVE%lc", c );
    if (( idcmp & IDCMP_GADGETDOWN ) == IDCMP_GADGETDOWN )
        MyFPrintf( file, "IDCMP_GADGETDOWN%lc", c );
    if (( idcmp & IDCMP_GADGETUP ) == IDCMP_GADGETUP )
        MyFPrintf( file, "IDCMP_GADGETUP%lc", c );
    if (( idcmp & IDCMP_REQSET ) == IDCMP_REQSET )
        MyFPrintf( file, "IDCMP_REQSET%lc", c );
    if (( idcmp & IDCMP_MENUPICK ) == IDCMP_MENUPICK )
        MyFPrintf( file, "IDCMP_MENUPICK%lc", c );
    if (( idcmp & IDCMP_CLOSEWINDOW ) == IDCMP_CLOSEWINDOW )
        MyFPrintf( file, "IDCMP_CLOSEWINDOW%lc", c );
    if (( idcmp & IDCMP_RAWKEY ) == IDCMP_RAWKEY )
        MyFPrintf( file, "IDCMP_RAWKEY%lc", c );
    if (( idcmp & IDCMP_REQVERIFY ) == IDCMP_REQVERIFY )
        MyFPrintf( file, "IDCMP_REQVERIFY%lc", c );
    if (( idcmp & IDCMP_REQCLEAR ) == IDCMP_REQCLEAR )
        MyFPrintf( file, "IDCMP_REQCLEAR%lc", c );
    if (( idcmp & IDCMP_MENUVERIFY ) == IDCMP_MENUVERIFY )
        MyFPrintf( file, "IDCMP_MENUVERIFY%lc\n", c );
    if (( idcmp & IDCMP_NEWPREFS ) == IDCMP_NEWPREFS )
        MyFPrintf( file, "IDCMP_NEWPREFS%lc", c );
    if (( idcmp & IDCMP_DISKINSERTED ) == IDCMP_DISKINSERTED )
        MyFPrintf( file, "IDCMP_DISKINSERTED%lc", c );
    if (( idcmp & IDCMP_DISKREMOVED ) == IDCMP_DISKREMOVED )
        MyFPrintf( file, "IDCMP_DISKREMOVED%lc", c );
    if (( idcmp & IDCMP_ACTIVEWINDOW ) == IDCMP_ACTIVEWINDOW )
        MyFPrintf( file, "IDCMP_ACTIVEWINDOW%lc", c );
    if (( idcmp & IDCMP_INACTIVEWINDOW ) == IDCMP_INACTIVEWINDOW )
        MyFPrintf( file, "IDCMP_INACTIVEWINDOW%lc", c );
    if (( idcmp & IDCMP_DELTAMOVE ) == IDCMP_DELTAMOVE )
        MyFPrintf( file, "IDCMP_DELTAMOVE%lc", c );
    if (( idcmp & IDCMP_VANILLAKEY ) == IDCMP_VANILLAKEY )
        MyFPrintf( file, "IDCMP_VANILLAKEY%lc", c );
    if (( idcmp & IDCMP_INTUITICKS ) == IDCMP_INTUITICKS )
        MyFPrintf( file, "IDCMP_INTUITICKS%lc", c );
    if (( idcmp & IDCMP_IDCMPUPDATE ) == IDCMP_IDCMPUPDATE )
        MyFPrintf( file, "IDCMP_IDCMPUPDATE%lc", c );
    if (( idcmp & IDCMP_MENUHELP ) == IDCMP_MENUHELP )
        MyFPrintf( file, "IDCMP_MENUHELP%lc", c );
    if (( idcmp & IDCMP_CHANGEWINDOW ) == IDCMP_CHANGEWINDOW )
        MyFPrintf( file, "IDCMP_CHANGEWINDOW%lc", c );
    MyFPrintf( file, "IDCMP_REFRESHWINDOW%lc", c );
    Seek( file, -1l, OFFSET_CURRENT );
    MyFPrintf( file, ",\n" );
}

/*
 * --- Write window flags.
 */
void WriteWindowFlags( BPTR file, long flags, long mode )
{
    UBYTE   c;

    if ( mode ) c = '!';
    else        c = '|';

    if (( flags & WFLG_SIZEGADGET ) == WFLG_SIZEGADGET )
        MyFPrintf( file, "WFLG_SIZEGADGET%lc", c );
    if (( flags & WFLG_DRAGBAR ) == WFLG_DRAGBAR )
        MyFPrintf( file, "WFLG_DRAGBAR%lc", c );
    if (( flags & WFLG_DEPTHGADGET ) == WFLG_DEPTHGADGET )
        MyFPrintf( file, "WFLG_DEPTHGADGET%lc", c );
    if (( flags & WFLG_CLOSEGADGET ) == WFLG_CLOSEGADGET )
        MyFPrintf( file, "WFLG_CLOSEGADGET%lc", c );
    if (( flags & WFLG_SIZEBRIGHT ) == WFLG_SIZEBRIGHT )
        MyFPrintf( file, "WFLG_SIZEBRIGHT%lc", c );
    if (( flags & WFLG_SIZEBBOTTOM ) == WFLG_SIZEBBOTTOM )
        MyFPrintf( file, "WFLG_SIZEBBOTTOM%lc", c );
    if (( flags & WFLG_SMART_REFRESH ) == WFLG_SMART_REFRESH )
        MyFPrintf( file, "WFLG_SMART_REFRESH%lc", c );
    if (( flags & WFLG_SIMPLE_REFRESH ) == WFLG_SIMPLE_REFRESH )
        MyFPrintf( file, "WFLG_SIMPLE_REFRESH%lc", c );
    if (( flags & WFLG_SUPER_BITMAP ) == WFLG_SUPER_BITMAP )
        MyFPrintf( file, "WFLG_SUPER_BITMAP%lc", c );
    if (( flags & WFLG_OTHER_REFRESH ) == WFLG_OTHER_REFRESH )
        MyFPrintf( file, "WFLG_OTHER_REFRESH%lc", c );
    if (( flags & WFLG_BACKDROP ) == WFLG_BACKDROP )
        MyFPrintf( file, "WFLG_BACKDROP%lc", c );
    if (( flags & WFLG_REPORTMOUSE ) == WFLG_REPORTMOUSE )
        MyFPrintf( file, "WFLG_REPORTMOUSE%lc", c );
    if (( flags & WFLG_GIMMEZEROZERO ) == WFLG_GIMMEZEROZERO )
        MyFPrintf( file, "WFLG_GIMMEZEROZERO%lc", c );
    if (( flags & WFLG_BORDERLESS ) == WFLG_BORDERLESS )
        MyFPrintf( file, "WFLG_BORDERLESS%lc", c );
    if (( flags & WFLG_ACTIVATE ) == WFLG_ACTIVATE )
        MyFPrintf( file, "WFLG_ACTIVATE%lc", c );
    if (( flags & WFLG_RMBTRAP ) == WFLG_RMBTRAP )
        MyFPrintf( file, "WFLG_RMBTRAP%lc", c );

    Seek( file, -1l, OFFSET_CURRENT );
    MyFPrintf( file, ",\n" );
}

/*
 * --- Write a single NewMenu structure.
 */
void WriteCNewMenu( BPTR file, struct ExtNewMenu *menu )
{
    ULONG flags;

    MyFPrintf( file, "    %s, ", gc_Types[ menu->em_NewMenu.nm_Type ] );
    if ( menu->em_NewMenu.nm_Label != NM_BARLABEL )
        MyFPrintf( file, "\"%s\", ", menu->em_NodeName );
    else {
        MyFPrintf( file, "NM_BARLABEL, 0l, 0, 0l, 0l,\n" );
        return;
    }
    if ( menu->em_NewMenu.nm_CommKey )
        MyFPrintf( file, "\"%s\", ", &menu->em_ShortCut[0] );
    else
        MyFPrintf( file, "0l, " );
    if ( flags = menu->em_NewMenu.nm_Flags ) {
        if ( menu->em_NewMenu.nm_Type == NM_TITLE ) {
            if (( flags & NM_MENUDISABLED ) == NM_MENUDISABLED )
                MyFPrintf( file, "NM_MENUDISABLED|" );
        }  else {
            if (( flags & NM_ITEMDISABLED ) == NM_ITEMDISABLED )
                MyFPrintf( file, "NM_ITEMDISABLED|" );
        }
        if (( flags & CHECKIT ) == CHECKIT )
            MyFPrintf( file, "CHECKIT|" );
        if (( flags & CHECKED ) == CHECKED )
            MyFPrintf( file, "CHECKED|" );
        if (( flags & MENUTOGGLE ) == MENUTOGGLE )
            MyFPrintf( file, "MENUTOGGLE|" );

        Seek( file, -1l, OFFSET_CURRENT );
        MyFPrintf( file, ", " );
    } else
        MyFPrintf( file, "0, " );
    MyFPrintf( file, "%ld, 0l,\n", menu->em_NewMenu.nm_MutualExclude );
}

/*
 * --- Write the NewMenu structures.
 */
void WriteCMenus( BPTR file )
{
    struct ExtNewMenu   *menu, *item, *sub;

    if ( NOT ExtMenus.ml_First->em_Next )
        return;

    if ( STAT ) MyFPrintf( file, "static " );

    MyFPrintf( file, "struct NewMenu %sNewMenu[] = {\n", &MainPrefs.pr_ProjectPrefix[0] );

    for ( menu = ExtMenus.ml_First; menu->em_Next; menu = menu->em_Next ) {
        WriteCNewMenu( file, menu );
        if ( menu->em_Items ) {
            for ( item = menu->em_Items->ml_First;  item->em_Next; item = item->em_Next ) {
                WriteCNewMenu( file, item );
                if ( item->em_Items ) {
                    for ( sub = item->em_Items->ml_First;  sub->em_Next; sub = sub->em_Next )
                        WriteCNewMenu( file, sub );
                }
            }
        }
    }
    MyFPrintf( file, "    NM_END, 0l, 0l, 0, 0l, 0l };\n\n" );
}

/*
 * --- Write the GadgetID defines.
 */
void WriteCID( BPTR file )
{
    struct ExtNewGadget *eng;

    Renumber();

    for ( eng = Gadgets.gl_First; eng->en_Next; eng = eng->en_Next )
        MyFPrintf( file, "#define GD_%-32s    %ld\n", &eng->en_SourceLabel[0], eng->en_NewGadget.ng_GadgetID );
    MyFPrintf( file, "\n" );
}

/*
 * --- Write the necessary globals.
 */
void WriteCGlob( BPTR file )
{
    if ( STAT ) MyFPrintf( file, "static " );
    MyFPrintf( file, "struct Window        *%sWnd        = 0l;\n", &MainPrefs.pr_ProjectPrefix[0] );
    if ( STAT ) MyFPrintf( file, "static " );
    MyFPrintf( file, "struct Screen        *%sScr        = 0l;\n", &MainPrefs.pr_ProjectPrefix[0] );
    if ( STAT ) MyFPrintf( file, "static " );
    MyFPrintf( file, "APTR                  %sVisualInfo = 0l;\n", &MainPrefs.pr_ProjectPrefix[0] );
    if ( Gadgets.gl_First->en_Next ) {
        if ( STAT ) MyFPrintf( file, "static " );
        MyFPrintf( file, "struct Gadget        *%sGList      = 0l;\n", &MainPrefs.pr_ProjectPrefix[0] );
        if ( STAT ) MyFPrintf( file, "static " );
        MyFPrintf( file, "struct Gadget        *%sGadgets[%ld];\n", &MainPrefs.pr_ProjectPrefix[0], CountGadgets());
    }
    if ( ExtMenus.ml_First->em_Next ) {
        if ( STAT ) MyFPrintf( file, "static " );
        MyFPrintf( file, "struct Menu          *%sMenus      = 0l;\n", &MainPrefs.pr_ProjectPrefix[0] );
    }
    if ( ws_ZoomF ) {
        if ( STAT ) MyFPrintf( file, "static " );
        MyFPrintf( file, "UWORD                 %sZoom[4]    = { %ld, %ld, %ld, %ld };\n", &MainPrefs.pr_ProjectPrefix[0], ws_ZLeft, ws_ZTop, ws_ZWidth, ws_ZHeight );
    }
    MyFPrintf( file, "\n" );
}

/*
 * --- Write the Cycle and Mx lables.
 */
void WriteCLabels( BPTR file )
{
    struct ExtNewGadget *eng;
    UWORD                i;

    for ( eng = Gadgets.gl_First; eng->en_Next; eng = eng->en_Next ) {
        if ( eng->en_Kind == CYCLE_KIND || eng->en_Kind == MX_KIND ) {
            if ( STAT ) MyFPrintf( file, "static " );
            MyFPrintf( file, "UBYTE         *%sLabels[] = {\n", &eng->en_SourceLabel[0] );
            for ( i = 0; i < 24; i++ ) {
                if ( eng->en_Labels[ i ] )
                    MyFPrintf( file, "    \"%s\",\n", eng->en_Labels[ i ] );
            }
            MyFPrintf( file, "    0l };\n\n" );
        }
    }
}

/*
 * --- Write a single ListView Node.
 */
void WriteCNode( BPTR file, struct ExtNewGadget *eng, struct ListViewNode *node, WORD num )
{
    if ( node->ln_Succ != ( struct ListViewNode * )&eng->en_Entries.lh_Tail )
        MyFPrintf( file, "    &%sNodes[%ld], ", &eng->en_SourceLabel[0], num + 1 );
    else
        MyFPrintf( file, "    ( struct Node * )&%sList.mlh_Tail, ", &eng->en_SourceLabel[0] );

    if ( node->ln_Pred == ( struct ListViewNode * )&eng->en_Entries )
        MyFPrintf( file, "( struct Node * )&%sList.mlh_Head, ", &eng->en_SourceLabel[0] );
    else
        MyFPrintf( file, "&%sNodes[%ld], ", &eng->en_SourceLabel[0], num - 1 );

    MyFPrintf( file, "0, 0, \"%s\",\n", &node->ln_NameBytes[0] );
}

/*
 * --- Write the ListView entries.
 */
void WriteCList( BPTR file )
{
    struct ExtNewGadget *eng;
    struct ListViewNode *node, *action;
    WORD                 nodenum;

    for ( eng = Gadgets.gl_First; eng->en_Next; eng = eng->en_Next ) {
        if ( eng->en_Kind == LISTVIEW_KIND ) {

            action = ( struct ListViewNode * )RemHead( &eng->en_Entries );

            if ( eng->en_Entries.lh_Head->ln_Succ ) {
                MyFPrintf( file, "extern struct MinList %sList;\n\n", &eng->en_SourceLabel[0] );
                if ( STAT ) MyFPrintf( file, "static " );
                MyFPrintf( file, "struct Node    %sNodes[] = {\n", &eng->en_SourceLabel[0] );
                for ( node = eng->en_Entries.lh_Head, nodenum = 0; node->ln_Succ; node = node->ln_Succ, nodenum++ )
                    WriteCNode( file, eng, node, nodenum );
                Seek( file, -2, OFFSET_CURRENT );
                MyFPrintf( file, " };\n\n" );
                MyFPrintf( file, "struct MinList %sList = {\n", &eng->en_SourceLabel[0] );
                MyFPrintf( file, "    &%sNodes[0], ( struct Node * )0l, &%sNodes[%ld] };\n\n", &eng->en_SourceLabel[0], &eng->en_SourceLabel[0], nodenum -1 );
            }
            AddHead( &eng->en_Entries, ( struct Node * )action );
        }
    }
}

/*
 * --- Write the TextAttr structure
 */
void WriteCTextAttr( BPTR file )
{
    struct ExtNewGadget *eng;
    UBYTE                fname[32], *ptr;

    strcpy( fname, MainFontName );

    ptr = strchr( fname, '.' );
    *ptr = 0;

    if ( STAT ) MyFPrintf( file, "static " );
    MyFPrintf( file, "struct TextAttr %s%ld = {\n", fname, MainFont.ta_YSize );
    MyFPrintf( file, "    ( STRPTR )\"%s\", %ld, 0x%02lx, 0x%02lx };\n\n", MainFontName, MainFont.ta_YSize, MainFont.ta_Style, MainFont.ta_Flags );
}

/*
 * --- Write the Window Tags.
 */
void WriteCWTags( BPTR file )
{
    gc_TagOffset = 0;

    if ( STAT ) MyFPrintf( file, "static " );
    MyFPrintf( file, "struct TagItem    %sWindowTags[] = {\n", &MainPrefs.pr_ProjectPrefix[0] );

    if ( MyTagInArray( WA_Left, nwTags ))
    {   MyFPrintf( file, "    WA_Left,          %ld,\n", GetTagData( WA_Left, 0l, nwTags )); gc_TagOffset++; }
    if ( MyTagInArray( WA_Top, nwTags ))
    {   MyFPrintf( file, "    WA_Top,           %ld,\n", GetTagData( WA_Top, 0l, nwTags )); gc_TagOffset++; }
    if ( ws_InnerW )
    {   MyFPrintf( file, "    WA_InnerWidth,    %ld,\n", ws_IWidth ); gc_TagOffset++;}
    else
    {   MyFPrintf( file, "    WA_Width,         %ld,\n", MainWindow->Width); gc_TagOffset++; }
    if ( ws_InnerH )
    {   MyFPrintf( file, "    WA_InnerHeight,   %ld,\n", ws_IHeight ); gc_TagOffset++; }
    else
    {   MyFPrintf( file, "    WA_Height,        %ld,\n", MainWindow->Height); gc_TagOffset++; }
    if ( MyTagInArray( WA_DetailPen, nwTags ))
    {   MyFPrintf( file, "    WA_DetailPen,     %ld,\n", GetTagData( WA_DetailPen, 0l, nwTags )); gc_TagOffset++; }
    if ( MyTagInArray( WA_BlockPen, nwTags ))
    {   MyFPrintf( file, "    WA_BlockPen,      %ld,\n", GetTagData( WA_BlockPen, 0l, nwTags )); gc_TagOffset++; }
    GetGadgetIDCMP();
    MyFPrintf( file, "    WA_IDCMP,         " );
    WriteIDCMPFlags( file, WindowIDCMP, 0l);
    gc_TagOffset++;
    if ( MyTagInArray( WA_Flags, nwTags ))
    {   MyFPrintf( file, "    WA_Flags,         " );
        WriteWindowFlags( file, GetTagData( WA_Flags, 0l, nwTags ), 0l);
        gc_TagOffset++;
    }
    if ( Gadgets.gl_First->en_Next ) {
        MyFPrintf( file, "    WA_Gadgets,       0l,\n" );
        gc_GadOffset = gc_TagOffset;
        gc_TagOffset++;
    }
    if ( strlen( MainWindowTitle ))
    {   MyFPrintf( file, "    WA_Title,         \"%s\",\n", MainWindowTitle ); gc_TagOffset++; }
    if ( strlen( MainScreenTitle ))
    {   MyFPrintf( file, "    WA_ScreenTitle,   \"%s\",\n", MainScreenTitle ); gc_TagOffset++; }
    if ( cs_ScreenType ) {
        if ( cs_ScreenType == 2 )
            MyFPrintf( file, "    WA_CustomScreen,  0l,\n" );
        if ( cs_ScreenType == 1 )
            MyFPrintf( file, "    WA_PubScreen,     0l,\n" );
        gc_ScreenOffset = gc_TagOffset;
    }
    if ( MyTagInArray( WA_MinWidth, nwTags ))
        MyFPrintf( file, "    WA_MinWidth,      %ld,\n", GetTagData( WA_MinWidth, 0l, nwTags ));
    if ( MyTagInArray( WA_MinHeight, nwTags ))
        MyFPrintf( file, "    WA_MinHeight,     %ld,\n", GetTagData( WA_MinHeight, 0l, nwTags ));
    if ( MyTagInArray( WA_MaxWidth, nwTags ))
        MyFPrintf( file, "    WA_MaxWidth,      %ld,\n", GetTagData( WA_MaxWidth, 0l, nwTags ));
    if ( MyTagInArray( WA_MaxHeight, nwTags ))
        MyFPrintf( file, "    WA_MaxHeight,     %ld,\n", GetTagData( WA_MaxHeight, 0l, nwTags ));
    if ( ws_ZoomF )
        MyFPrintf( file, "    WA_Zoom,          (Tag)%sZoom,\n", &MainPrefs.pr_ProjectPrefix[0] );
    if ( ws_MQueue )
        MyFPrintf( file, "    WA_MouseQueue,    %ld,\n", ws_MQue);
    if ( ws_RQueue )
        MyFPrintf( file, "    WA_RptQueue,      %ld,\n", ws_RQue );
    if ( ws_Adjust )
        MyFPrintf( file, "    WA_AutoAdjust,    1l,\n" );

    MyFPrintf( file, "    TAG_DONE };\n\n" );
}

/*
 * --- Write the Screen Tags and screen specific data.
 */
void WriteCSTags( BPTR file )
{
    UWORD           cnt;
    UBYTE           fname[32], *ptr;

    strcpy( fname, MainFontName );

    ptr = strchr( fname, '.' );
    *ptr = 0;

    if ( STAT ) MyFPrintf( file, "static " );
    MyFPrintf( file, "struct ColorSpec  %sScreenColors[] = {\n", &MainPrefs.pr_ProjectPrefix[0] );

    for ( cnt = 0; cnt < 33; cnt++ ) {
        if ( MainColors[ cnt ].ColorIndex != ~0 )
            MyFPrintf( file, "    %2ld, 0x%02lx, 0x%02lx, 0x%02lx,\n", MainColors[ cnt ].ColorIndex, MainColors[ cnt ].Red, MainColors[ cnt ].Green, MainColors[ cnt ].Blue );
        else {
            MyFPrintf( file, "    ~0, 0x00, 0x00, 0x00 };\n\n" );
            break;
        }
    }

    if ( STAT ) MyFPrintf( file, "static " );
    MyFPrintf( file, "UWORD             %sDriPens[] = {\n    ", &MainPrefs.pr_ProjectPrefix[0] );

    for ( cnt = 0; cnt < NUMDRIPENS + 1; cnt++ ) {
        if ( MainDriPen[ cnt ] != ~0 )
            MyFPrintf( file, "%ld,", MainDriPen[ cnt ] );
        else {
            MyFPrintf( file, "~0 };\n\n" );
            break;
        }
    }

    if ( STAT ) MyFPrintf( file, "static " );
    MyFPrintf( file, "struct TagItem    %sScreenTags[] = {\n", &MainPrefs.pr_ProjectPrefix[0] );

    if ( MyTagInArray( SA_Left, MainSTags ))
        MyFPrintf( file, "    SA_Left,          %ld,\n", GetTagData( SA_Left, 0l, MainSTags ));
    if ( MyTagInArray( SA_Top, MainSTags ))
        MyFPrintf( file, "    SA_Top,           %ld,\n", GetTagData( SA_Top, 0l, MainSTags ));
    if ( MyTagInArray( SA_Width, MainSTags ))
        MyFPrintf( file, "    SA_Width,         %ld,\n", GetTagData( SA_Width, 0l, MainSTags ));
    if ( MyTagInArray( SA_Height, MainSTags ))
        MyFPrintf( file, "    SA_Height,        %ld,\n", GetTagData( SA_Height, 0l, MainSTags ));
    if ( MyTagInArray( SA_Depth, MainSTags ))
        MyFPrintf( file, "    SA_Depth,         %ld,\n", GetTagData( SA_Depth, 0l, MainSTags ));
    if ( MyTagInArray( SA_DetailPen, MainSTags ))
        MyFPrintf( file, "    SA_DetailPen,     %ld,\n", GetTagData( SA_DetailPen, 0l, MainSTags ));
    if ( MyTagInArray( SA_BlockPen, MainSTags ))
        MyFPrintf( file, "    SA_BlockPen,      %ld,\n", GetTagData( SA_BlockPen, 0l, MainSTags ));
    if ( MyTagInArray( SA_Colors, MainSTags ))
        MyFPrintf( file, "    SA_Colors,        %sScreenColors,\n", &MainPrefs.pr_ProjectPrefix[0] );
    if ( MyTagInArray( SA_Font, MainSTags ))
        MyFPrintf( file, "    SA_Font,          &%s%ld,\n", fname, MainFont.ta_YSize );
    MyFPrintf( file, "    SA_Type,          CUSTOMSCREEN,\n" );
    if ( MyTagInArray( SA_DisplayID, MainSTags )) {
        MyFPrintf( file, "    SA_DisplayID,     " );
        WriteIDFlags( file, GetTagData( SA_DisplayID, 0l, MainSTags ), 0l);
    }
    if ( cs_AutoScroll )
        MyFPrintf( file, "    SA_AutoScroll,    1l,\n" );
    if ( MyTagInArray( SA_Pens, MainSTags ))
        MyFPrintf( file, "    SA_Pens,          %sDriPens,\n", &MainPrefs.pr_ProjectPrefix[0] );

    MyFPrintf( file, "    TAG_DONE };\n\n" );
}

/*
 * --- Write the C IntuiText structures.
 */
void WriteCIText( BPTR file )
{
    struct IntuiText   *t;
    UWORD               i = 1;
    UBYTE               fname[32], *ptr;

    strcpy( fname, MainFontName );

    ptr = strchr( fname, '.' );
    *ptr = 0;

    if ( NOT( t = WindowTxt )) return;

    if ( STAT ) MyFPrintf( file, "static " );
    MyFPrintf( file, "struct IntuiText  %sIText[] = {\n", &MainPrefs.pr_ProjectPrefix[0] );

    while ( t ) {
        MyFPrintf( file, "    %ld, %ld, ", t->FrontPen, t->BackPen );
        WriteCDrMd( file, t->DrawMode, 0l );
        MyFPrintf( file, "%ld, %ld, &%s%ld, ", t->LeftEdge, t->TopEdge, fname, MainFont.ta_YSize );
        MyFPrintf( file, "(UBYTE *)\"%s\", ", t->IText );

        if ( t->NextText )
            MyFPrintf( file, "&%sIText[%ld],\n", &MainPrefs.pr_ProjectPrefix[0], i++ );
        else
            MyFPrintf( file, "NULL };\n\n" );
        t = t->NextText;
    }
}

/*
 * --- Write the routine header.
 */
void WriteCHeader( BPTR file )
{
    if ( STAT ) MyFPrintf( file, "static " );

    MyFPrintf( file, "long %sInitStuff( void )\n{\n", &MainPrefs.pr_ProjectPrefix[0] );

    MyFPrintf( file, "    struct NewGadget     ng;\n"  );
    MyFPrintf( file, "    struct Gadget       *g;\n\n" );
}

/*
 * --- Write the C Gadgets initialization.
 */
void WriteCGadgets( BPTR file )
{
    struct ExtNewGadget *g, *pred;
    struct NewGadget    *ng, *ngp;
    UBYTE                fname[32], *ptr;
    UWORD                num;

    strcpy( fname, MainFontName );

    ptr  = strchr( fname, '.' );
    *ptr = 0;

    for ( g = Gadgets.gl_First, num = 0l; g->en_Next; g = g->en_Next, num++ ) {
        if (( pred = g->en_Prev ) == ( struct ExtNewGadget * )&Gadgets ) {
            pred = 0l;
            ngp  = 0l;
        } else
            ngp = &pred->en_NewGadget;

        ng = &g->en_NewGadget;

        if ( ngp ) {
            if ( g->en_Kind != STRING_KIND ) {
                if ( ng->ng_LeftEdge != ngp->ng_LeftEdge )
                    MyFPrintf( file, "    ng.ng_LeftEdge        =    %ld;\n", ng->ng_LeftEdge );
                if ( ng->ng_TopEdge  != ngp->ng_TopEdge )
                    MyFPrintf( file, "    ng.ng_TopEdge         =    %ld;\n", ng->ng_TopEdge );
            } else {
                if (( g->en_SpecialFlags & EGF_ISLOCKED ) != EGF_ISLOCKED ) {
                    if ( ng->ng_LeftEdge != ngp->ng_LeftEdge )
                        MyFPrintf( file, "    ng.ng_LeftEdge        =    %ld;\n", ng->ng_LeftEdge );
                    if ( ng->ng_TopEdge  != ngp->ng_TopEdge )
                        MyFPrintf( file, "    ng.ng_TopEdge         =    %ld;\n", ng->ng_TopEdge );
                }
            }

            if ( g->en_Kind != MX_KIND && g->en_Kind != CHECKBOX_KIND ) {
                if ( ng->ng_Width != ngp->ng_Width )
                    MyFPrintf( file, "    ng.ng_Width           =    %ld;\n", ng->ng_Width );
                if ( ng->ng_Height != ngp->ng_Height )
                    MyFPrintf( file, "    ng.ng_Height          =    %ld;\n", ng->ng_Height );
            }
            if ( ng->ng_GadgetText ) {
                if ( strcmp( ng->ng_GadgetText, ngp->ng_GadgetText ))
                    MyFPrintf( file, "    ng.ng_GadgetText      =    \"%s\";\n", ng->ng_GadgetText );
            } else
                    MyFPrintf( file, "    ng.ng_GadgetText      =    0l;\n" );
            MyFPrintf( file, "    ng.ng_GadgetID        =    GD_%s;\n", &g->en_SourceLabel[0] );
            if ( ng->ng_Flags != ngp->ng_Flags ) {
                MyFPrintf( file, "    ng.ng_Flags           =    " );
                WritePlaceFlags( file, (long)ng->ng_Flags, 0l );
            }
        } else {
            MyFPrintf( file, "    ng.ng_LeftEdge        =    %ld;\n", ng->ng_LeftEdge );
            MyFPrintf( file, "    ng.ng_TopEdge         =    %ld;\n", ng->ng_TopEdge );
            MyFPrintf( file, "    ng.ng_Width           =    %ld;\n", ng->ng_Width );
            MyFPrintf( file, "    ng.ng_Height          =    %ld;\n", ng->ng_Height );
            if ( ng->ng_GadgetText )
                MyFPrintf( file, "    ng.ng_GadgetText      =    \"%s\";\n", ng->ng_GadgetText );
            else
                MyFPrintf( file, "    ng.ng_GadgetText      =    0l;\n" );
            MyFPrintf( file, "    ng.ng_TextAttr        =    &%s%ld;\n", &fname[0], MainFont.ta_YSize );
            MyFPrintf( file, "    ng.ng_GadgetID        =    GD_%s;\n", &g->en_SourceLabel[0] );
            MyFPrintf( file, "    ng.ng_Flags           =    " );
            WritePlaceFlags( file, ng->ng_Flags, 0l );
            MyFPrintf( file, "    ng.ng_VisualInfo      =    %sVisualInfo;\n", &MainPrefs.pr_ProjectPrefix[0] );
        }

        MyFPrintf( file, "\n    g = CreateGadget( %s, g, &ng, ", gc_Kinds[ g->en_Kind ] );

        switch ( g->en_Kind ) {

            case    BUTTON_KIND:
                if (( g->en_SpecialFlags & EGF_DISABLED ) == EGF_DISABLED )
                    MyFPrintf( file, "GA_Disabled, TRUE, " );
                break;

            case    CHECKBOX_KIND:
                if (( g->en_SpecialFlags & EGF_CHECKED ) == EGF_CHECKED )
                    MyFPrintf( file, "GTCB_Checked, TRUE, " );
                if (( g->en_SpecialFlags & EGF_DISABLED ) == EGF_DISABLED )
                    MyFPrintf( file, "GA_Disabled, TRUE, " );
                break;

            case    CYCLE_KIND:
                MyFPrintf( file, "GTCY_Labels, %sLabels, ", &g->en_SourceLabel[0] );
                if (( g->en_SpecialFlags & EGF_DISABLED ) == EGF_DISABLED )
                    MyFPrintf( file, "GA_Disabled, TRUE, " );
                break;

            case    INTEGER_KIND:
                MyFPrintf( file, "GTIN_Number, %ld, ", g->en_DefInt );
                MyFPrintf( file, "GTIN_MaxChars, %ld, ", GetTagData( GTIN_MaxChars, 5l, g->en_Tags ));
                if (( g->en_SpecialFlags & EGF_DISABLED ) == EGF_DISABLED )
                    MyFPrintf( file, "GA_Disabled, TRUE, " );
                break;

            case    LISTVIEW_KIND:
                if ( g->en_Entries.lh_Head->ln_Succ->ln_Succ )
                    MyFPrintf( file, "GTLV_Labels, &%sList, ", &g->en_SourceLabel[0] );
                else
                    MyFPrintf( file, "GTLV_Labels, ~0, " );
                if (( g->en_SpecialFlags & EGF_NEEDLOCK ) == EGF_NEEDLOCK )
                    MyFPrintf( file, "GTLV_ShowSelected, %sGadgets[%ld], ", &MainPrefs.pr_ProjectPrefix[0], num - 1 );
                else if (MyTagInArray( GTLV_ShowSelected, g->en_Tags ))
                    MyFPrintf( file, "GTLV_ShowSelected, 0l, " );
                if ( MyTagInArray( GTLV_ScrollWidth, g->en_Tags ))
                    MyFPrintf( file, "GTLV_ScrollWidth, %ld, ", g->en_ScrollWidth );
                if (( g->en_SpecialFlags & EGF_READONLY ) == EGF_READONLY )
                    MyFPrintf( file, "GTLV_ReadOnly, TRUE, " );
                if ( MyTagInArray( LAYOUTA_Spacing, g->en_Tags ))
                    MyFPrintf( file, "LAYOUTA_Spacing, %ld, ", g->en_Spacing );
                break;

            case    MX_KIND:
                MyFPrintf( file, "GTMX_Labels, %sLabels, ", &g->en_SourceLabel[0] );
                if ( MyTagInArray( GTMX_Spacing, g->en_Tags ))
                    MyFPrintf( file, "GTMX_Spacing, %ld, ", g->en_Spacing );
                break;

            case    PALETTE_KIND:
                MyFPrintf( file, "GTPA_Depth, %ld, ", MainScreen->BitMap.Depth );
                if ( MyTagInArray( GTPA_IndicatorWidth, g->en_Tags ))
                    MyFPrintf( file, "GTPA_IndicatorWidth, %ld, ", GetTagData( GTPA_IndicatorWidth, 0l, g->en_Tags ));
                if ( MyTagInArray( GTPA_IndicatorHeight, g->en_Tags ))
                    MyFPrintf( file, "GTPA_IndicatorHeight, %ld, ", GetTagData( GTPA_IndicatorHeight, 0l, g->en_Tags ));
                if (( g->en_SpecialFlags & EGF_DISABLED ) == EGF_DISABLED )
                    MyFPrintf( file, "GA_Disabled, TRUE, " );
                break;

            case    SCROLLER_KIND:
                if ( MyTagInArray( GTSC_Top, g->en_Tags ))
                    MyFPrintf( file, "GTSC_Top, %ld, ", GetTagData( GTSC_Top, 0l, g->en_Tags ));
                if ( MyTagInArray( GTSC_Total, g->en_Tags ))
                    MyFPrintf( file, "GTSC_Total, %ld, ", GetTagData( GTSC_Total, 0l, g->en_Tags ));
                if ( MyTagInArray( GTSC_Visible, g->en_Tags ))
                    MyFPrintf( file, "GTSC_Visible, %ld, ", GetTagData( GTSC_Visible, 0l, g->en_Tags ));
                if ( MyTagInArray( GTSC_Arrows, g->en_Tags ))
                    MyFPrintf( file, "GTSC_Arrows, %ld, ", g->en_ArrowSize );
                if ( MyTagInArray( PGA_Freedom, g->en_Tags ))
                    MyFPrintf( file, "PGA_Freedom, LORIENT_VERT, " );
                else
                    MyFPrintf( file, "PGA_Freedom, LORIENT_HORIZ, " );
                if ( MyTagInArray( GA_Immediate, g->en_Tags ))
                    MyFPrintf( file, "GA_Immediate, TRUE, " );
                if ( MyTagInArray( GA_RelVerify, g->en_Tags ))
                    MyFPrintf( file, "GA_RelVerify, TRUE, " );
                if (( g->en_SpecialFlags & EGF_DISABLED ) == EGF_DISABLED )
                    MyFPrintf( file, "GA_Disabled, TRUE, " );
                break;

            case    SLIDER_KIND:
                if ( MyTagInArray( GTSL_Min, g->en_Tags ))
                    MyFPrintf( file, "GTSL_Min, %ld, ", GetTagData( GTSL_Min, 0l, g->en_Tags ));
                if ( MyTagInArray( GTSL_Max, g->en_Tags ))
                    MyFPrintf( file, "GTSL_Max, %ld, ", GetTagData( GTSL_Max, 0l, g->en_Tags ));
                if ( MyTagInArray( GTSL_Level, g->en_Tags ))
                    MyFPrintf( file, "GTSL_Level, %ld, ", GetTagData( GTSL_Level, 0l, g->en_Tags ));
                if ( MyTagInArray( GTSL_MaxLevelLen, g->en_Tags ))
                    MyFPrintf( file, "GTSL_MaxLevelLen, %ld, ", GetTagData( GTSL_MaxLevelLen, 0l, g->en_Tags ));
                if ( MyTagInArray( GTSL_LevelFormat, g->en_Tags ))
                    MyFPrintf( file, "GTSL_LevelFormat, \"%s\", ", g->en_LevelFormat );
                if ( MyTagInArray( GTSL_LevelPlace, g->en_Tags )) {
                    MyFPrintf( file, "GTSL_LevelPlace, " );
                    WritePlaceFlags( file, (long)GetTagData( GTSL_LevelPlace, 0l, g->en_Tags ), 0l );
                    Seek( file, -2, OFFSET_CURRENT );
                    MyFPrintf( file, ", " );
                }
                if ( MyTagInArray( PGA_Freedom, g->en_Tags ))
                    MyFPrintf( file, "PGA_Freedom, LORIENT_VERT, " );
                else
                    MyFPrintf( file, "PGA_Freedom, LORIENT_HORIZ, " );
                if ( MyTagInArray( GA_Immediate, g->en_Tags ))
                    MyFPrintf( file, "GA_Immediate, TRUE, " );
                if ( MyTagInArray( GA_RelVerify, g->en_Tags ))
                    MyFPrintf( file, "GA_RelVerify, TRUE, " );
                if (( g->en_SpecialFlags & EGF_DISABLED ) == EGF_DISABLED )
                    MyFPrintf( file, "GA_Disabled, TRUE, " );
                break;

            case    STRING_KIND:
                if ( g->en_DefString )
                    MyFPrintf( file, "GTST_String, \"%s\", ", g->en_DefString );
                MyFPrintf( file, "GTST_MaxChars, %ld, ", GetTagData( GTST_MaxChars, 5l, g->en_Tags ));
                if (( g->en_SpecialFlags & EGF_DISABLED ) == EGF_DISABLED )
                    MyFPrintf( file, "GA_Disabled, TRUE, " );
                break;
        }
        if ( MyTagInArray( GT_Underscore,  g->en_Tags ))
            MyFPrintf( file, "GT_Underscore, '_', " );

        MyFPrintf( file, "TAG_DONE );\n\n" );
        MyFPrintf( file, "    %sGadgets[ %ld ] = g;\n\n", &MainPrefs.pr_ProjectPrefix[0], num );
    }

    if ( Gadgets.gl_First->en_Next ) {
        MyFPrintf( file, "    if ( NOT g )\n        return( 4l );\n\n" );
        MyFPrintf( file, "    %sWindowTags[ %ld ].ti_Data = %sGList;\n\n", &MainPrefs.pr_ProjectPrefix[0], gc_GadOffset, &MainPrefs.pr_ProjectPrefix[0] );
    }
}

/*
 * --- Write the C cleanup routine.
 */
void WriteCCleanup( BPTR file )
{
    MyFPrintf( file, "void %sCleanStuff( void )\n{\n", &MainPrefs.pr_ProjectPrefix[0] );
    if ( ExtMenus.ml_First->em_Next )
        MyFPrintf( file, "    if ( %sMenus      ) {\n        ClearMenuStrip( %sWnd );\n        FreeMenus( %sMenus );\n    }\n\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0] );
    MyFPrintf( file, "    if ( %sWnd        )\n        CloseWindow( %sWnd );\n\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0] );
    if ( Gadgets.gl_First->en_Next )
        MyFPrintf( file, "    if ( %sGList      )\n        FreeGadgets( %sGList );\n\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0] );
    MyFPrintf( file, "    if ( %sVisualInfo )\n        FreeVisualInfo( %sVisualInfo );\n\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0] );
    if ( cs_ScreenType == 2 )
        MyFPrintf( file, "    if ( %sScr        )\n        CloseScreen( %sScr );\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0] );
    else
        MyFPrintf( file, "    if ( %sScr        )\n        UnlockPubScreen( 0l, %sScr );\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0] );
    MyFPrintf( file, "}\n" );
}

/*
 * --- Write the C Source file.
 */
long WriteCSource( void )
{
    BPTR                file = 0l;
    UBYTE                fname[32], *ptr;

    strcpy( fname, MainFontName );

    ptr  = strchr( fname, '.' );
    *ptr = 0;

    if ( gc_GenC = AllocAslRequest( ASL_FileRequest, TAG_DONE )) {
        gc_CTags[1].ti_Data = MainWindow;
        if ( AslRequest( gc_GenC, gc_CTags )) {

            strcpy( MainFileName, gc_GenC->rf_Dir );
            CheckDirExtension();
            strcat( MainFileName, gc_GenC->rf_File );

            strcpy( gc_CPath, gc_GenC->rf_Dir );
            strcpy( gc_CFile, gc_GenC->rf_File );
            strcpy( gc_CPatt, gc_GenC->rf_Pat );

            if ( file = MyOpen( MODE_NEWFILE )) {

                SetTitle( "Saving C Source..." );

                SetIoErr( 0l );

                MyFPrintf( file, "/*\n *  Source generated with GadToolsBox V1.0\n" );
                MyFPrintf( file, " *  which is (c) Copyright 1991 Jaba Development\n" );
                MyFPrintf( file, " */\n\n" );
                MyFPrintf( file, "#include <exec/types.h>\n#include <intuition/intuition.h>\n" );
                MyFPrintf( file, "#include <intuition/gadgetclass.h>\n#include <libraries/gadtools.h>\n" );
                MyFPrintf( file, "#include <graphics/displayinfo.h>\n" );
                MyFPrintf( file, "#include <clib/exec_protos.h>\n#include <clib/intuition_protos.h>\n");
                MyFPrintf( file, "#include <clib/gadtools_protos.h>\n\n" );

                WriteCID( file );
                WriteCGlob( file );
                WriteCLabels( file );
                WriteCList( file );
                WriteCTextAttr( file );
                WriteCWTags( file );
                WriteCIText( file );
                WriteCMenus( file );

                if ( cs_ScreenType == 2 )
                    WriteCSTags( file );

                WriteCHeader( file );

                if ( NOT cs_ScreenType )
                    MyFPrintf( file, "    if ( NOT( %sScr = LockPubScreen( \"Workbench\" )))\n        return( 1l );\n\n", &MainPrefs.pr_ProjectPrefix[0] );
                else if ( cs_ScreenType == 1 )
                    MyFPrintf( file, "    if ( NOT( %sScr = LockPubScreen( 0l )))\n        return( 1l );\n\n", &MainPrefs.pr_ProjectPrefix[0] );
                else
                    MyFPrintf( file, "    if ( NOT( %sScr = OpenScreenTagList( 0l, %sScreenTags )))\n        return( 1l );\n\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0] );

                if ( cs_ScreenType )
                    MyFPrintf( file, "    %sWindowTags[ %ld ].ti_Data = %sScr;\n\n", &MainPrefs.pr_ProjectPrefix[0], gc_ScreenOffset, &MainPrefs.pr_ProjectPrefix[0] );

                MyFPrintf( file, "    if ( NOT( %sVisualInfo = GetVisualInfo( %sScr, TAG_DONE )))\n        return( 2l );\n\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0] );

                if ( Gadgets.gl_First->en_Next )
                    MyFPrintf( file, "    if ( NOT( g = CreateContext( &%sGList )))\n        return( 3l );\n\n", &MainPrefs.pr_ProjectPrefix[0] );

                WriteCGadgets( file );

                if ( ExtMenus.ml_First->em_Next ) {
                    MyFPrintf( file, "    if ( NOT( %sMenus = CreateMenus( %sNewMenu, GTMN_FrontPen, 0l, TAG_DONE )))\n        return( 6l );\n\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0] );
                    MyFPrintf( file, "    LayoutMenus( %sMenus, %sVisualInfo, GTMN_TextAttr, &%s%ld, TAG_DONE );\n\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0], fname, MainFont.ta_YSize );
                }

                MyFPrintf( file, "    if ( NOT( %sWnd = OpenWindowTagList( 0l, %sWindowTags )))\n        return( 5l );\n\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0] );

                if ( ExtMenus.ml_First->em_Next )
                    MyFPrintf( file, "    SetMenuStrip( %sWnd, %sMenus );\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0] );

                MyFPrintf( file, "    GT_RefreshWindow( %sWnd, 0l );\n\n", &MainPrefs.pr_ProjectPrefix[0] );

                if ( ws_ZoomF ) {
                    MyFPrintf( file, "    %sZoom[0] = %ld;\n", &MainPrefs.pr_ProjectPrefix[0], MainWindow->LeftEdge );
                    MyFPrintf( file, "    %sZoom[1] = %ld;\n", &MainPrefs.pr_ProjectPrefix[0], MainWindow->TopEdge );
                    MyFPrintf( file, "    %sZoom[0] = %ld;\n", &MainPrefs.pr_ProjectPrefix[0], MainWindow->Width );
                    MyFPrintf( file, "    %sZoom[0] = %ld;\n\n", &MainPrefs.pr_ProjectPrefix[0], MainWindow->Height );
                }

                if ( WindowTxt )
                    MyFPrintf( file, "    PrintIText( %sWnd->RPort, %sIText, 0l, 0l );\n\n", &MainPrefs.pr_ProjectPrefix[0], &MainPrefs.pr_ProjectPrefix[0] );

                MyFPrintf( file, "    return( 0l );\n}\n\n" );

                WriteCCleanup( file );

                Close( file );

                file = 0l;

                if ( IoErr())
                    MyRequest( "Oh oh...", "CONTINUE", "Write Error !" );
            }
        }
    }

    SetWindowTitles( MainWindow, MainWindowTitle, MainScreenTitle );
    RefreshWindow();

    if ( file )     Close( file );
    if ( gc_GenC )  FreeAslRequest( gc_GenC );

    gc_GenC = 0l;

    ClearMsgPort( MainWindow->UserPort );
}
