File: infowin.c

package info (click to toggle)
notion 4.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,656 kB
  • sloc: ansic: 47,365; sh: 2,093; makefile: 594; perl: 270
file content (269 lines) | stat: -rw-r--r-- 4,872 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * ion/ioncore/infowin.h
 *
 * Copyright (c) Tuomo Valkonen 1999-2007.
 *
 * See the included file LICENSE for details.
 */

#include <string.h>

#include <libtu/objp.h>
#include "common.h"
#include "global.h"
#include "window.h"
#include "infowin.h"
#include "resize.h"
#include "gr.h"
#include "event.h"
#include "strings.h"


/*{{{ Init/deinit */


bool infowin_init(WInfoWin *p, WWindow *parent, const WFitParams *fp,
                  const char *style)
{
    XSetWindowAttributes attr;

    if(!window_init(&(p->wwin), parent, fp, "WInfoWin"))
        return FALSE;

    p->buffer=ALLOC_N(char, INFOWIN_BUFFER_LEN);
    if(p->buffer==NULL)
        goto fail;
    p->buffer[0]='\0';

    if(style==NULL)
        p->style=scopy("*");
    else
        p->style=scopy(style);
    if(p->style==NULL)
        goto fail2;

    p->brush=NULL;

    gr_stylespec_init(&p->attr);

    infowin_updategr(p);

    if(p->brush==NULL)
        goto fail3;

    p->wwin.region.flags|=REGION_SKIP_FOCUS;

    /* Enable save unders */
    attr.save_under=True;
    XChangeWindowAttributes(ioncore_g.dpy, p->wwin.win, CWSaveUnder, &attr);

    window_select_input(&(p->wwin), IONCORE_EVENTMASK_NORMAL);

    return TRUE;

fail3:
    gr_stylespec_unalloc(&p->attr);
    free(p->style);
fail2:
    free(p->buffer);
fail:
    window_deinit(&(p->wwin));
    return FALSE;
}


WInfoWin *create_infowin(WWindow *parent, const WFitParams *fp,
                         const char *style)
{
    CREATEOBJ_IMPL(WInfoWin, infowin, (p, parent, fp, style));
}


void infowin_deinit(WInfoWin *p)
{
    if(p->buffer!=NULL){
        free(p->buffer);
        p->buffer=NULL;
    }

    if(p->style!=NULL){
        free(p->style);
        p->style=NULL;
    }

    if(p->brush!=NULL){
        grbrush_release(p->brush);
        p->brush=NULL;
    }

    gr_stylespec_unalloc(&p->attr);

    window_deinit(&(p->wwin));
}


/*}}}*/


/*{{{ Drawing and geometry */


void infowin_draw(WInfoWin *p, bool UNUSED(complete))
{
    WRectangle g;

    if(p->brush==NULL)
        return;

    g.x=0;
    g.y=0;
    g.w=REGION_GEOM(p).w;
    g.h=REGION_GEOM(p).h;

    grbrush_begin(p->brush, &g, GRBRUSH_NO_CLEAR_OK);
    grbrush_init_attr(p->brush, &p->attr);
    grbrush_draw_textbox(p->brush, &g, p->buffer, TRUE);
    grbrush_end(p->brush);
}


void infowin_updategr(WInfoWin *p)
{
    GrBrush *nbrush;

    assert(p->style!=NULL);

    nbrush=gr_get_brush(p->wwin.win,
                        region_rootwin_of((WRegion*)p),
                        p->style);
    if(nbrush==NULL)
        return;

    if(p->brush!=NULL)
        grbrush_release(p->brush);

    p->brush=nbrush;

    window_draw(&(p->wwin), TRUE);
}



/*}}}*/


/*{{{ Content-setting */


GrStyleSpec *infowin_stylespec(WInfoWin *p)
{
    return &p->attr;
}


static void infowin_do_set_text(WInfoWin *p, const char *str)
{
    strncpy(INFOWIN_BUFFER(p), str, INFOWIN_BUFFER_LEN);
    INFOWIN_BUFFER(p)[INFOWIN_BUFFER_LEN-1]='\0';
}


static void infowin_resize(WInfoWin *p)
{
    WRQGeomParams rq=RQGEOMPARAMS_INIT;
    const char *str=INFOWIN_BUFFER(p);
    GrBorderWidths bdw;
    GrFontExtents fnte;

    rq.flags=REGION_RQGEOM_WEAK_X|REGION_RQGEOM_WEAK_Y;

    rq.geom.x=REGION_GEOM(p).x;
    rq.geom.y=REGION_GEOM(p).y;

    grbrush_get_border_widths(p->brush, &bdw);
    grbrush_get_font_extents(p->brush, &fnte);

    rq.geom.w=bdw.left+bdw.right;
    rq.geom.w+=grbrush_get_text_width(p->brush, str, strlen(str));
    rq.geom.h=fnte.max_height+bdw.top+bdw.bottom;

    if(rectangle_compare(&rq.geom, &REGION_GEOM(p))!=RECTANGLE_SAME)
        region_rqgeom((WRegion*)p, &rq, NULL);
}


/*EXTL_DOC
 * Set contents of the info window.
 */
EXTL_EXPORT_MEMBER
void infowin_set_text(WInfoWin *p, const char *str, int maxw)
{
    bool set=FALSE;

    if(maxw>0 && p->brush!=NULL){
        char *tmp=grbrush_make_label(p->brush, str, maxw);
        if(tmp!=NULL){
            infowin_do_set_text(p, tmp);
            free(tmp);
            set=TRUE;
        }
    }

    if(!set)
        infowin_do_set_text(p, str);

    infowin_resize(p);

    /* sometimes unnecessary */
    window_draw((WWindow*)p, TRUE);
}


/*}}}*/


/*{{{ Load */


WRegion *infowin_load(WWindow *par, const WFitParams *fp, ExtlTab tab)
{
    char *style=NULL, *text=NULL;
    WInfoWin *p;

    extl_table_gets_s(tab, "style", &style);

    p=create_infowin(par, fp, style);

    free(style);

    if(p==NULL)
        return NULL;

    if(extl_table_gets_s(tab, "text", &text)){
        infowin_do_set_text(p, text);
        free(text);
    }

    return (WRegion*)p;
}


/*}}}*/


/*{{{ Dynamic function table and class implementation */


static DynFunTab infowin_dynfuntab[]={
    {window_draw, infowin_draw},
    {region_updategr, infowin_updategr},
    END_DYNFUNTAB
};


EXTL_EXPORT
IMPLCLASS(WInfoWin, WWindow, infowin_deinit, infowin_dynfuntab);


/*}}}*/