Skip to content

Commit

Permalink
Added php-gtk helper file for functions, and start to handle errors o…
Browse files Browse the repository at this point in the history
…f parameters
  • Loading branch information
scorninpc committed Jun 13, 2019
1 parent c84ec7d commit b2b0671
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
37 changes: 37 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2637,4 +2637,41 @@ GValue phpgtk_get_gvalue(Php::Value phpgtk_value, GType type_column)

return gtk_value;

}



void phpgtk_throw_wrong_type(int param, Php::Type type)
{
char *buffer;
int len;
std::string str_type;

switch(type) {
case Php::Type::Undefined: str_type.assign("undefined"); break;
case Php::Type::Null: str_type.assign("Null"); break;
case Php::Type::False: str_type.assign("False"); break;
case Php::Type::True: str_type.assign("True"); break;
case Php::Type::Numeric: str_type.assign("Numeric"); break;
case Php::Type::Float: str_type.assign("Float"); break;
case Php::Type::String: str_type.assign("String"); break;
case Php::Type::Array: str_type.assign("Array"); break;
case Php::Type::Object: str_type.assign("Object"); break;
case Php::Type::Resource: str_type.assign("Resource"); break;
case Php::Type::Reference: str_type.assign("Reference"); break;
case Php::Type::Constant: str_type.assign("Constant"); break;
case Php::Type::ConstantAST: str_type.assign("ConstantAST"); break;
case Php::Type::Bool: str_type.assign("Bool"); break;
case Php::Type::Callable: str_type.assign("Callable"); break;
}

// Get len of string
len = snprintf(NULL, 0, "expects at least %d parameters, %s given", param, str_type.c_str());
buffer = (char *)malloc((len + 1) * sizeof(char));

// Save into buffer
snprintf(buffer, len+1, "expects at least %d parameters, %s given", param, str_type.c_str());

// throw
throw Php::Exception(buffer);
}
7 changes: 5 additions & 2 deletions main.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifndef _PHPGTK_H_
#define _PHPGTK_H_
#ifndef _PHPGTK_MAIN_H_
#define _PHPGTK_MAIN_H_

#include <phpcpp.h>
#include <iostream>
#include <gtk/gtk.h>

#include "php-gtk.h"

#include "src/GObject.h"

#include "src/Gdk.h"
Expand Down Expand Up @@ -189,5 +191,6 @@


GValue phpgtk_get_gvalue(Php::Value phpgtk_value, GType type_column);
void phpgtk_throw_wrong_type(int param, Php::Type type);

#endif
16 changes: 16 additions & 0 deletions php-gtk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include "php-gtk.h"

bool phpgtk_check_parameter(Php::Value value, Php::Type expected_type, bool required)
{
Php::call("var_dump", "OK1");
if(required) {
Php::call("var_dump", "OK2");
if(value.type() == Php::Type::Null) {
Php::call("var_dump", "OK3");
throw Php::Exception("tipo errado");
}
Php::call("var_dump", "OK4");
}

}
12 changes: 12 additions & 0 deletions php-gtk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _PHPGTK_H_
#define _PHPGTK_H_

#include <phpcpp.h>
#include <iostream>
#include <gtk/gtk.h>


bool phpgtk_check_parameter(Php::Value value, Php::Type expected_type, bool required);


#endif
2 changes: 2 additions & 0 deletions src/GtkBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ GtkBox_::~GtkBox_() = default;

void GtkBox_::__construct(Php::Parameters &parameters)
{
//phpgtk_check_parameter(parameters[0], Php::Type::Numeric, TRUE);

int int_child = (int)parameters[0];
GtkOrientation child = (GtkOrientation)int_child;

Expand Down
4 changes: 3 additions & 1 deletion src/GtkBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <phpcpp.h>
#include <gtk/gtk.h>

#include "GtkContainer.h"
#include "../php-gtk.h"

#include "GtkContainer.h"
#include "GtkWidget.h"
#include "GtkPackType.h"

Expand Down
7 changes: 5 additions & 2 deletions test.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,14 @@ function GtkCellRendererToggled($renderer=NULL, $path=NULL)

// ----------------------
// Vertical box
$vbox = new GtkBox(GtkOrientation::VERTICAL);
$vbox = new GtkBox();
die("OK");

// $vbox->pack_start(2.2, TRUE, TRUE);

// ----------------------
// Menu
$vbox->pack_start($hlbl = new GtkLabel("- GtkMenuBar"), TRUE, TRUE, 1); $hlbl->set_xalign(0);
$vbox->pack_start(2, TRUE, TRUE, 1); $hlbl->set_xalign(0);

$menubar = new GtkMenuBar();
$vbox->pack_start($menubar, FALSE, FALSE, 0);
Expand Down

0 comments on commit b2b0671

Please sign in to comment.