~ubuntu-branches/ubuntu/karmic/grace/karmic

« back to all changes in this revision

Viewing changes to src/fontwin.c

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2002-03-19 14:19:58 UTC
  • Revision ID: james.westby@ubuntu.com-20020319141958-5gxna6vo1ek3zjml
Tags: upstream-5.1.7
ImportĀ upstreamĀ versionĀ 5.1.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Grace - GRaphing, Advanced Computation and Exploration of data
 
3
 * 
 
4
 * Home page: http://plasma-gate.weizmann.ac.il/Grace/
 
5
 * 
 
6
 * Copyright (c) 1991-1995 Paul J Turner, Portland, OR
 
7
 * Copyright (c) 1996-2000 Grace Development Team
 
8
 * 
 
9
 * Maintained by Evgeny Stambulchik <fnevgeny@plasma-gate.weizmann.ac.il>
 
10
 * 
 
11
 * 
 
12
 *                           All Rights Reserved
 
13
 * 
 
14
 *    This program is free software; you can redistribute it and/or modify
 
15
 *    it under the terms of the GNU General Public License as published by
 
16
 *    the Free Software Foundation; either version 2 of the License, or
 
17
 *    (at your option) any later version.
 
18
 * 
 
19
 *    This program is distributed in the hope that it will be useful,
 
20
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
 *    GNU General Public License for more details.
 
23
 * 
 
24
 *    You should have received a copy of the GNU General Public License
 
25
 *    along with this program; if not, write to the Free Software
 
26
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
27
 */
 
28
 
 
29
/* 
 
30
 *
 
31
 * Font tool
 
32
 *
 
33
 */
 
34
 
 
35
#include <config.h>
 
36
 
 
37
#include <X11/X.h>
 
38
 
 
39
#include <Xm/Xm.h>
 
40
#include <Xm/DrawingA.h>
 
41
#include <Xm/Form.h>
 
42
#include <Xm/RowColumn.h>
 
43
#include <Xm/ScrolledW.h>
 
44
#include <Xm/DialogS.h>
 
45
#include <Xm/Text.h>
 
46
#include <Xm/XmosP.h>
 
47
 
 
48
#include <Xbae/Matrix.h>
 
49
 
 
50
#include "t1fonts.h"
 
51
#include "utils.h"
 
52
#include "motifinc.h"
 
53
#include "protos.h"
 
54
 
 
55
 
 
56
/* used globally */
 
57
extern Widget app_shell;
 
58
extern Display *disp;
 
59
extern Window root;
 
60
extern GC gc;
 
61
extern int depth;
 
62
 
 
63
extern unsigned long xvlibcolors[];
 
64
 
 
65
static Widget fonttool_frame = NULL;
 
66
static OptionStructure *font_select_item;
 
67
static TextStructure *string_item = NULL;
 
68
 
 
69
static Widget cstext_parent = NULL;
 
70
 
 
71
static int FontID;
 
72
static BBox bbox;
 
73
static float Size = 16.8;
 
74
 
 
75
static int enable_edit_cb;
 
76
 
 
77
static void DrawCB(Widget w,XtPointer cd, XbaeMatrixDrawCellCallbackStruct *cbs);
 
78
static void EnterCB(Widget w, XtPointer cd, XbaeMatrixEnterCellCallbackStruct *cbs);
 
79
static void update_fonttool_cb(int value, void *data);
 
80
static void EditStringCB(Widget w, XtPointer client_data, XmAnyCallbackStruct *cbs);
 
81
static void fonttool_aac_cb(void *data);
 
82
 
 
83
void create_fonttool_cb(void *data)
 
84
{
 
85
    create_fonttool((Widget) data);
 
86
}
 
87
 
 
88
#ifdef NEW_CODE
 
89
static void enlarge_glyph(Widget parent,
 
90
    XtPointer closure, XEvent *event, Boolean* doit)
 
91
{
 
92
    XButtonPressedEvent *e = (XButtonPressedEvent *) event;
 
93
    if (e->button == 3) {
 
94
        int row, col;
 
95
        int x0, y0, x1, y1, cwidth, cheight;
 
96
        XbaeMatrixRowColToXY(parent, 0, 0, &x0, &y0);
 
97
        XbaeMatrixRowColToXY(parent, 1, 1, &x1, &y1);
 
98
        cwidth  = x1 - x0;
 
99
        cheight = y0 - y1;
 
100
        col = (e->x - xleft)/cwidth;
 
101
        row = (yupper - e->y)/cheight;
 
102
        printf("%d %d\n", col, row);
 
103
    }
 
104
}
 
