1. added wxFont::GetHFONT() const (GetResourceHandle() is not const...)

2. added GetHfont[Of] convenience macros to msw/private.h
3. wxWindow::GetCharWidth/Height() now use correct font


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_2_BRANCH@7643 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2000-06-27 10:22:06 +00:00
parent 9e677ba35c
commit a750cce7a5
6 changed files with 84 additions and 18 deletions

View File

@@ -78,6 +78,11 @@ public:
virtual bool RealizeResource();
virtual WXHANDLE GetResourceHandle();
virtual bool FreeResource(bool force = FALSE);
// for consistency with other wxMSW classes and to have a const
// GetResourceHandle()-like function we have a synonym for it
WXHFONT GetHFONT() const;
/*
virtual bool UseResource();
virtual bool ReleaseResource();

View File

@@ -289,6 +289,9 @@ private:
#define GetHcursor() ((HCURSOR)GetHCURSOR())
#define GetHcursorOf(cursor) ((HCURSOR)(cursor).GetHCURSOR())
#define GetHfont() ((HFONT)GetHFONT())
#define GetHfontOf(font) ((HFONT)(font).GetHFONT())
#endif // wxUSE_GUI
// ---------------------------------------------------------------------------

1
locale/.cvsignore Normal file
View File

@@ -0,0 +1 @@
*.mo

View File

@@ -84,6 +84,9 @@ public:
// event handlers (these functions should _not_ be virtual)
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnIncFont(wxCommandEvent& event) { DoResizeFont(+2); }
void OnDecFont(wxCommandEvent& event) { DoResizeFont(-2); }
void OnViewMsg(wxCommandEvent& event);
void OnSelectFont(wxCommandEvent& event);
void OnEnumerateFamiliesForEncoding(wxCommandEvent& event);
@@ -100,10 +103,13 @@ protected:
wxFontEncoding encoding = wxFONTENCODING_SYSTEM,
bool silent = FALSE);
void DoResizeFont(int diff);
void DoChangeFont(const wxFont& font, const wxColour& col = wxNullColour);
void Resize(const wxSize& size, const wxFont& font = wxNullFont);
size_t m_fontSize; // in points
wxTextCtrl *m_textctrl;
MyCanvas *m_canvas;
@@ -123,6 +129,8 @@ enum
Font_Quit = 1,
Font_About,
Font_ViewMsg,
Font_IncSize,
Font_DecSize,
Font_Choose = 100,
Font_EnumFamiliesForEncoding,
Font_EnumFamilies,
@@ -141,6 +149,8 @@ enum
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(Font_Quit, MyFrame::OnQuit)
EVT_MENU(Font_About, MyFrame::OnAbout)
EVT_MENU(Font_IncSize, MyFrame::OnIncFont)
EVT_MENU(Font_DecSize, MyFrame::OnDecFont)
EVT_MENU(Font_ViewMsg, MyFrame::OnViewMsg)
EVT_MENU(Font_Choose, MyFrame::OnSelectFont)
EVT_MENU(Font_EnumFamiliesForEncoding, MyFrame::OnEnumerateFamiliesForEncoding)
@@ -191,6 +201,8 @@ bool MyApp::OnInit()
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size), m_textctrl(NULL)
{
m_fontSize = 12;
// create a menu bar
wxMenu *menuFile = new wxMenu;
@@ -202,12 +214,15 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
menuFile->Append(Font_Quit, "E&xit\tAlt-X", "Quit this program");
wxMenu *menuFont = new wxMenu;
menuFont->Append(Font_IncSize, "&Increase font size by 2 points\tCtrl-I");
menuFont->Append(Font_DecSize, "&Decrease font size by 2 points\tCtrl-D");
menuFont->AppendSeparator();
menuFont->Append(Font_Choose, "&Select font...\tCtrl-S",
"Select a standard font");
menuFont->AppendSeparator();
menuFont->Append(Font_EnumFamilies, "Enumerate font &families\tCtrl-F");
menuFont->Append(Font_EnumFixedFamilies,
"Enumerate f&ixed font families\tCtrl-I");
"Enumerate fi&xed font families\tCtrl-X");
menuFont->Append(Font_EnumEncodings,
"Enumerate &encodings\tCtrl-E");
menuFont->Append(Font_EnumFamiliesForEncoding,
@@ -391,6 +406,23 @@ void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent& WXUNUSED(event))
}
}
void MyFrame::DoResizeFont(int diff)
{
wxFont fontOld = m_canvas->GetTextFont();
DoChangeFont(
wxFont(
fontOld.GetPointSize() + diff,
fontOld.GetFamily(),
fontOld.GetStyle(),
fontOld.GetWeight(),
fontOld.GetUnderlined(),
fontOld.GetFaceName(),
fontOld.GetEncoding()
)
);
}
void MyFrame::DoChangeFont(const wxFont& font, const wxColour& col)
{
Resize(GetSize(), font);
@@ -610,7 +642,8 @@ void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
// output the font name/info
wxString fontInfo;
fontInfo.Printf("Font family is '%s', style '%s', weight '%s'",
fontInfo.Printf("Font size is %d points, family is %s, style %s, weight %s",
m_font.GetPointSize(),
m_font.GetFamilyString().c_str(),
m_font.GetStyleString().c_str(),
m_font.GetWeightString().c_str());

View File

@@ -228,6 +228,11 @@ bool wxFont::FreeResource(bool force)
}
WXHANDLE wxFont::GetResourceHandle()
{
return GetHFONT();
}
WXHFONT wxFont::GetHFONT() const
{
if ( !M_FONTDATA )
return 0;

View File

@@ -134,6 +134,9 @@ wxWindow *wxFindWinFromHandle(WXHWND hWnd);
// mouse clicks
static void TranslateKbdEventToMouse(wxWindow *win, int *x, int *y, WPARAM *flags);
// get the text metrics for the current font
static TEXTMETRIC wxGetTextMetrics(const wxWindow *win);
// ---------------------------------------------------------------------------
// event tables
// ---------------------------------------------------------------------------
@@ -1334,26 +1337,14 @@ void wxWindow::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags)
int wxWindow::GetCharHeight() const
{
TEXTMETRIC lpTextMetric;
HWND hWnd = GetHwnd();
HDC dc = ::GetDC(hWnd);
GetTextMetrics(dc, &lpTextMetric);
::ReleaseDC(hWnd, dc);
return lpTextMetric.tmHeight;
return wxGetTextMetrics(this).tmHeight;
}
int wxWindow::GetCharWidth() const
{
TEXTMETRIC lpTextMetric;
HWND hWnd = GetHwnd();
HDC dc = ::GetDC(hWnd);
GetTextMetrics(dc, &lpTextMetric);
::ReleaseDC(hWnd, dc);
return lpTextMetric.tmAveCharWidth;
// +1 is needed because Windows apparently adds it when calculating the
// dialog units size in pixels
return wxGetTextMetrics(this).tmAveCharWidth + 1;
}
void wxWindow::GetTextExtent(const wxString& string,
@@ -4280,3 +4271,31 @@ static void TranslateKbdEventToMouse(wxWindow *win, int *x, int *y, WPARAM *flag
win->ScreenToClient(x, y);
}
static TEXTMETRIC wxGetTextMetrics(const wxWindow *win)
{
// prepare the DC
TEXTMETRIC tm;
HWND hwnd = GetHwndOf(win);
HDC hdc = ::GetDC(hwnd);
// and select the current font into it
HFONT hfont = GetHfontOf(win->GetFont());
if ( hfont )
{
hfont = (HFONT)::SelectObject(hdc, hfont);
}
// finally retrieve the text metrics from it
GetTextMetrics(hdc, &tm);
// and clean up
if ( hfont )
{
(void)::SelectObject(hdc, hfont);
}
::ReleaseDC(hwnd, hdc);
return tm;
}