PageRenderTime 133ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/demos/freedraw.c

#
C | 449 lines | 290 code | 96 blank | 63 comment | 9 complexity | ced97c556fa51b5865f94d22b0bb9a09 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * This file is part of XForms.
  3. *
  4. * XForms is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU Lesser General Public License as
  6. * published by the Free Software Foundation; either version 2.1, or
  7. * (at your option) any later version.
  8. *
  9. * XForms is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with XForms; see the file COPYING. If not, write to
  16. * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
  17. * MA 02111-1307, USA.
  18. */
  19. /*
  20. * Demo showing the use of a free obejct
  21. *
  22. * This file is part of xforms package
  23. * T.C. Zhao and M. Overmars
  24. *
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include <stdlib.h>
  30. #include "include/forms.h"
  31. /**** Forms and Objects ****/
  32. typedef struct {
  33. FL_FORM * drawfree;
  34. void * vdata;
  35. char * cdata;
  36. long ldata;
  37. FL_OBJECT * freeobj;
  38. FL_OBJECT * figgrp;
  39. FL_OBJECT * colgrp;
  40. FL_OBJECT * colorobj;
  41. FL_OBJECT * rsli;
  42. FL_OBJECT * gsli;
  43. FL_OBJECT * bsli;
  44. FL_OBJECT * miscgrp;
  45. FL_OBJECT * sizegrp;
  46. FL_OBJECT * hsli;
  47. FL_OBJECT * wsli;
  48. FL_OBJECT * drobj[ 3 ];
  49. } FD_drawfree;
  50. extern FD_drawfree * create_form_drawfree( void );
  51. static FD_drawfree * drawui;
  52. extern void draw_initialize( FD_drawfree * );
  53. static int max_w = 150,
  54. max_h = 150;
  55. static Display *dpy;
  56. /***************************************
  57. ***************************************/
  58. int
  59. main( int argc,
  60. char * argv[ ] )
  61. {
  62. dpy = fl_initialize( &argc, argv, "FormDemo", 0, 0 );
  63. drawui = create_form_drawfree( );
  64. fl_set_object_color( drawui->colorobj, FL_FREE_COL1, FL_FREE_COL1 );
  65. draw_initialize( drawui );
  66. fl_show_form( drawui->drawfree, FL_PLACE_CENTER | FL_FREE_SIZE,
  67. FL_FULLBORDER, "FreeObject" );
  68. fl_do_forms( );
  69. fl_free( drawui );
  70. fl_finish( );
  71. return 0;
  72. }
  73. /* Structure mantainace */
  74. typedef void ( *DrawFunc )( int, int, int, int, int, unsigned long );
  75. /***************************************
  76. ***************************************/
  77. void
  78. draw_triangle( int fill,
  79. int x,
  80. int y,
  81. int w,
  82. int h,
  83. unsigned long col )
  84. {
  85. XPoint xpoint[ 4 ];
  86. GC gc = fl_state[ fl_vmode ].gc[ 0 ];
  87. Window win = fl_winget( );
  88. xpoint[ 0 ].x = x;
  89. xpoint[ 1 ].x = x + w / 2;
  90. xpoint[ 2 ].x = x + w - 1;
  91. xpoint[ 0 ].y = y + h - 1;
  92. xpoint[ 1 ].y = y;
  93. xpoint[ 2 ].y = y + h - 1;
  94. XSetForeground( dpy, gc, fl_get_pixel( col ) );
  95. if ( fill )
  96. XFillPolygon( dpy, win, gc, xpoint, 3, Nonconvex, Unsorted );
  97. else
  98. {
  99. xpoint[ 3 ].x = xpoint[ 0 ].x;
  100. xpoint[ 3 ].y = xpoint[ 0 ].y;
  101. XDrawLines( dpy, win, gc, xpoint, 4, CoordModeOrigin );
  102. }
  103. }
  104. static DrawFunc drawfunc[ ] = { fl_oval,
  105. fl_rectangle,
  106. draw_triangle
  107. };
  108. typedef struct
  109. {
  110. DrawFunc drawit;
  111. int x,
  112. y,
  113. w,
  114. h,
  115. fill,
  116. c[ 3 ];
  117. int newfig;
  118. FL_COLOR col;
  119. } DrawFigure;
  120. static DrawFigure saved_figure[ 800 ],
  121. *cur_fig;
  122. /***************************************
  123. ***************************************/
  124. void draw_initialize( FD_drawfree * ui )
  125. {
  126. fl_set_form_minsize( ui->drawfree, 530, 490);
  127. fl_set_object_gravity( ui->colgrp, WestGravity, WestGravity );
  128. fl_set_object_gravity( ui->sizegrp, SouthWestGravity, SouthWestGravity );
  129. fl_set_object_gravity( ui->figgrp, NorthWestGravity, NorthWestGravity );
  130. fl_set_object_gravity( ui->miscgrp, SouthGravity, SouthGravity );
  131. fl_set_object_resize( ui->miscgrp, FL_RESIZE_NONE );
  132. cur_fig = saved_figure;
  133. cur_fig->c[ 0 ] = cur_fig->c[ 1 ] = cur_fig->c[ 2 ] = 127,
  134. cur_fig->w = cur_fig->h = 30;
  135. cur_fig->drawit = fl_oval;
  136. cur_fig->fill = 1;
  137. cur_fig->col = FL_FREE_COL1 + 1;
  138. fl_mapcolor( FL_FREE_COL1,
  139. cur_fig->c[ 0 ], cur_fig->c[ 1 ], cur_fig->c[ 2 ] );
  140. fl_mapcolor( cur_fig->col,
  141. cur_fig->c[ 0 ], cur_fig->c[ 1 ], cur_fig->c[ 2 ] );
  142. fl_set_slider_bounds( ui->wsli, 1, max_w );
  143. fl_set_slider_bounds( ui->hsli, 1, max_h );
  144. fl_set_slider_precision( ui->wsli, 0 );
  145. fl_set_slider_precision( ui->hsli, 0 );
  146. fl_set_slider_value( ui->wsli, cur_fig->w );
  147. fl_set_slider_value( ui->hsli, cur_fig->h );
  148. /* color sliders */
  149. fl_set_slider_bounds( ui->rsli, 1.0, 0 );
  150. fl_set_slider_bounds( ui->gsli, 1.0, 0 );
  151. fl_set_slider_bounds( ui->bsli, 1.0, 0 );
  152. /* intial drawing function */
  153. fl_set_button( ui->drobj[ 0 ], 1 );
  154. /* Setup the color slider so we can find out colorobject from
  155. the callback funtions. This is not necessary as drawui
  156. is static, this is done to show how to access other objects
  157. from an object callback function */
  158. ui->rsli->u_vdata = ui;
  159. ui->gsli->u_vdata = ui;
  160. ui->bsli->u_vdata = ui;
  161. }
  162. /***************************************
  163. ***************************************/
  164. void
  165. switch_object( FL_OBJECT * ob FL_UNUSED_ARG,
  166. long which )
  167. {
  168. cur_fig->drawit = drawfunc[ which ];
  169. }
  170. /***************************************
  171. ***************************************/
  172. void
  173. change_color( FL_OBJECT * ob,
  174. long which )
  175. {
  176. cur_fig->c[ which ] = fl_get_slider_value(ob) * 255.01;
  177. fl_mapcolor( cur_fig->col,
  178. cur_fig->c[ 0 ], cur_fig->c[ 1 ], cur_fig->c[ 2 ] );
  179. fl_mapcolor( FL_FREE_COL1,
  180. cur_fig->c[ 0 ], cur_fig->c[ 1 ], cur_fig->c[ 2 ] );
  181. fl_redraw_object( ( ( FD_drawfree * ) ob->u_vdata )->colorobj );
  182. }
  183. /***************************************
  184. ***************************************/
  185. void
  186. fill_cb( FL_OBJECT * ob,
  187. long notused FL_UNUSED_ARG )
  188. {
  189. cur_fig->fill = !fl_get_button( ob );
  190. }
  191. /***************************************
  192. ***************************************/
  193. void
  194. change_size( FL_OBJECT * ob,
  195. long which )
  196. {
  197. if ( which == 0 )
  198. cur_fig->w = fl_get_slider_value( ob );
  199. else
  200. cur_fig->h = fl_get_slider_value( ob );
  201. }
  202. /***************************************
  203. ***************************************/
  204. void
  205. refresh_cb( FL_OBJECT * ob FL_UNUSED_ARG,
  206. long which FL_UNUSED_ARG )
  207. {
  208. fl_redraw_object( drawui->freeobj );
  209. }
  210. /***************************************
  211. ***************************************/
  212. void
  213. clear_cb( FL_OBJECT * ob FL_UNUSED_ARG,
  214. long notused FL_UNUSED_ARG )
  215. {
  216. saved_figure[ 0 ] = *cur_fig;
  217. cur_fig = saved_figure;
  218. fl_redraw_object( drawui->freeobj );
  219. }
  220. /***************************************
  221. * The routine that does drawing
  222. ***************************************/
  223. int
  224. freeobject_handler( FL_OBJECT * ob,
  225. int event,
  226. FL_Coord mx,
  227. FL_Coord my,
  228. int key,
  229. void * xev FL_UNUSED_ARG )
  230. {
  231. DrawFigure *dr;
  232. switch ( event )
  233. {
  234. case FL_DRAW:
  235. if ( cur_fig->newfig == 1 )
  236. {
  237. cur_fig->drawit( cur_fig->fill,
  238. cur_fig->x + ob->x,
  239. cur_fig->y + ob->y,
  240. cur_fig->w, cur_fig->h, cur_fig->col );
  241. }
  242. else
  243. {
  244. fl_draw_box( ob->boxtype, ob->x, ob->y, ob->w,
  245. ob->h, ob->col1, ob->bw );
  246. for ( dr = saved_figure; dr < cur_fig; dr++ )
  247. {
  248. dr->drawit( dr->fill, dr->x + ob->x,
  249. dr->y + ob->y,
  250. dr->w, dr->h, dr->col);
  251. }
  252. }
  253. cur_fig->newfig = 0;
  254. break;
  255. case FL_PUSH:
  256. if ( key != 2 )
  257. {
  258. cur_fig->x = mx - cur_fig->w / 2;
  259. cur_fig->y = my - cur_fig->h / 2;
  260. /* convert position to relative to the free object */
  261. cur_fig->x -= ob->x;
  262. cur_fig->y -= ob->y;
  263. cur_fig->newfig = 1;
  264. fl_redraw_object( ob );
  265. *(cur_fig+1) = *cur_fig;
  266. fl_mapcolor( cur_fig->col + 1, cur_fig->c[ 0 ],
  267. cur_fig->c[ 1 ], cur_fig->c[ 2 ] );
  268. cur_fig++;
  269. cur_fig->col++;
  270. }
  271. break;
  272. }
  273. return 0;
  274. }
  275. /***************************************
  276. ***************************************/
  277. FD_drawfree *
  278. create_form_drawfree( void )
  279. {
  280. FL_OBJECT *obj;
  281. FD_drawfree *fdui = fl_calloc(1, sizeof *fdui );
  282. fdui->drawfree = fl_bgn_form( FL_NO_BOX, 530, 490 );
  283. fl_add_box( FL_UP_BOX, 0, 0, 530, 490, "" );
  284. obj = fl_add_frame( FL_DOWN_FRAME, 145, 30, 370, 405, "" );
  285. fl_set_object_gravity( obj, FL_NorthWest, FL_SouthEast );
  286. fdui->freeobj = obj = fl_add_free( FL_NORMAL_FREE, 145, 30, 370, 405,"",
  287. freeobject_handler );
  288. fl_set_object_gravity( obj, FL_NorthWest, FL_SouthEast );
  289. obj = fl_add_checkbutton( FL_PUSH_BUTTON, 15, 25, 100, 35, "Outline" );
  290. fl_set_object_color( obj, FL_MCOL,FL_BLUE );
  291. fl_set_object_gravity( obj, FL_NorthWest, FL_NorthWest );
  292. fl_set_object_callback( obj, fill_cb, 0 );
  293. fdui->figgrp = fl_bgn_group( );
  294. fdui->drobj[ 0 ] = obj = fl_add_button( FL_RADIO_BUTTON, 10, 60, 40, 40,
  295. "@#circle" );
  296. fl_set_object_lcolor( obj, FL_YELLOW );
  297. fl_set_object_callback( obj, switch_object, 0 );
  298. fdui->drobj[ 1 ] = obj = fl_add_button( FL_RADIO_BUTTON, 50, 60, 40, 40,
  299. "@#square" );
  300. fl_set_object_lcolor( obj, FL_YELLOW );
  301. fl_set_object_callback( obj, switch_object, 1 );
  302. fdui->drobj[ 2 ] = obj = fl_add_button( FL_RADIO_BUTTON, 90, 60, 40, 40,
  303. "@#8>" );
  304. fl_set_object_lcolor( obj, FL_YELLOW );
  305. fl_set_object_callback( obj, switch_object, 2 );
  306. fl_end_group( );
  307. fdui->colgrp = fl_bgn_group();
  308. fdui->colorobj = fl_add_box( FL_BORDER_BOX, 25, 140, 90, 25, "" );
  309. fdui->rsli = obj = fl_add_slider( FL_VERT_FILL_SLIDER, 25, 170, 30, 125,
  310. "" );
  311. fl_set_object_color( obj, FL_COL1, FL_RED );
  312. fl_set_object_callback( obj, change_color, 0 );
  313. fl_set_slider_return( obj, FL_RETURN_CHANGED );
  314. fdui->gsli = obj = fl_add_slider( FL_VERT_FILL_SLIDER, 55, 170, 30, 125,
  315. "" );
  316. fl_set_object_color( obj, FL_COL1, FL_GREEN );
  317. fl_set_object_callback( obj, change_color, 1 );
  318. fl_set_slider_return( obj, FL_RETURN_CHANGED );
  319. fdui->bsli = obj = fl_add_slider( FL_VERT_FILL_SLIDER, 85, 170, 30, 125,
  320. "" );
  321. fl_set_object_color( obj, FL_COL1, FL_BLUE );
  322. fl_set_object_callback( obj, change_color, 2 );
  323. fl_set_slider_return( obj, FL_RETURN_CHANGED );
  324. fl_end_group( );
  325. fdui->miscgrp = fl_bgn_group( );
  326. obj = fl_add_button( FL_NORMAL_BUTTON, 420, 455, 105, 30, "Quit");
  327. fl_set_button_shortcut( obj,"Qq#q", 1 );
  328. obj = fl_add_button( FL_NORMAL_BUTTON, 280, 445, 105, 30, "Refresh" );
  329. fl_set_object_callback( obj, refresh_cb, 0 );
  330. obj = fl_add_button( FL_NORMAL_BUTTON, 165, 445, 105, 30, "Clear" );
  331. fl_set_object_callback( obj, clear_cb, 0 );
  332. fl_end_group( );
  333. fdui->sizegrp = fl_bgn_group( );
  334. fdui->hsli = obj = fl_add_valslider( FL_HOR_SLIDER, 15, 410, 120, 25,
  335. "Height" );
  336. fl_set_object_lalign( obj, FL_ALIGN_TOP );
  337. fl_set_object_callback( obj, change_size, 1 );
  338. fl_set_slider_return( obj, FL_RETURN_CHANGED );
  339. fdui->wsli = obj = fl_add_valslider( FL_HOR_SLIDER, 15, 370, 120, 25,
  340. "Width" );
  341. fl_set_object_lalign( obj, FL_ALIGN_TOP );
  342. fl_set_object_callback( obj, change_size, 0 );
  343. fl_set_slider_return( obj, FL_RETURN_CHANGED );
  344. fl_end_group( );
  345. fl_end_form( );
  346. return fdui;
  347. }
  348. /*
  349. * Local variables:
  350. * tab-width: 4
  351. * indent-tabs-mode: nil
  352. * End:
  353. */