File: scale_ui.c

package info (click to toggle)
ggobi 2.1.9~20091212-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,340 kB
  • ctags: 5,083
  • sloc: ansic: 57,242; xml: 30,604; cpp: 833; makefile: 355; java: 225; perl: 201; sh: 122; python: 23
file content (518 lines) | stat: -rw-r--r-- 17,109 bytes parent folder | download | duplicates (4)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/* scale_ui.c */
/*
 * ggobi
 * Copyright (C) AT&T, Duncan Temple Lang, Dianne Cook 1999-2005
 *
 * ggobi is free software; you may use, redistribute, and/or modify it
 * under the terms of the Common Public License, which is distributed
 * with the source code and displayed on the ggobi web site, 
 * www.ggobi.org.  For more information, contact the authors:
 *
 *   Deborah F. Swayne   dfs@research.att.com
 *   Di Cook             dicook@iastate.edu
 *   Duncan Temple Lang  duncan@wald.ucdavis.edu
 *   Andreas Buja        andreas.buja@wharton.upenn.edu
*/

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#ifdef USE_STRINGS_H
#include <strings.h>
#endif
#include <string.h>
#include "vars.h"
#include "externs.h"
#include <math.h>

void
scale_update_set (gboolean update, displayd * dsp, ggobid * gg)
{
  cpaneld *cpanel = &dsp->cpanel;
  GtkWidget *panel =
    mode_panel_get_by_name (GGOBI (getIModeName) (SCALE), gg);
  GtkWidget *w;
  GtkUpdateType policy;

  cpanel->scale.updateAlways_p = update;

  if (cpanel->scale.updateAlways_p)
    policy = GTK_UPDATE_CONTINUOUS;
  else
    policy = GTK_UPDATE_DISCONTINUOUS;

  /* When the update policy changes, change the update policy of
     the range widgets as well */
  w = widget_find_by_name (panel, "SCALE:x_zoom");
  gtk_range_set_update_policy (GTK_RANGE (w), policy);
  w = widget_find_by_name (panel, "SCALE:y_zoom");
  gtk_range_set_update_policy (GTK_RANGE (w), policy);
  w = widget_find_by_name (panel, "SCALE:x_pan");
  gtk_range_set_update_policy (GTK_RANGE (w), policy);
  w = widget_find_by_name (panel, "SCALE:y_pan");
  gtk_range_set_update_policy (GTK_RANGE (w), policy);

}

/* Use the hscale widget name to find the corresponding adjustment */
static GtkAdjustment *
scale_adjustment_find_by_name (gchar * name, ggobid * gg)
{
  GtkWidget *panel, *w;

  panel = mode_panel_get_by_name ("Scale", gg);
  w = widget_find_by_name (panel, name);
  if (GTK_IS_HSCALE (w))
    return (gtk_range_get_adjustment (GTK_RANGE (w)));
  else return NULL;
}

static void
increment_adjustment (GtkAdjustment * adj, gdouble step, gdouble eps)
{
  gdouble value = adj->value + step;
  value = MAX (value, adj->lower);
  value = MIN (value, adj->upper);
  if (fabs (value - adj->value) > eps)
    gtk_adjustment_set_value (adj, value);
}

