]> www.fi.muni.cz Git - evince.git/blob - lib/ev-gui.c
595be67426b54ec0ab9a2d7b5577034f4d069053
[evince.git] / lib / ev-gui.c
1 /*
2  *  Copyright (C) 2002 Marco Pesenti Gritti
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2, or (at your option)
7  *  any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  *  $Id$
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gtk/gtktreeview.h>
26 #include <gtk/gtktreeselection.h>
27
28 #include "ev-gui.h"
29
30 static void
31 ev_gui_sanitise_popup_position (GtkMenu *menu,
32                                 GtkWidget *widget,
33                                 gint *x,
34                                 gint *y)
35 {
36         GdkScreen *screen = gtk_widget_get_screen (widget);
37         gint monitor_num;
38         GdkRectangle monitor;
39         GtkRequisition req;
40
41         g_return_if_fail (widget != NULL);
42
43         gtk_widget_size_request (GTK_WIDGET (menu), &req);
44
45         monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
46         gtk_menu_set_monitor (menu, monitor_num);
47         gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
48
49         *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
50         *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
51 }
52
53 void
54 ev_gui_menu_position_tree_selection (GtkMenu   *menu,
55                                      gint      *x,
56                                      gint      *y,
57                                      gboolean  *push_in,
58                                      gpointer  user_data)
59 {
60         GtkTreeSelection *selection;
61         GList *selected_rows;
62         GtkTreeModel *model;
63         GtkTreeView *tree_view = GTK_TREE_VIEW (user_data);
64         GtkWidget *widget = GTK_WIDGET (user_data);
65         GtkRequisition req;
66         GdkRectangle visible;
67
68         gtk_widget_size_request (GTK_WIDGET (menu), &req);
69         gdk_window_get_origin (widget->window, x, y);
70
71         *x += (widget->allocation.width - req.width) / 2;
72
73         /* Add on height for the treeview title */
74         gtk_tree_view_get_visible_rect (tree_view, &visible);
75         *y += widget->allocation.height - visible.height;
76
77         selection = gtk_tree_view_get_selection (tree_view);
78         selected_rows = gtk_tree_selection_get_selected_rows (selection, &model);
79         if (selected_rows)
80         {
81                 GdkRectangle cell_rect;
82
83                 gtk_tree_view_get_cell_area (tree_view, selected_rows->data,
84                                              NULL, &cell_rect);
85
86                 *y += CLAMP (cell_rect.y + cell_rect.height, 0, visible.height);
87
88                 g_list_foreach (selected_rows, (GFunc)gtk_tree_path_free, NULL);
89                 g_list_free (selected_rows);
90         }
91
92         ev_gui_sanitise_popup_position (menu, widget, x, y);
93 }