Author Topic: Problem with wxT() and _T() functions when saving graphs.  (Read 1592 times)

Offline Grouch

  • Multiple posting newcomer
  • *
  • Posts: 40
Problem with wxT() and _T() functions when saving graphs.
« on: August 06, 2016, 10:51:29 pm »
I am mystified and stymied by a failure to compile a .cpp program written with codeblocks and
wxSmith.

I am trying to save a graph. The variable "paper" has been defined by
   wxBitmap *paper = new wxBitmap( 2000,1236);

With the line 

  paper->SaveFile( _T("GwxGraph.png"), wxBITMAP_TYPE_PNG,   (wxPalette*)NULL );

the program compiles and works just fine, but I want to make the name of
the file holding holding the graph variable. So I start with just

   char filename[32];
   strcpy(filename,"GwxGraph.png")
   paper->SaveFile( _T(filename), wxBITMAP_TYPE_PNG,(wxPalette*)NULL );
   
but the program won't even compile. It stops with the red error marker on the line marked with
===>>> in the text shown below and taken from wxChar.h.

The error message is that LGwxGraph.png is not declared, which is indeed true, but why
should it be declared?

Any suggestions about how I could make the filename of the graph a variable would be most
appreciated.

Maybe this question belongs over on the wxWidgets forum, but I have gotten such excellent here on
Code::Blocks, I thought to try first here.
==========================================================
From wxChar.h

    #define wxT(x) wxCONCAT_HELPER(L, x)
   
   
/* ---------------------------------------------------------------------------- */
/* define wxT() and related macros */
/* ---------------------------------------------------------------------------- */

#if wxUSE_UNICODE
    /* use wxCONCAT_HELPER so that x could be expanded if it's a macro */
===>>>    #define wxT(x) wxCONCAT_HELPER(L, x)
#else /* !Unicode */
    #define wxT(x) x
#endif /* Unicode/!Unicode */

/*
    This macro is defined for forward compatibility with wxWidgets 3. It should
    be used in the places where wxWidgets 2 API requires wxT() (in Unicode
    build) but wxWidgets 3 doesn't accept it, e.g. wxCmdLineEntryDesc struct
    elements initializers.
 */
#define wxT_2(x) wxT(x)

/*
    We define _T() as a synonym of wxT() for backwards compatibility and also
    for the benefit of Windows programmers used to it. But this identifier is a
    reserved one and this does create problems in practice, notably with Sun CC
    which uses it in the recent versions of its standard headers. So avoid
    defining it for this compiler at all, unless it was explicitly requested by
    predefining wxNEEDS__T macro before including this header or if we're
    building wx itself which does need and compiles fine thanks to the special
    workarounds for Sun CC in wx/{before,after}std.h.
 */

Offline Grouch

  • Multiple posting newcomer
  • *
  • Posts: 40
Re: Problem with wxT() and _T() functions when saving graphs.
« Reply #1 on: August 07, 2016, 08:19:45 am »
After several more hours of searching and reading, I found someone else suffering from the same problem and could profit from his solution. Instead of using the _T or wxT macro, use the wxString::FromAscii() function.
In my case, what I needed was

Code
 
    wxString wxGraphName = wxString::FromAscii(GraphName);
    paper->SaveFile(wxGraphName,wxBITMAP_TYPE_PNG,(wxPalette*)NULL);
where GraphName is a plain old null-terminated C string. So simple when you know how and so hard to find out how.