1 /*
2 
3 Copyright (c) 2001-2007 Michael Terry
4 Copyright (c) 2013-2014 Arthur Borsboom
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 
20 */
21 
22 #include "../config.h"
23 
24 #include <gtk/gtk.h>
25 
26 #include "xpad-grip-tool-item.h"
27 
28 struct XpadGripToolItemPrivate
29 {
30 	GtkWidget *drawbox;
31 };
32 
33 G_DEFINE_TYPE_WITH_PRIVATE(XpadGripToolItem, xpad_grip_tool_item, GTK_TYPE_TOOL_ITEM)
34 
35 static gboolean xpad_grip_tool_item_event_box_draw (GtkWidget *widget, cairo_t *cr);
36 static void xpad_grip_tool_item_event_box_realize (GtkWidget *widget);
37 static gboolean xpad_grip_tool_item_button_pressed_event (GtkWidget *widget, GdkEventButton *event);
38 
39 GtkToolItem *
xpad_grip_tool_item_new(void)40 xpad_grip_tool_item_new (void)
41 {
42 	return GTK_TOOL_ITEM (g_object_new (XPAD_TYPE_GRIP_TOOL_ITEM, NULL));
43 }
44 
45 static void
xpad_grip_tool_item_class_init(XpadGripToolItemClass * klass)46 xpad_grip_tool_item_class_init (XpadGripToolItemClass *klass)
47 {
48 }
49 
50 static void
xpad_grip_tool_item_init(XpadGripToolItem * grip)51 xpad_grip_tool_item_init (XpadGripToolItem *grip)
52 {
53 	/* Alignment of the grip tool has been removed, first since the function is deprecated and second,
54 	   it does not seem to make any difference. If it does, the old code can be enabled. Remove these
55 	   comments and the commented code in after March 2016 */
56 
57 	/* GtkWidget *alignment; */
58 	/* gboolean right; */
59 
60 	grip->priv = xpad_grip_tool_item_get_instance_private(grip);
61 
62 	grip->priv->drawbox = gtk_drawing_area_new ();
63 	gtk_widget_add_events (grip->priv->drawbox, GDK_BUTTON_PRESS_MASK | GDK_EXPOSURE_MASK);
64 	g_signal_connect (grip->priv->drawbox, "button-press-event", G_CALLBACK (xpad_grip_tool_item_button_pressed_event), NULL);
65 	g_signal_connect (grip->priv->drawbox, "realize", G_CALLBACK (xpad_grip_tool_item_event_box_realize), NULL);
66 	g_signal_connect (grip->priv->drawbox, "draw", G_CALLBACK (xpad_grip_tool_item_event_box_draw), NULL);
67 	gtk_widget_set_size_request (grip->priv->drawbox, 18, 18);
68 
69 	/* right = gtk_widget_get_direction (grip->priv->drawbox) == GTK_TEXT_DIR_LTR; */
70 	/* alignment = gtk_alignment_new (right ? 1 : 0, 1, 0, 0); */
71 
72 	/* gtk_container_add (GTK_CONTAINER (alignment), grip->priv->drawbox); */
73 	/* gtk_container_add (GTK_CONTAINER (grip), alignment); */
74 	gtk_container_add (GTK_CONTAINER (grip), grip->priv->drawbox);
75 }
76 
77 static gboolean
xpad_grip_tool_item_button_pressed_event(GtkWidget * widget,GdkEventButton * event)78 xpad_grip_tool_item_button_pressed_event (GtkWidget *widget, GdkEventButton *event)
79 {
80 	if (event->button == 1)
81 	{
82 		GdkWindowEdge edge;
83 
84 		if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
85 			edge = GDK_WINDOW_EDGE_SOUTH_EAST;
86 		else
87 			edge = GDK_WINDOW_EDGE_SOUTH_WEST;
88 
89 		gtk_window_begin_resize_drag (GTK_WINDOW (gtk_widget_get_toplevel (widget)),
90 			edge, (gint) event->button, (gint) event->x_root, (gint) event->y_root, event->time);
91 
92 		return TRUE;
93 	}
94 
95 	return FALSE;
96 }
97 
98 static void
xpad_grip_tool_item_event_box_realize(GtkWidget * widget)99 xpad_grip_tool_item_event_box_realize (GtkWidget *widget)
100 {
101 	GdkDisplay *display = gtk_widget_get_display (widget);
102 	GdkCursorType cursor_type;
103 	GdkCursor *cursor;
104 
105 	if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
106 		cursor_type = GDK_BOTTOM_RIGHT_CORNER;
107 	else
108 		cursor_type = GDK_BOTTOM_LEFT_CORNER;
109 
110 	cursor = gdk_cursor_new_for_display (display, cursor_type);
111 	gdk_window_set_cursor (gtk_widget_get_window(widget), cursor);
112 	g_clear_object (&cursor);
113 }
114 
115 static gboolean
xpad_grip_tool_item_event_box_draw(GtkWidget * widget,cairo_t * cr)116 xpad_grip_tool_item_event_box_draw (GtkWidget *widget, cairo_t *cr)
117 {
118 	gtk_render_handle(gtk_widget_get_style_context(widget),
119 			cr,
120 			0, 0,
121 			gtk_widget_get_allocated_width(widget),
122 			gtk_widget_get_allocated_width(widget));
123 
124 	return FALSE;
125 }
126