qwidget.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * The MiniQt Library
  3. *
  4. * Copyright (C) 1999 Sandro Sigala <ssigala@globalnet.it>
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <windows.h>
  9. #include "qwidget.h"
  10. #include "qapplication.h"
  11. #define MAX_WINDOW_HANDLES 128
  12. static int wndcounter = 0;
  13. static QWidget *regwnd[MAX_WINDOW_HANDLES];
  14. static int numregwnd = 0;
  15. struct QWidgetPrivate {
  16. Qt::WFlags flags;
  17. Qt::WState state;
  18. QString caption;
  19. int x, y, width, height;
  20. char wndName[32];
  21. WId winid;
  22. };
  23. static LRESULT CALLBACK windowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  24. {
  25. QWidget *w = QWidget::find(hwnd);
  26. if (!w) return DefWindowProc(hwnd, uMsg, wParam, lParam);
  27. switch (uMsg) {
  28. case WM_MOVE:
  29. w->movedTo((int)LOWORD(lParam), (int)HIWORD(lParam));
  30. break;
  31. case WM_SIZE:
  32. w->resizedTo((int)LOWORD(lParam), (int)HIWORD(lParam));
  33. break;
  34. case WM_DESTROY:
  35. if (w == qApp->mainWidget())
  36. PostQuitMessage(0);
  37. break;
  38. case WM_COMMAND:
  39. if (HIWORD(wParam) == BN_CLICKED) {
  40. QWidget *btn = QWidget::find((HWND)lParam);
  41. // Simulate a press and release
  42. QMouseEvent *e = new QMouseEvent(QEvent::MouseButtonPress);
  43. btn->event(e);
  44. e = new QMouseEvent(QEvent::MouseButtonRelease);
  45. btn->event(e);
  46. break;
  47. }
  48. break;
  49. }
  50. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  51. }
  52. QWidget::QWidget(QWidget *parent, const char *name, WFlags f)
  53. : QObject(parent, name)
  54. {
  55. #ifdef DEBUG
  56. qDebug("obj", "QWidget::constructor of \"%s\"", name ? name : "unnamed");
  57. #endif
  58. d = new QWidgetPrivate;
  59. d->flags = f;
  60. d->state = 0;
  61. d->x = d->y = d->width = d->height = 0;
  62. d->caption = "nocaption";
  63. if ((f & WType_Child) == 0) {
  64. if (parent == 0)
  65. d->flags |= WType_TopLevel;
  66. WNDCLASS wc;
  67. memset(&wc, 0, sizeof(WNDCLASS));
  68. wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  69. wc.lpfnWndProc = windowProc;
  70. wc.hInstance = hInstance;
  71. wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
  72. sprintf(d->wndName, "MainWClass%d", ++wndcounter);
  73. wc.lpszClassName = d->wndName;
  74. wc.lpszMenuName = NULL;
  75. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  76. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  77. if (!RegisterClass(&wc))
  78. qFatal("cannot register window");
  79. HWND hwnd = CreateWindowEx(
  80. 0, // no extended styles
  81. d->wndName, // class name
  82. d->caption.c_str(), // window name
  83. WS_OVERLAPPEDWINDOW, // style
  84. CW_USEDEFAULT, // geometry
  85. CW_USEDEFAULT,
  86. CW_USEDEFAULT,
  87. CW_USEDEFAULT,
  88. (HWND)NULL, // no parent or owner window
  89. (HMENU)NULL, // class menu used
  90. hInstance, // instance handle
  91. NULL); // no window creation data
  92. if (hwnd == (HWND)0)
  93. qFatal("cannot create window");
  94. setWinId(hwnd);
  95. registerWinId(hwnd, this);
  96. }
  97. if (parent && parent->isVisible())
  98. show();
  99. }
  100. QWidget::~QWidget()
  101. {
  102. #ifdef DEBUG
  103. qDebug("obj", "QWidget::destructor of \"%s\"", name("unnamed"));
  104. #endif
  105. if (winId() != 0) {
  106. unregisterWinId(winId());
  107. DestroyWindow(winId());
  108. }
  109. delete d;
  110. }
  111. WId QWidget::winId()
  112. {
  113. return d->winid;
  114. }
  115. void QWidget::setWinId(WId id)
  116. {
  117. d->winid = id;
  118. }
  119. QWidget *QWidget::find(WId id)
  120. {
  121. for (int i = 0; i < numregwnd; ++i)
  122. if (regwnd[i] && regwnd[i]->winId() == id)
  123. return regwnd[i];
  124. return 0;
  125. }
  126. QWidget *QWidget::parentWidget() const
  127. {
  128. return (QWidget *)parent();
  129. }
  130. bool QWidget::isTopLevel() const
  131. {
  132. return d->flags & WType_TopLevel;
  133. }
  134. bool QWidget::isWidgetType() const
  135. {
  136. return TRUE;
  137. }
  138. bool QWidget::isVisible() const
  139. {
  140. return d->state & WState_Visible;
  141. }
  142. void QWidget::setCaption(const QString& caption)
  143. {
  144. d->caption = caption;
  145. SetWindowText(d->winid, caption);
  146. }
  147. const QString& QWidget::caption() const
  148. {
  149. return d->caption;
  150. }
  151. void QWidget::show()
  152. {
  153. ShowWindow(d->winid, SW_SHOW);
  154. d->state |= WState_Visible;
  155. QObjectList olist = *children();
  156. for (QObject *o = olist.first(); o != 0; o = olist.next()) {
  157. if (o->isWidgetType())
  158. ((QWidget *)o)->show();
  159. }
  160. }
  161. void QWidget::hide()
  162. {
  163. ShowWindow(d->winid, SW_HIDE);
  164. d->state &= ~WState_Visible;
  165. QObjectList olist = *children();
  166. for (QObject *o = olist.first(); o != 0; o = olist.next()) {
  167. if (o->isWidgetType())
  168. ((QWidget *)o)->hide();
  169. }
  170. }
  171. bool QWidget::close()
  172. {
  173. return FALSE;
  174. }
  175. void QWidget::event(QEvent *e)
  176. {
  177. switch (e->type()) {
  178. case QEvent::None:
  179. delete e;
  180. break;
  181. case QEvent::MouseButtonPress: {
  182. QMouseEvent *me = (QMouseEvent *)e;
  183. mousePressEvent(me);
  184. delete me;
  185. break;
  186. }
  187. case QEvent::MouseButtonRelease: {
  188. QMouseEvent *me = (QMouseEvent *)e;
  189. mouseReleaseEvent(me);
  190. delete me;
  191. break;
  192. }
  193. }
  194. }
  195. void QWidget::mousePressEvent(QMouseEvent * /*e*/)
  196. {
  197. }
  198. void QWidget::mouseReleaseEvent(QMouseEvent * /*e*/)
  199. {
  200. }
  201. void QWidget::movedTo(int x, int y)
  202. {
  203. d->x = x;
  204. d->y = y;
  205. }
  206. void QWidget::resizedTo(int width, int height)
  207. {
  208. d->width = width;
  209. d->height = height;
  210. }
  211. void QWidget::move(int x, int y)
  212. {
  213. d->x = x;
  214. d->y = y;
  215. MoveWindow(d->winid, d->x, d->y, d->width, d->height, TRUE);
  216. }
  217. void QWidget::resize(int width, int height)
  218. {
  219. d->width = width;
  220. d->height = height;
  221. MoveWindow(d->winid, d->x, d->y, d->width, d->height, TRUE);
  222. }
  223. void QWidget::setGeometry(int x, int y, int width, int height)
  224. {
  225. d->x = x;
  226. d->y = y;
  227. d->width = width;
  228. d->height = height;
  229. MoveWindow(d->winid, d->x, d->y, d->width, d->height, TRUE);
  230. }
  231. void QWidget::registerWinId(WId id, QWidget *w)
  232. {
  233. #ifdef DEBUG
  234. qDebug("wid", "registering window ID %p \"%s\"", id, w->name());
  235. #endif
  236. for (int i = 0; i < numregwnd; ++i)
  237. if (!regwnd[i]) {
  238. regwnd[i] = w;
  239. return;
  240. }
  241. if (numregwnd >= MAX_WINDOW_HANDLES)
  242. qFatal("maximum number of window handles exceed");
  243. else
  244. regwnd[numregwnd++] = w;
  245. }
  246. void QWidget::unregisterWinId(WId id)
  247. {
  248. #ifdef DEBUG
  249. qDebug("wid", "unregistering window ID %p", id);
  250. #endif
  251. for (int i = 0; i < numregwnd; ++i)
  252. if (regwnd[i] && regwnd[i]->winId() == id) {
  253. regwnd[i] = 0;
  254. break;
  255. }
  256. }