From 5b528ec5dc146a0e4b22e64e78a29d640b5b402d Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Tue, 20 May 2014 09:34:37 +0400 Subject: [PATCH] support window icon --- examples/example1/res/mdpi/dlangui-logo1.png | Bin 0 -> 1970 bytes examples/example1/src/main.d | 1 + src/dlangui/graphics/drawbuf.d | 12 ++++++++ src/dlangui/graphics/resources.d | 8 ++++++ src/dlangui/platforms/common/platform.d | 3 ++ src/dlangui/platforms/sdl/sdlapp.d | 28 +++++++++++++++++-- 6 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 examples/example1/res/mdpi/dlangui-logo1.png diff --git a/examples/example1/res/mdpi/dlangui-logo1.png b/examples/example1/res/mdpi/dlangui-logo1.png new file mode 100644 index 0000000000000000000000000000000000000000..84593ee72490a84f89215cf8bc5f9465d8d66839 GIT binary patch literal 1970 zcmV;j2Tk~iP)mR02y>e zSad^gZEa<4bO1wgWnpw>WFU8GbZ8()Nlj2!fese{00$aLL_t(o!*!Qm=w($E$3N@O zIrsiF%hJlq%uLHb@SzAbFw<0mNngY;h`}(xCrYN2Kme&2SxOYG`Dz`YfX4 z7|`rw&1kfZmO^QFj|iTA_852k@M#VloU?K*d7zZcjklaSKKEVb;`#)1cq@<37t*wZrT)ZirSQC z{?l{kk1{8}RoHdMX*g2VOT`cb6+y%>Rf-s@hNz>GP;)FY5k1yCRKzGznhqGIO@g&U ztwY68b*}&V@c>LGi4UIPeEzBvK?K)@em4~pF;o?46IC74Ojt)m0ju&VKvpHxDba)o ztqYpr-ZyV_h<5C{E#W?;>slW0DXNZ(Dxy|@ZO5vHnxVx}>)NhM1waK*MA4G!`%-8w zK*g%3)G@0d+^RRs9M!IE%282Vw=L{Q=}^s>Wl9vnG8T8o)N?MD(dbahmN4s)EC@nTMT!x{SH|?+S_?bdGIugY zhFQOP9O|e!UWwMBS}?0;O_Xg$q!Be2MO!Le9WzBl5YhhwplgLm1!2}>+To@hVTB+Q z^fk9fZ1;c&MHaiyE2$cw>!(#%R&@}!N##T1f_aZ>1K~Yx>J>m_!lHSAZAL_lBEz_Q zjV%Ctttsag0NoT*8#&+$AY+7Bt6u2ay0Ohj=To!{WN}IiM5)EgRsgi3WcB@|GJfF& z?M6?y^|*Oo0YnfM6+o;4Fz=C~sAh7p7W-QObkmx$RJL)$)M^B%u|RY2qdbIyb+T-n^GiEqC-oqqGL;_ z)+3O{gJu3qM`cvCmLnYVg+O_awd$2DW1>86y2k4pwNRN2!P;bv5^1cUUwiG?X8Cp8 zgsxxPO!8YiBWh#Z*Lyc|K`Rkj^WYP@mN3qI|7oL)JP8qwCh#_wVP&?`NF) zo~3WoZ2-D%ihE1XKEcN(WrDi*m63}M#(T`BTzUDWqmgg__!s>3u_sucW!5(`e|u~n z-}~tUTMwpOJo-c>tx@b`Z_&hH=I z!{L`+=EiS*=M77>%Pu^hD=xbPQEkrO9tCQ)nWPqp$b3Pszn{w4tMV%*F2t@ z)~ndKxJIhVHv%cOB#Fg+SwCfLQfqBc%RSLn@~LC3U|H0UZZ>TN#vDXz|0afpp}FdQ zu@?KS`iUr22V%2#h^6vfYNv{d0=2|btzMAv7-~rV51;yw!aK19e*gdg07*qoM6N<$ Ef(gdF!~g&Q literal 0 HcmV?d00001 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();