Click here to Skip to main content
15,881,172 members
Articles / Containers / Virtual Machine

An extendable report editor

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
3 Sep 2008CPOL3 min read 40.8K   2K   35  
An extendable report editor. You can simply add your own controls without recompiling the program or writing annoying plug-ins.
// WndProperty.cpp: implementation of the CWndProperty class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "WndProperty.h"
#include "mem_tool.h"
#include "global.h"
#include "resource.h"
#include "winmisc.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWndProperty::CWndProperty()
{
    this->InitBasic();
}
CWndProperty::~CWndProperty()
{
    this->Destroy();
}
int CWndProperty::InitBasic()
{
    CWnd::InitBasic();
	
	this->eb_property = NULL;
	this->pb_apply = NULL;
	this->pb_cancel = NULL;
	this->pb_ok = NULL;
	this->pb_default = NULL;
	this->i_pobj = NULL;
	this->i_same_rect = NULL;
	this->tbb_file = NULL;
	this->tbb_folder = NULL;
	this->tbb_text = NULL;
	this->i_wnd_txt = NULL;

    return OK;
}
int CWndProperty::Init()
{
    this->InitBasic();
    CWnd::Init();
    //add your code
    return OK;
}
int  CWndProperty::Destroy()
{
	DEL(this->tbb_text);
	DEL(this->tbb_file);
	DEL(this->tbb_folder);
	DEL(this->tbb_color);
	DEL(this->tbb_font);
	DEL(this->tool_bar);
	DEL(eb_property);
	DEL(pb_ok);
	DEL(pb_cancel);
	DEL(pb_apply);
	DEL(pb_default);
    CWnd::Destroy();
    this->InitBasic();
    return OK;
}

int CWndProperty::OnCreate(WPARAM wparam, LPARAM lparam)
{
	NEW(this->eb_property,CEditBox);
	this->eb_property->Init();
	this->eb_property->SetMaxText(1024 * 1024);
	this->eb_property->SetParent(hwnd);
	this->eb_property->Create();
	this->eb_property->SetStockFont(OEM_FIXED_FONT);

	NEW(this->pb_apply,CButton);
	this->pb_apply->Init();
	this->pb_apply->SetParent(hwnd);
	this->pb_apply->Create();
	this->pb_apply->SetStockFont(DEFAULT_GUI_FONT);
	this->pb_apply->SetText("Apply");

	NEW(this->pb_default,CButton);
	this->pb_default->Init();
	this->pb_default->SetParent(hwnd);
	this->pb_default->Create();
	this->pb_default->SetStockFont(DEFAULT_GUI_FONT);
	this->pb_default->SetText("Default");

	NEW(this->pb_ok,CButton);
	this->pb_ok->Init();
	this->pb_ok->SetParent(hwnd);
	this->pb_ok->Create();
	this->pb_ok->SetStockFont(DEFAULT_GUI_FONT);
	this->pb_ok->SetText("OK");

	NEW(this->pb_cancel,CButton);
	this->pb_cancel->Init();
	this->pb_cancel->SetParent(hwnd);
	this->pb_cancel->Create();
	this->pb_cancel->SetStockFont(DEFAULT_GUI_FONT);
	this->pb_cancel->SetText("Cancel");

	NEW(this->tool_bar,CToolBar);
	this->tool_bar->Init();
	this->tool_bar->SetParent(hwnd);
	this->tool_bar->Create();
	this->tool_bar->CreateImageList(IDB_BITMAP_TOOLBAR_1,16,16,8,2,RGB(192,192,192));
	this->tool_bar->SetAutoSize();

	NEW(this->tbb_color,CTBButton);
	this->tbb_color->Init();
	this->tbb_color->SetImgIndex(0);
	this->tbb_color->SetToolTip("Insert color code");

	NEW(this->tbb_font,CTBButton);
	this->tbb_font->Init();
	this->tbb_font->SetImgIndex(1);
	this->tbb_font->SetToolTip("Insert font code");

	NEW(this->tbb_file,CTBButton);
	this->tbb_file->Init();
	this->tbb_file->SetImgIndex(2);
	this->tbb_file->SetToolTip("Insert file code");

	NEW(this->tbb_folder,CTBButton);
	this->tbb_folder->Init();
	this->tbb_folder->SetImgIndex(3);
	this->tbb_folder->SetToolTip("Insert folder code");

	NEW(this->tbb_text,CTBButton);
	this->tbb_text->Init();
	this->tbb_text->SetImgIndex(4);
	this->tbb_text->SetToolTip("Insert text code");

	this->tool_bar->AddButton(this->tbb_color);
	this->tool_bar->AddButton(this->tbb_font);
	this->tool_bar->AddButton(this->tbb_file);
	this->tool_bar->AddButton(this->tbb_folder);
	this->tool_bar->AddButton(this->tbb_text);

	return OK;
}
int CWndProperty::ProcessAllMsg(UINT message, WPARAM wparam, LPARAM lparam)
{
	switch(message)
	{
		case WM_WND_TXT_OK: 
			return this->OnWndTxtOk(wparam,lparam);
	}
	return CWnd::ProcessAllMsg(message,wparam,lparam);
}
int CWndProperty::OnSize(WPARAM wparam, LPARAM lparam)
{
	ASSERT(this->i_same_rect);

	RECT r,rt;
	
	int button_width=75,button_height=20,button_interval=10;
	int from_bottom = 5;

	this->tool_bar->OnSize();

	GetClientRect(hwnd,&r);
	GetWindowRect(this->tool_bar->hwnd,&rt);
	
	r.top += (rt.bottom - rt.top);

	if(r.right / 4 < (75+10))
	{
		button_width = (r.right / 4) * 8/10;
		button_interval = (r.right / 4) * 2/10;
	}

	this->eb_property->MoveWindow(r.left,r.top,r.right-r.left,r.bottom -r.top - 30);

	this->pb_default->MoveWindow(r.right - button_width * 4 - button_interval *4,
								 r.bottom - from_bottom - button_height,
								 button_width,button_height);

	this->pb_apply->MoveWindow(r.right - button_width * 3 - button_interval *3,
							   r.bottom - from_bottom - button_height,
							   button_width,button_height);
	
	this->pb_ok->MoveWindow(r.right - button_width * 2 - button_interval *2,
							r.bottom - from_bottom - button_height,
							button_width,button_height);

	this->pb_cancel->MoveWindow(r.right - button_width * 1 - button_interval *1,
								r.bottom - from_bottom - button_height,
								button_width,button_height);

	::GetWindowRect(hwnd,&r);	
	memcpy(this->i_same_rect,&r,sizeof(RECT));

	return OK;
}

