From ee8c2cf0dc3885751eb3e4b2c11da2576cac1371 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 28 Dec 2012 19:55:42 +0100 Subject: Move native code to seperate 'NativeWindow' classes. --- plugingui/Makefile.am.plugingui | 2 + plugingui/guievent.h | 3 +- plugingui/nativewindow.h | 55 ++++++++ plugingui/nativewindow_win32.cc | 134 +++++++++++++++++++ plugingui/nativewindow_win32.h | 58 ++++++++ plugingui/nativewindow_x11.cc | 233 ++++++++++++++++++++++++++++++++ plugingui/nativewindow_x11.h | 62 +++++++++ plugingui/window.cc | 290 +++------------------------------------- plugingui/window.h | 12 +- 9 files changed, 570 insertions(+), 279 deletions(-) create mode 100644 plugingui/nativewindow.h create mode 100644 plugingui/nativewindow_win32.cc create mode 100644 plugingui/nativewindow_win32.h create mode 100644 plugingui/nativewindow_x11.cc create mode 100644 plugingui/nativewindow_x11.h (limited to 'plugingui') diff --git a/plugingui/Makefile.am.plugingui b/plugingui/Makefile.am.plugingui index 634c641..0e96dd2 100644 --- a/plugingui/Makefile.am.plugingui +++ b/plugingui/Makefile.am.plugingui @@ -1,4 +1,6 @@ PLUGIN_GUI_SOURCES = \ + $(top_srcdir)/plugingui/nativewindow_x11.cc \ + $(top_srcdir)/plugingui/nativewindow_win32.cc \ $(top_srcdir)/plugingui/plugingui.cc \ $(top_srcdir)/plugingui/globalcontext.cc \ $(top_srcdir)/plugingui/label.cc \ diff --git a/plugingui/guievent.h b/plugingui/guievent.h index 3550003..0fb3ef2 100644 --- a/plugingui/guievent.h +++ b/plugingui/guievent.h @@ -33,6 +33,7 @@ #ifdef X11 #include +#include #endif/*X11*/ namespace GUI { @@ -52,7 +53,7 @@ public: virtual Type type() = 0; #ifdef X11 - Window window_id; + ::Window window_id; #endif/*X11*/ }; diff --git a/plugingui/nativewindow.h b/plugingui/nativewindow.h new file mode 100644 index 0000000..9225f28 --- /dev/null +++ b/plugingui/nativewindow.h @@ -0,0 +1,55 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * nativewindow.h + * + * Fri Dec 28 18:46:01 CET 2012 + * Copyright 2012 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __DRUMGIZMO_NATIVEWINDOW_H__ +#define __DRUMGIZMO_NATIVEWINDOW_H__ + +#include + +#include "globalcontext.h" + +namespace GUI { + +class NativeWindow { +public: + NativeWindow(GlobalContext *c) : gctx(c) {} + virtual ~NativeWindow() {} + + virtual void resize(int width, int height) = 0; + virtual void move(int x, int y) = 0; + virtual void show() = 0; + virtual void setCaption(const std::string &caption) = 0; + virtual void hide() = 0; + virtual void handleBuffer() = 0; + virtual void redraw() = 0; + +protected: + GlobalContext *gctx; +}; + +}; + +#endif/*__DRUMGIZMO_NATIVEWINDOW_H__*/ diff --git a/plugingui/nativewindow_win32.cc b/plugingui/nativewindow_win32.cc new file mode 100644 index 0000000..a98ef4c --- /dev/null +++ b/plugingui/nativewindow_win32.cc @@ -0,0 +1,134 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * nativewindow_win32.cc + * + * Fri Dec 28 18:45:52 CET 2012 + * Copyright 2012 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "nativewindow_win32.h" + +#ifdef WIN32 + +#include "window.h" + +// Delared in eventhandler.cc +LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp); + +GUI::NativeWindowWin32::NativeWindowWin32(GlobalContext *gctx, + GUI::Window *window) + : GUI::NativeWindow(gctx) +{ + this->window = window; + + WNDCLASSEX wcex; + WNDID wndId; + + gctx->m_hwnd = 0; + gctx->m_className = NULL; + + memset(&wcex, 0, sizeof(wcex)); + + //Time to register a window class. + //Generic flags and everything. cbWndExtra is the size of a pointer to an + // object - we need this in the wndproc handler. + + wcex.cbSize = sizeof(WNDCLASSEX); + wcex.style = 0;//class_style; + wcex.lpfnWndProc = (WNDPROC)dialogProc; + wcex.hCursor = LoadCursor(NULL, IDC_ARROW); + // Set data: + wcex.cbWndExtra = sizeof(EventHandler*); // Size of data. + wcex.hInstance = GetModuleHandle(NULL); + + // if(ex_style && WS_EX_TRANSPARENT == WS_EX_TRANSPARENT) { + // wcex.hbrBackground = NULL; + // } else { + wcex.hbrBackground = NULL;//(HBRUSH) COLOR_BACKGROUND + 1; + // } + + wcex.lpszClassName = gctx->m_className = strdup("DrumGizmoClass"); + + RegisterClassEx(&wcex); + + /* + if(parent) { + style = style | WS_CHILD; + wndId = parent->getWndId(); + } else { + */ + //style = style | WS_OVERLAPPEDWINDOW; + wndId = 0; + // } + + gctx->m_hwnd = CreateWindowEx(NULL/*ex_style*/, gctx->m_className, + "DGBasisWidget", + (WS_OVERLAPPEDWINDOW | WS_VISIBLE), + window->x(), window->y(), + window->width(), window->height(), + wndId, NULL, + GetModuleHandle(NULL), NULL); + + SetWindowLong(gctx->m_hwnd, GWL_USERDATA, (LONG)gctx->eventhandler); +} + +GUI::NativeWindowWin32::~NativeWindowWin32() +{ + UnregisterClass(gctx->m_className, GetModuleHandle(NULL)); + free(gctx->m_className); +} + +void GUI::NativeWindowWin32::resize(int width, int height) +{ + SetWindowPos(gctx->m_hwnd, NULL, -1, -1, (int)width, (int)height + 27, + SWP_NOMOVE); +} + +void GUI::NativeWindowWin32::move(int x, int y) +{ + SetWindowPos(gctx->m_hwnd, NULL, (int)x, (int)y, -1, -1, SWP_NOSIZE); +} + +void GUI::NativeWindowWin32::show() +{ + ShowWindow(gctx->m_hwnd, SW_SHOW); +} + +void GUI::NativeWindowWin32::handleBuffer() +{ +} + +void GUI::NativeWindowWin32::hide() +{ + ShowWindow(gctx->m_hwnd, SW_HIDE); +} + +void GUI::NativeWindowWin32::redraw() +{ + RedrawWindow(gctx->m_hwnd, NULL, NULL, RDW_ERASE|RDW_INVALIDATE); + UpdateWindow(gctx->m_hwnd); +} + +void GUI::NativeWindowWin32::setCaption(const std::string &caption) +{ +} + +#endif/*WIN32*/ diff --git a/plugingui/nativewindow_win32.h b/plugingui/nativewindow_win32.h new file mode 100644 index 0000000..6039439 --- /dev/null +++ b/plugingui/nativewindow_win32.h @@ -0,0 +1,58 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * nativewindow_win32.h + * + * Fri Dec 28 18:45:51 CET 2012 + * Copyright 2012 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __DRUMGIZMO_NATIVEWINDOW_WIN32_H__ +#define __DRUMGIZMO_NATIVEWINDOW_WIN32_H__ + +#ifdef WIN32 + +#include "nativewindow.h" + +namespace GUI { + +class Window; +class NativeWindowWin32 : public NativeWindow { +public: + NativeWindowWin32(GlobalContext *gctx, GUI::Window *window); + ~NativeWindowWin32(); + + void resize(int width, int height); + void move(int x, int y); + void show(); + void setCaption(const std::string &caption); + void hide(); + void handleBuffer(); + void redraw(); + +private: + GUI::Window *window; +}; + +}; + +#endif/*WIN32*/ + +#endif/*__DRUMGIZMO_NATIVEWINDOW_WIN32_H__*/ diff --git a/plugingui/nativewindow_x11.cc b/plugingui/nativewindow_x11.cc new file mode 100644 index 0000000..91d7ed6 --- /dev/null +++ b/plugingui/nativewindow_x11.cc @@ -0,0 +1,233 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * nativewindow_x11.cc + * + * Fri Dec 28 18:45:57 CET 2012 + * Copyright 2012 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "nativewindow_x11.h" + +#ifdef X11 +#include +#include + +#include "window.h" + +GUI::NativeWindowX11::NativeWindowX11(GlobalContext *gctx, GUI::Window *window) + : GUI::NativeWindow(gctx) +{ + this->window = window; + buffer = NULL; + + // Get some colors + int blackColor = BlackPixel(gctx->display, DefaultScreen(gctx->display)); + + ::Window w = DefaultRootWindow(gctx->display); + + // Create the window + xwindow = XCreateSimpleWindow(gctx->display, + w, + window->x(), window->y(), + window->width(), window->height(), + 0, + blackColor, blackColor); + + XSelectInput(gctx->display, xwindow, + StructureNotifyMask | + PointerMotionMask | + ButtonPressMask | + ButtonReleaseMask | + KeyPressMask | + KeyReleaseMask| + ExposureMask | + StructureNotifyMask | + SubstructureNotifyMask); + + // register interest in the delete window message + gctx->wmDeleteMessage = XInternAtom(gctx->display, "WM_DELETE_WINDOW", false); + XSetWMProtocols(gctx->display, xwindow, &gctx->wmDeleteMessage, 1); + + // "Map" the window (that is, make it appear on the screen) + XMapWindow(gctx->display, xwindow); + + // Create a "Graphics Context" + gc = XCreateGC(gctx->display, xwindow, 0, NULL); +} + +GUI::NativeWindowX11::~NativeWindowX11() +{ + XDestroyWindow(gctx->display, xwindow); + //gctx->widgets.erase(window); +} + +void GUI::NativeWindowX11::resize(int width, int height) +{ + XResizeWindow(gctx->display, xwindow, width, height); +} + +void GUI::NativeWindowX11::move(int x, int y) +{ + XMoveWindow(gctx->display, xwindow, x, y); +} + +void GUI::NativeWindowX11::show() +{ + XMapWindow(gctx->display, xwindow); +} + +void GUI::NativeWindowX11::hide() +{ + XUnmapWindow(gctx->display, xwindow); +} + +static int get_byte_order (void) +{ + union { + char c[sizeof(short)]; + short s; + } order; + + order.s = 1; + if ((1 == order.c[0])) { + return LSBFirst; + } else { + return MSBFirst; + } +} + +static XImage *create_image_from_buffer(Display *dis, int screen, + unsigned char *buf, + int width, int height) +{ + int depth; + XImage *img = NULL; + Visual *vis; + double rRatio; + double gRatio; + double bRatio; + int outIndex = 0; + int i; + int numBufBytes = (3 * (width * height)); + + depth = DefaultDepth(dis, screen); + vis = DefaultVisual(dis, screen); + + rRatio = vis->red_mask / 255.0; + gRatio = vis->green_mask / 255.0; + bRatio = vis->blue_mask / 255.0; + + if (depth >= 24) { + size_t numNewBufBytes = (4 * (width * height)); + u_int32_t *newBuf = (u_int32_t *)malloc (numNewBufBytes); + + for (i = 0; i < numBufBytes; ++i) { + unsigned int r, g, b; + r = (buf[i] * rRatio); + ++i; + g = (buf[i] * gRatio); + ++i; + b = (buf[i] * bRatio); + + r &= vis->red_mask; + g &= vis->green_mask; + b &= vis->blue_mask; + + newBuf[outIndex] = r | g | b; + ++outIndex; + } + + img = XCreateImage (dis, + CopyFromParent, depth, + ZPixmap, 0, + (char *) newBuf, + width, height, + 32, 0 + ); + + } else if (depth >= 15) { + size_t numNewBufBytes = (2 * (width * height)); + u_int16_t *newBuf = (u_int16_t *)malloc (numNewBufBytes); + + for (i = 0; i < numBufBytes; ++i) { + unsigned int r, g, b; + + r = (buf[i] * rRatio); + ++i; + g = (buf[i] * gRatio); + ++i; + b = (buf[i] * bRatio); + + r &= vis->red_mask; + g &= vis->green_mask; + b &= vis->blue_mask; + + newBuf[outIndex] = r | g | b; + ++outIndex; + } + + img = XCreateImage(dis, CopyFromParent, depth, ZPixmap, 0, (char *) newBuf, + width, height, 16, 0); + } else { + //fprintf (stderr, "This program does not support displays with a depth less than 15."); + return NULL; + } + + XInitImage (img); + /*Set the client's byte order, so that XPutImage knows what to do with the data.*/ + /*The default in a new X image is the server's format, which may not be what we want.*/ + if ((LSBFirst == get_byte_order ())) { + img->byte_order = LSBFirst; + } else { + img->byte_order = MSBFirst; + } + + /*The bitmap_bit_order doesn't matter with ZPixmap images.*/ + img->bitmap_bit_order = MSBFirst; + + return img; +} + +void GUI::NativeWindowX11::handleBuffer() +{ + if(buffer) XDestroyImage(buffer); + buffer = + create_image_from_buffer(gctx->display, DefaultScreen(gctx->display), + window->wpixbuf.buf, + window->wpixbuf.width, + window->wpixbuf.height); +} + +void GUI::NativeWindowX11::redraw() +{ + // http://stackoverflow.com/questions/6384987/load-image-onto-a-window-using-xlib + if(buffer == NULL) window->updateBuffer(); + XPutImage(gctx->display, xwindow, gc, buffer, 0, 0, 0, 0, + window->width(), window->height()); + XFlush(gctx->display); +} + +void GUI::NativeWindowX11::setCaption(const std::string &caption) +{ +} + +#endif/*X11*/ + diff --git a/plugingui/nativewindow_x11.h b/plugingui/nativewindow_x11.h new file mode 100644 index 0000000..2e51861 --- /dev/null +++ b/plugingui/nativewindow_x11.h @@ -0,0 +1,62 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * nativewindow_x11.h + * + * Fri Dec 28 18:45:56 CET 2012 + * Copyright 2012 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __DRUMGIZMO_NATIVEWINDOW_X11_H__ +#define __DRUMGIZMO_NATIVEWINDOW_X11_H__ +#endif/*__DRUMGIZMO_NATIVEWINDOW_X11_H__*/ + +#ifdef X11 +#include + +#include "nativewindow.h" + +namespace GUI { + +class Window; +class NativeWindowX11 : public NativeWindow { +public: + NativeWindowX11(GlobalContext *gctx, GUI::Window *window); + ~NativeWindowX11(); + + void resize(int width, int height); + void move(int x, int y); + void show(); + void setCaption(const std::string &caption); + void hide(); + void handleBuffer(); + void redraw(); + +private: + ::Window xwindow; + GC gc; + XImage *buffer; + + GUI::Window *window; +}; + +}; + +#endif/*X11*/ diff --git a/plugingui/window.cc b/plugingui/window.cc index 3c6a816..bbeffa1 100644 --- a/plugingui/window.cc +++ b/plugingui/window.cc @@ -30,23 +30,21 @@ #include "img_back.h" -#ifdef X11 -#include -#include -#endif/*X11*/ - #include #include #include -GUI::Window *gwindow = NULL; +#ifdef X11 +#include "nativewindow_x11.h" +#endif/*X11*/ #ifdef WIN32 -// Delared in eventhandler.cc -LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp); +#include "nativewindow_win32.h" #endif/*WIN32*/ +GUI::Window *gwindow = NULL; + GUI::Window::Window(GlobalContext *gctx) : Widget(NULL), wpixbuf(640, 200) { @@ -63,102 +61,22 @@ GUI::Window::Window(GlobalContext *gctx) _buttonDownFocus = NULL; #ifdef X11 - buffer = NULL; - - // Get some colors - int blackColor = BlackPixel(gctx->display, DefaultScreen(gctx->display)); - - ::Window w = DefaultRootWindow(gctx->display); - - // Create the window - xwindow = XCreateSimpleWindow(gctx->display, - w, _x, _y, _width, _height, 0, - blackColor, blackColor); - - XSelectInput(gctx->display, xwindow, - StructureNotifyMask | - PointerMotionMask | - ButtonPressMask | - ButtonReleaseMask | - KeyPressMask | - KeyReleaseMask| - ExposureMask | - StructureNotifyMask | - SubstructureNotifyMask); - - // register interest in the delete window message - gctx->wmDeleteMessage = XInternAtom(gctx->display, "WM_DELETE_WINDOW", false); - XSetWMProtocols(gctx->display, xwindow, &gctx->wmDeleteMessage, 1); - - // "Map" the window (that is, make it appear on the screen) - XMapWindow(gctx->display, xwindow); - - // Create a "Graphics Context" - gc = XCreateGC(gctx->display, xwindow, 0, NULL); + native = new NativeWindowX11(gctx, this); #endif/*X11*/ #ifdef WIN32 - WNDCLASSEX wcex; - WNDID wndId; - - gctx->m_hwnd = 0; - gctx->m_className = NULL; - - memset(&wcex, 0, sizeof(wcex)); - - //Time to register a window class. - //Generic flags and everything. cbWndExtra is the size of a pointer to an - // object - we need this in the wndproc handler. - - wcex.cbSize = sizeof(WNDCLASSEX); - wcex.style = 0;//class_style; - wcex.lpfnWndProc = (WNDPROC)dialogProc; - wcex.hCursor = LoadCursor(NULL, IDC_ARROW); - // Set data: - wcex.cbWndExtra = sizeof(EventHandler*); // Size of data. - wcex.hInstance = GetModuleHandle(NULL); - - // if(ex_style && WS_EX_TRANSPARENT == WS_EX_TRANSPARENT) { - // wcex.hbrBackground = NULL; - // } else { - wcex.hbrBackground = NULL;//(HBRUSH) COLOR_BACKGROUND + 1; - // } - - wcex.lpszClassName = gctx->m_className = strdup("DrumGizmoClass"); - - RegisterClassEx(&wcex); - - /* - if(parent) { - style = style | WS_CHILD; - wndId = parent->getWndId(); - } else { - */ - //style = style | WS_OVERLAPPEDWINDOW; - wndId = 0; - // } - - gctx->m_hwnd = CreateWindowEx(NULL/*ex_style*/, gctx->m_className, - "DGBasisWidget", - (WS_OVERLAPPEDWINDOW | WS_VISIBLE), - _x, _y, _width, _height, wndId, NULL, - GetModuleHandle(NULL), NULL); - - SetWindowLong(gctx->m_hwnd, GWL_USERDATA, (LONG)gctx->eventhandler); + native = new NativeWindowWin32(gctx, this); #endif/*WIN32*/ } GUI::Window::~Window() { -#ifdef X11 - XDestroyWindow(gctx->display, xwindow); - //gctx->widgets.erase(window); -#endif/*X11*/ + delete native; +} -#ifdef WIN32 - UnregisterClass(gctx->m_className, GetModuleHandle(NULL)); - free(gctx->m_className); -#endif/*WIN32*/ +void GUI::Window::setCaption(std::string caption) +{ + native->setCaption(caption); } void GUI::Window::repaintEvent(GUI::RepaintEvent *e) @@ -170,28 +88,13 @@ void GUI::Window::repaintEvent(GUI::RepaintEvent *e) void GUI::Window::resize(size_t width, size_t height) { // printf("Window::resize(%d, %d)\n", width, height); - -#ifdef X11 - XResizeWindow(gctx->display, xwindow, width, height); -#endif/*X11*/ - -#ifdef WIN32 - SetWindowPos(gctx->m_hwnd, NULL, -1, -1, (int)width, (int)height + 27, - SWP_NOMOVE); -#endif/*WIN32*/ - + native->resize(width, height); Widget::resize(width, height); } void GUI::Window::move(size_t x, size_t y) { -#ifdef X11 - XMoveWindow(gctx->display, xwindow, x, y); -#endif/*X11*/ - -#ifdef WIN32 - SetWindowPos(gctx->m_hwnd, NULL, (int)x, (int)y, -1, -1, SWP_NOSIZE); -#endif/*WIN32*/ + native->move(x, y); // Make sure widget corrds are updated. Widget::move(x, y); @@ -205,24 +108,12 @@ size_t GUI::Window::height() { return _height; } void GUI::Window::show() { repaint_r(NULL); -#ifdef X11 - XMapWindow(gctx->display, xwindow); -#endif/*X11*/ - -#ifdef WIN32 - ShowWindow(gctx->m_hwnd, SW_SHOW); -#endif/*WIN32*/ + native->show(); } void GUI::Window::hide() { -#ifdef X11 - XUnmapWindow(gctx->display, xwindow); -#endif/*X11*/ - -#ifdef WIN32 - ShowWindow(gctx->m_hwnd, SW_HIDE); -#endif/*WIN32*/ + native->hide(); } GUI::Window *GUI::Window::window() @@ -250,115 +141,6 @@ void GUI::Window::endPaint() } } -#ifdef X11 -static int get_byte_order (void) -{ - union { - char c[sizeof(short)]; - short s; - } order; - - order.s = 1; - if ((1 == order.c[0])) { - return LSBFirst; - } else { - return MSBFirst; - } -} - -static XImage *create_image_from_buffer(Display *dis, int screen, - unsigned char *buf, - int width, int height) -{ - int depth; - XImage *img = NULL; - Visual *vis; - double rRatio; - double gRatio; - double bRatio; - int outIndex = 0; - int i; - int numBufBytes = (3 * (width * height)); - - depth = DefaultDepth(dis, screen); - vis = DefaultVisual(dis, screen); - - rRatio = vis->red_mask / 255.0; - gRatio = vis->green_mask / 255.0; - bRatio = vis->blue_mask / 255.0; - - if (depth >= 24) { - size_t numNewBufBytes = (4 * (width * height)); - u_int32_t *newBuf = (u_int32_t *)malloc (numNewBufBytes); - - for (i = 0; i < numBufBytes; ++i) { - unsigned int r, g, b; - r = (buf[i] * rRatio); - ++i; - g = (buf[i] * gRatio); - ++i; - b = (buf[i] * bRatio); - - r &= vis->red_mask; - g &= vis->green_mask; - b &= vis->blue_mask; - - newBuf[outIndex] = r | g | b; - ++outIndex; - } - - img = XCreateImage (dis, - CopyFromParent, depth, - ZPixmap, 0, - (char *) newBuf, - width, height, - 32, 0 - ); - - } else if (depth >= 15) { - size_t numNewBufBytes = (2 * (width * height)); - u_int16_t *newBuf = (u_int16_t *)malloc (numNewBufBytes); - - for (i = 0; i < numBufBytes; ++i) { - unsigned int r, g, b; - - r = (buf[i] * rRatio); - ++i; - g = (buf[i] * gRatio); - ++i; - b = (buf[i] * bRatio); - - r &= vis->red_mask; - g &= vis->green_mask; - b &= vis->blue_mask; - - newBuf[outIndex] = r | g | b; - ++outIndex; - } - - img = XCreateImage(dis, CopyFromParent, depth, ZPixmap, 0, (char *) newBuf, - width, height, 16, 0); - } else { - //fprintf (stderr, "This program does not support displays with a depth less than 15."); - return NULL; - } - - XInitImage (img); - /*Set the client's byte order, so that XPutImage knows what to do with the data.*/ - /*The default in a new X image is the server's format, which may not be what we want.*/ - if ((LSBFirst == get_byte_order ())) { - img->byte_order = LSBFirst; - } else { - img->byte_order = MSBFirst; - } - - /*The bitmap_bit_order doesn't matter with ZPixmap images.*/ - img->bitmap_bit_order = MSBFirst; - - return img; -} -#endif/*X11*/ - void GUI::Window::updateBuffer() { // printf("updateBuffer w:%d h:%d\n", width(), height()); @@ -380,13 +162,7 @@ void GUI::Window::updateBuffer() } pli++; } - -#ifdef X11 - if(buffer) XDestroyImage(buffer); - buffer = - create_image_from_buffer(gctx->display, DefaultScreen(gctx->display), - wpixbuf.buf, wpixbuf.width, wpixbuf.height); -#endif/*X11*/ + native->handleBuffer(); } void GUI::Window::resized(size_t w, size_t h) @@ -402,17 +178,7 @@ void GUI::Window::resized(size_t w, size_t h) void GUI::Window::redraw() { -#ifdef X11 - // http://stackoverflow.com/questions/6384987/load-image-onto-a-window-using-xlib - if(buffer == NULL) updateBuffer(); - XPutImage(gctx->display, xwindow, gc, buffer, 0, 0, 0, 0, width(), height()); - XFlush(gctx->display); -#endif/*X11*/ - -#ifdef WIN32 - RedrawWindow(gctx->m_hwnd, NULL, NULL, RDW_ERASE|RDW_INVALIDATE); - UpdateWindow(gctx->m_hwnd); -#endif/*WIN32*/ + native->redraw(); } GUI::Widget *GUI::Window::keyboardFocus() @@ -436,21 +202,3 @@ void GUI::Window::setButtonDownFocus(GUI::Widget *widget) _buttonDownFocus = widget; // repaint_r(NULL); } - - -#ifdef TEST_WINDOW -//Additional dependency files -//deps: -//Required cflags (autoconf vars may be used) -//cflags: -//Required link options (autoconf vars may be used) -//libs: -#include "test.h" - -TEST_BEGIN; - -// TODO: Put some testcode here (see test.h for usable macros). - -TEST_END; - -#endif/*TEST_WINDOW*/ diff --git a/plugingui/window.h b/plugingui/window.h index e1e49c7..8e61603 100644 --- a/plugingui/window.h +++ b/plugingui/window.h @@ -32,6 +32,7 @@ #include "globalcontext.h" #include "pixelbuffer.h" +#include "nativewindow.h" namespace GUI { @@ -51,6 +52,8 @@ public: size_t width(); size_t height(); + void setCaption(std::string caption); + void addChild(Widget *widget); void repaintEvent(GUI::RepaintEvent *e); @@ -73,20 +76,15 @@ public: GlobalContext *gctx; PixelBuffer wpixbuf; - -protected: void updateBuffer(); +protected: size_t refcount; Widget *_keyboardFocus; Widget *_buttonDownFocus; -#ifdef X11 - ::Window xwindow; - GC gc; - XImage *buffer; -#endif/*X11*/ + NativeWindow *native; }; -- cgit v1.2.3