tictactoe.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. gtk_marshal_NONE__NONE (theobject,func,func_data,args);
  68. end;
  69. Procedure tictactoe_class_init (theclass : pTictactoeClass );
  70. var object_class : pGtkObjectClass ;
  71. begin
  72. object_class := pGtkObjectClass (theclass);
  73. tictactoe_signals[TICTACTOE_SIGNAL] :=gtk_signal_new ('tictactoe',
  74. GTK_RUN_FIRST,
  75. object_class^.thetype,
  76. @theclass^.tictactoe - pointer(theclass),
  77. @gtk_signal_default_marshallerT, GTK_TYPE_NONE, 0);
  78. gtk_object_class_add_signals (object_class, pguint(@tictactoe_signals), ANZ_SIGNAL);
  79. theclass^.tictactoe := NIL;
  80. end;
  81. Procedure tictactoe_init (ttt : pTictactoe );
  82. var table : pGtkWidget ;
  83. i,j : gint;
  84. begin
  85. table := gtk_table_new (3, 3, true);
  86. gtk_container_add (pGTKCONTAINER(ttt), table);
  87. gtk_widget_show (table);
  88. for i:=0 to 2 do
  89. for j:=0 to 2 do
  90. begin
  91. ttt^.buttons[i][j] := gtk_toggle_button_new ();
  92. gtk_table_attach_defaults (pGTKTABLE(table), ttt^.buttons[i][j],
  93. i, i+1, j, j+1);
  94. gtk_signal_connect (pGTKOBJECT (ttt^.buttons[i][j]), 'toggled',
  95. GTK_SIGNAL_FUNC (@tictactoe_toggle), ttt);
  96. gtk_widget_set_usize (ttt^.buttons[i][j], 20, 20);
  97. gtk_widget_show (ttt^.buttons[i][j]);
  98. end;
  99. end;
  100. Procedure tictactoe_class_init2 (theclass : gpointer ); cdecl;
  101. begin
  102. tictactoe_class_init (theclass);
  103. end;
  104. Procedure tictactoe_init2 (ttt : gpointer; klass:gpointer); cdecl;
  105. begin
  106. tictactoe_init (ttt);
  107. end;
  108. Function tictactoe_get_type:guint;
  109. const ttt_type : guint = 0;
  110. ttt_info: TGtkTypeInfo = (
  111. type_name : 'Tictactoe';
  112. object_size : sizeof (TTictactoe);
  113. class_size : sizeof (TTictactoeClass);
  114. class_init_func : @tictactoe_class_init2;
  115. object_init_func : @tictactoe_init2;
  116. );
  117. begin
  118. if (ttt_type = 0) then
  119. ttt_type := gtk_type_unique (gtk_vbox_get_type (), @ttt_info);
  120. tictactoe_get_type:= ttt_type;
  121. end;
  122. Function tictactoe_new:pGtkWidget;
  123. begin
  124. tictactoe_new:= pGTKWIDGET ( gtk_type_new (tictactoe_get_type ()));
  125. end;
  126. Procedure tictactoe_clear (ttt : pTictactoe );
  127. var i,j : integer;
  128. begin
  129. for i:=0 to 2 do
  130. for j:=0 to 2 do
  131. begin
  132. gtk_signal_handler_block_by_data (pGTKOBJECT(ttt^.buttons[i][j]), ttt);
  133. gtk_toggle_button_set_active (pGTKTOGGLEBUTTON (ttt^.buttons[i][j]),
  134. false);
  135. gtk_signal_handler_unblock_by_data (pGTKOBJECT(ttt^.buttons[i][j]), ttt);
  136. end;
  137. end;
  138. end.
  139. {
  140. $Log$
  141. Revision 1.3 2000-09-09 20:59:15 peter
  142. * win32 updates
  143. Revision 1.2 2000/07/13 11:33:18 michael
  144. + removed logs
  145. }