static void editorprint(control m) { printer lpr; font f; textbox t = getdata(m); const char *contents = gettext(t); char msg[LF_FACESIZE + 128]; char *linebuf = NULL; int cc, rr, fh, page, linep, i, j, istartline; int top, left; if (!(lpr = newprinter(0.0, 0.0, ""))) return; f = gnewfont(lpr, strcmp(fontname, "FixedFont") ? fontname : "Courier New", fontsty, pointsize, 0.0, 1); top = devicepixelsy(lpr) / 5; left = devicepixelsx(lpr) / 5; fh = fontheight(f); rr = getheight(lpr) - top; cc = getwidth(lpr) - 2*left; linep = rr; /* line in printer page */ page = 1; i = 0; while (i < strlen(contents)) { if ( linep + fh >= rr ) { /* new page */ if (page > 1) nextpage(lpr); snprintf(msg, LF_FACESIZE + 128, "Page %d", page++); gdrawstr(lpr, f, Black, pt(cc - gstrwidth(lpr, f, msg) - 1, top), msg); linep = top + 2*fh; } j = 0; istartline = i; while (contents[i] != '\n' && contents[i] != '\0') { ++i; ++j; } linebuf = realloc(linebuf, (j+1)*sizeof(char)); strncpy(linebuf, &contents[istartline], j); linebuf[j] = '\0'; gdrawstr(lpr, f, Black, pt(left, linep), linebuf); linep += fh; ++i; } free(linebuf); if (f != FixedFont) del(f); del(lpr); }
SEXP Win_selectlist(SEXP args) { SEXP choices, preselect, ans = R_NilValue; const char **clist; int i, j = -1, n, mw = 0, multiple, nsel = 0; int xmax, ymax, ylist, fht, h0; Rboolean haveTitle; choices = CAR(args); if(!isString(choices)) error(_("invalid '%s' argument"), "choices"); preselect = CADR(args); if(!isNull(preselect) && !isString(preselect)) error(_("invalid '%s' argument"), "preselect"); multiple = asLogical(CADDR(args)); if(multiple == NA_LOGICAL) multiple = 0; haveTitle = isString(CADDDR(args)); if(!multiple && isString(preselect) && LENGTH(preselect) != 1) error(_("invalid '%s' argument"), "preselect"); n = LENGTH(choices); clist = (const char **) R_alloc(n + 1, sizeof(char *)); for(i = 0; i < n; i++) { clist[i] = translateChar(STRING_ELT(choices, i)); mw = max(mw, gstrwidth(NULL, SystemFont, clist[i])); } clist[n] = NULL; fht = getSysFontSize().height; xmax = max(170, mw+60); /* allow for scrollbar */ if(ismdi()) { RECT *pR = RgetMDIsize(); h0 = pR->bottom; } else { h0 = deviceheight(NULL); } ymax = min(80+fht*n, h0-100); /* allow for window widgets, toolbar */ ylist = ymax - 60; wselect = newwindow(haveTitle ? translateChar(STRING_ELT(CADDDR(args), 0)): (multiple ? _("Select one or more") : _("Select one")), rect(0, 0, xmax, ymax), Titlebar | Centered | Modal | Floating); setbackground(wselect, dialog_bg()); if(multiple) f_list = newmultilist(clist, rect(10, 10, xmax-25, ylist), NULL, finish); else f_list = newlistbox(clist, rect(10, 10, xmax-25, ylist), NULL, finish); if(!isNull(preselect) && LENGTH(preselect)) { for(i = 0; i < n; i++) for(j = 0; j < LENGTH(preselect); j++) if(strcmp(clist[i], translateChar(STRING_ELT(preselect, j))) == 0) { setlistitem(f_list, i); break; } } bFinish = newbutton(G_("OK"), rect(xmax-160, ymax-40, 70, 25), finish); bCancel = newbutton(G_("Cancel"), rect(xmax-80, ymax-40, 70, 25), cancel); setkeydown(wselect, key1); show(wselect); done = 0; while(!done) { R_WaitEvent(); R_ProcessEvents(); } if(multiple) { if (done == 1) { /* Finish */ for(i = 0; i < n; i++) if(isselected(f_list, i)) nsel++; PROTECT(ans = allocVector(STRSXP, nsel)); for(i = 0, j = 0; i < n; i++) if(isselected(f_list, i)) SET_STRING_ELT(ans, j++, mkChar(clist[i])); } else { /* cancel */ PROTECT(ans = allocVector(STRSXP, 0)); } } else PROTECT(ans = mkString(selected)); cleanup(); show(RConsole); R_ProcessEvents(); UNPROTECT(1); return ans; }