x3win32.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "x3.h"
  2. #include "x3common.h"
  3. #include <stdio.h> /* for printf only, probably remove in production */
  4. HINSTANCE theInstance = NULL;
  5. void x3init_win32(HINSTANCE hInstance)
  6. {
  7. theInstance = hInstance;
  8. }
  9. void x3widget_init(x3widget *w, const x3type *type)
  10. {
  11. w->type = type;
  12. w->name = NULL;
  13. w->parent = NULL;
  14. w->var = x3winnone;
  15. w->u.hwnd = NULL;
  16. w->n_children = 0;
  17. w->children = NULL;
  18. }
  19. LRESULT CALLBACK x3WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  20. {
  21. switch (iMsg) {
  22. case WM_DESTROY:
  23. if (--x3n_winopen <= 0) {
  24. PostQuitMessage(0);
  25. return 0;
  26. }
  27. break;
  28. }
  29. return DefWindowProc(hwnd, iMsg, wParam, lParam);
  30. }
  31. void x3window_sizereq(x3widget *w)
  32. {
  33. }
  34. void x3window_sizealloc(x3widget *w, x3rect *r)
  35. {
  36. int i;
  37. RECT rect;
  38. x3rect child_r;
  39. GetClientRect(w->u.hwnd, &rect);
  40. child_r.x0 = 0;
  41. child_r.x1 = rect.right - rect.left;
  42. child_r.y0 = 0;
  43. child_r.y1 = rect.bottom - rect.top;
  44. printf("x3window_sizealloc (%ld, %ld) - (%ld, %ld)\n",
  45. rect.left, rect.top, rect.right, rect.bottom);
  46. for (i = 0; i < w->n_children; i++) {
  47. x3widget *child = w->children[i];
  48. if (child->type->sizealloc)
  49. child->type->sizealloc(child, &child_r);
  50. child->flags &= ~x3flag_needsizealloc;
  51. }
  52. }
  53. x3type x3windowtype = { x3window_sizereq,
  54. x3window_sizealloc,
  55. x3add_default };
  56. x3widget *x3window(x3windowflags flags, char *label,
  57. x3window_callback callback, void *data)
  58. {
  59. HWND hwnd;
  60. DWORD style = WS_OVERLAPPEDWINDOW;
  61. x3widget *result = (x3widget *)malloc(sizeof(x3widget));
  62. WNDCLASSEX wndclass;
  63. wndclass.cbSize = sizeof(wndclass);
  64. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  65. wndclass.lpfnWndProc = x3WndProc;
  66. wndclass.cbClsExtra = 0;
  67. wndclass.cbWndExtra = 0;
  68. wndclass.hInstance = theInstance;
  69. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  70. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  71. wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  72. wndclass.lpszMenuName = NULL;
  73. wndclass.lpszClassName = "x3win";
  74. wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  75. RegisterClassEx(&wndclass);
  76. hwnd = CreateWindowEx(0, "x3win", "My window",
  77. style, 100, 100, 300, 300,
  78. NULL, NULL,
  79. theInstance, NULL);
  80. x3widget_init(result, &x3windowtype);
  81. result->var = x3winhwnd;
  82. result->u.hwnd = hwnd;
  83. x3_nwinopen++;
  84. //ShowWindow(hwnd, SW_SHOWNORMAL);
  85. return result;
  86. }
  87. void x3_window_show(x3widget *w)
  88. {
  89. ShowWindow(w->u.hwnd, SW_SHOW);
  90. }
  91. static HWND x3hwnd_of(x3widget *w)
  92. {
  93. while (w->parent) w = w->parent;
  94. return w->var == x3winhwnd ? w->u.hwnd : NULL;
  95. }
  96. static x3widget *x3widget_new_hwnd(x3widget *parent, char *name,
  97. const x3type *type,
  98. HWND hwnd)
  99. {
  100. x3widget *result = (x3widget *)malloc(sizeof(x3widget));
  101. x3widget_init(result, type);
  102. result->name = name ? strdup(name) : NULL;
  103. result->var = x3winhwnd;
  104. result->u.hwnd = hwnd;
  105. x3add(parent, result);
  106. x3qsizereq(result);
  107. return result;
  108. }
  109. void x3button_sizereq(x3widget *w)
  110. {
  111. w->sizerequest.x0 = 0;
  112. w->sizerequest.y0 = 0;
  113. w->sizerequest.x1 = 100;
  114. w->sizerequest.y1 = 20;
  115. #ifdef VERBOSE
  116. printf("button sizereq = (%d, %d) - (%d, %d)\n",
  117. w->sizerequest.x0, w->sizerequest.y0,
  118. w->sizerequest.x1, w->sizerequest.y1);
  119. #endif
  120. }
  121. void x3button_sizealloc(x3widget *w, x3rect *r)
  122. {
  123. printf("button sizealloc = (%g, %g) - (%g, %g)\n",
  124. r->x0, r->y0, r->x1, r->y1);
  125. if (w->var == x3winhwnd) {
  126. SetWindowPos(w->u.hwnd, HWND_TOP,
  127. r->x0, r->y0, r->x1 - r->x0, r->y1 - r->y0,
  128. SWP_NOZORDER);
  129. }
  130. }
  131. x3type x3buttontype = { x3button_sizereq,
  132. x3button_sizealloc,
  133. x3add_default };
  134. x3widget *x3button(x3widget *parent, char *cmd, char *label)
  135. {
  136. HWND hwnd;
  137. hwnd = CreateWindow("button", label,
  138. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  139. 10, 10, 100, 20, x3hwnd_of(parent), NULL,
  140. theInstance, NULL);
  141. return x3widget_new_hwnd(parent, cmd, &x3buttontype, hwnd);
  142. }
  143. void x3main(void)
  144. {
  145. MSG msg;
  146. x3sync();
  147. while (GetMessage(&msg, NULL, 0, 0)) {
  148. TranslateMessage(&msg);
  149. DispatchMessage(&msg);
  150. }
  151. }