int CWndProperty::OnClose(WPARAM wparam, LPARAM lparam)
{
	this->Hide();
	return OK;
}

int CWndProperty::LoadObjBox()
{
	ASSERT(this->i_pobj);
	CMem mem;

	mem.Init();
	this->SetText(this->i_pobj->GetName());
	int size = this->i_pobj->mf_user_attrib->GetSize();	
	mem.Malloc(size + 10);

	mem.WriteFile(this->i_pobj->mf_user_attrib);
	mem.Putc(0);

	this->eb_property->SetText(mem.p);
	this->eb_property->SetModifyFlag(FALSE);

	return OK;
}

int CWndProperty::OnCommand(WPARAM wparam, LPARAM lparam)
{
	static int is_run = false;

	if(is_run)
		return ERROR;

	is_run = true;

	if(this->pb_ok->IsMyCommand(wparam))
	{
		this->Hide();
		this->OnOK();
	}

	if(this->pb_apply->IsMyCommand(wparam))
		this->OnOK();

	if(this->pb_cancel->IsMyCommand(wparam))
		this->Hide();
	
	if(this->pb_default->IsMyCommand(wparam))
		this->LoadDefault();

	if(this->tbb_color->IsMyCommand(wparam))
	{
		char buf[LBUF_SIZE];
		COLORREF c;
		if(CWinMisc::ChooseColor(this->wnd_create->hparent,&c))
		{
			sprintf(buf,"RGB(%d,%d,%d)",GetRValue(c),GetGValue(c),GetBValue(c));
			this->eb_property->ReplaceSel(buf);
		}
	}

	if(this->tbb_font->IsMyCommand(wparam))
	{
		CMem mem;

		LOCAL_MEM(mem);
		LOGFONT log_font;

		if(CWinMisc::ChooseFont(this->wnd_create->hparent,&log_font))
		{
			mem.Printf("\r\n/***log font***/\r\n");
			mem.Printf("log_font.lfHeight = %d;\r\n",log_font.lfHeight);
			mem.Printf("log_font.lfWidth = %d;\r\n",log_font.lfWidth);
			mem.Printf("log_font.lfEscapement = %d;\r\n",log_font.lfEscapement);
			//mem.Printf("log_font.lfOrientation = %d;\r\n",log_font.lfOrientation);
			mem.Printf("log_font.lfWeight = %d;\r\n",log_font.lfWeight);
			mem.Printf("log_font.lfItalic = %d;\r\n",log_font.lfItalic);
			mem.Printf("log_font.lfUnderline = %d;\r\n",log_font.lfUnderline);
			mem.Printf("log_font.lfStrikeOut = %d;\r\n",log_font.lfStrikeOut);
			mem.Printf("log_font.lfCharSet = %d;\r\n",log_font.lfCharSet);
			//mem.Printf("log_font.lfOutPrecision = %d;\r\n",log_font.lfOutPrecision);
			//mem.Printf("log_font.lfClipPrecision = %d;\r\n",log_font.lfClipPrecision);
			//mem.Printf("log_font.lfQuality = %d;\r\n",log_font.lfQuality);
			//mem.Printf("log_font.lfPitchAndFamily = %d;\r\n",log_font.lfPitchAndFamily);
			mem.Printf("strcpy(log_font.lfFaceName,\"%s\");\r\n",log_font.lfFaceName);			
			mem.Printf("/**************/\r\n");
			mem.Putc(0);

			this->eb_property->ReplaceSel(mem.p);
		}
	}

	if(this->tbb_file->IsMyCommand(wparam))
	{
		char fn[1024];
		if(CWinMisc::GetAFileName(this->wnd_create->hparent,fn))
		{
			CMem mem_rep;

			LOCAL_MEM(mem_rep);

			this->ToCStr(fn,&mem_rep);
			this->eb_property->ReplaceSel(mem_rep.p);
		}
	}
	
	if(this->tbb_folder->IsMyCommand(wparam))
	{
		char fn[1024];
		if(CWinMisc::FolderSelect(this->wnd_create->hparent,fn))
		{
			CMem mem_rep;
			
			LOCAL_MEM(mem_rep);			
			this->ToCStr(fn,&mem_rep);
			this->eb_property->ReplaceSel(mem_rep.p);
		}
	}

	if(this->tbb_text->IsMyCommand(wparam))
	{
		ASSERT(this->i_wnd_txt);		
		this->i_wnd_txt->Show();
	}

	is_run = false;

	return OK;
}

