1 /*
2  * Calcoo: gtkaux.c
3  *
4  * Copyright (C) 2001, 2002, 2005, 2007 Alexei Kaminski
5  *
6  * auxiliary functions for operations with gtk widgets
7  * no functions related to specific calcoo tasks
8  */
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include <gtk/gtk.h>
15 #include <gdk/gdk.h>
16 
17 #include "codes.h"
18 #include "body.h"
19 #include "defaults.h"
20 #include "gtkaux_headers.h"
21 #include "aux_headers.h"
22 #include "io_headers.h"
23 
create_fg_pixmap(GtkWidget ** pixmap_widget,char ** pixmap_xpm)24 void create_fg_pixmap(GtkWidget **pixmap_widget, char **pixmap_xpm)
25 /* creates a monochrome gtk pixmap having the color of the gtk foreground
26  * out of the given pixmap */
27 {
28 	GdkPixmap *pixmap, *all_black;
29 	GdkBitmap *mask;
30 	int w, h, tmp1, tmp2;
31 	int i;
32 	char *black_bits;
33 	int bitmap_size;
34 
35 	/* getting the transparency mask from the original pixmap */
36 	pixmap = gdk_pixmap_create_from_xpm_d (
37 		        (body->main_window)->window,
38 			&(mask),
39 			&(body->style)->bg[GTK_STATE_NORMAL],
40 			(gchar **) pixmap_xpm
41 		 );
42 
43 	/* getting the size of the original pixmap */
44 	sscanf(pixmap_xpm[0],"%d %d %d %d", &w, &h, &tmp1, &tmp2);
45 
46 	/* 7 and 8 here are not magic numbers!
47 	 * 8 is the number of bits in a byte
48 	 * 7 is 8 - 1 */
49 	bitmap_size = ((w + 7) / 8) * h;
50 
51 	/* creating a bitmap with all bits on,
52 	 * of the size of the original pixmap */
53 	black_bits = calloc(sizeof(char), bitmap_size);
54 
55         /* turning on all the pixels in the bitmap */
56 	for (i = 0; i < bitmap_size; i++)
57 		black_bits[i] = 0xff;
58 
59 	/* creating a bitmap with all pixels having the color of foreground,
60 	 * of the size of the original pixmap */
61 	all_black = gdk_pixmap_create_from_data(
62 		(body->main_window)->window,
63 		black_bits,
64 		w, h,
65 		-1,
66 		&(body->style)->fg[GTK_STATE_FOR_CALCOO_FOREGROUND_COLOR],
67 		&(body->style)->bg[GTK_STATE_NORMAL]);
68 
69 	if (CONVERT_ICONS_TO_FOREGROUND_COLOR)
70 		/* imposing the transparency mask of the original pixmap
71 		 * onto the foreground rectangle */
72 		*pixmap_widget = gtk_image_new_from_pixmap(all_black, mask);
73 	else
74 		*pixmap_widget = gtk_image_new_from_pixmap(pixmap, mask);
75 
76 	free(black_bits);
77 	g_object_unref(pixmap);
78 	g_object_unref(all_black);
79 	g_object_unref(mask);
80 }
81 
create_n_put_pixmap(GtkWidget ** pixmap_widget,int x,int y,char ** pixmap_xpm)82 void create_n_put_pixmap(GtkWidget **pixmap_widget, int x, int y,
83 			 char **pixmap_xpm)
84 {
85 	create_fg_pixmap(pixmap_widget, pixmap_xpm);
86 	gtk_fixed_put(GTK_FIXED(body->fixer), *pixmap_widget, x, y);
87 }
88 
own_selection(GtkWidget * widget,gint * have_selection)89 void own_selection( GtkWidget *widget,
90 		    gint      *have_selection )
91 {
92 	*have_selection = gtk_selection_owner_set (body->selection_widget,
93 						   GDK_SELECTION_PRIMARY,
94 						   GDK_CURRENT_TIME);
95 }
96 
set_selection(GtkWidget * widget,GtkSelectionData * selection_data,guint info,guint time_stamp,gpointer data)97 void set_selection( GtkWidget        *widget,
98 		    GtkSelectionData *selection_data,
99 		    guint             info,
100 		    guint             time_stamp,
101 		    gpointer          data )
102 {
103 	gtk_selection_data_set (selection_data,
104 				GDK_SELECTION_TYPE_STRING,
105 				8,
106  				(unsigned char*)body->char_display,
107  				strlen(body->char_display)
108 		);
109 }
110 
selection_clear(GtkWidget * widget,GdkEventSelection * event,gint * have_selection)111 gboolean selection_clear( GtkWidget         *widget,
112 			  GdkEventSelection *event,
113 			  gint              *have_selection )
114 {
115 	*have_selection = FALSE;
116 	return TRUE;
117 }
118 
request_selection(GtkWidget * widget,gpointer data)119 void request_selection( GtkWidget *widget,
120 			gpointer   data )
121 {
122   static GdkAtom targets_atom = GDK_NONE;
123   GtkWidget *window = (GtkWidget *)data;
124 
125   /* Get the atom corresponding to the string "TARGETS" */
126   if (targets_atom == GDK_NONE)
127     targets_atom = gdk_atom_intern ("STRING", FALSE);
128 
129   /* And request the "TARGETS" target for the primary selection */
130   gtk_selection_convert (window, GDK_SELECTION_PRIMARY, targets_atom,
131 			 GDK_CURRENT_TIME);
132 }
133 
134 #define MAX_SELECTION_LENGTH 20
135 
get_selection(GtkWidget * widget,GtkSelectionData * data,gpointer val)136 void get_selection(GtkWidget *widget, GtkSelectionData *data, gpointer val)
137 {
138 	char buff[MAX_SELECTION_LENGTH];
139 	int success;
140 	double x;
141 
142 	if( data->length + 1 > MAX_SELECTION_LENGTH )
143 		return; /* selection too long */
144 
145 	if(data->length < 0) /* the selection could not be retrieved */
146 		return;
147 	strncpy( buff, (char*)data->data, data->length );
148 	buff[data->length] = '\n';
149 	success = sscanf(buff,"%lf", &x);
150 	if (success)
151 		clicked_code_paste(x);
152 }
153 
154