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