refactoring and cleanup of wave_callback() -6-

This commit is contained in:
stefan schippers 2025-01-09 04:11:30 +01:00
parent 52b9cc1ee6
commit cd3eaf23ea
1 changed files with 53 additions and 43 deletions

View File

@ -39,7 +39,9 @@ static int waves_selected(int event, KeySym key, int state, int button)
rstate &= ~ShiftMask; /* don't use ShiftMask, identifying characters is sufficient */
if(xctx->ui_state & excl) skip = 1;
/* else if(event != -3 && sch_waves_loaded() < 0 ) skip = 1; */
else if(graph_use_ctrl_key && !(state & ControlMask)) skip = 1;
/* allow to work on graphs even if ctrl released while in GRAPHPAN mode
* This is useful on touchpads with TappingDragLock enabled */
else if(graph_use_ctrl_key && !(state & ControlMask) && !(xctx->ui_state & GRAPHPAN)) skip = 1;
else if(SET_MODMASK) skip = 1;
else if(event == MotionNotify && (state & Button2Mask)) skip = 1;
else if(event == MotionNotify && (state & Button1Mask) && (state & ShiftMask)) skip = 1;
@ -79,6 +81,7 @@ static int waves_selected(int event, KeySym key, int state, int button)
}
if(!is_inside) {
xctx->graph_master = -1;
xctx->ui_state &= ~GRAPHPAN; /* terminate ongoing GRAPHPAN to avoid deadlocks */
if(draw_xhair)
tclvareval(xctx->top_path, ".drw configure -cursor none" , NULL);
else
@ -384,7 +387,7 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int
xRect *r = NULL;
int access_cond = !graph_use_ctrl_key || (state & ControlMask);
dbg(1, "uistate=%d, graph_flags=%d\n", xctx->ui_state, xctx->graph_flags);
dbg(0, "uistate=%d, graph_flags=%d\n", xctx->ui_state, xctx->graph_flags);
if(event != -3 && !xctx->raw) return 0;
rstate = state; /* rstate does not have ShiftMask bit, so easier to test for KeyPress events */
rstate &= ~ShiftMask; /* don't use ShiftMask, identifying characters is sufficient */
@ -403,47 +406,6 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int
/* determine if mouse pointer is below xaxis or left of yaxis in some graph */
setup_graph_data(i, 0, gr);
/* check if user clicked on a wave label -> draw wave in bold */
if(event == ButtonPress && button == Button3 &&
edit_wave_attributes(2, i, gr)) {
draw_graph(i, 1 + 8 + (xctx->graph_flags & (2 | 4 | 128 | 256)), gr, NULL); /* draw data in graph box */
return 0;
}
/* destroy / show measurement widget */
if(xctx->graph_flags & 64) {
char sx[100], sy[100];
double xval, yval;
if(gr->digital) {
double deltag = gr->gy2 - gr->gy1;
double s1 = DIG_NWAVES; /* 1/DIG_NWAVES waveforms fit in graph if unscaled vertically */
double s2 = DIG_SPACE; /* (DIG_NWAVES - DIG_SPACE) spacing between traces */
double c = s1 * deltag;
deltag = deltag * s1 / s2;
yval=(DG_Y(xctx->mousey) - c) / s2;
yval=fmod(yval, deltag ) + gr->gy1;
if(yval > gr->gy2 + deltag * (s1 + s2) * 0.5) yval -= deltag;
} else {
yval = G_Y(xctx->mousey);
}
xval = G_X(xctx->mousex);
if(gr->logx) xval = pow(10, xval);
if(gr->logy) yval = pow(10, yval);
if(gr->unitx != 1.0)
my_snprintf(sx, S(sx), "%.5g%c", gr->unitx * xval, gr->unitx_suffix);
else
my_strncpy(sx, dtoa_eng(xval), S(sx));
if(gr->unitx != 1.0)
my_snprintf(sy, S(sy), "%.4g%c", gr->unity * yval, gr->unity_suffix);
else
my_strncpy(sy, dtoa_eng(yval), S(sy));
tclvareval("set measure_text \"y=", sy, "\nx=", sx, "\"", NULL);
tcleval("graph_show_measure");
} /* if(xctx->graph_flags & 64) */
gr->master_gx1 = gr->gx1;
gr->master_gx2 = gr->gx2;
gr->master_gw = gr->gw;
@ -457,6 +419,7 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int
my_strdup(_ALLOC_ID_, &r->prop_ptr, subst_token(r->prop_ptr, "hcursor1_y", dtoa(c)));
need_redraw_master = 1;
}
/* move hcursor2 */
else if(event == MotionNotify && (state & Button1Mask) && (xctx->graph_flags & 1024 )) {
double c;
@ -466,6 +429,7 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int
my_strdup(_ALLOC_ID_, &r->prop_ptr, subst_token(r->prop_ptr, "hcursor2_y", dtoa(c)));
need_redraw_master = 1;
}
/* move cursor1 */
/* set cursor position from master graph x-axis */
else if(event == MotionNotify && (state & Button1Mask) && (xctx->graph_flags & 16 )) {
@ -760,6 +724,13 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int
}
} /* key == 't' */
} /* if((i = xctx->graph_master) >= 0 && ((r = &xctx->rect[GRIDLAYER][i])->flags & 1)) */
/* check if user clicked on a wave label -> draw wave in bold */
if(event == ButtonPress && button == Button3 &&
edit_wave_attributes(2, i, gr)) {
draw_graph(i, 1 + 8 + (xctx->graph_flags & (2 | 4 | 128 | 256)), gr, NULL); /* draw data in graph box */
return 0;
}
/* save mouse position when doing pan operations */
else if(
( event == ButtonPress && (button == Button1 || button == Button3)) &&
@ -832,6 +803,45 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int
setup_graph_data(i, 1, gr); /* skip flag set, no reload x1 and x2 fields */
if(gr->dataset >= 0 /* && gr->dataset < xctx->raw->datasets */) dataset =gr->dataset;
else dataset = -1;
/* destroy / show measurement widget */
if(i == xctx->graph_master) {
if(xctx->graph_flags & 64) {
if( POINTINSIDE(xctx->mousex, xctx->mousey, gr->x1, gr->y1, gr->x2, gr->y2)) {
char sx[100], sy[100];
double xval, yval;
if(gr->digital) {
double deltag = gr->gy2 - gr->gy1;
double s1 = DIG_NWAVES; /* 1/DIG_NWAVES waveforms fit in graph if unscaled vertically */
double s2 = DIG_SPACE; /* (DIG_NWAVES - DIG_SPACE) spacing between traces */
double c = s1 * deltag;
deltag = deltag * s1 / s2;
yval=(DG_Y(xctx->mousey) - c) / s2;
yval=fmod(yval, deltag ) + gr->gy1;
if(yval > gr->gy2 + deltag * (s1 + s2) * 0.5) yval -= deltag;
} else {
yval = G_Y(xctx->mousey);
}
xval = G_X(xctx->mousex);
if(gr->logx) xval = pow(10, xval);
if(gr->logy) yval = pow(10, yval);
if(gr->unitx != 1.0)
my_snprintf(sx, S(sx), "%.5g%c", gr->unitx * xval, gr->unitx_suffix);
else
my_strncpy(sx, dtoa_eng(xval), S(sx));
if(gr->unitx != 1.0)
my_snprintf(sy, S(sy), "%.4g%c", gr->unity * yval, gr->unity_suffix);
else
my_strncpy(sy, dtoa_eng(yval), S(sy));
tclvareval("set measure_text \"y=", sy, "\nx=", sx, "\"", NULL);
tcleval("graph_show_measure");
} else {
tcleval("graph_show_measure stop");
}
} /* if(xctx->graph_flags & 64) */
} /* if(i == xctx->graph_master) */
dbg(1, "%g %g %g %g - %d %d\n", gr->gx1, gr->gy1, gr->gx2, gr->gy2, gr->divx, gr->divy);