Debian Codegraph @

fontforgeexe/windowmenu.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "basics.h"
31 #include "fontforgeui.h"
32 #include "gfile.h"
33 #include "scripting.h"
34 #include "splinefont.h"
35 #include "ustring.h"
36
37 static void WindowSelect(GWindow base,struct gmenuitem *mi,GEvent *e) {
38 GDrawRaise(mi->ti.userdata);
39 }
40
41 static void AddMI(GMenuItem *mi,GWindow gw,int changed, int top) {
42 char* title = GDrawGetWindowTitle8(gw);
43 mi->ti.userdata = gw;
44 mi->ti.bg = GDrawGetDefaultBackground(GDrawGetDisplayOfWindow(gw));
45 mi->invoke = WindowSelect;
46 mi->ti.text = utf82u_copy(title);
47 if(mi->ti.text == NULL)
48 mi->ti.text = utf82u_copy("(null)");
49 if ( u_strlen( mi->ti.text ) > 35 )
50 mi->ti.text[35] = '\0';
51 free(title);
52 }
53
54 /* Builds up a menu containing the titles of all the major windows */
55 void WindowMenuBuild(GWindow basew,struct gmenuitem *mi,GEvent *e) {
56 int i, cnt, precnt;
57 FontViewBase *fv;
58 CharViewBase *cv;
59 MetricsView *mv;
60 BitmapView *bv;
61 GMenuItem *sub;
62 BDFFont *bdf;
63
64 precnt = 6;
65 cnt = precnt;
66
67 for ( fv = (FontViewBase *) fv_list; fv!=NULL; fv = fv->next ) {
68 ++cnt; /* for the font */
69 for ( i=0; i<fv->sf->glyphcnt; ++i ) if ( fv->sf->glyphs[i]!=NULL ) {
70 for ( cv = fv->sf->glyphs[i]->views; cv!=NULL; cv=cv->next )
71 ++cnt; /* for each char view in the font */
72 }
73 for ( bdf= fv->sf->bitmaps; bdf!=NULL; bdf = bdf->next ) {
74 for ( i=0; i<bdf->glyphcnt; ++i ) if ( bdf->glyphs[i]!=NULL ) {
75 for ( bv = bdf->glyphs[i]->views; bv!=NULL; bv=bv->next )
76 ++cnt;
77 }
78 }
79 for ( mv=fv->sf->metrics; mv!=NULL; mv=mv->next )
80 ++cnt;
81 }
82 if ( cnt==0 ) {
83 /* This can't happen */
84 return;
85 }
86 sub = calloc(cnt+1,sizeof(GMenuItem));
87 memcpy(sub,mi->sub,precnt*sizeof(struct gmenuitem));
88 for ( i=0; i<precnt; ++i )
89 mi->sub[i].ti.text = NULL;
90 GMenuItemArrayFree(mi->sub);
91 mi->sub = sub;
92
93 for ( i=0; sub[i].ti.text!=NULL || sub[i].ti.line; ++i ) {
94 if ( sub[i].ti.text_is_1byte ) {
95 sub[i].ti.text = utf82u_mncopy((char *) sub[i].ti.text,&sub[i].ti.mnemonic);
96 } else
97 sub[i].ti.text = u_copy(sub[i].ti.text);
98 sub[i].ti.text_is_1byte = sub[i].ti.text_in_resource = false;
99 }
100 cnt = precnt;
101 for ( fv = (FontViewBase *) fv_list; fv!=NULL; fv = fv->next ) {
102 if( !((FontView *) fv)->gw ) {
103 continue;
104 }
105
106 AddMI(&sub[cnt++],((FontView *) fv)->gw,fv->sf->changed,true);
107 for ( i=0; i<fv->sf->glyphcnt; ++i ) if ( fv->sf->glyphs[i]!=NULL ) {
108 for ( cv = fv->sf->glyphs[i]->views; cv!=NULL; cv=cv->next )
109 AddMI(&sub[cnt++],((CharView *) cv)->gw,cv->sc->changed,false);
110 }
111 for ( bdf= fv->sf->bitmaps; bdf!=NULL; bdf = bdf->next ) {
112 for ( i=0; i<bdf->glyphcnt; ++i ) if ( bdf->glyphs[i]!=NULL ) {
113 for ( bv = bdf->glyphs[i]->views; bv!=NULL; bv=bv->next )
114 AddMI(&sub[cnt++],bv->gw,bv->bc->changed,false);
115 }
116 }
117 for ( mv=fv->sf->metrics; mv!=NULL; mv=mv->next )
118 AddMI(&sub[cnt++],mv->gw,false,false);
119 }
120 }
121
122 static void RecentSelect(GWindow base,struct gmenuitem *mi,GEvent *e) {
123 ViewPostScriptFont((char *) (mi->ti.userdata),0);
124 }
125
126 /* Builds up a menu containing the titles of all the unused recent files */
127 void MenuRecentBuild(GWindow base,struct gmenuitem *mi,GEvent *e) {
128 int i, cnt, cnt1;
129 FontViewBase *fv;
130 GMenuItem *sub;
131
132 if ( mi->sub!=NULL ) {
133 GMenuItemArrayFree(mi->sub);
134 mi->sub = NULL;
135 }
136
137 cnt = 0;
138 for ( i=0; i<RECENT_MAX && RecentFiles[i]!=NULL; ++i ) {
139 for ( fv=(FontViewBase *) fv_list; fv!=NULL; fv=fv->next )
140 if ( fv->sf->filename!=NULL && strcmp(fv->sf->filename,RecentFiles[i])==0 )
141 break;
142 if ( fv==NULL )
143 ++cnt;
144 }
145 if ( cnt==0 ) {
146 /* This can't happen */
147 return;
148 }
149 sub = calloc(cnt+1,sizeof(GMenuItem));
150 cnt1 = 0;
151 for ( i=0; i<RECENT_MAX && RecentFiles[i]!=NULL; ++i ) {
152 for ( fv=(FontViewBase *) fv_list; fv!=NULL; fv=fv->next )
153 if ( fv->sf->filename!=NULL && strcmp(fv->sf->filename,RecentFiles[i])==0 )
154 break;
155 if ( fv==NULL ) {
156 GMenuItem *mi = &sub[cnt1++];
157 mi->ti.userdata = RecentFiles[i];
158 mi->ti.bg = mi->ti.fg = COLOR_DEFAULT;
159 mi->invoke = RecentSelect;
160 mi->ti.text = def2u_copy(GFileNameTail(RecentFiles[i]));
161 }
162 }
163 if ( cnt!=cnt1 )
164 IError( "Bad counts in MenuRecentBuild");
165 mi->sub = sub;
166 }
167
168 int RecentFilesAny(void) {
169 int i;
170 FontViewBase *fvl;
171
172 for ( i=0; i<RECENT_MAX && RecentFiles[i]!=NULL; ++i ) {
173 for ( fvl=(FontViewBase *) fv_list; fvl!=NULL; fvl=fvl->next )
174 if ( fvl->sf->filename!=NULL && strcmp(fvl->sf->filename,RecentFiles[i])==0 )
175 break;
176 if ( fvl==NULL )
177 return( true );
178 }
179 return( false );
180 }
181
182 #if !defined(_NO_FFSCRIPT) || !defined(_NO_PYTHON)
183 static void ScriptSelect(GWindow base,struct gmenuitem *mi,GEvent *e) {
184 int index = (intptr_t) (mi->ti.userdata);
185 FontView *fv = (FontView *) GDrawGetUserData(base);
186
187 /* the menu is not always up to date. If user changed prefs and then used */
188 /* Alt|Ctl|Digit s/he would not get a new menu built and the old one might*/
189 /* refer to something out of bounds. Hence the check */
190 if ( index<0 || script_filenames[index]==NULL )
191 return;
192 ExecuteScriptFile((FontViewBase *) fv,NULL,script_filenames[index]);
193 }
194
195 /* Builds up a menu containing any user defined scripts */
196 void MenuScriptsBuild(GWindow base,struct gmenuitem *mi,GEvent *e) {
197 int i;
198 GMenuItem *sub;
199
200 if ( mi->sub!=NULL ) {
201 GMenuItemArrayFree(mi->sub);
202 mi->sub = NULL;
203 }
204
205 for ( i=0; i<SCRIPT_MENU_MAX && script_menu_names[i]!=NULL; ++i );
206 if ( i==0 ) {
207 /* This can't happen */
208 return;
209 }
210 sub = calloc(i+1,sizeof(GMenuItem));
211 for ( i=0; i<SCRIPT_MENU_MAX && script_menu_names[i]!=NULL; ++i ) {
212 GMenuItem *mi = &sub[i];
213 mi->ti.userdata = (void *) (intptr_t) i;
214 mi->ti.bg = mi->ti.fg = COLOR_DEFAULT;
215 mi->invoke = ScriptSelect;
216 mi->shortcut = i==9?'0':'1'+i;
217 mi->short_mask = ksm_control|ksm_meta;
218 mi->ti.text = u_copy(script_menu_names[i]);
219 }
220 mi->sub = sub;
221 }
222 #endif
223
224 /* Builds up a menu containing all the anchor classes */
225 void _aplistbuild(struct gmenuitem *top,SplineFont *sf,
226 void (*func)(GWindow,struct gmenuitem *,GEvent *)) {
227 int cnt;
228 GMenuItem *mi, *sub;
229 AnchorClass *ac;
230
231 if ( top->sub!=NULL ) {
232 GMenuItemArrayFree(top->sub);
233 top->sub = NULL;
234 }
235
236 cnt = 0;
237 for ( ac = sf->anchor; ac!=NULL; ac=ac->next ) ++cnt;
238 if ( cnt==0 )
239 cnt = 1;
240 else
241 cnt += 2;
242 sub = calloc(cnt+1,sizeof(GMenuItem));
243 mi = &sub[0];
244 mi->ti.userdata = (void *) (-1);
245 mi->ti.bg = mi->ti.fg = COLOR_DEFAULT;
246 mi->invoke = func;
247 mi->ti.text = utf82u_copy(_("All"));
248 if ( cnt==1 )
249 mi->ti.disabled = true;
250 else {
251 ++mi;
252 mi->ti.bg = mi->ti.fg = COLOR_DEFAULT;
253 mi->ti.line = true;
254 ++mi;
255 }
256 for ( ac=sf->anchor; ac!=NULL; ac = ac->next, ++mi ) {
257 mi->ti.userdata = (void *) ac;
258 mi->ti.bg = mi->ti.fg = COLOR_DEFAULT;
259 mi->invoke = func;
260 mi->ti.text = utf82u_copy(ac->name);
261 }
262 top->sub = sub;
263 }
264
265 void mbFreeGetText(GMenuItem *mb) {
266 /* free gettext substitutions on this menu and all sub menus */
267 int i;
268
269 if ( mb==NULL )
270 return;
271 for ( i=0; mb[i].ti.text!=NULL || mb[i].ti.line || mb[i].ti.image!=NULL; ++i ) {
272 if (mb[i].ti.text_untranslated != NULL) { free(mb[i].ti.text_untranslated); mb[i].ti.text_untranslated = NULL; }
273 if ( mb[i].ti.text!=NULL ) {
274 if ( mb[i].sub!=NULL )
275 mbFreeGetText(mb[i].sub);
276 }
277 }
278 }
279
280 void mbDoGetText(GMenuItem *mb) {
281 /* perform gettext substitutions on this menu and all sub menus */
282 int i;
283
284 if ( mb==NULL )
285 return;
286 for ( i=0; mb[i].ti.text!=NULL || mb[i].ti.line || mb[i].ti.image!=NULL; ++i ) {
287 if( mb[i].shortcut ) {
288 unichar_t tmp[2];
289 tmp[0] = mb[i].shortcut;
290 tmp[1] = (unichar_t)(0);
291 mb[i].ti.text_untranslated = cu_copy(tmp);
292 } else
293 mb[i].ti.text_untranslated = copy((char*)mb[i].ti.text);
294 if ( mb[i].ti.text!=NULL ) {
295 mb[i].ti.text = (unichar_t *) S_((char *) mb[i].ti.text);
296 if ( mb[i].sub!=NULL )
297 mbDoGetText(mb[i].sub);
298 }
299 }
300 }
301
302 void mb2FreeGetText(GMenuItem2 *mb) {
303 /* free gettext substitutions on this menu and all sub menus */
304 int i;
305
306 if ( mb==NULL )
307 return;
308 for ( i=0; mb[i].ti.text!=NULL || mb[i].ti.line || mb[i].ti.image!=NULL; ++i ) {
309 if (mb[i].ti.text_untranslated != NULL) { free(mb[i].ti.text_untranslated); mb[i].ti.text_untranslated = NULL; }
310 if ( mb[i].ti.text!=NULL ) {
311 if ( mb[i].sub!=NULL )
312 mb2FreeGetText(mb[i].sub);
313 }
314 }
315 }
316
317 void mb2DoGetText(GMenuItem2 *mb) {
318 /* perform gettext substitutions on this menu and all sub menus */
319 int i;
320
321 if ( mb==NULL )
322 return;
323 for ( i=0; mb[i].ti.text!=NULL || mb[i].ti.line || mb[i].ti.image!=NULL; ++i ) {
324 if( mb[i].shortcut )
325 mb[i].ti.text_untranslated = copy(mb[i].shortcut);
326 else
327 mb[i].ti.text_untranslated = copy((char*)mb[i].ti.text);
328 if ( mb[i].ti.text!=NULL ) {
329 mb[i].ti.text = (unichar_t *) S_((char *) mb[i].ti.text);
330 if ( mb[i].sub!=NULL )
331 mb2DoGetText(mb[i].sub);
332 }
333 }
334 }