summaryrefslogtreecommitdiff
path: root/pickme.c
blob: 02899b0e92ca60d3cb56868b5760c8f2c8c4b6f0 (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
151
152
153
154
155
156
157
158
159
160
161
162
#include <HsFFI.h>

#include <stdlib.h>
#include <gtk/gtk.h>
#include "config.h"

GtkWidget *window;
GtkWidget *input;
GtkWidget *box;
GtkWidget *scwin;
GtkWidget *flowbox;
GtkEntryBuffer *input_buffer;
char *labels;
GtkWidget **buttons;

bool inited = false;

extern char *searchCharacterByName (const char *);
extern char *favoriteEmojis (void);

// forward declaration
void on_click (GtkWidget *widget, gpointer data);

void draw_results (GtkText *text, char *results)
{
  if (!inited)
    {
      scwin = gtk_scrolled_window_new ();
      flowbox = gtk_flow_box_new ();
      gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scwin), flowbox);
      gtk_box_append (GTK_BOX (box), scwin);
    }
  else
    {
      flowbox = gtk_flow_box_new ();
      gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scwin), flowbox);
    }

  inited = true;
  
  graphene_rect_t box_bounds;
  if (gtk_widget_compute_bounds (box, window, &box_bounds))
    {
      graphene_rect_t text_bounds;
      if (gtk_widget_compute_bounds (GTK_WIDGET (text), window, &text_bounds))
	{
	  int height = box_bounds.size.height - text_bounds.size.height - 10;
	  gtk_scrolled_window_set_max_content_height (GTK_SCROLLED_WINDOW (scwin), height);
	  gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (scwin), height);
	}
    }

  gsize buf_size = gtk_entry_buffer_get_bytes (input_buffer);
  size_t l = strlen (results);

  if (l == 0)
    {
      return;
    }
  
  wchar_t symbols[l];
  int i = 0;
  int o = 0;
  int o_ = 0;
  mbstowcs (symbols, results, l);

  if (buttons)
    {
      buttons = realloc (buttons, l * sizeof (GtkWidget *));
    }
  else
    {
      buttons = malloc (l * sizeof (GtkWidget *));
    }
  memset (buttons, 0, l * sizeof (GtkWidget *));
  
  if (labels)
    {
      labels = realloc (labels, 5*l);
    }
  else
    {
      labels = malloc (5*l);
    }
  memset (labels, 0, 5*l);
  
  while (symbols[i])
    {
      const  wchar_t hold[] = { symbols[i], 0 };
      o_ += wcstombs (&labels[o], hold, 4) +1;
      buttons[i] = gtk_button_new_with_label (&labels[o]);
      g_signal_connect (buttons[i], "clicked", G_CALLBACK (on_click), &labels[o]);
      gtk_flow_box_append ((GtkFlowBox *)flowbox, buttons[i]);
      ++i;
      o = o_;
    }
}

void
on_click (GtkWidget *widget, gpointer data)
{
  gdk_clipboard_set_text (gtk_widget_get_primary_clipboard (widget), (char *)data);
}

void
on_input (GtkText *text, gpointer user_data)
{
  const char *buffer_contents = gtk_entry_buffer_get_text (input_buffer);   
  char *results = searchCharacterByName (buffer_contents);
  draw_results (text, results);
}

void
activate (GtkApplication *app, gpointer user_data)
{
  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "pickme");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  input_buffer = gtk_entry_buffer_new (NULL, -1);
  input = gtk_text_new_with_buffer (input_buffer);
  g_signal_connect (input, "activate", G_CALLBACK (on_input), NULL);

  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
  gtk_box_set_homogeneous (GTK_BOX (box), FALSE);
  gtk_box_append (GTK_BOX (box), input);
  gtk_window_set_child (GTK_WINDOW (window), box);

  gtk_window_present (GTK_WINDOW (window));

  if (strcmp (favoriteEmojis (), ""))
    {
      draw_results (GTK_TEXT (input), favoriteEmojis ());
    }
}

void
shutdown (GtkApplication *app, gpointer user_data)
{
  if (!inited)
    {
      free (buttons);
      free (labels);
    }
}

int
main (int argc, char **argv)
{
  hs_init (&argc, &argv);
  
  GtkApplication *app;
  int res;

  app = gtk_application_new (APP_ID, G_APPLICATION_DEFAULT_FLAGS);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  g_signal_connect (app, "shutdown", G_CALLBACK (shutdown), NULL);
  res = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return res;
}