support window icon

This commit is contained in:
Vadim Lopatin 2014-05-20 09:34:37 +04:00
parent f6d7350e77
commit 5b528ec5dc
6 changed files with 49 additions and 3 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@ -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

View File

@ -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;

View File

@ -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");
}

View File

@ -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)

View File

@ -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();