1 /*
2 
3 Copyright (C) 2015-2018 Night Dive Studios, LLC.
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 #include "tngtextg.h"
20 //#include <_ui.h>
21 #include "tngslidr.h"
22 #include "fakefont.h"
23 #include "tngfont.h"
24 
25 errtype tng_textgadget_move(TNG *ptng, short code);
26 
27 // Callbacks....
28 
29 #pragma require_prototypes off
30 
31 // For the text tool to communicate with the rest of the universe....
tng_textgadget_ui_display(void * ptng,LGRect * rect)32 void tng_textgadget_ui_display(void *ptng, LGRect * rect)
33 {
34    //Spew(DSRC_UI_Textgadget, ("About to trigger display from texttool call!\n"));
35    //Spew(DSRC_UI_Textgadget, ("r = (%d,%d)(%d, %d)\n",RECT_EXPAND_ARGS(r)));
36    if (ptng != NULL)
37       TNG_DRAWRECT((TNG *)ptng, NULL);
38    //else
39       //Spew(DSRC_UI_Textgadget, ("Problem was a null ptng in ui_display...\n"));
40 }
41 
tng_textgadget_hscroll_changed(void * ui_data,intptr_t user_data)42 uchar tng_textgadget_hscroll_changed(void *ui_data, intptr_t user_data)
43 {
44    TNG *ptng;
45    void *dummy;
46    dummy = ui_data;
47 
48    ptng = (TNG *)user_data;
49    ptng->signal(ptng, TNG_SIGNAL_CHANGED);
50    return(FALSE);
51 }
52 
tng_textgadget_vscroll_changed(void * ui_data,intptr_t user_data)53 uchar tng_textgadget_vscroll_changed(void *ui_data, intptr_t user_data)
54 {
55    TNG *ptng;
56    void *dummy;
57    dummy = ui_data;
58 
59    ptng = (TNG *)user_data;
60    ptng->signal(ptng, TNG_SIGNAL_CHANGED);
61    return(FALSE);
62 }
63 #pragma require_prototypes on
64 
tng_textgadget_destroy(TNG * ptng)65 errtype tng_textgadget_destroy(TNG *ptng)
66 {
67    GUI_DEALLOC(ptng->ui_data, ptng->type_data);
68    return(OK);
69 }
70 
71 // Initializes the TNG
tng_textgadget_init(void * ui_data,TNG * ptng,TNGStyle * sty,ulong options,LGPoint size,LGPoint abs_loc)72 errtype tng_textgadget_init(void *ui_data, TNG *ptng, TNGStyle *sty, ulong options, LGPoint size, LGPoint abs_loc)
73 {
74    TNG_textgadget *ptxtng;
75    extern TTFontInfo TTTNGFontInfo;
76    TTState TTs;
77    TTRect TTr;
78    static uchar inited=FALSE;
79 
80    ptxtng = (TNG_textgadget *)GUI_MALLOC(ptng->ui_data, sizeof(TNG_textgadget));
81 
82    TNGInit(ptng,sty,ui_data);
83    ptng->flags = TNG_BEVEL;
84    ptng->type_data = ptxtng;
85    ptng->draw_func = &tng_textgadget_2d_draw;
86 //   ptng->mousebutt = &tng_textgadget_mousebutt;
87    ptng->keycooked = &tng_textgadget_keycooked;
88    ptng->signal = &tng_textgadget_signal;
89 
90    ptxtng->tng_data = ptng;
91    ptxtng->size = size;
92    ptxtng->last_key = 0;
93    ptxtng->hscroll_tng = NULL;
94    ptxtng->vscroll_tng = NULL;
95    ptxtng->options = options;
96 
97    if (inited)
98       fnt_select(0);                   /* select font 0 */
99    else
100       fnt_init_from_style(ptng->style);          /* get the default font */
101    TTs.left_m = 0;  TTs.right_m = 0;
102    TTs.max_w = 0;
103    TTs.mode = 0;
104    if (options & TNG_TG_SINGLE_LINE)
105       TTs.mode |= TTS_SINGLE;
106    if (options & TNG_TG_READ_ONLY)
107       TTs.mode |= TTS_READONLY;
108    if (options & TNG_TG_LINE_SET)
109       TTs.mode |= TTS_LINES;
110    if (TTs.mode == 0)
111       TTs.mode = TTS_FULL | TTS_WRAP;
112    //Spew(DSRC_UI_Textgadget, ("options = %x  TTs.mode = %x\n",options,TTs.mode));
113    TTr.crn.pt.x = abs_loc.x;
114    TTr.crn.pt.y = abs_loc.y;
115    TTr.w = size.x - (2 * TNG_TG_BORDER_WIDTH);
116    if (options & TNG_TG_VERT_SCROLL)
117    {
118       TTr.crn.pt.x += TNG_TG_SCROLL_X;
119       TTr.w -= TNG_TG_SCROLL_X;
120    }
121    TTr.crn.pt.x += TNG_TG_BORDER_WIDTH;
122    TTr.h = size.y - (2 * TNG_TG_BORDER_WIDTH);
123    TTr.crn.pt.y += TNG_TG_BORDER_WIDTH;
124    if (options & TNG_TG_HORZ_SCROLL)
125    {
126       TTr.crn.pt.y += TNG_TG_SCROLL_Y;
127       TTr.h -= TNG_TG_SCROLL_Y;
128    }
129    TTs.r_cnt = 1;
130    TTs.last_ev = TTEV_NULL;
131    ptxtng->tt=tt_full_build(&TTr,&TTs,&TTTNGFontInfo,ptng,NULL, &tng_textgadget_ui_display);
132 
133    return(OK);
134 }
135 
tng_textgadget_init2(TNG * ptng)136 errtype tng_textgadget_init2(TNG *ptng)
137 {
138    TNG_textgadget *ptxtng;
139    int id;
140    LGPoint sloc, ssize;
141 
142    ptxtng = TNG_TG(ptng);
143 
144    if (ptxtng->options & TNG_TG_HORZ_SCROLL)
145    {
146       sloc.x = TNG_TG_SCROLL_X + TNG_TG_BORDER_WIDTH;
147       sloc.y = TNG_TG_BORDER_WIDTH;
148       ssize.x = ptxtng->size.x - sloc.x - TNG_TG_BORDER_WIDTH;
149       ssize.y = TNG_TG_SCROLL_Y;
150       TNG_CREATE_SLIDER(ptng->ui_data, sloc, &(ptxtng->hscroll_tng), ptng->style, TNG_SL_HORIZONTAL, 0, 100, 0, 5, ssize);
151       tng_install_callback(ptxtng->hscroll_tng, TNG_EVENT_SIGNAL, TNG_SIGNAL_CHANGED, &tng_textgadget_hscroll_changed, (intptr_t)ptng, &id);
152    }
153    else
154    {
155       ptxtng->hscroll_tng = NULL;
156    }
157    if (ptxtng->options & TNG_TG_VERT_SCROLL)
158    {
159       sloc.x = TNG_TG_BORDER_WIDTH;
160       sloc.y = TNG_TG_SCROLL_Y + TNG_TG_BORDER_WIDTH;
161       ssize.x = TNG_TG_SCROLL_X;
162       ssize.y = ptxtng->size.y - sloc.y - TNG_TG_BORDER_WIDTH;
163       TNG_CREATE_SLIDER(ptng->ui_data, sloc, &(ptxtng->vscroll_tng), ptng->style, TNG_SL_VERTICAL, 0, 100, 0, 5, ssize);
164       tng_install_callback(ptxtng->vscroll_tng, TNG_EVENT_SIGNAL, TNG_SIGNAL_CHANGED, &tng_textgadget_vscroll_changed, (intptr_t)ptng, &id);
165    }
166    else
167    {
168       ptxtng->vscroll_tng = NULL;
169    }
170 
171    return(OK);
172 }
173 
174 // Draw the specified parts (may be all) of the TNG at screen coordinates loc
175 // assumes all appropriate setup has already been done!
tng_textgadget_2d_draw(TNG * ptng,ushort us,LGPoint loc)176 errtype tng_textgadget_2d_draw(TNG *ptng, ushort us, LGPoint loc)
177 {
178    TNG_textgadget *ptxtng;
179    LGRect r;
180    TextTool *t;
181 
182    //Spew(DSRC_UI_Textgadget, ("TNG Textgadget 2d Draw at (%d, %d) -- partmask = %x\n",loc.x,loc.y,partmask));
183    TNG_IF_OBSCURED(ptng)
184    {
185       return(OK);
186    }
187    ptng->signal(ptng, TNG_SIGNAL_EXPOSE);
188    ptxtng = TNG_TG(ptng);
189    TNGDrawBase(ptng, loc, ptxtng->size);
190    r.ul.x = loc.x + TNG_TG_BORDER_WIDTH;
191    r.ul.y = loc.y + TNG_TG_BORDER_WIDTH;
192    t = TNG_TG_TT(ptng);
193    if (t != NULL)
194       tt_show_all(t);
195 
196    return(OK);
197 }
198 
199 // Fill in ppt with the size...
tng_textgadget_size(TNG * ptng,LGPoint * ppt)200 errtype tng_textgadget_size(TNG *ptng, LGPoint *ppt)
201 {
202    *ppt = TNG_TG(ptng)->size;
203    return(OK);
204 }
205 
206 // Returns the current "value" of the TNG
tng_textgadget_getvalue(TNG * ptng)207 int tng_textgadget_getvalue(TNG *ptng)
208 {
209    return(TNG_TG(ptng)->last_key);
210 }
211 
212 // React appropriately for receiving the specified cooked key
tng_textgadget_keycooked(TNG * ptng,ushort key)213 uchar tng_textgadget_keycooked(TNG *ptng, ushort key)
214 {
215    short code;
216    uchar retval = FALSE;
217 /*¥¥¥
218 
219    code = key & 0xff;
220    //Spew(DSRC_UI_Textgadget, ("%x was typed!\n",code));
221    tt_parse_char(TNG_TG_TT(ptng),key);
222    switch(code)
223    {
224       case TNG_TG_RETURN_KEY:
225          IF_SET_RV(ptng->signal(ptng, TNG_SIGNAL_SELECT));
226          break;
227       case TNG_TG_SCROLL_UP_KEY:
228       case TNG_TG_SCROLL_DOWN_KEY:
229       case TNG_TG_SCROLL_LEFT_KEY:
230       case TNG_TG_SCROLL_RIGHT_KEY:
231          TNG_TG_LASTKEY(ptng) = code;
232          IF_SET_RV(ptng->signal(ptng, TNG_SIGNAL_SCROLL));
233          break;
234    }
235 //   Spew(DSRC_UI_Textgadget, ("About to tt_parse_char...\n"));
236    IF_SET_RV(tng_cb_keycooked(ptng, key));
237    retval = TRUE;
238 */
239    return(retval);
240 }
241 
242 // React appropriately for receiving the specified mouse button event
tng_textgadget_mousebutt(TNG * ptng,uchar type,LGPoint loc)243 uchar tng_textgadget_mousebutt(TNG *ptng, uchar type, LGPoint loc)
244 {
245    return(tng_cb_mousebutt(ptng,type,loc));
246 }
247 
248 // Handle incoming signals
tng_textgadget_signal(TNG * ptng,ushort signal)249 uchar tng_textgadget_signal(TNG *ptng, ushort signal)
250 {
251    uchar retval = FALSE;
252    //Spew(DSRC_UI_Textgadget, ("Textgadget Received signal: %x\n",signal));
253    if (signal & TNG_SIGNAL_CHANGED)
254       TNG_DRAWPART(ptng, TNG_ALLPARTS);
255    if (signal & TNG_SIGNAL_SCROLL)
256       tng_textgadget_scroll(ptng);
257    IF_SET_RV(tng_cb_signal(ptng, signal));
258    retval = TRUE;
259    return(retval);
260 }
261 // -----------------------------
262 
263 
264 // Assumes that lastkey contains the hex code of the
265 // appropriate kind of scroll key.
tng_textgadget_scroll(TNG * ptng)266 errtype tng_textgadget_scroll(TNG *ptng)
267 {
268 /*¥¥¥
269    short code;
270    TNG *which_bar;
271    uchar increm;
272    code = TNG_TG_LASTKEY(ptng);
273    which_bar = NULL;
274    switch (code)
275    {
276       case TNG_TG_SCROLL_UP_KEY:
277          which_bar = TNG_TG(ptng)->vscroll_tng;
278          increm = TRUE;
279          break;
280       case TNG_TG_SCROLL_DOWN_KEY:
281          which_bar = TNG_TG(ptng)->vscroll_tng;
282          increm = FALSE;
283          break;
284       case TNG_TG_SCROLL_LEFT_KEY:
285          which_bar = TNG_TG(ptng)->hscroll_tng;
286          increm = FALSE;
287          break;
288       case TNG_TG_SCROLL_RIGHT_KEY:
289          which_bar = TNG_TG(ptng)->hscroll_tng;
290          increm = TRUE;
291          break;
292    }
293    if (which_bar != NULL)
294    {
295       if (increm)
296          which_bar->signal(which_bar, TNG_SIGNAL_INCREMENT);
297       else
298          which_bar->signal(which_bar, TNG_SIGNAL_DECREMENT);
299       ptng->signal(ptng, TNG_SIGNAL_CHANGED);
300    }
301 */
302    return(OK);
303 }
304 
tng_textgadget_addstring(TNG * ptng,char * s)305 errtype tng_textgadget_addstring(TNG *ptng, char *s)
306 {
307    region_begin_sequence();
308    tt_parse_string(TNG_TG_TT(ptng), s);
309    region_end_sequence(TRUE);
310    return(OK);
311 }
312