当前位置: 首页 > 工具软件 > weasel > 使用案例 >

山东大学软件工程应用与实践——WeaselUI(三)

戈安翔
2023-12-01

2021SC@SDUSC

WeaselPanel

#pragma once
#include <WeaselCommon.h>
#include <WeaselUI.h>

WeaselPanel.cpp引用WeaselPanel.h头文件,头文件中又引用WeaselCommon.h和WeaselUI.h,这两个文件在wtl包中,定义了关于基本布局的一些内容,所以我们先看一下这两个文件,本篇报告先分析一下WeaselCommon.h文件代码。

我们首先介绍一下wtl

WTL是Windows Template Library的缩写,由微软的ATL(Active Template Library)小组开发,主要是基于ATL对Wind32 API的封装。从2.0后,功能逐步完善,成为了一个完整的支持窗口的框架。WTL功能不如MFC完善,但是比MFC更小巧,不依赖MFC的DLL。
它给各种类型的应用程序提供了一个基本的框架,虽然没有MFC那样的文档/视图结构,但是有视图(Views)。在WTL有大量的代码来管理视图,而且加入自己的代码也很容易。WTL有AppWizard,可以生成SDI,MDI和多线程SDI程序,多线程SDI跟IE或Windows Explorer很像,它看起来是打开了很多个程序实例,实际上这些窗口都是属于一个进程。
现在WTL也有GDI类了,然而HDC的封装类就像CWindow一样,只进行了很简单的封装,它几乎没有加入任何新的功能,不过,在WTL,你可以得到播放meta文件和OpenGL支持, WTL有打印机支持,甚至打印预览, 当然也有GDI对象的封装,诸如画笔,画刷,区域等。
WTL还有一些实用类.最重要的是CString,不错,它是从MFC克隆得到的(copy on write),具有MFC版本的所有方法,还有查找文件的API的封装类,以及CRect,CSize and CPoint。在WeaselUI这个包中就使用到了CSize。

WeaselCommon.h

  • 在WeaselCommon.h文件中就定义了weasel的命名空间
enum TextAttributeType
	{
		NONE = 0,
		HIGHLIGHTED,
		LAST_TYPE
	};

定义了一个枚举类型的文本属性类型

struct TextRange
	{
		TextRange() : start(0), end(0) {}
		TextRange(int _start, int _end) : start(_start), end(_end) {}
		int start;
		int end;
	};

结构体TextRange定义结构体的文本范围,包括开始start()
和结束end()

struct TextAttribute
	{
		TextAttribute() : type(NONE) {}
		TextAttribute(int _start, int _end, TextAttributeType _type) : range(_start, _end), type(_type) {}
		TextRange range;
		TextAttributeType type;
	};

结构体TextAttribute定义了文本的属性

struct Text
	{
		Text() : str(L"") {}
		Text(std::wstring const& _str) : str(_str) {}
		void clear()
		{
			str.clear();
			attributes.clear();
		}
		bool empty() const
		{
			return str.empty();
		}
		std::wstring str;
		std::vector<TextAttribute> attributes;
	};

结构体Test定义了文本的一些格式,包括test方法,清空clear()方法,empty()方法

struct CandidateInfo
	{
		CandidateInfo()
		{
			currentPage = 0;
			totalPages = 0;
			highlighted = 0;
		}
		void clear()
		{
			currentPage = 0;
			totalPages = 0;
			highlighted = 0;
			candies.clear();
			labels.clear();
		}
		bool empty() const
		{
			return candies.empty();
		}
		int currentPage;
		int totalPages;
		int highlighted;
		std::vector<Text> candies;
		std::vector<Text> comments;
		std::vector<Text> labels;
	};

结构体CandidateInfo定义了候选信息,声明了CandidateInfo()、clear()、empty() 等方法以及currentPage、totalPages、highlighted等变量,定义了当前页面、全部页面以及选中部分等。

struct Context
	{
		Context() {}
		void clear()
		{
			preedit.clear();
			aux.clear();
			cinfo.clear();
		}
		bool empty() const
		{
			return preedit.empty() && aux.empty() && cinfo.empty();
		}
		Text preedit;
		Text aux;
		CandidateInfo cinfo;
	};

结构体Context定义了上下文语境等,实例化了Text的两个对象 preedit和aux,,以及CandidateInfo的一个对象cinfo来调用CandidateInfo,通过调用Text的clear方法来清空一些数据,然后empty方法的返回值为bool类型,返回是否清空的信息。

