Debian Codegraph @

gdraw/gfiledlg.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 "gdraw.h"
31 #include "ggadget.h"
32 #include "ggadgetP.h"
33 #include "gwidget.h"
34 #include "ustring.h"
35
36 struct gfc_data {
37 int done;
38 unichar_t *ret;
39 GGadget *gfc;
40 };
41
42 static int GFD_Ok(GGadget *g, GEvent *e) {
43 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
44 struct gfc_data *d = GDrawGetUserData(GGadgetGetWindow(g));
45 GGadget *tf;
46 GFileChooserGetChildren(d->gfc,NULL,NULL,&tf);
47 if ( *_GGadgetGetTitle(tf)!='\0' ) {
48 d->done = true;
49 d->ret = GGadgetGetTitle(d->gfc);
50 }
51 }
52 return( true );
53 }
54
55 static int GFD_Cancel(GGadget *g, GEvent *e) {
56 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
57 struct gfc_data *d = GDrawGetUserData(GGadgetGetWindow(g));
58 d->done = true;
59 }
60 return( true );
61 }
62
63 static int e_h(GWindow gw, GEvent *event) {
64 if ( event->type==et_close ) {
65 struct gfc_data *d = GDrawGetUserData(gw);
66 d->done = true;
67 } else if ( event->type == et_map ) {
68 /* Above palettes */
69 GDrawRaise(gw);
70 } else if ( event->type == et_char ) {
71 return( false );
72 } else if ( event->type == et_mousemove ||
73 (event->type==et_mousedown && event->u.mouse.button==3 )) {
74 struct gfc_data *d = GDrawGetUserData(gw);
75 GFileChooserPopupCheck(d->gfc,event);
76 } else if (( event->type==et_mouseup || event->type==et_mousedown ) &&
77 (event->u.mouse.button>=4 && event->u.mouse.button<=7) ) {
78 struct gfc_data *d = GDrawGetUserData(gw);
79 return( GGadgetDispatchEvent((GGadget *) (d->gfc),event));
80 }
81 return( true );
82 }
83
84 static unichar_t *GWidgetOpenFileWPath(const unichar_t *title, const unichar_t *defaultfile,
85 const unichar_t *initial_filter, unichar_t **mimetypes,
86 GFileChooserFilterType filter, const char* const* path) {
87 GRect pos;
88 GWindow gw;
89 GWindowAttrs wattrs;
90 GGadgetCreateData gcd[7], boxes[3], *varray[5], *harray[8];
91 GTextInfo label[4];
92 struct gfc_data d;
93 int bs = GIntGetResource(_NUM_Buttonsize), bsbigger, totwid;
94
95 GProgressPauseTimer();
96 memset(&wattrs,0,sizeof(wattrs));
97 wattrs.mask = wam_events|wam_cursor|wam_wtitle|wam_undercursor|wam_restrict|wam_isdlg;
98 wattrs.event_masks = ~(1<<et_charup);
99 wattrs.restrict_input_to_me = 1;
100 wattrs.undercursor = 1;
101 wattrs.is_dlg = 1;
102 wattrs.cursor = ct_pointer;
103 wattrs.window_title = (unichar_t *) title;
104 pos.x = pos.y = 0;
105 totwid = GGadgetScale(223);
106 bsbigger = 3*bs+4*14>totwid; totwid = bsbigger?3*bs+4*12:totwid;
107 pos.width = GDrawPointsToPixels(NULL,totwid);
108 pos.height = GDrawPointsToPixels(NULL,223);
109 gw = GDrawCreateTopWindow(NULL,&pos,e_h,&d,&wattrs);
110
111 memset(&label,0,sizeof(label));
112 memset(&gcd,0,sizeof(gcd));
113 memset(&boxes,0,sizeof(boxes));
114 gcd[0].gd.pos.x = 12; gcd[0].gd.pos.y = 6;
115 gcd[0].gd.pos.width = 223; gcd[0].gd.pos.height = 180;
116 gcd[0].gd.flags = gg_visible | gg_enabled;
117 gcd[0].creator = GFileChooserCreate;
118 varray[0] = &gcd[0]; varray[1] = NULL;
119
120 gcd[1].gd.pos.x = 12; gcd[1].gd.pos.y = 192-3;
121 gcd[1].gd.pos.width = -1;
122 gcd[1].gd.flags = gg_visible | gg_enabled | gg_but_default;
123 label[1].text = (unichar_t *) _("_OK");
124 label[1].text_is_1byte = true;
125 label[1].text_in_resource = true;
126 gcd[1].gd.mnemonic = 'O';
127 gcd[1].gd.label = &label[1];
128 gcd[1].gd.handle_controlevent = GFD_Ok;
129 gcd[1].creator = GButtonCreate;
130 harray[0] = GCD_Glue; harray[1] = &gcd[1];
131
132 gcd[2].gd.pos.x = (totwid-bs)*100/GIntGetResource(_NUM_ScaleFactor)/2; gcd[2].gd.pos.y = gcd[1].gd.pos.y+3;
133 gcd[2].gd.pos.width = -1;
134 gcd[2].gd.flags = gg_visible | gg_enabled;
135 label[2].text = (unichar_t *) _("_Filter");
136 label[2].text_is_1byte = true;
137 label[2].text_in_resource = true;
138 gcd[2].gd.mnemonic = 'F';
139 gcd[2].gd.label = &label[2];
140 gcd[2].gd.handle_controlevent = GFileChooserFilterEh;
141 gcd[2].creator = GButtonCreate;
142 harray[2] = GCD_Glue; harray[3] = &gcd[2];
143
144 gcd[3].gd.pos.x = -gcd[1].gd.pos.x; gcd[3].gd.pos.y = gcd[2].gd.pos.y;
145 gcd[3].gd.pos.width = -1;
146 gcd[3].gd.flags = gg_visible | gg_enabled | gg_but_cancel;
147 label[3].text = (unichar_t *) _("_Cancel");
148 label[3].text_is_1byte = true;
149 label[3].text_in_resource = true;
150 gcd[3].gd.label = &label[3];
151 gcd[3].gd.mnemonic = 'C';
152 gcd[3].gd.handle_controlevent = GFD_Cancel;
153 gcd[3].creator = GButtonCreate;
154 harray[4] = GCD_Glue; harray[5] = &gcd[3]; harray[6] = GCD_Glue; harray[7] = NULL;
155
156 boxes[2].gd.flags = gg_visible | gg_enabled;
157 boxes[2].gd.u.boxelements = harray;
158 boxes[2].creator = GHBoxCreate;
159 varray[2] = &boxes[2]; varray[3] = NULL;
160 varray[4] = NULL;
161
162 boxes[0].gd.pos.x = boxes[0].gd.pos.y = 2;
163 boxes[0].gd.flags = gg_visible | gg_enabled;
164 boxes[0].gd.u.boxelements = varray;
165 boxes[0].creator = GHVGroupCreate;
166
167 gcd[4].gd.pos.x = 2; gcd[4].gd.pos.y = 2;
168 gcd[4].gd.pos.width = pos.width-4; gcd[4].gd.pos.height = pos.height-4;
169 gcd[4].gd.flags = gg_enabled | gg_visible | gg_pos_in_pixels;
170 gcd[4].creator = GGroupCreate;
171
172 GGadgetsCreate(gw,boxes);
173 GGadgetSetUserData(gcd[2].ret,gcd[0].ret);
174 GHVBoxSetExpandableRow(boxes[0].ret,0);
175 GHVBoxSetExpandableCol(boxes[2].ret,gb_expandgluesame);
176 GHVBoxFitWindow(boxes[0].ret);
177
178 GFileChooserConnectButtons(gcd[0].ret,gcd[1].ret,gcd[2].ret);
179 GFileChooserSetFilterText(gcd[0].ret,initial_filter);
180 GFileChooserSetFilterFunc(gcd[0].ret,filter);
181 GFileChooserSetMimetypes(gcd[0].ret,mimetypes);
182 GFileChooserSetPaths(gcd[0].ret,path);
183 GGadgetSetTitle(gcd[0].ret,defaultfile);
184
185 memset(&d,'\0',sizeof(d));
186 d.gfc = gcd[0].ret;
187
188 GDrawSetVisible(gw,true);
189 while ( !d.done )
190 GDrawProcessOneEvent(NULL);
191 GDrawDestroyWindow(gw);
192 GDrawProcessPendingEvents(NULL); /* Give the window a chance to vanish... */
193 GDrawSync(NULL);
194 GDrawProcessPendingEvents(NULL); /* Give the window a chance to vanish... */
195 GProgressResumeTimer();
196 return(d.ret);
197 }
198
199 char *GWidgetOpenFileWPath8(const char *title, const char *defaultfile,
200 const char *initial_filter, char **mimetypes,
201 GFileChooserFilterType filter, const char* const* path) {
202 unichar_t *tit=NULL, *def=NULL, *filt=NULL, **mimes=NULL, *ret;
203 char *utf8_ret;
204 int i;
205
206 if ( title!=NULL )
207 tit = utf82u_copy(title);
208 if ( defaultfile!=NULL )
209 def = utf82u_copy(defaultfile);
210 else if ( path!=NULL && path[0]!=NULL )
211 def = utf82u_copy(path[0]);
212 if ( initial_filter!=NULL )
213 filt = utf82u_copy(initial_filter);
214 if ( mimetypes!=NULL ) {
215 for ( i=0; mimetypes[i]!=NULL; ++i );
216 mimes = malloc((i+1)*sizeof(unichar_t *));
217 for ( i=0; mimetypes[i]!=NULL; ++i )
218 mimes[i] = utf82u_copy(mimetypes[i]);
219 mimes[i] = NULL;
220 }
221 ret = GWidgetOpenFileWPath(tit,def,filt,mimes,filter,path);
222 if ( mimes!=NULL ) {
223 for ( i=0; mimes[i]!=NULL; ++i )
224 free(mimes[i]);
225 free(mimes);
226 }
227 free(filt); free(def); free(tit);
228 utf8_ret = u2utf8_copy(ret);
229 free(ret);
230 return( utf8_ret );
231 }
232
233 char *GWidgetOpenFile8(const char *title, const char *defaultfile,
234 const char *initial_filter, char **mimetypes,
235 GFileChooserFilterType filter) {
236 return( GWidgetOpenFileWPath8(title,defaultfile,initial_filter,mimetypes,filter,NULL));
237 }