diff --git a/src/GtkDialog.cpp b/src/GtkDialog.cpp index f557704..3850095 100644 --- a/src/GtkDialog.cpp +++ b/src/GtkDialog.cpp @@ -14,152 +14,156 @@ GtkDialog_::~GtkDialog_() = default; void GtkDialog_::__construct() { - instance = (gpointer *)gtk_dialog_new (); + instance = (gpointer *)gtk_dialog_new (); } + Php::Value GtkDialog_::new_with_buttons(Php::Parameters ¶meters) { - std::string s_title = parameters[0]; - gchar *title = (gchar *)s_title.c_str(); - - Php::Value object_parent = parameters[1]; - GtkWindow *parent; - if (object_parent.instanceOf("GtkWindow")) { - GtkWindow_ *phpgtk_parent = (GtkWindow_ *)object_parent.implementation(); - parent = GTK_WINDOW(phpgtk_parent->get_instance()); - } - - int int_flags = (int)parameters[2]; - GtkDialogFlags flags = (GtkDialogFlags)int_flags; - - // std::string s_first_button_text = parameters[3]; - // gchar *first_button_text = (gchar *)s_first_button_text.c_str(); - - // Create object - GtkDialog_ *dialog = new GtkDialog_(); - // dialog->__construct(); - - // gtk_window_set_title(GTK_WINDOW(dialog->instance), title); - // gtk_widget_set_parent(GTK_WIDGET(dialog->instance), GTK_WIDGET(parent)); - // gtk_dialog_add_button(GTK_DIALOG(dialog->instance), "OK", GTK_RESPONSE_OK); - // gtk_dialog_add_button(GTK_DIALOG(dialog->instance), "Cancel", GTK_RESPONSE_CANCEL); - - dialog->set_instance((gpointer *)gtk_dialog_new_with_buttons(title, GTK_WINDOW(parent), flags, "OK", GTK_RESPONSE_OK, "Cancel", GTK_RESPONSE_CANCEL, NULL)); - - - return Php::Object("GtkDialog", dialog); + std::string s_title = parameters[0]; + gchar *title = (gchar *)s_title.c_str(); + + Php::Value object_parent = parameters[1]; + GtkWindow *parent; + if (object_parent.instanceOf("GtkWindow")) { + GtkWindow_ *phpgtk_parent = (GtkWindow_ *)object_parent.implementation(); + parent = GTK_WINDOW(phpgtk_parent->get_instance()); + } + + int int_flags = (int)parameters[2]; + GtkDialogFlags flags = (GtkDialogFlags)int_flags; + + Php::Value arr = parameters[3]; + if(arr.size() < 2) { + throw Php::Exception("parameters expect one button with response"); + } + + GtkWidget *instance_dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(parent), flags, arr[0], (int)arr[1], NULL); + + // Add buttons + for(int index=2; index < (int)arr.size(); index+=2) { + gtk_dialog_add_button(GTK_DIALOG(instance_dialog), arr[index], (int)arr[index+1]); + + } + + // Create object + GtkDialog_ *dialog = new GtkDialog_(); + dialog->set_instance((gpointer *)instance_dialog); + return Php::Object("GtkDialog", dialog); } Php::Value GtkDialog_::run() { - gint ret = gtk_dialog_run (GTK_DIALOG(instance)); + gint ret = gtk_dialog_run (GTK_DIALOG(instance)); - return ret; + return ret; } void GtkDialog_::response(Php::Parameters ¶meters) { - gint response_id = (gint)parameters[0]; + gint response_id = (gint)parameters[0]; - gtk_dialog_response (GTK_DIALOG(instance), response_id); + gtk_dialog_response (GTK_DIALOG(instance), response_id); } void GtkDialog_::add_button(Php::Parameters ¶meters) { - std::string s_button_text = parameters[0]; - gchar *button_text = (gchar *)s_button_text.c_str(); + std::string s_button_text = parameters[0]; + gchar *button_text = (gchar *)s_button_text.c_str(); - gint response_id = (gint)parameters[1]; + gint response_id = (gint)parameters[1]; - gtk_dialog_add_button (GTK_DIALOG(instance), button_text, response_id); + gtk_dialog_add_button (GTK_DIALOG(instance), button_text, response_id); } void GtkDialog_::add_buttons(Php::Parameters ¶meters) { - std::string s_button_text = parameters[0]; - gchar *button_text = (gchar *)s_button_text.c_str(); - - gint response_id = (gint)parameters[1]; - gtk_dialog_add_buttons (GTK_DIALOG(instance), button_text, response_id, NULL); + Php::Value arr = parameters[0]; + + // Add buttons + for(int index=0; index<(int)arr.size(); index+=2) { + gtk_dialog_add_button(GTK_DIALOG(instance), arr[index], arr[index+1]); + + } } void GtkDialog_::add_action_widget(Php::Parameters ¶meters) { - GtkWidget *child; - if(parameters.size() > 0) { - Php::Value object_child = parameters[0]; - GtkWidget_ *phpgtk_child = (GtkWidget_ *)object_child.implementation(); - child = GTK_WIDGET(phpgtk_child->get_instance()); - } + GtkWidget *child; + if(parameters.size() > 0) { + Php::Value object_child = parameters[0]; + GtkWidget_ *phpgtk_child = (GtkWidget_ *)object_child.implementation(); + child = GTK_WIDGET(phpgtk_child->get_instance()); + } - gint response_id = (gint)parameters[1]; + gint response_id = (gint)parameters[1]; - gtk_dialog_add_action_widget (GTK_DIALOG(instance), child, response_id); + gtk_dialog_add_action_widget (GTK_DIALOG(instance), child, response_id); } void GtkDialog_::set_default_response(Php::Parameters ¶meters) { - gint response_id = (gint)parameters[0]; + gint response_id = (gint)parameters[0]; - gtk_dialog_set_default_response (GTK_DIALOG(instance), response_id); + gtk_dialog_set_default_response (GTK_DIALOG(instance), response_id); } void GtkDialog_::set_response_sensitive(Php::Parameters ¶meters) { - gint response_id = (gint)parameters[0]; + gint response_id = (gint)parameters[0]; - gboolean setting = (gboolean)parameters[1]; + gboolean setting = (gboolean)parameters[1]; - gtk_dialog_set_response_sensitive (GTK_DIALOG(instance), response_id, setting); + gtk_dialog_set_response_sensitive (GTK_DIALOG(instance), response_id, setting); } void GtkDialog_::get_response_for_widget(Php::Parameters ¶meters) { - GtkWidget *widget; - if(parameters.size() > 0) { - Php::Value object_widget = parameters[0]; - GtkWidget_ *phpgtk_widget = (GtkWidget_ *)object_widget.implementation(); - widget = GTK_WIDGET(phpgtk_widget->get_instance()); - } + GtkWidget *widget; + if(parameters.size() > 0) { + Php::Value object_widget = parameters[0]; + GtkWidget_ *phpgtk_widget = (GtkWidget_ *)object_widget.implementation(); + widget = GTK_WIDGET(phpgtk_widget->get_instance()); + } - gtk_dialog_get_response_for_widget (GTK_DIALOG(instance), widget); + gtk_dialog_get_response_for_widget (GTK_DIALOG(instance), widget); } Php::Value GtkDialog_::get_widget_for_response(Php::Parameters ¶meters) { - gint response_id = (gint)parameters[0]; + gint response_id = (gint)parameters[0]; - GtkWidget *ret = gtk_dialog_get_widget_for_response (GTK_DIALOG(instance), response_id); + GtkWidget *ret = gtk_dialog_get_widget_for_response (GTK_DIALOG(instance), response_id); - GtkWidget_ *return_parsed = new GtkWidget_(); - return_parsed->set_instance((gpointer *)ret); - return Php::Object("GtkWidget", return_parsed); + GtkWidget_ *return_parsed = new GtkWidget_(); + return_parsed->set_instance((gpointer *)ret); + return Php::Object("GtkWidget", return_parsed); } Php::Value GtkDialog_::get_content_area() { - GtkWidget *ret = gtk_dialog_get_content_area (GTK_DIALOG(instance)); + GtkWidget *ret = gtk_dialog_get_content_area (GTK_DIALOG(instance)); - GtkBox_ *return_parsed = new GtkBox_(); - return_parsed->set_instance((gpointer *)ret); - return Php::Object("GtkBox", return_parsed); + GtkBox_ *return_parsed = new GtkBox_(); + return_parsed->set_instance((gpointer *)ret); + return Php::Object("GtkBox", return_parsed); } Php::Value GtkDialog_::get_header_bar() { - GtkWidget *ret = gtk_dialog_get_header_bar (GTK_DIALOG(instance)); + GtkWidget *ret = gtk_dialog_get_header_bar (GTK_DIALOG(instance)); - GtkWidget_ *return_parsed = new GtkWidget_(); - return_parsed->set_instance((gpointer *)ret); - return Php::Object("GtkWidget", return_parsed); + GtkWidget_ *return_parsed = new GtkWidget_(); + return_parsed->set_instance((gpointer *)ret); + return Php::Object("GtkWidget", return_parsed); } diff --git a/src/GtkFileChooserDialog.cpp b/src/GtkFileChooserDialog.cpp index 927c423..4930652 100644 --- a/src/GtkFileChooserDialog.cpp +++ b/src/GtkFileChooserDialog.cpp @@ -26,23 +26,18 @@ void GtkFileChooserDialog_::__construct(Php::Parameters ¶meters) int int_action = (int)parameters[2]; GtkFileChooserAction action = (GtkFileChooserAction)int_action; - instance = (gpointer *)gtk_file_chooser_dialog_new (title, parent, action, NULL); + Php::Value arr = parameters[3]; + if(arr.size() < 2) { + throw Php::Exception("parameters expect one button with response"); + } - // Step buttons - Php::Array arr = parameters[3]; - for(int i=0; i<=arr.size(); i+=2) { + instance = (gpointer *)gtk_file_chooser_dialog_new (title, parent, action, arr[0], (int)arr[1], NULL); - if(arr.size() >= i+1) { - std::string button_text = arr[i]; - gchar *text = (gchar *)button_text.c_str(); - - int button_action = (int)arr[i+1]; - GtkResponseType action = (GtkResponseType)button_action; - - gtk_dialog_add_button(GTK_DIALOG(instance), text, action); - } - - } + // Add buttons + for(int index=2; index < (int)arr.size(); index+=2) { + gtk_dialog_add_button(GTK_DIALOG(instance), arr[index], (int)arr[index+1]); + + } } @@ -410,7 +405,7 @@ Php::Value GtkFileChooserDialog_::list_filters() for(int index=0; GSList *item=g_slist_nth(ret, index); index++) { GtkFileFilter_ *return_parsed = new GtkFileFilter_(); - return_parsed->set_instance((gpointer *)GTK_FILE_CHOOSER(item->data)); + return_parsed->set_instance((gpointer *)GTK_FILE_FILTER(item->data)); ret_arr[index] = Php::Object("GtkFileFilter", return_parsed); diff --git a/test1.php b/test1.php index 4b415a5..540cfce 100644 --- a/test1.php +++ b/test1.php @@ -12,4 +12,71 @@ function GtkWindowDestroy($widget=NULL, $event=NULL) $win->show_all(); +/* +// Create dialog with buttons +$dialogBox = GtkDialog::new_with_buttons("Pergunta", $win, GtkDialogFlags::MODAL, ["OK", GtkResponseType::OK, "Cancel", GtkResponseType::CANCEL]); + // Create content area + $hbox = new GtkHBox(); + $label = new GtkLabel("Hello!"); + $hbox->pack_start($label, FALSE, FALSE); + + // Add the content area + $topArea = $dialogBox->get_content_area(); + $topArea->add($hbox); + $dialogBox->show_all(); + + // run + $result = $dialogBox->run(); + if($result == GtkResponseType::OK) { + var_dump("OK"); + } + $dialogBox->destroy(); +*/ + +/* +// Create a message dialog +$dialog = GtkMessageDialog::new_with_markup($win, GtkDialogFlags::MODAL, GtkMessageType::INFO, GtkButtonsType::OK, "Want close PHP-GTK3 test program?"); + // run + $result = $dialog->run(); + if($result == GtkResponseType::OK) { + var_dump("OK"); + } + $dialog->destroy(); +*/ + +// File chooser +$dialog = new GtkFileChooserDialog("Open file", $win, GtkFileChooserAction::OPEN, ["Cancel", GtkResponseType::CANCEL, "Ok", GtkResponseType::OK]); + + // filters + $filter = new GtkFileFilter(); + $filter->set_name("PHP Files"); + $filter->add_pattern("*.php"); + $dialog->add_filter($filter); + + // run with filter + $dialog->set_select_multiple(TRUE); + $result = $dialog->run(); + if($result == GtkResponseType::OK) { + var_dump($dialog->get_filenames()); + } + $dialog->destroy(); + + // Remove filters + $a = $dialog->list_filters(); + foreach($a as $b) { + $dialog->remove_filter($b); + } + + // run without filters + $dialog->set_select_multiple(TRUE); + $result = $dialog->run(); + if($result == GtkResponseType::OK) { + var_dump($dialog->get_filenames()); + } + $dialog->destroy(); + + + + + Gtk::main(); \ No newline at end of file diff --git a/test2.php b/test2.php index af63358..ccfe9c6 100644 --- a/test2.php +++ b/test2.php @@ -153,17 +153,15 @@ public function create_new_tab($label) } + $a = $dialog->list_filters(); + foreach($a as $b) { + $dialog->remove_filter($b); + } - // $a = $dialog->list_filters(); - // foreach($a as $b) { - // $dialog->remove_filter($a); - // // var_dump($b->get_name()); - // } - - // $a = $dialog->run(); - // if($a == GtkResponseType::OK) { - // var_dump($dialog->get_filenames()); - // } + $a = $dialog->run(); + if($a == GtkResponseType::OK) { + var_dump($dialog->get_filenames()); + }