UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2024 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "window.h" 30 #include <Windows.h> 31 32 #include "container.h" 33 34 #include "../common/object.h" 35 36 37 #include <stdbool.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 41 #include "win32.h" 42 #include "menu.h" 43 44 static W32WidgetClass w32_toplevel_widget_class = { 45 .eventproc = ui_window_widget_event, 46 .show = ui_window_widget_show, 47 .enable = NULL, 48 .get_preferred_size = NULL, 49 .destroy = w32_widget_default_destroy 50 }; 51 52 static HINSTANCE hInstance; 53 54 static const char *mainWindowClass = "UiMainWindow"; 55 56 57 void ui_window_init(void) { 58 hInstance = GetModuleHandle(NULL); 59 60 WNDCLASSEX wc = { sizeof(WNDCLASSEX) }; 61 wc.lpfnWndProc = ui_default_eventproc; 62 wc.hInstance = hInstance; 63 wc.lpszClassName = mainWindowClass; 64 wc.hCursor = LoadCursor(NULL, IDC_ARROW); 65 wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); 66 67 if(!RegisterClassExA(&wc)) { 68 MessageBox(NULL, "RegisterClassEx failed", "Error", MB_ICONERROR); 69 exit(-1); 70 } 71 } 72 73 static UiObject* create_window(const char *title, bool simple) { 74 UiObject *obj = uic_object_new_toplevel(); 75 76 HWND hwnd = CreateWindowExA( 77 0, 78 "UiMainWindow", 79 title, 80 WS_OVERLAPPEDWINDOW, 81 CW_USEDEFAULT, 82 CW_USEDEFAULT, 83 800, 84 600, 85 NULL, 86 NULL, 87 hInstance, 88 NULL); 89 90 if (!simple) { 91 HMENU menubar = ui_create_main_menu(obj); 92 if (menubar) { 93 SetMenu(hwnd, menubar); 94 } 95 } 96 97 UpdateWindow(hwnd); 98 99 UiContainerX *container = ui_box_container_create(obj, hwnd, UI_BOX_VERTICAL, 0, INSETS_ZERO); 100 uic_object_push_container(obj, container); 101 UiBoxContainer *box = (UiBoxContainer*)container; 102 103 UiWindow *widget = w32_widget_create(&w32_toplevel_widget_class, hwnd, sizeof(UiWindow)); 104 widget->obj = obj; 105 widget->widget.layout = (W32LayoutFunc)ui_grid_layout; 106 widget->widget.layoutmanager = box->layout; 107 obj->widget = (W32Widget*)widget; 108 obj->ref = 1; 109 110 return obj; 111 } 112 113 UiObject *ui_window(const char *title) { 114 return create_window(title, window_data, FALSE); 115 } 116 117 UiObject *ui_simple_window(const char *title) { 118 return create_window(title, window_data, TRUE); 119 } 120 121 int ui_window_widget_event(W32Widget *widget, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { 122 //UiWindow *window = (UiWindow*)widget; 123 return 0; 124 } 125 126 void ui_window_widget_show(W32Widget *w, BOOLEAN show) { 127 ShowWindow(w->hwnd, show ? SW_SHOWNORMAL : SW_HIDE); 128 } 129