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