goat3d

view exporters/maxgoat/src/minwin.cc @ 64:99715321ad6d

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Apr 2014 08:53:42 +0300
parents 0c3576325480
children
line source
1 #include <string.h>
2 #include <windows.h>
3 #include "minwin.h"
4 #include "logger.h"
6 #define GOATSCE_WCLASS "goatsce-window"
8 struct MinWidget {
9 HWND win;
11 MWCallback cbfunc;
12 void *cbcls;
14 MinWidget() { win = 0; cbfunc = 0; cbcls = 0; }
15 };
17 static void init();
18 static MinWidget *createwin(MinWidget *parent, const char *cls, const char *name,
19 unsigned int style, int x, int y, int xsz, int ysz);
20 static LRESULT CALLBACK handle_msg(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam);
22 extern HINSTANCE hinst; // defined in maxgoat.cc
24 void mw_set_callback(MinWidget *w, MWCallback func, void *cls)
25 {
26 w->cbfunc = func;
27 w->cbcls = cls;
28 }
30 MinWidget *mw_create_window(MinWidget *parent, const char *name, int x, int y, int xsz, int ysz)
31 {
32 unsigned int style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
33 if(parent) {
34 style |= WS_CHILD;
35 }
37 maxlog("creating window: %s\n", name);
39 MinWidget *w = createwin(parent, GOATSCE_WCLASS, name, style, x, y, xsz, ysz);
40 return w;
41 }
43 MinWidget *mw_create_button(MinWidget *parent, const char *text, int x, int y, int xsz, int ysz)
44 {
45 unsigned int style = BS_PUSHBUTTON | WS_VISIBLE;
46 if(parent) {
47 style |= WS_CHILD;
48 }
50 maxlog("creating button: %s\n", text);
52 MinWidget *w = createwin(parent, "BUTTON", text, style, x, y, xsz, ysz);
53 return w;
54 }
56 MinWidget *mw_create_checkbox(MinWidget *parent, const char *text, int x, int y, int xsz, int ysz, bool checked)
57 {
58 unsigned int style = BS_CHECKBOX | WS_VISIBLE;
59 if(parent) {
60 style |= WS_CHILD;
61 }
63 maxlog("creating checkbox: %s\n", text);
65 MinWidget *w = createwin(parent, "CHECKBOX", text, style, x, y, xsz, ysz);
66 return w;
67 }
69 static DWORD WINAPI gui_thread_func(void *cls);
71 void mw_test()
72 {
73 init();
75 HANDLE thread = CreateThread(0, 0, gui_thread_func, 0, 0, 0);
76 //WaitForSingleObject(thread, 5000);
77 }
79 static DWORD WINAPI gui_thread_func(void *cls)
80 {
81 MinWidget *win = mw_create_window(0, "test window!", -1, -1, 400, 400);
82 MinWidget *bn_ok = mw_create_button(win, "Ok", 50, 100, 150, 40);
83 MinWidget *bn_cancel = mw_create_button(win, "Cancel", 250, 100, 150, 40);
84 MinWidget *ck_lights = mw_create_checkbox(win, "Export lights", 20, 20, 250, 40, true);
85 MinWidget *ck_cameras = mw_create_checkbox(win, "Export cameras", 20, 60, 250, 40, true);
87 MSG msg;
88 while(GetMessage(&msg, win->win, 0, 0)) {
89 TranslateMessage(&msg);
90 DispatchMessage(&msg);
91 }
93 DestroyWindow(win->win);
94 delete bn_ok;
95 delete bn_cancel;
96 delete ck_lights;
97 delete ck_cameras;
98 delete win;
100 return 0;
101 }
103 static void init()
104 {
105 static bool done_init;
106 if(done_init) {
107 return;
108 }
109 done_init = true;
111 size_t sz = mbstowcs(0, GOATSCE_WCLASS, 0);
112 wchar_t *cname = new wchar_t[sz + 1];
113 mbstowcs(cname, GOATSCE_WCLASS, sz + 1);
115 WNDCLASS wc;
116 memset(&wc, 0, sizeof wc);
117 wc.lpszClassName = cname;
118 wc.hInstance = hinst;
119 wc.lpfnWndProc = handle_msg;
120 wc.style = CS_HREDRAW | CS_VREDRAW;
121 wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
122 wc.hCursor = LoadCursor(0, IDC_ARROW);
124 RegisterClass(&wc);
125 }
127 static MinWidget *createwin(MinWidget *parent, const char *cls, const char *name,
128 unsigned int style, int x, int y, int xsz, int ysz)
129 {
130 init();
132 MinWidget *w = new MinWidget;
134 size_t sz = mbstowcs(0, cls, 0);
135 wchar_t *wcls = new wchar_t[sz + 1];
136 mbstowcs(wcls, cls, sz + 1);
138 sz = mbstowcs(0, name, 0);
139 wchar_t *wname = new wchar_t[sz + 1];
140 mbstowcs(wname, name, sz + 1);
142 if(x <= 0) x = CW_USEDEFAULT;
143 if(y <= 0) y = CW_USEDEFAULT;
145 w->win = CreateWindow(wcls, wname, style, x, y, xsz, ysz, parent ? parent->win : 0, 0, hinst, 0);
147 delete [] wcls;
148 delete [] wname;
150 if(!w->win) {
151 delete w;
152 return 0;
153 }
154 return w;
155 }
158 static LRESULT CALLBACK handle_msg(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam)
159 {
160 switch(msg) {
161 case WM_CLOSE:
162 DestroyWindow(win);
163 break;
165 default:
166 return DefWindowProc(win, msg, wparam, lparam);
167 }
169 return 0;