Commit 27a0ca6513dec8b4463103d9ad05da518dcd36b7
1 parent
18eb35eb
Exists in
master
and in
1 other branch
Implementing font chooser widget.
Showing
1 changed file
with
93 additions
and
9 deletions
Show diff stats
src/dialogs/font/chooser.c
| @@ -38,6 +38,7 @@ | @@ -38,6 +38,7 @@ | ||
| 38 | #include <v3270/dialogs.h> | 38 | #include <v3270/dialogs.h> |
| 39 | #include <v3270/settings.h> | 39 | #include <v3270/settings.h> |
| 40 | #include <lib3270/log.h> | 40 | #include <lib3270/log.h> |
| 41 | + #include <terminal.h> | ||
| 41 | 42 | ||
| 42 | /*--[ Widget definition ]----------------------------------------------------------------------------*/ | 43 | /*--[ Widget definition ]----------------------------------------------------------------------------*/ |
| 43 | 44 | ||
| @@ -55,6 +56,10 @@ | @@ -55,6 +56,10 @@ | ||
| 55 | GtkWidget * font_list; | 56 | GtkWidget * font_list; |
| 56 | GtkWidget * preview; | 57 | GtkWidget * preview; |
| 57 | 58 | ||
| 59 | + struct { | ||
| 60 | + cairo_font_face_t * face; | ||
| 61 | + } font; | ||
| 62 | + | ||
| 58 | } V3270FontChooserWidget; | 63 | } V3270FontChooserWidget; |
| 59 | 64 | ||
| 60 | typedef struct _V3270HostSelectWidgetClass | 65 | typedef struct _V3270HostSelectWidgetClass |
| @@ -97,10 +102,24 @@ static void load(GtkWidget *widget, GtkWidget *terminal) | @@ -97,10 +102,24 @@ static void load(GtkWidget *widget, GtkWidget *terminal) | ||
| 97 | 102 | ||
| 98 | } | 103 | } |
| 99 | 104 | ||
| 105 | +static void dispose(GObject *object) | ||
| 106 | +{ | ||
| 107 | + V3270FontChooserWidget * widget = GTK_V3270_FONT_CHOOSER(object); | ||
| 108 | + | ||
| 109 | + if(widget->font.face) { | ||
| 110 | + cairo_font_face_destroy(widget->font.face); | ||
| 111 | + widget->font.face = NULL; | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + G_OBJECT_CLASS(V3270FontChooserWidget_parent_class)->dispose(object); | ||
| 115 | +} | ||
| 116 | + | ||
| 100 | static void V3270FontChooserWidget_class_init(V3270FontChooserWidgetClass *klass) | 117 | static void V3270FontChooserWidget_class_init(V3270FontChooserWidgetClass *klass) |
| 101 | { | 118 | { |
| 102 | V3270SettingsClass * widget = GTK_V3270_SETTINGS_CLASS(klass); | 119 | V3270SettingsClass * widget = GTK_V3270_SETTINGS_CLASS(klass); |
| 103 | 120 | ||
| 121 | + G_OBJECT_CLASS(klass)->dispose = dispose; | ||
| 122 | + | ||
| 104 | widget->apply = apply; | 123 | widget->apply = apply; |
| 105 | widget->load = load; | 124 | widget->load = load; |
| 106 | 125 | ||
| @@ -122,11 +141,71 @@ static void V3270FontChooserWidget_class_init(V3270FontChooserWidgetClass *klass | @@ -122,11 +141,71 @@ static void V3270FontChooserWidget_class_init(V3270FontChooserWidgetClass *klass | ||
| 122 | // Update terminal widget | 141 | // Update terminal widget |
| 123 | GtkWidget * terminal = v3270_settings_get_terminal_widget(GTK_WIDGET(widget)); | 142 | GtkWidget * terminal = v3270_settings_get_terminal_widget(GTK_WIDGET(widget)); |
| 124 | if(terminal) | 143 | if(terminal) |
| 144 | + { | ||
| 125 | v3270_set_font_family(terminal,g_value_get_string(&value)); | 145 | v3270_set_font_family(terminal,g_value_get_string(&value)); |
| 146 | + gtk_widget_queue_draw(widget->preview); | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + // Update font | ||
| 150 | + | ||
| 151 | + widget->font.face = cairo_toy_font_face_create(g_value_get_string(&value), CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); | ||
| 126 | 152 | ||
| 127 | g_value_unset(&value); | 153 | g_value_unset(&value); |
| 128 | } | 154 | } |
| 129 | 155 | ||
| 156 | + static gboolean draw_preview(GtkWidget *widget, cairo_t *cr, V3270FontChooserWidget *chooser) { | ||
| 157 | + | ||
| 158 | + GtkWidget * t = v3270_settings_get_terminal_widget(GTK_WIDGET(chooser)); | ||
| 159 | + | ||
| 160 | + if(!t) | ||
| 161 | + return TRUE; | ||
| 162 | + | ||
| 163 | + v3270 * terminal = GTK_V3270(t); | ||
| 164 | + guint width = gtk_widget_get_allocated_width (widget); | ||
| 165 | + guint height = gtk_widget_get_allocated_height (widget); | ||
| 166 | + | ||
| 167 | + gdk_cairo_set_source_rgba(cr,terminal->color+V3270_COLOR_BACKGROUND); | ||
| 168 | + cairo_rectangle(cr, 0, 0, width, height); | ||
| 169 | + cairo_fill(cr); | ||
| 170 | + cairo_stroke(cr); | ||
| 171 | + | ||
| 172 | + if(chooser->font.face) { | ||
| 173 | + | ||
| 174 | + debug("%s",__FUNCTION__); | ||
| 175 | + | ||
| 176 | + cairo_set_font_face(cr,chooser->font.face); | ||
| 177 | + cairo_set_font_size(cr,15); | ||
| 178 | + | ||
| 179 | + cairo_font_extents_t extents; | ||
| 180 | + cairo_font_extents(cr,&extents); | ||
| 181 | + | ||
| 182 | + double spacing = extents.height + extents.descent; | ||
| 183 | + double row = spacing; | ||
| 184 | + | ||
| 185 | + static const enum V3270_COLOR colors[] = { | ||
| 186 | + V3270_COLOR_FIELD, | ||
| 187 | + V3270_COLOR_FIELD_INTENSIFIED, | ||
| 188 | + V3270_COLOR_FIELD_PROTECTED_INTENSIFIED | ||
| 189 | + }; | ||
| 190 | + | ||
| 191 | + size_t ix; | ||
| 192 | + | ||
| 193 | + for(ix = 0; ix < G_N_ELEMENTS(colors); ix++) { | ||
| 194 | + | ||
| 195 | + cairo_move_to(cr,0,row); | ||
| 196 | + gdk_cairo_set_source_rgba(cr,terminal->color+colors[ix]); | ||
| 197 | + cairo_show_text(cr,pango_language_get_sample_string(NULL)); | ||
| 198 | + | ||
| 199 | + row += spacing; | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + } | ||
| 203 | + | ||
| 204 | + cairo_stroke(cr); | ||
| 205 | + | ||
| 206 | + return FALSE; | ||
| 207 | +} | ||
| 208 | + | ||
| 130 | static void V3270FontChooserWidget_init(V3270FontChooserWidget *widget) | 209 | static void V3270FontChooserWidget_init(V3270FontChooserWidget *widget) |
| 131 | { | 210 | { |
| 132 | gtk_widget_set_size_request(GTK_WIDGET(widget),-1,136); | 211 | gtk_widget_set_size_request(GTK_WIDGET(widget),-1,136); |
| @@ -161,19 +240,24 @@ static void V3270FontChooserWidget_init(V3270FontChooserWidget *widget) | @@ -161,19 +240,24 @@ static void V3270FontChooserWidget_init(V3270FontChooserWidget *widget) | ||
| 161 | gtk_grid_attach(GTK_GRID(widget),box,0,0,1,5); | 240 | gtk_grid_attach(GTK_GRID(widget),box,0,0,1,5); |
| 162 | } | 241 | } |
| 163 | 242 | ||
| 164 | - // Add preview widgets | 243 | + // Add preview widget |
| 165 | { | 244 | { |
| 166 | - widget->preview = gtk_entry_new(); | ||
| 167 | - gtk_entry_set_text(GTK_ENTRY(widget->preview),pango_language_get_sample_string(NULL)); | 245 | + GtkWidget * frame = gtk_frame_new (NULL); |
| 246 | + gtk_frame_set_shadow_type(GTK_FRAME (frame), GTK_SHADOW_IN); | ||
| 247 | + gtk_widget_set_vexpand(frame,TRUE); | ||
| 248 | + gtk_widget_set_hexpand(frame,TRUE); | ||
| 168 | 249 | ||
| 169 | - gtk_widget_set_can_default(widget->preview,FALSE); | ||
| 170 | - gtk_widget_set_can_focus(widget->preview,FALSE); | ||
| 171 | - gtk_editable_set_editable(GTK_EDITABLE(widget->preview),FALSE); | ||
| 172 | - | ||
| 173 | - gtk_widget_set_vexpand(widget->preview,FALSE); | 250 | + widget->preview = gtk_drawing_area_new(); |
| 251 | + gtk_widget_set_vexpand(widget->preview,TRUE); | ||
| 174 | gtk_widget_set_hexpand(widget->preview,TRUE); | 252 | gtk_widget_set_hexpand(widget->preview,TRUE); |
| 175 | 253 | ||
| 176 | - gtk_grid_attach(GTK_GRID(widget),widget->preview,1,0,5,1); | 254 | + gtk_widget_set_size_request(widget->preview,400,-1); |
| 255 | + | ||
| 256 | + g_signal_connect(widget->preview, "draw", G_CALLBACK(draw_preview), widget); | ||
| 257 | + | ||
| 258 | + gtk_container_add(GTK_CONTAINER (frame), widget->preview); | ||
| 259 | + | ||
| 260 | + gtk_grid_attach(GTK_GRID(widget),frame,1,0,5,3); | ||
| 177 | } | 261 | } |
| 178 | 262 | ||
| 179 | } | 263 | } |