| 1 | /*
|
|---|
| 2 | * Copyright (c) 2023 SimonJRiddix
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #ifndef UI_WINDOW_H
|
|---|
| 30 | #define UI_WINDOW_H
|
|---|
| 31 |
|
|---|
| 32 | #include "vector.h"
|
|---|
| 33 |
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <gfx/coord.h>
|
|---|
| 36 | #include <stdbool.h>
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <str.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <task.h>
|
|---|
| 42 | #include <ui/fixed.h>
|
|---|
| 43 | #include <ui/resource.h>
|
|---|
| 44 | #include <ui/ui.h>
|
|---|
| 45 | #include <ui/window.h>
|
|---|
| 46 |
|
|---|
| 47 | static const char *display_spec = UI_DISPLAY_DEFAULT;
|
|---|
| 48 |
|
|---|
| 49 | typedef struct sWindow Window;
|
|---|
| 50 | struct sWindow
|
|---|
| 51 | {
|
|---|
| 52 | vector controls;
|
|---|
| 53 | char * name;
|
|---|
| 54 |
|
|---|
| 55 | ui_t *ui;
|
|---|
| 56 | ui_resource_t *ui_res;
|
|---|
| 57 | ui_wnd_params_t params;
|
|---|
| 58 | ui_window_t *window;
|
|---|
| 59 | ui_fixed_t *fixed;
|
|---|
| 60 |
|
|---|
| 61 | int (*draw)(Window* w);
|
|---|
| 62 | void (*destroy)(Window* w);
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | int draw_window(Window* w);
|
|---|
| 66 | void destroy_window(Window* w);
|
|---|
| 67 | int init_window(Window* w, const char * name, const char * caption);
|
|---|
| 68 |
|
|---|
| 69 | int draw_window(Window* w)
|
|---|
| 70 | {
|
|---|
| 71 | ui_window_add(w->window, ui_fixed_ctl(w->fixed));
|
|---|
| 72 |
|
|---|
| 73 | errno_t rc = ui_window_paint(w->window);
|
|---|
| 74 | if (rc != EOK) {
|
|---|
| 75 | printf("Error painting window.\n");
|
|---|
| 76 | return rc;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | ui_run(w->ui);
|
|---|
| 80 |
|
|---|
| 81 | return EOK;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | void destroy_window(Window* w)
|
|---|
| 85 | {
|
|---|
| 86 | ui_window_destroy(w->window);
|
|---|
| 87 | ui_destroy(w->ui);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | int init_window(Window* w, const char * name, const char * caption)
|
|---|
| 91 | {
|
|---|
| 92 | //vector_init(&w->controls);
|
|---|
| 93 | //w->name = name;
|
|---|
| 94 |
|
|---|
| 95 | errno_t rc = ui_create(display_spec, &w->ui);
|
|---|
| 96 | if (rc != EOK) {
|
|---|
| 97 | printf("Error creating UI on display %s.\n", display_spec);
|
|---|
| 98 | return rc;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | ui_wnd_params_init(&w->params);
|
|---|
| 102 | w->params.caption = caption;
|
|---|
| 103 | w->params.placement = ui_wnd_place_bottom_left;
|
|---|
| 104 | w->params.rect.p0.x = 0;
|
|---|
| 105 | w->params.rect.p0.y = 0;
|
|---|
| 106 | w->params.rect.p1.x = 380;
|
|---|
| 107 | w->params.rect.p1.y = 450;
|
|---|
| 108 | w->params.style = ui_wds_frame;
|
|---|
| 109 |
|
|---|
| 110 | rc = ui_window_create(w->ui, &w->params, &w->window);
|
|---|
| 111 | if (rc != EOK) {
|
|---|
| 112 | printf("Error creating window.\n");
|
|---|
| 113 | return rc;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | w->ui_res = ui_window_get_res(w->window);
|
|---|
| 117 |
|
|---|
| 118 | rc = ui_fixed_create(&w->fixed);
|
|---|
| 119 | if (rc != EOK) {
|
|---|
| 120 | printf("Error creating fixed layout.\n");
|
|---|
| 121 | return rc;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | w->draw = draw_window;
|
|---|
| 125 |
|
|---|
| 126 | w->destroy = destroy_window;
|
|---|
| 127 |
|
|---|
| 128 | return EOK;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | #endif
|
|---|