/*
	Copyright (c) 2024 PTC Inc. and/or Its Subsidiary Companies. All Rights Reserved.
*/


/* This code is used for test: ptn_custbugs_i03j01j03_1 */

#include <ProToolkit.h>
#include <ProDtlnote.h>
#include <PTApplsUnicodeUtils.h>
#include <UgDrawing.h>
/*====================================================================*\ 
FUNCTION : UsrSurfNoteCreate() 
PURPOSE  : Utility to create a note that documents the surface name or id. 
\*====================================================================*/ 
int UsrSurfNoteCreate(
     ProDrawing drawing,
     ProSelection surf_sel,      /* The surface - where the note leader
                                    should be attached. */
     ProVector pos)              /* The location of the note itself */
{
     ProDtlnotetext text;
     ProName font;
     ProDtlnoteline line;
     ProModelitem modelitem;
     ProName wname;
     ProCharName name;
     ProLine wstr;
     ProCharLine str;
     ProDtlnotedata ndata;
     ProView view;
     ProDtlattach attach, leader;
     ProDtlnote note;
     ProTextStyle style;

/*--------------------------------------------------------------------*\ 
    Allocate a text item, and set its properties 
\*--------------------------------------------------------------------*/ 
    ProDtlnotetextAlloc(&text);
     style = UserCreateTextStyle(L"cgomg", -1.0, -1.0, 0.0, 0.0);
     ProDtlnotetextStyleSet(text, style);

/*--------------------------------------------------------------------*\ 
    Get the name of the selected surface, form a string which
     describes it, and add the string to the text item 
\*--------------------------------------------------------------------*/ 
    ProSelectionModelitemGet(surf_sel, &modelitem);
    if(ProModelitemNameGet(&modelitem, wname) == PRO_TK_NO_ERROR)
         ProWstringToString(name, wname);

    else
         ProTKSprintf(name, "id %d", modelitem.id); 
    
    ProTKSprintf(str, "Surface %s", name); 

    ProStringToWstring(wstr, str); 
    ProDtlnotetextStringSet(text, wstr);
    
/*--------------------------------------------------------------------*\ 
    Allocate a new text line, and add the text item to it 
\*--------------------------------------------------------------------*/ 
    ProDtlnotelineAlloc(&line);
    ProDtlnotelineTextAdd(line, text);

/*--------------------------------------------------------------------*\ 
    Allocate a note description, and add the line to it 
\*--------------------------------------------------------------------*/ 
    ProDtlnotedataAlloc(drawing, &ndata); 
    ProDtlnotedataLineAdd(ndata, line);

/*--------------------------------------------------------------------*\ 
    Set the attachment of the note itself 
\*--------------------------------------------------------------------*/ 
    ProSelectionViewGet(surf_sel, &view);
    ProDtlattachAlloc(PRO_DTLATTACHTYPE_FREE, view, pos, NULL, &attach);
    ProDtlnotedataAttachmentSet(ndata, attach);

/*--------------------------------------------------------------------*\ 
    Add the note leader 
\*--------------------------------------------------------------------*/ 
    ProDtlattachAlloc(PRO_DTLATTACHTYPE_PARAMETRIC, NULL, NULL, surf_sel,
                         &leader);
    ProDtlnotedataLeaderAdd(ndata, leader);

/*--------------------------------------------------------------------*\ 
    Create the note 
\*--------------------------------------------------------------------*/ 
    ProDtlnoteCreate(drawing, NULL, ndata, &note);

/*--------------------------------------------------------------------*\ 
    Display the note 
\*--------------------------------------------------------------------*/ 
    ProAnnotationShow(&note,NULL, view);

/*--------------------------------------------------------------------*\ 
    Free the memory for the note description 
\*--------------------------------------------------------------------*/ 
    ProDtlnotedataFree(ndata);
    ProTextStyleFree(&style);
    
    return 1;

 }

int UsrSurfNoteCreateWrapper ()
{
   ProDrawing drawing;
    ProMouseButton button;
    ProSelection *sel;
    int n_sel;
    ProVector pos;
    ProFileName msgfil;

    ProStringToWstring (msgfil, "msg_ugdrawing.txt");

    ProMdlCurrentGet((ProMdl*)&drawing);

    ProMessageDisplay(msgfil,"USER Select surface");
    if(ProSelect("surface",1,NULL,NULL,NULL,NULL,&sel,&n_sel) !=
            PRO_TK_NO_ERROR || n_sel < 1)
        return(0);

    ProMessageDisplay(msgfil,"USER Select note position");
    if(ProMousePickGet(PRO_ANY_BUTTON, &button, pos) != PRO_TK_NO_ERROR)
      return(0);

    UsrSurfNoteCreate(drawing, sel[0], pos);

    return (1);
}