static void
zoom_cb (GtkAdjustment * adj, ggobid * gg)
{
  displayd *display = gg->current_display;
  splotd *sp = gg->current_splot;
  cpaneld *cpanel = &display->cpanel;
  gchar *name = (gchar *) g_object_get_data (G_OBJECT (adj), "name");
  greal oscalex = sp->scale.x, oscaley = sp->scale.y;
  GtkAdjustment *adj_other;
  // step and eps are in the space of the adjustment values;
  // exp_eps is in the space of the scaling values.
  gdouble expvalue = pow (10., adj->value), step = 0.0; // exp10
  gdouble eps = .0001, exp_eps = .001;

  /* this unappealing case arises when cpanel_scale_set is resetting
     adjustment values, which calls the associated callbacks. */
  if (display != sp->displayptr)
    return;

  if (strcmp (name, "SCALE:x_zoom_adj") == 0) {
    cpanel->scale.zoomval.x = adj->value;
    step = adj->value - log10 (sp->scale.x);
    sp->scale.x = expvalue;
    if (cpanel->scale.fixAspect_p && fabs (step) > eps) {
      adj_other = scale_adjustment_find_by_name ("SCALE:y_zoom", gg);
      sp->scale.y = pow (10., adj_other->value + step);
      increment_adjustment (adj_other, step, eps);
    }
  }
  else {
    cpanel->scale.zoomval.y = adj->value;
    step = adj->value - log10 (sp->scale.y);
    sp->scale.y = expvalue;
    if (cpanel->scale.fixAspect_p && fabs (step) > eps) {
      adj_other = scale_adjustment_find_by_name ("SCALE:x_zoom", gg);
      sp->scale.x = pow (10.0, adj_other->value + step);
      increment_adjustment (adj_other, step, eps);
    }
  }

  if (fabs (oscalex - sp->scale.x) > exp_eps ||
      fabs (oscaley - sp->scale.y) > exp_eps) {
    splot_plane_to_screen (display, cpanel, sp, gg);
    ruler_ranges_set (false, display, sp, gg);
    splot_redraw (sp, FULL, gg);
  }
}

static void
pan_cb (GtkAdjustment * adj, ggobid * gg)
{
  displayd *display = gg->current_display;
  splotd *sp = gg->current_splot;
  cpaneld *cpanel = &display->cpanel;
  gchar *name = (gchar *) g_object_get_data (G_OBJECT (adj), "name");

  /* this unappealing case arises when cpanel_scale_set is resetting
     adjustment values, which calls the associated callbacks. */
  if (display != sp->displayptr)
    return;

  if (strcmp (name, "SCALE:x_pan_adj") == 0) {
    cpanel->scale.panval.x = adj->value;
    sp->pmid.x = -1 * adj->value;
  }
  else {
    cpanel->scale.panval.y = adj->value;
    sp->pmid.y = -1 * adj->value;
  }

  splot_plane_to_screen (display, cpanel, sp, gg);
  ruler_ranges_set (false, display, sp, gg);
  splot_redraw (sp, FULL, gg);
}

void
scale_pan_reset (displayd * display)
{
  ggobid *gg = display->ggobi;
  splotd *sp = gg->current_splot;
  GtkAdjustment *adj;
  gdouble value = 0.0;

  adj = scale_adjustment_find_by_name ("SCALE:y_pan", gg);
  gtk_adjustment_set_value (adj, value);
  adj = scale_adjustment_find_by_name ("SCALE:x_pan", gg);
  gtk_adjustment_set_value (adj, value);

  /* If we've been scaling using the scale widgets, the following is
     redundant, but we need it if we've been scaling by direct
     manipulation */

  sp->pmid.x = sp->pmid.y = 0;

  splot_plane_to_screen (display, &display->cpanel, sp, gg);
  ruler_ranges_set (false, gg->current_display, sp, gg);
  splot_redraw (sp, FULL, gg);
}

void
scale_zoom_reset (displayd * dsp)
{
  ggobid *gg = dsp->ggobi;
  splotd *sp = gg->current_splot;
  GtkAdjustment *adj;
  gdouble value = log10 (SCALE_DEFAULT);

  adj = scale_adjustment_find_by_name ("SCALE:y_zoom", gg);
  gtk_adjustment_set_value (adj, value);
  adj = scale_adjustment_find_by_name ("SCALE:x_zoom", gg);
  gtk_adjustment_set_value (adj, value);

  /* If we've been scaling using the scale widgets, the following is
     redundant, but we need it if we've been scaling by direct
     manipulation */

  sp->scale.x = sp->scale.y = SCALE_DEFAULT;

  splot_plane_to_screen (dsp, &dsp->cpanel, sp, gg);
  ruler_ranges_set (false, dsp, sp, gg);
  splot_redraw (sp, FULL, gg);
}

/*--------------------------------------------------------------------*/
/*           Resetting various state variables                        */
/*--------------------------------------------------------------------*/

static void
aspect_ratio_cb (GtkToggleButton * button, ggobid * gg)
{
  displayd *display = gg->current_display;
  cpaneld *cpanel = &display->cpanel;

  cpanel->scale.fixAspect_p = button->active;
}

