tkc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include <tk.h>
  2. #include <tcl.h>
  3. #define TCL_FUNCTION_SIGNITURE ClientData clientData, Tcl_Interp *pInterp, int argc, char ** argv
  4. typedef char * TKC_WIDGET;
  5. unsigned int tkc_widget_count = 0;
  6. Tcl_Interp * interp;
  7. Tcl_Channel tkc_stdout;
  8. // ---------------------------------------------------------------------------------- Init and quit functions
  9. int tkc_init(); // Initialises tkc
  10. int tkc_mainloop(); // Initialises mainloop
  11. int tkc_puts(char * output); // Prints a string to the console with a new line
  12. int tkc_execute(char * format/* ...*/); // Executes a formatted command
  13. int tkc_configure(TKC_WIDGET widget, char * format/*...*/); // Configures a formatted options string
  14. TKC_WIDGET tkc_button(char * format/* ...*/); // Creates a button with a formatted options string
  15. TKC_WIDGET tkc_canvas(char * format/* ...*/); // Creates a canvas with a formatted options string
  16. TKC_WIDGET tkc_checkbutton(char * format/*...*/); // Creates a checkbutton with a formatted options string
  17. TKC_WIDGET tkc_entry(char * format/* ...*/); // Creates a entry with a formatted options string
  18. TKC_WIDGET tkc_frame(char * format/* ...*/); // Creates a frame with a formatted options string
  19. TKC_WIDGET tkc_label(char * format/* ...*/); // Creates a label with a formatted options string
  20. TKC_WIDGET tkc_labelframe(char * format/* ...*/); // Creates a labelframe with a formatted options string
  21. TKC_WIDGET tkc_listbox(char * format/* ...*/); // Creates a listbox with a formatted options string
  22. TKC_WIDGET tkc_menu(char * format/* ...*/); // Creates a menu with a formatted options string
  23. TKC_WIDGET tkc_menubutton(char * format/* ...*/); // Creates a menubutton with a formatted options string
  24. TKC_WIDGET tkc_message(char * format/* ...*/); // Creates a message with a formatted options string
  25. TKC_WIDGET tkc_optionmenu(char * format/* ...*/); // Creates a optionmenu with a formatted options string
  26. TKC_WIDGET tkc_panedwindow(char * format/*...*/); // Creates a panedwindow with a formatted options string
  27. TKC_WIDGET tkc_radiobutton(char * format/*...*/); // Creates a radiobutton with a formatted options string
  28. TKC_WIDGET tkc_scale(char * format/* ...*/); // Creates a scale with a formatted options string
  29. TKC_WIDGET tkc_scrollbar(char * format/* ...*/); // Creates a scrollbar with a formatted options string
  30. TKC_WIDGET tkc_spinbox(char * format/* ...*/); // Creates a spinbox with a formatted options string
  31. TKC_WIDGET tkc_text(char * format/* ...*/); // Creates a text with a formatted options string
  32. int tkc_command(char * name, int function()); // Creates a Tcl command and links it to a C fucntion
  33. int tkc_pack(TKC_WIDGET widget, char * options); // Packs a widget with an options string
  34. int tkc_place(TKC_WIDGET widget, int x, int y, char * options); // Places a widget at x and y with an options string
  35. int tkc_grid(TKC_WIDGET widget, int x, int y, int w, int h, char * options); // Grids a widget with x,y,w,h and an options string
  36. int tkc_set_arc_theme(); // set an arc theme for some widgets
  37. // ---------------------------------------------------------------------------------- Init and quit functions
  38. int tkc_init()
  39. {
  40. interp = Tcl_CreateInterp();
  41. Tcl_Init(interp);
  42. tkc_stdout = Tcl_GetStdChannel(TCL_STDOUT);
  43. return 0;
  44. }
  45. int tkc_mainloop()
  46. {
  47. Tk_MainLoop();
  48. Tcl_DeleteInterp(interp);
  49. return 0;
  50. }
  51. // ----------------------------------------------------------------------------------------- Utility commands
  52. int tkc_puts(char * output)
  53. {
  54. Tcl_Write(tkc_stdout, output, -1);
  55. Tcl_Write(tkc_stdout, "\n", -1);
  56. Tcl_Flush(tkc_stdout);
  57. return 0;
  58. }
  59. #define tkc_execute(command_format, ...) tkc_execute_object(Tcl_ObjPrintf(command_format, ##__VA_ARGS__) ) ;
  60. int tkc_execute_object(Tcl_Obj * command)
  61. {
  62. Tcl_IncrRefCount(command);
  63. if ( Tcl_EvalObj(interp, command) != TCL_OK )
  64. {
  65. Tcl_Obj * output = Tcl_ObjPrintf("%s: %s\n", command->bytes, Tcl_GetStringResult(interp));
  66. Tcl_WriteObj(tkc_stdout, output);
  67. Tcl_DecrRefCount(output);
  68. Tcl_Flush(tkc_stdout);
  69. }
  70. Tcl_DecrRefCount(command);
  71. return 0;
  72. }
  73. #define tkc_configure(widget, options_format, ...) tkc_formatted_configure(widget, Tcl_ObjPrintf(options_format, ##__VA_ARGS__) ) ;
  74. int tkc_formatted_configure(TKC_WIDGET widget, Tcl_Obj * options)
  75. {
  76. Tcl_Obj * command = Tcl_ObjPrintf("%s configure %s", widget, options->bytes);
  77. tkc_execute_object(command);
  78. Tcl_DecrRefCount(options);
  79. return 0;
  80. }
  81. // --------------------------------------------------------------------------------------- Creation functions
  82. int tkc_window_(const char * title, const char * geometry, const char * resizable)
  83. {
  84. tkc_execute
  85. (
  86. "package require Tk;"
  87. "wm title . %s;"
  88. "wm geometry . %s;"
  89. "wm resizable . %s;"
  90. "proc quit {} {\n"
  91. " exit\n"
  92. "};"
  93. "bind . <Escape> quit;",
  94. title,
  95. geometry,
  96. resizable
  97. );
  98. return 0;
  99. }
  100. TKC_WIDGET tkc_create(char * type, Tcl_Obj * options)
  101. {
  102. char * widget_id = Tcl_GetString( Tcl_ObjPrintf(".widget_0x%010x", tkc_widget_count++) );\
  103. Tcl_Obj * command = Tcl_ObjPrintf("%s %s %s", type, widget_id, options->bytes);\
  104. tkc_execute_object(command);\
  105. Tcl_DecrRefCount(options);
  106. return widget_id;
  107. }
  108. #define tkc_button(format, ...) tkc_create("button", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  109. #define tkc_canvas(format, ...) tkc_create("canvas", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  110. #define tkc_checkbutton(format, ...)tkc_create("checkbutton",Tcl_ObjPrintf(format, ##__VA_ARGS__))
  111. #define tkc_entry(format, ...) tkc_create("entry", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  112. #define tkc_frame(format, ...) tkc_create("frame", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  113. #define tkc_label(format, ...) tkc_create("label", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  114. #define tkc_labelframe(format, ...) tkc_create("labelframe", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  115. #define tkc_listbox(format, ...) tkc_create("listbox", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  116. #define tkc_menu(format, ...) tkc_create("menu", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  117. #define tkc_menubutton(format, ...) tkc_create("menubutton", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  118. #define tkc_message(format, ...) tkc_create("message", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  119. #define tkc_optionmenu(format, ...) tkc_create("optionmenu", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  120. #define tkc_panedwindow(format, ...)tkc_create("panedwindow",Tcl_ObjPrintf(format, ##__VA_ARGS__))
  121. #define tkc_radiobutton(format, ...)tkc_create("radiobutton",Tcl_ObjPrintf(format, ##__VA_ARGS__))
  122. #define tkc_scale(format, ...) tkc_create("scale", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  123. #define tkc_scrollbar(format, ...) tkc_create("scrollbar", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  124. #define tkc_spinbox(format, ...) tkc_create("spinbox", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  125. #define tkc_text(format, ...) tkc_create("text", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  126. #define tkc_widget(format, ...) tkc_create("widget", Tcl_ObjPrintf(format, ##__VA_ARGS__))
  127. int tkc_command(char * name, int function())
  128. {
  129. Tcl_CreateCommand(interp, name, function, NULL, NULL);
  130. return 0;
  131. }
  132. // ----------------------------------------------------------------------------------------- Layout functions
  133. int tkc_pack(TKC_WIDGET widget, char * options)
  134. {
  135. return tkc_execute("pack %s %s", widget, options);
  136. }
  137. int tkc_place(TKC_WIDGET widget, int x, int y, char * options)
  138. {
  139. return tkc_execute("place %s -x %d -y %d %s", widget, x, y, options);
  140. }
  141. int tkc_grid(TKC_WIDGET widget, int x, int y, int w, int h, char * options)
  142. {
  143. return tkc_execute("grid %s -row %d -column %d -columnspan %d -rowspan %d %s", widget, x, y, w, h, options);
  144. }
  145. // -------------------------------------------------------------------------------------------------- Theming
  146. int tkc_set_arc_theme()
  147. {
  148. return tkc_execute
  149. (
  150. ". configure -background #383c4a\n"
  151. "option add *Button.background #444a58\n"
  152. "option add *Button.foreground #b3bac4\n"
  153. "option add *Button.activeBackground #505666\n"
  154. "option add *Button.activeForeground #b3bac4\n"
  155. "option add *Button.highlightColor #5294e2\n"
  156. "option add *Button.highlightBackground #2b2e39\n"
  157. "option add *Button.borderWidth 0\n"
  158. "option add *Button.relief flat\n"
  159. "option add *Button.overRelief flat\n"
  160. "option add *Button.padY 1\n"
  161. "option add *Entry.background #404552\n"
  162. "option add *Entry.foreground #b3bac4\n"
  163. "option add *Entry.selectBackground #5294e2\n"
  164. "option add *Entry.selectForeground #ffffff\n"
  165. "option add *Entry.readonlyBackground #404552\n"
  166. "option add *Entry.highlightColor #5294e2\n"
  167. "option add *Entry.highlightBackground #2c303a\n"
  168. "option add *Entry.relief flat\n"
  169. "option add *Entry.insertBackground #d3dae3\n"
  170. "option add *Frame.borderWidth 1\n"
  171. "option add *Frame.background #2c303a\n"
  172. "option add *Label.background #383c4a\n"
  173. "option add *Label.foreground #ccd3db\n"
  174. "option add *Label.relief flat\n"
  175. "option add *Text.background #404552\n"
  176. "option add *Text.foreground #d3dae3\n"
  177. "option add *Text.selectBackground #5294e2\n"
  178. "option add *Text.selectForeground #ffffff\n"
  179. "option add *Text.highlightColor #5294e2\n"
  180. "option add *Text.highlightBackground #2c303a\n"
  181. "option add *Text.relief flat\n"
  182. "option add *Text.insertBackground #d3dae3\n"
  183. "option add *Spinbox.highlightColor #2c303a\n"
  184. "option add *Spinbox.highlightBackground #2c303a\n"
  185. "option add *Spinbox.relief flat\n"
  186. "option add *Spinbox.background #404552\n"
  187. "option add *Spinbox.foreground #b3bac4\n"
  188. "option add *Spinbox.selectBackground #5294e2\n"
  189. "option add *Spinbox.selectForeground #ffffff\n"
  190. "option add *Spinbox.buttonBackground #444a58\n"
  191. "option add *Spinbox.insertBackground #d3dae3\n"
  192. "option add *Checkbutton.background #383c4a\n"
  193. "option add *Checkbutton.foreground #d3dae3\n"
  194. "option add *Checkbutton.activeBackground #383c4a\n"
  195. "option add *Checkbutton.activeForeground #d3dae3\n"
  196. "option add *Checkbutton.selectColor #2d323d\n"
  197. "option add *Checkbutton.highlightColor #5294e2\n"
  198. "option add *Checkbutton.highlightBackground #383c4a\n"
  199. "option add *Checkbutton.relief flat\n"
  200. );
  201. }