Example #1
0
/*
 *  This function forms a search string, to look for the best
 *  letter to underline in a menu item. It first tries the
 *  accelerator key, then the uppercase letters from the menu name,
 *  then the digits, and finally the lowercase letters.
 *  It ignores spaces.
 */
static void set_search_string(char *search, const char *name, int key)
{
    int source;
    int dest = 0;
    int mb_len;
    mbstate_t mb_st;

    /* handle a couple of special cases first */
    if (! string_diff(name, "Cut"))  search[dest++] = 't';
    if (! string_diff(name, "Exit")) search[dest++] = 'x';
    if (! string_diff(name, "?")) search[dest++] = '?';

    {
	/* If there is an '&' in the string, use the next letter first */
	char *p = strchr(name, '&');
	if (p && *(p+1)) search[dest++] = *(p+1);
    }

    /* add the accelerator key if it is in the name string */
    if (key) {
	key = toupper(key);
	if (find_char(key, name) >= 0)
	    search[dest++] = (char) key;
	else {
	    key = tolower(key);
	    if (find_char(key, name) >= 0)
		search[dest++] = (char) key;
	}
    }
    /* add the uppercase letters */
    for (source=0; name[source]; source++) {
	mbs_init(mb_st);
	mb_len = Rf_mbrtowc(NULL, name + source, MB_CUR_MAX,&mb_st);
	if (mb_len > 1) source += mb_len-1;
	else
	    if (isupper(name[source])) search[dest++] = name[source];
    }
    /* add the digits */
    for (source=0; name[source]; source++) {
	mbs_init(mb_st);
	mb_len = Rf_mbrtowc(NULL, name + source, MB_CUR_MAX,&mb_st);
	if (mb_len > 1) source += mb_len-1;
	else
	    if (isdigit(name[source])) search[dest++] = name[source];
    }
    /* add the lowercase letters */
    for (source=0; name[source]; source++) {
	mbs_init(mb_st);
	mb_len = Rf_mbrtowc(NULL, name + source, MB_CUR_MAX,&mb_st);
	if (mb_len > 1) source += mb_len-1;
	else
	    if (islower(name[source])) search[dest++] = name[source];
    }
    /* end the search string */
    search[dest] = '\0';
}
Example #2
0
/*
 *  Load a font by name.
 */
font newfont(char *name, int style, int size)
{
	font obj;
	HFONT hf;
	LOGFONT lf;

	initapp(0,NULL);

	/* This next calculation should convert from point size to
	   pixels.  We use this since we always use the MM_TEXT mode,
	   which is in pixels. */
	/* Windows requires the lfHeight field must be negative
	   to specify point size, positive to specify pixel size. */

	if (size < 0) /* negative size indicates this is a point size */
		lf.lfHeight = ((screen_dpiy * size)/72);
	else /* positive size indicates a pixel height for the font */
		lf.lfHeight = size;

	lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
	lf.lfWeight = FW_NORMAL;
	lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
	lf.lfCharSet = ANSI_CHARSET;
	if ((! string_diff(name, "Symbol"))
		|| (! string_diff(name, "Wingdings")))
			lf.lfCharSet = SYMBOL_CHARSET;
	lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
	lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	lf.lfQuality = DEFAULT_QUALITY;
	lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
	if ((name != 0) && (*name != '\0'))
		strncpy(lf.lfFaceName, name, LF_FACESIZE-1);

	if (style & Italic)
		lf.lfItalic = 1;
	if (style & Bold)
		lf.lfWeight = FW_BOLD;
	if (style & FixedWidth)
		lf.lfPitchAndFamily |= FIXED_PITCH;
	if (style & SansSerif)
		lf.lfPitchAndFamily |= FF_SWISS;

	if ((hf = CreateFontIndirect(&lf)) == 0)
		return NULL;

	obj = new_font_object(hf);
	if (obj)
		obj->text = new_string(name);

	return (font) obj;
}
Example #3
0
// this function tries to detect long option entry in string
static opt_type check_for_long_option(const struct option *longopts, int *longindex,
	const char* str, const char*& arg)
{
	assert(longindex);
	assert(str);

	arg = 0;

	if (!longopts)
		return opt_not_found;

	// looking for '=' in string...
	int i;
	for (i = 0; str[i] && str[i] != '='; i++);
	assert(str[i] == '=' || !str[i]);

	int len = i; // len used for detecting length of option name in string

	if (str[i] == '=' && str[i + 1]) // ok, symbol '=' detected...
		arg = str + i + 1; // set potential option name substring length

	// detecting the nearest option name to string
	int index = 1;
	const char* opt = longopts->name;

	if (!opt)
		return opt_not_found;

	int diff = string_diff(opt, str, len);
	int selected = diff >= 0 ? 0 : -1;
	bool ambiguous = false;

	while (longopts[index].name) {
		opt = longopts[index].name;

		int _diff = string_diff(opt, str, len);
		if (!_diff) {
			diff = 0;
			selected = index;
			break;
		}

		if (_diff > 0 && diff > 0)
			ambiguous = true; // detected 2 or more options which are confirms string

		else if (_diff > 0 && (_diff < diff || diff < 0)) {
			diff = _diff;
			selected = index;
		}
		index++;
	}

	if (ambiguous && diff > 0) // if detected multiple confirms and no exact match - return error
		return opt_err_ambiguous;

	if (selected >= 0) {
		if (longopts[selected].has_arg == no_argument && arg) {
			return opt_err_no_arg; // argument is not required but specified
		}
		else if (longopts[selected].has_arg == no_argument /*&& !arg*/) {
			*longindex = index;
			return opt_single; // option without argument
		}
		else if (longopts[selected].has_arg == required_argument && arg) {
			*longindex = index;
			return opt_inplace_arg;
		}
		else if (longopts[selected].has_arg == required_argument) {
			*longindex = index;
			return opt_has_arg;
		}
		else if (longopts[selected].has_arg == optional_argument && arg) {
			*longindex = index;
			return opt_inplace_arg;
		}
		else if (longopts[selected].has_arg == optional_argument) {
			*longindex = index;
			return opt_single; // option without argument
		}
	}
	return opt_not_found;
}