zathura/shortcuts.c

392 lines
10 KiB
C
Raw Normal View History

/* See LICENSE file for license and copyright information */
2010-11-12 13:48:18 +01:00
#include <girara.h>
#include <gtk/gtk.h>
#include "callbacks.h"
#include "shortcuts.h"
#include "document.h"
2010-11-13 12:40:48 +01:00
#include "zathura.h"
2011-02-09 21:28:36 +01:00
#include "render.h"
2011-02-10 04:33:28 +01:00
#include "utils.h"
2010-11-13 10:05:28 +01:00
bool
sc_abort(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2010-11-13 12:40:48 +01:00
g_return_val_if_fail(session != NULL, false);
2011-05-07 22:00:52 +02:00
girara_mode_set(session, session->modes.normal);
2010-11-13 12:40:48 +01:00
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_adjust_window(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_change_mode(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2010-11-13 12:40:48 +01:00
g_return_val_if_fail(session != NULL, false);
girara_mode_set(session, argument->n);
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_focus_inputbar(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2010-11-13 12:40:48 +01:00
g_return_val_if_fail(session != NULL, false);
if (!(gtk_widget_get_visible(GTK_WIDGET(session->gtk.inputbar)))) {
2010-11-13 12:40:48 +01:00
gtk_widget_show(GTK_WIDGET(session->gtk.inputbar));
2010-11-17 22:51:15 +01:00
}
2010-11-13 12:40:48 +01:00
2011-02-09 12:29:09 +01:00
if (argument->data) {
2010-11-13 12:40:48 +01:00
gtk_entry_set_text(session->gtk.inputbar, (char*) argument->data);
gtk_widget_grab_focus(GTK_WIDGET(session->gtk.inputbar));
gtk_editable_set_position(GTK_EDITABLE(session->gtk.inputbar), -1);
}
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_follow(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_goto(girara_session_t* session, girara_argument_t* argument, unsigned int t)
2010-11-13 10:05:28 +01:00
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
g_return_val_if_fail(argument != NULL, false);
g_return_val_if_fail(zathura->document != NULL, false);
2011-02-09 17:12:09 +01:00
if (argument->n == TOP) {
girara_argument_t arg = { TOP, NULL };
sc_scroll(session, &arg, 0);
return false;
} else {
if (t == 0) {
girara_argument_t arg = { BOTTOM, NULL };
sc_scroll(session, &arg, 0);
return true;
}
2011-04-27 20:32:57 +02:00
page_set(zathura, t - 1);
2011-02-09 17:12:09 +01:00
}
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_navigate(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
g_return_val_if_fail(argument != NULL, false);
g_return_val_if_fail(zathura->document != NULL, false);
2010-12-12 22:04:42 +01:00
2011-04-18 17:55:50 +02:00
unsigned int number_of_pages = zathura->document->number_of_pages;
unsigned int new_page = zathura->document->current_page_number;
2010-12-12 22:04:42 +01:00
2011-02-09 12:29:09 +01:00
if (argument->n == NEXT) {
2010-12-12 22:04:42 +01:00
new_page = (new_page + 1) % number_of_pages;
2011-02-09 12:29:09 +01:00
} else if (argument->n == PREVIOUS) {
2010-12-12 22:04:42 +01:00
new_page = (new_page + number_of_pages - 1) % number_of_pages;
}
page_set(zathura, new_page);
2010-12-12 22:04:42 +01:00
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_recolor(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
2011-04-30 13:27:27 +02:00
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
zathura->global.recolor = !zathura->global.recolor;
render_all(zathura);
2011-04-18 17:55:50 +02:00
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_reload(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_rotate(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
g_return_val_if_fail(zathura->document != NULL, false);
2011-03-06 14:49:44 +01:00
/* update rotate value */
2011-04-18 17:55:50 +02:00
zathura->document->rotate = (zathura->document->rotate + 90) % 360;
2011-03-06 14:49:44 +01:00
/* render all pages again */
render_all(zathura);
2011-03-06 14:49:44 +01:00
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_scroll(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
g_return_val_if_fail(argument != NULL, false);
2011-09-01 11:51:49 +02:00
if (zathura->document == NULL) {
return false;
}
2011-04-18 17:55:50 +02:00
2011-01-24 12:54:20 +01:00
GtkAdjustment* adjustment = NULL;
2011-02-09 12:29:09 +01:00
if ( (argument->n == LEFT) || (argument->n == RIGHT) )
adjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
2011-01-24 12:54:20 +01:00
else
adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view));
2011-01-24 12:54:20 +01:00
gdouble view_size = gtk_adjustment_get_page_size(adjustment);
gdouble value = gtk_adjustment_get_value(adjustment);
gdouble max = gtk_adjustment_get_upper(adjustment) - view_size;
gdouble scroll_step = 40;
gdouble new_value;
switch(argument->n)
{
case FULL_UP:
new_value = (value - view_size) < 0 ? 0 : (value - view_size);
break;
case FULL_DOWN:
new_value = (value + view_size) > max ? max : (value + view_size);
break;
case HALF_UP:
new_value = (value - (view_size / 2)) < 0 ? 0 : (value - (view_size / 2));
break;
case HALF_DOWN:
new_value = (value + (view_size / 2)) > max ? max : (value + (view_size / 2));
break;
case LEFT:
case UP:
new_value = (value - scroll_step) < 0 ? 0 : (value - scroll_step);
break;
case RIGHT:
case DOWN:
new_value = (value + scroll_step) > max ? max : (value + scroll_step);
break;
case TOP:
new_value = 0;
break;
case BOTTOM:
new_value = max;
break;
default:
new_value = 0;
}
gtk_adjustment_set_value(adjustment, new_value);
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_search(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
g_return_val_if_fail(argument != NULL, false);
g_return_val_if_fail(zathura->document != NULL, false);
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_navigate_index(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
g_return_val_if_fail(argument != NULL, false);
g_return_val_if_fail(zathura->document != NULL, false);
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_toggle_index(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
g_return_val_if_fail(argument != NULL, false);
g_return_val_if_fail(zathura->document != NULL, false);
2011-02-10 04:33:28 +01:00
girara_tree_node_t* document_index = NULL;
GtkWidget* treeview = NULL;
GtkTreeModel* model = NULL;
GtkCellRenderer* renderer = NULL;
if (zathura->ui.index == NULL) {
2011-02-10 04:33:28 +01:00
/* create new index widget */
zathura->ui.index = gtk_scrolled_window_new(NULL, NULL);
2011-02-10 04:33:28 +01:00
if (zathura->ui.index == NULL) {
2011-02-10 04:33:28 +01:00
goto error_ret;
}
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(zathura->ui.index),
2011-02-10 04:33:28 +01:00
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
/* create index */
2011-04-18 17:55:50 +02:00
document_index = zathura_document_index_generate(zathura->document);
2011-02-10 04:33:28 +01:00
if (document_index == NULL) {
// TODO: Error message
goto error_free;
}
model = GTK_TREE_MODEL(gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_POINTER));
if (model == NULL) {
goto error_free;
}
treeview = gtk_tree_view_new_with_model(model);
if (treeview == NULL) {
goto error_free;
}
g_object_unref(model);
renderer = gtk_cell_renderer_text_new();
if (renderer == NULL) {
goto error_free;
}
document_index_build(model, NULL, document_index);
girara_node_free(document_index);
/* setup widget */
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW (treeview), 0, "Title", renderer, "markup", 0, NULL);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
g_object_set(G_OBJECT(gtk_tree_view_get_column(GTK_TREE_VIEW(treeview), 0)), "expand", TRUE, NULL);
gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview), gtk_tree_path_new_first(), NULL, FALSE);
/*g_signal_connect(G_OBJECT(treeview), "row-activated", G_CALLBACK(cb_index_row_activated), NULL); TODO*/
gtk_container_add(GTK_CONTAINER(zathura->ui.index), treeview);
2011-02-10 04:33:28 +01:00
gtk_widget_show(treeview);
}
if (gtk_widget_get_visible(GTK_WIDGET(zathura->ui.index))) {
girara_set_view(session, zathura->ui.page_view);
gtk_widget_hide(GTK_WIDGET(zathura->ui.index));
2011-02-10 04:33:28 +01:00
} else {
girara_set_view(session, zathura->ui.index);
gtk_widget_show(GTK_WIDGET(zathura->ui.index));
2011-02-10 04:33:28 +01:00
}
return false;
error_free:
if (zathura->ui.index != NULL) {
g_object_ref_sink(zathura->ui.index);
zathura->ui.index = NULL;
2011-02-10 04:33:28 +01:00
}
if (document_index != NULL) {
girara_node_free(document_index);
}
2011-04-18 17:55:50 +02:00
2011-02-10 04:33:28 +01:00
error_ret:
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_toggle_fullscreen(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
2011-02-10 03:06:13 +01:00
static bool fullscreen = false;
if (fullscreen) {
gtk_window_unfullscreen(GTK_WINDOW(session->gtk.window));
} else {
gtk_window_fullscreen(GTK_WINDOW(session->gtk.window));
}
fullscreen = fullscreen ? false : true;
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_quit(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
2010-11-12 13:48:18 +01:00
girara_argument_t arg = { GIRARA_HIDE, NULL };
girara_isc_completion(session, &arg, 0);
2010-11-12 13:48:18 +01:00
cb_destroy(NULL, NULL);
2010-11-12 13:48:18 +01:00
gtk_main_quit();
2010-11-13 10:05:28 +01:00
return false;
}
2010-11-13 10:05:28 +01:00
bool
sc_zoom(girara_session_t* session, girara_argument_t* argument, unsigned int t)
{
2011-04-18 17:55:50 +02:00
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
g_return_val_if_fail(argument != NULL, false);
g_return_val_if_fail(zathura->document != NULL, false);
2011-02-09 21:28:36 +01:00
/* retreive zoom step value */
int* value = girara_setting_get(zathura->ui.session, "zoom-step");
2011-02-09 21:28:36 +01:00
if (value == NULL) {
return false;
}
float zoom_step = *value / 100.0f;
if (argument->n == ZOOM_IN) {
2011-04-18 17:55:50 +02:00
zathura->document->scale += zoom_step;
2011-02-09 21:28:36 +01:00
} else if (argument->n == ZOOM_OUT) {
2011-04-18 17:55:50 +02:00
zathura->document->scale -= zoom_step;
2011-02-09 21:28:36 +01:00
} else {
2011-04-18 17:55:50 +02:00
zathura->document->scale = 1.0;
2011-02-09 21:28:36 +01:00
}
render_all(zathura);
2011-02-09 21:28:36 +01:00
2010-11-13 10:05:28 +01:00
return false;
}