1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
/*
* blink.c,v 1.4 2006/11/11 22:16:39 jenglish Exp
*
* Copyright 2004, Joe English.
*
* Usage:
* TtkBlinkCursor(corePtr), usually called in a widget's Init hook,
* arranges to periodically toggle the corePtr->flags CURSOR_ON bit
* on and off (and schedule a redisplay) whenever the widget has focus.
*
* Note: Widgets may have additional logic to decide whether
* to display the cursor or not (e.g., readonly or disabled states);
* TtkBlinkCursor() does not account for this.
*
* TODO:
* Add script-level access to configure application-wide blink rate.
*/
#include <tk.h>
#include "tkTheme.h"
#include "widget.h"
#define DEF_CURSOR_ON_TIME 600 /* milliseconds */
#define DEF_CURSOR_OFF_TIME 300 /* milliseconds */
/* Interp-specific data for tracking cursors:
*/
typedef struct
{
WidgetCore *owner; /* Widget that currently has cursor */
Tcl_TimerToken timer; /* Blink timer */
int onTime; /* #milliseconds to blink cursor on */
int offTime; /* #milliseconds to blink cursor off */
} CursorManager;
/* CursorManagerDeleteProc --
* InterpDeleteProc for cursor manager.
*/
static void CursorManagerDeleteProc(ClientData clientData, Tcl_Interp *interp)
{
CursorManager *cm = (CursorManager*)clientData;
if (cm->timer) {
Tcl_DeleteTimerHandler(cm->timer);
}
ckfree(clientData);
}
/* GetCursorManager --
* Look up and create if necessary the interp's cursor manager.
*/
static CursorManager *GetCursorManager(Tcl_Interp *interp)
{
static const char *cm_key = "tile::CursorManager";
CursorManager *cm = (CursorManager *) Tcl_GetAssocData(interp, cm_key,0);
if (!cm) {
cm = (CursorManager*)ckalloc(sizeof(*cm));
cm->timer = 0;
cm->owner = 0;
cm->onTime = DEF_CURSOR_ON_TIME;
cm->offTime = DEF_CURSOR_OFF_TIME;
Tcl_SetAssocData(interp,cm_key,CursorManagerDeleteProc,(ClientData)cm);
}
return cm;
}
/* CursorBlinkProc --
* Timer handler to blink the insert cursor on and off.
*/
static void
CursorBlinkProc(ClientData clientData)
{
CursorManager *cm = (CursorManager*)clientData;
int blinkTime;
if (cm->owner->flags & CURSOR_ON) {
cm->owner->flags &= ~CURSOR_ON;
blinkTime = cm->offTime;
} else {
cm->owner->flags |= CURSOR_ON;
blinkTime = cm->onTime;
}
cm->timer = Tcl_CreateTimerHandler(blinkTime, CursorBlinkProc, clientData);
TtkRedisplayWidget(cm->owner);
}
/* LoseCursor --
* Turn cursor off, disable blink timer.
*/
static void LoseCursor(CursorManager *cm, WidgetCore *corePtr)
{
if (corePtr->flags & CURSOR_ON) {
corePtr->flags &= ~CURSOR_ON;
TtkRedisplayWidget(corePtr);
}
if (cm->owner == corePtr) {
cm->owner = NULL;
}
if (cm->timer) {
Tcl_DeleteTimerHandler(cm->timer);
cm->timer = 0;
}
}
/* ClaimCursor --
* Claim ownership of the insert cursor and blink on.
*/
static void ClaimCursor(CursorManager *cm, WidgetCore *corePtr)
{
if (cm->owner == corePtr)
return;
if (cm->owner)
LoseCursor(cm, cm->owner);
corePtr->flags |= CURSOR_ON;
TtkRedisplayWidget(corePtr);
cm->owner = corePtr;
cm->timer = Tcl_CreateTimerHandler(cm->onTime, CursorBlinkProc, cm);
}
/*
* CursorEventProc --
* Event handler for FocusIn and FocusOut events;
* claim/lose ownership of the insert cursor when the widget
* acquires/loses keyboard focus.
*/
#define CursorEventMask (FocusChangeMask|StructureNotifyMask)
#define RealFocusEvent(d) \
(d == NotifyInferior || d == NotifyAncestor || d == NotifyNonlinear)
static void
CursorEventProc(ClientData clientData, XEvent *eventPtr)
{
WidgetCore *corePtr = (WidgetCore *)clientData;
CursorManager *cm = GetCursorManager(corePtr->interp);
switch (eventPtr->type) {
case DestroyNotify:
if (cm->owner == corePtr)
LoseCursor(cm, corePtr);
Tk_DeleteEventHandler(
corePtr->tkwin, CursorEventMask, CursorEventProc, clientData);
break;
case FocusIn:
if (RealFocusEvent(eventPtr->xfocus.detail))
ClaimCursor(cm, corePtr);
break;
case FocusOut:
if (RealFocusEvent(eventPtr->xfocus.detail))
LoseCursor(cm, corePtr);
break;
}
}
/*
* TtkBlinkCursor (main routine) --
* Arrange to blink the cursor on and off whenever the
* widget has focus.
*/
void TtkBlinkCursor(WidgetCore *corePtr)
{
Tk_CreateEventHandler(
corePtr->tkwin, CursorEventMask, CursorEventProc, corePtr);
}
/*EOF*/
|