Permalink
Browse files

Menu as EventEmitter

  • Loading branch information...
1 parent a5b21d5 commit a97185c7689edff8bdcc7a657c82b3a12e6217cf @milani milani committed Oct 29, 2012
View
@@ -39,7 +39,11 @@ inherit(App, EventEmitter, [
function createMenu(options){
var self = this,
nativeMenu = new NativeMenu(options);
-
+ inherit(nativeMenu, EventEmitter, [
+ function toString(){
+ return '[object Menu]';
+ }
+ ]);
return nativeMenu;
},
function createWindow(url, options){
@@ -102,7 +106,7 @@ inherit(App, EventEmitter, [
]);
-
+NativeMenu.prototype.__proto__ = process.EventEmitter.prototype;
NativeWindow.prototype.__proto__ = process.EventEmitter.prototype;
decorate(NativeWindow.prototype, [
@@ -16,9 +16,23 @@ NativeMenu::~NativeMenu(){
// destroy all v8 persistent objects
}
+void NativeMenu::Emit(Handle<Value>* args,int length){
+ node::MakeCallback(v8handle_, "emit", length, args);
+}
+
+void NativeMenu::Emit(const char* event){
+ Handle<Value> args[1] = { String::New(event) };
+ Emit(args,1);
+}
+
+void NativeMenu::Emit(const char* event, Handle<Value> arg){
+
+ Handle<Value> args[2] = {
+ String::New(event),
+ arg
+ };
-void NativeMenu::SetV8Handle(Handle<Object> v8handle) {
- v8handle_ = v8handle;
+ Emit(args,2);
}
@@ -16,7 +16,13 @@ class NativeMenu {
NativeMenu(Settings* settings);
~NativeMenu();
void Init(Settings* settings);
- void SetV8Handle(v8::Handle<v8::Object> v8handle);
+
+ void Emit(v8::Handle<v8::Value>* args,int);
+ void Emit(const char* event);
+ void Emit(const char* event, v8::Handle<v8::Value> arg);
+
+ void SetV8Handle(v8::Handle<v8::Object> v8handle) {v8handle_ = v8handle;};
+ v8::Handle<v8::Object> GetV8Handle() {return v8handle_;};
#ifdef __LINUX__
std::vector<GtkWidget*> GetMenu() {return menuItems_;};
int AddSubMenu(GtkWidget*,Settings*);
@@ -2,10 +2,10 @@
#include <gtk/gtk.h>
#include "appjs.h"
-#include "native_menu/native_menu.h"
#include "includes/cef.h"
#include "includes/util.h"
#include "includes/cef_handler.h"
+#include "native_menu/native_menu.h"
extern CefRefPtr<ClientHandler> g_handler;
@@ -16,13 +16,21 @@ using namespace v8;
typedef struct _appjs_action_callback {
Persistent<Object> action;
Persistent<Object> item;
+ NativeMenu* menu;
} appjs_action_callback;
-void MenuClicked(GtkWidget* menuItem,appjs_action_callback* actionCallback){
+void menu_active_handler(GtkWidget* menuItem,appjs_action_callback* actionCallback){
Persistent<Object> action = actionCallback->action;
- const int argc = 1;
- Handle<Value> argv[argc] = {actionCallback->item};
- action->CallAsFunction(action,argc,argv);
+ NativeMenu* menu = actionCallback->menu;
+
+ if(action->IsCallable()) {
+ const int argc = 1;
+ Handle<Value> argv[argc] = {actionCallback->item};
+ action->CallAsFunction(menu->GetV8Handle(),argc,argv);
+ }
+
+ menu->Emit("select",Local<Object>::New(actionCallback->item));
+
}
void NativeMenu::Init(Settings* settings) {
@@ -48,15 +56,17 @@ int NativeMenu::AddSubMenu(GtkWidget* menu,Settings* settings){
GtkWidget* menuItem;
char* label;
char* icon;
- appjs_action_callback* actionCb = new appjs_action_callback();
+ appjs_action_callback* actionCb;
for( int i = 0; i < length; i++ ) {
item = new Settings( settings->getObject( i ) );
label = item->getString("label","");
icon = item->getString("icon","");
+ actionCb = new appjs_action_callback();
actionCb->action = Persistent<Object>::New( item->getObject("action") );
actionCb->item = Persistent<Object>::New( settings->getObject( i ) );
+ actionCb->menu = this;
if( strlen( label ) == 0 ) {
menuItem = gtk_separator_menu_item_new();
@@ -75,7 +85,7 @@ int NativeMenu::AddSubMenu(GtkWidget* menu,Settings* settings){
if(AddSubMenu(submenu,subsettings)){
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuItem),submenu);
} else {
- g_signal_connect(G_OBJECT(menuItem), "activate",G_CALLBACK(MenuClicked),actionCb);
+ g_signal_connect(G_OBJECT(menuItem), "activate",G_CALLBACK(menu_active_handler),actionCb);
}
gtk_menu_shell_append( GTK_MENU_SHELL( menu ), menuItem );
@@ -217,6 +217,7 @@ void NativeWindow::SetMenuBar(NativeMenu* nativeMenu) {
gtk_box_pack_start(GTK_BOX (gtk_bin_get_child(GTK_BIN(handle_))), menu_bar, FALSE, FALSE, 0);
gtk_box_reorder_child(GTK_BOX (gtk_bin_get_child(GTK_BIN(handle_))),menu_bar,0);
gtk_widget_show (menu_bar);
+
}
void NativeWindow::OpenFileDialog(uv_work_t* req) {

0 comments on commit a97185c

Please sign in to comment.