105
#endif
 
106
 
 
107
void create_fonttool(Widget cstext)
 
108
{
 
109
    int i;
 
110
    short widths[16];
 
111
    unsigned char column_alignments[16];
 
112
    Widget fonttool_panel, font_table, aac_buts;
 
113
    
 
114
    if (string_item != NULL && cstext == string_item->text) {
 
115
        /* avoid recursion */
 
116
        return;
 
117
    }
 
118
    
 
119
    if (cstext_parent != NULL) {
 
120
        /* unlock previous parent */
 
121
        SetSensitive(cstext_parent, True);
 
122
    }
 
123
    
 
124
    cstext_parent = cstext;
 
125
    
 
126
    if (fonttool_frame == NULL) {
 
127
        fonttool_frame = XmCreateDialogShell(app_shell, "Font tool", NULL, 0);
 
128
        handle_close(fonttool_frame);
 
129
        fonttool_panel = XtVaCreateWidget("fonttool_panel", xmFormWidgetClass, 
 
130
                                        fonttool_frame, NULL, 0);
 
131
 
 
132
        font_select_item = CreateFontChoice(fonttool_panel, "Font:");
 
133
        XtVaSetValues(font_select_item->menu,
 
134
            XmNleftAttachment, XmATTACH_FORM,
 
135
            XmNrightAttachment, XmATTACH_FORM,
 
136
            XmNtopAttachment, XmATTACH_FORM,
 
137
            NULL);
 
138
        
 
139
        for (i = 0; i < 16; i++) {
 
140
            widths[i] = 2;
 
141
            column_alignments[i] = XmALIGNMENT_END;
 
142
        }
 
143
        font_table = XtVaCreateManagedWidget(
 
144
            "fontTable", xbaeMatrixWidgetClass, fonttool_panel,
 
145
            XmNrows, 16,
 
146
            XmNcolumns, 16,
 
147
            XmNvisibleRows, 8,
 
148
            XmNvisibleColumns, 16,
 
149
            XmNfill, True,
 
150
            XmNcolumnWidths, widths,
 
151
            XmNcolumnAlignments, column_alignments,
 
152
            XmNgridType, XmGRID_CELL_SHADOW,
 
153
            XmNcellShadowType, XmSHADOW_ETCHED_OUT,
 
154
            XmNcellShadowThickness, 2,
 
155
            XmNaltRowCount, 0,
 
156
            XmNleftAttachment, XmATTACH_FORM,
 
157
            XmNrightAttachment, XmATTACH_FORM,
 
158
            XmNtopAttachment, XmATTACH_WIDGET,
 
159
            XmNtopWidget, font_select_item->menu,
 
160
            NULL);
 
161
            
 
162
        XtAddCallback(font_table, XmNdrawCellCallback, (XtCallbackProc) DrawCB, NULL);
 
163
        XtAddCallback(font_table, XmNenterCellCallback, (XtCallbackProc) EnterCB, NULL);
 
164
#ifdef NEW_CODE
 
165
        XtAddEventHandler(font_table, ButtonPressMask, False, 
 
166
                            enlarge_glyph, NULL);
 
167
#endif
 
168
        AddOptionChoiceCB(font_select_item, update_fonttool_cb, font_table);
 
169
 
 
170
        string_item = CreateCSText(fonttool_panel, "CString:");
 
171
        XtVaSetValues(string_item->form,
 
172
            XmNleftAttachment, XmATTACH_FORM,
 
173
            XmNrightAttachment, XmATTACH_FORM,
 
174
            XmNtopAttachment, XmATTACH_WIDGET,
 
175
            XmNtopWidget, font_table,
 
176
            NULL);
 
177
 
 
178
        XtAddCallback(string_item->text,
 
179
            XmNmodifyVerifyCallback, (XtCallbackProc) EditStringCB, font_table);
 
180
        
 
181
#ifdef NEW_CODE
 
182
        scrolled_window = XtVaCreateManagedWidget("scrolled_window",
 
183
            xmScrolledWindowWidgetClass, fonttool_panel,
 
184
            XmNscrollingPolicy, XmAUTOMATIC,
 
185
            XmNvisualPolicy, XmVARIABLE,
 
186
            XmNleftAttachment, XmATTACH_FORM,
 
187
            XmNrightAttachment, XmATTACH_FORM,
 
188
            XmNtopAttachment, XmATTACH_WIDGET,
 
189
            XmNtopWidget, GetParent(string_item),
 
190
            XmNbottomAttachment, XmATTACH_FORM,
 
191
            NULL);
 
192
 
 
193
        glyph_item = XtVaCreateManagedWidget("glyph",
 
194
            xmDrawingAreaWidgetClass, scrolled_window,
 
195
            XmNheight, (Dimension) 100,
 
196
            XmNwidth, (Dimension) 600,
 
197
            XmNresizePolicy, XmRESIZE_ANY,
 
198
            XmNbackground,
 
199
            xvlibcolors[0],
 
200
            NULL);
 
201
#endif
 
202
 
 
203
        aac_buts = CreateAACButtons(fonttool_panel,
 
204
            fonttool_panel, fonttool_aac_cb);
 
205
        XtVaSetValues(aac_buts,
 
206
            XmNleftAttachment, XmATTACH_FORM,
 
207
            XmNrightAttachment, XmATTACH_FORM,
 
208
            XmNtopAttachment, XmATTACH_WIDGET,
 
209
            XmNtopWidget, string_item->form,
 
210
            XmNbottomAttachment, XmATTACH_FORM,
 
211
            NULL);
 
212
        
 
213
        update_fonttool_cb(0, font_table);
 
214
        ManageChild(fonttool_panel);
 
215
    }
 
216
 
 
217
    enable_edit_cb = FALSE;
 
218
    if (cstext_parent == NULL) {
 
219
        SetTextString(string_item, "");
 
220
    } else {
 
221
        SetTextString(string_item, xv_getstr(cstext_parent));
 
222
        /* Lock editable text */
 
223
        SetSensitive(cstext_parent, False);
 
224
    }
 
225
    enable_edit_cb = TRUE;
 
226
    
 
227
    RaiseWindow(fonttool_frame);
 
228
}
 
