aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/e-table/e-cell-text.c
blob: da263026649e4a75217e0082b4c7853c2782c2dd (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
/*
 * 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 "e-cell-text.h"
#include "e-util.h"

#define PARENT_TYPE e_cell_get_type()

#define TEXT_PAD 2

typedef struct {
    ECellView    cell_view;
    GdkGC       *gc;
    GdkFont     *font;
    GnomeCanvas *canvas;
} ECellTextView;

static ECellClass *parent_class;

static ECellView *
ect_realize (ECell *ecell, GnomeCanvas *canvas)
{
    ECellText *ect = E_CELL_TEXT (ecell);
    ECellTextView *ectv = g_new0 (ECellTextView, 1);

    ectv->cell_view.ecell = ecell;
    ectv->gc = gdk_gc_new (GTK_WIDGET (canvas)->window);
    if (ect->font_name){
        GdkFont *f;

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

    return (ECellView *)ectv;
}

static void
ect_unrealize (ECellView *ecv)
{
    ECellTextView *ectv = (ECellTextView *) ecv;

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

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

    g_free (ectv);
}

static void
ect_draw (ECellView *ecell_view, GdkDrawable *drawable, int col, int row, int x1, int y1, int x2, int y2)
{
    ECellText *ect = E_CELL_TEXT (ecell_view->ecell);
    ECellTextView *text_view = (ECellTextView *) ecell_view;
    GdkRectangle rect;
    const char *str = e_table_model_value_at (ecell_view->ecell->table_model, col, row);
    int selected = e_table_model_get_selected_row (ecell_view->ecell->table_model) == row;
    int xoff, w;
        
    rect.x = x1;
    rect.y = y1;
    rect.width = x2 - x1;
    rect.height = y2 - y1;
    
    gdk_gc_set_clip_rectangle (text_view->gc, &rect);

    switch (ect->justify){
    case GTK_JUSTIFY_LEFT:
        xoff = 1;
        break;
        
    case GTK_JUSTIFY_RIGHT:
        w = 1 + gdk_text_width (text_view->font, str, strlen (str));
        xoff = (x2 - x1) - w;
        break;
        
    case GTK_JUSTIFY_CENTER:
        xoff = ((x2 - x1) - gdk_text_width (text_view->font, str, strlen (str))) / 2;
        break;
    default:
        g_warning ("Can not handle GTK_JUSTIFY_FILL");
        break;
    }

    /* Draw now */
    {
        GtkWidget *w = GTK_WIDGET (text_view->canvas);
        GdkColor *background, *foreground;
        const int height = text_view->font->ascent + text_view->font->descent;
        
        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, text_view->font, text_view->gc,
                 x1 + xoff, y2 - text_view->font->descent - ((y2-y1-height)/2), str);
    }
}

static void
e_cell_text_start_editing (ECellText *ect, int col, int row)
{
    printf ("Starting to edit %d %d\n", col, row);
}

static gint
ect_event (ECellView *ecell_view, GdkEvent *event, int col, int row)
{
    ECell *ecell = ecell_view->ecell;
    ECellText *ect = E_CELL_TEXT (ecell);
    
    switch (event->type){
    case GDK_BUTTON_PRESS:
        if (e_table_model_get_selected_row (ecell->table_model) == row)
            e_cell_text_start_editing (ect, col, row);
        else
            e_table_model_select_row (ecell->table_model, row);
        return TRUE;
    }
}

static int
ect_height (ECellView *ecell_view, int col, int row)
{
    ECellTextView *text_view = (ECellTextView *) ecell_view;
    
    return (text_view->font->ascent + text_view->font->descent) + TEXT_PAD;
}

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;
    
    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;
    E_CELL (ect)->table_model = etm;
      
    return (ECell *) ect;
}