tictactoe.pp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. {
  2. $Id$
  3. Converted from C to Pascal by Frank Loemker
  4. <[email protected]>
  5. }
  6. unit tictactoe;
  7. interface
  8. uses
  9. glib,gdk,gtk;
  10. type
  11. PTictactoe = ^TTictactoe;
  12. TTictactoe = record
  13. vbox : TGtkVBox ;
  14. buttons : array [0..2 , 0..2] of pGtkWidget;
  15. end;
  16. PTictactoeClass = ^TTictactoeClass;
  17. TTictactoeClass = record
  18. parent_class: TGtkVBoxClass ;
  19. tictactoe: Procedure (ttt : pTictactoe); cdecl;
  20. end;
  21. Function tictactoe_get_type : guint;
  22. Function tictactoe_new : pGtkWidget;
  23. procedure tictactoe_clear (ttt : pTictactoe);
  24. implementation
  25. const
  26. ANZ_SIGNAL = 1;
  27. type
  28. TTT_Signals = (TICTACTOE_SIGNAL);
  29. const
  30. tictactoe_signals: array[TTT_Signals] of guint = (0);
  31. Procedure tictactoe_toggle (widget : pGtkWidget ; ttt: pTictactoe); cdecl;
  32. const rwins: array[0..7,0..2] of integer =
  33. ( ( 0, 0, 0 ), ( 1, 1, 1 ), ( 2, 2, 2 ),
  34. ( 0, 1, 2 ), ( 0, 1, 2 ), ( 0, 1, 2 ),
  35. ( 0, 1, 2 ), ( 0, 1, 2 ) );
  36. cwins:array [0..7,0..2] of integer =
  37. ( ( 0, 1, 2 ), ( 0, 1, 2 ), ( 0, 1, 2 ),
  38. ( 0, 0, 0 ), ( 1, 1, 1 ), ( 2, 2, 2 ),
  39. ( 0, 1, 2 ), ( 2, 1, 0 ) );
  40. var i, k : integer;
  41. success, found : boolean;
  42. begin
  43. for k:=0 to 7 do
  44. begin
  45. success := TRUE;
  46. found := FALSE;
  47. for i:=0 to 2 do
  48. begin
  49. success := success and
  50. boolean(active(pGTKTOGGLEBUTTON(ttt^.buttons[rwins[k,i],cwins[k,i]])^));
  51. found := found or
  52. (ttt^.buttons[rwins[k,i],cwins[k,i]] = widget);
  53. end;
  54. if (success and found) then
  55. begin
  56. gtk_signal_emit (pGTKOBJECT (ttt),
  57. tictactoe_signals[TICTACTOE_SIGNAL]);
  58. break;
  59. end;
  60. end;
  61. end;
  62. Procedure gtk_signal_default_marshallerT(theobject : pGtkObject;
  63. func : GTK_SIGNAL_FUNC;
  64. func_data : gpointer;
  65. args : pGtkArg); cdecl;
  66. begin
  67. {$ifndef win32}
  68. gtk_marshal_NONE__NONE (theobject,func,func_data,args);
  69. {$endif}
  70. end;
  71. Procedure tictactoe_class_init (theclass : pTictactoeClass );
  72. var object_class : pGtkObjectClass ;
  73. begin
  74. object_class := pGtkObjectClass (theclass);
  75. tictactoe_signals[TICTACTOE_SIGNAL] :=gtk_signal_new ('tictactoe',
  76. GTK_RUN_FIRST,
  77. object_class^.thetype,
  78. @theclass^.tictactoe - pointer(theclass),
  79. @gtk_signal_default_marshallerT, GTK_TYPE_NONE, 0);
  80. gtk_object_class_add_signals (object_class, pguint(@tictactoe_signals), ANZ_SIGNAL);
  81. theclass^.tictactoe := NIL;
  82. end;
  83. Procedure tictactoe_init (ttt : pTictactoe );
  84. var table : pGtkWidget ;
  85. i,j : gint;
  86. begin
  87. table := gtk_table_new (3, 3, true);
  88. gtk_container_add (pGTKCONTAINER(ttt), table);
  89. gtk_widget_show (table);
  90. for i:=0 to 2 do
  91. for j:=0 to 2 do
  92. begin
  93. ttt^.buttons[i][j] := gtk_toggle_button_new ();
  94. gtk_table_attach_defaults (pGTKTABLE(table), ttt^.buttons[i][j],
  95. i, i+1, j, j+1);
  96. gtk_signal_connect (pGTKOBJECT (ttt^.buttons[i][j]), 'toggled',
  97. GTK_SIGNAL_FUNC (@tictactoe_toggle), ttt);
  98. gtk_widget_set_usize (ttt^.buttons[i][j], 20, 20);
  99. gtk_widget_show (ttt^.buttons[i][j]);
  100. end;
  101. end;
  102. Procedure tictactoe_class_init2 (theclass : gpointer ); cdecl;
  103. begin
  104. tictactoe_class_init (theclass);
  105. end;
  106. Procedure tictactoe_init2 (ttt : gpointer; klass:gpointer); cdecl;
  107. begin
  108. tictactoe_init (ttt);
  109. end;
  110. Function tictactoe_get_type:guint;
  111. const ttt_type : guint = 0;
  112. ttt_info: TGtkTypeInfo = (
  113. type_name : 'Tictactoe';
  114. object_size : sizeof (TTictactoe);
  115. class_size : sizeof (TTictactoeClass);
  116. class_init_func : @tictactoe_class_init2;
  117. object_init_func : @tictactoe_init2;
  118. );
  119. begin
  120. if (ttt_type = 0) then
  121. ttt_type := gtk_type_unique (gtk_vbox_get_type (), @ttt_info);
  122. tictactoe_get_type:= ttt_type;
  123. end;
  124. Function tictactoe_new:pGtkWidget;
  125. begin
  126. tictactoe_new:= pGTKWIDGET ( gtk_type_new (tictactoe_get_type ()));
  127. end;
  128. Procedure tictactoe_clear (ttt : pTictactoe );
  129. var i,j : integer;
  130. begin
  131. for i:=0 to 2 do
  132. for j:=0 to 2 do
  133. begin
  134. gtk_signal_handler_block_by_data (pGTKOBJECT(ttt^.buttons[i][j]), ttt);
  135. gtk_toggle_button_set_active (pGTKTOGGLEBUTTON (ttt^.buttons[i][j]),
  136. false);
  137. gtk_signal_handler_unblock_by_data (pGTKOBJECT(ttt^.buttons[i][j]), ttt);
  138. end;
  139. end;
  140. end.
  141. {
  142. $Log$
  143. Revision 1.1 1999-11-24 23:36:33 peter
  144. * moved to packages dir
  145. Revision 1.8 1999/10/05 09:28:26 peter
  146. * patches from Frank Loemker
  147. Revision 1.7 1999/06/10 20:00:16 peter
  148. * fixed tictactoe
  149. Revision 1.6 1999/05/10 19:18:15 peter
  150. * more fixes for the examples to work
  151. Revision 1.1 1999/05/10 09:02:36 peter
  152. * gtk 1.2 port working
  153. Revision 1.5 1999/05/07 15:09:38 peter
  154. * more fixes
  155. Revision 1.4 1999/02/02 16:13:38 michael
  156. + Applied second patch from Frank Loemker
  157. Revision 1.3 1999/01/26 12:42:21 michael
  158. *** empty log message ***
  159. Revision 1.2 1998/10/22 11:37:32 peter
  160. * fixes for win32
  161. Revision 1.1 1998/10/21 22:27:01 peter
  162. + initial version
  163. }