Skip to content

Commit bd1481c

Browse files
committed
Basic UI skeleton
1 parent 1654245 commit bd1481c

File tree

3 files changed

+192
-11
lines changed

3 files changed

+192
-11
lines changed

Makefile

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.PHONY: all clean run sanitize backends windows full backends-full install
2-
OBJS = core/core.o core/config.o core/backend.o core/plugin.o core/routing.o
2+
CORE_OBJS = core/core.o core/config.o core/backend.o core/plugin.o core/routing.o
33

44
PREFIX ?= /usr
55
PLUGIN_INSTALL = $(PREFIX)/lib/midimonster
@@ -20,17 +20,17 @@ core/%: CFLAGS += -I./
2020
midimonster: LDLIBS = -ldl
2121
# Replace version string with current git-describe if possible
2222
ifneq "$(GITVERSION)" ""
23-
midimonster: CFLAGS += -DMIDIMONSTER_VERSION=\"$(GITVERSION)\"
24-
midimonster.exe: CFLAGS += -DMIDIMONSTER_VERSION=\"$(GITVERSION)\"
25-
assets/resource.o: RCCFLAGS += -DMIDIMONSTER_VERSION=\\\"$(GITVERSION)\\\"
23+
CFLAGS += -DMIDIMONSTER_VERSION=\"$(GITVERSION)\"
2624
endif
2725

2826
# Work around strange linker passing convention differences in Linux and OSX
2927
ifeq ($(SYSTEM),Linux)
3028
midimonster: LDFLAGS += -Wl,-export-dynamic
29+
midimonster_gui: LDFLAGS += -Wl,-export-dynamic
3130
endif
3231
ifeq ($(SYSTEM),Darwin)
3332
midimonster: LDFLAGS += -Wl,-export_dynamic
33+
midimonster_gui: LDFLAGS += -Wl,-export_dynamic
3434
endif
3535

3636
# Allow overriding the locations for backend plugins and default configuration
@@ -56,8 +56,16 @@ backends-full:
5656
$(MAKE) -C backends full
5757

5858
# This rule can not be the default rule because OSX the target prereqs are not exactly the build prereqs
59-
midimonster: midimonster.c portability.h $(OBJS)
60-
$(CC) $(CFLAGS) $(LDFLAGS) $< $(OBJS) $(LDLIBS) -o $@
59+
midimonster: midimonster.c portability.h $(CORE_OBJS)
60+
$(CC) $(CFLAGS) $(LDFLAGS) $< $(CORE_OBJS) $(LDLIBS) -o $@
61+
62+
# The minimal GUI works reasonably well with both gtk+-2.0 and gtk+-3.0
63+
midimonster_gui: GTK_VERSION ?= gtk+-3.0
64+
midimonster_gui: LDLIBS = -ldl
65+
midimonster_gui: GTK_CFLAGS ?= -Wno-pedantic $(shell pkg-config --cflags $(GTK_VERSION))
66+
midimonster_gui: GTK_LDLIBS ?= $(shell pkg-config --libs $(GTK_VERSION))
67+
midimonster_gui: midimonster_gui.c portability.h $(CORE_OBJS)
68+
$(CC) $(CFLAGS) $(GTK_CFLAGS) $(LDFLAGS) $< $(CORE_OBJS) $(LDLIBS) $(GTK_LDLIBS) -o $@
6169

6270
assets/resource.o: assets/midimonster.rc assets/midimonster.ico
6371
$(RCC) $(RCCFLAGS) $< -o $@ --output-format=coff
@@ -70,15 +78,15 @@ midimonster.exe: RCC ?= x86_64-w64-mingw32-windres
7078
midimonster.exe: CFLAGS += -Wno-format
7179
midimonster.exe: LDLIBS = -lws2_32
7280
midimonster.exe: LDFLAGS += -Wl,--out-implib,libmmapi.a
73-
midimonster.exe: midimonster.c portability.h $(OBJS) assets/resource.o
74-
$(CC) $(CFLAGS) $(LDFLAGS) $< $(OBJS) assets/resource.o $(LDLIBS) -o $@
81+
midimonster.exe: midimonster.c portability.h $(CORE_OBJS) assets/resource.o
82+
$(CC) $(CFLAGS) $(LDFLAGS) $< $(CORE_OBJS) assets/resource.o $(LDLIBS) -o $@
7583

