/src/wrappers/gtk/examples/gtk-demo/app_window.e

http://github.com/tybor/Liberty · Specman e · 598 lines · 66 code · 90 blank · 442 comment · 3 complexity · ffc68795416a436eae7634948c4c9363 MD5 · raw file

  1. indexing
  2. description: "Application main window - Demonstrates a typical application window with menubar, toolbar, statusbar."
  3. copyright: "[
  4. Copyright (C) 2006 eiffel-libraries team, GTK+ team
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation; either version 2.1 of
  8. the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA
  17. ]"
  18. class APP_WINDOW
  19. inherit
  20. DEMO
  21. GTK_WINDOW redefine make end
  22. insert
  23. GTK_DIALOG_FLAGS
  24. GTK_MESSAGE_TYPE
  25. GTK_BUTTONS_TYPE
  26. creation make
  27. feature
  28. name: STRING is "Appliction main window"
  29. description: STRING is "Demonstrates a typical application window with menubar, toolbar, statusbar."
  30. filename: STRING is "app_window.e"
  31. title_string: STRING is "Application Window"
  32. feature -- Creation
  33. make is
  34. -- local
  35. -- table, statusbar, contents, sw, bar: GTK_WIDGET;
  36. -- buffer: GTK_TEXT_BUFFER;
  37. -- action_group: GTK_ACTION_GROUP;
  38. -- open_action: GTK_ACTION;
  39. -- merge: GTK_UI_MANAGER;
  40. -- error: G_ERROR
  41. do
  42. -- Create the toplevel window
  43. Precursor
  44. register_stock_icons
  45. -- TODO: gtk_window_set_screen (GTK_WINDOW (window),
  46. -- TODO: gtk_widget_get_screen (do_widget));
  47. set_title (title_string)
  48. -- gtk_window_set_icon_name (GTK_WINDOW (window), "gtk-open");
  49. -- /* NULL window variable when window is closed */
  50. -- g_signal_connect (window, "destroy",
  51. -- G_CALLBACK (gtk_widget_destroyed),
  52. -- &window);
  53. -- table = gtk_table_new (1, 4, FALSE);
  54. -- gtk_container_add (GTK_CONTAINER (window), table);
  55. -- /* Create the menubar and toolbar
  56. -- */
  57. -- action_group = gtk_action_group_new ("AppWindowActions");
  58. -- open_action = g_object_new (tool_menu_action_get_type (),
  59. -- "name", "Open",
  60. -- "label", "_Open",
  61. -- "tooltip", "Open a file",
  62. -- "stock-id", GTK_STOCK_OPEN,
  63. -- NULL);
  64. -- gtk_action_group_add_action (action_group, open_action);
  65. -- gtk_action_group_add_actions (action_group,
  66. -- entries, n_entries,
  67. -- window);
  68. -- gtk_action_group_add_toggle_actions (action_group,
  69. -- toggle_entries, n_toggle_entries,
  70. -- NULL);
  71. -- gtk_action_group_add_radio_actions (action_group,
  72. -- color_entries, n_color_entries,
  73. -- COLOR_RED,
  74. -- G_CALLBACK (activate_radio_action),
  75. -- NULL);
  76. -- gtk_action_group_add_radio_actions (action_group,
  77. -- shape_entries, n_shape_entries,
  78. -- SHAPE_SQUARE,
  79. -- G_CALLBACK (activate_radio_action),
  80. -- NULL);
  81. -- merge = gtk_ui_manager_new ();
  82. -- g_object_set_data_full (G_OBJECT (window), "ui-manager", merge,
  83. -- g_object_unref);
  84. -- gtk_ui_manager_insert_action_group (merge, action_group, 0);
  85. -- gtk_window_add_accel_group (GTK_WINDOW (window),
  86. -- gtk_ui_manager_get_accel_group (merge));
  87. -- if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
  88. -- {
  89. -- g_message ("building menus failed: %s", error->message);
  90. -- g_error_free (error);
  91. -- }
  92. -- bar = gtk_ui_manager_get_widget (merge, "/MenuBar");
  93. -- gtk_widget_show (bar);
  94. -- gtk_table_attach (GTK_TABLE (table),
  95. -- bar,
  96. -- /* X direction */ /* Y direction */
  97. -- 0, 1, 0, 1,
  98. -- GTK_EXPAND | GTK_FILL, 0,
  99. -- 0, 0);
  100. -- bar = gtk_ui_manager_get_widget (merge, "/ToolBar");
  101. -- gtk_toolbar_set_tooltips (GTK_TOOLBAR (bar), TRUE);
  102. -- gtk_widget_show (bar);
  103. -- gtk_table_attach (GTK_TABLE (table),
  104. -- bar,
  105. -- /* X direction */ /* Y direction */
  106. -- 0, 1, 1, 2,
  107. -- GTK_EXPAND | GTK_FILL, 0,
  108. -- 0, 0);
  109. -- /* Create document
  110. -- */
  111. -- sw = gtk_scrolled_window_new (NULL, NULL);
  112. -- gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
  113. -- GTK_POLICY_AUTOMATIC,
  114. -- GTK_POLICY_AUTOMATIC);
  115. -- gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
  116. -- GTK_SHADOW_IN);
  117. -- gtk_table_attach (GTK_TABLE (table),
  118. -- sw,
  119. -- /* X direction */ /* Y direction */
  120. -- 0, 1, 2, 3,
  121. -- GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
  122. -- 0, 0);
  123. -- gtk_window_set_default_size (GTK_WINDOW (window),
  124. -- 200, 200);
  125. -- contents = gtk_text_view_new ();
  126. -- gtk_widget_grab_focus (contents);
  127. -- gtk_container_add (GTK_CONTAINER (sw),
  128. -- contents);
  129. -- /* Create statusbar */
  130. -- statusbar = gtk_statusbar_new ();
  131. -- gtk_table_attach (GTK_TABLE (table),
  132. -- statusbar,
  133. -- /* X direction */ /* Y direction */
  134. -- 0, 1, 3, 4,
  135. -- GTK_EXPAND | GTK_FILL, 0,
  136. -- 0, 0);
  137. -- /* Show text widget info in the statusbar */
  138. -- buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
  139. -- g_signal_connect_object (buffer,
  140. -- "changed",
  141. -- G_CALLBACK (update_statusbar),
  142. -- statusbar,
  143. -- 0);
  144. -- g_signal_connect_object (buffer,
  145. -- "mark_set", /* cursor moved */
  146. -- G_CALLBACK (mark_set_callback),
  147. -- statusbar,
  148. -- 0);
  149. -- g_signal_connect_object (window,
  150. -- "window_state_event",
  151. -- G_CALLBACK (update_resize_grip),
  152. -- statusbar,
  153. -- 0);
  154. -- update_statusbar (buffer, GTK_STATUSBAR (statusbar));
  155. -- }
  156. -- if (!GTK_WIDGET_VISIBLE (window))
  157. -- {
  158. -- gtk_widget_show_all (window);
  159. -- }
  160. -- else
  161. -- {
  162. -- gtk_widget_destroy (window);
  163. -- window = NULL;
  164. -- }
  165. -- return window;
  166. -- }
  167. load_source_code
  168. end
  169. feature -- Actions
  170. activate_action (an_action: GTK_ACTION) is
  171. local dialog: GTK_WIDGET
  172. do
  173. create {GTK_MESSAGE_DIALOG} dialog.make
  174. (Current, gtk_dialog_destroy_with_parent,
  175. gtk_message_info, gtk_buttons_close,
  176. "You activated action: `"+an_action.name+"'") -- of type `"+an_action.typename+"'")
  177. -- Close dialog on user response
  178. -- TODO: dialog.connect_to_response_signal(agent ....) note
  179. -- that usually in C you write: g_signal_connect (dialog,
  180. -- "response", G_CALLBACK (gtk_widget_destroy), NULL);
  181. dialog.show
  182. end
  183. activate_radio_action (an_action: GTK_ACTION; a_radio_action: GTK_RADIO_ACTION) is
  184. local
  185. dialog: GTK_WIDGET
  186. do
  187. if a_radio_action.is_active then
  188. create {GTK_MESSAGE_DIALOG} dialog.make
  189. (Current, gtk_dialog_destroy_with_parent,
  190. gtk_message_info, gtk_buttons_close,
  191. "You activated radio action: `"+a_radio_action.name+"'.\n"+
  192. -- of type `" + a_radio_action.typename+
  193. "Current value: " + a_radio_action.current_value.out)
  194. -- Close dialog on user response g_signal_connect (dialog,
  195. -- "response", G_CALLBACK (gtk_widget_destroy), NULL);
  196. dialog.show
  197. end
  198. end
  199. -- static void
  200. -- activate_email (GtkAboutDialog *about,
  201. -- const gchar *link,
  202. -- gpointer data)
  203. -- {
  204. -- g_print ("send mail to %s\n", link);
  205. -- }
  206. -- static void
  207. -- activate_url (GtkAboutDialog *about,
  208. -- const gchar *link,
  209. -- gpointer data)
  210. -- {
  211. -- g_print ("show url %s\n", link);
  212. -- }
  213. -- static void
  214. -- about_cb (GtkAction *action,
  215. -- GtkWidget *window)
  216. -- {
  217. -- GdkPixbuf *pixbuf, *transparent;
  218. -- gchar *filename;
  219. -- const gchar *authors[] = {
  220. -- "Peter Mattis",
  221. -- "Spencer Kimball",
  222. -- "Josh MacDonald",
  223. -- "and many more...",
  224. -- NULL
  225. -- };
  226. -- const gchar *documentors[] = {
  227. -- "Owen Taylor",
  228. -- "Tony Gale",
  229. -- "Matthias Clasen <mclasen@redhat.com>",
  230. -- "and many more...",
  231. -- NULL
  232. -- };
  233. -- const gchar *license =
  234. -- "This library is free software; you can redistribute it and/or\n"
  235. -- "modify it under the terms of the GNU Library General Public License as\n"
  236. -- "published by the Free Software Foundation; either version 2 of the\n"
  237. -- "License, or (at your option) any later version.\n"
  238. -- "\n"
  239. -- "This library is distributed in the hope that it will be useful,\n"
  240. -- "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  241. -- "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  242. -- "Library General Public License for more details.\n"
  243. -- "\n"
  244. -- "You should have received a copy of the GNU Library General Public\n"
  245. -- "License along with the Gnome Library; see the file COPYING.LIB. If not,\n"
  246. -- "write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
  247. -- "Boston, MA 02111-1307, USA.\n";
  248. -- pixbuf = NULL;
  249. -- transparent = NULL;
  250. -- filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
  251. -- if (filename)
  252. -- {
  253. -- pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
  254. -- g_free (filename);
  255. -- transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
  256. -- g_object_unref (pixbuf);
  257. -- }
  258. -- gtk_about_dialog_set_email_hook (activate_email, NULL, NULL);
  259. -- gtk_about_dialog_set_url_hook (activate_url, NULL, NULL);
  260. -- gtk_show_about_dialog (GTK_WINDOW (window),
  261. -- "name", "GTK+ Code Demos",
  262. -- "version", PACKAGE_VERSION,
  263. -- "copyright", "(C) 1997-2005 The GTK+ Team",
  264. -- "license", license,
  265. -- "website", "http://www.gtk.org",
  266. -- "comments", "Program to demonstrate GTK+ functions.",
  267. -- "authors", authors,
  268. -- "documenters", documentors,
  269. -- "logo", transparent,
  270. -- NULL);
  271. -- g_object_unref (transparent);
  272. -- }
  273. -- typedef struct
  274. -- {
  275. -- GtkAction action;
  276. -- } ToolMenuAction;
  277. -- typedef struct
  278. -- {
  279. -- GtkActionClass parent_class;
  280. -- } ToolMenuActionClass;
  281. -- G_DEFINE_TYPE(ToolMenuAction, tool_menu_action, GTK_TYPE_ACTION);
  282. -- static void
  283. -- tool_menu_action_class_init (ToolMenuActionClass *class)
  284. -- {
  285. -- GTK_ACTION_CLASS (class)->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
  286. -- }
  287. -- static void
  288. -- tool_menu_action_init (ToolMenuAction *action)
  289. -- {
  290. -- }
  291. -- static GtkActionEntry entries[] = {
  292. -- { "FileMenu", NULL, "_File" }, /* name, stock id, label */
  293. -- { "OpenMenu", NULL, "_Open" }, /* name, stock id, label */
  294. -- { "PreferencesMenu", NULL, "_Preferences" }, /* name, stock id, label */
  295. -- { "ColorMenu", NULL, "_Color" }, /* name, stock id, label */
  296. -- { "ShapeMenu", NULL, "_Shape" }, /* name, stock id, label */
  297. -- { "HelpMenu", NULL, "_Help" }, /* name, stock id, label */
  298. -- { "New", GTK_STOCK_NEW, /* name, stock id */
  299. -- "_New", "<control>N", /* label, accelerator */
  300. -- "Create a new file", /* tooltip */
  301. -- G_CALLBACK (activate_action) },
  302. -- { "File1", NULL, /* name, stock id */
  303. -- "File1", NULL, /* label, accelerator */
  304. -- "Open first file", /* tooltip */
  305. -- G_CALLBACK (activate_action) },
  306. -- { "Save", GTK_STOCK_SAVE, /* name, stock id */
  307. -- "_Save","<control>S", /* label, accelerator */
  308. -- "Save current file", /* tooltip */
  309. -- G_CALLBACK (activate_action) },
  310. -- { "SaveAs", GTK_STOCK_SAVE, /* name, stock id */
  311. -- "Save _As...", NULL, /* label, accelerator */
  312. -- "Save to a file", /* tooltip */
  313. -- G_CALLBACK (activate_action) },
  314. -- { "Quit", GTK_STOCK_QUIT, /* name, stock id */
  315. -- "_Quit", "<control>Q", /* label, accelerator */
  316. -- "Quit", /* tooltip */
  317. -- G_CALLBACK (activate_action) },
  318. -- { "About", NULL, /* name, stock id */
  319. -- "_About", "<control>A", /* label, accelerator */
  320. -- "About", /* tooltip */
  321. -- G_CALLBACK (about_cb) },
  322. -- { "Logo", "demo-gtk-logo", /* name, stock id */
  323. -- NULL, NULL, /* label, accelerator */
  324. -- "GTK+", /* tooltip */
  325. -- G_CALLBACK (activate_action) },
  326. -- };
  327. -- static guint n_entries = G_N_ELEMENTS (entries);
  328. -- static GtkToggleActionEntry toggle_entries[] = {
  329. -- { "Bold", GTK_STOCK_BOLD, /* name, stock id */
  330. -- "_Bold", "<control>B", /* label, accelerator */
  331. -- "Bold", /* tooltip */
  332. -- G_CALLBACK (activate_action),
  333. -- TRUE }, /* is_active */
  334. -- };
  335. -- static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
  336. -- enum {
  337. -- COLOR_RED,
  338. -- COLOR_GREEN,
  339. -- COLOR_BLUE
  340. -- };
  341. -- static GtkRadioActionEntry color_entries[] = {
  342. -- { "Red", NULL, /* name, stock id */
  343. -- "_Red", "<control>R", /* label, accelerator */
  344. -- "Blood", COLOR_RED }, /* tooltip, value */
  345. -- { "Green", NULL, /* name, stock id */
  346. -- "_Green", "<control>G", /* label, accelerator */
  347. -- "Grass", COLOR_GREEN }, /* tooltip, value */
  348. -- { "Blue", NULL, /* name, stock id */
  349. -- "_Blue", "<control>B", /* label, accelerator */
  350. -- "Sky", COLOR_BLUE }, /* tooltip, value */
  351. -- };
  352. -- static guint n_color_entries = G_N_ELEMENTS (color_entries);
  353. -- enum {
  354. -- SHAPE_SQUARE,
  355. -- SHAPE_RECTANGLE,
  356. -- SHAPE_OVAL
  357. -- };
  358. -- static GtkRadioActionEntry shape_entries[] = {
  359. -- { "Square", NULL, /* name, stock id */
  360. -- "_Square", "<control>S", /* label, accelerator */
  361. -- "Square", SHAPE_SQUARE }, /* tooltip, value */
  362. -- { "Rectangle", NULL, /* name, stock id */
  363. -- "_Rectangle", "<control>R", /* label, accelerator */
  364. -- "Rectangle", SHAPE_RECTANGLE }, /* tooltip, value */
  365. -- { "Oval", NULL, /* name, stock id */
  366. -- "_Oval", "<control>O", /* label, accelerator */
  367. -- "Egg", SHAPE_OVAL }, /* tooltip, value */
  368. -- };
  369. -- static guint n_shape_entries = G_N_ELEMENTS (shape_entries);
  370. -- static const gchar *ui_info =
  371. -- "<ui>"
  372. -- " <menubar name='MenuBar'>"
  373. -- " <menu action='FileMenu'>"
  374. -- " <menuitem action='New'/>"
  375. -- " <menuitem action='Open'/>"
  376. -- " <menuitem action='Save'/>"
  377. -- " <menuitem action='SaveAs'/>"
  378. -- " <separator/>"
  379. -- " <menuitem action='Quit'/>"
  380. -- " </menu>"
  381. -- " <menu action='PreferencesMenu'>"
  382. -- " <menu action='ColorMenu'>"
  383. -- " <menuitem action='Red'/>"
  384. -- " <menuitem action='Green'/>"
  385. -- " <menuitem action='Blue'/>"
  386. -- " </menu>"
  387. -- " <menu action='ShapeMenu'>"
  388. -- " <menuitem action='Square'/>"
  389. -- " <menuitem action='Rectangle'/>"
  390. -- " <menuitem action='Oval'/>"
  391. -- " </menu>"
  392. -- " <menuitem action='Bold'/>"
  393. -- " </menu>"
  394. -- " <menu action='HelpMenu'>"
  395. -- " <menuitem action='About'/>"
  396. -- " </menu>"
  397. -- " </menubar>"
  398. -- " <toolbar name='ToolBar'>"
  399. -- " <toolitem action='Open'>"
  400. -- " <menu action='OpenMenu'>"
  401. -- " <menuitem action='File1'/>"
  402. -- " </menu>"
  403. -- " </toolitem>"
  404. -- " <toolitem action='Quit'/>"
  405. -- " <separator action='Sep1'/>"
  406. -- " <toolitem action='Logo'/>"
  407. -- " </toolbar>"
  408. -- "</ui>";
  409. register_stock_icons is
  410. -- Registers our custom toolbar icons, so they can be themed.
  411. -- It's totally optional to do this, you could just manually
  412. -- insert icons and have them not be themeable, especially if
  413. -- you never expect people to theme your app.
  414. do
  415. -- {
  416. -- static gboolean registered = FALSE;
  417. -- if (!registered)
  418. -- {
  419. -- GdkPixbuf *pixbuf;
  420. -- GtkIconFactory *factory;
  421. -- char *filename;
  422. -- static GtkStockItem items[] = {
  423. -- { "demo-gtk-logo",
  424. -- "_GTK!",
  425. -- 0, 0, NULL }
  426. -- };
  427. -- registered = TRUE;
  428. -- /* Register our stock items */
  429. -- gtk_stock_add (items, G_N_ELEMENTS (items));
  430. -- /* Add our custom icon factory to the list of defaults */
  431. -- factory = gtk_icon_factory_new ();
  432. -- gtk_icon_factory_add_default (factory);
  433. -- /* demo_find_file() looks in the the current directory first,
  434. -- * so you can run gtk-demo without installing GTK, then looks
  435. -- * in the location where the file is installed.
  436. -- */
  437. -- pixbuf = NULL;
  438. -- filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
  439. -- if (filename)
  440. -- {
  441. -- pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
  442. -- g_free (filename);
  443. -- }
  444. -- /* Register icon to accompany stock item */
  445. -- if (pixbuf != NULL)
  446. -- {
  447. -- GtkIconSet *icon_set;
  448. -- GdkPixbuf *transparent;
  449. -- /* The gtk-logo-rgb icon has a white background, make it transparent */
  450. -- transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
  451. -- icon_set = gtk_icon_set_new_from_pixbuf (transparent);
  452. -- gtk_icon_factory_add (factory, "demo-gtk-logo", icon_set);
  453. -- gtk_icon_set_unref (icon_set);
  454. -- g_object_unref (pixbuf);
  455. -- g_object_unref (transparent);
  456. -- }
  457. -- else
  458. -- g_warning ("failed to load GTK logo for toolbar");
  459. -- /* Drop our reference to the factory, GTK will hold a reference. */
  460. -- g_object_unref (factory);
  461. -- }
  462. -- }
  463. end
  464. -- static void
  465. -- update_statusbar (GtkTextBuffer *buffer,
  466. -- GtkStatusbar *statusbar)
  467. -- {
  468. -- gchar *msg;
  469. -- gint row, col;
  470. -- gint count;
  471. -- GtkTextIter iter;
  472. -- gtk_statusbar_pop (statusbar, 0); /* clear any previous message,
  473. -- * underflow is allowed
  474. -- */
  475. -- count = gtk_text_buffer_get_char_count (buffer);
  476. -- gtk_text_buffer_get_iter_at_mark (buffer,
  477. -- &iter,
  478. -- gtk_text_buffer_get_insert (buffer));
  479. -- row = gtk_text_iter_get_line (&iter);
  480. -- col = gtk_text_iter_get_line_offset (&iter);
  481. -- msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
  482. -- row, col, count);
  483. -- gtk_statusbar_push (statusbar, 0, msg);
  484. -- g_free (msg);
  485. -- }
  486. -- static void
  487. -- mark_set_callback (GtkTextBuffer *buffer,
  488. -- const GtkTextIter *new_location,
  489. -- GtkTextMark *mark,
  490. -- gpointer data)
  491. -- {
  492. -- update_statusbar (buffer, GTK_STATUSBAR (data));
  493. -- }
  494. -- static void
  495. -- update_resize_grip (GtkWidget *widget,
  496. -- GdkEventWindowState *event,
  497. -- GtkStatusbar *statusbar)
  498. -- {
  499. -- if (event->changed_mask & (GDK_WINDOW_STATE_MAXIMIZED |
  500. -- GDK_WINDOW_STATE_FULLSCREEN))
  501. -- {
  502. -- gboolean maximized;
  503. -- maximized = event->new_window_state & (GDK_WINDOW_STATE_MAXIMIZED |
  504. -- GDK_WINDOW_STATE_FULLSCREEN);
  505. -- gtk_statusbar_set_has_resize_grip (statusbar, !maximized);
  506. -- }
  507. -- }
  508. end