More implementation of menus on GTK+.

This commit is contained in:
Pietro Gagliardi 2015-04-20 18:56:42 -04:00
parent 9f82838632
commit b7e2905c72
4 changed files with 98 additions and 7 deletions

5
menu.c
View File

@ -1,6 +1,9 @@
// 20 april 2015 // 20 april 2015
#include "uipriv.h" #include "uipriv.h"
#if 0
TODO
// dummy items for various predefined menu identifiers // dummy items for various predefined menu identifiers
// these are so we can get a definite unique pointer value // these are so we can get a definite unique pointer value
static volatile char dummyQuit = 'q'; static volatile char dummyQuit = 'q';
@ -12,3 +15,5 @@ const char *const uiMenuItemQuit = &dummyQuit;
const char *const uiMenuItemPreferences = &dummyPreferences; const char *const uiMenuItemPreferences = &dummyPreferences;
const char *const uiMenuItemAbout = &dummyAbout; const char *const uiMenuItemAbout = &dummyAbout;
const char *const uiMenuItemSeparator = &dummySeparator; const char *const uiMenuItemSeparator = &dummySeparator;
#endif

30
test.c
View File

@ -4,11 +4,33 @@
#include <string.h> #include <string.h>
// TODO convert to using the new conversion macros // TODO convert to using the new conversion macros
// TODO why can't these be const?
static const uiMenu menu[] = { static uiMenuItem fileMenu[] = {
{ "File", NULL }, { "New", uiMenuItemTypeCommand },
{ "Edit", NULL }, { "Open", uiMenuItemTypeCommand },
{ "Help", NULL }, { uiMenuItemQuit, uiMenuItemTypeCommand },
{ NULL, 0 },
};
static uiMenuItem editMenu[] = {
{ "Undo", uiMenuItemTypeCommand },
{ uiMenuItemSeparator, uiMenuItemTypeSeparator },
{ "Check Me", uiMenuItemTypeCheckbox },
{ uiMenuItemPreferences, uiMenuItemTypeCommand },
{ NULL, 0 },
};
static uiMenuItem helpMenu[] = {
{ "Help", uiMenuItemTypeCommand },
{ uiMenuItemAbout, uiMenuItemTypeCommand },
{ NULL, 0 },
};
static uiMenu menu[] = {
{ "File", fileMenu },
{ "Edit", editMenu },
{ "Help", helpMenu },
{ NULL, NULL }, { NULL, NULL },
}; };

6
ui.idl
View File

@ -154,10 +154,16 @@ enum MenuItemType {
}; };
// TODO allow these to be specified directly in the IDL // TODO allow these to be specified directly in the IDL
/* TODO
raw "extern const char *const uiMenuItemQuit;"; raw "extern const char *const uiMenuItemQuit;";
raw "extern const char *const uiMenuItemPreferences;"; raw "extern const char *const uiMenuItemPreferences;";
raw "extern const char *const uiMenuItemAbout;"; raw "extern const char *const uiMenuItemAbout;";
raw "extern const char *const uiMenuItemSeparator;"; raw "extern const char *const uiMenuItemSeparator;";
*/
raw "#define uiMenuItemQuit ((char *)1)";
raw "#define uiMenuItemPreferences ((char *)2)";
raw "#define uiMenuItemAbout ((char *)3)";
raw "#define uiMenuItemSeparator ((char *)4)";
raw "#endif"; raw "#endif";

View File

@ -1,9 +1,67 @@
// 20 april 2015 // 20 april 2015
#include "uipriv_unix.h" #include "uipriv_unix.h"
static GtkWidget *makeMenuItem(const char *name, uiMenuItem *items) static void appendSeparator(GtkMenuShell *menu)
{ {
return gtk_menu_item_new_with_label(name); gtk_menu_shell_append(menu, gtk_separator_menu_item_new());
}
static void appendMenuItem(GtkMenuShell *menu, const uiMenuItem *item)
{
GtkWidget *iw;
// TODO see if there are stock items for these three
if (item->Name == uiMenuItemQuit) {
// TODO verify type
appendSeparator(menu);
iw = gtk_menu_item_new_with_label("Quit");
gtk_menu_shell_append(menu, iw);
return;
}
if (item->Name == uiMenuItemPreferences) {
// TODO verify type
appendSeparator(menu);
iw = gtk_menu_item_new_with_label("Preferences");
gtk_menu_shell_append(menu, iw);
return;
}
if (item->Name == uiMenuItemAbout) {
// TODO verify type
appendSeparator(menu);
iw = gtk_menu_item_new_with_label("About");
gtk_menu_shell_append(menu, iw);
return;
}
if (item->Name == uiMenuItemSeparator) {
// TODO verify type
appendSeparator(menu);
return;
}
switch (item->Type) {
case uiMenuItemTypeCommand:
iw = gtk_menu_item_new_with_label(item->Name);
gtk_menu_shell_append(menu, iw);
return;
case uiMenuItemTypeCheckbox:
iw = gtk_check_menu_item_new_with_label(item->Name);
gtk_menu_shell_append(menu, iw);
return;
}
// TODO complain
}
static GtkWidget *makeMenu(const char *name, uiMenuItem *items)
{
GtkWidget *menu;
GtkWidget *submenu;
const uiMenuItem *i;
menu = gtk_menu_item_new_with_label(name);
submenu = gtk_menu_new();
for (i = items; i->Name != NULL; i++)
appendMenuItem(GTK_MENU_SHELL(submenu), i);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), submenu);
return menu;
} }
GtkWidget *makeMenubar(void) GtkWidget *makeMenubar(void)
@ -17,7 +75,7 @@ GtkWidget *makeMenubar(void)
menubar = gtk_menu_bar_new(); menubar = gtk_menu_bar_new();
for (m = options.Menu; m->Name != NULL; m++) for (m = options.Menu; m->Name != NULL; m++)
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), makeMenuItem(m->Name, m->Items)); gtk_menu_shell_append(GTK_MENU_SHELL(menubar), makeMenu(m->Name, m->Items));
gtk_widget_set_hexpand(menubar, TRUE); gtk_widget_set_hexpand(menubar, TRUE);
gtk_widget_set_halign(menubar, GTK_ALIGN_FILL); gtk_widget_set_halign(menubar, GTK_ALIGN_FILL);