aboutsummaryrefslogtreecommitdiff
path: root/vito.c
blob: 9d2b34400194b6e682db5b8431a373d8b4349f51 (plain)
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
#include <gtk/gtk.h>
#include <sys/time.h>

#define LEN(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))

static void aeGetTime(long *seconds, long *milliseconds)
{
    struct timeval tv;

    gettimeofday(&tv, NULL);
    *seconds = tv.tv_sec;
    *milliseconds = tv.tv_usec/1000;
}

GtkWidget *window;
GtkWidget *button;
GtkWidget *text_widget;
GtkTextBuffer *text_buffer;
GtkTextTag *tag;

char alphabet[] = "abcdefghijklmnopqrstuvwxyz_!?.";
char buf[1000000];

/* The current alphabetical letter. */
int current_letter = 0;
/* The current position in the buffer.
 *
 * FIXME: What to do if we run out of buffer space?. */
int current_position = 0;

/* Time the mouse was clicked (seconds) */
long pressed_time_sec = 0;
/* Time the mouse was clicked (milliseconds) */
long pressed_time_msec = 0;
/* Time the mouse was released (seconds) */
long released_time_sec = 0;
/* Time the mouse was released (milliseconds) */
long released_time_msec = 0;

static int pressed(GtkWidget *widget, gpointer   data)
{
    aeGetTime(&pressed_time_sec,&pressed_time_msec);
    g_print("mouse pressed\n");
    return TRUE;
}

static void write_buf(void)
{
    GtkTextIter start, end;

    text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
    sprintf(buf+current_position,"%c",alphabet[current_letter % LEN(alphabet)]);

    gtk_text_buffer_set_text (text_buffer, buf, -1);

    gtk_text_buffer_get_iter_at_offset (text_buffer, &start, strlen(buf)-1);
    gtk_text_buffer_get_iter_at_offset (text_buffer, &end, strlen(buf));
    gtk_text_buffer_apply_tag (text_buffer, tag, &start, &end);
}

static int released(GtkWidget *widget, gpointer   data)
{
    aeGetTime(&released_time_sec,&released_time_msec);
    long long diff = (released_time_sec - pressed_time_sec)*1000 + (released_time_msec - pressed_time_msec);

    if (diff > 10000) {
        current_position = 0;
        current_letter = 0;
    } else if (diff > 500) {
        if (alphabet[current_letter % LEN(alphabet)] == '_')
            sprintf(buf+current_position," ");
        else
            sprintf(buf+current_position,"%c",alphabet[current_letter % LEN(alphabet)]);
        current_position += 1;
        if (current_position > LEN(buf) - 1) {
            /* FIXME: better way to do this? */
            current_position = 0;
        }
        current_letter = 0;
    } else {
        current_letter += 1;
    }

    write_buf();

    g_print("mouse released\n");
    return TRUE;
}

static void activate (GtkApplication *app, gpointer user_data)
{
    GtkCssProvider *provider;
    GtkStyleContext *context;
    GtkGesture *gesture;

    text_buffer = gtk_text_buffer_new(NULL);
    gtk_text_buffer_set_text(text_buffer,"blah",4);

    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW (window), "Window");
    gtk_window_set_default_size(GTK_WINDOW (window), 200, 200);

    gesture = gtk_gesture_click_new();
    g_signal_connect(gesture, "pressed", G_CALLBACK (pressed), NULL);
    g_signal_connect(gesture, "released", G_CALLBACK (released), NULL);
    gtk_widget_add_controller(window, GTK_EVENT_CONTROLLER (gesture));

    text_widget = gtk_text_view_new();
    gtk_text_view_set_editable((GtkTextView *) text_widget, FALSE);
    gtk_text_view_set_cursor_visible((GtkTextView *) text_widget, TRUE);
    gtk_text_view_set_wrap_mode((GtkTextView *) text_widget, GTK_WRAP_CHAR);
    gtk_widget_set_can_focus(text_widget, FALSE);
    gtk_widget_set_can_target(text_widget, FALSE);
    text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));

    /* Change default font and color throughout the widget */
    provider = gtk_css_provider_new ();
    gtk_css_provider_load_from_data (provider,
                                     "textview {"
                                     " font: 144pt monospace;"
                                     "  color: black;"
                                     "}",
                                     -1);
    context = gtk_widget_get_style_context (text_widget);
    gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

    /* Use a tag to change the color for just one part of the widget */
    tag = gtk_text_buffer_create_tag(text_buffer, "red_foreground", "foreground", "red", NULL);  
    buf[0] = 'a';
    buf[1] = '\0';
    write_buf();
    gtk_window_set_child(GTK_WINDOW (window), text_widget);

    gtk_window_fullscreen(GTK_WINDOW(window));

    gtk_window_present(GTK_WINDOW (window));
}

int main(int argc, char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}