Advertisement
palmerstone

Gtk 2

Nov 17th, 2011
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <gtk/gtk.h>
  4. #include <cairo.h>
  5.  
  6. GtkContainer *box;
  7. GtkWidget *window;
  8. GtkWidget *vbox;
  9. GtkWidget *menubar;
  10. GtkWidget *filemenu, *help, *helpmenu;
  11. GtkWidget *file, *quit, *open, *save, *properties, *about, *test;
  12. GtkWidget *img, *img2;
  13. GtkWidget *dialog;
  14.  
  15. void CreateMenu()
  16. {
  17. vbox = gtk_vbox_new(FALSE, 0);
  18. box = gtk_vbox_new(FALSE, 0);
  19. gtk_box_pack_start(GTK_BOX(box), vbox, TRUE, TRUE, 0);
  20. menubar = gtk_menu_bar_new();
  21. filemenu = gtk_menu_new();
  22.  
  23. file = gtk_menu_item_new_with_label("File");
  24. quit = gtk_menu_item_new_with_label("Quit");
  25. open = gtk_menu_item_new_with_label("Open");
  26. save = gtk_menu_item_new_with_label("Save");
  27. properties = gtk_menu_item_new_with_label("Properties");
  28.  
  29. gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
  30. gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), open);
  31. gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), save);
  32. gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), properties);
  33. gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
  34. gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
  35.  
  36. helpmenu = gtk_menu_new();
  37. about = gtk_menu_item_new_with_label("About");
  38. help = gtk_menu_item_new_with_label("Help");
  39. gtk_menu_item_set_submenu(GTK_MENU_ITEM(help), helpmenu);
  40. gtk_menu_shell_append(GTK_MENU_SHELL(helpmenu), about);
  41. gtk_menu_shell_append(GTK_MENU_SHELL(menubar), help);
  42. gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 3);
  43. }
  44.  
  45. void show_about()
  46. {
  47. GtkWidget *dialog;
  48. const char *author[] = {"None yet\n(However, Special thanks to Mehedi Hasan :-) )",NULL};
  49. dialog=gtk_about_dialog_new();
  50. gtk_about_dialog_set_program_name (GTK_ABOUT_DIALOG(dialog),"You just clicked about!");
  51. gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG(dialog),author);
  52. gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG(dialog),"Good job and go to hell");
  53. gtk_window_set_position(GTK_WINDOW(dialog),GTK_WIN_POS_CENTER);
  54. gtk_dialog_run(GTK_DIALOG(dialog));
  55. }
  56.  
  57. void open_file()
  58. {
  59. dialog = gtk_file_chooser_dialog_new ("Open File",window,GTK_FILE_CHOOSER_ACTION_OPEN,GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,NULL);
  60. if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
  61. {
  62. char *filename;
  63. filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
  64. img2 = gtk_image_new_from_file(filename);
  65. gtk_widget_show(img2);
  66. gtk_container_remove(GTK_CONTAINER(box), img);
  67. gtk_box_pack_start(GTK_BOX(box), img2, TRUE, TRUE, 0);
  68. g_free (filename);
  69. }
  70. gtk_widget_destroy (dialog);
  71. }
  72.  
  73. int main(int argc, char*argv[])
  74. {
  75. gtk_init(&argc, &argv);
  76. window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  77. gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  78. gtk_window_set_default_size(GTK_WINDOW(window), 800, 500);
  79. gtk_window_set_title(GTK_WINDOW(window), "Aaarghhhh");
  80.  
  81. CreateMenu();
  82. char picture_name[] = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg";
  83. img = gtk_image_new_from_file(picture_name);
  84. gtk_box_pack_start(GTK_BOX(box), img, TRUE, TRUE, 0);
  85. gtk_container_add(GTK_CONTAINER(window), box);
  86.  
  87. g_signal_connect_swapped(G_OBJECT(about), "activate", G_CALLBACK(show_about), NULL);
  88. g_signal_connect_swapped(G_OBJECT(open), "activate", G_CALLBACK(open_file), NULL);
  89. g_signal_connect_swapped(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), NULL);
  90. g_signal_connect(G_OBJECT(quit), "activate",G_CALLBACK(gtk_main_quit), NULL);
  91.  
  92. gtk_widget_show_all(window);
  93. gtk_main();
  94. return 0;
  95. }
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement