diff --git a/examples/example1/res/mdpi/dlangui-logo1.png b/examples/example1/res/mdpi/dlangui-logo1.png new file mode 100644 index 00000000..84593ee7 Binary files /dev/null and b/examples/example1/res/mdpi/dlangui-logo1.png differ diff --git a/examples/example1/src/main.d b/examples/example1/src/main.d index 5996a12d..15f947a1 100644 --- a/examples/example1/src/main.d +++ b/examples/example1/src/main.d @@ -469,6 +469,7 @@ extern (C) int UIAppMain(string[] args) { } else { window.mainWidget = (new Button()).text("sample button"); } + window.windowIcon = drawableCache.getImage("dlangui-logo1"); window.show(); //window.windowCaption = "New Window Caption"; // run message loop diff --git a/src/dlangui/graphics/drawbuf.d b/src/dlangui/graphics/drawbuf.d index 9982bbc6..059013ba 100644 --- a/src/dlangui/graphics/drawbuf.d +++ b/src/dlangui/graphics/drawbuf.d @@ -823,9 +823,21 @@ class GrayDrawBuf : DrawBuf { class ColorDrawBuf : ColorDrawBufBase { uint[] _buf; + /// create ARGB8888 draw buf of specified width and height this(int width, int height) { resize(width, height); } + /// create copy of ColorDrawBuf + this(ColorDrawBuf v) { + this(v.width, v.height); + _buf.length = v._buf.length; + for (int i = 0; i < _buf.length; i++) + _buf[i] = v._buf[i]; + } + void invertAlpha() { + foreach(pixel; _buf) + pixel ^= 0xFF000000; + } override uint * scanLine(int y) { if (y >= 0 && y < _dy) return _buf.ptr + _dx * y; diff --git a/src/dlangui/graphics/resources.d b/src/dlangui/graphics/resources.d index 75ab9fe1..fe84524a 100644 --- a/src/dlangui/graphics/resources.d +++ b/src/dlangui/graphics/resources.d @@ -886,6 +886,14 @@ class DrawableCache { Log.w("resource ", id, " is not found"); return null; } + /// get image (DrawBuf) from imageCache by resource id + DrawBufRef getImage(string id) { + DrawBufRef res; + string fname = findResource(id); + if (fname.endsWith(".png") || fname.endsWith(".jpg")) + return imageCache.get(fname); + return res; + } this() { debug Log.i("Creating DrawableCache"); } diff --git a/src/dlangui/platforms/common/platform.d b/src/dlangui/platforms/common/platform.d index ac344552..6fd3fb42 100644 --- a/src/dlangui/platforms/common/platform.d +++ b/src/dlangui/platforms/common/platform.d @@ -69,6 +69,9 @@ class Window { abstract @property dstring windowCaption(); /// sets window caption abstract @property void windowCaption(dstring caption); + /// sets window icon + abstract @property void windowIcon(DrawBufRef icon); + /// requests layout for main widget and popups void requestLayout() { if (_mainWidget) diff --git a/src/dlangui/platforms/sdl/sdlapp.d b/src/dlangui/platforms/sdl/sdlapp.d index 7d4af51c..e48b512e 100644 --- a/src/dlangui/platforms/sdl/sdlapp.d +++ b/src/dlangui/platforms/sdl/sdlapp.d @@ -87,13 +87,14 @@ version(USE_SDL) { protected uint _flags; bool create(uint flags) { _flags = flags; - uint windowFlags = 0; + uint windowFlags = SDL_WINDOW_HIDDEN; if (flags & WindowFlag.Resizable) windowFlags |= SDL_WINDOW_RESIZABLE; if (flags & WindowFlag.Fullscreen) windowFlags |= SDL_WINDOW_FULLSCREEN; - if (flags & WindowFlag.Modal) - windowFlags |= SDL_WINDOW_INPUT_GRABBED; + // TODO: implement modal behavior + //if (flags & WindowFlag.Modal) + // windowFlags |= SDL_WINDOW_INPUT_GRABBED; version(USE_OPENGL) { if (_enableOpengl) windowFlags |= SDL_WINDOW_OPENGL; @@ -178,6 +179,27 @@ version(USE_SDL) { SDL_SetWindowTitle(_win, toUTF8(_caption).toStringz); } + /// sets window icon + @property override void windowIcon(DrawBufRef buf) { + ColorDrawBuf icon = cast(ColorDrawBuf)buf.get; + if (!icon) { + Log.e("Trying to set null icon for window"); + return; + } + icon = new ColorDrawBuf(icon); + icon.invertAlpha(); + SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(icon.scanLine(0), icon.width, icon.height, 32, icon.width * 4, 0x00ff0000,0x0000ff00,0x000000ff,0xff000000); + if (surface) { + // The icon is attached to the window pointer + SDL_SetWindowIcon(_win, surface); + // ...and the surface containing the icon pixel data is no longer required. + SDL_FreeSurface(surface); + } else { + Log.e("failed to set window icon"); + } + destroy(icon); + } + /// after drawing, call to schedule redraw if animation is active override void scheduleAnimation() { invalidate();