PageRenderTime 63ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/html/examples/tests/canvas.c

https://gitlab.com/willemmali-c/iup
C | 440 lines | 323 code | 75 blank | 42 comment | 6 complexity | 539fc1009df8004d528061c280e8dc0c MD5 | raw file
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "iup.h"
  4. #include "iupkey.h"
  5. #include "iupdraw.h"
  6. static unsigned char matrx_img_cur_excel[15*15] =
  7. {
  8. 0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,
  9. 0,0,0,0,1,2,2,2,2,1,1,0,0,0,0,
  10. 0,0,0,0,1,2,2,2,2,1,1,0,0,0,0,
  11. 0,0,0,0,1,2,2,2,2,1,1,0,0,0,0,
  12. 1,1,1,1,1,2,2,2,2,1,1,1,1,1,0,
  13. 1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,
  14. 1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,
  15. 1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,
  16. 1,1,1,1,1,2,2,2,2,1,1,1,1,1,1,
  17. 0,1,1,1,1,2,2,2,2,1,1,1,1,1,1,
  18. 0,0,0,0,1,2,2,2,2,1,1,0,0,0,0,
  19. 0,0,0,0,1,2,2,2,2,1,1,0,0,0,0,
  20. 0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,
  21. 0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,
  22. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  23. };
  24. static unsigned char pixmap_cursor [ ] =
  25. {
  26. 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,
  27. 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,
  28. 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,
  29. 0,0,0,0,0,0,0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0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  45. 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,
  46. 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,
  47. 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,
  48. 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,
  49. 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,
  50. 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,
  51. 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,
  52. 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,
  53. 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,
  54. 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,
  55. 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,
  56. 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,
  57. 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
  58. } ;
  59. #define USE_IUPDRAW
  60. //#define USE_GDK
  61. /* draw a rectangle that has w=600 always, white background and a red X */
  62. #ifdef USE_IUPDRAW
  63. static void drawTest(Ihandle *ih, int posx)
  64. {
  65. int w, h;
  66. IupDrawBegin(ih);
  67. IupDrawGetSize(ih, &w, &h);
  68. /* white background */
  69. IupSetAttribute(ih, "DRAWCOLOR", "255 255 255");
  70. IupSetAttribute(ih, "DRAWSTYLE", "FILL");
  71. IupDrawRectangle(ih, 0, 0, w, h);
  72. w = 600; /* virtual size */
  73. /* red X */
  74. IupSetAttribute(ih, "DRAWCOLOR", "255 0 0");
  75. IupDrawLine(ih, -posx, 0, w - posx, h);
  76. IupDrawLine(ih, -posx, h, w - posx, 0);
  77. IupDrawEnd(ih);
  78. }
  79. #else
  80. #ifdef USE_GDK
  81. #include <gtk/gtk.h>
  82. #ifdef USE_GTK3
  83. static void drawTest(Ihandle *ih, int posx)
  84. {
  85. int w, h;
  86. GdkWindow* wnd = (GdkWindow*)IupGetAttribute(ih, "DRAWABLE");
  87. cairo_t* cr = (cairo_t*)IupGetAttribute(ih, "CAIRO_CR");
  88. w = gdk_window_get_width(wnd);
  89. h = gdk_window_get_height(wnd);
  90. /* white background */
  91. cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
  92. cairo_rectangle(cr, 0, 0, w, h);
  93. cairo_fill(cr);
  94. /* virtual size */
  95. w = 600;
  96. /* red X */
  97. cairo_set_source_rgba(cr, 1.0, 0, 0, 1.0);
  98. cairo_move_to(cr, -posx, 0);
  99. cairo_line_to(cr, w-posx, h);
  100. cairo_move_to(cr, -posx, h);
  101. cairo_line_to(cr, w-posx, 0);
  102. cairo_stroke(cr);
  103. }
  104. #else
  105. static void drawTest(Ihandle *ih, int posx)
  106. {
  107. GdkWindow* wnd = (GdkWindow*)IupGetAttribute(ih, "DRAWABLE");
  108. GdkGC* gc = gdk_gc_new(wnd);
  109. int w, h;
  110. GdkColor color;
  111. IupGetIntInt(ih, "DRAWSIZE", &w, &h);
  112. /* white background */
  113. color.red = 65535; color.green = 65535; color.blue = 65535;
  114. gdk_gc_set_rgb_fg_color(gc, &color);
  115. gdk_draw_rectangle(wnd, gc, TRUE, 0, 0, w, h);
  116. w = 600; /* virtual size */
  117. /* red X */
  118. color.red = 65535; color.green = 0; color.blue = 0;
  119. gdk_gc_set_rgb_fg_color(gc, &color);
  120. gdk_draw_line(wnd, gc, -posx, 0, w-posx, h);
  121. gdk_draw_line(wnd, gc, -posx, h, w-posx, 0);
  122. g_object_unref(gc);
  123. }
  124. #endif
  125. #else
  126. #ifdef WIN32
  127. #undef _WIN32_WINNT
  128. #define _WIN32_WINNT 0x0500
  129. #include <windows.h>
  130. static void drawTest(Ihandle *ih, int posx)
  131. {
  132. RECT rect;
  133. int w, h;
  134. HWND hWnd = (HWND)IupGetAttribute(ih, "HWND");
  135. HPEN oldPen;
  136. HDC hDC = GetDC(hWnd);
  137. IupGetIntInt(ih, "DRAWSIZE", &w, &h);
  138. SetRect(&rect, 0, 0, w, h);
  139. FillRect(hDC, &rect, GetStockObject(WHITE_BRUSH));
  140. oldPen = SelectObject(hDC, GetStockObject(DC_PEN));
  141. SetDCPenColor(hDC, RGB(255, 0, 0));
  142. w = 600; /* virtual size */
  143. MoveToEx(hDC, -posx, 0, NULL);
  144. LineTo(hDC, w-posx, h);
  145. MoveToEx(hDC, -posx, h, NULL);
  146. LineTo(hDC, w-posx, 0);
  147. SelectObject(hDC, oldPen);
  148. ReleaseDC(hWnd, hDC);
  149. }
  150. #else
  151. #include <X11/Xlib.h>
  152. #define xCOLOR8TO16(_x) (_x*257) /* 65535/255 = 257 */
  153. static unsigned long xGetPixel(Display* dpy, unsigned char cr, unsigned char cg, unsigned char cb)
  154. {
  155. XColor xc;
  156. xc.red = xCOLOR8TO16(cr);
  157. xc.green = xCOLOR8TO16(cg);
  158. xc.blue = xCOLOR8TO16(cb);
  159. xc.flags = DoRed | DoGreen | DoBlue;
  160. XAllocColor(dpy, DefaultColormap(dpy, XDefaultScreen(dpy)), &xc);
  161. return xc.pixel;
  162. }
  163. static void drawTest(Ihandle *ih, int posx)
  164. {
  165. int w, h;
  166. Display* dpy = (Display*)IupGetAttribute(ih, "XDISPLAY"); /* works for Motif and GTK */
  167. Drawable wnd = (Drawable)IupGetAttribute(ih, "XWINDOW");
  168. GC gc = XCreateGC(dpy, wnd, 0, NULL);
  169. IupGetIntInt(ih, "DRAWSIZE", &w, &h);
  170. XSetForeground(dpy, gc, xGetPixel(dpy, 255, 255, 255));
  171. XFillRectangle(dpy, wnd, gc, 0, 0, w, h);
  172. XSetForeground(dpy, gc, xGetPixel(dpy, 255, 0, 0));
  173. w = 600; /* virtual size */
  174. XDrawLine(dpy, wnd, gc, -posx, 0, w-posx, h);
  175. XDrawLine(dpy, wnd, gc, -posx, h, w-posx, 0);
  176. XFreeGC(dpy, gc);
  177. }
  178. #endif
  179. #endif
  180. #endif
  181. static int testDropData_cb(Ihandle *self, char* type, void* data, int len, int x, int y)
  182. {
  183. // Data is a text for this sample, so we can print it
  184. printf("\n DROPDATA_CB(type=%s, data=%s, size=%d, x=%d, y=%d)\n\n", type, (char*)data, len, x, y);
  185. return IUP_DEFAULT;
  186. }
  187. static int map_cb(Ihandle *ih)
  188. {
  189. printf("MAP_CB()\n");
  190. return IUP_DEFAULT;
  191. }
  192. static int action(Ihandle *ih, float posx, float posy)
  193. {
  194. printf("ACTION(posx=%.2f, posy=%.2f)\n", posx, posy);
  195. drawTest(ih, (int)posx);
  196. return IUP_DEFAULT;
  197. }
  198. static int scroll_cb(Ihandle *ih, int op, float posx, float posy)
  199. {
  200. char* op2str[12] = {"SBUP", "SBDN", "SBPGUP", "SBPGDN", "SBPOSV", "SBDRAGV",
  201. "SBLEFT", "SBRIGHT", "SBPGLEFT", "SBPGRIGHT", "SBPOSH", "SBDRAGH"};
  202. printf("SCROLL_CB(%s, posx=%.2f, posy=%.2f)\n", op2str[op], posx, posy);
  203. IupRedraw(ih, 0);
  204. return IUP_DEFAULT;
  205. }
  206. static int dropfiles_cb(Ihandle *ih, const char* filename, int num, int x, int y)
  207. {
  208. printf("DROPFILES_CB(%s, %d, x=%d, y=%d)\n", filename, num, x, y);
  209. return IUP_DEFAULT;
  210. }
  211. static int resize_cb(Ihandle *ih, int w, int h)
  212. {
  213. printf("RESIZE_CB(%d, %d) RASTERSIZE=%s DRAWSIZE=%s \n", w, h, IupGetAttribute(ih, "RASTERSIZE"), IupGetAttribute(ih, "DRAWSIZE"));
  214. /* update page size */
  215. IupSetfAttribute(ih, "DX", "%d", w);
  216. return IUP_DEFAULT;
  217. }
  218. static int getfocus_cb(Ihandle *ih)
  219. {
  220. printf("GETFOCUS_CB()\n");
  221. return IUP_DEFAULT;
  222. }
  223. static int help_cb(Ihandle* ih)
  224. {
  225. printf("HELP_CB()\n");
  226. return IUP_DEFAULT;
  227. }
  228. static int killfocus_cb(Ihandle *ih)
  229. {
  230. printf("KILLFOCUS_CB()\n");
  231. return IUP_DEFAULT;
  232. }
  233. static int focus_cb(Ihandle *ih, int focus)
  234. {
  235. printf("FOCUS_CB(%d)\n", focus);
  236. return IUP_DEFAULT;
  237. }
  238. static int leavewindow_cb(Ihandle *ih)
  239. {
  240. printf("LEAVEWINDOW_CB()\n");
  241. return IUP_DEFAULT;
  242. }
  243. static int enterwindow_cb(Ihandle *ih)
  244. {
  245. printf("ENTERWINDOW_CB()\n");
  246. return IUP_DEFAULT;
  247. }
  248. static int button_cb(Ihandle *ih,int but,int pressed,int x,int y,char* status)
  249. {
  250. if (but==IUP_BUTTON1 && pressed)
  251. IupSetAttribute(ih, "CURSOR", "CROSS");
  252. if (but==IUP_BUTTON1 && !pressed)
  253. IupSetAttribute(ih, "CURSOR", NULL);
  254. printf("BUTTON_CB(but=%c (%d), x=%d, y=%d [%s])\n",(char)but,pressed,x,y, status);
  255. return IUP_DEFAULT;
  256. }
  257. static int wheel_cb(Ihandle *ih,float delta,int x,int y,char* status)
  258. {
  259. printf("WHEEL_CB(delta=%.2f, x=%d, y=%d [%s])\n",delta,x,y, status);
  260. return IUP_DEFAULT;
  261. }
  262. static int motion_cb(Ihandle *ih,int x,int y,char* status)
  263. {
  264. printf("MOTION_CB(x=%d, y=%d [%s])\n",x,y, status);
  265. return IUP_DEFAULT;
  266. }
  267. char *iupKeyCodeToName(int code);
  268. static int k_any(Ihandle *ih, int c)
  269. {
  270. //if (c == K_a)
  271. // IupSetAttribute(ih, "POSX", "100");
  272. if (iup_isprint(c))
  273. printf("K_ANY(0x%X = %s \'%c\')\n", c, iupKeyCodeToName(c), (char)c);
  274. else
  275. printf("K_ANY(0x%X = %s)\n", c, iupKeyCodeToName(c));
  276. printf(" MODKEYSTATE(%s)\n", IupGetGlobal("MODKEYSTATE"));
  277. printf(" isShiftXkey(%d)\n", iup_isShiftXkey(c));
  278. printf(" isCtrlXkey(%d)\n", iup_isCtrlXkey(c));
  279. printf(" isAltXkey(%d)\n", iup_isAltXkey(c));
  280. return IUP_DEFAULT;
  281. }
  282. static int keypress_cb(Ihandle *ih, int c, int pressed)
  283. {
  284. if (iup_isprint(c))
  285. printf("KEYPRESS_CB(%d = %s \'%c\' (%d))\n", c, iupKeyCodeToName(c), (char)c, pressed);
  286. else
  287. printf("KEYPRESS_CB(%d = %s (%d))\n", c, iupKeyCodeToName(c), pressed);
  288. return IUP_DEFAULT;
  289. }
  290. void CanvasTest(void)
  291. {
  292. Ihandle *dlg, *canvas, *box, *image;
  293. //image = IupImage(32, 32, pixmap_cursor);
  294. //IupSetAttribute(image, "1", "255 0 0");
  295. //IupSetAttribute(image, "2", "128 0 0");
  296. //IupSetAttribute(image, "HOTSPOT", "21:10");
  297. //image = IupImage(15,15, matrx_img_cur_excel);
  298. //IupSetAttribute (image, "0", "BGCOLOR");
  299. //IupSetAttribute (image, "1", "0 0 0");
  300. //IupSetAttribute (image, "2", "255 255 255");
  301. //IupSetAttribute (image, "HOTSPOT", "7:7");
  302. box = IupVbox(NULL);
  303. IupSetAttribute(box, "MARGIN", "5x5");
  304. // IupSetAttribute(box, "CMARGIN", "30x30");
  305. canvas = IupCanvas(NULL);
  306. IupAppend(box, canvas);
  307. IupSetAttribute(canvas, "RASTERSIZE", "300x200");
  308. // IupSetAttribute(canvas, "EXPAND", "NO");
  309. IupSetAttribute(canvas, "TIP", "Canvas Tip");
  310. IupSetAttribute(canvas, "SCROLLBAR", "HORIZONTAL");
  311. //IupSetAttribute(canvas, "BGCOLOR", "0 255 0");
  312. // IupSetAttribute(canvas, "SCROLLBAR", "NO");
  313. // IupSetAttribute(canvas, "XAUTOHIDE", "NO");
  314. IupSetAttribute(canvas, "XMAX", "600");
  315. IupSetAttribute(canvas, "DX", "300"); /* use a 1x1 scale, this value is updated in RESIZE_CB,
  316. so when the canvas is larger than 600
  317. it will hide the scrollbar */
  318. //IupSetAttributeHandle(canvas, "CURSOR", image);
  319. IupSetAttribute(canvas, "DROPTARGET", "YES");
  320. IupSetAttribute(canvas, "DROPTYPES", "TEXT");
  321. IupSetCallback (canvas, "DROPDATA_CB", (Icallback)testDropData_cb);
  322. // IupSetAttribute(canvas, "BORDER", "NO");
  323. // IupSetAttribute(canvas, "CURSOR", "CROSS");
  324. IupSetCallback(canvas, "MAP_CB", (Icallback)map_cb);
  325. IupSetCallback(canvas, "KEYPRESS_CB", (Icallback)keypress_cb);
  326. IupSetCallback(canvas, "K_ANY", (Icallback)k_any);
  327. IupSetCallback(canvas, "HELP_CB", (Icallback)help_cb);
  328. IupSetCallback(canvas, "GETFOCUS_CB", (Icallback)getfocus_cb);
  329. IupSetCallback(canvas, "KILLFOCUS_CB", (Icallback)killfocus_cb);
  330. IupSetCallback(canvas, "FOCUS_CB", (Icallback)focus_cb);
  331. IupSetCallback(canvas, "ENTERWINDOW_CB", (Icallback)enterwindow_cb);
  332. IupSetCallback(canvas, "LEAVEWINDOW_CB", (Icallback)leavewindow_cb);
  333. IupSetCallback(canvas, "BUTTON_CB", (Icallback)button_cb);
  334. // IupSetCallback(canvas, "MOTION_CB", (Icallback)motion_cb);
  335. IupSetCallback(canvas, "SCROLL_CB", (Icallback)scroll_cb);
  336. IupSetCallback(canvas, "WHEEL_CB", (Icallback)wheel_cb);
  337. IupSetCallback(canvas, "RESIZE_CB", (Icallback)resize_cb);
  338. // IupSetAttribute(canvas, "BGCOLOR", "255 128 128");
  339. IupSetCallback(canvas, "ACTION", (Icallback)action);
  340. // Windows and GTK Only
  341. IupSetCallback(canvas, "DROPFILES_CB", (Icallback)dropfiles_cb);
  342. //{
  343. // Ihandle* cnv = IupCanvas(NULL);
  344. // IupAppend(box, cnv);
  345. // IupSetAttribute(cnv, "RASTERSIZE", "300x200");
  346. // IupSetAttribute(cnv, "CURSOR", "NONE");
  347. //}
  348. dlg = IupDialog(box);
  349. IupSetAttribute(dlg, "TITLE", "IupCanvas Test");
  350. printf("IupMap\n");
  351. IupMap(dlg);
  352. printf("IupShow\n");
  353. IupShow(dlg);
  354. // IupSetAttribute(canvas, "RASTERSIZE", NULL); /* release the minimum limitation */
  355. }
  356. #ifndef BIG_TEST
  357. int main(int argc, char* argv[])
  358. {
  359. IupOpen(&argc, &argv);
  360. CanvasTest();
  361. IupMainLoop();
  362. IupClose();
  363. return EXIT_SUCCESS;
  364. }
  365. #endif