diff --git a/include/wx/msw/font.h b/include/wx/msw/font.h index 1ab10844bc..f7c564b1e9 100644 --- a/include/wx/msw/font.h +++ b/include/wx/msw/font.h @@ -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(); diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index 6c5f404608..16a579b957 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -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 // --------------------------------------------------------------------------- diff --git a/locale/.cvsignore b/locale/.cvsignore new file mode 100644 index 0000000000..cd1f2c9439 --- /dev/null +++ b/locale/.cvsignore @@ -0,0 +1 @@ +*.mo diff --git a/samples/font/font.cpp b/samples/font/font.cpp index 8c1a0b630a..c8dd213baa 100644 --- a/samples/font/font.cpp +++ b/samples/font/font.cpp @@ -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()); diff --git a/src/msw/font.cpp b/src/msw/font.cpp index 0a38c677af..071d8b0bd9 100644 --- a/src/msw/font.cpp +++ b/src/msw/font.cpp @@ -228,6 +228,11 @@ bool wxFont::FreeResource(bool force) } WXHANDLE wxFont::GetResourceHandle() +{ + return GetHFONT(); +} + +WXHFONT wxFont::GetHFONT() const { if ( !M_FONTDATA ) return 0; diff --git a/src/msw/window.cpp b/src/msw/window.cpp index 4b7e7370d5..c3dd1a083b 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -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; +}