]> git.decadent.org.uk Git - ion3.git/blob - ioncore/infowin.c
[svn-inject] Installing original source of ion3
[ion3.git] / ioncore / infowin.c
1 /*
2  * ion/ioncore/infowin.h
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2006. 
5  *
6  * Ion is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <string.h>
13
14 #include <libtu/objp.h>
15 #include "common.h"
16 #include "global.h"
17 #include "window.h"
18 #include "infowin.h"
19 #include "resize.h"
20 #include "gr.h"
21 #include "event.h"
22
23
24 /*{{{ Init/deinit */
25
26
27 bool infowin_init(WInfoWin *p, WWindow *parent, const WFitParams *fp,
28                   const char *style)
29 {
30     XSetWindowAttributes attr;
31     
32     if(!window_init(&(p->wwin), parent, fp))
33         return FALSE;
34     
35     p->buffer=ALLOC_N(char, INFOWIN_BUFFER_LEN);
36     if(p->buffer==NULL)
37         goto fail;
38     p->buffer[0]='\0';
39     
40     if(style==NULL)
41         p->style=scopy("*");
42     else
43         p->style=scopy(style);
44     if(p->style==NULL)
45         goto fail2;
46     
47     p->brush=NULL;
48     p->attr=NULL;
49     
50     infowin_updategr(p);
51     
52     if(p->brush==NULL)
53         goto fail3;
54     
55     p->wwin.region.flags|=REGION_SKIP_FOCUS;
56     
57     /* Enable save unders */
58     attr.save_under=True;
59     XChangeWindowAttributes(ioncore_g.dpy, p->wwin.win, CWSaveUnder, &attr);
60     
61     window_select_input(&(p->wwin), IONCORE_EVENTMASK_NORMAL);
62
63     return TRUE;
64
65 fail3:    
66     free(p->style);
67 fail2:
68     free(p->buffer);
69 fail:    
70     window_deinit(&(p->wwin));
71     return FALSE;
72 }
73
74
75 WInfoWin *create_infowin(WWindow *parent, const WFitParams *fp,
76                          const char *style)
77 {
78     CREATEOBJ_IMPL(WInfoWin, infowin, (p, parent, fp, style));
79 }
80
81
82 void infowin_deinit(WInfoWin *p)
83 {
84     if(p->buffer!=NULL){
85         free(p->buffer);
86         p->buffer=NULL;
87     }
88
89     if(p->attr!=NULL){
90         free(p->attr);
91         p->attr=NULL;
92     }
93     
94     if(p->style!=NULL){
95         free(p->style);
96         p->style=NULL;
97     }
98     
99     if(p->brush!=NULL){
100         grbrush_release(p->brush);
101         p->brush=NULL;
102     }
103     
104     window_deinit(&(p->wwin));
105 }
106
107
108 /*}}}*/
109
110
111 /*{{{ Drawing and geometry */
112
113
114 void infowin_draw(WInfoWin *p, bool complete)
115 {
116     WRectangle g;
117     
118     if(p->brush==NULL)
119         return;
120     
121     g.x=0;
122     g.y=0;
123     g.w=REGION_GEOM(p).w;
124     g.h=REGION_GEOM(p).h;
125
126     grbrush_begin(p->brush, &g, GRBRUSH_NO_CLEAR_OK);
127     grbrush_draw_textbox(p->brush, &g, p->buffer, p->attr, TRUE);
128     grbrush_end(p->brush);
129 }
130
131
132 void infowin_updategr(WInfoWin *p)
133 {
134     GrBrush *nbrush;
135     
136     assert(p->style!=NULL);
137     
138     nbrush=gr_get_brush(p->wwin.win, 
139                         region_rootwin_of((WRegion*)p),
140                         p->style);
141     if(nbrush==NULL)
142         return;
143     
144     if(p->brush!=NULL)
145         grbrush_release(p->brush);
146     
147     p->brush=nbrush;
148     
149     window_draw(&(p->wwin), TRUE);
150 }
151
152
153
154 /*}}}*/
155
156
157 /*{{{ Content-setting */
158
159
160 bool infowin_set_attr2(WInfoWin *p, const char *attr1, const char *attr2)
161 {
162     char *p2=NULL;
163     
164     if(attr1!=NULL){
165         if(attr2==NULL)
166             p2=scopy(attr1);
167         else
168             libtu_asprintf(&p2, "%s-%s", attr1, attr2);
169         if(p2==NULL)
170             return FALSE;
171     }
172     
173     if(p->attr)
174         free(p->attr);
175     
176     p->attr=p2;
177     
178     return TRUE;
179 }
180
181
182 static void infowin_do_set_text(WInfoWin *p, const char *str)
183 {
184     strncpy(INFOWIN_BUFFER(p), str, INFOWIN_BUFFER_LEN);
185     INFOWIN_BUFFER(p)[INFOWIN_BUFFER_LEN-1]='\0';
186 }
187
188
189 static void infowin_resize(WInfoWin *p)
190 {
191     WRQGeomParams rq=RQGEOMPARAMS_INIT;
192     const char *str=INFOWIN_BUFFER(p);
193     GrBorderWidths bdw;
194     GrFontExtents fnte;
195     
196     rq.flags=REGION_RQGEOM_WEAK_X|REGION_RQGEOM_WEAK_Y;
197     
198     rq.geom.x=REGION_GEOM(p).x;
199     rq.geom.y=REGION_GEOM(p).y;
200     
201     grbrush_get_border_widths(p->brush, &bdw);
202     grbrush_get_font_extents(p->brush, &fnte);
203         
204     rq.geom.w=bdw.left+bdw.right;
205     rq.geom.w+=grbrush_get_text_width(p->brush, str, strlen(str));
206     rq.geom.h=fnte.max_height+bdw.top+bdw.bottom;
207
208     if(rectangle_compare(&rq.geom, &REGION_GEOM(p))!=RECTANGLE_SAME)
209         region_rqgeom((WRegion*)p, &rq, NULL);
210 }
211
212
213 /*EXTL_DOC
214  * Set contents of the info window.
215  */
216 EXTL_EXPORT_MEMBER
217 void infowin_set_text(WInfoWin *p, const char *str)
218 {
219     infowin_do_set_text(p, str);
220
221     infowin_resize(p);
222     
223     /* sometimes unnecessary */
224     window_draw((WWindow*)p, TRUE);
225 }
226
227
228 /*}}}*/
229
230
231 /*{{{ Load */
232
233
234 WRegion *infowin_load(WWindow *par, const WFitParams *fp, ExtlTab tab)
235 {
236     char *style=NULL, *text=NULL;
237     WInfoWin *p;
238     
239     extl_table_gets_s(tab, "style", &style);
240     
241     p=create_infowin(par, fp, style);
242     
243     free(style);
244     
245     if(p==NULL)
246         return NULL;
247
248     if(extl_table_gets_s(tab, "text", &text)){
249         infowin_do_set_text(p, text);
250         free(text);
251     }
252     
253     return (WRegion*)p;
254 }
255
256
257 /*}}}*/
258
259
260 /*{{{ Dynamic function table and class implementation */
261
262
263 static DynFunTab infowin_dynfuntab[]={
264     {window_draw, infowin_draw},
265     {region_updategr, infowin_updategr},
266     END_DYNFUNTAB
267 };
268
269
270 EXTL_EXPORT
271 IMPLCLASS(WInfoWin, WWindow, infowin_deinit, infowin_dynfuntab);
272
273     
274 /*}}}*/
275