7684
clean:
77-
$(RM) midimonster
85+
$(RM) midimonster midimonster_gui
7886
$(RM) midimonster.exe
7987
$(RM) libmmapi.a
8088
$(RM) assets/resource.o
81-
$(RM) $(OBJS)
89+
$(RM) $(CORE_OBJS)
8290
$(MAKE) -C backends clean
8391

8492
run:

midimonster.c

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ static int usage(char* fn){
3939
return EXIT_FAILURE;
4040
}
4141

42-
4342
static int platform_initialize(){
4443
#ifdef _WIN32
4544
unsigned error_mode = SetErrorMode(0);

midimonster_gui.c

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#include <stdio.h>
2+
#include <gtk/gtk.h>
3+
4+
#ifndef _WIN32
5+
#define MM_API __attribute__((visibility("default")))
6+
#else
7+
#define MM_API __attribute__((dllexport))
8+
#endif
9+
10+
#include "midimonster.h"
11+
#include "core/core.h"
12+
#include "core/config.h"
13+
14+
/*
15+
* TODO
16+
* * disable menu items (load, start, stop) when appropriate
17+
* * ringbuffer for log entries
18+
*/
19+
20+
static GtkTreeView* list_view = NULL;
21+
static GtkWidget* window = NULL;
22+
23+
enum {
24+
UI_NONE_ID = 0,
25+
UI_MENU_EXIT,
26+
UI_MENU_LOAD,
27+
UI_MENU_START,
28+
UI_MENU_STOP,
29+
UI_MENU_ABOUT,
30+
UI_MENU_HOMEPAGE,
31+
UI_SENTINEL_ID
32+
};
33+
34+
static void ui_listview_push(char* module, char* message){
35+
GtkTreeIter iter;
36+
GtkListStore* list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
37+
gtk_list_store_append(list_store, &iter);
38+
gtk_list_store_set(list_store, &iter, 0, module, 1, message, -1);
39+
//return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),NULL) - 1;
40+
}
41+
42+
MM_API int log_printf(int level, char* module, char* fmt, ...){
43+
int rv = 0;
44+
va_list args;
45+
va_start(args, fmt);
46+
fprintf(stderr, "%s%s\t", level ? "debug/" : "", module);
47+
rv = vfprintf(stderr, fmt, args);
48+
va_end(args);
49+
return rv;
50+
}
51+
52+
static void ui_filechooser(){
53+
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
54+
gint res;
55+
56+
GtkWidget* dialog = gtk_file_chooser_dialog_new("Load configuration", GTK_WINDOW(window), action, "_Cancel", GTK_RESPONSE_CANCEL, "_Open", GTK_RESPONSE_ACCEPT, NULL);
57+
res = gtk_dialog_run(GTK_DIALOG(dialog));
58+
if(res == GTK_RESPONSE_ACCEPT){
59+
char* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
60+
ui_listview_push("ui", filename);
61+
g_free(filename);
62+
}
63+
64+
gtk_widget_destroy(dialog);
65+
}
66+
67+
static gboolean ui_menu_handler(GtkWidget* widget, GdkEvent* raw_event, gpointer data){
68+
GdkEventButton* event = (GdkEventButton*) raw_event;
69+
if(event->type != GDK_BUTTON_PRESS){
70+
return FALSE;
71+
}
72+
73+
switch((size_t) data){
74+
case UI_MENU_EXIT:
75+
gtk_main_quit();
76+
break;
77+
case UI_MENU_START:
78+
ui_listview_push("ui", "Starting midimonster core");
79+
break;
80+
case UI_MENU_LOAD:
81+
ui_filechooser();
82+
break;
83+
case UI_MENU_STOP:
84+
ui_listview_push("ui", "Stopping midimonster core");
85+
break;
86+
case UI_MENU_HOMEPAGE:
87+
//gtk_show_uri(NULL, "https://midimonster.net/", event->time, NULL);
88+
break;
89+
default:
90+
break;
91+
}
92+
return TRUE;
93+
}
94+
95+
static void ui_menu_item(GtkWidget* menu, char* title, size_t identifier, void* callback){
96+
GtkWidget* item = gtk_menu_item_new_with_label(title);
97+
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
98+
//g_signal_connect(item, "activate", G_CALLBACK(callback), (gpointer) identifier);
99+
g_signal_connect(item, "event", G_CALLBACK(callback), (gpointer) identifier);
100+
gtk_widget_show(item);
101+
}
102+
103+
static void ui_menu_submenu(GtkWidget* menu, char* title, GtkWidget* submenu){
104+
GtkWidget* item = gtk_menu_item_new_with_label(title);
105+
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
106+
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
107+
gtk_widget_show(item);
108+
}
109+
110+
static void ui_rootmenu(GtkWidget* menu_bar){
111+
GtkWidget* file_menu = gtk_menu_new();
112+
GtkWidget* translation_menu = gtk_menu_new();
113+
GtkWidget* help_menu = gtk_menu_new();
114+
115+
ui_menu_item(file_menu, "Load configuration", UI_MENU_LOAD, ui_menu_handler);
116+
ui_menu_item(file_menu, "Exit", UI_MENU_EXIT, ui_menu_handler);
117+
118+
ui_menu_item(translation_menu, "Start", UI_MENU_START, ui_menu_handler);
119+
ui_menu_item(translation_menu, "Stop", UI_MENU_STOP, ui_menu_handler);
120+
121+
ui_menu_item(help_menu, "About", UI_MENU_ABOUT, ui_menu_handler);
122+
ui_menu_item(help_menu, "Open homepage", UI_MENU_HOMEPAGE, ui_menu_handler);
123+
124+
ui_menu_submenu(menu_bar, "File", file_menu);
125+
ui_menu_submenu(menu_bar, "Translation", translation_menu);
126+
ui_menu_submenu(menu_bar, "Help", help_menu);
127+
}
128+
129+
static GtkTreeViewColumn* ui_list_column(GtkTreeView* listview, char* name, size_t identifier){
130+
GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes(name, gtk_cell_renderer_text_new(), "text", identifier, NULL);
131+
gtk_tree_view_append_column(GTK_TREE_VIEW(listview), column);
132+
gtk_tree_view_column_set_resizable(column, TRUE);
133+
return column;
134+
}
135+
136+
int main(int argc, char** argv){
137+
GtkWidget* menu_bar, *scroller, *vbox;
138+
139+
gtk_init(&argc, &argv);
140+
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
141+
gtk_widget_set_size_request(GTK_WIDGET(window), 400, 200);
142+
gtk_window_set_title(GTK_WINDOW(window), MIDIMONSTER_VERSION);
143+
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
144+
//gtk_container_set_border_width(GTK_CONTAINER(window), 10);
145+
146+
menu_bar = gtk_menu_bar_new();
147+
ui_rootmenu(menu_bar);
148+
149+
#if GTK_MAJOR_VERSION >= 3
150+
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
151+
#else
152+
vbox = gtk_vbox_new(FALSE, 0);
153+
#endif
154+
gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, FALSE, 2);
155+
156+
GtkListStore *store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING, -1);
157+
list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
158+
g_object_unref(store);
159+
gtk_tree_view_set_reorderable(list_view, FALSE);
160+
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view)), GTK_SELECTION_NONE);
161+
162+
ui_list_column(list_view, "Module", 0);
163+
ui_list_column(list_view, "Message", 1);
164+
165+
scroller = gtk_scrolled_window_new(NULL, NULL);
166+
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroller), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
167+
gtk_container_add(GTK_CONTAINER(scroller), GTK_WIDGET(list_view));
168+
gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET(scroller), TRUE, TRUE, 2);
169+
170+
gtk_container_add(GTK_CONTAINER(window), vbox);
171+
gtk_widget_show_all(window);
172+
gtk_main();
173+
return 0;
174+
}

0 commit comments

Comments
 (0)