Backend support for using dynamic styles. See also Ticket #49.
[rodin/chimara.git] / tests / styletest.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <assert.h>
5 #include <libchimara/glk.h>
6
7 void print_help();
8 void do_style_test();
9 void do_link_test();
10 void do_mouse_test();
11
12 winid_t mainwin;
13 winid_t statuswin;
14
15 void glk_main(void)
16 {
17         char stringbuffer[128];
18     event_t ev;
19
20         /* Define some custom styles */
21         glk_stylehint_set(wintype_AllTypes, style_User1, stylehint_TextColor, 0x00FF0000);
22
23     mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 0);
24     if(!mainwin)
25         return;
26
27         statuswin = glk_window_open(mainwin, winmethod_Above | winmethod_Fixed, 3, wintype_TextGrid, 1);
28     
29     glk_set_window(mainwin);
30     
31     char *buffer = calloc(256, sizeof(char));
32     assert(buffer);
33     
34     glk_put_string("Welcome to the style test\n");
35     glk_request_line_event(mainwin, buffer, 255, 0);
36     while(strncmp(buffer, "quit", 4)) 
37     {
38         glk_select(&ev);
39         if(ev.type == evtype_LineInput)
40         {
41                         if( !strncmp(buffer, "help", 4) ) {
42                                 print_help();
43                         }
44                         else if( !strncmp(buffer, "style", 4) ) {
45                                 do_style_test();
46                         }
47                         else if( !strncmp(buffer, "link", 4) ) {
48                                 do_link_test();
49                         }
50                         else if( !strncmp(buffer, "mouse", 4) ) {
51                                 do_mouse_test();
52                         }
53                         else {
54                                 glk_put_string("Huh?\n");
55                         }
56                 glk_request_line_event(mainwin, buffer, 255, 0);
57         }
58                 else if(ev.type == evtype_Hyperlink)
59                 {
60                         glk_cancel_line_event(mainwin, NULL);
61                         snprintf(stringbuffer, 128, "Link %d was clicked\n", ev.val1);
62                         glk_put_string(stringbuffer);
63                 glk_request_line_event(mainwin, buffer, 255, 0);
64                 }
65                 else if(ev.type == evtype_MouseInput)
66                 {
67                         glk_cancel_line_event(mainwin, NULL);
68                         snprintf(stringbuffer, 128, "Mouse click: x=%d, y=%d\n", ev.val1, ev.val2);
69                         glk_put_string(stringbuffer);
70                 glk_request_line_event(mainwin, buffer, 255, 0);
71                 }
72     }
73
74     glk_cancel_line_event(mainwin, NULL);
75     glk_window_clear(mainwin);
76     free(buffer);
77 }
78
79 void
80 do_style_test() {
81         glk_set_style(style_Normal);
82         glk_put_string("Normal\n");
83
84         glk_set_style(style_Emphasized);
85         glk_put_string("Emphasized\n");
86
87         glk_set_style(style_Preformatted);
88         glk_put_string("Preformatted\n");
89
90         glk_set_style(style_Header);
91         glk_put_string("Header\n");
92
93         glk_set_style(style_Subheader);
94         glk_put_string("Subheader\n");
95
96         glk_set_style(style_Alert);
97         glk_put_string("Alert\n");
98
99         glk_set_style(style_Note);
100         glk_put_string("Note\n");
101
102         glk_set_style(style_BlockQuote);
103         glk_put_string("BlockQuote\n");
104
105         glk_set_style(style_Input);
106         glk_put_string("Input\n");
107
108         glk_set_style(style_User1);
109         glk_put_string("User1\n");
110
111         glk_set_style(style_User2);
112         glk_put_string("User2\n");
113
114         glk_set_style(style_Normal);
115 }
116
117 void
118 do_link_test() {
119     glk_set_window(mainwin);
120         glk_set_hyperlink(1);
121         glk_put_string("This is link 1\n");
122         glk_set_hyperlink(2);
123         glk_put_string("This is link 2\n");
124         glk_set_hyperlink(0);
125
126     glk_set_window(statuswin);
127         glk_set_hyperlink(3);
128         glk_window_move_cursor(statuswin, 0, 0);
129         glk_put_string("This is link 3\n");
130         glk_set_hyperlink(4);
131         glk_window_move_cursor(statuswin, 0, 1);
132         glk_put_string("This is link 4\n");
133         glk_set_hyperlink(0);
134
135         glk_request_hyperlink_event(mainwin);
136         glk_request_hyperlink_event(statuswin);
137
138     glk_set_window(mainwin);
139 }
140
141 void
142 do_mouse_test() {
143     glk_set_window(statuswin);
144         glk_window_move_cursor(statuswin, 0, 0);
145         glk_put_string("Click me......\n");
146         glk_request_mouse_event(statuswin);
147         glk_set_window(mainwin);
148 }
149
150 void
151 print_help() {
152         glk_put_string("The following commands are supported:\n - help (this help text)\n - style (perform style test)\n - link (perform hyperlink test)\n - mouse (perform mouse test)\n - quit (quit the program)\n");
153 }