[8009dc27] | 1 | /*
|
---|
[7481ee19] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[8009dc27] | 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 | /** @addtogroup libui
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file UI control
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <errno.h>
|
---|
[7481ee19] | 37 | #include <io/kbd_event.h>
|
---|
[8009dc27] | 38 | #include <io/pos_event.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
| 40 | #include <ui/control.h>
|
---|
| 41 | #include "../private/control.h"
|
---|
| 42 |
|
---|
| 43 | /** Allocate new UI control.
|
---|
| 44 | *
|
---|
| 45 | * @param ops Control ops
|
---|
| 46 | * @param ext Control extended data
|
---|
| 47 | * @param rcontrol Place to store pointer to new control
|
---|
| 48 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 49 | */
|
---|
| 50 | errno_t ui_control_new(ui_control_ops_t *ops, void *ext,
|
---|
| 51 | ui_control_t **rcontrol)
|
---|
| 52 | {
|
---|
| 53 | ui_control_t *control;
|
---|
| 54 |
|
---|
| 55 | control = calloc(1, sizeof(ui_control_t));
|
---|
| 56 | if (control == NULL)
|
---|
| 57 | return ENOMEM;
|
---|
| 58 |
|
---|
| 59 | control->ops = ops;
|
---|
| 60 | control->ext = ext;
|
---|
| 61 | *rcontrol = control;
|
---|
| 62 | return EOK;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /** Delete UI control.
|
---|
[c6f00b40] | 66 | *
|
---|
| 67 | * Deletes the base control (not the extended data).
|
---|
[8009dc27] | 68 | *
|
---|
| 69 | * @param control UI control or @c NULL
|
---|
| 70 | */
|
---|
| 71 | void ui_control_delete(ui_control_t *control)
|
---|
| 72 | {
|
---|
| 73 | if (control == NULL)
|
---|
| 74 | return;
|
---|
| 75 |
|
---|
| 76 | free(control);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[c6f00b40] | 79 | /** Destroy UI control.
|
---|
| 80 | *
|
---|
| 81 | * Run the virtual control destructor (destroy complete control including
|
---|
| 82 | * extended data).
|
---|
| 83 | *
|
---|
[b71c0fc] | 84 | * @param control Control or @c NULL
|
---|
[c6f00b40] | 85 | */
|
---|
| 86 | void ui_control_destroy(ui_control_t *control)
|
---|
| 87 | {
|
---|
[b71c0fc] | 88 | if (control == NULL)
|
---|
| 89 | return;
|
---|
| 90 |
|
---|
[c6f00b40] | 91 | return control->ops->destroy(control->ext);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[7481ee19] | 94 | /** Deliver keyboard event to UI control.
|
---|
| 95 | *
|
---|
| 96 | * @param control Control
|
---|
| 97 | * @param kbd_event Keyboard event
|
---|
| 98 | * @return @c ui_claimed iff the event is claimed
|
---|
| 99 | */
|
---|
| 100 | ui_evclaim_t ui_control_kbd_event(ui_control_t *control, kbd_event_t *event)
|
---|
| 101 | {
|
---|
| 102 | if (control->ops->kbd_event != NULL)
|
---|
| 103 | return control->ops->kbd_event(control->ext, event);
|
---|
| 104 | else
|
---|
| 105 | return ui_unclaimed;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[4df6607] | 108 | /** Paint UI control.
|
---|
| 109 | *
|
---|
[c6f00b40] | 110 | * @param control Control
|
---|
[4df6607] | 111 | * @return EOK on success or an error code
|
---|
| 112 | */
|
---|
| 113 | errno_t ui_control_paint(ui_control_t *control)
|
---|
| 114 | {
|
---|
| 115 | return control->ops->paint(control->ext);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[8009dc27] | 118 | /** Deliver position event to UI control.
|
---|
| 119 | *
|
---|
[c6f00b40] | 120 | * @param control Control
|
---|
[8009dc27] | 121 | * @param pos_event Position event
|
---|
| 122 | * @return @c ui_claimed iff the event is claimed
|
---|
| 123 | */
|
---|
| 124 | ui_evclaim_t ui_control_pos_event(ui_control_t *control, pos_event_t *event)
|
---|
| 125 | {
|
---|
| 126 | return control->ops->pos_event(control->ext, event);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[62223ec] | 129 | /** Inform UI control that window has been unfocused.
|
---|
| 130 | *
|
---|
| 131 | * @param control Control
|
---|
| 132 | */
|
---|
| 133 | void ui_control_unfocus(ui_control_t *control)
|
---|
| 134 | {
|
---|
| 135 | if (control->ops->unfocus != NULL)
|
---|
| 136 | control->ops->unfocus(control->ext);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[8009dc27] | 139 | /** @}
|
---|
| 140 | */
|
---|