📄 viewer-win32.c
字号:
pango_layout_context_changed (para->layout); para_list = para_list->next; } gtk_widget_queue_resize (layout);}static voidreload_font (){ GList *para_list; pango_context_set_font_description (context, font_description); para_list = paragraphs; while (para_list) { Paragraph *para = para_list->data; pango_layout_context_changed (para->layout); para_list = para_list->next; } if (layout) gtk_widget_queue_resize (layout);}voidset_family (GtkWidget *entry, gpointer data){ gchar *family_name = gtk_editable_get_chars (GTK_EDITABLE (entry), 0, -1); if (!family_name || family_name[0] == '\0') return; pango_font_description_set_family(font_description, family_name); fill_styles_combo (styles_combo);}voidset_style (GtkWidget *entry, gpointer data){ char *str = gtk_editable_get_chars (GTK_EDITABLE (entry), 0, -1); PangoFontDescription *tmp_desc; tmp_desc = pango_font_description_from_string (str); pango_font_description_set_style(font_description, pango_font_description_get_style(tmp_desc)); pango_font_description_set_variant(font_description, pango_font_description_get_variant(tmp_desc)); pango_font_description_set_weight(font_description, pango_font_description_get_weight(tmp_desc)); pango_font_description_set_stretch(font_description, pango_font_description_get_stretch(tmp_desc)); pango_font_description_free (tmp_desc); g_free (str); reload_font ();}voidfont_size_changed (GtkAdjustment *adj){ pango_font_description_set_size(font_description, (int)(adj->value * PANGO_SCALE + 0.5)); reload_font();}static intcompare_font_descriptions (const PangoFontDescription *a, const PangoFontDescription *b){ int val = strcmp (pango_font_description_get_family(a), pango_font_description_get_family(b)); if (val != 0) return val; if (pango_font_description_get_weight(a) != pango_font_description_get_weight(b)) return pango_font_description_get_weight(a) - pango_font_description_get_weight(b); if (pango_font_description_get_style(a) != pango_font_description_get_style(b)) return pango_font_description_get_style(a) - pango_font_description_get_style(b); if (pango_font_description_get_stretch(a) != pango_font_description_get_stretch(b)) return pango_font_description_get_stretch(a) - pango_font_description_get_stretch(b); if (pango_font_description_get_stretch(a) != pango_font_description_get_stretch(b)) return pango_font_description_get_stretch(a) - pango_font_description_get_stretch(b); return 0;}static intfont_description_sort_func (const void *a, const void *b){ return compare_font_descriptions (*(PangoFontDescription **)a, *(PangoFontDescription **)b);}typedef struct{ PangoFontDescription **descs; int n_descs;} FontDescInfo;static voidfree_info (FontDescInfo *info){ pango_font_descriptions_free (info->descs, info->n_descs);}static voidfill_styles_combo (GtkWidget *combo){ int i; GList *style_list = NULL; PangoFontFace **faces; PangoFontFamily *family, **families; FontDescInfo *info; int n_families; const char *family_name = pango_font_description_get_family(font_description); /* * Now map back the given family name to the family. There are more efficient * ways to handle this but it should not matter much ... */ pango_context_list_families (context, &families, &n_families); g_assert(n_families > 0); family = families[0]; /* just in case */ for (i = 0; i < n_families; i++) { if (0 == g_strcasecmp(pango_font_family_get_name(families[i]), family_name)) { family = families[i]; break; } } g_free (families); info = g_new (FontDescInfo, 1); pango_font_family_list_faces (family, &faces, &info->n_descs); info->descs = g_new0 (PangoFontDescription*, info->n_descs); for (i = 0; i < info->n_descs; i++) { info->descs[i] = pango_font_face_describe(faces[i]); } g_free (faces); gtk_object_set_data_full (GTK_OBJECT (combo), "descs", info, (GtkDestroyNotify)free_info); qsort (info->descs, info->n_descs, sizeof(PangoFontDescription *), font_description_sort_func); for (i=0; i<info->n_descs; i++) { char *str; PangoFontDescription *tmp_desc; tmp_desc = info->descs[i]; pango_font_description_set_family(tmp_desc, NULL); pango_font_description_unset_fields(tmp_desc, PANGO_FONT_MASK_SIZE); str = pango_font_description_to_string (tmp_desc); style_list = g_list_prepend (style_list, str); } style_list = g_list_reverse (style_list); gtk_combo_set_popdown_strings (GTK_COMBO (combo), style_list); g_list_foreach (style_list, (GFunc)g_free, NULL);}static GtkWidget *make_styles_combo (){ GtkWidget *combo; combo = gtk_combo_new (); gtk_combo_set_value_in_list (GTK_COMBO (combo), TRUE, FALSE); gtk_editable_set_editable (GTK_EDITABLE (GTK_COMBO (combo)->entry), FALSE); gtk_signal_connect (GTK_OBJECT (GTK_COMBO (combo)->entry), "changed", GTK_SIGNAL_FUNC (set_style), NULL); styles_combo = combo; fill_styles_combo (combo); return combo;}static intcmp_families (const PangoFontFamily** a, const PangoFontFamily** b){ return strcmp (pango_font_family_get_name (*a), pango_font_family_get_name (*b));}GtkWidget *make_families_menu (){ GtkWidget *combo; int n_families; PangoFontFamily **families; GList *family_list = NULL; int i; pango_context_list_families (context, &families, &n_families); qsort (families, n_families, sizeof(char *), cmp_families); for (i=0; i<n_families; i++) family_list = g_list_prepend (family_list, pango_font_family_get_name (families[i])); family_list = g_list_reverse (family_list); combo = gtk_combo_new (); gtk_combo_set_popdown_strings (GTK_COMBO (combo), family_list); gtk_combo_set_value_in_list (GTK_COMBO (combo), TRUE, FALSE); gtk_editable_set_editable (GTK_EDITABLE (GTK_COMBO (combo)->entry), FALSE); gtk_entry_set_text (GTK_ENTRY (GTK_COMBO (combo)->entry), pango_font_description_get_family(font_description)); gtk_signal_connect (GTK_OBJECT (GTK_COMBO (combo)->entry), "changed", GTK_SIGNAL_FUNC (set_family), NULL); g_list_free (family_list); return combo;}GtkWidget *make_font_selector (void){ GtkWidget *hbox; GtkWidget *util_hbox; GtkWidget *label; GtkWidget *option_menu; GtkWidget *spin_button; GtkAdjustment *adj; hbox = gtk_hbox_new (FALSE, 4); util_hbox = gtk_hbox_new (FALSE, 2); label = gtk_label_new ("Family:"); gtk_box_pack_start (GTK_BOX (util_hbox), label, FALSE, FALSE, 0); option_menu = make_families_menu (); gtk_box_pack_start (GTK_BOX (util_hbox), option_menu, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (hbox), util_hbox, FALSE, FALSE, 0); util_hbox = gtk_hbox_new (FALSE, 2); label = gtk_label_new ("Style:"); gtk_box_pack_start (GTK_BOX (util_hbox), label, FALSE, FALSE, 0); option_menu = make_styles_combo (); gtk_box_pack_start (GTK_BOX (util_hbox), option_menu, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (hbox), util_hbox, FALSE, FALSE, 0); util_hbox = gtk_hbox_new (FALSE, 2); label = gtk_label_new ("Size:"); gtk_box_pack_start (GTK_BOX (util_hbox), label, FALSE, FALSE, 0); spin_button = gtk_spin_button_new (NULL, 1., 0); gtk_box_pack_start (GTK_BOX (util_hbox), spin_button, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (hbox), util_hbox, FALSE, FALSE, 0); adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (spin_button)); adj->value = PANGO_PIXELS (pango_font_description_get_size(font_description)); adj->lower = 0; adj->upper = 1024; adj->step_increment = 1; adj->page_size = 10; gtk_adjustment_changed (adj); gtk_adjustment_value_changed (adj); gtk_signal_connect (GTK_OBJECT (adj), "value_changed", GTK_SIGNAL_FUNC (font_size_changed), NULL); return hbox;}intmain (int argc, char **argv){ char *text; GtkWidget *window; GtkWidget *scrollwin; GtkWidget *vbox, *hbox; GtkWidget *frame; GtkWidget *checkbutton; gtk_init (&argc, &argv); if (argc != 2) { fprintf (stderr, "Usage: %s FILE\n", g_get_prgname ()); exit(1); } /* Create the list of paragraphs from the supplied file */ text = read_file (argv[1]); if (!text) exit(1); context = pango_win32_get_context (); paragraphs = split_paragraphs (text); pango_context_set_language (context, pango_language_from_string ("en_US")); pango_context_set_base_dir (context, PANGO_DIRECTION_LTR); font_description = pango_font_description_new (); pango_font_description_set_family(font_description, "sans"); pango_font_description_set_size(font_description, 16 * PANGO_SCALE);#if 0 /* default init ok? */ font_description.style = PANGO_STYLE_NORMAL; font_description.variant = PANGO_VARIANT_NORMAL; font_description.weight = 500; font_description.stretch = PANGO_STRETCH_NORMAL;#endif pango_context_set_font_description (context, font_description); /* Create the user interface */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size (GTK_WINDOW (window), 400, 400); gtk_signal_connect (GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (gtk_main_quit), NULL); vbox = gtk_vbox_new (FALSE, 4); gtk_container_add (GTK_CONTAINER (window), vbox); hbox = make_font_selector (); gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); scrollwin = gtk_scrolled_window_new (NULL, NULL); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollwin), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); gtk_box_pack_start (GTK_BOX (vbox), scrollwin, TRUE, TRUE, 0); layout = gtk_layout_new (NULL, NULL); gtk_widget_set_events (layout, GDK_BUTTON_PRESS_MASK); gtk_widget_set_app_paintable (layout, TRUE); gtk_signal_connect (GTK_OBJECT (layout), "size_allocate", GTK_SIGNAL_FUNC (size_allocate), paragraphs); gtk_signal_connect (GTK_OBJECT (layout), "expose_event", GTK_SIGNAL_FUNC (expose), paragraphs); gtk_signal_connect (GTK_OBJECT (layout), "draw", GTK_SIGNAL_FUNC (draw), paragraphs); gtk_signal_connect (GTK_OBJECT (layout), "button_press_event", GTK_SIGNAL_FUNC (button_press), paragraphs);#if GTK_CHECK_VERSION (1,3,2) gtk_widget_set_double_buffered (layout, FALSE);#endif gtk_container_add (GTK_CONTAINER (scrollwin), layout); frame = gtk_frame_new (NULL); gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN); gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0); message_label = gtk_label_new ("Current char:"); gtk_misc_set_padding (GTK_MISC (message_label), 1, 1); gtk_misc_set_alignment (GTK_MISC (message_label), 0.0, 0.5); gtk_container_add (GTK_CONTAINER (frame), message_label); checkbutton = gtk_check_button_new_with_label ("Use RTL global direction"); gtk_signal_connect (GTK_OBJECT (checkbutton), "toggled", GTK_SIGNAL_FUNC (checkbutton_toggled), NULL); gtk_box_pack_start (GTK_BOX (vbox), checkbutton, FALSE, FALSE, 0); gtk_widget_show_all (window); gtk_main (); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -