summaryrefslogtreecommitdiff
path: root/iup/test/canvas.c
blob: b01e5a675c03f981287be15928040401ba29f90f (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
#include <stdlib.h>
#include <stdio.h>
#include "iup.h"
#include "iupkey.h"

static unsigned char matrx_img_cur_excel[15*15] = 
  {
    0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,
    0,0,0,0,1,2,2,2,2,1,1,0,0,0,0,
    0,0,0,0,1,2,2,2,2,1,1,0,0,0,0,
    0,0,0,0,1,2,2,2,2,1,1,0,0,0,0,
    1,1,1,1,1,2,2,2,2,1,1,1,1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,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
} ;

//#define USE_GDK

/* draw a rectangle that has w=600 always, white background and a red X */
#ifdef USE_GDK
#include <gtk/gtk.h>
static void drawTest(Ihandle *ih, int posx)
{
  GtkWidget* widget = (GtkWidget*)IupGetAttribute(ih, "WID");
  GdkGC* gc = widget->style->fg_gc[GTK_WIDGET_STATE(widget)];
  int w, h;
  GdkColor color;

  IupGetIntInt(ih, "DRAWSIZE", &w, &h);

  /* white background */
  color.red = 65535;  color.green = 65535;  color.blue  = 65535;
  gdk_gc_set_rgb_fg_color(gc, &color);
  gdk_draw_rectangle(widget->window, gc, TRUE, 0, 0, w, h);

  /* red X */
  w = 600; /* virtual size */
  color.red = 65535;  color.green = 0;  color.blue  = 0;
  gdk_gc_set_rgb_fg_color(gc, &color);
  gdk_draw_line(widget->window, gc, -posx, 0, w-posx, h);
  gdk_draw_line(widget->window, gc, -posx, h, w-posx, 0);
}
#else
#ifdef WIN32
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#include <windows.h>
static void drawTest(Ihandle *ih, int posx)
{
  RECT rect;
  int w, h;
  HWND hWnd = (HWND)IupGetAttribute(ih, "HWND");
  HDC hDC = GetDC(hWnd);

  IupGetIntInt(ih, "DRAWSIZE", &w, &h);

  SetRect(&rect, 0, 0, w, h);
  FillRect(hDC, &rect, GetStockObject(WHITE_BRUSH));

  SelectObject(hDC, GetStockObject(DC_PEN));
  SetDCPenColor(hDC, RGB(255, 0, 0));

  w = 600; /* virtual size */
  MoveToEx(hDC, -posx, 0, NULL);
  LineTo(hDC, w-posx, h);
  MoveToEx(hDC, -posx, h, NULL);
  LineTo(hDC, w-posx, 0);

  ReleaseDC(hWnd, hDC);
}
#else
#include <X11/Xlib.h>

#define xCOLOR8TO16(_x) (_x*257)  /* 65535/255 = 257 */
static unsigned long xGetPixel(Display* dpy, unsigned char cr, unsigned char cg, unsigned char cb)
{
  XColor xc;
  xc.red = xCOLOR8TO16(cr);
  xc.green = xCOLOR8TO16(cg);
  xc.blue = xCOLOR8TO16(cb);
  xc.flags = DoRed | DoGreen | DoBlue;

  XAllocColor(dpy, DefaultColormap(dpy, XDefaultScreen(dpy)), &xc);
  return xc.pixel;
}

static void drawTest(Ihandle *ih, int posx)
{
  int w, h;
  Display* dpy = (Display*)IupGetAttribute(ih, "XDISPLAY");   /* works for Motif and GTK */
  Drawable wnd = (Drawable)IupGetAttribute(ih, "XWINDOW");
  GC gc = XCreateGC(dpy, wnd, 0, NULL);

  IupGetIntInt(ih, "DRAWSIZE", &w, &h);

  XSetForeground(dpy, gc, xGetPixel(dpy, 255, 255, 255));
  XFillRectangle(dpy, wnd, gc, 0, 0, w, h);

  XSetForeground(dpy, gc, xGetPixel(dpy, 255, 0, 0));

  w = 600; /* virtual size */
  XDrawLine(dpy, wnd, gc, -posx, 0, w-posx, h);
  XDrawLine(dpy, wnd, gc, -posx, h, w-posx, 0);

  XFreeGC(dpy, gc); 
}
#endif
#endif

static int map_cb(Ihandle *ih)
{
  printf("MAP_CB()\n");
  return IUP_DEFAULT;
}

static int action(Ihandle *ih, float posx, float posy)
{
  printf("ACTION(posx=%.2f, posy=%.2f)\n", posx, posy);
  drawTest(ih, (int)posx);
  return IUP_DEFAULT;
}

static int scroll_cb(Ihandle *ih, int op, float posx, float posy)
{
  char* op2str[12] = {"SBUP", "SBDN", "SBPGUP", "SBPGDN", "SBPOSV", "SBDRAGV",
                      "SBLEFT", "SBRIGHT", "SBPGLEFT", "SBPGRIGHT", "SBPOSH", "SBDRAGH"};
  printf("SCROLL_CB(%s, posx=%.2f, posy=%.2f)\n", op2str[op], posx, posy);
  IupRedraw(ih, 0);
  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 resize_cb(Ihandle *ih, int w, int h)
{
  printf("RESIZE_CB(%d, %d) RASTERSIZE=%s CLIENTSIZE=%s\n", w, h, IupGetAttribute(ih, "RASTERSIZE"), IupGetAttribute(ih, "CLIENTSIZE"));

  /* update page size */
  IupSetfAttribute(ih, "DX", "%d", w);

  return IUP_DEFAULT;
}

static int getfocus_cb(Ihandle *ih)
{
  printf("GETFOCUS_CB()\n");
  return IUP_DEFAULT;
}

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

static int focus_cb(Ihandle *ih, int focus)
{
  printf("FOCUS_CB(%d)\n", focus);
  return IUP_DEFAULT;
}

static int leavewindow_cb(Ihandle *ih)
{
  printf("LEAVEWINDOW_CB()\n");
  return IUP_DEFAULT;
}

static int enterwindow_cb(Ihandle *ih)
{
  printf("ENTERWINDOW_CB()\n");
  return IUP_DEFAULT;
}

static int button_cb(Ihandle *ih,int but,int pressed,int x,int y,char* status)
{
  if (but==IUP_BUTTON1 && pressed)
    IupSetAttribute(ih, "CURSOR", "CROSS");
  if (but==IUP_BUTTON1 && !pressed)
    IupSetAttribute(ih, "CURSOR", NULL);
  printf("BUTTON_CB(but=%c (%d), x=%d, y=%d [%s])\n",(char)but,pressed,x,y, status);
  return IUP_DEFAULT;
}

static int wheel_cb(Ihandle *ih,float delta,int x,int y,char* status)
{
  printf("WHEEL_CB(delta=%.2f, x=%d, y=%d [%s])\n",delta,x,y, status);
  return IUP_DEFAULT;
}

static int motion_cb(Ihandle *ih,int x,int y,char* status)
{
  printf("MOTION_CB(x=%d, y=%d [%s])\n",x,y, status);

  {
    static int count=0;
#ifdef WIN32
    Sleep(1000);
#else
    sleep(1); //or do anything that takes some time
#endif
    count++;
    printf("count(%d)\n",count);
  }

  return IUP_DEFAULT;
}

char *iupKeyCodeToName(int code);

static int k_any(Ihandle *ih, int c)
{
  if (c == K_a)
    IupSetAttribute(ih, "POSX", "100");

  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("  MODKEYSTATE(%s)\n", IupGetGlobal("MODKEYSTATE"));
  return IUP_DEFAULT;
}

static int keypress_cb(Ihandle *ih, int c, int pressed)
{
  if (iup_isprint(c))
    printf("KEYPRESS_CB(%d = %s \'%c\' (%d))\n", c, iupKeyCodeToName(c), (char)c, pressed);
  else
    printf("KEYPRESS_CB(%d = %s (%d))\n", c, iupKeyCodeToName(c), pressed);
  return IUP_DEFAULT;
}

void CanvasTest(void)
{
  Ihandle *dlg, *canvas, *box, *image;

  //image = IupImage(32, 32, pixmap_cursor);
  //IupSetAttribute(image, "1", "255 0 0"); 
  //IupSetAttribute(image, "2", "128 0 0"); 
  //IupSetAttribute(image, "HOTSPOT", "21:10");

  image = IupImage(15,15, matrx_img_cur_excel);
  IupSetAttribute (image, "0", "BGCOLOR"); 
  IupSetAttribute (image, "1", "0 0 0"); 
  IupSetAttribute (image, "2", "255 255 255"); 
  IupSetAttribute (image, "HOTSPOT", "7:7");

  box = IupVbox(NULL);
  IupSetAttribute(box, "MARGIN", "5x5");

  canvas = IupCanvas(NULL);
  IupAppend(box, canvas);
  IupSetAttribute(canvas, "RASTERSIZE", "300x200");
  IupSetAttribute(canvas, "TIP", "Canvas Tip");
  IupSetAttribute(canvas, "SCROLLBAR", "YES");
//  IupSetAttribute(canvas, "XAUTOHIDE", "NO");
  IupSetAttribute(canvas, "XMAX", "600");
  IupSetAttribute(canvas, "DX", "300");  /* use a 1x1 scale, this value is updated in RESIZE_CB,
                                           so when the canvas is larger than 600 
                                           it will hide the scrollbar */
  IupSetAttributeHandle(canvas, "CURSOR", image);

//  IupSetAttribute(canvas, "BORDER", "NO");
//  IupSetAttribute(canvas, "CURSOR", "CROSS");

  IupSetCallback(canvas, "MAP_CB",       (Icallback)map_cb);

  IupSetCallback(canvas, "KEYPRESS_CB",  (Icallback)keypress_cb);
//  IupSetCallback(canvas, "K_ANY",        (Icallback)k_any);
  IupSetCallback(canvas, "HELP_CB",      (Icallback)help_cb);

  IupSetCallback(canvas, "GETFOCUS_CB",  (Icallback)getfocus_cb); 
  IupSetCallback(canvas, "KILLFOCUS_CB", (Icallback)killfocus_cb);
  IupSetCallback(canvas, "FOCUS_CB", (Icallback)focus_cb);
  IupSetCallback(canvas, "ENTERWINDOW_CB", (Icallback)enterwindow_cb);
  IupSetCallback(canvas, "LEAVEWINDOW_CB", (Icallback)leavewindow_cb);

  IupSetCallback(canvas, "BUTTON_CB",    (Icallback)button_cb);
//  IupSetCallback(canvas, "MOTION_CB",    (Icallback)motion_cb);
  IupSetCallback(canvas, "SCROLL_CB",  (Icallback)scroll_cb);
//  IupSetCallback(canvas, "WHEEL_CB",    (Icallback)wheel_cb);

  IupSetCallback(canvas, "RESIZE_CB",    (Icallback)resize_cb);
//  IupSetAttribute(canvas, "BGCOLOR", "255 128 128");
  IupSetCallback(canvas, "ACTION",  (Icallback)action);
                   
  // Windows and GTK Only  
  IupSetCallback(canvas, "DROPFILES_CB", (Icallback)dropfiles_cb);

//{
//  Ihandle* cnv = IupCanvas(NULL);
//  IupAppend(box, cnv);
//  IupSetAttribute(cnv, "RASTERSIZE", "300x200");
//  IupSetAttribute(cnv, "CURSOR", "NONE");
//}

  dlg = IupDialog(box);
  IupSetAttribute(dlg, "TITLE", "IupCanvas Test");

  IupShow(dlg);
  IupSetAttribute(canvas, "RASTERSIZE", NULL);  /* release the minimum limitation */
}

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

  CanvasTest();

  IupMainLoop();

  IupClose();

  return EXIT_SUCCESS;
}
#endif