Skip to content

Commit

Permalink
Merge pull request #6 from federico-terzi/dev
Browse files Browse the repository at this point in the history
Version 0.1.1
  • Loading branch information
federico-terzi authored Sep 24, 2020
2 parents 6871c9d + 1a6a389 commit b4f6d2c
Show file tree
Hide file tree
Showing 22 changed files with 189 additions and 126 deletions.
9 changes: 9 additions & 0 deletions examples/form/form3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
layout: "Author: {{author}}"
fields:
author:
default: "Two"
type: choice
values:
- "AmberHaelo"
- "Two"
- "Three"
6 changes: 6 additions & 0 deletions examples/form/form4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
layout: |
Hello 中文測試 {{name}},
This form is built with modulo!
fields:
name:
default:
9 changes: 6 additions & 3 deletions modulo-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ fn build_native() {
let cpp_flags = get_cpp_flags(&config_path);

let mut build = cc::Build::new();
build.cpp(true)
build
.cpp(true)
.file("native/form.cpp")
.file("native/common.cpp")
.file("native/search.cpp");
.file("native/search.cpp")
.file("native/mac.mm");
build.flag("-std=c++17");

for flag in cpp_flags {
Expand Down Expand Up @@ -220,7 +222,8 @@ fn build_native() {
let cpp_flags = get_cpp_flags(&config_path);

let mut build = cc::Build::new();
build.cpp(true)
build
.cpp(true)
.file("native/form.cpp")
.file("native/search.cpp")
.file("native/common.cpp");
Expand Down
6 changes: 6 additions & 0 deletions modulo-sys/native/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#ifdef __WXMSW__
#include <windows.h>
#endif
#ifdef __WXOSX__
#include "mac.h"
#endif

void setFrameIcon(const char * iconPath, wxFrame * frame) {
if (iconPath) {
Expand Down Expand Up @@ -49,4 +52,7 @@ void Activate(wxFrame * frame) {
SetForegroundWindow(handle);

#endif
#ifdef __WXOSX__
ActivateApp();
#endif
}
26 changes: 20 additions & 6 deletions modulo-sys/native/form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ bool FormApp::OnInit()
FormFrame *frame = new FormFrame(formMetadata->windowTitle, wxPoint(50, 50), wxSize(450, 340) );
setFrameIcon(formMetadata->iconPath, frame);
frame->Show( true );

Activate(frame);

return true;
Expand Down Expand Up @@ -122,7 +122,7 @@ void FormFrame::AddComponent(wxPanel *parent, wxBoxSizer *sizer, FieldMetadata m
case FieldType::LABEL:
{
const LabelMetadata *labelMeta = static_cast<const LabelMetadata*>(meta.specific);
auto label = new wxStaticText(parent, wxID_ANY, labelMeta->text, wxDefaultPosition, wxDefaultSize);
auto label = new wxStaticText(parent, wxID_ANY, wxString::FromUTF8(labelMeta->text), wxDefaultPosition, wxDefaultSize);
control = label;
fields.push_back(label);
break;
Expand All @@ -135,7 +135,7 @@ void FormFrame::AddComponent(wxPanel *parent, wxBoxSizer *sizer, FieldMetadata m
style |= wxTE_MULTILINE;
}

auto textControl = new wxTextCtrl(parent, NewControlId(), textMeta->defaultText, wxDefaultPosition, wxDefaultSize, style);
auto textControl = new wxTextCtrl(parent, NewControlId(), wxString::FromUTF8(textMeta->defaultText), wxDefaultPosition, wxDefaultSize, style);

if (textMeta->multiline) {
textControl->SetMinSize(wxSize(MULTILINE_MIN_WIDTH, MULTILINE_MIN_HEIGHT));
Expand All @@ -151,21 +151,35 @@ void FormFrame::AddComponent(wxPanel *parent, wxBoxSizer *sizer, FieldMetadata m
case FieldType::CHOICE:
{
const ChoiceMetadata *choiceMeta = static_cast<const ChoiceMetadata*>(meta.specific);

int selectedItem = -1;
wxArrayString choices;
for (int i = 0; i<choiceMeta->valueSize; i++) {
choices.Add(choiceMeta->values[i]);
choices.Add(wxString::FromUTF8(choiceMeta->values[i]));

if (strcmp(choiceMeta->values[i], choiceMeta->defaultValue) == 0) {
selectedItem = i;
}
}

void * choice = nullptr;
if (choiceMeta->choiceType == ChoiceType::DROPDOWN) {
choice = (void*) new wxChoice(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, choices);


if (selectedItem >= 0) {
((wxChoice*)choice)->SetSelection(selectedItem);
}

// Create the field wrapper
std::unique_ptr<FieldWrapper> field((FieldWrapper*) new ChoiceFieldWrapper((wxChoice*) choice));
idMap[meta.id] = std::move(field);
}else {
choice = (void*) new wxListBox(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, choices);


if (selectedItem >= 0) {
((wxListBox*)choice)->SetSelection(selectedItem);
}

// Create the field wrapper
std::unique_ptr<FieldWrapper> field((FieldWrapper*) new ListFieldWrapper((wxListBox*) choice));
idMap[meta.id] = std::move(field);
Expand Down
1 change: 1 addition & 0 deletions modulo-sys/native/interop.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef enum ChoiceType {
typedef struct ChoiceMetadata {
const char * const * values;
const int valueSize;
const char *defaultValue;
const ChoiceType choiceType;
} ChoiceMetadata;

Expand Down
1 change: 1 addition & 0 deletions modulo-sys/native/mac.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void ActivateApp();
5 changes: 5 additions & 0 deletions modulo-sys/native/mac.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <AppKit/AppKit.h>

void ActivateApp() {
[[NSRunningApplication currentApplication] activateWithOptions:(NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)];
}
20 changes: 13 additions & 7 deletions modulo-sys/src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub mod types {
pub struct ChoiceMetadata {
pub values: Vec<String>,
pub choice_type: ChoiceType,
pub default_value: String,
}
}

Expand Down Expand Up @@ -103,17 +104,18 @@ mod interop {

let icon_path = if let Some(icon_path) = form.icon.as_ref() {
icon_path.clone()
}else{
} else {
"".to_owned()
};

let icon_path = CString::new(icon_path).expect("unable to convert form icon to CString");
let icon_path =
CString::new(icon_path).expect("unable to convert form icon to CString");

let icon_path_ptr = if form.icon.is_some() {
icon_path.as_ptr()
}else{
} else {
std::ptr::null()
};
};

let _interop = Box::new(FormMetadata {
windowTitle: title.as_ptr(),
Expand Down Expand Up @@ -250,6 +252,7 @@ mod interop {
struct OwnedChoiceMetadata {
values: Vec<CString>,
values_ptr_array: Vec<*const c_char>,
default_value: CString,
_interop: Box<ChoiceMetadata>,
}

Expand All @@ -275,14 +278,19 @@ mod interop {
types::ChoiceType::List => ChoiceType_LIST,
};

let default_value = CString::new(metadata.default_value)
.expect("unable to convert default value to CString");

let _interop = Box::new(ChoiceMetadata {
values: values_ptr_array.as_ptr(),
valueSize: values.len() as c_int,
choiceType: choice_type,
defaultValue: default_value.as_ptr(),
});
Self {
values,
values_ptr_array,
default_value,
_interop,
}
}
Expand Down Expand Up @@ -328,7 +336,7 @@ mod interop {

pub fn show(form: types::Form) -> HashMap<String, String> {
use crate::Interoperable;
use std::os::raw::{c_void, c_char};
use std::os::raw::{c_char, c_void};

let owned_form: interop::OwnedForm = form.into();
let metadata: *const FormMetadata = owned_form.as_ptr() as *const FormMetadata;
Expand Down Expand Up @@ -356,8 +364,6 @@ pub fn show(form: types::Form) -> HashMap<String, String> {
}
}



unsafe {
// TODO: Nested rows should fail, add check
crate::interop::interop_show_form(
Expand Down
20 changes: 4 additions & 16 deletions modulo-sys/src/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![allow(non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

use std::os::raw::{c_void, c_int, c_char};
use std::os::raw::{c_char, c_int, c_void};

// Native bindings

Expand All @@ -13,11 +13,7 @@ extern "C" {
// FORM
pub(crate) fn interop_show_form(
metadata: *const FormMetadata,
callback: extern "C" fn(
values: *const ValuePair,
size: c_int,
map: *mut c_void,
),
callback: extern "C" fn(values: *const ValuePair, size: c_int, map: *mut c_void),
map: *mut c_void,
);

Expand All @@ -30,17 +26,9 @@ extern "C" {
data: *const c_void,
),
items: *const c_void,
result_callback: extern "C" fn(
id: *const c_char,
result: *mut c_void,
),
result_callback: extern "C" fn(id: *const c_char, result: *mut c_void),
result: *mut c_void,
);

pub(crate) fn update_items(
app: *const c_void,
items: *const SearchItem,
itemCount: c_int,
);
pub(crate) fn update_items(app: *const c_void, items: *const SearchItem, itemCount: c_int);
}

4 changes: 2 additions & 2 deletions modulo-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub mod form;
pub mod search;
pub(crate) mod interop;
pub mod search;

use std::ffi::c_void;

pub(crate) trait Interoperable {
fn as_ptr(&self) -> *const c_void;
}
}
Loading

0 comments on commit b4f6d2c

Please sign in to comment.