123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /*
- * The MiniQt Library
- *
- * Copyright (C) 1999 Sandro Sigala <ssigala@globalnet.it>
- */
- #include <stdio.h>
- #include <string.h>
- #include <windows.h>
- #include "qwidget.h"
- #include "qapplication.h"
- #define MAX_WINDOW_HANDLES 128
- static int wndcounter = 0;
- static QWidget *regwnd[MAX_WINDOW_HANDLES];
- static int numregwnd = 0;
- struct QWidgetPrivate {
- Qt::WFlags flags;
- Qt::WState state;
- QString caption;
- int x, y, width, height;
- char wndName[32];
- WId winid;
- };
- static LRESULT CALLBACK windowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- QWidget *w = QWidget::find(hwnd);
- if (!w) return DefWindowProc(hwnd, uMsg, wParam, lParam);
- switch (uMsg) {
- case WM_MOVE:
- w->movedTo((int)LOWORD(lParam), (int)HIWORD(lParam));
- break;
- case WM_SIZE:
- w->resizedTo((int)LOWORD(lParam), (int)HIWORD(lParam));
- break;
- case WM_DESTROY:
- if (w == qApp->mainWidget())
- PostQuitMessage(0);
- break;
- case WM_COMMAND:
- if (HIWORD(wParam) == BN_CLICKED) {
- QWidget *btn = QWidget::find((HWND)lParam);
- // Simulate a press and release
- QMouseEvent *e = new QMouseEvent(QEvent::MouseButtonPress);
- btn->event(e);
- e = new QMouseEvent(QEvent::MouseButtonRelease);
- btn->event(e);
- break;
- }
- break;
- }
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- QWidget::QWidget(QWidget *parent, const char *name, WFlags f)
- : QObject(parent, name)
- {
- #ifdef DEBUG
- qDebug("obj", "QWidget::constructor of \"%s\"", name ? name : "unnamed");
- #endif
- d = new QWidgetPrivate;
- d->flags = f;
- d->state = 0;
- d->x = d->y = d->width = d->height = 0;
- d->caption = "nocaption";
- if ((f & WType_Child) == 0) {
- if (parent == 0)
- d->flags |= WType_TopLevel;
- WNDCLASS wc;
- memset(&wc, 0, sizeof(WNDCLASS));
- wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
- wc.lpfnWndProc = windowProc;
- wc.hInstance = hInstance;
- wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
- sprintf(d->wndName, "MainWClass%d", ++wndcounter);
- wc.lpszClassName = d->wndName;
- wc.lpszMenuName = NULL;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- if (!RegisterClass(&wc))
- qFatal("cannot register window");
- HWND hwnd = CreateWindowEx(
- 0, // no extended styles
- d->wndName, // class name
- d->caption.c_str(), // window name
- WS_OVERLAPPEDWINDOW, // style
- CW_USEDEFAULT, // geometry
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- (HWND)NULL, // no parent or owner window
- (HMENU)NULL, // class menu used
- hInstance, // instance handle
- NULL); // no window creation data
- if (hwnd == (HWND)0)
- qFatal("cannot create window");
- setWinId(hwnd);
- registerWinId(hwnd, this);
- }
- if (parent && parent->isVisible())
- show();
- }
- QWidget::~QWidget()
- {
- #ifdef DEBUG
- qDebug("obj", "QWidget::destructor of \"%s\"", name("unnamed"));
- #endif
- if (winId() != 0) {
- unregisterWinId(winId());
- DestroyWindow(winId());
- }
- delete d;
- }
- WId QWidget::winId()
- {
- return d->winid;
- }
- void QWidget::setWinId(WId id)
- {
- d->winid = id;
- }
- QWidget *QWidget::find(WId id)
- {
- for (int i = 0; i < numregwnd; ++i)
- if (regwnd[i] && regwnd[i]->winId() == id)
- return regwnd[i];
- return 0;
- }
- QWidget *QWidget::parentWidget() const
- {
- return (QWidget *)parent();
- }
- bool QWidget::isTopLevel() const
- {
- return d->flags & WType_TopLevel;
- }
- bool QWidget::isWidgetType() const
- {
- return TRUE;
- }
- bool QWidget::isVisible() const
- {
- return d->state & WState_Visible;
- }
- void QWidget::setCaption(const QString& caption)
- {
- d->caption = caption;
- SetWindowText(d->winid, caption);
- }
- const QString& QWidget::caption() const
- {
- return d->caption;
- }
- void QWidget::show()
- {
- ShowWindow(d->winid, SW_SHOW);
- d->state |= WState_Visible;
- QObjectList olist = *children();
- for (QObject *o = olist.first(); o != 0; o = olist.next()) {
- if (o->isWidgetType())
- ((QWidget *)o)->show();
- }
- }
- void QWidget::hide()
- {
- ShowWindow(d->winid, SW_HIDE);
- d->state &= ~WState_Visible;
- QObjectList olist = *children();
- for (QObject *o = olist.first(); o != 0; o = olist.next()) {
- if (o->isWidgetType())
- ((QWidget *)o)->hide();
- }
- }
- bool QWidget::close()
- {
- return FALSE;
- }
- void QWidget::event(QEvent *e)
- {
- switch (e->type()) {
- case QEvent::None:
- delete e;
- break;
- case QEvent::MouseButtonPress: {
- QMouseEvent *me = (QMouseEvent *)e;
- mousePressEvent(me);
- delete me;
- break;
- }
- case QEvent::MouseButtonRelease: {
- QMouseEvent *me = (QMouseEvent *)e;
- mouseReleaseEvent(me);
- delete me;
- break;
- }
- }
- }
- void QWidget::mousePressEvent(QMouseEvent * /*e*/)
- {
- }
- void QWidget::mouseReleaseEvent(QMouseEvent * /*e*/)
- {
- }
- void QWidget::movedTo(int x, int y)
- {
- d->x = x;
- d->y = y;
- }
- void QWidget::resizedTo(int width, int height)
- {
- d->width = width;
- d->height = height;
- }
- void QWidget::move(int x, int y)
- {
- d->x = x;
- d->y = y;
- MoveWindow(d->winid, d->x, d->y, d->width, d->height, TRUE);
- }
- void QWidget::resize(int width, int height)
- {
- d->width = width;
- d->height = height;
- MoveWindow(d->winid, d->x, d->y, d->width, d->height, TRUE);
- }
- void QWidget::setGeometry(int x, int y, int width, int height)
- {
- d->x = x;
- d->y = y;
- d->width = width;
- d->height = height;
- MoveWindow(d->winid, d->x, d->y, d->width, d->height, TRUE);
- }
- void QWidget::registerWinId(WId id, QWidget *w)
- {
- #ifdef DEBUG
- qDebug("wid", "registering window ID %p \"%s\"", id, w->name());
- #endif
- for (int i = 0; i < numregwnd; ++i)
- if (!regwnd[i]) {
- regwnd[i] = w;
- return;
- }
- if (numregwnd >= MAX_WINDOW_HANDLES)
- qFatal("maximum number of window handles exceed");
- else
- regwnd[numregwnd++] = w;
- }
- void QWidget::unregisterWinId(WId id)
- {
- #ifdef DEBUG
- qDebug("wid", "unregistering window ID %p", id);
- #endif
- for (int i = 0; i < numregwnd; ++i)
- if (regwnd[i] && regwnd[i]->winId() == id) {
- regwnd[i] = 0;
- break;
- }
- }
|