Re: How to make cursor blinking in Gtk 1.2?



ming deng <mingd oeone com> writes:
Though I saw there are cursor blinking settings in GtkSettings of Gtk
2.0, I want to know how to make cursor blinking in current 1.2
application?  Cursors in mozilla blinks,  does anyone know who to?
Mozilla implements its own cursor; a blinking cursor in the
standard text areas is not a feature of GTK+-1.2.

Yeah, been there :-)

Here's some code I have, which implements a flashing cursor ....

static const char dc_bits[] = {
   0x20, 0x00, 0x70, 0x00, 0xf8, 0x00, 0xfc, 0x01, 0xfe, 0x03, 0xff, 0x07,
   0xfe, 0x03, 0xfc, 0x01, 0xf8, 0x00, 0x70, 0x00, 0x20, 0x00};

#define dc_width	11
#define dc_height	11

#define dcxorigin	(dc_width/2)
#define dcyorigin	(dc_height/2)

static GdkBitmap	*cursor_diamond;

[...]

static void gm_cursor_flash_on(GMEditor *gme)
	/*
	 * Draw the cursor. Drawing is easy - we just copy the cursor
	 * image onto the canvas. The underlying image is still preserved
	 * in the editor's score image.
	 */
{
	GdkGC *gc;

	gc = gm_colour_gc(GM_COLOUR_CURSOR);
	gdk_gc_set_clip_origin(gc, gme->cursor_x, gme->cursor_y);
	gdk_gc_set_clip_mask(gc, cursor_diamond);
	XCopyPlane(GDK_WINDOW_XDISPLAY(gme->canvas->window),
		   GDK_WINDOW_XWINDOW(cursor_diamond),
		   GDK_WINDOW_XWINDOW(gme->canvas->window), GDK_GC_XGC(gc),
		   0, 0, dc_width, dc_height, gme->cursor_x, gme->cursor_y, 1);
}

static void gm_cursor_flash_off(GMEditor *gme)
	/*
	 * Neuk the cursor by redrawing it's bounding box from the
	 * score image.
	 */
{
	gint spos, ymiddle, yledger, icbottom1, y;

	gdk_draw_pixmap(gme->canvas->window,
		gme->canvas->style->fg_gc[GTK_WIDGET_STATE(gme->canvas)],
		gme->image, gme->cursor_x, gme->cursor_y,
		gme->cursor_x, gme->cursor_y, dc_width, dc_height);

	/*
	 * Now redraw any cursor ledger lines destroyed by the erasure
	 * of the cursor.
	 */
	spos = gme->cursor_staveposition;
	if(spos >= 6) {
		/*
		 * Ledger lines above the stave. spos is positive.
		 */
		if(spos & 1) --spos;
		ymiddle = gme->cursor_sys->ymiddle[gme->cursor_staveno];
		yledger = ymiddle-(spos/2)*gme->pd->gap_line-gme->scroll_yoffset;
		icbottom1 = gme->cursor_y+dc_height;
		for(y = yledger; y < icbottom1; y += gme->pd->gap_line)
			gm_draw_hline(gme->canvas->window, gme->cursor_x, y,
				  dc_width, gme->pd->weight_stave,
				  GM_COLOUR_CURSOR);
	} else if(spos <= -6) {
		/*
		 * Ledger lines below the stave. spos is negative.
		 */
		spos = gme->cursor_staveposition;
		if(spos & 1) ++spos;
		ymiddle = gme->cursor_sys->ymiddle[gme->cursor_staveno];
		yledger = ymiddle-(spos/2)*gme->pd->gap_line-gme->scroll_yoffset;
		for(y = yledger; y >= gme->cursor_y; y -= gme->pd->gap_line)
			gm_draw_hline(gme->canvas->window, gme->cursor_x, y,
				  dc_width, gme->pd->weight_stave,
				  GM_COLOUR_CURSOR);
	}
}

static gint gm_cursor_timeout(GMEditor *gme)
	/*
	 * Either draw or erase the cursor. Used to display a flashing
	 * cursor at the insertion point.
	 */
{
	if(gme->cursor_on) {
		gm_cursor_flash_off(gme);
		gme->cursor_on = FALSE;
	} else {
		gm_cursor_flash_on(gme);
		gme->cursor_on = TRUE;
	}
	return TRUE;
}

[...]

void gm_cursor_start(GMEditor *gme)
{
	gme->cursor_sys = gme->gmesystem;
	if(gme->cursor_sys) {
		gm_cursor_sequence(gme, 0, 0);
		gm_cursor_locate(gme);
		gme->cursor_tag = gtk_timeout_add(500,
					(GtkFunction)gm_cursor_timeout, gme);
	}
}

void gm_cursor_stop(GMEditor *gme)
{
	gtk_timeout_remove(gme->cursor_tag);
}

void gm_cursor_init()
{
	cursor_diamond = gdk_bitmap_create_from_data(NULL, dc_bits,
				dc_width, dc_height);
}

This is just as an example to show you how I do it .... you won't be
aboe to drop this straight into another app, of course.

You'll need to do your own GC & data structures, and there's also
some 'junk', e.g. the ledger line stuff, but this should show you
the relevant GTK+ bits & pieces you need.

Allan




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]