~leonerd/libtickit/trunk

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
#include "tickit.h"

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

static struct {
  int vis : 1;
  int blink : 1;
  unsigned int shape : 2;
} modes;

static void render_modes(TickitTerm *tt)
{
  tickit_term_goto(tt, 5, 3);
  tickit_term_print(tt, "Cursor visible:  ");
  tickit_term_print(tt, modes.vis   ? "| >On< |  Off  |" : "|  On  | >Off< |");

  tickit_term_goto(tt, 7, 3);
  tickit_term_print(tt, "Cursor blink:    ");
  tickit_term_print(tt, modes.blink ? "| >On< |  Off  |" : "|  On  | >Off< |");

  tickit_term_goto(tt, 9, 3);
  tickit_term_print(tt, "Cursor shape:    ");
  tickit_term_print(tt, modes.shape == TICKIT_CURSORSHAPE_BLOCK ? "| >Block< |  Under  |  Bar  |" :
                        modes.shape == TICKIT_CURSORSHAPE_UNDER ? "|  Block  | >Under< |  Bar  |" :
                                                                       "|  Block  |  Under  | >Bar< |");

  tickit_term_goto(tt, 20, 10);
  tickit_term_print(tt, "Cursor  >   <");
  tickit_term_goto(tt, 20, 20);
}

static int event(TickitTerm *tt, TickitEventFlags flags, void *_info, void *data)
{
  TickitMouseEventInfo *info = _info;

  if(info->type != TICKIT_MOUSEEV_PRESS || info->button != 1)
    return 0;

  if(info->line == 5) {
    if(info->col >= 21 && info->col <= 26)
      modes.vis = 1;
    else if(info->col >= 28 && info->col <= 34)
      modes.vis = 0;
    else
      return 0;

    tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORVIS, modes.vis);
  }

  if(info->line == 7) {
    if(info->col >= 21 && info->col <= 26)
      modes.blink = 1;
    else if(info->col >= 28 && info->col <= 34)
      modes.blink = 0;
    else
      return 0;

    tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORBLINK, modes.blink);
  }

  if(info->line == 9) {
    if(info->col >= 21 && info->col <= 29)
      modes.shape = TICKIT_CURSORSHAPE_BLOCK;
    else if(info->col >= 31 && info->col <= 39)
      modes.shape = TICKIT_CURSORSHAPE_UNDER;
    else if(info->col >= 40 && info->col <= 47)
      modes.shape = TICKIT_CURSORSHAPE_LEFT_BAR;
    else
      return 0;

    tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORSHAPE, modes.shape);
  }

  render_modes(tt);

  return 1;
}

static int later(Tickit *t, TickitEventFlags flags, void *_info, void *data)
{
  TickitTerm *tt = tickit_get_term(t);

  render_modes(tt);

  tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORVIS, modes.vis);

  return 1;
}

int main(int argc, char *argv[])
{
  Tickit *t = tickit_new_stdtty();

  TickitTerm *tt = tickit_get_term(t);
  if(!tt) {
    fprintf(stderr, "Cannot create TickitTerm - %s\n", strerror(errno));
    return 1;
  }

  tickit_term_setctl_int(tt, TICKIT_TERMCTL_MOUSE, TICKIT_TERM_MOUSEMODE_CLICK);

  tickit_term_bind_event(tt, TICKIT_TERM_ON_MOUSE, 0, event, NULL);

  tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORVIS,   (modes.vis = 1));
  tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORBLINK, (modes.blink = 1));
  tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORSHAPE, (modes.shape = TICKIT_CURSORSHAPE_BLOCK));

  tickit_watch_later(t, 0, &later, NULL);

  tickit_run(t);

  tickit_unref(t);

  return 0;
}