goat3d

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