John Torjo - C++ Expert

Win32 GUI Generics Library : Save_dlg

save_dlg

True binding of your data to UI controls on your dialog.

I know a lot of you won't believe this, since it's quite close to impossible. So, just to give you a taste, I'll show you a few examples:

Here's how you create a simple dialog to allow editing of user-name and passw.

std::string user_name = "john", passw = "secret";
create_modal_save_dlg( 
    IDD_login,
    // in-line correspondence. No validation
    save_dlg::corresp()
        .add_corresp(&user_name, ID_user_name)
        .add_corresp(&passw, ID_passw),
    null_wnd);
msg_box(null_wnd, "You selected '" + user_name + "' and '" + passw + "'");

This will result in the following:

and

 

 

Synchronizing width/height according to a certain ratio:

struct picture_size {
    int width;
    int height;
    bool preserve_ratio;
};

void on_change_width(const int & old_val, int & new_val, save_dlg::info<picture_size> & info) {
    if ( !info.new_val.preserve_ratio) return;

    if (info.new_val.height == 0) return; // avoid divide by zero
    static double ratio = (double)old_val / info.new_val.height;
    if (ratio > .05)
        info.new_val.height = (int)((double)new_val / ratio);
}

void on_change_height(const int & old_val, int & new_val, save_dlg::info<picture_size> & info) {
    // ... similar
}

int APIENTRY WinMain(...) {
    picture_size pict;
    pict.width = 800;
    pict.height = 600;
    pict.preserve_ratio = true;

    create_modal_save_dlg( 
        IDD_SAMPLE,
        save_dlg::corresp()
            .add_var(pict)
            .add_corresp(&picture_size::width, ID_width)
            .add_corresp(&picture_size::height, ID_height)
            .add_corresp(&picture_size::preserve_ratio, ID_ratio)
            .add_validator(ID_width, on_change_width, validate::on_change)
            .add_validator(ID_height, on_change_height, validate::on_change) ,
        null_wnd);
}

This will result in the following:

 

And finally, a slightly more complex one:

// Full example: win32gui/samples/simple_empl_edit

// employee.h
struct employee {
    std::string first_name;
    std::string last_name;
    // ...
};

// ui_empl.h
#include 
#include "employee.h"

struct ui_empl : win32::gui::save_dlg::corresp {
    ui_empl(employee & empl);
    // ...
};

// ui_empl.cpp
ui_empl::ui_empl(employee & empl) {
    add_var(empl);
    add_corresp( &employee::first_name, ID_first_name);
    add_corresp( &employee::last_name, ID_last_name);
    add_corresp( &employee::birth_date, IDC_birth);

    add_corresp( &employee::id, ID_id);
    add_corresp( &employee::salary, ID_salary);
    add_corresp( &employee::country, ID_country);
    add_corresp( &employee::address, ID_address);
    add_corresp( &employee::email, ID_email);
    add_corresp( &employee::has_homepage, IDC_has_hp);
    add_corresp( &employee::homepage, IDC_hp);

    add_validator(ID_first_name, at_least_6_chars);
    add_validator(ID_last_name, at_least_6_chars);
    add_validator(ID_salary, salary_too_big);
    add_validator(ID_salary, salary_too_small);
    add_validator(ID_id, in_range("Employee ID", 10000, 11000) );
    // on cancel, ask the user if he wants to save...
    add_validator(ID_first_name, what, validate::on_cancel);
}

// main.cpp
int APIENTRY WinMain(...) {
    using namespace win32::gui;
    employee john;
    john.first_name = "John";
    john.last_name = "Doe";
    john.birth_date = mktime_t(1978, one_month(2), 15); // 15th of Feb, 1978
    // ...

    ui_empl ui(john);
    create_modal_save_dlg(IDD_empl, ui, null_wnd);

    std::ostringstream out;
    out << "You have chosen: \n"
        << "\nName: " << john.first_name << ' ' << john.last_name 
        << "\nID: " << john.id
        ...
    msg_box(null_wnd, out.str() );
}

This will result in:

and