An old fashioned guy, revisited

If you search long enough you can eventually find what you’re looking for.

I do have to wonder, however, why some people have to make things so difficult.

So, it turns out that if I look for a tutorial on just plain GTK then it’s easier to find what I’m looking for. On first blush it’s not all that easy to design a form using GTK. There are a lot of fiddly bits to get all lined up to make it work.

Now, I’ll admit right here that I’m not at one with graphical design (using graphical tools to create things) but to my way of thinking there is a lot of non-intuitive stuff going on.

When I first tried putting a menu on the form, I dragged it on and it promptly expanded to be full size - it took up the entire window. Now, that might be OK if the new object was resizable, but it’s not. It turns out that you need containers for all your widgets. This just seems counter-intuitive to me, but now that I know (or at least have an idea of) how it works, I’ll give using GTK another shot. I had switched back to using Windows Forms for a couple of reasons. The first, of course, is that the GTK version wasn’t making sense. Secondly, I’ll give the designers at Microsoft kudos for how they implemented their stuff. It is much simpler and straight forward to implement than the folks at GTK came up with. Thirdly, since this is meant to be a cross platform application I wasn’t sure I wanted to mess around with adding a GTK requirement for the Windows folks.

I don’t know enough yet about distributing ECMA 334 applications yet, so that last point my be a non-issue. Far more important, to me, is the ease of programming. Especially as GTK seems so convoluted. I’ll try an example here to show you.

Windows Forms

public class RSSMenu : MainMenu
{
MenuItem miFile;
MenuItem miFileOpen;
MenuItem miFileClose;
MenuItem miFileExit;

public RSSMenu()
{
miFile = new MenuItem("&File");
MenuItems.Add(miFile);

miFileOpen = new MenuItem("&Open");
miFileClose = new MenuItem("&Close");
miFileExit = new MenuItem("&Exit");

miFile.MenuItems.Add(miFileOpen);
miFile.MenuItems.Add(miFileClose);
miFile.MenuItems.Add(new MenuItem("-"));
miFile.MenuItems.Add(miFileExit);

miFileOpen.Click += new EventHandler(miFileOpen_Click);
miFileClose.Click += new EventHandler(miFileClose_Click);
miFileExit.Click += new EventHandler(miFileExit_Click);

}

public void miFileOpen_Click(object sender, EventArgs e)
{
}
}

Note how easy it is to add items to a menu and event handlers to the widgets.
GTK

menu = gtk_menu_new ();

/* Next we make a little loop that makes three menu-entries for "test-menu".
* Notice the call to gtk_menu_shell_append.  Here we are adding a list of
* menu items to our menu.  Normally, we'd also catch the "clicked"
* signal on each of the menu items and setup a callback for it,
* but it's omitted here to save space. */

for (i = 0; i < 3; i++)
{
/* Copy the names to the buf. */
sprintf (buf, "Test-undermenu - %d", i);

/* Create a new menu-item with a name... */
menu_items = gtk_menu_item_new_with_label (buf);

/* ...and add it to the menu. */
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_items);

/* Do something interesting when the menuitem is selected */
g_signal_connect_swapped (G_OBJECT (menu_items), "activate",
G_CALLBACK (menuitem_response),
(gpointer) g_strdup (buf));

/* Show the widget */
gtk_widget_show (menu_items);
}

/* This is the root menu, and will be the label
* displayed on the menu bar.  There won't be a signal handler attached,
* as it only pops up the rest of the menu when pressed. */
root_menu = gtk_menu_item_new_with_label ("Root Menu");

gtk_widget_show (root_menu);

/* Now we specify that we want our newly created "menu" to be the menu
* for the "root menu" */
gtk_menu_item_set_submenu (GTK_MENU_ITEM (root_menu), menu);
/* Create a menu-bar to hold the menus and add it to our main window */
menu_bar = gtk_menu_bar_new ();

gtk_widget_show (menu_bar);

/* Create a button to which to attach menu as a popup */
button = gtk_button_new_with_label ("press me");
g_signal_connect_swapped (G_OBJECT (button), "event",
G_CALLBACK (button_press),
G_OBJECT (menu));

gtk_widget_show (button);

/* And finally we append the menu-item to the menu-bar -- this is the
* "root" menu-item I have been raving about =) */
gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), root_menu);

/* always display the window as the last step so it all splashes on
* the screen at once. */
gtk_widget_show (window);

The GTK code came from a tutorial located at http://www.gtk.org/tutorial/x1579.html. Note that I am not criticizing his programming techniques here, just contrasting the two methods.

Stumble it!

Explore posts in the same categories: Coding, Ramblings

Comment:

You must be logged in to post a comment.