1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/*
# vlshow.c
# Copyright (C) 2006 Joachim Breitner
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <pango/pango.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "config.h"
#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* for getopt_long */
#endif
#include <getopt.h>
#define min(x,y) ((x) < (y) ? (x) : (y))
#define AUTOHIDE_TIMEOUT 3
static gboolean quality = TRUE;
static gboolean need_resize = TRUE;
static int timeout_id=0;
static GtkWidget* window;
static GtkWidget* draw;
static GdkCursor *cursor;
static GtkWidget* quit;
static GtkWidget* tv;
static GtkWidget* entry_widget;
static GtkSettings* settings;
static GtkTextBuffer* tb;
static PangoFontDescription *font;
static PangoLayout* layout;
static char *foreground = NULL;
static char *background = NULL;
static char *fontdesc = NULL;
static rotation = 0; // 0 = normal, 1 = left, 2 = inverted, 3 = right
gboolean hide_entry(gpointer *user_data) {
gtk_widget_hide(entry_widget);
gtk_widget_grab_focus(draw);
gtk_widget_queue_draw(draw);
gdk_window_set_cursor(GTK_WIDGET(draw)->window, cursor);
return FALSE;
}
static void show_entry() {
if (timeout_id) {
g_source_remove(timeout_id);
timeout_id = 0;
}
gtk_widget_show(entry_widget);
gtk_widget_grab_focus(tv);
gdk_window_set_cursor(GTK_WIDGET(draw)->window, NULL);
timeout_id = g_timeout_add_seconds (AUTOHIDE_TIMEOUT, (GSourceFunc)hide_entry, NULL);
}
static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval, GdkModifierType modifier) {
if( gtk_text_buffer_get_char_count(tb) ) {
gtk_text_buffer_set_text(tb,"",-1);
show_entry();
} else {
gtk_main_quit();
}
}
static char *get_text() {
GtkTextIter start, end;
gtk_text_buffer_get_start_iter(tb,&start);
gtk_text_buffer_get_end_iter(tb,&end);
return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
}
static void hq(gboolean q, gboolean force){
if (q != quality)
if (q)
gtk_settings_set_long_property(settings,"gtk-xft-antialias",1,"Hier halt");
else
gtk_settings_set_long_property(settings,"gtk-xft-antialias",0,"Hier halt");
else
if (force)
gtk_widget_queue_draw(draw);
quality = q;
}
static void redraw() {
const char *text = pango_layout_get_text(layout);
if (strlen(text) > 0) {
GdkGC *gc = gtk_widget_get_style(draw)->fg_gc[GTK_STATE_NORMAL];
int w1, h1;
pango_layout_get_pixel_size(layout, &w1, &h1);
if (w1>0 && h1>0) {
int w2 = window->allocation.width;
int h2 = window->allocation.height;
int rw1, rh1;
if (rotation == 0 || rotation == 2) {
rw1 = w1;
rh1 = h1;
} else {
rw1 = h1;
rh1 = w1;
}
double s = min ((double)w2/rw1, (double)h2/rh1);
PangoMatrix matrix = PANGO_MATRIX_INIT;
// Move matrix to the screen center
pango_matrix_translate(&matrix, w2/2, h2/2);
// Scale as required
pango_matrix_scale(&matrix, s, s);
// Rotate if needed
pango_matrix_rotate (&matrix, rotation * 90);
// Move matrix so that text will be centered
//pango_matrix_translate(&matrix, -w1/2, -h1/2);
// Apply matrix
PangoContext *context = pango_layout_get_context(layout);
pango_context_set_matrix (context, &matrix);
pango_layout_context_changed (layout);
gdk_draw_layout(draw->window, gc,
(w2-(s*rw1))/2, (h2-(s*rh1))/2,layout);
// Reset matrix
PangoMatrix matrix2 = PANGO_MATRIX_INIT;
pango_context_set_matrix (context, &matrix2);
pango_layout_context_changed (layout);
hq(TRUE, FALSE);
}
}
}
static gboolean text_keypress(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
// forward signal to the text view
gboolean ret;
g_signal_emit_by_name(tv, "key-press-event", event, &ret);
gtk_widget_grab_focus(tv);
return ret;
}
static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
show_entry();
if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
gchar *txt = gtk_clipboard_wait_for_text(cb);
if (txt != NULL) {
gtk_text_buffer_set_text(tb,txt,-1);
g_free(txt);
}
}
return FALSE;
}
static void newtext() {
show_entry();
pango_layout_set_text(layout, get_text(), -1);
hq(FALSE, TRUE);
}
static void move_cursor(GtkTextView* tv, GtkMovementStep step, gint count, gboolean extend_selection, gpointer user_data) {
show_entry();
}
static struct option const long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{"foreground", required_argument, NULL, 'f'},
{"background", required_argument, NULL, 'b'},
{"font", required_argument, NULL, 'n'},
{"rotate", required_argument, NULL, 'r'},
{0,0,0,0}
};
static void usage(char *cmd) {
printf("Usage: %s [-h|--help] [-V|--version] [-f|--foreground=colordesc] [-b|--background=colordesc] [-n|--font=fontdesc] [-r|--rotate=0,1,2,3]\n", cmd);
}
static void version() {
printf("%s\n", PACKAGE_STRING);
}
int main(int argc, char **argv) {
GString *input;
int c;
int input_provided = 0;
while ((c = getopt_long (argc, argv, "hVf:b:n:r:", long_options, (int *) 0)) != EOF) {
switch (c) {
case 'h':
usage(argv[0]);
return 0;
break;
case 'V':
version();
return 0;
break;
case 'f':
foreground = optarg;
break;
case 'b':
background = optarg;
break;
case 'n':
fontdesc = optarg;
break;
case 'r':
rotation = atoi(optarg);
break;
default:
/* unknown switch received - at least
* give usage but continue and use the
* data */
usage(argv[0]);
break;
}
}
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
gtk_window_set_icon_name (GTK_WINDOW (window), "sm");
GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
gtk_widget_set_size_request(window, gdk_screen_get_width(screen),
gdk_screen_get_height(screen));
gtk_window_fullscreen(GTK_WINDOW(window));
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
settings = gtk_settings_get_default();
GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
GdkColor white, black;
gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
gdk_colormap_alloc_color(colormap, &black, TRUE, TRUE);
if (foreground != NULL) {
gdk_color_parse(foreground, &black);
} else {
gdk_color_parse("black", &black);
}
if (background != NULL) {
gdk_color_parse(background, &white);
} else {
gdk_color_parse("white", &white);
}
draw = gtk_drawing_area_new();
gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK|GDK_KEY_PRESS_MASK);
gtk_widget_set_size_request(draw,400,400);
gtk_widget_modify_bg(draw, GTK_STATE_NORMAL, &white);
gtk_widget_modify_fg(draw, GTK_STATE_NORMAL, &black);
g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
g_signal_connect(G_OBJECT(draw), "key-press-event", G_CALLBACK(text_keypress), NULL);
gtk_widget_set_can_focus(draw, TRUE);
GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
GdkColor color;
cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
tv = gtk_text_view_new();
tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
if (argc > optind)
if (!strcmp(argv[optind], "-") ) {
// read from stdin
gchar buf[1024];
int num;
input = g_string_new("");
while ((num = fread (buf, 1, sizeof(buf), stdin)) > 0) {
g_string_append_len(input, buf, num);
}
// remove trailing newline, if any
if ((input->len > 0) && (input->str[input->len - 1] == '\n')) {
g_string_truncate(input, input->len - 1);
}
input_provided++;
} else {
int i;
input = g_string_new("");
for (i = optind; i < argc; i++) {
g_string_append(input, argv[i]);
if (i < argc - 1) {
g_string_append(input, " ");
}
}
input_provided++;
}
else
input = g_string_new(":-)");
gtk_text_buffer_set_text(tb, input->str, input->len);
g_string_free(input, TRUE);
GtkTextIter start, end;
gtk_text_buffer_get_bounds(tb, &start, &end);
gtk_text_buffer_select_range(tb, &start, &end);
quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *vbox_button = gtk_vbox_new(FALSE, 0);
gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);
GtkWidget *hbox = gtk_hbox_new(FALSE,0);
entry_widget = hbox;
gtk_box_pack_start(GTK_BOX(hbox), tv, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);
GtkWidget *vbox = gtk_vbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE, TRUE, 0);
gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
font = pango_font_description_new();
if (fontdesc != NULL) {
pango_font_description_set_family(font, fontdesc);
} else {
pango_font_description_set_family(font, "sans-serif");
}
pango_font_description_set_size(font, 200*PANGO_SCALE);
layout = gtk_widget_create_pango_layout(window,get_text());
pango_layout_set_font_description(layout, font);
pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
GtkAccelGroup *accel = gtk_accel_group_new();
guint key;
GdkModifierType mod;
gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
gtk_accelerator_parse("Escape", &key, &mod);
gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
gtk_window_add_accel_group(GTK_WINDOW(window), accel);
gtk_widget_show_all(window);
g_signal_connect_after(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
g_signal_connect(G_OBJECT(tv), "move-cursor", G_CALLBACK(move_cursor), NULL);
if (!input_provided)
show_entry();
else
hide_entry(NULL);
gtk_main();
return 0;
}
|