/* * File Editor :: VoiDAPI Test Project * ----------------------------------- * Coded by: VoiDeD * * Uses: - GNU C library (string) * - WinAPI (HFONT) * - VoiDAPI * * Time Spent: Waaaaay too much... * Mostly in bug smashing... * * Todo: - Fix/Add v_window::set_icon()/get_icon() * - Fix v_file::write_string() */ #include "voidapi.h" //main window v_window* wnd; //open file dialog v_dialog* open_diag; //text edit control v_control* text_edit; //open/save/close buttons v_control* open_btn; v_control* save_btn; v_control* close_btn; //status label v_control* status_lb; //file v_file* file; //winapi font used in the text edit control HFONT font; //used for storing previous length; int p_len; void exit(v_window* wind) { //called when the window X is clicked //destroys the window and the current message loop for that window wind->destroy(); } void openfile(v_control* ctrl, void* data) { //called when the open button is clicked //creates an open file dialog open_diag = new v_dialog("Open File...", "Text Files (*.txt)\0*.txt\0\0", v_openfile, wnd->get_hwnd()); //check if the user clicked ok if (open_diag->show() != v_ok) return; //create a new file class which will open the file file = new v_file(open_diag->get_filename()); //check file length and set the text edit control's text p_len = file->get_length(); if (p_len > 0) { text_edit->set_text(file->read_string(file->get_length())); } //set the title of the window std::string str = ""; str += "File Editor ["; str += open_diag->get_filename(); str += "]"; wnd->set_text(str.c_str()); //update the status label status_lb->set_text("Status: File Opened"); } void savefile(v_control* ctrl, void* data) { //called when the save button is clicked //seek to 0x0 file->seek(0x0, FILE_BEGIN); //clear the file file->write_data(malloc(p_len), p_len); //seek to 0x0 again file->seek(0x0, FILE_BEGIN); //write all the text in the text edit control into the file, replacing old text file->write_string(text_edit->get_text()); //update status label status_lb->set_text("Status: File Saved"); } void closefile(v_control* ctrl, void* data) { //called when the close button is clicked //close the file file->close(); //clear the text edit control text_edit->set_text(""); //set the window title wnd->set_text("File Editor"); //update status label status_lb->set_text("Status: File Closed"); } void init(v_window* ind) { //called when the main window is created //create a new font which will be used in the text edit control font = CreateFont( //mostly annoying WinAPI code, but it can work back and forth 10+4, //between the WinAPI and VoiDAPI 0, //still haven't figured out how to create a font of a specific 0, //size... WinAPI forces you to choose height and width 0, //and its not in pts... FW_NORMAL, false, false, false, ANSI_CHARSET, OUT_TT_ONLY_PRECIS, CLIP_TT_ALWAYS, PROOF_QUALITY, DEFAULT_PITCH | FF_DONTCARE, //the only flag that I like in the WinAPI, FF_DONTCARE :D "Lucida Console\0"); //all that just to select the Lucida Console font... //set the text edit control's font SendMessage(text_edit->get_hwnd(), WM_SETFONT, (WPARAM)font, MAKELPARAM(true, 0)); } int main(int argc, char* argv[]) { //create a new window wnd = new v_window("File Editor", new v_size(600,400)); //create a text edit control text_edit = new v_textbox(new v_size(594,340), new v_point(0,0), wnd->get_hwnd(), true); //create the 3 buttons which control opening/saving/closing files open_btn = new v_button("Open", new v_size(100, 25), new v_point(10, 340), wnd->get_hwnd()); save_btn = new v_button("Save", new v_size(100, 25), new v_point(120, 340), wnd->get_hwnd()); close_btn = new v_button("Close", new v_size(100, 25), new v_point(230, 340), wnd->get_hwnd()); //create a status label status_lb = new v_label("Status: Idle", new v_size(130, 15), new v_point(350, 345), wnd->get_hwnd(), false); //register the close and create events wnd->register_event(WM_CLOSE, &exit); wnd->register_event(WM_CREATE, &init); //register clicked events to handle file opening/saving/closing open_btn->register_event(BN_CLICKED, &openfile); save_btn->register_event(BN_CLICKED, &savefile); close_btn->register_event(BN_CLICKED, &closefile); //add all the controls to the window wnd->control_add(text_edit); wnd->control_add(open_btn); wnd->control_add(save_btn); wnd->control_add(close_btn); wnd->control_add(status_lb); //finally, show the window wnd->show(); }