/* Copyright (C) 2000-2012 by George Williams */ /* * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include "gdraw.h" #include "ggadget.h" #include "ggadgetP.h" #include "gwidget.h" #include "ustring.h" #include extern void GTextFieldInit(); static GWindow last; static const char *last_title; struct dlg_info { int done; int ret; int multi; int exposed; int size_diff; int bcnt; }; static int d_e_h(GWindow gw, GEvent *event) { struct dlg_info *d = GDrawGetUserData(gw); if ( event->type==et_close ) { d->done = true; /* d->ret is initialized to cancel */ } else if ( event->type==et_controlevent && event->u.control.subtype == et_buttonactivate ) { d->done = true; d->ret = GGadgetGetCid(event->u.control.g); } else if ( event->type == et_expose ) { d->exposed = true; } else if ( event->type == et_char ) { return( false ); } else if ( event->type == et_resize && !d->exposed ) { GRect pos,rootsize; GDrawGetSize(gw,&pos); GDrawGetSize(GDrawGetRoot(NULL),&rootsize); if ( pos.x+pos.width>=rootsize.width || pos.y+pos.height>=rootsize.height ) { if ( pos.x+pos.width>=rootsize.width ) if (( pos.x = rootsize.width - pos.width )<0 ) pos.x = 0; if ( pos.y+pos.height>=rootsize.height ) if (( pos.y = rootsize.height - pos.height )<0 ) pos.y = 0; GDrawMove(gw,pos.x,pos.y); } } else if ( event->type == et_map ) { /* Above palettes */ GDrawRaise(gw); } return( true ); } static int w_e_h(GWindow gw, GEvent *event) { if ( event->type==et_close || (event->type==et_controlevent && event->u.control.subtype == et_buttonactivate ) || event->type==et_timer ) GDrawDestroyWindow(gw); else if ( event->type == et_map ) { /* Above palettes */ GDrawRaise(gw); } else if ( event->type == et_char ) { return( false ); } else if ( event->type == et_destroy ) { if ( last==gw ) { last = NULL; last_title = NULL; } } return( true ); } #define GLINE_MAX 20 static int FindLineBreaks(const unichar_t *question, GTextInfo linebreaks[GLINE_MAX+1]) { int lb, i; const unichar_t *pt, *last; /* Find good places to break the question into bits */ last = pt = question; linebreaks[0].text = (unichar_t *) question; lb=0; while ( *pt!='\0' && lbtype==et_controlevent && e->u.control.subtype == et_buttonactivate ) { GWindow gw = GGadgetGetWindow(g); GGadget *list = GWidgetGetControl(gw,CID_List); GGadgetSelectListItem(list,-1,GGadgetGetCid(g)==CID_SelectAll?1:0 ); } return( true ); } static int c_e_h(GWindow gw, GEvent *event) { struct dlg_info *d = GDrawGetUserData(gw); if ( event->type==et_close ) { d->done = true; d->ret = -1; } else if ( event->type==et_resize ) { GDrawRequestExpose(gw,NULL,false); } else if ( event->type==et_controlevent && (event->u.control.subtype == et_buttonactivate || event->u.control.subtype == et_listdoubleclick )) { d->done = true; if ( GGadgetGetCid(event->u.control.g)==CID_Cancel ) d->ret = -1; else d->ret = GGadgetGetFirstListSelectedItem(GWidgetGetControl(gw,CID_List)); } else if ( event->type == et_char ) { return( false ); } else if ( event->type == et_map ) { /* Above palettes */ GDrawRaise(gw); } return( true ); } /* ************** Parallel routines using utf8 arguments ******************** */ static GWindow DlgCreate8(const char *title,const char *question,va_list ap, const char **answers, int def, int cancel, struct dlg_info *d, int add_text, const char *defstr, int restrict_input, int center) { GTextInfo qlabels[GLINE_MAX+1], *blabels; GGadgetCreateData *gcd, *array[2*(GLINE_MAX+5)+1], boxes[5], **barray, *labarray[4]; int lb, bcnt=0, l; GRect pos; GWindow gw; GWindowAttrs wattrs; int as, ds, ld, fh; int w, maxw, bw, bspace; int i, y; char *buf; unichar_t *ubuf; extern GBox _GGadget_defaultbutton_box; if ( d!=NULL ) memset(d,0,sizeof(*d)); buf = vsmprintf(question, ap); if ( screen_display==NULL ) { fprintf(stderr, "%s\n", buf); if ( d!=NULL ) d->done = true; free(buf); return( NULL ); } ubuf = utf82u_copy(buf); free(buf); if( !ubuf ) { fprintf( stderr, "Failed to convert question string in DlgCreate8()\n" ); return( NULL ); } GGadgetInit(); GProgressPauseTimer(); memset(qlabels,'\0',sizeof(qlabels)); lb = FindLineBreaks(ubuf,qlabels); for ( bcnt=0; answers[bcnt]!=NULL; ++bcnt); blabels = calloc(bcnt+1,sizeof(GTextInfo)); barray = calloc(2*bcnt+3,sizeof(GGadgetCreateData *)); for ( bcnt=0; answers[bcnt]!=NULL; ++bcnt) { blabels[bcnt].text = (unichar_t *) answers[bcnt]; blabels[bcnt].text_is_1byte = true; blabels[bcnt].text_in_resource = true; /* Look for mnemonics in the utf8 string (preceded by _) */ } memset(&wattrs,0,sizeof(wattrs)); /* If we have many questions in quick succession the dlg will jump around*/ /* as it tracks the cursor (which moves to the buttons). That's not good*/ /* So I don't do undercursor here */ wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_isdlg; if ( restrict_input ) wattrs.mask |= wam_restrict; else wattrs.mask |= wam_notrestricted; if ( center ) wattrs.mask |= wam_centered; else wattrs.mask |= wam_undercursor; wattrs.not_restricted = true; wattrs.restrict_input_to_me = 1; wattrs.is_dlg = true; wattrs.event_masks = ~(1<maxw ) maxw = w; } if ( add_text && defstr!=NULL ) { extern GResFont _gtextfield_font; GTextFieldInit(); if ( _gtextfield_font.fi!=NULL ) { GDrawSetFont(gw,_gtextfield_font.fi); w = GDrawGetText8Width(gw,defstr,-1); GDrawSetDefaultFont(gw); } else w = 8*GDrawGetText8Width(gw,defstr,-1)/5; w += GDrawPointsToPixels(gw,40); if ( w >1000 ) w = 1000; if ( w>maxw ) maxw = w; } bw = 0; for ( i=0; ibw ) bw = w; } bw += GDrawPointsToPixels(gw,20); bspace = GDrawPointsToPixels(gw,6); if ( (bw+bspace) * bcnt > maxw ) maxw = (bw+bspace)*bcnt; if ( bcnt!=1 ) bspace = (maxw-bcnt*bw)/(bcnt-1); maxw += GDrawPointsToPixels(gw,16); gcd = calloc(lb+bcnt+2,sizeof(GGadgetCreateData)); memset(boxes,0,sizeof(boxes)); l = 0; if ( lb==1 ) { gcd[0].gd.pos.width = GDrawGetTextWidth(gw,qlabels[0].text,-1); gcd[0].gd.pos.x = (maxw-gcd[0].gd.pos.width)/2; gcd[0].gd.pos.y = GDrawPointsToPixels(gw,6); gcd[0].gd.pos.height = fh; gcd[0].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels | gg_pos_use0; gcd[0].gd.label = &qlabels[0]; gcd[0].creator = GLabelCreate; labarray[0] = GCD_Glue; labarray[1] = &gcd[0]; labarray[2] = GCD_Glue; labarray[3] = NULL; boxes[2].gd.flags = gg_visible|gg_enabled; boxes[2].gd.u.boxelements = labarray; boxes[2].creator = GHBoxCreate; array[0] = &boxes[2]; array[1] = NULL; l = 1; } else for ( i=0; iret = cancel; d->bcnt = bcnt; } GDrawSetVisible(gw,true); free(blabels); free(gcd); free(barray); for ( i=0; i0 ) GDrawRequestTimer(gw,timeout*1000,0,NULL); /* Continue merrily on our way. Window will destroy itself in 40 secs */ /* or when user kills it. We can ignore it */ last = gw; last_title = title; } void GWidgetPostNotice8(const char *title,const char *statement,...) { va_list ap; /* Force an old notice to disappear */ if ( title==NULL ) { if ( last!=NULL ) GDrawDestroyWindow(last); return; } va_start(ap,statement); _GWidgetPostNotice8(title,statement,ap,40); va_end(ap); } void GWidgetPostNoticeTimeout8(int timeout,const char *title,const char *statement,...) { va_list ap; /* Force an old notice to disappear */ if ( title==NULL ) { if ( last!=NULL ) GDrawDestroyWindow(last); return; } va_start(ap,statement); _GWidgetPostNotice8(title,statement,ap,timeout); va_end(ap); } int GWidgetPostNoticeActive8(const char *title) { return( last!=NULL && last_title == title ); } void GWidgetError8(const char *title,const char *statement, ...) { struct dlg_info d; GWindow gw; char *ob[2]; va_list ap; ob[0] = _("_OK"); ob[1]=NULL; va_start(ap,statement); gw = DlgCreate8(title,statement,ap,(const char **) ob,0,0,&d,false,NULL,true,true); va_end(ap); if ( gw!=NULL ) { while ( !d.done ) GDrawProcessOneEvent(NULL); GDrawDestroyWindow(gw); } } static GWindow ChoiceDlgCreate8(struct dlg_info *d,const char *title, const char *question, va_list ap, const char **choices, int cnt, const char *multisel, char *buts[2], int def, int restrict_input, int center) { GTextInfo qlabels[GLINE_MAX+1], *llabels, blabel[4]; GGadgetCreateData *gcd, **array, boxes[5], *labarray[4], *barray[10], *barray2[8]; int lb,l; GRect pos; GWindow gw; GWindowAttrs wattrs; int as, ds, ld, fh; int w, maxw; int i, y, listi; char buf[600]; unichar_t *ubuf; extern GBox _GGadget_defaultbutton_box; memset(d,0,sizeof(*d)); GProgressPauseTimer(); vsnprintf(buf,sizeof(buf)/sizeof(buf[0]),question,ap); ubuf = utf82u_copy(buf); memset(qlabels,'\0',sizeof(qlabels)); lb = FindLineBreaks(ubuf,qlabels); llabels = calloc(cnt+1,sizeof(GTextInfo)); for ( i=0; i900 ) w = 900; if ( w>maxw ) maxw = w; } } for ( i=0; imaxw ) maxw = w; } maxw += GDrawPointsToPixels(gw,20); gcd = calloc(lb+1+2+2+2,sizeof(GGadgetCreateData)); array = calloc(2*(lb+1+2+2+2+1),sizeof(GGadgetCreateData *)); memset(boxes,0,sizeof(boxes)); l=0; if ( lb==1 ) { gcd[0].gd.pos.width = GDrawGetTextWidth(gw,qlabels[0].text,-1); gcd[0].gd.pos.x = (maxw-gcd[0].gd.pos.width)/2; gcd[0].gd.pos.y = GDrawPointsToPixels(gw,6); gcd[0].gd.pos.height = fh; gcd[0].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels | gg_pos_use0; gcd[0].gd.label = &qlabels[0]; gcd[0].creator = GLabelCreate; labarray[0] = GCD_Glue; labarray[1] = &gcd[0]; labarray[2] = GCD_Glue; labarray[3] = NULL; boxes[2].gd.flags = gg_visible|gg_enabled; boxes[2].gd.u.boxelements = labarray; boxes[2].creator = GHBoxCreate; array[0] = &boxes[2]; array[1] = NULL; i = l = 1; } else for ( i=0; iret = -1; d->size_diff = pos.height - gcd[listi].gd.pos.height; free(llabels); free(array); free(gcd); for ( i=0; iselected; } GDrawDestroyWindow(gw); GDrawSync(NULL); GDrawProcessPendingEvents(NULL); return(d.ret); }