Skip to content

Commit

Permalink
First commit!
Browse files Browse the repository at this point in the history
  • Loading branch information
tongbin01 committed Apr 15, 2018
0 parents commit 170b1c5
Show file tree
Hide file tree
Showing 13 changed files with 829 additions and 0 deletions.
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# CtrlUI

### 说明
一个很久前开发的`windows`控制台控制UI,包含编辑框,选择框等控件,重新整理出来纪念一下


### Examples
---------------

##### 对话框

```c++
#include"ctrl/msgbox.h"

int main()
{
MsgBox mb("对话框测试\n(左右光标移动)", 40-14/2, 4);
Application app((Ctrl*)(&mb));
app.start();
return 0;
}
```

##### 编辑框

```c++
#include"ctrl/edit.h"

int main()
{
Edit e("选择", 0, 0, -1, (Ctrl*)(&vlay));
Application app((Ctrl*)(&e));
app.start();
return 0;
}
```

##### 组合使用

```c++
#include"ctrl/edit.h"
#include"ctrl/layout.h"

int main()
{
HLayout hlay;
VLayout vlay(&hlay);
Edit e1("测试1", 2, 2, -1, (Ctrl*)(&vlay));
Edit e2("测试2", 2, 4, 10, (Ctrl*)(&vlay));
Edit e3("测试3", 20, 4, -1, (Ctrl*)(&hlay));

Application app((Ctrl*)(&hlay));
app.start();
return 0;
}
```

##### 事件响应

```c++
#include"ctrl/radio.h"
#include"ctrl/msgbox.h"

class Menu: public Radio {
public:
Menu(const char *title, int x, int y, bool vertical=false, Ctrl *parent=NULL):
Radio(title, x, y, vertical, parent){}

virtual int Enter() {
std::string title="确定选择: ";
title+=this->text();
MsgBox message(title.c_str(), 40-14/2, 4);
int ret = message.execute();
if(ret!=-1) {
system("cls");
this->setCursor(40-14/2, 4);
if(ret==0) {
printf("选择: %s", this->text());
} else {
printf("未选择!");
}
return -1;
}
return 1;
}
};

int main()
{
Menu menu("请选择:", 40-14/2, 4, 1);
menu.addItem("abcde");
menu.addItem("12345");
menu.addItem("!@#$%");
menu.addItem("+-*/=");
Application app((Ctrl*)(&menu));
app.start();
return 0;
}
```

Copyright © 2017 [[email protected]](mailto:[email protected])
57 changes: 57 additions & 0 deletions ctrl/Edit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "Edit.h"


Edit::Edit(const char *title, int x, int y, int max, Ctrl *parent): Ctrl(parent) {
this->title = title;
this->pos.X = x;
this->pos.Y = y;
this->max = max;
}

void Edit::toShow(int pFocus) {
if(!(pFocus && this->focus)) {
printf("%s:%s", title.c_str(), text.c_str());
} else {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0080);
printf("%s", title.c_str());
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0007);
printf(":%s", text.c_str());
}
}

int Edit::CharEvent(char key) {
static char last_key = 0;
if(this->max>0 && this->text.length()>=this->max) {
return 1;;
}

if(key<0 && !last_key) {
last_key=key;
} else {
if (last_key) {
this->text+=last_key;
last_key=0;
}
this->text+=key;
this->show();
}
return 1;
}

int Edit::BackSpace() {
static int times=1;
if (text.length()==0) return 1;

char c = text[text.length()-1];
text[text.length()-1] = ' ';
this->show();
this->setCursor(this->cur.X-1, this->cur.Y);
text.erase(text.end()-1);

if(c < 0 && times==1) {
times=2;
this->BackSpace();
times=1;
}
return 1;
}
19 changes: 19 additions & 0 deletions ctrl/Edit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __EDIT_H__
#define __EDIT_H__

#include"ctrl.h"

class Edit: public Ctrl {
protected:
int max;
std::string title;
std::string text;

public:
Edit(const char *title, int x, int y, int max=-1, Ctrl *parent=NULL);
virtual void toShow(int pFocus);
virtual int CharEvent(char key);
virtual int BackSpace();
};

#endif
55 changes: 55 additions & 0 deletions ctrl/Layout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "Layout.h"


Layout::Layout(Ctrl *p): Ctrl(p) {
this->index=0;
}