/*--------------------------------------------------------------------*/
/*      Handling keyboard and mouse events in the plot window         */
/*--------------------------------------------------------------------*/

/*
If ctrl-alt-p is pressed, toggle on and off click-style panning.
If ctrl-alt-z is pressed, toggle on and off click-style zooming.
*/

static gint
key_press_cb (GtkWidget * w, GdkEventKey * event, splotd * sp)
{
  ggobid *gg = GGobiFromSPlot (sp);
  cpaneld *cpanel = &gg->current_display->cpanel;

/*-- add a key_press_cb in each mode, and let it begin with these lines --*/
  if (splot_event_handled (w, event, cpanel, sp, gg))
    return true;

  return false;
}

static gint
motion_notify_cb (GtkWidget * w, GdkEventMotion * event, splotd * sp)
{
  gboolean button1_p, button2_p;
  ggobid *gg = GGobiFromSPlot (sp);
  displayd *display = (displayd *) sp->displayptr;
  cpaneld *cpanel = &display->cpanel;

  /*-- get the mouse position and find out which buttons are pressed --*/
  mousepos_get_motion (w, event, &button1_p, &button2_p, sp);

  /*-- if neither button is pressed, we shouldn't have gotten the event --*/
  if (!button1_p && !button2_p)
    return false;

  /*-- I'm not sure this could ever happen --*/
  if (sp->mousepos.x == sp->mousepos_o.x
      && sp->mousepos.y == sp->mousepos_o.y)
    return false;

  if (button1_p) {
    pan_by_drag (sp, gg);
  }
  else if (button2_p) {
    zoom_by_drag (sp, gg);
  }

  if (cpanel->scale.updateAlways_p) {
    /*-- redisplay this plot --*/
    splot_plane_to_screen (display, &display->cpanel, sp, gg);
    ruler_ranges_set (false, gg->current_display, sp, gg);
    splot_redraw (sp, FULL, gg);
  }
  else {
    splot_redraw (sp, QUICK, gg);
  }

  sp->mousepos_o.x = sp->mousepos.x;
  sp->mousepos_o.y = sp->mousepos.y;

  return true;
}

static gint
scroll_cb (GtkWidget *w, GdkEventScroll *event, splotd *sp)
{
  /* For now, make this a fixed-ratio zoom. Most people don't have horizontal
     mouse wheels. */
  gfloat factor = 1.0, xscale = sp->scale.x, yscale = sp->scale.y;
  if (event->direction == GDK_SCROLL_UP)
    factor += SCALE_SCROLL_INC;
  else if (event->direction == GDK_SCROLL_DOWN)
    factor -= SCALE_SCROLL_INC;
  xscale *= factor;
  yscale *= factor;
  splot_zoom(sp, xscale, yscale);
  return(true);
}

static gint
button_press_cb (GtkWidget * w, GdkEventButton * event, splotd * sp)
{
  gboolean retval = true;
  ggobid *gg = GGobiFromSPlot (sp);
  gboolean button1_p, button2_p;
  mousepos_get_pressed (w, event, &button1_p, &button2_p, sp);

  gg->current_splot = sp->displayptr->current_splot = sp;
  gg->current_display = (displayd *) sp->displayptr;

  sp->mousepos_o.x = sp->mousepos.x;
  sp->mousepos_o.y = sp->mousepos.y;

  sp->mousedownpos.x = sp->mousepos.x;
  sp->mousedownpos.y = sp->mousepos.y;

  sp->motion_id = g_signal_connect (G_OBJECT (sp->da),
                                    "motion_notify_event",
                                    G_CALLBACK (motion_notify_cb),
                                    (gpointer) sp);
  return retval;
}

static gint
button_release_cb (GtkWidget * w, GdkEventButton * event, splotd * sp)
{
  gboolean retval = true;
  ggobid *gg = GGobiFromSPlot (sp);
  GdkModifierType state;
  displayd *dsp = sp->displayptr;
  cpaneld *cpanel = &dsp->cpanel;

  gg->buttondown = 0;

  gdk_window_get_pointer (w->window, &sp->mousepos.x, &sp->mousepos.y,
                          &state);

  gdk_pointer_ungrab (event->time);
  disconnect_motion_signal (sp);

  if (!cpanel->scale.updateAlways_p) {
    displayd *display = sp->displayptr;
    /*-- redisplay this plot --*/
    splot_plane_to_screen (display, &display->cpanel, sp, gg);
    ruler_ranges_set (false, gg->current_display, sp, gg);
    splot_redraw (sp, FULL, gg);
  }
  else {
    splot_redraw (sp, QUICK, gg);
  }

  return retval;
}