// 由ime管理
	struct Status
	{
		Status() : ascii_mode(false), composing(false), disabled(false) {}
		void reset()
		{
			schema_name.clear();
			ascii_mode = false;
			composing = false;
			disabled = false;
		}
		// 输入方法
		std::wstring schema_name;
		// 转换开关
		bool ascii_mode;
		// 写作状态
		bool composing;
		// 维护模式(暂停输入功能)
		bool disabled;
	};
Status结构体定义了一些状态信息。
其中std::wstring schema_name;为输入方法,schema_name为模式名称,猜测应该是小狼毫的各种输入方法,例如:简体中文、繁体中文、英文以及一些其他自定义的语言等等。
ascii_mode为转换开关,猜测应该是对输入方法进行转换的一个变量。
composing为写作状态,是一个布尔类型的变量,两种状态分别代表在输入和不在输入。
disabled为维护模式,也是一个布尔类型的变量,当该变量值为true时,代表维护模式,即暂停输入的功能,为false时即为非维护模式,可以正常输入。
“Status() : ascii_mode(false), composing(false), disabled(false) {}” States方法一开始将转换开关、写作状态和维护模式都初始为false,表示我们输入法一开始时并不是写作状态,也不是维护状态,可以正常使用,当使用输入法进行输入时才会切换为写作状态。
reset()方法返回值为空,它的作用是将定义的状态重置为初始值。
// 用於向前端告知設置信息
	struct Config
	{
		Config() : inline_preedit(false) {}
		void reset()
		{
			inline_preedit = false;
		}
		bool inline_preedit;
	};

Config结构体定义配置信息,用于向前端告知设置信息。

struct UIStyle
	{
		enum PreeditType
		{
			COMPOSITION,
			PREVIEW
		};
		enum LayoutType
		{
			LAYOUT_VERTICAL = 0,
			LAYOUT_HORIZONTAL,
			LAYOUT_VERTICAL_FULLSCREEN,
			LAYOUT_HORIZONTAL_FULLSCREEN,
			LAYOUT_TYPE_LAST
		};

		PreeditType preedit_type;
		LayoutType layout_type;
		std::wstring font_face;
		int font_point;
		bool inline_preedit;
		bool display_tray_icon;
		std::wstring label_text_format;
		// layout
		int min_width;
		int min_height;
		int border;
		int margin_x;
		int margin_y;
		int spacing;
		int candidate_spacing;
		int hilite_spacing;
		int hilite_padding;
		int round_corner;
		// color scheme
		int text_color;
		int candidate_text_color;
		int label_text_color;
		int comment_text_color;
		int back_color;
		int border_color;
		int hilited_text_color;
		int hilited_back_color;
		int hilited_candidate_text_color;
		int hilited_candidate_back_color;
		int hilited_label_text_color;
		int hilited_comment_text_color;
		// per client
		int client_caps;

		UIStyle() : 
		  font_face(),
			font_point(0),
			inline_preedit(false),
			preedit_type(COMPOSITION),
			display_tray_icon(false),
			label_text_format(L"%s."),
			layout_type(LAYOUT_VERTICAL),
			min_width(0),
			min_height(0),
			border(0),
			margin_x(0),
			margin_y(0),
			spacing(0),
			candidate_spacing(0),
			hilite_spacing(0),
			hilite_padding(0),
			round_corner(0),
			text_color(0),
			candidate_text_color(0),
			label_text_color(0),
			comment_text_color(0),
			back_color(0),
			border_color(0),
			hilited_text_color(0),
			hilited_back_color(0),
			hilited_candidate_text_color(0),
			hilited_candidate_back_color(0),
			hilited_label_text_color(0),
			hilited_comment_text_color(0),
			client_caps(0) {}
	};
}
UIStyle结构体定义了界面设计风格。
其中定义了两个枚举类型的函数,PreeditType()定义了预编译类型,函数体只包含两个常量COMPOSITION和PREVIEW。LayoutType()函数定义了布局类型,函数体包括垂直布局LAYOUT_VERTICAL,水平布局LAYOUT_HORIZONTAL,全屏垂直布局LAYOUT_VERTICAL_FULLSCREEN,全屏水平布局LAYOUT_HORIZONTAL_FULLSCREEN,最终布局LAYOUT_TYPE_LAST等常量。
~定义了 font_point、inline_preedit、display_tray_icon这几个变量font_point应该是字号的意思是,inline_preedit应该是单行模式,display_tray_icon
应该是显示任务栏图标的意思。
以及定义了一系列关于布局和颜色模式的变量。
 类似资料: