summaryrefslogtreecommitdiff
path: root/iup/test/text.c
blob: f1c29a2d1ee2dbf8d75e4f6d712de63df7906350 (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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include <stdlib.h>
#include <stdio.h>
#include <iup.h>
#include <iupkey.h>

static void text2multiline (Ihandle* ih, char* attribute)
{
  Ihandle *mltline = IupGetDialogChild(ih, "mltline");
  Ihandle *text = IupGetDialogChild(ih, "text");
  IupSetAttribute (mltline, attribute, IupGetAttribute (text, "VALUE"));
}

static void multiline2text (Ihandle* ih, char* attribute)
{
  Ihandle *mltline = IupGetDialogChild(ih, "mltline");
  Ihandle *text = IupGetDialogChild(ih, "text");
  IupSetAttribute (text, "VALUE", IupGetAttribute(mltline, attribute));
}

static int btn_append_cb (Ihandle* ih)
{
  text2multiline(ih, "APPEND"); 
  return IUP_DEFAULT;
}

static int btn_insert_cb (Ihandle* ih)
{
  text2multiline(ih, "INSERT"); 
  return IUP_DEFAULT;
}

static int btn_clip_cb (Ihandle* ih)
{
  text2multiline(ih, "CLIPBOARD"); 
  return IUP_DEFAULT;
}

int iupKeyNameToCode(const char *name);

static int btn_key_cb (Ihandle* ih)
{
  Ihandle *mltline = IupGetDialogChild(ih, "mltline");
  Ihandle *text = IupGetDialogChild(ih, "text");
  IupSetFocus(mltline);
  IupFlush();
  IupSetfAttribute(NULL, "KEY", "%d", iupKeyNameToCode(IupGetAttribute(text, "VALUE")));
  return IUP_DEFAULT;
}

static int btn_caret_cb (Ihandle* ih)
{
  Ihandle *opt = IupGetHandle("text2multi");
  if (IupGetInt(opt, "VALUE")) 
    text2multiline(ih, "CARET"); 
  else 
    multiline2text(ih, "CARET");
  return IUP_DEFAULT;
}

static int btn_readonly_cb (Ihandle* ih)
{
  Ihandle *opt = IupGetHandle("text2multi");
  if (IupGetInt(opt, "VALUE")) 
    text2multiline(ih, "READONLY"); 
  else 
    multiline2text(ih, "READONLY");
  return IUP_DEFAULT;
}

static int btn_selection_cb (Ihandle* ih)
{
  Ihandle *opt = IupGetHandle ("text2multi");
  if (IupGetInt (opt, "VALUE")) 
    text2multiline (ih, "SELECTION"); 
  else 
    multiline2text (ih, "SELECTION");
  return IUP_DEFAULT;
}

static int btn_selectedtext_cb (Ihandle* ih)
{
  Ihandle *opt = IupGetHandle ("text2multi");
  if (IupGetInt (opt, "VALUE")) 
    text2multiline (ih, "SELECTEDTEXT"); 
  else 
    multiline2text (ih, "SELECTEDTEXT");
  return IUP_DEFAULT;
}

static int btn_overwrite_cb (Ihandle* ih)
{
  Ihandle *opt = IupGetHandle ("text2multi");
  if (IupGetInt (opt, "VALUE")) 
    text2multiline (ih, "OVERWRITE"); 
  else 
    multiline2text (ih, "OVERWRITE");
  return IUP_DEFAULT;
}

static int btn_active_cb(Ihandle *ih)
{
  Ihandle *opt = IupGetHandle ("text2multi");
  if (IupGetInt (opt, "VALUE")) 
    text2multiline (ih, "ACTIVE"); 
  else 
    multiline2text (ih, "ACTIVE");
  return IUP_DEFAULT;
}

static int btn_remformat_cb(Ihandle *ih)
{
  text2multiline (ih, "REMOVEFORMATTING"); 
  return IUP_DEFAULT;
}

static int btn_nc_cb (Ihandle* ih)
{
  Ihandle *opt = IupGetHandle ("text2multi");
  if (IupGetInt (opt, "VALUE")) 
    text2multiline (ih, "NC"); 
  else 
    multiline2text (ih, "NC");
  return IUP_DEFAULT;
}

static int btn_value_cb (Ihandle* ih)
{
  Ihandle *opt = IupGetHandle ("text2multi");
  if (IupGetInt (opt, "VALUE")) 
    text2multiline (ih, "VALUE"); 
  else 
    multiline2text (ih, "VALUE");
  return IUP_DEFAULT;
}

static int btn_tabsize_cb (Ihandle* ih)
{
  Ihandle *opt = IupGetHandle ("text2multi");
  if (IupGetInt (opt, "VALUE")) 
    text2multiline (ih, "TABSIZE"); 
  else 
    multiline2text (ih, "TABSIZE");
  return IUP_DEFAULT;
}

static int k_f2(void)
{
  printf("K_F2\n");
  return IUP_DEFAULT;
}

static int file_open(void)
{
  char filename[100] = "";
  IupGetFile(filename);  // test key after dlg in multiline
  printf(filename);
  return IUP_DEFAULT;
}

char *iupKeyCodeToName(int code);

static int k_any(Ihandle *ih, int c)
{
  if (iup_isprint(c))
    printf("K_ANY(%d = %s \'%c\')\n", c, iupKeyCodeToName(c), (char)c);
  else
    printf("K_ANY(%d = %s)\n", c, iupKeyCodeToName(c));
  printf("  CARET(%s)\n", IupGetAttribute(ih, "CARET"));
  if (c == K_cA)
    return IUP_IGNORE;   // Sound a beep in Windows
  if (c == K_cP)
  {
    file_open();
    return IUP_IGNORE;   // Sound a beep in Windows
  }
  return IUP_CONTINUE;
}

static int action(Ihandle *ih, int c, char* after)
{
  if (iup_isprint(c))
    printf("ACTION(%d = %s \'%c\', %s)\n", c, iupKeyCodeToName(c), (char)c, after);
  else
    printf("ACTION(%d = %s, %s)\n", c, iupKeyCodeToName(c), after);
  if (c == K_i)
    return IUP_IGNORE;   // OK
  if (c == K_cD)
    return IUP_IGNORE;   // Sound a beep in Windows
  if (c == K_h)
    return K_j;
  return IUP_DEFAULT;
}

static int caret_cb(Ihandle *ih, int lin, int col, int pos)
{
  printf("CARET_CB(%d, %d - %d)\n", lin, col, pos);
  printf("  CARET(%s - %s)\n", IupGetAttribute(ih, "CARET"), IupGetAttribute(ih, "CARETPOS"));
  return IUP_DEFAULT;
}

static int getfocus_cb(void)
{
  printf("GETFOCUS_CB()\n");
  return IUP_DEFAULT;
}

static int help_cb(void)
{
  printf("HELP_CB()\n");
  return IUP_DEFAULT;
}
     
static int killfocus_cb(void)
{
  printf("KILLFOCUS_CB()\n");
  return IUP_DEFAULT;
}

static int leavewindow_cb(void)
{
  printf("LEAVEWINDOW_CB()\n");
  return IUP_DEFAULT;
}

static int enterwindow_cb(void)
{
  printf("ENTERWINDOW_CB()\n");
  return IUP_DEFAULT;
}

static int btn_def_esc_cb(void)
{
  printf("DEFAULTESC\n");
  return IUP_DEFAULT;
}

static int btn_def_enter_cb(void)
{
  printf("DEFAULTENTER\n");
  return IUP_DEFAULT;
}

static int dropfiles_cb(Ihandle *ih, const char* filename, int num, int x, int y)
{
  printf("DROPFILES_CB(%s, %d, x=%d, y=%d)\n", filename, num, x, y);
  return IUP_DEFAULT;
}

static int button_cb(Ihandle *ih,int but,int pressed,int x,int y,char* status)
{
  int lin, col, pos;
  printf("BUTTON_CB(but=%c (%d), x=%d, y=%d [%s])\n",(char)but,pressed,x,y, status);
  pos = IupConvertXYToPos(ih, x, y);
  IupTextConvertPosToLinCol(ih, pos, &lin, &col);
  printf("         (lin=%d, col=%d, pos=%d)\n", lin, col, pos);
  return IUP_DEFAULT;
}

static int motion_cb(Ihandle *ih,int x,int y,char* status)
{
  int lin, col, pos;
  printf("MOTION_CB(x=%d, y=%d [%s])\n",x,y, status);
  pos = IupConvertXYToPos(ih, x, y);
  IupTextConvertPosToLinCol(ih, pos, &lin, &col);
  printf("         (lin=%d, col=%d, pos=%d)\n", lin, col, pos);
  return IUP_DEFAULT;
}

void TextTest(void)
{
  int formatting = 0;
  Ihandle *dlg, *mltline, *text, *opt, *btn_def_enter, *btn_def_esc, *btn_active, *btn_overwrite,
          *btn_append, *btn_insert, *btn_caret, *btn_clip, *btn_key, *btn_readonly, *btn_tabsize,
          *btn_selection, *btn_selectedtext, *btn_nc, *btn_value, *lbl, *formattag, *btn_remformat;

//  IupSetGlobal("UTF8AUTOCONVERT", "NO");

  text = IupText (NULL);
  IupSetAttribute(text, "EXPAND", "HORIZONTAL");
//  IupSetAttribute(text, "VALUE", "Single Line Text");
  IupSetAttribute(text, "CUEBANNER", "Enter Attribute Value Here");
  IupSetAttribute(text, "NAME", "text");
  IupSetAttribute(text, "TIP", "Attribute Value");

  opt = IupToggle("Set/Get", NULL);
  IupSetAttribute (opt, "VALUE", "ON");
  IupSetHandle ("text2multi", opt);

  mltline = IupMultiLine(NULL);  
//  mltline = IupText(NULL);  
//  IupSetAttribute(mltline, "MULTILINE", "YES");
  IupSetAttribute(mltline, "NAME", "mltline");

  IupSetCallback(mltline, "DROPFILES_CB", (Icallback)dropfiles_cb);
  IupSetCallback(mltline, "BUTTON_CB",    (Icallback)button_cb);
//  IupSetCallback(mltline, "MOTION_CB",    (Icallback)motion_cb);
  IupSetCallback(mltline, "HELP_CB",      (Icallback)help_cb);
  IupSetCallback(mltline, "GETFOCUS_CB",  (Icallback)getfocus_cb); 
  IupSetCallback(mltline, "KILLFOCUS_CB", (Icallback)killfocus_cb);
  IupSetCallback(mltline, "ENTERWINDOW_CB", (Icallback)enterwindow_cb);
  IupSetCallback(mltline, "LEAVEWINDOW_CB", (Icallback)leavewindow_cb);
  //IupSetCallback(mltline, "ACTION", (Icallback)action);
  IupSetCallback(mltline, "K_ANY", (Icallback)k_any);
  IupSetCallback(mltline, "K_F2", (Icallback)k_f2);
  IupSetCallback(mltline, "CARET_CB", (Icallback)caret_cb);
//  IupSetAttribute(mltline, "WORDWRAP", "YES");
//  IupSetAttribute(mltline, "BORDER", "NO");
//  IupSetAttribute(mltline, "AUTOHIDE", "YES");
//  IupSetAttribute(mltline, "BGCOLOR", "255 0 128");
//  IupSetAttribute(mltline, "FGCOLOR", "0 128 192");
//  IupSetAttribute(mltline, "PADDING", "15x15");
//  IupSetAttribute(mltline, "VALUE", "First Line\nSecond Line Big Big Big\nThird Line\nmore\nmore\nçãõáóé"); // UTF-8
  IupSetAttribute(mltline, "VALUE", "First Line\nSecond Line Big Big Big\nThird Line\nmore\nmore\nçãõáóé"); // Windows-1252
  IupSetAttribute(mltline, "TIP", "First Line\nSecond Line\nThird Line");
//  IupSetAttribute(mltline, "FONT", "Helvetica, 14");
//  IupSetAttribute(mltline, "MASK", IUP_MASK_FLOAT);
//  IupSetAttribute(mltline, "FILTER", "UPPERCASE");
//  IupSetAttribute(mltline, "ALIGNMENT", "ACENTER");

  /* Turns on multiline expand and text horizontal expand */
  IupSetAttribute(mltline, "SIZE", "80x40");
  IupSetAttribute(mltline, "EXPAND", "YES");

  formatting = 0;
  if (formatting)          /* just to make easier to comment this section */
  {
    /* formatting before Map */
    IupSetAttribute(mltline, "FORMATTING", "YES");

    formattag = IupUser();
    IupSetAttribute(formattag, "ALIGNMENT", "CENTER");
    IupSetAttribute(formattag, "SPACEAFTER", "10");
    IupSetAttribute(formattag, "FONTSIZE", "24");
    IupSetAttribute(formattag, "SELECTION", "3,1:3,50");
    IupSetAttribute(mltline, "ADDFORMATTAG_HANDLE", (char*)formattag);

    formattag = IupUser();
    IupSetAttribute(formattag, "BGCOLOR", "255 128 64");
    IupSetAttribute(formattag, "UNDERLINE", "SINGLE");
    IupSetAttribute(formattag, "WEIGHT", "BOLD");
    IupSetAttribute(formattag, "SELECTION", "3,7:3,11");
    IupSetAttribute(mltline, "ADDFORMATTAG_HANDLE", (char*)formattag);
  }

  /* Creates buttons */
//  btn_append = IupButton ("APPEND çãõáóé", NULL);   // Windows-1252
//  btn_append = IupButton ("APPEND çãõáóé", NULL);  // UTF-8
  btn_append = IupButton ("&APPEND", NULL);
  btn_insert = IupButton ("INSERT", NULL);
  btn_caret = IupButton ("CARET", NULL);
  btn_readonly = IupButton ("READONLY", NULL);
  btn_selection = IupButton ("SELECTION", NULL);
  btn_selectedtext = IupButton ("SELECTEDTEXT", NULL);
  btn_nc = IupButton ("NC", NULL);
  btn_value = IupButton ("VALUE", NULL);
  btn_tabsize = IupButton ("TABSIZE", NULL);
  btn_clip = IupButton ("CLIPBOARD", NULL);
  btn_key = IupButton ("KEY", NULL);
  btn_def_enter = IupButton ("Default Enter", NULL);
  btn_def_esc = IupButton ("Default Esc", NULL);
  btn_active = IupButton("ACTIVE", NULL);
  btn_remformat = IupButton("REMOVEFORMATTING", NULL);
  btn_overwrite = IupButton("OVERWRITE", NULL);

  IupSetAttribute(btn_append, "TIP", "First Line\nSecond Line\nThird Line");

  /* Registers callbacks */
  IupSetCallback(btn_append, "ACTION", (Icallback) btn_append_cb);
  IupSetCallback(btn_insert, "ACTION", (Icallback) btn_insert_cb);
  IupSetCallback(btn_caret, "ACTION", (Icallback) btn_caret_cb);
  IupSetCallback(btn_readonly, "ACTION", (Icallback) btn_readonly_cb);
  IupSetCallback(btn_selection, "ACTION", (Icallback) btn_selection_cb);
  IupSetCallback(btn_selectedtext, "ACTION", (Icallback) btn_selectedtext_cb);
  IupSetCallback(btn_nc, "ACTION", (Icallback) btn_nc_cb);
  IupSetCallback(btn_value, "ACTION", (Icallback) btn_value_cb);
  IupSetCallback(btn_tabsize, "ACTION", (Icallback) btn_tabsize_cb);
  IupSetCallback(btn_clip, "ACTION", (Icallback) btn_clip_cb);
  IupSetCallback(btn_key, "ACTION", (Icallback) btn_key_cb);
  IupSetCallback(btn_def_enter, "ACTION", (Icallback) btn_def_enter_cb);
  IupSetCallback(btn_def_esc, "ACTION", (Icallback) btn_def_esc_cb);
  IupSetCallback(btn_active, "ACTION", (Icallback) btn_active_cb);
  IupSetCallback(btn_remformat, "ACTION", (Icallback) btn_remformat_cb);
  IupSetCallback(btn_overwrite, "ACTION", (Icallback) btn_overwrite_cb);

  lbl = IupLabel("&Multiline:");
  IupSetAttribute(lbl, "PADDING", "2x2");

  /* Creates dlg */
  dlg = IupDialog(IupVbox(lbl,
                          mltline, 
                          IupHbox (text, opt, NULL),
                          IupHbox (btn_append, btn_insert, btn_caret, btn_readonly, btn_selection, NULL),
                          IupHbox (btn_selectedtext, btn_nc, btn_value, btn_tabsize, btn_clip, btn_key, NULL), 
                          IupHbox (btn_def_enter, btn_def_esc, btn_active, btn_remformat, btn_overwrite, NULL), 
                          NULL));
  IupSetCallback(dlg, "K_cO", (Icallback)file_open);
  IupSetAttribute(dlg, "TITLE", "IupText Test");
  IupSetAttribute(dlg, "MARGIN", "10x10");
  IupSetAttribute(dlg, "GAP", "5");
  IupSetAttributeHandle(dlg, "DEFAULTENTER", btn_def_enter);
  IupSetAttributeHandle(dlg, "DEFAULTESC", btn_def_esc);
  IupSetAttribute(dlg, "SHRINK", "YES");

  if (formatting)          /* just to make easier to comment this section */
  {
    IupMap(dlg);
    /* formatting after Map */

    formattag = IupUser();
    IupSetAttribute(formattag, "ITALIC", "YES");
    IupSetAttribute(formattag, "STRIKEOUT", "YES");
    IupSetAttribute(formattag, "SELECTION", "2,1:2,12");
    IupSetAttribute(mltline, "ADDFORMATTAG_HANDLE", (char*)formattag);
  }

  /* Shows dlg in the center of the screen */
  IupShowXY(dlg, IUP_CENTER, IUP_CENTER);
}

#ifndef BIG_TEST
int main(int argc, char* argv[])
{
  IupOpen(&argc, &argv);

  TextTest();

  IupMainLoop();

  IupClose();

  return EXIT_SUCCESS;
}
#endif