storing font in config file Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Edgar
Knows some wx things
Knows some wx things
Posts: 40
Joined: Sun Sep 13, 2009 3:55 am

storing font in config file

Post by Edgar »

in TheApp.h:

Code: Select all

class TheApp:public wxApp {
 public:
   [...]
   wxFont mWaveformFont;
   wxColor mWaveformFontColor;
[...]
};

In the Prefs panel a wxFontDialog returns a wxFontData from which a wxFont and wxColour are Get...()ed and stored in the App:

Code: Select all

void GUIPrefs::OnFontDialogButton(wxCommandEvent & e)
{
   wxFontDialog fontDialog = new wxFontDialog((wxWindow *)this);
   fontDialog.ShowModal();
   wxFontData fontData = fontDialog.GetFontData();
   wxGetApp().mWaveformFont = fontData.GetChosenFont();
   wxGetApp().mWaveformFontColor = fontData.GetColour();
}
Works great during the execution. Now I need to store the user's choice in the prefs config file:
color & size are easy...

Code: Select all

unsigned char red = wxGetApp().mWaveformFontColor.Red();
unsigned char green = wxGetApp().mWaveformFontColor.Green();
unsigned char blue = wxGetApp().mWaveformFontColor.Blue();
gPrefs->Write(wxT("/GUI/FontColorRed"), red);
gPrefs->Write(wxT("/GUI/FontColorGreen"), green);
gPrefs->Write(wxT("/GUI/FontColorBlue"), blue);

gPrefs->Write(wxT("/GUI/FontSize"), wxGetApp().mWaveformFont.GetPointSize());
How may I store the other disk-based font-specific details; I need disk-font, style etc.?
--Edgar
Muetdhiver
Super wx Problem Solver
Super wx Problem Solver
Posts: 323
Joined: Sun Jun 08, 2008 11:59 am
Location: Bordeaux, France

Re: storing font in config file

Post by Muetdhiver »

Hi,

I don't understand what you actually want to store (which types of data ?), and what do you mean by "I need disc font"...
But you can store everything in a pref config file, the wxConfigBase does it for you. Data must not be too big.

Depending on what you want to store, you maybe need to serialize data in order to store it. This data can be a simple buffer and thus you can use the

Code: Select all

bool 	Write (const wxString &key, const wxMemoryBuffer &buf)
function to store it. Have a look to memory buffer.

Or you maybe don't need to serialize data, if you only use simple types (long, char, bool) and that you store your data in an organized way :
GUI/Font1/Color
GUI/Font1/Size

etc...

Please can you tell what is your real problem with wxConfigBase class ?

Thanks, Bye.
OS: Ubuntu 11.10
Compiler: g++ 4.6.1 (Eclipse CDT Indigo)
wxWidgets: 2.9.3
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: storing font in config file

Post by doublemax »

Use the source, Luke!
Edgar
Knows some wx things
Knows some wx things
Posts: 40
Joined: Sun Sep 13, 2009 3:55 am

Re: storing font in config file

Post by Edgar »

Muetdhiver wrote: Please can you tell what is your real problem with wxConfigBase class ?
No problem with the class, see next reply...
--Edgar
Edgar
Knows some wx things
Knows some wx things
Posts: 40
Joined: Sun Sep 13, 2009 3:55 am

Re: storing font in config file

Post by Edgar »

doublemax wrote:Use wxFont::GetNativeFontInfoDesc() / wxFont::SetNativeFontInfo()

http://docs.wxwidgets.org/stable/wx_wxf ... ntinfodesc
http://docs.wxwidgets.org/stable/wx_wxf ... vefontinfo
This was what I needed I think--will need to test this out and report back later. Thanks!
--Edgar
Edgar
Knows some wx things
Knows some wx things
Posts: 40
Joined: Sun Sep 13, 2009 3:55 am

Re: storing font in config file

Post by Edgar »

Here is what I came up with...
storing the data:

Code: Select all

void GUIPrefs::OnFontDialogButton(wxCommandEvent & e)
{
   wxFontDialog fontDialog = new wxFontDialog((wxWindow *)this);
   fontDialog.ShowModal();
   wxFontData fontData = fontDialog.GetFontData();
   gPrefs->Write(wxT("/GUI/NativeFontForNameInWaveform"), fontData.GetChosenFont().GetNativeFontInfoDesc());
   const wxColor fontColor = fontData.GetColour();
   unsigned char red = fontColor.Red();
   unsigned char green = fontColor.Green();
   unsigned char blue = fontColor.Blue();
   gPrefs->Write(wxT("/GUI/FontColorRedForNameInWaveform"), red);
   gPrefs->Write(wxT("/GUI/FontColorGreenForNameInWaveform"), green);
   gPrefs->Write(wxT("/GUI/FontColorBlueForNameInWaveform"), blue);
}
retrieving the data:

Code: Select all

wxFont labelFont(8, wxFONTFAMILY_SWISS, wxNORMAL, wxNORMAL);
wxString fontDescription = gPrefs->Read(wxT("/GUI/NativeFontForNameInWaveform"), wxEmptyString);
if (!fontDescription.IsEmpty())
   labelFont.SetNativeFontInfo(fontDescription);
this does not seem to preserve color, so I also need:

Code: Select all

const wxColour& originalForegroundTextColor = dc.GetTextForeground();
wxString red;
long redValue = 0;
if (gPrefs->Read(wxT("/GUI/FontColorRedForNameInWaveform"), &red))
   red.ToLong(&redValue);

wxString green;
long greenValue = 0;
if (gPrefs->Read(wxT("/GUI/FontColorGreenForNameInWaveform"), &green))
   green.ToLong(&greenValue);

wxString blue;
long blueValue = 0;
if (gPrefs->Read(wxT("/GUI/FontColorBlueForNameInWaveform"), &blue))
   blue.ToLong(&blueValue);

wxColour newForegroundColor(redValue, greenValue, blueValue);
dc.SetTextForeground(newForegroundColor);
dc.DrawText (wt->GetName(), r.x+10, r.y);  // move right 10 pixels to avoid overwriting <- symbol
dc.SetTextForeground(originalForegroundTextColor);
--Edgar
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: storing font in config file

Post by doublemax »

I think you can simplify it a little bit more by using wxColour::GetAsString().
The wxColour ctor that takes a wxString as parameter understands the format, so you can use it directly.
Use the source, Luke!
Edgar
Knows some wx things
Knows some wx things
Posts: 40
Joined: Sun Sep 13, 2009 3:55 am

Re: storing font in config file

Post by Edgar »

doublemax wrote:I think you can simplify it a little bit more by using wxColour::GetAsString().
The wxColour ctor that takes a wxString as parameter understands the format, so you can use it directly.
Good suggestion, will give it a go, thanks!
--Edgar
Edgar
Knows some wx things
Knows some wx things
Posts: 40
Joined: Sun Sep 13, 2009 3:55 am

Re: storing font in config file

Post by Edgar »

Worked a charm, thanks! reduction of about 30 lines of code.
--Edgar
Post Reply