Select right text in rectangle

With this commit zathura selects the text in the selected
rectangle and copies it to the clipboard.
This commit is contained in:
Moritz Lipp 2010-05-18 00:19:09 +02:00
parent e17a8b433a
commit ee182e035a

119
zathura.c
View file

@ -384,7 +384,7 @@ init_directories()
} }
GError* error = NULL; GError* error = NULL;
if(!g_key_file_load_from_file(Zathura.Bookmarks.data, bookmarks, if(!g_key_file_load_from_file(Zathura.Bookmarks.data, bookmarks,
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error)) G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error))
{ {
notify(ERROR, g_strdup_printf("Could not load bookmark file: %s", error->message)); notify(ERROR, g_strdup_printf("Could not load bookmark file: %s", error->message));
@ -483,7 +483,7 @@ init_zathura()
g_signal_connect(G_OBJECT(Zathura.UI.view), "scroll-event", G_CALLBACK(cb_view_scrolled), NULL); g_signal_connect(G_OBJECT(Zathura.UI.view), "scroll-event", G_CALLBACK(cb_view_scrolled), NULL);
gtk_container_add(GTK_CONTAINER(Zathura.UI.view), GTK_WIDGET(Zathura.UI.viewport)); gtk_container_add(GTK_CONTAINER(Zathura.UI.view), GTK_WIDGET(Zathura.UI.viewport));
gtk_viewport_set_shadow_type(Zathura.UI.viewport, GTK_SHADOW_NONE); gtk_viewport_set_shadow_type(Zathura.UI.viewport, GTK_SHADOW_NONE);
#if SHOW_SCROLLBARS #if SHOW_SCROLLBARS
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Zathura.UI.view), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Zathura.UI.view), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
#else #else
@ -707,11 +707,11 @@ draw(int page_id)
float sg = ((float) g2 - g1) / (max - min); float sg = ((float) g2 - g1) / (max - min);
float sb = ((float) b2 - b1) / (max - min); float sb = ((float) b2 - b1) / (max - min);
for (y = 0; y < height; y++) for (y = 0; y < height; y++)
{ {
unsigned char* data = image + y * rowstride; unsigned char* data = image + y * rowstride;
for (x = 0; x < width; x++) for (x = 0; x < width; x++)
{ {
mean = (data[0] + data[1] + data[2]) / 3; mean = (data[0] + data[1] + data[2]) / 3;
data[2] = sr * (mean - min) + r1 + 0.5; data[2] = sr * (mean - min) + r1 + 0.5;
@ -844,7 +844,7 @@ void notify(int level, char* message)
gtk_widget_modify_text(GTK_WIDGET(Zathura.UI.inputbar), GTK_STATE_NORMAL, &(Zathura.Style.inputbar_fg)); gtk_widget_modify_text(GTK_WIDGET(Zathura.UI.inputbar), GTK_STATE_NORMAL, &(Zathura.Style.inputbar_fg));
break; break;
} }
if(message) if(message)
gtk_entry_set_text(Zathura.UI.inputbar, message); gtk_entry_set_text(Zathura.UI.inputbar, message);
} }
@ -943,11 +943,11 @@ open_file(char* path, char* password)
{ {
if(strcmp(keys[i], BM_PAGE_ENTRY)) if(strcmp(keys[i], BM_PAGE_ENTRY))
{ {
Zathura.Bookmarks.bookmarks = realloc(Zathura.Bookmarks.bookmarks, Zathura.Bookmarks.bookmarks = realloc(Zathura.Bookmarks.bookmarks,
(Zathura.Bookmarks.number_of_bookmarks + 1) * sizeof(Bookmark)); (Zathura.Bookmarks.number_of_bookmarks + 1) * sizeof(Bookmark));
Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].id = keys[i]; Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].id = keys[i];
Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].page = Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].page =
g_key_file_get_integer(Zathura.Bookmarks.data, file, keys[i], NULL); g_key_file_get_integer(Zathura.Bookmarks.data, file, keys[i], NULL);
Zathura.Bookmarks.number_of_bookmarks++; Zathura.Bookmarks.number_of_bookmarks++;
@ -1085,7 +1085,7 @@ void
setCompletionRowColor(GtkBox* results, int mode, int id) setCompletionRowColor(GtkBox* results, int mode, int id)
{ {
GtkEventBox *row = (GtkEventBox*) g_list_nth_data(gtk_container_get_children(GTK_CONTAINER(results)), id); GtkEventBox *row = (GtkEventBox*) g_list_nth_data(gtk_container_get_children(GTK_CONTAINER(results)), id);
if(row) if(row)
{ {
GtkBox *col = (GtkBox*) g_list_nth_data(gtk_container_get_children(GTK_CONTAINER(row)), 0); GtkBox *col = (GtkBox*) g_list_nth_data(gtk_container_get_children(GTK_CONTAINER(row)), 0);
@ -1250,7 +1250,7 @@ watch_file(void* parameter)
/* reopen and restore settings */ /* reopen and restore settings */
cmd_close(0, NULL); cmd_close(0, NULL);
open_file(path, password); open_file(path, password);
Zathura.PDF.scale = scale; Zathura.PDF.scale = scale;
gdk_threads_enter(); gdk_threads_enter();
@ -1358,7 +1358,7 @@ sc_change_mode(Argument* argument)
change_mode(argument->n); change_mode(argument->n);
} }
void void
sc_focus_inputbar(Argument* argument) sc_focus_inputbar(Argument* argument)
{ {
if(argument->data) if(argument->data)
@ -1429,7 +1429,7 @@ sc_navigate(Argument* argument)
if(argument->n == NEXT) if(argument->n == NEXT)
new_page = (new_page + number_of_pages + 1) % number_of_pages; new_page = (new_page + number_of_pages + 1) % number_of_pages;
else if(argument->n == PREVIOUS) else if(argument->n == PREVIOUS)
new_page = (new_page + number_of_pages - 1) % number_of_pages; new_page = (new_page + number_of_pages - 1) % number_of_pages;
set_page(new_page); set_page(new_page);
@ -1617,13 +1617,13 @@ isc_command_history(Argument* argument)
} }
} }
void void
isc_completion(Argument* argument) isc_completion(Argument* argument)
{ {
gchar *input = gtk_editable_get_chars(GTK_EDITABLE(Zathura.UI.inputbar), 1, -1); gchar *input = gtk_editable_get_chars(GTK_EDITABLE(Zathura.UI.inputbar), 1, -1);
gchar identifier = gtk_editable_get_chars(GTK_EDITABLE(Zathura.UI.inputbar), 0, 1)[0]; gchar identifier = gtk_editable_get_chars(GTK_EDITABLE(Zathura.UI.inputbar), 0, 1)[0];
int length = strlen(input); int length = strlen(input);
if(!length && !identifier) if(!length && !identifier)
return; return;
@ -1933,8 +1933,8 @@ cmd_bookmark(int argc, char** argv)
} }
/* add new bookmark */ /* add new bookmark */
Zathura.Bookmarks.bookmarks = realloc(Zathura.Bookmarks.bookmarks, Zathura.Bookmarks.bookmarks = realloc(Zathura.Bookmarks.bookmarks,
(Zathura.Bookmarks.number_of_bookmarks + 1) * sizeof(Bookmark)); (Zathura.Bookmarks.number_of_bookmarks + 1) * sizeof(Bookmark));
Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].id = id->str; Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].id = id->str;
Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].page = Zathura.PDF.page_number; Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].page = Zathura.PDF.page_number;
@ -1948,7 +1948,7 @@ cmd_open_bookmark(int argc, char** argv)
{ {
if(!Zathura.PDF.document || argc < 1) if(!Zathura.PDF.document || argc < 1)
return TRUE; return TRUE;
/* get id */ /* get id */
int i; int i;
GString *id = g_string_new(""); GString *id = g_string_new("");
@ -2157,7 +2157,7 @@ cmd_export(int argc, char** argv)
if(argv[1][0] == '~') if(argv[1][0] == '~')
{ {
file = malloc(((int) strlen(filename) + (int) strlen(argv[1]) file = malloc(((int) strlen(filename) + (int) strlen(argv[1])
+ (int) strlen(getenv("HOME")) - 1) * sizeof(char)); + (int) strlen(getenv("HOME")) - 1) * sizeof(char));
file = g_strdup_printf("%s%s%s", getenv("HOME"), argv[1] + 1, filename); file = g_strdup_printf("%s%s%s", getenv("HOME"), argv[1] + 1, filename);
} }
@ -2688,7 +2688,7 @@ cc_set(char* input)
} }
/* buffer command implementation */ /* buffer command implementation */
void void
bcmd_goto(char* buffer, Argument* argument) bcmd_goto(char* buffer, Argument* argument)
{ {
int b_length = strlen(buffer); int b_length = strlen(buffer);
@ -3031,7 +3031,7 @@ cb_view_kb_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
int i; int i;
for(i = 0; i < LENGTH(shortcuts); i++) for(i = 0; i < LENGTH(shortcuts); i++)
{ {
if (event->keyval == shortcuts[i].key && if (event->keyval == shortcuts[i].key &&
(((event->state & shortcuts[i].mask) == shortcuts[i].mask) || shortcuts[i].mask == 0) (((event->state & shortcuts[i].mask) == shortcuts[i].mask) || shortcuts[i].mask == 0)
&& (Zathura.Global.mode == shortcuts[i].mode || shortcuts[i].mode == -1)) && (Zathura.Global.mode == shortcuts[i].mode || shortcuts[i].mode == -1))
{ {
@ -3122,7 +3122,7 @@ cb_view_button_release(GtkWidget* widget, GdkEventButton* event, gpointer data)
if(!Zathura.PDF.document) if(!Zathura.PDF.document)
return FALSE; return FALSE;
double scale, offset_x, offset_y; double scale, offset_x, offset_y, page_width, page_height;
PopplerRectangle rectangle; PopplerRectangle rectangle;
cairo_t* cairo; cairo_t* cairo;
@ -3147,36 +3147,79 @@ cb_view_button_release(GtkWidget* widget, GdkEventButton* event, gpointer data)
cairo_fill(cairo); cairo_fill(cairo);
gtk_widget_queue_draw(Zathura.UI.drawing_area); gtk_widget_queue_draw(Zathura.UI.drawing_area);
/* reset points of the rectangle so that p1 is in the top-left corner
* and p2 is in the bottom right corner */
if(rectangle.x1 > rectangle.x2)
{
double d = rectangle.x1 - rectangle.x2;
rectangle.x1 = rectangle.x1 - d;
rectangle.x2 = rectangle.x2 - d;
}
if(rectangle.y2 > rectangle.y1)
{
double d = rectangle.y2 - rectangle.y1;
rectangle.y1 = rectangle.y1 + d;
rectangle.y2 = rectangle.y2 - d;
}
/* resize selection rectangle to document page */ /* resize selection rectangle to document page */
g_static_mutex_lock(&(Zathura.Lock.pdflib_lock));
poppler_page_get_size(Zathura.PDF.pages[Zathura.PDF.page_number]->page, &page_width, &page_height);
g_static_mutex_unlock(&(Zathura.Lock.pdflib_lock));
scale = ((double) Zathura.PDF.scale / 100.0); scale = ((double) Zathura.PDF.scale / 100.0);
rectangle.x1 = (rectangle.x1 - offset_x) / scale; rectangle.x1 = (rectangle.x1 - offset_x) / scale;
rectangle.y1 = (rectangle.y1 - offset_y) / scale; rectangle.y1 = (rectangle.y1 - offset_y) / scale;
rectangle.x2 = (rectangle.x2 - offset_x) / scale; rectangle.x2 = (rectangle.x2 - offset_x) / scale;
rectangle.y2 = (rectangle.y2 - offset_y) / scale; rectangle.y2 = (rectangle.y2 - offset_y) / scale;
/* rotation */
int rotate = Zathura.PDF.rotate;
double x1 = rectangle.x1;
double x2 = rectangle.x2;
double y1 = rectangle.y1;
double y2 = rectangle.y2;
switch(rotate)
{
case 90:
rectangle.x1 = y1;
rectangle.y1 = page_height - x2;
rectangle.x2 = y2;
rectangle.y2 = page_height - x1;
break;
case 180:
rectangle.x1 = (page_height - y1);
rectangle.y1 = (page_width - x2);
rectangle.x2 = (page_height - y2);
rectangle.y2 = (page_width - x1);
break;
case 270:
rectangle.x1 = page_width - y2;
rectangle.y1 = x1;
rectangle.x2 = page_width - y1;
rectangle.y2 = x2;
break;
}
/* reset points of the rectangle so that p1 is in the top-left corner
* and p2 is in the bottom right corner */
if(rectangle.x1 > rectangle.x2)
{
double d = rectangle.x1;
rectangle.x1 = rectangle.x2;
rectangle.x2 = d;
}
if(rectangle.y2 > rectangle.y1)
{
double d = rectangle.y1;
rectangle.y1 = rectangle.y2;
rectangle.y2 = d;
}
/* adapt y coordinates */
rectangle.y1 = page_height - rectangle.y1;
rectangle.y2 = page_height - rectangle.y2;
/* get selected text */
g_static_mutex_lock(&(Zathura.Lock.pdflib_lock)); g_static_mutex_lock(&(Zathura.Lock.pdflib_lock));
char* selected_text = poppler_page_get_text(Zathura.PDF.pages[Zathura.PDF.page_number]->page, char* selected_text = poppler_page_get_text(
POPPLER_SELECTION_GLYPH, &rectangle); Zathura.PDF.pages[Zathura.PDF.page_number]->page,POPPLER_SELECTION_GLYPH,
&rectangle);
if(selected_text) if(selected_text)
{
gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), selected_text, -1); gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), selected_text, -1);
g_free(selected_text);
}
g_static_mutex_unlock(&(Zathura.Lock.pdflib_lock)); g_static_mutex_unlock(&(Zathura.Lock.pdflib_lock));
return TRUE; return TRUE;
} }