void
scale_event_handlers_toggle (splotd * sp, gboolean state)
{
  displayd *display = (displayd *) sp->displayptr;

  if (state == on) {
    if (GGOBI_IS_WINDOW_DISPLAY (display)
        && GGOBI_WINDOW_DISPLAY (display)->useWindow)
      sp->key_press_id =
        g_signal_connect (G_OBJECT (GGOBI_WINDOW_DISPLAY (display)->window),
                          "key_press_event", G_CALLBACK (key_press_cb),
                          (gpointer) sp);


    sp->press_id = g_signal_connect (G_OBJECT (sp->da),
                                     "button_press_event",
                                     G_CALLBACK (button_press_cb),
                                     (gpointer) sp);
    sp->release_id = g_signal_connect (G_OBJECT (sp->da),
                                       "button_release_event",
                                       G_CALLBACK (button_release_cb),
                                       (gpointer) sp);
    sp->scroll_id = g_signal_connect (G_OBJECT (sp->da),
                                       "scroll_event",
                                       G_CALLBACK (scroll_cb),
                                       (gpointer) sp);
  }
  else {
    disconnect_key_press_signal (sp);
    disconnect_button_press_signal (sp);
    disconnect_button_release_signal (sp);
    disconnect_scroll_signal (sp);
  }
}

void
cpanel_scale_make (ggobid * gg)
{
  modepaneld *panel;
  GtkWidget *f, *vb;
  GtkAdjustment *adjx, *adjy;
  GtkWidget *sbarx, *sbary;
  GtkWidget *tgl;

  panel = (modepaneld *) g_malloc (sizeof (modepaneld));
  gg->control_panels = g_list_append (gg->control_panels, (gpointer) panel);
  panel->name = g_strdup (GGOBI (getIModeName) (SCALE));
  panel->w = gtk_vbox_new (false, VBOX_SPACING);
  gtk_container_set_border_width (GTK_CONTAINER (panel->w), 5);

  /*-- frame and vbox for zoom controls --*/
  f = gtk_frame_new ("Zoom");
  gtk_box_pack_start (GTK_BOX (panel->w), f, false, false, 0);

  vb = gtk_vbox_new (true, 0);
  gtk_container_add (GTK_CONTAINER (f), vb);

  /* value, lower, upper, step_increment, page_increment, page_size */
  adjx = (GtkAdjustment *)
    gtk_adjustment_new (log10 (SCALE_DEFAULT), -1.0, 1.0, 0.05, 0.05, 0.0);
  g_object_set_data (G_OBJECT (adjx), "name", "SCALE:x_zoom_adj");
  g_signal_connect (G_OBJECT (adjx), "value_changed",
                    G_CALLBACK (zoom_cb), gg);
  sbarx = gtk_hscale_new (GTK_ADJUSTMENT (adjx));
  gtk_widget_set_name (sbarx, "SCALE:x_zoom");
  scale_set_default_values (GTK_SCALE (sbarx));
  gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), sbarx,
                        "Zoom horizontally", NULL);
  gtk_box_pack_start (GTK_BOX (vb), sbarx, true, true, 0);

  adjy = (GtkAdjustment *)
    gtk_adjustment_new (log10 (SCALE_DEFAULT), -1.0, 1.0, 0.05, 0.05, 0.0);
  g_object_set_data (G_OBJECT (adjy), "name", "SCALE:y_zoom_adj");
  g_signal_connect (G_OBJECT (adjy), "value_changed",
                    G_CALLBACK (zoom_cb), gg);
  sbary = gtk_hscale_new (GTK_ADJUSTMENT (adjy));
  gtk_widget_set_name (sbary, "SCALE:y_zoom");
  scale_set_default_values (GTK_SCALE (sbary));
  gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), sbary,
                        "Zoom vertically", NULL);
  gtk_box_pack_start (GTK_BOX (vb), sbary, true, true, 0);

  tgl = gtk_check_button_new_with_mnemonic ("Fixed _aspect");
  gtk_widget_set_name (tgl, "SCALE:aspect_ratio_tgl");
  gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), tgl,
                        "Fix the aspect ratio while zooming.", NULL);
  g_signal_connect (G_OBJECT (tgl), "toggled",
                    G_CALLBACK (aspect_ratio_cb), (gpointer) gg);
  gtk_box_pack_start (GTK_BOX (vb), tgl, false, false, 3);

  /*-- frame and vbox for pan controls --*/
  f = gtk_frame_new ("Pan");
  gtk_box_pack_start (GTK_BOX (panel->w), f, false, false, 0);

  vb = gtk_vbox_new (true, 0);
  gtk_container_add (GTK_CONTAINER (f), vb);

  /* value, lower, upper, step_increment, page_increment, page_size */
  adjx = (GtkAdjustment *)
    gtk_adjustment_new (0.0, -2 * PRECISION1, 2 * PRECISION1, 200, 400, 0.0);
  g_object_set_data (G_OBJECT (adjx), "name", "SCALE:x_pan_adj");
  g_signal_connect (G_OBJECT (adjx), "value_changed",
                    G_CALLBACK (pan_cb), gg);
  sbarx = gtk_hscale_new (GTK_ADJUSTMENT (adjx));
  gtk_widget_set_name (sbarx, "SCALE:x_pan");
  scale_set_default_values (GTK_SCALE (sbarx));
  gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), sbarx,
                        "Pan horizontally", NULL);
  gtk_box_pack_start (GTK_BOX (vb), sbarx, true, true, 0);

  adjy = (GtkAdjustment *)
    gtk_adjustment_new (0.0, -2 * PRECISION1, 2 * PRECISION1, 200, 400, 0.0);
  g_object_set_data (G_OBJECT (adjy), "name", "SCALE:y_pan_adj");
  g_signal_connect (G_OBJECT (adjy), "value_changed",
                    G_CALLBACK (pan_cb), gg);
  sbary = gtk_hscale_new (GTK_ADJUSTMENT (adjy));
  gtk_widget_set_name (sbary, "SCALE:y_pan");
  scale_set_default_values (GTK_SCALE (sbary));
  gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), sbary,
                        "Pan vertically", NULL);
  gtk_box_pack_start (GTK_BOX (vb), sbary, true, true, 0);


  gtk_widget_show_all (panel->w);
}