int CWndProperty::LoadDefault()
{
	ASSERT(this->i_pobj);
	
	CMem mem;

	mem.Init();
	
	CXmlNode *px = this->i_pobj->i_xml_node->GetChildByName("default");
	ASSERT(px && px->mf_value);
	
	mem.Malloc(px->mf_value->GetSize() + 10);
	mem.WriteFile(px->mf_value);
	mem.Putc(0);

	this->eb_property->SetText(mem.p);
	this->eb_property->SetModifyFlag(TRUE);

	return OK;
}

int CWndProperty::OnOK()
{	
	if(this->i_pobj == NULL)
	{
		this->eb_property->SetText("");
		return ERROR;
	}

	if(!(this->eb_property->Modified()))
		return ERROR;

	CMem mem;
	int len,ret;
	
	mem.Init();

	len = this->eb_property->GetTextLen();
	mem.Malloc(len+10);
	this->eb_property->GetText(mem.p,mem.GetMaxSize());
	mem.SetSize(len);

	this->i_pobj->mf_user_attrib->SetSize(0);
	this->i_pobj->mf_user_attrib->WriteFile(&mem);

	ret = SendMessage(this->wnd_create->hparent,WM_WND_PROPERTY_OK,0,0);
	
	if(ret == OK)
		this->eb_property->SetModifyFlag(FALSE);
	
	return OK;
}

int CWndProperty::OnPosChanged(WPARAM wparam,LPARAM lparam)
{
	RECT r;

	::GetWindowRect(hwnd,&r);	
	memcpy(this->i_same_rect,&r,sizeof(RECT));

	return OK;
}
int CWndProperty::Show()
{
	ASSERT(this->i_same_rect);
	this->MoveWindow(this->i_same_rect);
	CWnd::Show();

	return OK;
}
int CWndProperty::OnNotify(WPARAM wparam, LPARAM lparam)
{
	this->tool_bar->OnNotify(wparam,lparam);
	return OK;
}

int CWndProperty::ToCStr(CFileBase *in, CFileBase *out)
{
	ASSERT(in && out);
	
	char ch;

	in->Seek(0);
	out->SetSize(0);
	out->Putc('\"');
	
	while(!in->IsEnd())
	{
		ch = in->Getc();
		if(ch == 0)
			break;

		if(strchr("\"\'\\",ch))
		{
			out->Putc('\\');
			out->Putc(ch);
		}
		else if(ch == '\r')
			out->Puts("\\r");
		else if(ch == '\n')
			out->Puts("\\n\"\r\n\"");
		else if(ch == '\t')
			out->Puts("\\t");
		else 
			out->Putc(ch);		
	}

	out->Putc('\"');	
	out->Putc(0);

	return OK;
}
int CWndProperty::ToCStr(char *str_in,CFileBase *out)
{
	ASSERT(str_in && out);

	CMem mem;

	mem.Init();
	mem.SetP(str_in,strlen(str_in));

	return CWndProperty::ToCStr(&mem,out);
}

int CWndProperty::OnWndTxtOk(WPARAM wparam, LPARAM lparam)
{
	ASSERT(this->i_wnd_txt);

	CMemFile mf;
	CMem mem;

	mem.Init();
	mf.Init();

	this->ToCStr(this->i_wnd_txt->mem_txt,&mf);

	mem.Malloc(mf.GetSize() + 10);

	mem.WriteFile(&mf);
	mem.Putc(0);

	this->eb_property->ReplaceSel(mem.p);
	
	return OK;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
China China
26 years old, 2 years work experience.

Comments and Discussions