mlib - multimedia libraries Code
Status: Alpha
Brought to you by:
mark_wexler
--- a +++ b/dbox.cpp @@ -0,0 +1,433 @@ +/* + +How to do dialogs, including color boxes, properly + +- Assume you have a dialog template, ID_DIALOG +- you have a parameter struct or class, and an instance named par (with initial values set) +- and a text control, ID_COLOR_TEXT, and a static box for displaying color, ID_COLOR, +- and a color member in par: par.col +- To start dialog, do something like: + +if(DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(PARDIALOG), NULL, ParametersProc, LPARAM(&par)) != IDOK) + return false; + +- Write the dialog procedure (called ParametersProc in this example) as follows: + +BOOL CALLBACK ParametersProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam) +{ + static parameters *p; + static dbox *pd; + + case WM_INITDIALOG: + p = (parameters *) lParam; + pd = new dbox(hdlg); + + pd->put(ID_CONTROL, p->member); + ... + pd->put(ID_COLOR_TEXT, p->col); + return TRUE; + case WM_PAINT: + pd->put(ID_COLOR, p->col); + return 0; + case WM_COMMAND: + switch(LOWORD(wParam)) { + case ACTIVE_CONTROL: + // real-time responses to events + return 0; + case ID_TEXT: + pd->get(ID_COLOR_TEXT, p->col); + return 0; + case IDOK: + pd->get(ID_CONTROL, p->member); + ... + pd->get(ID_COLOR_TEXT, p->col); + delete pd; + EndDialog(hdlg, IDOK); + return 0; + case IDCANCEL: + delete pd; + EndDialog(hdlg, IDCANCEL); + return 0; + } + break; + } + return(FALSE); +} +*/ + +#include <vector> +#include <string> +using namespace std; +#include <iostream> +#include <sstream> +#include <windows.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "newutil.h" +#include "vect.h" +#include "dbox.h" +#include "winutil.h" +#pragma warning(disable: 4800) + +#define TEMP_LENGTH 5000 +#define MAX_CLASS_NAME 100 + +static char temp[TEMP_LENGTH]; + +dbox::dbox(HWND hdlg) +{ + _hdlg = hdlg; +} + +bool dbox::put(int item, const int val) +{ + item_type t = type(item); + if(t == COMBO) + return SendMessage(GetDlgItem(_hdlg, item), CB_SETCURSEL, (WPARAM) val, 0) != CB_ERR; + else if(t == LIST) + return SendMessage(GetDlgItem(_hdlg, item), LB_SETCURSEL, (WPARAM) val, 0) != CB_ERR; + else + return SetDlgItemInt(_hdlg, item, val, TRUE); +} + +bool dbox::put(int item, const vector<int> &val, const bool linebreak) +{ + strcpy(temp, ""); + if(val.size() > 0) { + if(linebreak) + sprintf(temp, "%d%c\n", val[0], 13); + else + sprintf(temp, "%d", val[0]); + } + for(int i = 1; i < val.size(); i++) { + if(linebreak) + sprintf(temp, "%s%d%c\n", temp, val[i], 13); + else + sprintf(temp, "%s %d", temp, val[i]); + } + return SetDlgItemText(_hdlg, item, temp); +} + +bool dbox::put(int item, const double val) +{ + sprintf(temp, "%g", val); + return SetDlgItemText(_hdlg, item, temp); +} + +bool dbox::put(int item, const vector<double> &val, const bool linebreak) +{ + strcpy(temp, ""); + if(val.size() > 0) { + if(linebreak) + sprintf(temp, "%g%c\n", val[0], 13); + else + sprintf(temp, "%g", val[0]); + } + for(int i = 1; i < val.size(); i++) { + if(linebreak) + sprintf(temp, "%s%g%c\n", temp, val[i], 13); + else + sprintf(temp, "%s %g", temp, val[i]); + } + return SetDlgItemText(_hdlg, item, temp); +} + +bool dbox::put(int item, const string &val) +{ + const item_type t = type(item); + if(t == COMBO) { + return CB_ERR != SendMessage(window_handle(item), CB_SELECTSTRING, + WPARAM(-1), LPARAM(val.c_str())); + } + else if(t == COMBO) + return LB_ERR != SendMessage(window_handle(item), LB_SELECTSTRING, + WPARAM(-1), LPARAM(val.c_str())); + else + return SetDlgItemText(_hdlg, item, val.c_str()); +} + +bool dbox::put(int item, const char *fmt, ...) +{ + char str[TEMP_LENGTH]; + va_list args; + va_start(args, fmt); + vsprintf(str, fmt, args); + va_end(args); + return put(item, string(str)); +} + +bool dbox::put(int item, const bool val) +{ + return CheckDlgButton(_hdlg, item, val ? BST_CHECKED : BST_UNCHECKED); +} + +bool dbox::put(int from, const int to, const int val) +{ + return CheckRadioButton(_hdlg, from, to, val); +} + +bool dbox::put(int item, const v2 &val) +{ + ostringstream out; + for(int i = 0; i < 2; i++) { + out << val(i); + if(i < 2 - 1) out << ' '; + } + return put(item, out.str()); +} + +bool dbox::put(int item, const v3 &val) +{ + ostringstream out; + for(int i = 0; i < 3; i++) { + out << val(i); + if(i < 3 - 1) out << ' '; + } + return put(item, out.str()); +} + +bool dbox::put(int item, const color &col) +{ + if(type(item) == EDIT) { + ostringstream out; + out << col.r << " " << col.g << " " << col.b; + put(item, out.str()); + } + else { + HWND hwnd = GetDlgItem(_hdlg, item); + InvalidateRect(hwnd, NULL, TRUE); + PAINTSTRUCT ps; + HDC hdc = BeginPaint(hwnd, &ps); + HBRUSH hbrush = CreateSolidBrush(col.gdi()); + HBRUSH hbrush_old = HBRUSH(SelectObject(hdc, hbrush)); + HPEN hpen = CreatePen(PS_NULL, 0, 0); + HPEN hpen_old = HPEN(SelectObject(hdc, hpen)); + RECT rect; GetClientRect(hwnd, &rect); + Rectangle(hdc, 0, 0, rect.right, rect.bottom); + SelectObject(hdc, hpen_old); DeleteObject(hpen); + SelectObject(hdc, hbrush_old); DeleteObject(hbrush); + EndPaint(hwnd, &ps); + } + return true; +} + +bool dbox::load(int item, const string &fn) +{ + return load_file_into_edit_box(fn, GetDlgItem(_hdlg, item)); +} + +// for combo or list boxes (currently doesn't work for list boxes) +// put sel = -1 to remove any selection +bool dbox::load(int item, const vector<string> &val, const int sel) +{ + const item_type t = type(item); + if(t != COMBO && t != LIST) return false; + load_internal(item, val); + if(sel >= -1) put(item, sel); + return true; +} + +bool dbox::load(int item, const vector<string> &val, const string &sel) +{ + const item_type t = type(item); + if(t != COMBO && t != LIST) return false; + load_internal(item, val); + put(item, sel); + return true; +} + +void dbox::load_internal(int item, const vector<string> &val) +{ + item_type t = type(item); + if(t != COMBO && t != LIST) return; + HWND hwnd = GetDlgItem(_hdlg, item); + SendMessage(hwnd, t == COMBO ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0); + for(vector<string>::const_iterator it = val.begin(); it != val.end(); it++) + SendMessage(hwnd, COMBO ? CB_ADDSTRING : LB_ADDSTRING, 0, (LPARAM) it->c_str()); +} + +bool dbox::get(int item, int &var) +{ + BOOL ok; + item_type t = type(item); + if(t == COMBO) { + const LRESULT res = SendMessage(GetDlgItem(_hdlg, item), CB_GETCURSEL, 0, 0); + if(res != CB_ERR) { + var = int(res); + return true; + } + else + return false; + } + else { + int ret = GetDlgItemInt(_hdlg, item, &ok, TRUE); + if(ok) var = ret; + return ok; + } +} + +bool dbox::get(int item, vector<int> &var) +{ + if(GetDlgItemText(_hdlg, item, temp, TEMP_LENGTH) == 0) return false; + extract_from_string(temp, var); + return true; +} + +bool dbox::get(int item, double &var) +{ + if(GetDlgItemText(_hdlg, item, temp, TEMP_LENGTH) == 0) return false; + double ret; + if(sscanf(temp, " %lf ", &ret) == 1) { + var = ret; + return(true); + } + else + return(false); +} + +bool dbox::get(int item, vector<double> &var) +{ + if(GetDlgItemText(_hdlg, item, temp, TEMP_LENGTH) == 0) return false; + extract_from_string(temp, var); + return true; +} + +bool dbox::get(int item, string &var) +{ + GetDlgItemText(_hdlg, item, temp, TEMP_LENGTH); + var = temp; + return true; +} + +bool dbox::get(int item, bool &var) +{ + var = (IsDlgButtonChecked(_hdlg, item) == BST_CHECKED); + return true; +} + +bool dbox::get(int from, int to, int &var) +{ + for(int i = from; i <= to; i++) { + if(IsDlgButtonChecked(_hdlg, i) == BST_CHECKED) { + var = i; + return true; + } + } + return false; +} + +bool dbox::get(int item, v2 &var) +{ + if(GetDlgItemText(_hdlg, item, temp, TEMP_LENGTH) == 0) return false; + vector<double> v; + extract_from_string(temp, v); + if(v.size() != 2) return false; + var = v2(v); + return true; +} + +bool dbox::get(int item, v3 &var) +{ + if(GetDlgItemText(_hdlg, item, temp, TEMP_LENGTH) == 0) return false; + vector<double> v; + extract_from_string(temp, v); + if(v.size() != 3) return false; + var = v3(v); + return true; +} + +bool dbox::get(int item, color &col) +{ + if(type(item) != EDIT) return false; + string text; + get(item, text); + istringstream in(text); + vector<float> x; + float x_temp; + in >> x_temp; + while(in) { + x.push_back(x_temp); + in >> x_temp; + } + bool ok = true; + if(x.size() == 1) + col = color(x[0]); + else if(x.size() == 3) + col = color(x[0], x[1], x[2]); + else + ok = false; + if(ok) update(); + return ok; +} + +void dbox::enable(int item, bool enabl) +{ + EnableWindow(GetDlgItem(_hdlg, item), enabl); +} + +void dbox::focus(int item) +{ + SetFocus(GetDlgItem(_hdlg, item)); +} + +void dbox::update() +{ + PostMessage(_hdlg, WM_PAINT, 0, 0); +} + +HWND dbox::window_handle(int item) +{ + return GetDlgItem(_hdlg, item); +} + +/* +void SetComboRange(HWND hdlg, int item, int from, int to) +{ + int i; + HWND hcombo; + + hcombo = GetDlgItem(hdlg, item); + SendMessage(hcombo, CB_RESETCONTENT, 0, 0); + for(i = from; i <= to; i++) { + sprintf(temp, "%i", i); + SendMessage(hcombo, CB_ADDSTRING, 0, (LPARAM) temp); + } + return; +} + +int GetComboRange(HWND hdlg, int item, int from) +{ + int i; + HWND hcombo; + LRESULT res; + + hcombo = GetDlgItem(hdlg, item); + res = SendMessage(hcombo, CB_GETCURSEL, 0, 0); + if(res != CB_ERR) { + i = (int) res; + return(from + i); + } + else + return(-1); +} +*/ + +dbox::item_type dbox::type(int item) +{ + static char class_name[MAX_CLASS_NAME]; + HWND hwnd = GetDlgItem(_hdlg, item); + GetClassName(hwnd, class_name, MAX_CLASS_NAME - 1); + if(strcmp(class_name, "Edit") == 0) + return EDIT; + else if(strcmp(class_name, "ComboBox") == 0) + return COMBO; + else if(strcmp(class_name, "ListBox") == 0) + return LIST; + else if(strcmp(class_name, "Button") == 0) + return BUTTON; + else if(strcmp(class_name, "Static") == 0) + return STATIC; + else + return UNKNOWN; +}