3 # Copyright (C) 2006 Joachim Breitner
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <pango/pango.h>
30 #define _GNU_SOURCE /* for getopt_long */
34 #define min(x,y) ((x) < (y) ? (x) : (y))
36 #define AUTOHIDE_TIMEOUT 5
38 static gboolean quality = TRUE;
39 static gboolean need_resize = TRUE;
41 static int timeout_id=0;
43 static GtkWidget* window;
44 static GtkWidget* draw;
45 static GdkCursor *cursor;
46 static GtkWidget* quit;
48 static GtkWidget* entry_widget;
49 static GtkSettings* settings;
50 static GtkTextBuffer* tb;
51 static PangoFontDescription *font;
52 static PangoLayout* layout;
53 static char *foreground = NULL;
54 static char *background = NULL;
55 static char *fontdesc = NULL;
56 static rotation = 0; // 0 = normal, 1 = left, 2 = inverted, 3 = right
58 gboolean hide_entry(gpointer *user_data) {
59 gtk_widget_hide(entry_widget);
60 gtk_widget_grab_focus(tv);
61 gtk_widget_queue_draw(draw);
62 gdk_window_set_cursor(GTK_WIDGET(draw)->window, cursor);
66 static void show_entry() {
68 g_source_remove(timeout_id);
71 gtk_widget_show(entry_widget);
72 gdk_window_set_cursor(GTK_WIDGET(draw)->window, NULL);
74 timeout_id = g_timeout_add_seconds (AUTOHIDE_TIMEOUT, (GSourceFunc)hide_entry, NULL);
77 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval, GdkModifierType modifier) {
78 if( gtk_text_buffer_get_char_count(tb) ) {
79 gtk_text_buffer_set_text(tb,"",-1);
86 static char *get_text() {
87 GtkTextIter start, end;
88 gtk_text_buffer_get_start_iter(tb,&start);
89 gtk_text_buffer_get_end_iter(tb,&end);
90 return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
93 static void hq(gboolean q, gboolean force){
96 gtk_settings_set_long_property(settings,"gtk-xft-antialias",1,"Hier halt");
98 gtk_settings_set_long_property(settings,"gtk-xft-antialias",0,"Hier halt");
101 gtk_widget_queue_draw(draw);
106 static void redraw() {
107 const char *text = pango_layout_get_text(layout);
108 if (strlen(text) > 0) {
109 GdkGC *gc = gtk_widget_get_style(draw)->fg_gc[GTK_STATE_NORMAL];
111 pango_layout_get_pixel_size(layout, &w1, &h1);
113 int w2 = window->allocation.width;
114 int h2 = window->allocation.height;
117 if (rotation == 0 || rotation == 2) {
125 double s = min ((double)w2/rw1, (double)h2/rh1);
128 PangoMatrix matrix = PANGO_MATRIX_INIT;
129 // Move matrix to the screen center
130 pango_matrix_translate(&matrix, w2/2, h2/2);
132 pango_matrix_scale(&matrix, s, s);
134 pango_matrix_rotate (&matrix, rotation * 90);
135 // Move matrix so that text will be centered
136 //pango_matrix_translate(&matrix, -w1/2, -h1/2);
139 PangoContext *context = pango_layout_get_context(layout);
140 pango_context_set_matrix (context, &matrix);
141 pango_layout_context_changed (layout);
143 gdk_draw_layout(draw->window, gc,
144 (w2-(s*rw1))/2, (h2-(s*rh1))/2,layout);
150 static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
152 if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
153 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
155 gchar *txt = gtk_clipboard_wait_for_text(cb);
157 gtk_text_buffer_set_text(tb,txt,-1);
165 static void newtext() {
167 pango_layout_set_text(layout, get_text(), -1);
171 static struct option const long_options[] =
173 {"help", no_argument, NULL, 'h'},
174 {"version", no_argument, NULL, 'V'},
175 {"foreground", required_argument, NULL, 'f'},
176 {"background", required_argument, NULL, 'b'},
177 {"font", required_argument, NULL, 'n'},
178 {"rotate", required_argument, NULL, 'r'},
182 static void usage(char *cmd) {
183 printf("Usage: %s [-h|--help] [-V|--version] [-f|--foreground=colordesc] [-b|--background=colordesc] [-n|--font=fontdesc] [-r|--rotate=0,1,2,3]\n", cmd);
186 static void version() {
187 printf("%s\n", PACKAGE_STRING);
190 int main(int argc, char **argv) {
193 int input_provided = 0;
195 while ((c = getopt_long (argc, argv, "hVf:b:n:r:", long_options, (int *) 0)) != EOF) {
219 rotation = atoi(optarg);
222 /* unknown switch received - at least
223 * give usage but continue and use the
230 gtk_init(&argc, &argv);
232 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
233 gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
234 gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
235 gtk_window_set_icon_name (GTK_WINDOW (window), "sm");
237 GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
238 gtk_widget_set_size_request(window, gdk_screen_get_width(screen),
239 gdk_screen_get_height(screen));
240 gtk_window_fullscreen(GTK_WINDOW(window));
241 g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
243 settings = gtk_settings_get_default();
244 GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
245 GdkColor white, black;
246 gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
247 gdk_colormap_alloc_color(colormap, &black, TRUE, TRUE);
248 if (foreground != NULL) {
249 gdk_color_parse(foreground, &black);
251 gdk_color_parse("black", &black);
253 if (background != NULL) {
254 gdk_color_parse(background, &white);
256 gdk_color_parse("white", &white);
259 draw = gtk_drawing_area_new();
260 gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK);
261 gtk_widget_set_size_request(draw,400,400);
262 gtk_widget_modify_bg(draw, GTK_STATE_NORMAL, &white);
263 gtk_widget_modify_fg(draw, GTK_STATE_NORMAL, &black);
264 g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
266 GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
268 cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
270 tv = gtk_text_view_new();
271 tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
274 if (!strcmp(argv[optind], "-") ) {
279 input = g_string_new("");
281 while ((num = fread (buf, 1, sizeof(buf), stdin)) > 0) {
282 g_string_append_len(input, buf, num);
285 // remove trailing newline, if any
286 if ((input->len > 0) && (input->str[input->len - 1] == '\n')) {
287 g_string_truncate(input, input->len - 1);
293 input = g_string_new("");
295 for (i = optind; i < argc; i++) {
296 g_string_append(input, argv[i]);
299 g_string_append(input, " ");
305 input = g_string_new(":-)");
307 gtk_text_buffer_set_text(tb, input->str, input->len);
308 GtkTextIter start, end;
309 gtk_text_buffer_get_bounds(tb, &start, &end);
310 gtk_text_buffer_select_range(tb, &start, &end);
312 quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
313 g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
315 GtkWidget *vbox_button = gtk_vbox_new(FALSE, 0);
316 gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);
318 GtkWidget *hbox = gtk_hbox_new(FALSE,0);
320 gtk_box_pack_start(GTK_BOX(hbox), tv, TRUE, TRUE, 0);
321 gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);
323 GtkWidget *vbox = gtk_vbox_new(FALSE,0);
324 gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE, TRUE, 0);
325 gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
327 gtk_container_add(GTK_CONTAINER(window), vbox);
329 font = pango_font_description_new();
330 if (fontdesc != NULL) {
331 pango_font_description_set_family(font, fontdesc);
333 pango_font_description_set_family(font, "sans-serif");
335 pango_font_description_set_size(font, 200*PANGO_SCALE);
337 layout = gtk_widget_create_pango_layout(window,get_text());
338 pango_layout_set_font_description(layout, font);
339 pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
341 GtkAccelGroup *accel = gtk_accel_group_new();
344 gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
345 gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
346 gtk_accelerator_parse("Escape", &key, &mod);
347 gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
348 gtk_window_add_accel_group(GTK_WINDOW(window), accel);
349 gtk_widget_show_all(window);
351 g_signal_connect_after(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
352 g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);