imtk

view src/imtk.c @ 5:09b6e8a5dc14

reorganizing the code
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Apr 2011 14:22:20 +0300
parents 00a4ea4ee6dc
children 6d35e6c7b2ca
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <stdarg.h>
6 #include <assert.h>
7 #ifndef __APPLE__
8 #include <GL/glut.h>
9 #else
10 #include <GLUT/glut.h>
11 #endif
12 #include "imtk.h"
15 static void draw_progress(int id, float pos, int x, int y);
18 void imtk_begin(void)
19 {
20 glMatrixMode(GL_PROJECTION);
21 glPushMatrix();
22 glLoadIdentity();
23 glTranslatef(-1, 1, 0);
24 glScalef(2.0 / scr_width, -2.0 / scr_height, 1.0);
26 glMatrixMode(GL_MODELVIEW);
27 glPushMatrix();
28 glLoadIdentity();
30 glPushAttrib(GL_ENABLE_BIT);
31 glDisable(GL_DEPTH_TEST);
32 glDisable(GL_CULL_FACE);
33 glDisable(GL_LIGHTING);
34 }
36 void imtk_end(void)
37 {
38 glPopAttrib();
40 glMatrixMode(GL_PROJECTION);
41 glPopMatrix();
42 glMatrixMode(GL_MODELVIEW);
43 glPopMatrix();
44 }
47 void imtk_post_redisplay(void)
48 {
49 glutPostRedisplay();
50 }
54 void imtk_progress(int id, float pos, int x, int y)
55 {
56 draw_progress(id, pos, x, y);
57 }
59 int imtk_listbox(int id, const char *list, int sel, int x, int y)
60 {
61 int i;
62 assert(id >= 0);
64 if(!list) {
65 return -1;
66 }
68 if(id & 1) {
69 id++;
70 }
72 for(i=0; *list; i++) {
73 if(imtk_button(id + i * 2 + 1, list, x, y + i * 20)) {
74 sel = i;
75 }
76 list += strlen(list) + 1;
77 }
78 return sel;
79 }
81 int imtk_combobox(int id, char *textbuf, size_t buf_sz, const char *list, int sel, int x, int y)
82 {
83 imtk_textbox(id + 1, textbuf, buf_sz, x, y);
84 imtk_button(id + 3, "V", x + TEXTBOX_SIZE, y);
86 if(prev_active == id + 3) {
87 sel = imtk_listbox(id + 5, list, sel, x, y + 20);
88 }
89 return sel;
90 }
92 char *imtk_create_list(const char *first, ...)
93 {
94 int sz;
95 char *buf, *item;
96 va_list ap;
98 if(!first) {
99 return 0;
100 }
102 sz = strlen(first) + 2;
103 if(!(buf = malloc(sz))) {
104 return 0;
105 }
106 memcpy(buf, first, sz - 2);
107 buf[sz - 1] = buf[sz - 2] = 0;
109 va_start(ap, first);
110 while((item = va_arg(ap, char*))) {
111 int len = strlen(item);
112 char *tmp = realloc(buf, sz + len + 1);
113 if(!tmp) {
114 free(buf);
115 return 0;
116 }
117 buf = tmp;
119 memcpy(buf + sz - 1, item, len);
120 sz += len + 1;
121 buf[sz - 1] = buf[sz - 2] = 0;
122 }
123 va_end(ap);
125 return buf;
126 }
128 void imtk_free_list(char *list)
129 {
130 free(list);
131 }
134 static void draw_progress(int id, float pos, int x, int y)
135 {
136 int bar_size = SLIDER_SIZE * pos;
138 if(pos < 0.0) pos = 0.0;
139 if(pos > 1.0) pos = 1.0;
141 /* through */
142 glBegin(GL_QUADS);
143 glColor3fv(colors[IMTK_BASE_COLOR]);
144 glVertex2f(x - 1, y - 1);
145 glVertex2f(x + SLIDER_SIZE + 1, y - 1);
146 glVertex2f(x + SLIDER_SIZE + 1, y + 17);
147 glVertex2f(x - 1, y + 17);
148 glEnd();
149 draw_frame(x - 1, y - 1, SLIDER_SIZE + 2, 17, FRAME_INSET);
151 if(pos > 0.0) {
152 /* bar */
153 glBegin(GL_QUADS);
154 glColor3fv(colors[IMTK_BASE_COLOR]);
155 glVertex2f(x, y);
156 glVertex2f(x + bar_size, y);
157 glVertex2f(x + bar_size, y + 15);
158 glVertex2f(x, y + 15);
159 glEnd();
160 draw_frame(x, y, bar_size, 15, FRAME_OUTSET);
161 }
162 }