From f39b95b3540ad4a2be51968f561350f562e3f87d Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Tue, 4 Mar 2014 00:05:51 +0400 Subject: [PATCH] add logging; fixes for font manager --- 3rdparty/win32/wingdi.d | 22 ++--- dlangui.visualdproj | 7 +- src/dlangui/core/logger.d | 78 ++++++++++++++++ src/dlangui/graphics/fonts.d | 4 +- src/dlangui/platforms/windows/winapp.d | 123 +++++++++++++++---------- winmain.d | 6 +- 6 files changed, 175 insertions(+), 65 deletions(-) create mode 100644 src/dlangui/core/logger.d diff --git a/3rdparty/win32/wingdi.d b/3rdparty/win32/wingdi.d index bd136f6c..cd7194c4 100644 --- a/3rdparty/win32/wingdi.d +++ b/3rdparty/win32/wingdi.d @@ -3936,17 +3936,17 @@ alias DRAWPATRECT* PDRAWPATRECT; // --------- // Callbacks -alias BOOL function (HDC, int) ABORTPROC; -alias int function (HDC, HANDLETABLE*, METARECORD*, int, LPARAM) MFENUMPROC; -alias int function (HDC, HANDLETABLE*, const(ENHMETARECORD)*, int, LPARAM) ENHMFENUMPROC; -alias int function (const(LOGFONTA)*, const(TEXTMETRICA)*, DWORD, LPARAM) FONTENUMPROCA, OLDFONTENUMPROCA; -alias int function (const(LOGFONTW)*, const(TEXTMETRICW)*, DWORD, LPARAM) FONTENUMPROCW, OLDFONTENUMPROCW; -alias int function (LPSTR, LPARAM) ICMENUMPROCA; -alias int function (LPWSTR, LPARAM) ICMENUMPROCW; -alias void function (LPVOID, LPARAM) GOBJENUMPROC; -alias void function (int, int, LPARAM) LINEDDAPROC; -alias UINT function (HWND, HMODULE, LPDEVMODEA, LPSTR, LPSTR, LPDEVMODEA, LPSTR, UINT) LPFNDEVMODE; -alias DWORD function (LPSTR, LPSTR, UINT, LPSTR, LPDEVMODEA) LPFNDEVCAPS; +alias extern(Windows) BOOL function (HDC, int) ABORTPROC; +alias extern(Windows) int function (HDC, HANDLETABLE*, METARECORD*, int, LPARAM) MFENUMPROC; +alias extern(Windows) int function (HDC, HANDLETABLE*, const(ENHMETARECORD)*, int, LPARAM) ENHMFENUMPROC; +alias extern(Windows) int function (const(LOGFONTA)*, const(TEXTMETRICA)*, DWORD, LPARAM) FONTENUMPROCA, OLDFONTENUMPROCA; +alias extern(Windows) int function (const(LOGFONTW)*, const(TEXTMETRICW)*, DWORD, LPARAM) FONTENUMPROCW, OLDFONTENUMPROCW; +alias extern(Windows) int function (LPSTR, LPARAM) ICMENUMPROCA; +alias extern(Windows) int function (LPWSTR, LPARAM) ICMENUMPROCW; +alias extern(Windows) void function (LPVOID, LPARAM) GOBJENUMPROC; +alias extern(Windows) void function (int, int, LPARAM) LINEDDAPROC; +alias extern(Windows) UINT function (HWND, HMODULE, LPDEVMODEA, LPSTR, LPSTR, LPDEVMODEA, LPSTR, UINT) LPFNDEVMODE; +alias extern(Windows) DWORD function (LPSTR, LPSTR, UINT, LPSTR, LPDEVMODEA) LPFNDEVCAPS; // --------- diff --git a/dlangui.visualdproj b/dlangui.visualdproj index 63480e47..c933b26d 100644 --- a/dlangui.visualdproj +++ b/dlangui.visualdproj @@ -246,11 +246,9 @@ + - - - @@ -263,6 +261,9 @@ + + + diff --git a/src/dlangui/core/logger.d b/src/dlangui/core/logger.d new file mode 100644 index 00000000..210b37f2 --- /dev/null +++ b/src/dlangui/core/logger.d @@ -0,0 +1,78 @@ +module dlangui.core.logger; + +import std.stdio; +import std.datetime; + +enum LogLevel : int { + Fatal, + Error, + Warn, + Info, + Debug, + Trace +} + +__gshared LogLevel logLevel = LogLevel.Info; +__gshared std.stdio.File logFile; + +public void setLogLevel(LogLevel level) { + logLevel = level; +} + +public void setStdoutLogger() { + logFile = stdout; +} + +public void setStderrLogger() { + logFile = stderr; +} + +public void setFileLogger(File file) { + logFile = file; +} + +class Log { + public static string logLevelName(LogLevel level) { + switch (level) { + case LogLevel.Fatal: return "F"; + case LogLevel.Error: return "E"; + case LogLevel.Warn: return "W"; + case LogLevel.Info: return "I"; + case LogLevel.Debug: return "D"; + case LogLevel.Trace: return "V"; + default: return "?"; + } + } + public static void log(S...)(LogLevel level, S args) { + if (logLevel >= level && logFile.isOpen) { + SysTime ts = Clock.currTime(); + logFile.writef("%04d-%02d-%02d %02d:%02d:%02d.%03d %s ", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fracSec.msecs, logLevelName(level)); + logFile.writeln(args); + logFile.flush(); + } + } + public static void v(S...)(S args) { + if (logLevel >= LogLevel.Trace && logFile.isOpen) + log(LogLevel.Trace, args); + } + public static void d(S...)(S args) { + if (logLevel >= LogLevel.Debug && logFile.isOpen) + log(LogLevel.Debug, args); + } + public static void i(S...)(S args) { + if (logLevel >= LogLevel.Info && logFile.isOpen) + log(LogLevel.Info, args); + } + public static void w(S...)(S args) { + if (logLevel >= LogLevel.Warn && logFile.isOpen) + log(LogLevel.Warn, args); + } + public static void e(S...)(S args) { + if (logLevel >= LogLevel.Error && logFile.isOpen) + log(LogLevel.Error, args); + } + public static void f(S...)(S args) { + if (logLevel >= LogLevel.Fatal && logFile.isOpen) + log(LogLevel.Fatal, args); + } +} diff --git a/src/dlangui/graphics/fonts.d b/src/dlangui/graphics/fonts.d index d1c8c65e..fd1b87f3 100644 --- a/src/dlangui/graphics/fonts.d +++ b/src/dlangui/graphics/fonts.d @@ -23,10 +23,10 @@ alias FontRef = Ref!Font; class FontManager { static __gshared FontManager _instance; - public @property void instance(FontManager manager) { + public static @property void instance(FontManager manager) { _instance = manager; } - public @property FontManager instance() { + public static @property FontManager instance() { return _instance; } abstract public Font getFont(int size, int weight, bool italic, FontFamily family, string face); diff --git a/src/dlangui/platforms/windows/winapp.d b/src/dlangui/platforms/windows/winapp.d index 4a74e6c1..4f651781 100644 --- a/src/dlangui/platforms/windows/winapp.d +++ b/src/dlangui/platforms/windows/winapp.d @@ -11,6 +11,7 @@ import std.stdio; import dlangui.platforms.common.platform; import dlangui.graphics.drawbuf; import dlangui.graphics.fonts; +import dlangui.core.logger; pragma(lib, "gdi32.lib"); pragma(lib, "user32.lib"); @@ -43,6 +44,10 @@ class Win32Font : Font { _baseline = 0; _size = 0; } + if (_drawbuf !is null) { + destroy(_drawbuf); + _drawbuf = null; + } } public this(HFONT hfont, int size, int height, int weight, bool italic, string face, FontFamily family, int baseline) { _hfont = hfont; @@ -61,7 +66,7 @@ class Win32Font : Font { public @property override bool italic() { return _italic; } public @property override string face() { return _face; } public @property override FontFamily family() { return _family; } - public @property override bool isNull() { return _hfont !is null; } + public @property override bool isNull() { return _hfont is null; } public bool create(const LOGFONTA * logfont) { if (!isNull()) clear(); @@ -88,60 +93,76 @@ class Win32Font : Font { int i = 0; for (;_logfont.lfFaceName[i]; i++) { } - _face = _logfont.lfFaceName[0..i].dup; + _face = cast(string)(_logfont.lfFaceName[0..i].dup); _height = tm.tmHeight; _baseline = _height - tm.tmDescent; return true; } } + + class Win32FontManager : FontManager { public this() { instance = this; init(); } public bool init() { + Log.i("Win32FontManager.init()"); Win32ColorDrawBuf drawbuf = new Win32ColorDrawBuf(1,1); LOGFONTA lf; - lf.lfCharSet = ANSI_CHARSET; + lf.lfCharSet = ANSI_CHARSET; //DEFAULT_CHARSET; + lf.lfFaceName[0] = 0; + HDC dc = drawbuf.dc; int res = EnumFontFamiliesExA( - drawbuf.dc, // handle to DC + dc, // handle to DC &lf, // font information &LVWin32FontEnumFontFamExProc, // callback function (FONTENUMPROC) cast(LPARAM)(cast(void*)this), // additional data 0U // not used; must be 0 ); - + destroy(drawbuf); + Log.i("EnumFontFamiliesExA returned ", res); return res!=0; } public override Font getFont(int size, int weight, bool italic, FontFamily family, string face) { // TODO: return null; } - public bool registerFont(const LOGFONTA * logfont) { + public bool registerFont(FontFamily family, string fontFace, const LOGFONTA * logfont) { + Log.d("registerFont(", family, ",", fontFace, ")"); return true; } } +string fromStringz(const(char[]) s) { + int i = 0; + while(s[i]) + i++; + return cast(string)(s[0..i].dup); +} // definition -//extern(Windows) { +extern(Windows) { int LVWin32FontEnumFontFamExProc( const (LOGFONTA) *lf, // logical-font data const (TEXTMETRICA) *lpntme, // physical-font data //ENUMLOGFONTEX *lpelfe, // logical-font data //NEWTEXTMETRICEX *lpntme, // physical-font data - DWORD FontType, // type of font + DWORD fontType, // type of font LPARAM lParam // application-defined data ) { // - if (FontType == TRUETYPE_FONTTYPE) + //Log.d("LVWin32FontEnumFontFamExProc fontType=", fontType); + if (fontType == TRUETYPE_FONTTYPE) { void * p = cast(void*)lParam; Win32FontManager fontman = cast(Win32FontManager)p; - Win32Font fnt; + string face = fromStringz(lf.lfFaceName); + Log.d("face:", face); + Win32Font fnt = new Win32Font(); //if (strcmp(lf->lfFaceName, "Courier New")) // return 1; if (fnt.create(lf)) @@ -154,13 +175,14 @@ class Win32FontManager : FontManager { //if (!fnt.getGlyphInfo( chars[i], &glyph, L' ' )) //def_char // return 1; } - fontman.registerFont(lf); + fontman.registerFont(fnt.family, fnt.face, lf); } fnt.clear(); + destroy(fnt); } return 1; } -//} +} extern (C) int UIAppMain(); @@ -288,9 +310,12 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) { + setFileLogger(std.stdio.File("ui.log", "w")); + setLogLevel(LogLevel.Trace); + _cmdShow = iCmdShow; _hInstance = hInstance; - writeln("Creating window"); + Log.d("Inside myWinMain"); string appName = "HelloWin"; @@ -300,6 +325,8 @@ int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int return 0; } Platform.setInstance(platform); + Win32FontManager fontMan = new Win32FontManager(); + FontManager.instance = fontMan; return UIAppMain(); } @@ -364,41 +391,41 @@ class Win32ColorDrawBuf : ColorDrawBufBase { } } -void drawBuf2DC(HDC dc, int x, int y, DrawBuf buf) -{ - uint * drawpixels; - HDC drawdc; - HBITMAP drawbmp; - - int buf_width = buf.width(); - int bytesPerRow = buf_width * 4; - BITMAPINFO bmi; - //memset( &bmi, 0, sizeof(bmi) ); - bmi.bmiHeader.biSize = (bmi.bmiHeader.sizeof); - bmi.bmiHeader.biWidth = buf_width; - bmi.bmiHeader.biHeight = buf.height; - bmi.bmiHeader.biPlanes = 1; - bmi.bmiHeader.biBitCount = 32; - bmi.bmiHeader.biCompression = BI_RGB; - bmi.bmiHeader.biSizeImage = 0; - bmi.bmiHeader.biXPelsPerMeter = 1024; - bmi.bmiHeader.biYPelsPerMeter = 1024; - bmi.bmiHeader.biClrUsed = 0; - bmi.bmiHeader.biClrImportant = 0; - drawbmp = CreateDIBSection( NULL, &bmi, DIB_RGB_COLORS, cast(void**)(&drawpixels), NULL, 0 ); - drawdc = CreateCompatibleDC(NULL); - SelectObject(drawdc, drawbmp); - for (int yy=0; yy < buf.height; yy++) - { - uint * src = buf.scanLine(yy); - uint * dst = drawpixels + (buf.height - 1 - yy) * buf.width; - for (int xx = 0; xx < buf_width; xx++) - dst[xx] = src[xx]; - } - BitBlt( dc, x, y, buf_width, buf.height, drawdc, 0, 0, SRCCOPY); - DeleteObject( drawbmp ); - DeleteObject( drawdc ); -} +//void drawBuf2DC(HDC dc, int x, int y, DrawBuf buf) +//{ +// uint * drawpixels; +// HDC drawdc; +// HBITMAP drawbmp; +// +// int buf_width = buf.width(); +// int bytesPerRow = buf_width * 4; +// BITMAPINFO bmi; +// //memset( &bmi, 0, sizeof(bmi) ); +// bmi.bmiHeader.biSize = (bmi.bmiHeader.sizeof); +// bmi.bmiHeader.biWidth = buf_width; +// bmi.bmiHeader.biHeight = buf.height; +// bmi.bmiHeader.biPlanes = 1; +// bmi.bmiHeader.biBitCount = 32; +// bmi.bmiHeader.biCompression = BI_RGB; +// bmi.bmiHeader.biSizeImage = 0; +// bmi.bmiHeader.biXPelsPerMeter = 1024; +// bmi.bmiHeader.biYPelsPerMeter = 1024; +// bmi.bmiHeader.biClrUsed = 0; +// bmi.bmiHeader.biClrImportant = 0; +// drawbmp = CreateDIBSection( NULL, &bmi, DIB_RGB_COLORS, cast(void**)(&drawpixels), NULL, 0 ); +// drawdc = CreateCompatibleDC(NULL); +// SelectObject(drawdc, drawbmp); +// for (int yy=0; yy < buf.height; yy++) +// { +// uint * src = buf.scanLine(yy); +// uint * dst = drawpixels + (buf.height - 1 - yy) * buf.width; +// for (int xx = 0; xx < buf_width; xx++) +// dst[xx] = src[xx]; +// } +// BitBlt( dc, x, y, buf_width, buf.height, drawdc, 0, 0, SRCCOPY); +// DeleteObject( drawbmp ); +// DeleteObject( drawdc ); +//} extern(Windows) diff --git a/winmain.d b/winmain.d index 5108b8ae..ed993cb8 100644 --- a/winmain.d +++ b/winmain.d @@ -2,9 +2,13 @@ module winmain; import dlangui.platforms.common.platform; import dlangui.widgets.widget; - +import dlangui.core.logger; +import std.stdio; extern (C) int UIAppMain() { + Log.d("Some debug message"); + Log.e("Sample error #", 22); + Window window = Platform.instance().createWindow("My Window", null); Widget myWidget = new Widget(); window.mainWidget = myWidget;