X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fui%2Fgui%2Fpsppire-output-view.c;h=2feaf90d62cc37135adb01d757f8c0d6bcd3c338;hb=da6ac7ebe47553c89c7d06592770a49fa11b3a32;hp=fef0185b0307d737b61ebd6ec9c37d82751d8020;hpb=5bd5007b498d18182af272222a0021af178fc531;p=pspp diff --git a/src/ui/gui/psppire-output-view.c b/src/ui/gui/psppire-output-view.c index fef0185b03..2feaf90d62 100644 --- a/src/ui/gui/psppire-output-view.c +++ b/src/ui/gui/psppire-output-view.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2008-2015 Free Software Foundation. + Copyright (C) 2008-2015, 2016 Free Software Foundation. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -54,7 +54,7 @@ struct psppire_output_view GtkLayout *output; int render_width; int max_width; - int y; + glong y; struct string_map render_opts; GtkTreeView *overview; @@ -82,43 +82,11 @@ enum N_COLS }; -static void on_dwgarea_realize (GtkWidget *widget, gpointer data); - static gboolean draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data) { - struct psppire_output_view *view = data; struct xr_rendering *r = g_object_get_data (G_OBJECT (widget), "rendering"); - - const GtkStyle *style = gtk_widget_get_style (GTK_WIDGET (view->output)); - - PangoFontDescription *font_desc; - char *font_name; - - gchar *fgc = - gdk_color_to_string (&style->text[gtk_widget_get_state (GTK_WIDGET (view->output))]); - - string_map_replace (&view->render_opts, "foreground-color", fgc); - - free (fgc); - - /* Use GTK+ default font as proportional font. */ - font_name = pango_font_description_to_string (style->font_desc); - string_map_replace (&view->render_opts, "prop-font", font_name); - g_free (font_name); - - /* Derived emphasized font from proportional font. */ - font_desc = pango_font_description_copy (style->font_desc); - pango_font_description_set_style (font_desc, PANGO_STYLE_ITALIC); - font_name = pango_font_description_to_string (font_desc); - string_map_replace (&view->render_opts, "emph-font", font_name); - g_free (font_name); - pango_font_description_free (font_desc); - - xr_rendering_apply_options (r, &view->render_opts); - xr_rendering_draw_all (r, cr); - return TRUE; } @@ -132,7 +100,6 @@ free_rendering (gpointer rendering_) static void create_xr (struct psppire_output_view *view) { - const GtkStyle *style = gtk_widget_get_style (GTK_WIDGET (view->output)); struct text_item *text_item; PangoFontDescription *font_desc; struct xr_rendering *r; @@ -141,21 +108,30 @@ create_xr (struct psppire_output_view *view) cairo_t *cr; gchar *fgc; + GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (view->output)); + GtkStateFlags state = gtk_widget_get_state_flags (GTK_WIDGET (view->output)); + GdkRGBA *fg_color; + + gtk_style_context_get (context, state, + "font", &font_desc, "color", &fg_color, NULL); + cr = gdk_cairo_create (gtk_widget_get_window (GTK_WIDGET (view->output))); /* Set the widget's text color as the foreground color for the output driver */ - fgc = gdk_color_to_string (&style->text[gtk_widget_get_state (GTK_WIDGET (view->output))]); - + /* gdk_rgba_to_string() would be perfect, but xr's parse_color does not */ + /* understand the rgb(255,128,33) format. Therefore we do it ourself. */ + fgc = xasprintf("#%4x%4x%4x",(int)(fg_color->red*0xffff), + (int)(fg_color->green*0xffff),(int)(fg_color->blue*0xffff)); string_map_insert (&view->render_opts, "foreground-color", fgc); g_free (fgc); + gdk_rgba_free (fg_color); /* Use GTK+ default font as proportional font. */ - font_name = pango_font_description_to_string (style->font_desc); + font_name = pango_font_description_to_string (font_desc); string_map_insert (&view->render_opts, "prop-font", font_name); g_free (font_name); /* Derived emphasized font from proportional font. */ - font_desc = pango_font_description_copy (style->font_desc); pango_font_description_set_style (font_desc, PANGO_STYLE_ITALIC); font_name = pango_font_description_to_string (font_desc); string_map_insert (&view->render_opts, "emph-font", font_name); @@ -179,27 +155,49 @@ create_xr (struct psppire_output_view *view) text_item = text_item_create (TEXT_ITEM_PARAGRAPH, "X"); r = xr_rendering_create (view->xr, text_item_super (text_item), cr); xr_rendering_measure (r, &font_width, &view->font_height); - /* xr_rendering_destroy (r); */ text_item_unref (text_item); cairo_destroy (cr); } +/* Return the horizontal position to place a widget whose + width is CHILD_WIDTH */ +static gint +get_xpos (const struct psppire_output_view *view, gint child_width) +{ + GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output)); + guint w = gdk_window_get_width (gdkw); + int gutter = 0; + g_object_get (view->output, "border-width", &gutter, NULL); + return (gtk_widget_get_direction (GTK_WIDGET (view->output)) == GTK_TEXT_DIR_RTL) ? w - child_width - gutter: gutter; +} + static void create_drawing_area (struct psppire_output_view *view, GtkWidget *drawing_area, struct xr_rendering *r, int tw, int th) { + /* Enable this to help with debugging. It shows you which widgets are being + put where. */ + if (0) + { + GdkRGBA green = {0, 1, 0, 1}; + gtk_widget_override_background_color (GTK_WIDGET (view->output), + GTK_STATE_NORMAL, &green); + GdkRGBA red = {1, 0, 0, 1}; + gtk_widget_override_background_color (drawing_area, GTK_STATE_NORMAL, &red); + } + g_object_set_data_full (G_OBJECT (drawing_area), "rendering", r, free_rendering); - g_signal_connect (drawing_area, "realize", - G_CALLBACK (on_dwgarea_realize), view); g_signal_connect (drawing_area, "draw", G_CALLBACK (draw_callback), view); gtk_widget_set_size_request (drawing_area, tw, th); - gtk_layout_put (view->output, drawing_area, 0, view->y); + gint xpos = get_xpos (view, tw); + + gtk_layout_put (view->output, drawing_area, xpos, view->y); gtk_widget_show (drawing_area); } @@ -208,17 +206,15 @@ static void rerender (struct psppire_output_view *view) { struct output_view_item *item; + GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output)); cairo_t *cr; - if (!view->n_items || !gtk_widget_get_window (GTK_WIDGET (view->output))) + if (!view->n_items || ! gdkw) return; - string_map_clear (&view->render_opts); - xr_driver_destroy (view->xr); - create_xr (view); - - cr = gdk_cairo_create (gtk_widget_get_window (GTK_WIDGET (view->output))); - + cr = gdk_cairo_create (gdkw); + if (view->xr == NULL) + create_xr (view); view->y = 0; view->max_width = 0; for (item = view->items; item < &view->items[view->n_items]; item++) @@ -239,6 +235,8 @@ rerender (struct psppire_output_view *view) xr_rendering_measure (r, &tw, &th); + gint xpos = get_xpos (view, tw); + if (!item->drawing_area) { item->drawing_area = gtk_drawing_area_new (); @@ -249,14 +247,26 @@ rerender (struct psppire_output_view *view) g_object_set_data_full (G_OBJECT (item->drawing_area), "rendering", r, free_rendering); gtk_widget_set_size_request (item->drawing_area, tw, th); - gtk_layout_move (view->output, item->drawing_area, 0, view->y); + gtk_layout_move (view->output, item->drawing_area, xpos, view->y); } - alloc.x = 0; + { + gint minw; + gint minh; + /* This code probably doesn't bring us anthing, but Gtk + shows warnings if get_preferred_width/height is not + called before the size_allocate below is called. */ + gtk_widget_get_preferred_width (item->drawing_area, &minw, NULL); + gtk_widget_get_preferred_height (item->drawing_area, &minh, NULL); + if (th > minh) th = minh; + if (tw > minw) tw = minw; + } + alloc.x = xpos; alloc.y = view->y; alloc.width = tw; alloc.height = th; - gtk_widget_size_allocate(item->drawing_area,&alloc); + + gtk_widget_size_allocate (item->drawing_area, &alloc); if (view->max_width < tw) view->max_width = tw; @@ -375,7 +385,7 @@ psppire_output_view_put (struct psppire_output_view *view, gtk_tree_store_set (store, &iter, COL_NAME, ds_cstr (&name), COL_ADDR, item, - COL_Y, view->y, + COL_Y, (view->y), -1); ds_destroy (&name); @@ -425,32 +435,14 @@ on_row_activate (GtkTreeView *overview, } static void -copy_base_to_bg (GtkWidget *dest, GtkWidget *src) -{ - int i; - for (i = 0; i < 5; ++i) - { - gtk_widget_modify_bg (dest, i, >k_widget_get_style (src)->base[i]); - gtk_widget_modify_fg (dest, i, >k_widget_get_style (src)->text[i]); - } -} - -/* Copy the base style from the parent widget to the container and all its - children. We do this because the container's primary purpose is to display - text. This way psppire appears to follow the chosen gnome theme. */ -static void -on_style_set (GtkWidget *toplevel, GtkStyle *prev, - struct psppire_output_view *view) -{ - copy_base_to_bg (GTK_WIDGET (view->output), toplevel); - gtk_container_foreach (GTK_CONTAINER (view->output), - (GtkCallback) copy_base_to_bg, view->output); -} - -static void -on_dwgarea_realize (GtkWidget *dwg_area, gpointer data) +on_style_updated (GtkWidget *toplevel, struct psppire_output_view *view) { - copy_base_to_bg (dwg_area, gtk_widget_get_toplevel (dwg_area)); + if (!view->n_items || !gtk_widget_get_window (GTK_WIDGET (view->output))) + return; + string_map_clear (&view->render_opts); + xr_driver_destroy (view->xr); + create_xr (view); + rerender (view); } enum { @@ -609,10 +601,12 @@ on_copy (struct psppire_output_view *view) } static void -on_selection_change (GtkTreeSelection *sel, GtkAction *copy_action) +on_selection_change (GtkTreeSelection *sel, GAction *copy_action) { /* The Copy action is available only if there is something selected */ - gtk_action_set_sensitive (copy_action, gtk_tree_selection_count_selected_rows (sel) > 0); + g_object_set (copy_action, + "enabled", gtk_tree_selection_count_selected_rows (sel) > 0, + NULL); } static void @@ -629,6 +623,7 @@ on_size_allocate (GtkWidget *widget, struct psppire_output_view *view) { int new_render_width = MAX (300, allocation->width); + if (view->render_width != new_render_width) { view->render_width = new_render_width; @@ -636,14 +631,39 @@ on_size_allocate (GtkWidget *widget, } } +static void +on_realize (GtkWidget *overview, GObject *view) +{ + GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (overview)); + gtk_tree_selection_set_mode (sel, GTK_SELECTION_MULTIPLE); + + GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (overview)); + + GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel), + "copy"); + + GAction *select_all_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel), + "select-all"); + + g_object_set (copy_action, "enabled", FALSE, NULL); + + g_signal_connect_swapped (select_all_action, "activate", + G_CALLBACK (on_select_all), view); + + g_signal_connect_swapped (copy_action, "activate", + G_CALLBACK (on_copy), view); + + g_signal_connect (sel, "changed", G_CALLBACK (on_selection_change), + copy_action); +} + struct psppire_output_view * -psppire_output_view_new (GtkLayout *output, GtkTreeView *overview, - GtkAction *copy_action, GtkAction *select_all_action) +psppire_output_view_new (GtkLayout *output, GtkTreeView *overview) { struct psppire_output_view *view; GtkTreeViewColumn *column; GtkCellRenderer *renderer; - GtkTreeSelection *sel; + GtkTreeModel *model; view = xmalloc (sizeof *view); @@ -666,12 +686,17 @@ psppire_output_view_new (GtkLayout *output, GtkTreeView *overview, view->print_n_pages = 0; view->paginated = FALSE; - g_signal_connect (view->toplevel, "style-set", G_CALLBACK (on_style_set), view); + g_signal_connect (output, "style-updated", G_CALLBACK (on_style_updated), view); g_signal_connect (output, "size-allocate", G_CALLBACK (on_size_allocate), view); + gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (output)), + GTK_STYLE_CLASS_VIEW); + if (overview) { + g_signal_connect (overview, "realize", G_CALLBACK (on_realize), view); + model = GTK_TREE_MODEL (gtk_tree_store_new ( N_COLS, G_TYPE_STRING, /* COL_NAME */ @@ -680,11 +705,6 @@ psppire_output_view_new (GtkLayout *output, GtkTreeView *overview, gtk_tree_view_set_model (overview, model); g_object_unref (model); - sel = gtk_tree_view_get_selection (overview); - gtk_tree_selection_set_mode (sel, GTK_SELECTION_MULTIPLE); - g_signal_connect (sel, "changed", G_CALLBACK (on_selection_change), - copy_action); - column = gtk_tree_view_column_new (); gtk_tree_view_append_column (GTK_TREE_VIEW (overview), column); renderer = gtk_cell_renderer_text_new (); @@ -693,12 +713,6 @@ psppire_output_view_new (GtkLayout *output, GtkTreeView *overview, g_signal_connect (GTK_TREE_VIEW (overview), "row-activated", G_CALLBACK (on_row_activate), view); - - gtk_action_set_sensitive (copy_action, FALSE); - g_signal_connect_swapped (copy_action, "activate", - G_CALLBACK (on_copy), view); - g_signal_connect_swapped (select_all_action, "activate", - G_CALLBACK (on_select_all), view); } return view; @@ -712,8 +726,8 @@ psppire_output_view_destroy (struct psppire_output_view *view) if (!view) return; - g_signal_handlers_disconnect_by_func (view->toplevel, - G_CALLBACK (on_style_set), view); + g_signal_handlers_disconnect_by_func (view->output, + G_CALLBACK (on_style_updated), view); string_map_destroy (&view->render_opts); @@ -749,7 +763,7 @@ psppire_output_view_clear (struct psppire_output_view *view) view->items = NULL; view->n_items = view->allocated_items = 0; } - + /* Export. */ void @@ -775,17 +789,17 @@ static cairo_t * get_cairo_context_from_print_context (GtkPrintContext *context) { cairo_t *cr = gtk_print_context_get_cairo_context (context); - + /* For all platforms except windows, gtk_print_context_get_dpi_[xy] returns 72. Windows returns 600. */ double xres = gtk_print_context_get_dpi_x (context); double yres = gtk_print_context_get_dpi_y (context); - + /* This means that the cairo context now has its dimensions in Points */ cairo_scale (cr, xres / 72.0, yres / 72.0); - + return cr; } @@ -904,7 +918,7 @@ psppire_output_view_print (struct psppire_output_view *view, GtkPrintOperation *print = gtk_print_operation_new (); - if (view->print_settings != NULL) + if (view->print_settings != NULL) gtk_print_operation_set_print_settings (print, view->print_settings); g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), view);