1 /*
2  * Author:      William Chia-Wei Cheng (bill.cheng@acm.org)
3  *
4  * Copyright (C) 2001-2009, William Chia-Wei Cheng.
5  *
6  * This file may be distributed under the terms of the Q Public License
7  * as defined by Trolltech AS of Norway and appearing in the file
8  * LICENSE.QPL included in the packaging of this file.
9  *
10  * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING
11  * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
13  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
14  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
16  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * @(#)$Header: /mm2/home/cvs/bc-src/tgif/tginssym.c,v 1.5 2011/06/02 15:53:08 william Exp $
19  */
20 
21 #define _INCLUDE_FROM_TGINSSYM__C_
22 
23 #include "tgifdefs.h"
24 #include "tidget.h"
25 
26 #include "auxtext.e"
27 #include "dialog.e"
28 #include "font.e"
29 #include "msg.e"
30 #include "setup.e"
31 #include "strtbl.e"
32 #include "tginssym.e"
33 #include "tgisdl.e"
34 #include "tidget.e"
35 #include "util.e"
36 
37 /* --------------------- Init & Clean Up --------------------- */
38 
TgInsertSymbol_CleanUp(dpy,win)39 void TgInsertSymbol_CleanUp(dpy, win)
40    Display *dpy;
41    Window win;
42 {
43    if (gInsertSymbolInfo.initialized) {
44       if (gInsertSymbolInfo.dialogbox_tidgetinfo != NULL) {
45          DestroyTdgtInsertSymbolDialogBox(dpy,
46                gInsertSymbolInfo.dialogbox_tidgetinfo);
47       }
48    }
49    if (gInsertSymbolInfo.sym_pixmap != None) {
50       XFreePixmap(mainDisplay, gInsertSymbolInfo.sym_pixmap);
51    }
52    if (gInsertSymbolInfo.xfs != NULL) {
53       XFreeFont(mainDisplay, gInsertSymbolInfo.xfs);
54    }
55    if (gInsertSymbolInfo.gc != NULL) {
56       XFreeGC(mainDisplay, gInsertSymbolInfo.gc);
57    }
58    memset(&gInsertSymbolInfo, 0, sizeof(InsertSymbolInfo));
59 
60    CleanUpTdgtInsertSymbolDlg();
61 }
62 
TgInsertSymbol_Init(dpy,win,arg)63 int TgInsertSymbol_Init(dpy, win, arg)
64    Display *dpy;
65    Window win;
66    char *arg;
67 {
68    XGCValues values;
69 
70    memset(&gInsertSymbolInfo, 0, sizeof(InsertSymbolInfo));
71 
72    gInsertSymbolInfo.xfs = LoadASymbolFont(14);
73    if (gInsertSymbolInfo.xfs == NULL) {
74       TgInsertSymbol_CleanUp(dpy, win);
75       return FALSE;
76    }
77    gInsertSymbolInfo.font_width = gInsertSymbolInfo.xfs->max_bounds.width;
78    gInsertSymbolInfo.font_asc = gInsertSymbolInfo.xfs->max_bounds.ascent;
79    gInsertSymbolInfo.font_des = gInsertSymbolInfo.xfs->max_bounds.descent;
80    gInsertSymbolInfo.font_height = gInsertSymbolInfo.font_asc +
81          gInsertSymbolInfo.font_des;
82    gInsertSymbolInfo.padding = 2;
83 
84    values.foreground = myFgPixel;
85    values.background = myBgPixel;
86    values.fill_style = FillSolid;
87    values.font = gInsertSymbolInfo.xfs->fid;
88 
89    gInsertSymbolInfo.gc = XCreateGC(mainDisplay, mainWindow,
90          GCForeground | GCBackground | GCFillStyle | GCFont, &values);
91 
92    if (gInsertSymbolInfo.gc == NULL) {
93       TgInsertSymbol_CleanUp(dpy, win);
94       return FALSE;
95    }
96    gInsertSymbolInfo.a_symbol_w = gInsertSymbolInfo.font_width +
97          (gInsertSymbolInfo.padding<<1);
98    gInsertSymbolInfo.a_symbol_h = gInsertSymbolInfo.font_height +
99          (gInsertSymbolInfo.padding<<1);
100    gInsertSymbolInfo.pixmap_w = TGIS_COLS*(gInsertSymbolInfo.a_symbol_w+1) + 1;
101    gInsertSymbolInfo.pixmap_h = TGIS_ROWS*(gInsertSymbolInfo.a_symbol_h+1) + 1;
102 
103    gInsertSymbolInfo.sym_pixmap = XCreatePixmap(mainDisplay, rootWindow,
104          gInsertSymbolInfo.pixmap_w, gInsertSymbolInfo.pixmap_h, mainDepth);
105    if (gInsertSymbolInfo.sym_pixmap == None) {
106       FailAllocPixmapMessage(gInsertSymbolInfo.pixmap_w,
107             gInsertSymbolInfo.pixmap_h);
108       TgInsertSymbol_CleanUp(dpy, win);
109       return FALSE;
110    }
111    if (!InitTdgtInsertSymbolDlg()) {
112       TgInsertSymbol_CleanUp(dpy, win);
113 
114       return FALSE;
115    }
116    return TRUE;
117 }
118 
CleanUpInsertSymbol()119 void CleanUpInsertSymbol()
120 {
121    TgInsertSymbol_CleanUp(mainDisplay, drawWindow);
122 }
123 
InitInsertSymbol()124 int InitInsertSymbol()
125 {
126    return TgInsertSymbol_Init(mainDisplay, drawWindow, NULL);
127 }
128 
129 /* --------------------- TgInsertSymbol_SendKey --------------------- */
130 
131 static XComposeStatus c_stat;
132 
TgInsertSymbol_SendKey(dpy,win,key_ev,buf)133 int TgInsertSymbol_SendKey(dpy, win, key_ev, buf)
134    Display *dpy;
135    Window win;
136    XKeyEvent *key_ev;
137    char *buf;
138    /* return FALSE means key_ev will be handled without modifications */
139 {
140    int has_ch=0;
141    char s[80];
142    KeySym key_sym=(KeySym)0;
143 
144    has_ch = XLookupString(key_ev, s, sizeof(s), &key_sym, &c_stat);
145    TranslateKeys(s, &key_sym);
146 
147    return FALSE;
148 }
149 
150 /* --------------------- TgInsertSymbol_HandleCntrlSpace --------------------- */
151 
TgInsertSymbol_HandleCntrlSpace(dpy,win)152 int TgInsertSymbol_HandleCntrlSpace(dpy, win)
153    Display *dpy;
154    Window win;
155 {
156    if (!gInsertSymbolInfo.initialized) {
157       gInsertSymbolInfo.initialized = TRUE;
158       /* create window */
159       gInsertSymbolInfo.dialogbox_tidgetinfo =
160             CreateTdgtInsertSymbolDialogBox(dpy, rootWindow);
161       if (gInsertSymbolInfo.dialogbox_tidgetinfo == NULL) {
162          /* print a message */
163       }
164    }
165    if (gInsertSymbolInfo.dialogbox_tidgetinfo == NULL) {
166       return FALSE;
167    }
168    if (!gInsertSymbolInfo.mapped) {
169       ShowTdgtInsertSymbolDialogBox();
170       TdgtInsertSymbolDlgLoop(gInsertSymbolInfo.dialogbox_tidgetinfo);
171    } else {
172       HideTdgtInsertSymbolDialogBox();
173    }
174    return FALSE;
175 }
176 
177 /* --------------------- TgInsertSymbol_HandleCreateText --------------------- */
178 
TgInsertSymbol_HandleCreateText(dpy,win)179 int TgInsertSymbol_HandleCreateText(dpy, win)
180    Display *dpy;
181    Window win;
182 {
183    if (gInsertSymbolInfo.initialized && gInsertSymbolInfo.mapped) {
184       HideTdgtInsertSymbolDialogBox();
185    }
186    return TRUE;
187 }
188 
189