void Layout::next() {
if(!this->children.size()) return;

int old = this->index;
this->index--;
if(this->index<0){
this->index = this->children.size()-1;
}
this->children[this->index]->setFocus(this->children[old]);
this->show();
}

void Layout::prev() {
if(!this->children.size()) return;

int old = this->index;
this->index++;
if(this->index>=this->children.size()){
this->index = 0;
}
this->children[this->index]->setFocus(this->children[old]);
this->show();
}

HLayout::HLayout(Ctrl *p): Layout(p) {}

int HLayout::Left() {
this->prev();
return 1;
}

int HLayout::Right() {
this->next();
return 1;
}

VLayout::VLayout(Ctrl *p): Layout(p) {}

int VLayout::Up() {
this->prev();
return 1;
}

int VLayout::Down() {
this->next();
return 1;
}

32 changes: 32 additions & 0 deletions ctrl/Layout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef __LAYOUT_H__
#define __LAYOUT_H__

#include "ctrl.h"

class Layout: public Ctrl {
protected:
int index;

public:
Layout(Ctrl *p=NULL);
void next();
void prev();
};


class HLayout: public Layout {
public:
HLayout(Ctrl *p=NULL);
virtual int Left();
virtual int Right();
};


class VLayout: public Layout {
public:
VLayout(Ctrl *p=NULL);
virtual int Up();
virtual int Down();
};

#endif
91 changes: 91 additions & 0 deletions ctrl/MsgBox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "MsgBox.h"


void MsgBox::getNextCursor(int index, short &x, short &y) {
int i, pos=0, length=0, size=0;
for(i=0;i<this->title.length();i++) {
if(this->title[i] == '\n') {
if(i-pos > length) length=i-pos;
pos=i+1;
}
}
length = i-pos>length?i-pos:length;

for(i=0;i<this->items.size();i++) size+=strlen(this->items[i]);
size += (this->items.size()-1)*4;
int start = this->pos.X+length/2-size/2<0?0:this->pos.X+length/2-size/2;

this->getCursor(x, y);
x=start;

if(index == 0) {
y++;
return;
}
for(i=0;i<index;i++) {
x+=4+strlen(this->items[i]);
}
}

MsgBox::MsgBox(const char *title, int x, int y, const char *items[], int size, Ctrl *parent): Select(title, x, y, items, size, parent) {
this->title = title;
this->pos.X = x;
this->pos.Y = y;
this->select = 0;
for(int i=0;i<size;i++) this->items.push_back(items[i]);
}

MsgBox::MsgBox(const char *title, int x, int y, Ctrl *parent): Select(title, x, y, NULL, 0, parent) {
this->title = title;
this->pos.X = x;
this->pos.Y = y;
this->select = 0;
const char *items[]={"OK", "Cancel"};
for(int i=0;i<2;i++) this->items.push_back(items[i]);
}

int MsgBox::execute() {
Application *oApp = this->app;

system("cls");
Application app((Ctrl*)(this));
app.start();
this->setApp(oApp);

system("cls");
this->app->main->show();
return this->select;
}

int MsgBox::Esc() {
this->select = -1;
return -1;
}

int MsgBox::Enter() {
return -1;
}

int MsgBox::Left() {
this->prev();
return 0;
}

int MsgBox::Right() {
this->next();
return 0;
}

void MsgBox::printTitle() {
int line = 0;
const char *title=this->title.c_str();
for(const char *ch=title;*ch;ch++)
{
if(*ch != '\n') {
putchar(*ch);
} else {
this->setCursor(this->pos.X, this->pos.Y+(++line));
}
}
putchar('\n');
}
21 changes: 21 additions & 0 deletions ctrl/MsgBox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef __MSGBOX_H__
#define __MSGBOX_H__

#include"ctrl.h"

class MsgBox: public Select {
protected:
virtual void getNextCursor(int index, short &x, short &y);
virtual void printTitle();

public:
MsgBox(const char *title, int x, int y, const char *items[], int size, Ctrl *parent=NULL);
MsgBox(const char *title, int x, int y, Ctrl *parent=NULL);
virtual int execute();
virtual int Enter();
virtual int Esc();
virtual int Left();
virtual int Right();
};

#endif
Loading

0 comments on commit 170b1c5

Please sign in to comment.