⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 window.cpp

📁 MONA是为数不多的C++语言编写的一个很小的操作系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*Copyright (c) 2004 baysideAll rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditionsare met:1. Redistributions of source code must retain the above copyright   notice, this list of conditions and the following disclaimer.2. Redistributions in binary form must reproduce the above copyright   notice, this list of conditions and the following disclaimer in the   documentation and/or other materials provided with the distribution.3. The name of the author may not be used to endorse or promote products   derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS ORIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIESOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUTNOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OFTHIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#include "baygui.h"/** タイマースレッドID */static dword timerID = THREAD_UNKNOWN;/** タイマースレッド */static void TimerThread(){	MonAPI::Message::send(timerID, MSG_SERVER_START_OK);	//printf("TimerThread created\n");		MessageInfo info;	while (1) {		if (!MonAPI::Message::receive(&info)) {			if (info.header == MSG_GUISERVER_SETTIMER) {				//printf("WindowManager->TimerThread MSG_GUISERVER_SETTIMER received %d, %d\n", info.arg1, info.arg2);				syscall_sleep(info.arg2 / 10);				MonAPI::Message::send(info.arg1, MSG_GUISERVER_ONTIMER, 0, 0, 0, NULL);			}		}	}}/** 閉じるボタン */static unsigned int closeIcon[13][13] = {	{0x94928c,0x949294,0x848284,0x848684,0x949294,0x848684,0x848684,0x848284,0x8c8a8c,0x848284,0x949294,0x7c7e7c,0xe4e2e4},	{0x848284,0x141214,0x343234,0x1c1e1c,0x242624,0x242224,0x1c1e1c,0x242224,0x2c2e2c,0x141614,0x343634,0x1c1e1c,0xeceaec},	{0x94928c,0x444244,0xdcdedc,0xd4d2d4,0xcccecc,0xcccecc,0xcccecc,0xcccecc,0xc4c6c4,0xd4d2d4,0xb4b2b4,0x3c3a3c,0xfcfefc},	{0x84827c,0x141214,0xd4d6d4,0x9c9e9c,0x9c9a9c,0xacaaac,0xa4a6a4,0xbcbabc,0xbcbebc,0xc4c6c4,0x949694,0x1c1a1c,0xfcfefc},	{0x8c8a8c,0x2c2a2c,0xd4d6d4,0x9c9a9c,0xa4a6a4,0xb4b6b4,0xbcbabc,0xc4c2c4,0xcccecc,0xd4d2d4,0x8c8a8c,0x1c1a1c,0xfcfefc},	{0x8c8a84,0x1c1e1c,0xcccacc,0xa4a2a4,0xb4b2b4,0xb4b6b4,0xc4c2c4,0xcccacc,0xd4d2d4,0xdcdadc,0x8c8a8c,0x242224,0xfcfefc},	{0x848684,0x242224,0xcccacc,0xacaeac,0xb4b6b4,0xbcbebc,0xcccecc,0xd4d2d4,0xdcdedc,0xdcdedc,0x848284,0x242624,0xf4f6f4},	{0x8c8a8c,0x2c2a2c,0xcccecc,0xb4b2b4,0xbcbebc,0xcccacc,0xcccacc,0xd4d2d4,0xe4e6e4,0xf4f2f4,0x94928c,0x242224,0xfcfafc},	{0x94928c,0x1c1e1c,0xcccecc,0xbcbebc,0xcccacc,0xd4d6d4,0xd4d2d4,0xdcdedc,0xeceeec,0xe4e6e4,0x8c8a8c,0x141614,0xfcfefc},	{0x84827c,0x1c1a1c,0xd4d2d4,0xc4c6c4,0xcccacc,0xdcdedc,0xe4e2e4,0xf4f2f4,0xf4f2f4,0xf4f6f4,0x9c9a9c,0x141614,0xfcfefc},	{0x948e8c,0x2c2e2c,0xb4b2b4,0x9c9a9c,0x848684,0x8c8a8c,0x8c8a8c,0x8c8a8c,0x7c7e7c,0x9c9a9c,0x747274,0x2c2a2c,0xfcfefc},	{0x9c9e9c,0x141214,0x444244,0x242624,0x1c1e1c,0x2c2a2c,0x2c2a2c,0x242224,0x242624,0x1c1a1c,0x2c2a2c,0x343634,0xe4e6e4},	{0xc4c2c4,0xfcfefc,0xfcfefc,0xf4f2f4,0xf4f6f4,0xfcfefc,0xfcfefc,0xf4f6f4,0xfcfefc,0xfcfefc,0xfcfefc,0xeceaec,0xfcfefc},};/** 最小化ボタン */static unsigned int minimizeIcon[13][13] = {	{0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0xcccecc},	{0x8c8e8c,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0xfcfefc},	{0x8c8e8c,0x242224,0xfcfefc,0xcccecc,0xcccecc,0xcccecc,0xcccecc,0xcccecc,0xcccecc,0xcccecc,0xcccecc,0x242224,0xfcfefc},	{0x8c8e8c,0x242224,0xcccecc,0x9c9e9c,0x9c9e9c,0xacaeac,0xacaeac,0xbcbebc,0xbcbebc,0xcccecc,0x8c8e8c,0x242224,0xfcfefc},	{0x8c8e8c,0x242224,0xbcbebc,0x9c9e9c,0xacaeac,0xacaeac,0xbcbebc,0xbcbebc,0xcccecc,0xcccecc,0x8c8e8c,0x242224,0xfcfefc},	{0x8c8e8c,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0xfcfefc},	{0x8c8e8c,0x242224,0xcccecc,0xacaeac,0xbcbebc,0xbcbebc,0xcccecc,0xcccecc,0xdcdedc,0xdcdedc,0x8c8e8c,0x242224,0xfcfefc},	{0x8c8e8c,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0xfcfefc},	{0x8c8e8c,0x242224,0xcccecc,0xbcbebc,0xcccecc,0xcccecc,0xdcdedc,0xdcdedc,0xeceeec,0xeceeec,0x8c8e8c,0x242224,0xfcfefc},	{0x8c8e8c,0x242224,0xcccecc,0xcccecc,0xcccecc,0xdcdedc,0xdcdedc,0xeceeec,0xeceeec,0xfcfefc,0x8c8e8c,0x242224,0xfcfefc},	{0x8c8e8c,0x242224,0xcccecc,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x8c8e8c,0x242224,0xfcfefc},	{0x8c8e8c,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0x242224,0xfcfefc},	{0xcccecc,0xfcfefc,0xfcfefc,0xfcfefc,0xfcfefc,0xfcfefc,0xfcfefc,0xfcfefc,0xfcfefc,0xfcfefc,0xfcfefc,0xfcfefc,0xfcfefc},};/** コンストラクタ */Window::Window(){	copyString(title, "window");	// スレッドIDを得る	threadID = MonAPI::System::getThreadID();	// GUIサーバーを探す	guisvrID = MonAPI::Message::lookupMainThread(GUISERVER_NAME);	if (guisvrID == 0xFFFFFFFF) {		//printf("Window: GuiServer not found %d\n", threadID);	} else {		//printf("Window: GuiServer found %d\n", threadID);	}	// ウィンドウ内部描画領域	__g = new Graphics();		// キーイベント	_keyEvent = new KeyEvent(KEY_PRESSED, this, 0, 0);		// マウスイベント	_mouseEvent = new MouseEvent(MOUSE_PRESSED, this, 0, 0);		// タイマーイベント	_timerEvent = new Event(TIMER, this);	// フォントロード	FontManager::getInstance();	// タイマー起動	if (timerID == THREAD_UNKNOWN) timerID = syscall_get_tid();	MessageInfo msg, src;	dword id = syscall_mthread_create((dword)TimerThread);	syscall_mthread_join(id);	src.header = MSG_SERVER_START_OK;	MonAPI::Message::receive(&msg, &src, MonAPI::Message::equalsHeader);	timerID = msg.from;}/** デストラクタ */Window::~Window() {	delete(__g);	delete(_keyEvent);	delete(_mouseEvent);	delete(_timerEvent);}/** タイトルを得る */char *Window::getTitle(){	return title;}/** タイトル設定 @param title タイトル */void Window::setTitle(char *title){	copyString(this->title, title);}/** 大きさを設定する. @param x x座標 @param y y座標 @param width 幅 @param height 高さ*/void Window::setRect(int x, int y, int width, int height){	int i;	int mx = x - this->x; // 移動量	int my = y - this->y; // 移動量		this->x = x;	this->y = y;	this->width = width;	this->height = height;	_g->translate(x, y);	_g->setClip(x, y, width, height);	// ウィンドウ内部描画領域領域	__g->translate(x + INSETS_LEFT, y + INSETS_TOP);	__g->setClip(x + INSETS_LEFT, y + INSETS_TOP, 		width - INSETS_LEFT - INSETS_RIGHT, height - INSETS_TOP - INSETS_BOTTOM);	// 子部品の位置も更新	for(i = 0; i < _controlList->getLength(); i++) {		Control *control = (Control *)_controlList->getItem(i)->data;		Rect *rect = control->getRect();		control->setRect(			rect->x + mx, 			rect->y + my, 			rect->width, 			rect->height		);	}}/** タイマーをセットする @param duration 発動するまでの時間[ms] */void Window::setTimer(int duration){	// 非活性のときはタイマーを発生させない	if (focused == false || enabled == false || iconified == true) return;		// タイマー設定メッセージを投げる	if (MonAPI::Message::send(timerID, MSG_GUISERVER_SETTIMER, threadID, duration, 0, NULL)) {		//printf("Window->Window: MSG_GUISERVER_SETTIMER failed %d\n", threadID);	} else {		//printf("Window->Window: MSG_GUISERVER_SETTIMER sended %d\n", threadID);	}}/** ウィンドウの背景を復元する <ul> <li>壁紙があるときは壁紙を描画し、なおときは背景色で塗りつぶす <li>Window::repaint() をオーバーライドした矩形ウィンドウでないとおそらく使い道なし </ul> */void Window::restoreBackGround(){	// 背景復元メッセージを投げる	MessageInfo info;	if (MonAPI::Message::sendReceive(&info, guisvrID, MSG_GUISERVER_RESTORE, threadID, 0, 0, NULL)) {		//printf("Control->WindowManager: MSG_GUISERVER_RESTORE failed %d\n", threadID);	} else {		//printf("Control->WindowManager: MSG_GUISERVER_RESTORE sended %d\n", threadID);	}}/** 指定した部品を追加する @param control 指定する部品 */void Window::add(Control *control){	// 最後に追加する	control->setParent(this);	Rect *rect = control->getRect();	// 部品をコンテナーの中に配置する	control->setRect		(x + INSETS_LEFT + rect->x, y + INSETS_TOP + rect->y, rect->width, rect->height);	// 部品を再描画	if (firstpaint == true) {		control->repaint();	}	_controlList->add(new LinkedItem(control));}/** 指定した部品を削除する @param control 指定する部品 @return 削除された部品(なければNULL) */void Window::remove(Control *control){	for(int i = 0; i < _controlList->getLength(); i++) {		LinkedItem *item = _controlList->getItem(i);		if (item->data == control) {			_controlList->remove(item);		}	}}/** イベント処理 */void Window::postEvent(Event *event){	// 非活性の時はイベントを受け付けない	if (enabled == false) return;	// 活性部品にキーイベントを投げる	if (KEY_PRESSED <= event->type && event->type <= KEY_RELEASED) {		Control *control = findChild();		// 部品でイベントが起こった		if (control != NULL) {			event->source = control;			control->postEvent(event);		// 部品以外でイベントが起こった		} else {			onEvent(event);		}	// マウスクリック	} else if (event->type == MOUSE_PRESSED) {		// マウスイベントが起こった部品を探す		int ex = ((MouseEvent *)event)->x;		int ey = ((MouseEvent *)event)->y;		Control *control = findChild(ex, ey);		// 部品でイベントが起こった		if (control != NULL) {			// イベントが起こった部品以外をフォーカスアウト状態にする			LinkedItem *item = _controlList->firstItem;			// NULLチェック			if (item != NULL) {				Control *c = (Control *)item->data;				if (c == control) {					c->setFocused(true);				} else {					c->setFocused(false);				}
				while (item->next != NULL) {
					item = item->next;					c = (Control *)item->data;
					if (c == control) {						c->setFocused(true);					} else {						c->setFocused(false);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -