853d28c85bf2cbf3d5cde7039191e176540a8a28
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / renderers / widget / textinput.c
1 /*
2  * Acess2 Window Manager v3
3  * - By John Hodge (thePowersGang)
4  * 
5  * renderer/widget/textinput.c
6  * - Single line text box
7  *
8  * TODO: Support Right-to-Left text
9  */
10 #include <common.h>
11 #include "./common.h"
12 #include "./colours.h"
13
14 struct sTextInputInfo
15 {
16          int    DrawOfs;        // Byte offset for the leftmost character
17          int    CursorXOfs;     // Pixel offset of the cursor
18 };
19
20 void Widget_TextInput_Render(tWindow *Window, tElement *Element)
21 {
22         struct sTextInputInfo   *info = (void*)Element->Data;
23         struct sWidgetWin       *wininfo = Window->RendererInfo;
24         
25         WM_Render_FillRect(Window, 
26                 Element->CachedX, Element->CachedY,
27                 Element->CachedW, Element->CachedH,
28                 TEXTINPUT_BACKGROUND
29                 );
30         WM_Render_DrawRect(Window, 
31                 Element->CachedX, Element->CachedY,
32                 Element->CachedW, Element->CachedH,
33                 TEXTINPUT_BORDER_OUT
34                 );
35         WM_Render_DrawRect(Window, 
36                 Element->CachedX+1, Element->CachedY+1,
37                 Element->CachedW-2, Element->CachedH-2,
38                 TEXTINPUT_BORDER_IN
39                 );
40         
41         // Text
42         WM_Render_DrawText(Window,
43                 Element->CachedX+2, Element->CachedY+2,
44                 Element->CachedW-4, Element->CachedW-4,
45                 NULL, TEXTINPUT_TEXT,
46                 &Element->Text[info->DrawOfs]
47                 );
48
49         // TODO: Determine if this element has focus
50         if( wininfo->FocusedElement == Element )
51         {
52                 // TODO: Multiple Cursors
53                 WM_Render_SetTextCursor(Window,
54                         Element->CachedX+2+info->CursorXOfs,
55                         Element->CachedY+2,
56                         Element->CachedW-4, 1,
57                         TEXTINPUT_TEXT
58                         );
59         }
60 }
61
62 void Widget_TextInput_Init(tElement *Element)
63 {
64         struct sTextInputInfo   *info;
65          int    h;
66
67         // TODO: Select font correctly  
68         WM_Render_GetTextDims(NULL, "jJ", NULL, &h);
69
70         h += 2+2;       // Border padding       
71
72         Element->MinH = h;
73         Element->MinW = 4;
74
75         info = Element->Data = malloc(sizeof(*info));
76         info->DrawOfs = 0;
77         info->CursorXOfs = 0;
78
79         // No need to explicitly update parent min dims, as the AddElement routine does that    
80 }
81
82 DEFWIDGETTYPE(ELETYPE_TEXTINPUT,
83         WIDGETTYPE_FLAG_NOCHILDREN,
84         .Render = Widget_TextInput_Render,
85         .Init = Widget_TextInput_Init
86         );
87

UCC git Repository :: git.ucc.asn.au