/*--------------------------------------------------------------------*/
/*                      Control panel updating                        */
/*--------------------------------------------------------------------*/

void
cpanel_scale_init (cpaneld * cpanel, ggobid * gg)
{

  cpanel->scale.updateAlways_p = true;
  cpanel->scale.fixAspect_p = false;

  cpanel->scale.zoomval.x = log10 (SCALE_DEFAULT);
  cpanel->scale.zoomval.y = log10 (SCALE_DEFAULT);
  cpanel->scale.panval.x = 0.0;
  cpanel->scale.panval.y = 0.0;
}

void
cpanel_scale_set (displayd * display, cpaneld * cpanel, ggobid * gg)
{
  GtkWidget *w;
  GtkWidget *panel =
    mode_panel_get_by_name (GGOBI (getIModeName) (SCALE), gg);
  GtkAdjustment *adj;

  if (panel == (GtkWidget *) NULL)
    return;

  w = widget_find_by_name (panel, "SCALE:aspect_ratio_tgl");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w),
                                cpanel->scale.fixAspect_p);

  /*  One by one, find the adjustments and reset them */
  adj = scale_adjustment_find_by_name ("SCALE:x_zoom", gg);
  gtk_adjustment_set_value (adj, cpanel->scale.zoomval.x);
  adj = scale_adjustment_find_by_name ("SCALE:y_zoom", gg);
  gtk_adjustment_set_value (adj, cpanel->scale.zoomval.y);
  adj = scale_adjustment_find_by_name ("SCALE:x_pan", gg);
  gtk_adjustment_set_value (adj, cpanel->scale.panval.x);
  adj = scale_adjustment_find_by_name ("SCALE:y_pan", gg);
  gtk_adjustment_set_value (adj, cpanel->scale.panval.y);
}