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