source: mainline/uspace/lib/ui/src/wdecor.c@ 1769693

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1769693 was 1769693, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Window decoration

  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 * Copyright (c) 2020 Jiri Svoboda
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 Window decoration
34 */
35
36#include <errno.h>
37#include <gfx/color.h>
38#include <gfx/context.h>
39#include <gfx/render.h>
40#include <gfx/text.h>
41#include <io/pos_event.h>
42#include <stdlib.h>
43#include <str.h>
44#include <ui/paint.h>
45#include <ui/wdecor.h>
46#include "../private/resource.h"
47#include "../private/wdecor.h"
48
49/** Create new window decoration.
50 *
51 * @param resource UI resource
52 * @param caption Window caption
53 * @param rwdecor Place to store pointer to new window decoration
54 * @return EOK on success, ENOMEM if out of memory
55 */
56errno_t ui_wdecor_create(ui_resource_t *resource, const char *caption,
57 ui_wdecor_t **rwdecor)
58{
59 ui_wdecor_t *wdecor;
60
61 wdecor = calloc(1, sizeof(ui_wdecor_t));
62 if (wdecor == NULL)
63 return ENOMEM;
64
65 wdecor->caption = str_dup(caption);
66 if (wdecor->caption == NULL) {
67 free(wdecor);
68 return ENOMEM;
69 }
70
71 wdecor->res = resource;
72 wdecor->active = true;
73 *rwdecor = wdecor;
74 return EOK;
75}
76
77/** Destroy window decoration.
78 *
79 * @param wdecor Window decoration or @c NULL
80 */
81void ui_wdecor_destroy(ui_wdecor_t *wdecor)
82{
83 if (wdecor == NULL)
84 return;
85
86 free(wdecor);
87}
88
89/** Set window decoration callbacks.
90 *
91 * @param wdecor Window decoration
92 * @param cb Window decoration callbacks
93 * @param arg Callback argument
94 */
95void ui_wdecor_set_cb(ui_wdecor_t *wdecor, ui_wdecor_cb_t *cb, void *arg)
96{
97 wdecor->cb = cb;
98 wdecor->arg = arg;
99}
100
101/** Set window decoration rectangle.
102 *
103 * @param wdecor Window decoration
104 * @param rect New button rectanle
105 */
106void ui_wdecor_set_rect(ui_wdecor_t *wdecor, gfx_rect_t *rect)
107{
108 wdecor->rect = *rect;
109}
110
111/** Set active flag.
112 *
113 * Active window is the one receiving keyboard events.
114 *
115 * @param wdecor Window decoration
116 * @param active @c true iff window is active
117 */
118void ui_wdecor_set_active(ui_wdecor_t *wdecor, bool active)
119{
120 wdecor->active = active;
121}
122
123/** Paint window decoration.
124 *
125 * @param wdecor Window decoration
126 * @return EOK on success or an error code
127 */
128errno_t ui_wdecor_paint(ui_wdecor_t *wdecor)
129{
130 errno_t rc;
131 gfx_rect_t rect;
132 gfx_rect_t trect;
133 gfx_text_fmt_t fmt;
134 gfx_coord2_t pos;
135
136 rect = wdecor->rect;
137
138 rc = ui_paint_bevel(wdecor->res->gc, &rect,
139 wdecor->res->wnd_frame_hi_color,
140 wdecor->res->wnd_frame_sh_color, 1, &rect);
141 if (rc != EOK)
142 return rc;
143
144 rc = ui_paint_bevel(wdecor->res->gc, &rect,
145 wdecor->res->wnd_highlight_color,
146 wdecor->res->wnd_shadow_color, 1, &rect);
147 if (rc != EOK)
148 return rc;
149
150 rc = ui_paint_bevel(wdecor->res->gc, &rect,
151 wdecor->res->wnd_face_color,
152 wdecor->res->wnd_face_color, 2, &rect);
153 if (rc != EOK)
154 return rc;
155
156 trect.p0 = rect.p0;
157 trect.p1.x = rect.p1.x;
158 trect.p1.y = rect.p0.y + 22;
159
160 rc = ui_paint_bevel(wdecor->res->gc, &trect,
161 wdecor->res->wnd_shadow_color,
162 wdecor->res->wnd_highlight_color, 1, &trect);
163 if (rc != EOK)
164 return rc;
165
166 rc = gfx_set_color(wdecor->res->gc, wdecor->active ?
167 wdecor->res->tbar_act_bg_color :
168 wdecor->res->tbar_inact_bg_color);
169 if (rc != EOK)
170 return rc;
171
172 rc = gfx_fill_rect(wdecor->res->gc, &trect);
173 if (rc != EOK)
174 return rc;
175
176 gfx_text_fmt_init(&fmt);
177 fmt.halign = gfx_halign_center;
178 fmt.valign = gfx_valign_center;
179
180 pos.x = (trect.p0.x + trect.p1.x) / 2;
181 pos.y = (trect.p0.y + trect.p1.y) / 2;
182
183 rc = gfx_set_color(wdecor->res->gc, wdecor->active ?
184 wdecor->res->tbar_act_text_color :
185 wdecor->res->tbar_inact_text_color);
186 if (rc != EOK)
187 return rc;
188
189 rc = gfx_puttext(wdecor->res->font, &pos, &fmt, wdecor->caption);
190 if (rc != EOK)
191 return rc;
192
193 return EOK;
194}
195
196/** Send decoration move event.
197 *
198 * @param wdecor Window decoration
199 * @param pos Position where the title bar was pressed
200 */
201void ui_wdecor_move(ui_wdecor_t *wdecor, gfx_coord2_t *pos)
202{
203 if (wdecor->cb != NULL && wdecor->cb->move != NULL)
204 wdecor->cb->move(wdecor, wdecor->arg, pos);
205}
206
207/** Handle window decoration position event.
208 *
209 * @param wdecor Window decoration
210 * @param pos_event Position event
211 */
212void ui_wdecor_pos_event(ui_wdecor_t *wdecor, pos_event_t *event)
213{
214 gfx_rect_t trect;
215 gfx_coord2_t pos;
216
217 trect.p0.x = wdecor->rect.p0.x + 3;
218 trect.p0.y = wdecor->rect.p0.y + 3;
219 trect.p1.x = wdecor->rect.p1.x - 3;
220 trect.p1.y = trect.p0.y + 22;
221
222 pos.x = event->hpos;
223 pos.y = event->vpos;
224
225 if (event->type == POS_PRESS && gfx_pix_inside_rect(&pos, &trect))
226 ui_wdecor_move(wdecor, &pos);
227}
228
229/** @}
230 */
Note: See TracBrowser for help on using the repository browser.