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 "toolkit.h" 30 #include "Windows.h" 31 32 #include "window.h" 33 34 #include "../common/menu.h" 35 #include "../common/toolbar.h" 36 #include "../common/document.h" 37 #include "../common/properties.h" 38 #include "../common/app.h" 39 40 #include "../ui/widget.h" 41 42 #include <stdio.h> 43 #include <stdlib.h> 44 45 #include <commctrl.h> 46 47 static const char *application_name; 48 49 static HFONT ui_font = NULL; 50 51 void ui_init(const char *appname, int argc, char **argv) { 52 application_name = appname; 53 54 uic_init_global_context(); 55 uic_menu_init(); 56 uic_toolbar_init(); 57 uic_load_app_properties(); 58 59 INITCOMMONCONTROLSEX icex = { 60 sizeof(icex), 61 ICC_WIN95_CLASSES | ICC_LISTVIEW_CLASSES | ICC_TREEVIEW_CLASSES | ICC_BAR_CLASSES | ICC_TAB_CLASSES | ICC_STANDARD_CLASSES | ICC_USEREX_CLASSES 62 }; 63 InitCommonControlsEx(&icex); 64 65 SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); 66 67 NONCLIENTMETRICS ncm = { sizeof(ncm) }; 68 SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, FALSE); 69 ui_font = CreateFontIndirect(&ncm.lfMessageFont); 70 71 ui_window_init(); 72 } 73 74 HFONT ui_win32_get_font(void) { 75 return ui_font; 76 } 77 78 void ui_win32_set_ui_font(HWND control) { 79 if (ui_font) { 80 SendMessage(control, WM_SETFONT, (WPARAM)ui_font, TRUE); 81 } 82 } 83 84 const char* ui_appname() { 85 return application_name; 86 } 87 88 void ui_main() { 89 uic_application_startup(NULL); 90 91 // event loop 92 MSG msg; 93 while (GetMessage(&msg, NULL, 0, 0)) { 94 TranslateMessage(&msg); 95 DispatchMessage(&msg); 96 } 97 98 uic_application_exit(NULL); 99 uic_store_app_properties(); 100 } 101 102 void ui_show(UiObject *obj) { 103 ui_set_visible(obj->widget, TRUE); 104 } 105 106 LRESULT CALLBACK ui_default_eventproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { 107 W32Widget *widget = (W32Widget*)GetWindowLongPtr(hwnd, GWLP_USERDATA); 108 if (widget && widget->wclass->eventproc) { 109 if (widget->wclass->eventproc(widget, hwnd, uMsg, wParam, lParam)) { 110 return 1; 111 } 112 } 113 114 switch(uMsg) { 115 case WM_DESTROY: { 116 PostQuitMessage(0); 117 break; 118 } 119 case WM_COMMAND: { 120 HWND hwndCtrl = (HWND)lParam; 121 W32Widget *cmdWidget = (W32Widget*)GetWindowLongPtr(hwndCtrl, GWLP_USERDATA); 122 if (cmdWidget && cmdWidget->wclass->eventproc) { 123 cmdWidget->wclass->eventproc(cmdWidget, hwnd, uMsg, wParam, lParam); 124 } 125 break; 126 } 127 case WM_NOTIFY: { 128 LPNMHDR hdr = (LPNMHDR)lParam; 129 HWND hwndCtrl = hdr->hwndFrom; 130 W32Widget *cmdWidget = (W32Widget*)GetWindowLongPtr(hwndCtrl, GWLP_USERDATA); 131 if (cmdWidget && cmdWidget->wclass->eventproc) { 132 cmdWidget->wclass->eventproc(cmdWidget, hwnd, uMsg, wParam, lParam); 133 } 134 break; 135 } 136 case WM_SIZE: { 137 int width = LOWORD(lParam); 138 int height = HIWORD(lParam); 139 if (widget && widget->layout) { 140 widget->layout(widget->layoutmanager, width, height); 141 } 142 break; 143 } 144 default: break;//return DefWindowProc(hwnd, uMsg, wParam, lParam); 145 } 146 return DefWindowProc(hwnd, uMsg, wParam, lParam);; 147 } 148 149 void ui_call_mainthread(ui_threadfunc tf, void* td) { 150 // TODO 151 } 152