aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/table/e-cell-text.c
blob: 79fe4323263b9c960920b341677e037dc0b5a794 (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
/*
 * e-cell-text.c: Text cell renderer
 *
 * Author:
 *   Miguel de Icaza (miguel@kernel.org)
 *
 * (C) 1999 Helix Code, Inc
 */
#include <config.h>
#include <gtk/gtkenums.h>
#include <gtk/gtkentry.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtksignal.h>
#include <gdk/gdkkeysyms.h>
#include <libgnomeui/gnome-canvas.h>
#include <stdio.h>
#include "e-cell-text.h"
#include "e-util.h"
#include "e-table-item.h"

#define PARENT_TYPE e_cell_get_type()

#define TEXT_PAD 2

typedef struct {
    char         *old_text;
    GtkWidget    *entry_top;
    GtkEntry     *entry;

    /*
     * Where the editing is taking place
     */
    int           model_col, view_col, row;
} CellEdit;

typedef struct {
    ECellView    cell_view;
    GdkGC       *gc;
    GdkFont     *font;
    GnomeCanvas *canvas;
    ETableItem  *eti;
    
    /*
     * During edition.
     */
    CellEdit    *edit;
} ECellTextView;

static ECellClass *parent_class;

static void
ect_queue_redraw (ECellTextView *text_view, int view_col, int view_row)
{
    e_table_item_redraw_range (text_view->eti, view_col, view_row, view_col, view_row);
}

/*
 * Accept the currently edited text
 */
static void
ect_accept_edits (ECellTextView *text_view)
{
    const char *text = gtk_entry_get_text (text_view->edit->entry);
    CellEdit *edit = text_view->edit;
    
    e_table_model_set_value_at (text_view->cell_view.table_model, edit->model_col, edit->row, text);
}

/*
 * Shuts down the editing process
 */
static void
ect_stop_editing (ECellTextView *text_view)
{
    CellEdit *edit = text_view->edit;
    
    g_free (edit->old_text);
    edit->old_text = NULL;
    gtk_widget_destroy (edit->entry_top);
    edit->entry_top = NULL;
    edit->entry = NULL;

    g_free (edit);
    
    text_view->edit = NULL;

    e_table_item_leave_edit (text_view->eti);
}

/*
 * Cancels the edits
 */
static void
ect_cancel_edit (ECellTextView *text_view)
{
    ect_queue_redraw (text_view, text_view->edit->view_col, text_view->edit->row);
    ect_stop_editing (text_view);
}

/*
 * ECell::realize method
 */
static ECellView *
ect_realize (ECell *ecell, ETableModel *table_model, void *view)
{
    ECellText *ect = E_CELL_TEXT (ecell);
    ECellTextView *text_view = g_new0 (ECellTextView, 1);
    ETableItem *eti = E_TABLE_ITEM (view);
    GnomeCanvas *canvas = GNOME_CANVAS_ITEM (eti)->canvas;
    
    text_view->cell_view.ecell = ecell;
    text_view->cell_view.table_model = table_model;
    
    text_view->gc = gdk_gc_new (GTK_WIDGET (canvas)->window);
    if (ect->font_name){
        GdkFont *f;

        f = gdk_fontset_load (ect->font_name);
        text_view->font = f;
    }
    if (!text_view->font){
        text_view->font = GTK_WIDGET (canvas)->style->font;
        
        gdk_font_ref (text_view->font);
    }

    text_view->eti = eti;
    text_view->canvas = canvas;
    
    return (ECellView *)text_view;
}

/*
 * ECell::unrealize method
 */
static void
ect_unrealize (ECellView *ecv)
{
    ECellTextView *text_view = (ECellTextView *) ecv;

    gdk_gc_unref (text_view->gc);
    text_view->gc = NULL;

    gdk_font_unref (text_view->font);
    text_view->font = NULL;

    g_free (text_view);
}

/*
 * ECell::draw method
 */
static void
ect_draw (ECellView *ecell_view, GdkDrawable *drawable,
      int model_col, int view_col, int row, gboolean selected,
      int x1, int y1, int x2, int y2)
{
    ECellText *ect = E_CELL_TEXT (ecell_view->ecell);
    ECellTextView *text_view = (ECellTextView *) ecell_view;
    GtkWidget *w = GTK_WIDGET (text_view->canvas);
    GdkRectangle rect;
    const char *str = e_table_model_value_at (ecell_view->table_model, model_col, row);
    GdkFont *font = text_view->font;
    const int height = font->ascent + font->descent;
    int xoff;
    gboolean edit_display = FALSE;

    /*
     * Figure if this cell is being edited
     */
    if (text_view->edit){
        CellEdit *edit = text_view->edit;
        
        if ((edit->view_col == view_col) && (edit->row == row))
            edit_display = TRUE;
    }

    /*
     * Be a nice citizen: clip to the region we are supposed to draw on
     */
    rect.x = x1;
    rect.y = y1;
    rect.width = x2 - x1;
    rect.height = y2 - y1;
    gdk_gc_set_clip_rectangle (text_view->gc, &rect);

    if (edit_display){
        CellEdit *edit = text_view->edit;
        const char *text = gtk_entry_get_text (edit->entry);
        GdkWChar *p, *text_wc = g_new (GdkWChar, strlen (text) + 1);
        int text_wc_len = gdk_mbstowcs (text_wc, text, strlen (text));
        const int cursor_pos = GTK_EDITABLE (edit->entry)->current_pos;
        const int left_len = gdk_text_width_wc (text_view->font, text_wc, cursor_pos);

        text_wc [text_wc_len] = 0;
        /*
         * Paint
         */
        gdk_gc_set_foreground (text_view->gc, &w->style->base [GTK_STATE_NORMAL]);
        gdk_draw_rectangle (drawable, text_view->gc, TRUE,
                    rect.x, rect.y, rect.width, rect.height);
        gdk_gc_set_foreground (text_view->gc, &w->style->text [GTK_STATE_NORMAL]);

        {
            GdkGC *gc = text_view->gc;
            const int y = y2 - font->descent - ((y2-y1-height)/2);
            int px, i;

            /*
             * Border
             */
            x1 += 2;
            x2--;
            
            px = x1;

            /*
             * If the cursor is outside the visible range
             *
             * FIXME: we really want a better behaviour.
             */
            if ((px + left_len) > x2)
                px -= left_len - (x2-x1);
                
            for (i = 0, p = text_wc; *p; p++, i++){
                gdk_draw_text_wc (
                    drawable, font, gc, px, y, p, 1);

                if (i == cursor_pos){
                    gdk_draw_line (
                        drawable, gc,
                        px, y - font->ascent,
                        px, y + font->descent - 1);
                }

                px += gdk_text_width_wc (font, p, 1);
            }

            if (i == cursor_pos){
                gdk_draw_line (
                    drawable, gc,
                    px, y - font->ascent,
                    px, y + font->descent - 1);
            }
        }
        g_free (text_wc);
    } else {
        /*
         * Regular cell
         */
        GdkColor *background, *foreground;
        int width;

        /*
         * Border
         */
        x1++;
        x2--;
        
        /*
         * Compute draw mode
         */
        switch (ect->justify){
        case GTK_JUSTIFY_LEFT:
            xoff = 1;
            break;
            
        case GTK_JUSTIFY_RIGHT:
            width = 1 + gdk_text_width (font, str, strlen (str));
            xoff = (x2 - x1) - width;
            break;
            
        case GTK_JUSTIFY_CENTER:
            xoff = ((x2 - x1) - gdk_text_width (font, str, strlen (str))) / 2;
            break;
        default:
            xoff = 0;
            g_warning ("Can not handle GTK_JUSTIFY_FILL");
            break;
        }
        
        
        if (selected){
            background = &w->style->bg [GTK_STATE_SELECTED];
            foreground = &w->style->text [GTK_STATE_SELECTED];
        } else {
            background = &w->style->base [GTK_STATE_NORMAL];
            foreground = &w->style->text [GTK_STATE_NORMAL];
        }
        
        gdk_gc_set_foreground (text_view->gc, background); 
        gdk_draw_rectangle (drawable, text_view->gc, TRUE,
                    rect.x, rect.y, rect.width, rect.height);
        gdk_gc_set_foreground (text_view->gc, foreground);

        gdk_draw_string (
            drawable, font, text_view->gc,
            x1 + xoff,
            y2 - font->descent - ((y2-y1-height)/2), str);
    }
}

/*
 * Selects the entire string
 */
static void
ect_edit_select_all (ECellTextView *text_view)
{
    g_assert (text_view->edit);
    
    gtk_editable_select_region (GTK_EDITABLE (text_view->edit->entry), 0, -1);
}

/*
 * ECell::event method
 */
static gint
ect_event (ECellView *ecell_view, GdkEvent *event, int model_col, int view_col, int row)
{
    ECellTextView *text_view = (ECellTextView *) ecell_view;
    
    switch (event->type){
    case GDK_BUTTON_PRESS:
        /*
         * Adjust for the border we use
         */
        event->button.x++;
        
        printf ("Button pressed at %g %g\n", event->button.x, event->button.y);
        if (text_view->edit){
            printf ("FIXME: Should handle click here\n");
        } else 
            e_table_item_enter_edit (text_view->eti, view_col, row);
        break;

    case GDK_BUTTON_RELEASE:
        /*
         * Adjust for the border we use
         */
        event->button.x++;
        printf ("Button released at %g %g\n", event->button.x, event->button.y);
        return TRUE;

    case GDK_KEY_PRESS:
        if (event->key.keyval == GDK_Escape){
            ect_cancel_edit (text_view);
            return TRUE;
        }
        
        if (!text_view->edit){
            e_table_item_enter_edit (text_view->eti, view_col, row);
            ect_edit_select_all (text_view);
        }

        gtk_widget_event (GTK_WIDGET (text_view->edit->entry), event);
        ect_queue_redraw (text_view, view_col, row);
        break;
        
    case GDK_KEY_RELEASE:
        break;
        
    default:
        return FALSE;
    }
    return TRUE;
}

/*
 * ECell::height method
 */
static int
ect_height (ECellView *ecell_view, int model_col, int view_col, int row) 
{
    ECellTextView *text_view = (ECellTextView *) ecell_view;
    
    return (text_view->font->ascent + text_view->font->descent) + TEXT_PAD;
}

/*
 * Callback: invoked when the user pressed "enter" on the GtkEntry
 */
static void
ect_entry_activate (GtkEntry *entry, ECellTextView *text_view)
{
    e_table_item_leave_edit (text_view->eti);
}

/*
 * ECellView::enter_edit method
 */
static void *
ect_enter_edit (ECellView *ecell_view, int model_col, int view_col, int row)
{
    ECellTextView *text_view = (ECellTextView *) ecell_view;
    const char *str = e_table_model_value_at (ecell_view->table_model, model_col, row);
    CellEdit *edit;

    edit = g_new (CellEdit, 1);
    text_view->edit = edit;

    edit->model_col = model_col;
    edit->view_col = view_col;
    edit->row = row;
    
    edit->entry = (GtkEntry *) gtk_entry_new ();
    gtk_entry_set_text (edit->entry, str);
    edit->old_text = g_strdup (str);
    gtk_signal_connect (GTK_OBJECT (edit->entry), "activate",
                GTK_SIGNAL_FUNC (ect_entry_activate), text_view);

    /*
     * The hack: create this window off-screen
     */
    edit->entry_top = gtk_window_new (GTK_WINDOW_POPUP);
    gtk_container_add (GTK_CONTAINER (edit->entry_top), GTK_WIDGET (edit->entry));
    gtk_widget_set_uposition (edit->entry_top, 20000, 20000);
    gtk_widget_show_all (edit->entry_top);

    ect_queue_redraw (text_view, view_col, row);
    
    return NULL;
}

/*
 * ECellView::leave_edit method
 */
static void
ect_leave_edit (ECellView *ecell_view, int model_col, int view_col, int row, void *edit_context)
{
    ECellTextView *text_view = (ECellTextView *) ecell_view;

    if (text_view->edit){
        ect_accept_edits (text_view);
        ect_stop_editing (text_view);
    } else {
        /*
         * We did invoke this leave edit internally
         */
    }
}

/*
 * GtkObject::destroy method
 */
static void
ect_destroy (GtkObject *object)
{
    ECellText *ect = E_CELL_TEXT (object);

    g_free (ect->font_name);

    GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

static void
e_cell_text_class_init (GtkObjectClass *object_class)
{
    ECellClass *ecc = (ECellClass *) object_class;

    object_class->destroy = ect_destroy;
    
    ecc->realize    = ect_realize;
    ecc->unrealize  = ect_unrealize;
    ecc->draw       = ect_draw;
    ecc->event      = ect_event;
    ecc->height     = ect_height;
    ecc->enter_edit = ect_enter_edit;
    ecc->leave_edit = ect_leave_edit;

    parent_class = gtk_type_class (PARENT_TYPE);
}

E_MAKE_TYPE(e_cell_text, "ECellText", ECellText, e_cell_text_class_init, NULL, PARENT_TYPE);

ECell *
e_cell_text_new (ETableModel *etm, const char *fontname, GtkJustification justify)
{
    ECellText *ect = gtk_type_new (e_cell_text_get_type ());

    ect->font_name = g_strdup (fontname);
    ect->justify = justify;
      
    return (ECell *) ect;
}