⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coolwidget.c

📁 具有IDE功能的编辑器
💻 C
📖 第 1 页 / 共 3 页
字号:
/* coolwidget.c - routines for simple widgets. Widget setup and destruction   Copyright (C) 1996-2000 Paul Sheer   This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 2 of the License, or   (at your option) any later version.   This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.   You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA   02111-1307, USA. */#define COOL_WIDGET_C#include <X11/Xatom.h>#include "coolwidget.h"#include "coollocal.h"#include "mad.h"extern struct look *look;/* call this for fatal errors */void CError (const char *fmt,...){    va_list s;    char *str;    va_start (s, fmt);    str = vsprintf_alloc (catstrs (" ", fmt, " ", 0), s);    CFatalErrorDialog (20, 20, str);    free (str);}/* an malloc with an error check */#ifndef HAVE_MAD#ifndef CMallocvoid *CMalloc (size_t size){    void *p;    if ((p = malloc (size + 8)) == NULL)/* Not essential to translate */	CError (_("Unable to allocate memory.\n"));    return p;}#endif#ifdef DEBUG#ifndef HAVE_MADvoid *CDebugMalloc (size_t x, int line, const char *file){    void *p;    if ((p = malloc (x)) == NULL)/* Not essential to translate */	CError (_("Unable to allocate memory: line %d, file %s.\n"), line, file);    return p;}#endif#endif#endifint allocate_color (char *color){    if (!color)	return NO_COLOR;    if (*color >= '0' && *color <= '9') {	return atoi (color);    } else {	int i;	XColor c;	if (!color)	    return NO_COLOR;	if (!XParseColor (CDisplay, CColormap, color, &c))	    return NO_COLOR;	if (!XAllocColor (CDisplay, CColormap, &c))	    return NO_COLOR;	for (i = 0; i < color_last_pixel; i++)	    if (color_palette (i) == c.pixel)		return i;	color_palette (color_last_pixel) = c.pixel;	return color_last_pixel++;    }}struct cursor_state {    int x, y, h, w;    Window window;    GC gc;    XFontSet font_set;    XFontStruct *font_struct;    int state;    int type;    wchar_t chr;    unsigned long bg, fg;    int style;    int font_x, font_y;};struct cursor_state CursorState ={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};void render_cursor (struct cursor_state c);void set_cursor_position (Window win, int x, int y, int w, int h, int type, wchar_t chr, unsigned long bg, unsigned long fg, int style){    if (win == CGetFocus ()) {	CursorState.x = x;	CursorState.y = y;	CursorState.h = h;	CursorState.w = w;	CursorState.window = win;	CursorState.gc = CGC;	CursorState.font_set = current_font->font_set;	CursorState.font_struct = current_font->font_struct;	CursorState.type = type;	CursorState.chr = chr;	CursorState.style = style;	CursorState.bg = bg;	CursorState.fg = fg;	CursorState.font_x = FONT_OFFSET_X;	CursorState.font_y = FONT_OFFSET_Y;	render_cursor (CursorState);    } else {	if (!(win | h | w))	    CursorState.window = 0;    }}int option_xor_cursor = 0;int option_flashing_cursor = 1;unsigned long option_cursor_color;void render_cursor (struct cursor_state c){    if (!CursorState.window)	return;    if (c.type == CURSOR_TYPE_EDITOR) {	if (CursorState.window != CGetFocus ())	    return;	CPushFont ("editor", 0);	if (!option_xor_cursor) {	    if (c.state || !option_flashing_cursor)		CSetColor (option_cursor_color);	    else		CSetColor (c.bg);	    if (c.style & MOD_REVERSE) {		CLine (c.window, c.x + c.w - 1, c.y + FONT_OVERHEAD, c.x + c.w - 1, c.y + c.h - 1);		CLine (c.window, c.x + c.w - 2, c.y + FONT_OVERHEAD, c.x + c.w - 2, c.y + c.h - 1);	    } else {		CLine (c.window, c.x, c.y + FONT_OVERHEAD, c.x, c.y + c.h - 1);		CLine (c.window, c.x + 1, c.y + FONT_OVERHEAD, c.x + 1, c.y + c.h - 1);	    }	    CLine (c.window, c.x, c.y + FONT_OVERHEAD, c.x + c.w - 1, c.y + FONT_OVERHEAD);	    CLine (c.window, c.x, c.y + FONT_OVERHEAD + 1, c.x + c.w - 1, c.y + FONT_OVERHEAD + 1);	}	if (!c.state && option_flashing_cursor) {	    XSetBackground (CDisplay, c.gc, c.bg);	    XSetForeground (CDisplay, c.gc, c.fg);	    CImageTextWC (c.window, c.x + c.font_x, c.y + c.font_y, 0, &(c.chr), 1);	} else {	    if (option_xor_cursor) {		XSetBackground (CDisplay, c.gc, c.fg);		XSetForeground (CDisplay, c.gc, c.bg);		CImageTextWC (c.window, c.x + c.font_x, c.y + c.font_y, 0, &(c.chr), 1);	    }	}	CPopFont ();    } else {	if (CursorState.window != CGetFocus ()) {	    CSetColor (COLOR_FLAT);	    CLine (c.window, c.x, c.y, c.x, c.y + c.h - 6);	} else {	    render_bevel (c.window, c.x - 1, c.y - 1, c.x, c.y + c.h - 5, 1, CursorState.state ? 0 : 1);	}    }}void CSetCursorColor (unsigned long c){    option_cursor_color = c;}/* this is called from CNextEvent if an alarm event comes */void toggle_cursor (void){    CursorState.state = 1 - CursorState.state;    render_cursor (CursorState);}void set_cursor_visible (void){    CursorState.state = 1;    render_cursor (CursorState);}void set_cursor_invisible (void){    CursorState.state = 0;    render_cursor (CursorState);}/*   These three routines are not much slower than doing the same   thing with integers as identifiers instead of strings.   It returns the index in the global array of widgets of   the widget named ident. Returns 0 if not found. */static int find_ident (const char *ident){    int i = last_widget + 1;    u_32bit_t p;    if (!ident)	return 0;    if (!ident[0])	return 0;    memcpy (&p, ident, sizeof (u_32bit_t));	/* we need four byte alignment for some machines */    if (!ident[1])	goto word_search;    if (ident[2]) {		/* can compare first four bytes at once */	while (--i)	    if (CIndex (i))		if (*((u_32bit_t *) CIndex (i)->ident) == p)		    if (!strcmp (CIndex (i)->ident, ident))			return i;	return 0;    } else {	word s;      word_search:	s = *((word *) (&p));	while (--i)	    if (CIndex (i))		if (*((word *) CIndex (i)->ident) == s)		    if (!strcmp (CIndex (i)->ident, ident))			return i;    }    return 0;}CWidget *CIdent (const char *ident){    return CIndex (find_ident (ident));}CWidget *CWidgetOfWindow (Window win){    return CIndex (widget_of_window (win));}int CSystem (const char *string){    int r;    CDisableAlarm ();    r = system (string);    CEnableAlarm ();    return r;}extern int (*global_alarm_callback[33]) (CWidget *, XEvent *, CEvent *);void CAddCallback (const char *ident, int (*callback) (CWidget *, XEvent *, CEvent *)){    CWidget *w = CIdent (ident);    if (w)	w->callback = callback;    else {	if (!strcmp (ident, "AlarmCallback"))	    global_alarm_callback[0] = callback;	if (!strncmp (ident, "AlarmCallback", 13))	    global_alarm_callback[atoi (ident + 13) + 1] = callback;    }}void CAddBeforeCallback (const char *ident, int (*callback) (CWidget *, XEvent *, CEvent *)){    CWidget *w = CIdent (ident);    if (w)	w->callback_before = callback;}#ifdef DEBUG_DOUBLE/* checks the magic numbers */int widget_check_magic (){    int i = 0;    while (last_widget > i++)	if (CIndex (i) != NULL)	    if (CIndex (i)->magic_begin != WIDGET_MAGIC_BEGIN || CIndex (i)->magic_end != WIDGET_MAGIC_END)/* NLS ? */		CError ("Cool widget internal error - magic number overwritten overwritten.\n");    return 0;}#endif/* sends a full expose event to the widget */void CExpose (const char *ident){    CWidget *w = CIdent (ident);    if (w)	CSendExpose (w->winid, 0, 0, w->width, w->height);}/* Returns the widgets window or 0 if not found */Window CWindowOfWidget (const char *ident){    CWidget *w = CIdent (ident);    if (w)	return w->winid;    else	return 0;}/* send an expose event to the internel queue */void CExposeWindowArea (Window win, int count, int x, int y, int w, int h){    if (x < 0) {	w = x + w;	x = 0;    }    if (y < 0) {	h = y + h;	y = 0;    }    if (w <= 0 || h <= 0)	return;    CSendExpose (win, x, y, w, h);}/* Returns the first NULL list entry. Exits if list full. */CWidget **find_empty_widget_entry (){    int i = 0;/* widget can be added to an empty point in the list (created from an   undraw command, or to the end of the list. */    while (last_widget > i++) {	if (CIndex (i) == NULL)	    break;    }    if (i == MAX_NUMBER_OF_WIDGETS - 2)/* NLS ? */	CError ("No more space in widget list\nIncrease MAX_NUMBER_OF_WIDGETS in coolwidget.h\n");    if (i == last_widget)	last_widget++;		/* increase list length if an entry was added to the end */    return &(CIndex (i));}/* Fills in the widget structure. */CWidget *allocate_widget (Window newwin, const char *ident, Window parent, int x, int y,			  int width, int height, int kindofwidget){    CWidget *w = CMalloc (sizeof (CWidget));    memset (w, 0, sizeof (CWidget));	/*: important, 'cos free's check if NULL before freeing many parems */    w->magic_begin = WIDGET_MAGIC_BEGIN;    w->winid = newwin;    w->parentid = parent;    w->width = width;    w->height = height;    w->x = x;    w->y = y;    strncpy (w->ident, ident, 32);    w->kind = kindofwidget;    w->magic_end = WIDGET_MAGIC_END;    return w;}static int override_redirect = 0;void CSetOverrideRedirect (void){    override_redirect = 1;}void CClearOverrideRedirect (void){    override_redirect = 0;}/*   Sets up the widget's window and calls allocate_widget()   to allocate space and set up the data structures.   What is set up here is common to all widgets, so   it will always be the first routine called by a CDraw...()   function. */CWidget *CSetupWidget (const char *identifier, Window parent, int x, int y,	    int width, int height, int kindofwidget, unsigned long input,		       unsigned long bgcolor, int takes_focus){    XSetWindowAttributes xswa;    Window newwin;    CWidget **w;/* NLS ? */    if (CIdent (identifier) && kindofwidget == C_BUTTON_WIDGET)/* Not essential to translate */	CError (_ ("Trying to create a button with the same identifier as an existing widget.\n"));    xswa.colormap = CColormap;    xswa.bit_gravity = NorthWestGravity;    xswa.background_pixel = bgcolor;    switch (kindofwidget) {    case C_MENU_WIDGET:    case C_TOOLHINT_WIDGET:    case C_ICON_WIDGET:	xswa.override_redirect = 1;	break;    default:	xswa.override_redirect = override_redirect;	break;    }    newwin = XCreateWindow (CDisplay, parent, x, y, width, height, 0,			    CDepth, InputOutput, CVisual,	    CWOverrideRedirect | CWColormap | CWBackPixel | CWBitGravity,			    &xswa);    w = find_empty_widget_entry ();	/* find first unused list entry in list of widgets */    *w = allocate_widget (newwin, identifier, parent, x, y,			  width, height, kindofwidget);    (*w)->mainid = CFindParentMainWindow (parent);    (*w)->eh = default_event_handler (kindofwidget);    (*w)->takes_focus = takes_focus;    XSelectInput (CDisplay, newwin, input);    switch ((*w)->kind) {    case C_WINDOW_WIDGET:	/* window widgets must only be mapped 				   once all there children are drawn */#ifdef USE_XIM	if (CIM) {	    create_input_context (*w, get_input_style ());	    set_status_position (*w);	}#endif	break;    default:	XMapWindow (CDisplay, newwin);	/* shows the window */	XFlush (CDisplay);	break;    }    return (*w);}extern Atom ATOM_WM_PROTOCOLS, ATOM_WM_DELETE_WINDOW, ATOM_WM_NAME, ATOM_WM_NORMAL_HINTS;void CSetWindowSizeHints (CWidget * wdt, int min_w, int min_h, int max_w, int max_h){    XSizeHints size_hints;    long d;    size_hints.min_width = min_w;    size_hints.min_height = min_h;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -