scribble.pp 4.6 KB

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