2
0

scribble.pp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. {
  2. Converted from C to Pascal by Frank Loemker
  3. <[email protected]>
  4. }
  5. program scribble;
  6. uses
  7. glib,Gdk,Gtk;
  8. { Backing pixmap for drawing area }
  9. const
  10. pixmap : PGdkPixmap = NIL;
  11. { Create a new backing pixmap of the appropriate size }
  12. function configure_event (widget : PGtkWidget; event: PGdkEventConfigure):boolean; cdecl;
  13. begin
  14. if pixmap<>NIL then
  15. gdk_pixmap_unref(pixmap);
  16. pixmap := gdk_pixmap_new(widget^.window,
  17. widget^.allocation.width,
  18. widget^.allocation.height,
  19. -1);
  20. gdk_draw_rectangle (pixmap,
  21. PGtkStyle(widget^.thestyle)^.white_gc,
  22. gint(true),
  23. 0, 0,
  24. widget^.allocation.width,
  25. widget^.allocation.height);
  26. configure_event := TRUE;
  27. end;
  28. { Redraw the screen from the backing pixmap }
  29. function expose_event (widget : PGtkWidget ; event : PGdkEventExpose ) : boolean; cdecl;
  30. begin
  31. gdk_draw_pixmap(widget^.window,
  32. PGtkStyle(widget^.thestyle)^.fg_gc[gtk_WIDGET_STATE (widget)],
  33. pixmap,
  34. event^.area.x, event^.area.y,
  35. event^.area.x, event^.area.y,
  36. event^.area.width, event^.area.height);
  37. expose_event:= FALSE;
  38. end;
  39. { Draw a rectangle on the screen }
  40. procedure draw_brush (widget : PGtkWidget ; x, y: gint16);
  41. var update_rect : TGdkRectangle;
  42. begin
  43. update_rect.x := x - 5;
  44. update_rect.y := y - 5;
  45. update_rect.width := 10;
  46. update_rect.height := 10;
  47. gdk_draw_rectangle (pixmap,
  48. PGtkStyle(widget^.thestyle)^.black_gc,
  49. gint(true),
  50. update_rect.x, update_rect.y,
  51. update_rect.width, update_rect.height);
  52. gtk_widget_draw (widget, @update_rect);
  53. end;
  54. function button_press_event (widget : PGtkWidget ; event: PGdkEventButton ) : boolean; cdecl;
  55. begin
  56. if (event^.button = 1) and (pixmap <> NIL) then begin
  57. draw_brush (widget, trunc(event^.x), trunc(event^.y));
  58. end;
  59. button_press_event := TRUE;
  60. end;
  61. function motion_notify_event (widget : PGtkWidget ; event: PGdkEventMotion ) : boolean; cdecl;
  62. var x, y : longint ;
  63. state : longint;
  64. begin
  65. if (event^.is_hint<>0) then begin
  66. gdk_window_get_pointer (event^.window, @x, @y, @state);
  67. end else begin
  68. x := trunc(event^.x);
  69. y := trunc(event^.y);
  70. state := event^.state;
  71. end;
  72. if ((state and gdk_BUTTON1_MASK)<>0) and (pixmap <> NIL) then
  73. draw_brush (widget, x, y);
  74. motion_notify_event := TRUE;
  75. end;
  76. procedure quit;
  77. begin
  78. gtk_exit (0);
  79. end;
  80. var window, drawing_area, vbox, button : PGtkWidget;
  81. begin
  82. gtk_init (@argc, @argv);
  83. gtk_rc_init;
  84. window := gtk_window_new (gtk_WINDOW_TOPLEVEL);
  85. gtk_widget_set_name (window, 'Test Input');
  86. vbox := gtk_vbox_new (false, 0);
  87. gtk_container_add (PGtkCONTAINER (window), vbox);
  88. gtk_widget_show (vbox);
  89. gtk_signal_connect (PGtkOBJECT (window), 'destroy',
  90. gtk_SIGNAL_FUNC (@quit), NIL);
  91. { Create the drawing area }
  92. drawing_area := gtk_drawing_area_new ();
  93. gtk_drawing_area_size (PGtkDRAWINGAREA (drawing_area), 200, 200);
  94. gtk_box_pack_start (PGtkBOX (vbox), drawing_area, true, true, 0);
  95. gtk_widget_show (drawing_area);
  96. { Signals used to handle backing pixmap }
  97. gtk_signal_connect (PGtkOBJECT (drawing_area), 'expose_event',
  98. gtk_SIGNAL_FUNC (@expose_event), NIL);
  99. gtk_signal_connect (PGtkOBJECT(drawing_area),'configure_event',
  100. gtk_SIGNAL_FUNC (@configure_event), NIL);
  101. { Event signals }
  102. gtk_signal_connect (PGtkOBJECT (drawing_area), 'motion_notify_event',
  103. gtk_SIGNAL_FUNC (@motion_notify_event), NIL);
  104. gtk_signal_connect (PGtkOBJECT (drawing_area), 'button_press_event',
  105. gtk_SIGNAL_FUNC (@button_press_event), NIL);
  106. gtk_widget_set_events (drawing_area, gdk_EXPOSURE_MASK
  107. or gdk_LEAVE_NOTIFY_MASK
  108. or gdk_BUTTON_PRESS_MASK
  109. or gdk_POINTER_MOTION_MASK
  110. or gdk_POINTER_MOTION_HINT_MASK);
  111. { .. And a quit button }
  112. button := gtk_button_new_with_label ('Quit');
  113. gtk_box_pack_start (PGtkBOX (vbox), button, false, false, 0);
  114. gtk_signal_connect_object (PGtkOBJECT (button), 'clicked',
  115. gtk_SIGNAL_FUNC (@gtk_widget_destroy),
  116. PGtkOBJECT (window));
  117. gtk_widget_show (button);
  118. gtk_widget_show (window);
  119. gtk_main ();
  120. end.