AmendHub

Download

jcs

/

subtext

/

focusable.c

 

(View History)

jcs   focusable: Check xreallocarray result Latest amendment: 611 on 2025-12-06

1 /*
2 * Copyright (c) 2021-2022 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <string.h>
18
19 #include "focusable.h"
20 #include "util.h"
21
22 struct focusable **focusables = NULL;
23 short nfocusables = 0;
24
25 struct focusable *
26 find_focusable(GrafPtr win)
27 {
28 short n;
29
30 for (n = 0; n < nfocusables; n++) {
31 if (focusables[n]->win == win)
32 return focusables[n];
33 }
34
35 return NULL;
36 }
37
38 bool
39 add_focusable(struct focusable *focusable)
40 {
41 short n;
42 struct focusable **new_focusables;
43
44 new_focusables = xreallocarray(focusables, sizeof(struct focusable *),
45 nfocusables + 1);
46 if (new_focusables == NULL)
47 return false;
48
49 focusables = new_focusables;
50 nfocusables++;
51
52 if (nfocusables > 1)
53 for (n = nfocusables - 1; n > 0; n--)
54 focusables[n] = focusables[n - 1];
55
56 focusables[0] = focusable;
57
58 show_focusable(focusable);
59 return true;
60 }
61
62 void
63 show_focusable(struct focusable *focusable)
64 {
65 struct focusable *last, *tmp;
66 short n;
67
68 if (nfocusables > 1 && focusables[0] != focusable) {
69 last = focusables[0];
70 focusables[0] = focusable;
71 for (n = 1; n < nfocusables; n++) {
72 tmp = focusables[n];
73 focusables[n] = last;
74 last = tmp;
75 if (last == focusable)
76 break;
77 }
78 }
79
80 focusable->visible = true;
81 ShowWindow(focusable->win);
82 SelectWindow(focusable->win);
83 }
84
85 void
86 destroy_focusable(struct focusable **focusablep)
87 {
88 struct focusable *focusable = *focusablep;
89 short n;
90
91 if (focusable->visible)
92 CloseWindow(focusable->win);
93
94 for (n = 0; n < nfocusables; n++) {
95 if (focusables[n] == focusable) {
96 for (; n < nfocusables - 1; n++)
97 focusables[n] = focusables[n + 1];
98 break;
99 }
100 }
101
102 nfocusables--;
103 if (nfocusables) {
104 focusables = xreallocarray(focusables, sizeof(Ptr), nfocusables);
105 if (focusables == NULL)
106 panic("Out of memory");
107 } else {
108 xfree(&focusables);
109 focusables = NULL;
110 }
111
112 if (nfocusables && focusables[0]->visible)
113 SelectWindow(focusables[0]->win);
114
115 *focusablep = NULL;
116 }
117
118 void
119 hide_focusable(struct focusable *focusable)
120 {
121 short n;
122
123 HideWindow(focusable->win);
124 focusable->visible = false;
125
126 for (n = 0; n < nfocusables; n++) {
127 if (focusables[n] == focusable) {
128 for (; n < nfocusables - 1; n++)
129 focusables[n] = focusables[n + 1];
130 focusables[nfocusables - 1] = focusable;
131 break;
132 }
133 }
134 }