229
 
 
230
static T1_TMATRIX UNITY_MATRIX = {1.0, 0.0, 0.0, 1.0};
 
231
 
 
232
static void DrawCB(Widget w, XtPointer cd, XbaeMatrixDrawCellCallbackStruct *cbs)
 
233
{
 
234
    unsigned char c;
 
235
    GLYPH *glyph;
 
236
    int height, width, hshift, vshift;
 
237
    Pixmap pixmap, ptmp;
 
238
    char dummy_bits[1] = {0};
 
239
    int valid_char;
 
240
    long bg, fg;
 
241
    
 
242
        
 
243
    c = 16*cbs->row + cbs->column;
 
244
        
 
245
    if (FontID == BAD_FONT_ID) {
 
246
        glyph = NULL;
 
247
    } else {
 
248
        glyph = T1_SetChar(FontID, c, Size, &UNITY_MATRIX);
 
249
    }
 
250
       
 
251
    if (glyph != NULL && glyph->bits != NULL) {
 
252
        valid_char = TRUE;
 
253
        height = glyph->metrics.ascent - glyph->metrics.descent;
 
254
        width = glyph->metrics.rightSideBearing - glyph->metrics.leftSideBearing;
 
255
        hshift = MAX2(glyph->metrics.leftSideBearing - bbox.llx, 0);
 
256
        vshift = MAX2(bbox.ury - glyph->metrics.ascent, 0);
 
257
        XtVaGetValues(w, XmNbackground, &bg, XmNforeground, &fg, NULL);
 
258
        XSetForeground(disp, gc, bg);
 
259
        ptmp = XCreateBitmapFromData(disp, root,
 
260
                    (char *) glyph->bits, width, height);
 
261
        XSetBackground(disp, gc, bg);
 
262
        pixmap = XCreatePixmap(disp, root, bbox.urx - bbox.llx, bbox.ury - bbox.lly, depth);
 
263
        XFillRectangle(disp, pixmap, gc, 0, 0, bbox.urx - bbox.llx, bbox.ury - bbox.lly);
 
264
        XSetForeground(disp, gc, fg);
 
265
        XCopyPlane(disp, ptmp, pixmap, gc, 0, 0, width, height, hshift, vshift, 1);
 
266
        XFreePixmap(disp, ptmp);
 
267
    } else {
 
268
        if (c == ' ') {
 
269
            valid_char = TRUE;
 
270
        } else {
 
271
            valid_char = FALSE;
 
272
        }
 
273
        pixmap = XCreateBitmapFromData(disp, root,
 
274
             dummy_bits, 1, 1);
 
275
    }
 
276
    
 
277
    /* Assign it a pixmap */
 
278
    cbs->pixmap = pixmap;
 
279
    cbs->type = XbaePixmap;
 
280
    XbaeMatrixSetCellUserData(w, cbs->row, cbs->column, (XtPointer) valid_char);  
 
281
   
 
282
    return;
 
283
}
 
