📄 ccc_x11.cpp
字号:
/***************************************************************************/
int GraphicWindow::put_string(const char instr[], int str_x, int curr_x)
{
int direction, ascent, descent;
XCharStruct overall;
XTextExtents(_fontinfo_ptr, instr, strlen(instr), &direction, &ascent, &descent, &overall);
XClearArea(display, win,
str_x - 1,
0, /*clear from top*/
curr_x + 1, /*clear to right edge of screen*/
ascent + descent + 1,
0 /* generate exposure events */);
XSetForeground(display, xgc, BlackPixel(display, screen_num));
XDrawImageString(display, win, xgc, str_x, ascent, instr, strlen(instr));
curr_x = str_x + overall.width;
/* redraw cursor */
XDrawLine(display, win, xgc, curr_x, 0, curr_x, ascent + descent);
return curr_x;
}
/***************************************************************************/
string GraphicWindow::get_string(string outstr)
{
XEvent report;
string instring;
char instr[50];
char buffer[3];
int buffsize = 3;
KeySym keysym;
XComposeStatus compose;
int count, length;
int str_x; // initial x position, at the end of the prompt string
int str_y; // initial yposition
int curr_x; // current x position (measured from str_x)
int direction; // draw chars l->r
int ascent; // above font's baseline
int descent; // below font's baseline
XCharStruct overall;
_display_string=outstr;
statusline_prompt(outstr);
XTextExtents(_fontinfo_ptr, _display_string.c_str(),
_display_string.length(), &direction, &ascent, &descent, &overall);
str_x = overall.width;
curr_x = overall.width;
str_y = ascent; // set initial cursor position
instr[0] = '\0';
curr_x = put_string(instr, str_x, curr_x);
//Event loop
while(1)
{
XNextEvent(display, &report);
switch (report.type)
{
case Expose:
cwin.repaint();
break;
case KeyPress:
/* get characters until carriage return */
count = XLookupString(&(report.xkey), buffer,
buffsize, &keysym, &compose);
/* got a carriage return */
if ((keysym == XK_Return) || (keysym == XK_KP_Enter)
|| (keysym == XK_Linefeed))
{
XClearArea(display, win,
0,
0, /*clear from top*/
0, /*clear to right edge of screen*/
ascent + descent + 1,
0 /* generate expoure events */);
instring = instr;
return instring;
}
/* eat white space */
else if (((keysym >= XK_KP_Space) && (keysym <= XK_KP_9))
|| ((keysym >= XK_space) && (keysym <= XK_asciitilde)))
{
if ((strlen(instr) + strlen(buffer)) >= 50)
XBell(display, 100);
else
{
buffer[1] = '\0';
strcat (instr, buffer);
}
}
/* got a delete key */
else if ((keysym == XK_BackSpace) || (keysym == XK_Delete))
{
if ((length = strlen(instr)) > 0)
{
// resize the string
instr[length-1] = '\0';
}
else
XBell(display, 100);
}
/* display the new string, and reposition cursor */
curr_x = put_string(instr, str_x, curr_x);
}
}
return instring; // never get here but it keeps compiler happy :)!
}
int GraphicWindow::get_int(const string& out_string)
{
return atoi(get_string(out_string).c_str());
}
double GraphicWindow::get_double(const string& out_string)
{
return atof(get_string(out_string).c_str());
}
#define BITMAPDEPTH 1
#define TOO_SMALL 0
#define BIG_ENOUGH 1
/***************************************************************************/
int main(int argc, char** argv)
{
// The usual X overhead...
unsigned int width, height; // window size
unsigned int border_width = 4;
char* window_name = argv[0];
char* icon_name = "GCT";
Pixmap icon_pixmap;
XSizeHints* size_hints;
XWMHints* wm_hints;
XClassHint* class_hints;
XTextProperty windowName;
XTextProperty iconName;
XEvent report;
int window_size_ok = 1;
char* display_name = 0;
char* progname = argv[0];
Window win;
Display* display;
int screen_num;
// check allocation of hints
if (!(size_hints = XAllocSizeHints())
|| !(wm_hints = XAllocWMHints())
|| !(class_hints = XAllocClassHint()))
{
cerr << progname << " error: failure allocating memory" << endl;
exit(-1);
}
progname = argv[0];
// connect to X server
display = XOpenDisplay(display_name);
if (display == NULL)
{
cerr << progname << " error: can't connect to server " <<
XDisplayName(display_name) << endl;
exit(-1);
}
// get screen size from display structure macro
screen_num = DefaultScreen(display);
win = XCreateSimpleWindow(display, RootWindow(display, screen_num),
0, 0, DEF_WIDTH, DEF_HEIGHT, border_width, BlackPixel(display,
screen_num), WhitePixel(display, screen_num));
if (XStringListToTextProperty(&window_name, 1, &windowName) == 0)
{
cerr << progname
<< " error: structure allocation for windowName failed."
<< endl;
exit(-1);
}
if (XStringListToTextProperty(&icon_name, 1, &iconName) == 0)
{
cerr << progname
<< " error: structure allocation for iconName failed."
<< endl;
exit(-1);
}
size_hints->flags = PPosition | PSize | PMinSize;
size_hints->min_width = DEF_WIDTH;
size_hints->min_height = DEF_HEIGHT;
wm_hints->initial_state = NormalState;
wm_hints->input = True;
wm_hints->icon_pixmap = icon_pixmap;
wm_hints->flags = StateHint | IconPixmapHint | InputHint;
class_hints->res_name = progname;
class_hints->res_class = "chigcx";
XSetWMProperties(display, win, &windowName, &iconName, argv, argc,
size_hints, wm_hints, class_hints);
// Select desired event types
XSelectInput(display, win, ExposureMask | KeyPressMask
| ButtonPressMask);
// Highly intuitive way to state that we can process "delete window"
Atom delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", false);
XSetWMProtocols(display, win, &delete_atom, 1);
XMapWindow(display, win);
cwin.open(display, win);
// Event loop
int draw_flag = 1;
while (true)
{
XNextEvent(display, &report);
switch ((int)report.type)
{
case Expose:
if (report.xexpose.count == 0)
{
if (window_size_ok)
{
if (draw_flag)
{
ccc_win_main();
draw_flag = 0;
}
else
cwin.repaint();
}
}
break;
case ConfigureNotify:
/*
window has been resized, change width and
height to send to draw_text and draw_graphics
in next Expose
*/
width = report.xconfigure.width;
height = report.xconfigure.height;
window_size_ok = (width >= size_hints->min_width)
&& (height >= size_hints->min_height);
break;
case ClientMessage:
if (report.xclient.data.l[0] == delete_atom)
{
cwin.close();
exit(0);
}
break;
case ButtonPress:
case KeyPress:
default:
break;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -