aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/table/e-cell-spin-button.c
blob: 9a4d935d601b82862f8da5453a1515926ab81bbb (plain) (blame)
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * e-cell-spin-button.c: Spin button item for e-table.
 * Copyright 2001, CodeFactory AB
 * Copyright 2001, Mikael Hallendal <micke@codefactory.se>
 *
 * Authors:
 *   Mikael Hallendal <micke@codefactory.se>
 *
 * Celltype for drawing a spinbutton in a cell. 
 *
 * Used ECellPopup by Damon Chaplin <damon@ximian.com> as base for 
 * buttondrawings.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License, version 2, as published by the Free Software Foundation.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include <config.h>

#include <gtk/gtk.h>

#include "table/e-cell-float.h"
#include "table/e-cell-number.h"
#include "table/e-table-item.h"
#include "table/e-table-model.h"
#include "e-util/e-util.h"

#include "e-cell-spin-button.h"

#define E_CELL_SPIN_BUTTON_ARROW_WIDTH  16
#define PARENT_TYPE e_cell_get_type ()

static void e_cell_spin_button_class_init   (GObjectClass   *klass);
static void e_cell_spin_button_init         (GtkObject        *object);

static void         ecsb_dispose      (GObject  *object);

/* ECell Functions */
static ECellView *  ecsb_new_view     (ECell                 *ecell,
                       ETableModel           *etm,
                       void                  *eti_view);
static void         ecsb_realize      (ECellView             *ecv);
static void         ecsb_kill_view    (ECellView             *ecv);
static void         ecsb_unrealize    (ECellView             *ecv);
static void         ecsb_draw         (ECellView             *ecv, 
                       GdkDrawable           *drawable, 
                       int                    model_col, 
                       int                    view_col, 
                       int                    row, 
                       ECellFlags             flags, 
                       int                    x1, 
                       int                    y1, 
                       int                    x2, 
                       int                    y2); 

static gint         ecsb_event        (ECellView             *ecv, 
                       GdkEvent              *event, 
                       int                    model_col, 
                       int                    view_col, 
                       int                    row,
                       ECellFlags             flags, 
                       ECellActions          *actions); 

static gint         ecsb_height       (ECellView             *ecv,
                       int                    model_col,
                       int                    view_col,
                       int                    row);

static void *       ecsb_enter_edit   (ECellView             *ecv,
                       int                    model_col, 
                       int                    view_col, 
                       int                    row);

static void         ecsb_leave_edit   (ECellView             *ecv,
                       int                    model_col, 
                       int                    view_col, 
                       int                    row, 
                       void                  *context);
static void         ecsb_focus        (ECellView             *ecell_view,
                       int                    model_col,
                       int                    view_col,
                       int                    row,
                       int                    x1,
                       int                    y1,
                       int                    x2,
                       int                    y2);
static void         ecsb_unfocus      (ECellView             *ecell_view);

static void         ecsb_show_tooltip (ECellView             *ecv,
                       int                    model_col, 
                       int                    view_col, 
                       int                    row, 
                       int                    col_width, 
                       ETableTooltip          *tooltip);

typedef struct {
    ECellView    cell_view;
    
    ECellView   *child_view;
} ECellSpinButtonView;

enum {
    STEP,
    LAST_SIGNAL
};

static guint    signals[LAST_SIGNAL] = { 0 };
static ECell   *parent_class;