284
 
 
285
static void insert_into_string(char *s)
 
286
{
 
287
    int pos;
 
288
    
 
289
    pos = GetTextCursorPos(string_item);
 
290
    TextInsert(string_item, pos, s);
 
291
}
 
292
 
 
293
static void EnterCB(Widget w, XtPointer cd, XbaeMatrixEnterCellCallbackStruct *cbs)
 
294
{
 
295
    int valid_char;
 
296
    char s[7];
 
297
    unsigned char c;
 
298
    
 
299
    valid_char = (int) XbaeMatrixGetCellUserData(w, cbs->row, cbs->column);
 
300
    if (valid_char == TRUE) {
 
301
        c = 16*cbs->row + cbs->column;
 
302
        /* TODO: check for c being displayable in the _X_ font */
 
303
        if (c > 31) {
 
304
            s[0] = (char) c;
 
305
            s[1] = '\0';
 
306
        } else {
 
307
            sprintf(s, "\\#{%02x}", c);
 
308
        }
 
309
        insert_into_string(s);
 
310
    } else {
 
311
        XBell(disp, 25);
 
312
    }
 
313
}
 
314
 
 
315
 
 
316
static void update_fonttool_cb(int value, void *data)
 
317
{
 
318
    char buf[32];
 
319
    Widget font_table = (Widget) data;
 
320
    
 
321
    FontID = value;
 
322
    switch (CheckForFontID(FontID)) {
 
323
    case 0:
 
324
        T1_LoadFont(FontID);
 
325
        break;
 
326
    case -1:
 
327
        errmsg("Couldn't load font");
 
328
        FontID = BAD_FONT_ID;
 
329
        return;
 
330
        break;
 
331
    default:
 
332
        break;
 
333
    }
 
334
 
 
335
    bbox = T1_GetFontBBox(FontID);
 
336
    
 
337
    bbox.llx = bbox.llx*Size/1000;
 
338
    bbox.lly = bbox.lly*Size/1000;
 
339
    bbox.urx = bbox.urx*Size/1000;
 
340
    bbox.ury = bbox.ury*Size/1000;
 
341
    
 
342
    XbaeMatrixRefresh(font_table);
 
343
    sprintf(buf, "\\f{%s}", get_fontalias(FontID));
 
344
    insert_into_string(buf);
 
345
}
 
346
 
 
347
 
 
348
static void EditStringCB(Widget w, XtPointer client_data, XmAnyCallbackStruct *cbs)
 
349
{
 
350
    unsigned char c;
 
351
    int valid_char;
 
352
    static int column = 0, row = 0;
 
353
    XmTextVerifyCallbackStruct *tcbs;
 
354
    XmTextBlock text;
 
355
    Widget ftable = (Widget) client_data;
 
356
    
 
357
    if (enable_edit_cb != TRUE) {
 
358
        return;
 
359
    }
 
360
    
 
361
    XbaeMatrixDeselectCell(ftable, row, column);
 
362
    
 
363
    tcbs = (XmTextVerifyCallbackStruct *) cbs;
 
364
    
 
365
    text = tcbs->text;
 
366
    
 
367
    if (text->length == 1) {
 
368
        /* */
 
369
        c = text->ptr[0];
 
370
        row = c/16;
 
371
        column = c % 16;
 
372
 
 
373
        valid_char = (int) XbaeMatrixGetCellUserData(ftable, row, column);
 
374
        if (valid_char == TRUE) {
 
375
            XbaeMatrixSelectCell(ftable, row, column);
 
376
        } else {
 
377
            tcbs->doit = False;
 
378
        }
 
379
    }
 
380
}
 
381
 
 
382
static void fonttool_aac_cb(void *data)
 
383
{
 
384
    int aac_mode;
 
385
    
 
386
    aac_mode = (int) data;
 
387
    
 
388
    if (aac_mode == AAC_CLOSE) {
 
389
        UnmanageChild(fonttool_frame);
 
390
        if (cstext_parent != NULL) {
 
391
            SetSensitive(cstext_parent, True);
 
392
        }
 
393
        return;
 
394
    }
 
395
 
 
396
    if (cstext_parent != NULL) {
 
397
        xv_setstr(cstext_parent, GetTextString(string_item));
 
398
    }
 
399
    
 
400
    if (aac_mode == AAC_ACCEPT) {
 
401
        UnmanageChild(fonttool_frame);
 
402
        if (cstext_parent != NULL) {
 
403
            SetSensitive(cstext_parent, True);
 
404
        }
 
405
    }
 
406
}