static void 
e_cell_spin_button_class_init     (GObjectClass   *klass)
{
        ECellClass             *ecc    = (ECellClass *) klass;  
    ECellSpinButtonClass   *ecsbc  = (ECellSpinButtonClass *) klass;
        
        klass->dispose     = ecsb_dispose;

    ecc->realize       = ecsb_realize;
    ecc->unrealize     = ecsb_unrealize;
    ecc->new_view      = ecsb_new_view;
    ecc->kill_view     = ecsb_kill_view;
    ecc->draw          = ecsb_draw;
    ecc->event         = ecsb_event;
    ecc->height        = ecsb_height;
    ecc->enter_edit    = ecsb_enter_edit;
    ecc->leave_edit    = ecsb_leave_edit;
    ecc->focus         = ecsb_focus;
    ecc->unfocus       = ecsb_unfocus;  ecc->print         = NULL;
    ecc->print_height  = NULL;
    ecc->max_width     = NULL;
    ecc->show_tooltip  = ecsb_show_tooltip;

    ecsbc->step        = NULL;

        parent_class       = g_type_class_ref (E_CELL_TYPE);

    signals[STEP] =
        g_signal_new ("step",
                  G_OBJECT_CLASS_TYPE (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (ECellSpinButtonClass, step),
                  NULL, NULL,
                  e_util_marshal_NONE__POINTER_INT_INT_INT,
                  G_TYPE_NONE,
                  4, G_TYPE_POINTER, G_TYPE_INT, 
                  G_TYPE_INT, G_TYPE_INT);
}

static void 
e_cell_spin_button_init           (GtkObject        *object)
{
        ECellSpinButton   *ecsb;
    
    g_return_if_fail (object != NULL);
    g_return_if_fail (M_IS_CELL_SPIN_BUTTON (object));
    
    ecsb = E_CELL_SPIN_BUTTON (object);

    ecsb->up_pressed    = FALSE;
    ecsb->down_pressed  = FALSE;
}

static ECellView *  
ecsb_new_view     (ECell            *ecell,
           ETableModel      *etm,
           void             *eti_view)
{
    ECellSpinButton       *ecsb = E_CELL_SPIN_BUTTON (ecell);
    ECellSpinButtonView   *ecsb_view;

    g_return_val_if_fail (ecsb->child != NULL, NULL);

    ecsb_view = g_new0 (ECellSpinButtonView, 1);

    ecsb_view->cell_view.ecell = ecell;
    ecsb_view->cell_view.e_table_model = etm;
    ecsb_view->cell_view.e_table_item_view = eti_view;

    ecsb_view->child_view = e_cell_new_view (ecsb->child, etm, eti_view);

    return (ECellView *) ecsb_view;
}

static void         
ecsb_realize      (ECellView        *ecv)
{ 
    ECellSpinButtonView   *ecsb_view;
    
    g_return_if_fail (ecv != NULL);

    ecsb_view = (ECellSpinButtonView *) ecv;

    e_cell_realize (ecsb_view->child_view);
} 

static void         
ecsb_kill_view    (ECellView        *ecv)
{
    ECellSpinButtonView   *ecsb_view;
    
    g_return_if_fail (ecv != NULL);

    ecsb_view = (ECellSpinButtonView *) ecv;

    if (ecsb_view->child_view) {
        e_cell_kill_view (ecsb_view->child_view);
    }
    
    g_free (ecsb_view);
}

static void         
ecsb_unrealize    (ECellView        *ecv)
{ 
    ECellSpinButtonView   *ecsb_view;
    
    g_return_if_fail (ecv != NULL);

    ecsb_view = (ECellSpinButtonView *) ecv;

    e_cell_unrealize (ecsb_view->child_view);
}

static void         
ecsb_draw         (ECellView        *ecv, 
           GdkDrawable      *drawable, 
           int               model_col, 
           int               view_col, 
           int               row, 
           ECellFlags        flags, 
           int               x1, 
           int               y1, 
           int               x2, 
           int               y2)
{
    ECellSpinButton        *ecsb;
    ECellSpinButtonView    *ecsb_view;
    ETableItem             *eti;
    GtkWidget              *canvas;
    GtkShadowType           shadow = GTK_SHADOW_OUT;
    GdkRectangle            rect;
    
    g_return_if_fail (ecv != NULL);
    
    ecsb_view   = (ECellSpinButtonView *) ecv;
    ecsb        = E_CELL_SPIN_BUTTON (ecsb_view->cell_view.ecell);
    
    eti         = E_TABLE_ITEM (ecsb_view->cell_view.e_table_item_view);
    canvas      = GTK_WIDGET (GNOME_CANVAS_ITEM (eti)->canvas);
    
    if (eti->editing_col == view_col && 
        eti->editing_row == row) {

        /* Draw child (Whats shown under the buttons) */
        e_cell_draw (ecsb_view->child_view, 
                 drawable, model_col, view_col,
                 row, flags, 
                 x1, y1, 
                 x2 - E_CELL_SPIN_BUTTON_ARROW_WIDTH, y2); 

        /* Draw down-arrow */
        rect.x       = x2 - E_CELL_SPIN_BUTTON_ARROW_WIDTH;
        rect.y       = y1 + (y2 - y1) / 2;
        rect.width   = E_CELL_SPIN_BUTTON_ARROW_WIDTH;
        rect.height  = (y2 - y1) / 2;
        
        if (ecsb->down_pressed) {
            shadow = GTK_SHADOW_IN;
        } else {
            shadow = GTK_SHADOW_OUT;
        }
        
        gtk_paint_box (canvas->style, drawable, 
                   GTK_STATE_NORMAL, shadow,
                   &rect, canvas, "ecellspinbutton_down",
                   rect.x, rect.y, rect.width, rect.height);

        gtk_paint_arrow (canvas->style, drawable,
                 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
                 &rect, canvas, NULL,
                 GTK_ARROW_DOWN, TRUE,
                 rect.x,
                 rect.y,
                 rect.width,
                 rect.height);

        /* Draw up-arrow */
        rect.y       = y1;
        
        if (ecsb->up_pressed) {
            shadow = GTK_SHADOW_IN;
        } else {
            shadow = GTK_SHADOW_OUT;
        }

        gtk_paint_box (canvas->style, drawable, 
                   GTK_STATE_NORMAL, shadow,
                   &rect, canvas, "ecellspinbutton_up",
                   rect.x, rect.y, rect.width, rect.height);

        gtk_paint_arrow (canvas->style, drawable,
                 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
                 &rect, canvas, NULL,
                 GTK_ARROW_UP, TRUE,
                 rect.x,
                 rect.y,
                 rect.width,
                 rect.height);
    } else {
        /* Draw child */
        e_cell_draw (ecsb_view->child_view, 
                 drawable, model_col, view_col,
                 row, flags, 
                 x1, y1, 
                 x2, y2); 
    }
}

static gint         
ecsb_event        (ECellView        *ecv, 
           GdkEvent         *event, 
           int               model_col, 
           int               view_col, 
           int               row,
           ECellFlags        flags, 
           ECellActions     *actions)
{
    ECellSpinButton       *ecsb;
    ECellSpinButtonView   *ecsb_view;
    ETableItem            *eti;
    gint                   height, width;
    
    g_return_val_if_fail (ecv != NULL, FALSE);
        
    ecsb_view   = (ECellSpinButtonView *) ecv;
    ecsb        = E_CELL_SPIN_BUTTON (ecsb_view->cell_view.ecell);
    eti         = E_TABLE_ITEM (ecsb_view->cell_view.e_table_item_view);
    
    switch (event->type) {
    case GDK_BUTTON_PRESS:
        if (eti->editing_col == view_col &&
            eti->editing_row == row) {
            width = e_table_header_col_diff (eti->header,
                             view_col,
                             view_col + 1);
            height = e_table_item_row_diff (eti, row, row + 1);

            /* Check if inside a button */
            if (event->button.x >= width - E_CELL_SPIN_BUTTON_ARROW_WIDTH) {
                /* Yep, which one? */
                if (event->button.y <= height / 2) {
                    ecsb->up_pressed = TRUE;
                    g_signal_emit (ecsb,
                               signals[STEP], 0,
                               ecv,
                               STEP_UP,
                               view_col,
                               row);
                } else {
                    ecsb->down_pressed = TRUE;
                    g_signal_emit (ecsb,
                               signals[STEP], 0,
                               ecv,
                               STEP_DOWN,
                               view_col,
                               row);
                }

                e_table_item_redraw_range (eti,
                               view_col,
                               row,
                               view_col,
                               row);
                
            }
        } 
        
        break;
    case GDK_BUTTON_RELEASE:
        ecsb->up_pressed = FALSE;
        ecsb->down_pressed = FALSE;
        e_table_item_redraw_range (eti,
                       view_col,
                       row,
                       view_col,
                       row);
        break;
    case GDK_KEY_PRESS:
        break;
    default:
        break;
    }

    return e_cell_event (ecsb_view->child_view, event, model_col,
                 view_col, row, flags, actions);
}

static gint         
ecsb_height       (ECellView        *ecv,
           int               model_col,
           int               view_col,
           int               row)
{
    ECellSpinButtonView   *ecsb_view;
    
    g_return_val_if_fail (ecv != NULL, -1);
    
    ecsb_view = (ECellSpinButtonView *) ecv;

    return e_cell_height (ecsb_view->child_view, model_col, view_col, row);
}

static void *       
ecsb_enter_edit   (ECellView        *ecv,
           int               model_col, 
           int               view_col, 
           int               row)
{
    ECellSpinButtonView   *ecsb_view;
    
    g_return_val_if_fail (ecv != NULL, NULL);

    ecsb_view = (ECellSpinButtonView *) ecv;

    return e_cell_enter_edit (ecsb_view->child_view, model_col,
                  view_col, row);
}


static void         
ecsb_leave_edit   (ECellView        *ecv,
           int               model_col, 
           int               view_col, 
           int               row, 
           void             *context)
{
    ECellSpinButtonView   *ecsb_view;
    
    g_return_if_fail (ecv != NULL);
    
    ecsb_view = (ECellSpinButtonView *) ecv;

    e_cell_leave_edit (ecsb_view->child_view, model_col, view_col, 
               row, context);
}

static void
ecsb_focus        (ECellView        *ecell_view,
           int               model_col,
           int               view_col,
           int               row,
           int           x1,
           int           y1,
           int           x2,
           int           y2)
{
    ECellClass  *klass;
    
    klass = E_CELL_GET_CLASS (ecell_view->ecell);

    if (klass->focus)
        klass->focus (ecell_view, model_col, view_col, row, 
                  x1, y1, x2, y2);
}

static void
ecsb_unfocus      (ECellView        *ecell_view)
{
    ECellClass  *klass;
    
    klass = E_CELL_GET_CLASS (ecell_view->ecell);

    if (klass->unfocus)
        klass->unfocus (ecell_view);
}

static void         
ecsb_show_tooltip (ECellView        *ecv,
           int               model_col, 
           int               view_col, 
           int               row, 
           int               col_width, 
           ETableTooltip    *tooltip)
{
    ECellSpinButtonView   *ecsb_view;
    
    g_return_if_fail (ecv != NULL);

    ecsb_view = (ECellSpinButtonView *) ecv;
    
    e_cell_show_tooltip (ecsb_view->child_view, model_col, view_col, 
                 row, col_width, tooltip);
}

static void 
ecsb_dispose (GObject   *object)
{
    g_return_if_fail (object != NULL);
    g_return_if_fail (M_IS_CELL_SPIN_BUTTON (object));
    
    G_OBJECT_CLASS (parent_class)->dispose (object);
}

ECell *
e_cell_spin_button_new (gint     min,
            gint     max,
            gint     step,
            ECell   *child_cell)
{
    ECellSpinButton   *ecsb;
    
    ecsb = g_object_new (E_CELL_SPIN_BUTTON_TYPE, NULL);

    if (!child_cell) {
        child_cell = e_cell_number_new (NULL, 
                        GTK_JUSTIFY_LEFT);
        
        g_signal_connect (ecsb, "step",
                  G_CALLBACK (e_cell_spin_button_step),
                  NULL);
    }
    
    ecsb->child   = child_cell;
    ecsb->min.i   = min;
    ecsb->max.i   = max;
    ecsb->step.i  = step;

    return E_CELL (ecsb);
}

ECell *
e_cell_spin_button_new_float (gfloat    min,
                  gfloat    max,
                  gfloat    step,
                  ECell    *child_cell)
{
    ECellSpinButton   *ecsb;
    
    ecsb = g_object_new (E_CELL_SPIN_BUTTON_TYPE, NULL);

    if (!child_cell) {
        child_cell = e_cell_float_new (NULL, GTK_JUSTIFY_LEFT);
        g_signal_connect (ecsb, "step",
                  G_CALLBACK (e_cell_spin_button_step_float),
                  NULL);
    }
    
    ecsb->child   = child_cell;
    ecsb->min.f   = min;
    ecsb->max.f   = max;
    ecsb->step.f  = step;

    return E_CELL (ecsb);
}

void
e_cell_spin_button_step   (ECellSpinButton       *ecsb,
               ECellView             *ecv,
               ECellSpinButtonStep    direction,
               gint                   col,
               gint                   row)
{
    ECellSpinButtonView   *ecsb_view; 
    
    ETableModel           *etm;
    gint                   value;
    gint                   new_value;
    gchar                 *str_value;
    
    g_return_if_fail (ecsb != NULL);
    g_return_if_fail (M_IS_CELL_SPIN_BUTTON (ecsb));
    g_return_if_fail (ecv != NULL);
    
    ecsb_view  = (ECellSpinButtonView *) ecv; 
    etm        = ecsb_view->cell_view.e_table_model;
    
    value  = GPOINTER_TO_INT (e_table_model_value_at (etm, col, row));
    new_value = value;
    
    switch (direction) {
    case STEP_UP:
        new_value = CLAMP (value + ecsb->step.i, 
                   ecsb->min.i, ecsb->max.i);
        break;
    case STEP_DOWN:
        new_value = CLAMP (value - ecsb->step.i, 
                   ecsb->min.i, ecsb->max.i);
        break;
    default:
        break;
    };

    str_value = g_strdup_printf ("%d", new_value);

    e_table_model_set_value_at (etm, col, row, str_value);

    g_free (str_value);
}

void
e_cell_spin_button_step_float  (ECellSpinButton       *ecsb,
                ECellView             *ecv,
                ECellSpinButtonStep    direction,
                gint                   col,
                gint                   row)
{
    ECellSpinButtonView   *ecsb_view; 
    
    ETableModel           *etm;
    gfloat                 value;
    gfloat                 new_value;
    gchar                 *str_value;
    
    g_return_if_fail (ecsb != NULL);
    g_return_if_fail (M_IS_CELL_SPIN_BUTTON (ecsb));
    g_return_if_fail (ecv != NULL);
    
    ecsb_view  = (ECellSpinButtonView *) ecv; 
    etm        = ecsb_view->cell_view.e_table_model;
    
    value  = *(gfloat *) e_table_model_value_at (etm, col, row);

    switch (direction) {
    case STEP_UP:
        new_value = CLAMP (value + ecsb->step.f, 
                   ecsb->min.f, ecsb->max.f);
        break;
    case STEP_DOWN:
        new_value = CLAMP (value - ecsb->step.f, 
                   ecsb->min.f, ecsb->max.f);
        break;
    default:
        new_value = value;
        break;
    };
    
    str_value = g_strdup_printf ("%f", new_value);

    e_table_model_set_value_at (etm, col, row, str_value);
    
    g_free (str_value);
}

E_MAKE_TYPE (e_cell_spin_button, "ECellSpinButton", ECellSpinButton, 
         e_cell_spin_button_class_init, e_cell_spin_button_init, 
         PARENT_TYPE)