Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.
| Následující verze | Předchozí verze | ||
| pitel:itu:cviceni3 [03. 07. 2012, 11.53:41] – upraveno mimo DokuWiki 127.0.0.1 | pitel:itu:cviceni3 [30. 12. 2022, 13.43:01] (aktuální) – upraveno mimo DokuWiki 127.0.0.1 | ||
|---|---|---|---|
| Řádek 1: | Řádek 1: | ||
| + | ====== X-Window (Motif) ====== | ||
| + | [[http:// | ||
| + | <file c motif.c> | ||
| + | // | ||
| + | /* | ||
| + | | ||
| + | | ||
| + | */ | ||
| + | // | ||
| + | #define AUTHOR_NAME " | ||
| + | // | ||
| + | // | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | /* | ||
| + | * Public include files for widgets used in this file. | ||
| + | */ | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // | ||
| + | |||
| + | Widget | ||
| + | mainWin, | ||
| + | frame, | ||
| + | menu_bar, | ||
| + | messagebox, | ||
| + | drawarea, | ||
| + | pane, | ||
| + | row, | ||
| + | text, | ||
| + | mainmenub[5], | ||
| + | menu, | ||
| + | toggle1, | ||
| + | toggle2; | ||
| + | |||
| + | Pixel | ||
| + | int | ||
| + | int | ||
| + | |||
| + | static char *lines; | ||
| + | static int nlines; | ||
| + | static int ncols; | ||
| + | |||
| + | // | ||
| + | // prototypes | ||
| + | |||
| + | void AllocColors(); | ||
| + | void read_file(const char *name); | ||
| + | extern ssize_t read(int, | ||
| + | extern int close(int); | ||
| + | |||
| + | // | ||
| + | //Callbacks | ||
| + | |||
| + | void messageboxCB(Widget w, XtPointer client_data, | ||
| + | { | ||
| + | XtUnmanageChild(messagebox); | ||
| + | } | ||
| + | |||
| + | // | ||
| + | |||
| + | void textCB(Widget w, XtPointer client_data, | ||
| + | { | ||
| + | // | ||
| + | XClearArea(XtDisplay(drawarea), | ||
| + | } | ||
| + | |||
| + | // | ||
| + | |||
| + | void menuCB(Widget w, XtPointer client_data, | ||
| + | { | ||
| + | // | ||
| + | XmString str; | ||
| + | XmPushButtonCallbackStruct *cb= (XmPushButtonCallbackStruct *)call_data; | ||
| + | | ||
| + | cb-> | ||
| + | switch((int)client_data) | ||
| + | { | ||
| + | case 0://quit button | ||
| + | exit(1); | ||
| + | break; | ||
| + | case 1://first button | ||
| + | used_menu=0; | ||
| + | XmMenuPosition(menu, | ||
| + | XtManageChild(menu); | ||
| + | break; | ||
| + | case 2://second button | ||
| + | used_menu=1; | ||
| + | XmMenuPosition(menu, | ||
| + | XtManageChild(menu); | ||
| + | break; | ||
| + | case 3://about button | ||
| + | |||
| + | str = XmStringCreateLocalized(" | ||
| + | XtVaSetValues(messagebox, | ||
| + | XmNmessageAlignment, | ||
| + | XmStringFree(str); | ||
| + | |||
| + | str = XmStringCreateLocalized(" | ||
| + | XtVaSetValues(messagebox, | ||
| + | XmStringFree(str); | ||
| + | |||
| + | str = XmStringCreateLocalized(" | ||
| + | XtVaSetValues(messagebox, | ||
| + | XmStringFree(str); | ||
| + | |||
| + | XtManageChild(messagebox); | ||
| + | break; | ||
| + | |||
| + | default: | ||
| + | break; | ||
| + | |||
| + | } | ||
| + | } | ||
| + | |||
| + | // | ||
| + | |||
| + | void menu_toggle1CB(Widget w, XtPointer client_data, | ||
| + | { | ||
| + | unsigned char ro_rw; | ||
| + | XtVaGetValues(w, | ||
| + | if(ro_rw==XmUNSET) | ||
| + | |||
| + | XtVaSetValues(text, | ||
| + | else | ||
| + | XtVaSetValues(text, | ||
| + | // | ||
| + | } | ||
| + | |||
| + | // | ||
| + | |||
| + | void menu_toggle2CB(Widget w, XtPointer client_data, | ||
| + | { | ||
| + | unsigned char progress; | ||
| + | XtVaGetValues(w, | ||
| + | if(progress==XmUNSET) { | ||
| + | // | ||
| + | draw_area_usage = 0; | ||
| + | } else { | ||
| + | // | ||
| + | draw_area_usage = 1; | ||
| + | } | ||
| + | XClearArea(XtDisplay(drawarea), | ||
| + | // | ||
| + | } | ||
| + | |||
| + | // | ||
| + | |||
| + | void MenuX_ButtonClickCB(Widget w, XtPointer client_data, | ||
| + | { | ||
| + | // | ||
| + | if(used_menu==0) | ||
| + | XtVaSetValues(mainmenub[1], | ||
| + | else | ||
| + | XtVaSetValues(mainmenub[2], | ||
| + | // | ||
| + | XClearArea(XtDisplay(drawarea), | ||
| + | } | ||
| + | |||
| + | // | ||
| + | |||
| + | void da_exposeCB(Widget w, XtPointer client_data, | ||
| + | { | ||
| + | XmTextPosition textcount = XmTextGetLastPosition(text); | ||
| + | XmTextPosition textposition = XmTextGetCursorPosition(text); | ||
| + | Pixel color1; | ||
| + | Pixel color2; | ||
| + | // | ||
| + | // | ||
| + | |||
| + | Arg al[10]; | ||
| + | int ac; | ||
| + | Dimension width, | ||
| + | ac=0; | ||
| + | XtSetArg(al[ac], | ||
| + | XtSetArg(al[ac], | ||
| + | XtGetValues(w, | ||
| + | XtVaGetValues(mainmenub[1], | ||
| + | XtVaGetValues(mainmenub[2], | ||
| + | | ||
| + | XClearWindow(XtDisplay(w), | ||
| + | | ||
| + | // drawing primitives | ||
| + | |||
| + | if(draw_area_usage==0) | ||
| + | { | ||
| + | XGCValues v; | ||
| + | GC drawGC; | ||
| + | v.foreground = color1; | ||
| + | v.background = color2; | ||
| + | v.line_width = 1; | ||
| + | drawGC = XCreateGC(XtDisplay(w), | ||
| + | GCForeground | GCLineWidth , &v); | ||
| + | XDrawLine(XtDisplay(w), | ||
| + | 5, 5, | ||
| + | 5, height-5); | ||
| + | XDrawLine(XtDisplay(w), | ||
| + | 5, height-5, | ||
| + | width-5, | ||
| + | XDrawLine(XtDisplay(w), | ||
| + | width-5, | ||
| + | width-5, | ||
| + | XFreeGC(XtDisplay(w), | ||
| + | |||
| + | v.foreground = color2; | ||
| + | v.background = color1; | ||
| + | v.line_width = 3; | ||
| + | drawGC = XCreateGC(XtDisplay(w), | ||
| + | GCForeground | GCLineWidth , &v); | ||
| + | XDrawLine(XtDisplay(w), | ||
| + | 15, 15, | ||
| + | 15, height-15); | ||
| + | XDrawLine(XtDisplay(w), | ||
| + | 15, height-15, | ||
| + | width/ | ||
| + | XFreeGC(XtDisplay(w), | ||
| + | |||
| + | v.foreground = color1; | ||
| + | v.background = color2; | ||
| + | v.line_width = 1; | ||
| + | drawGC = XCreateGC(XtDisplay(w), | ||
| + | GCForeground | GCLineWidth , &v); | ||
| + | XFillRectangle(XtDisplay(w), | ||
| + | 30, 10, | ||
| + | 50, height-50); | ||
| + | XFreeGC(XtDisplay(w), | ||
| + | |||
| + | v.foreground = color2; | ||
| + | v.background = color1; | ||
| + | v.line_width = 3; | ||
| + | drawGC = XCreateGC(XtDisplay(w), | ||
| + | GCForeground | GCLineWidth , &v); | ||
| + | XDrawRectangle(XtDisplay(w), | ||
| + | 30, 10, | ||
| + | 50, height-50); | ||
| + | XFreeGC(XtDisplay(w), | ||
| + | |||
| + | v.foreground = color1; | ||
| + | v.background = color2; | ||
| + | v.line_style = LineOnOffDash; | ||
| + | v.line_width = 2; | ||
| + | drawGC = XCreateGC(XtDisplay(w), | ||
| + | GCForeground | GCLineWidth |GCLineStyle , &v); | ||
| + | XDrawRectangle(XtDisplay(w), | ||
| + | 100, 10, | ||
| + | 70, height-50); | ||
| + | XFillArc(XtDisplay(w), | ||
| + | 200, 10, | ||
| + | 70, height-50, | ||
| + | |||
| + | XDrawArc(XtDisplay(w), | ||
| + | 200, 10, | ||
| + | 70, height-50, | ||
| + | | ||
| + | XFreeGC(XtDisplay(w), | ||
| + | |||
| + | v.foreground = color2; | ||
| + | v.background = color1; | ||
| + | v.line_style = LineOnOffDash; | ||
| + | v.line_width = 2; | ||
| + | drawGC = XCreateGC(XtDisplay(w), | ||
| + | GCForeground | GCLineWidth |GCLineStyle , &v); | ||
| + | XFillArc(XtDisplay(w), | ||
| + | 300, 10, | ||
| + | 70, height-50, | ||
| + | XDrawArc(XtDisplay(w), | ||
| + | 300, 10, | ||
| + | 70, height-50, | ||
| + | XFreeGC(XtDisplay(w), | ||
| + | |||
| + | v.foreground = cl_black; | ||
| + | v.background = color1; | ||
| + | drawGC = XCreateGC(XtDisplay(w), | ||
| + | GCForeground | GCBackground , &v); | ||
| + | XDrawString(XtDisplay(w), | ||
| + | "ITU demo", | ||
| + | XFreeGC(XtDisplay(w), | ||
| + | v.foreground = color1; | ||
| + | v.background = color2; | ||
| + | drawGC = XCreateGC(XtDisplay(w), | ||
| + | GCForeground | GCBackground , &v); | ||
| + | XDrawString(XtDisplay(w), | ||
| + | AUTHOR_NAME, | ||
| + | char buffer[128]=""; | ||
| + | sprintf(buffer," | ||
| + | XDrawString(XtDisplay(w), | ||
| + | buffer, | ||
| + | XFreeGC(XtDisplay(w), | ||
| + | | ||
| + | |||
| + | } else { | ||
| + | XGCValues v; | ||
| + | GC drawGC; | ||
| + | double ratio = ((double) textposition / (double) textcount); | ||
| + | | ||
| + | //Pravy obdelnik | ||
| + | v.foreground = color2; | ||
| + | drawGC = XCreateGC(XtDisplay(w), | ||
| + | XFillRectangle(XtDisplay(w), | ||
| + | | ||
| + | //Levy obdelnik | ||
| + | v.foreground = color1; | ||
| + | drawGC = XCreateGC(XtDisplay(w), | ||
| + | XFillRectangle(XtDisplay(w), | ||
| + | | ||
| + | //Text | ||
| + | v.foreground = cl_black; | ||
| + | drawGC = XCreateGC(XtDisplay(w), | ||
| + | char buffer[16]=""; | ||
| + | sprintf(buffer, | ||
| + | XDrawString(XtDisplay(w), | ||
| + | | ||
| + | //Obrys | ||
| + | v.foreground = cl_black; | ||
| + | v.line_width = 1; | ||
| + | drawGC = XCreateGC(XtDisplay(w), | ||
| + | XDrawRectangle(XtDisplay(w), | ||
| + | | ||
| + | XFreeGC(XtDisplay(w), | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // | ||
| + | |||
| + | void da_inputCB(Widget w, XtPointer client_data, | ||
| + | { | ||
| + | // | ||
| + | } | ||
| + | |||
| + | // | ||
| + | |||
| + | void da_resizeCB(Widget w, XtPointer client_data, | ||
| + | { | ||
| + | // | ||
| + | // | ||
| + | } | ||
| + | |||
| + | // | ||
| + | |||
| + | int main(int argc, char **argv) | ||
| + | { | ||
| + | XtAppContext app_context; | ||
| + | Arg al[10]; | ||
| + | int ac=0; | ||
| + | Widget tmpBtn; | ||
| + | |||
| + | /* | ||
| + | * Register the default language procedure | ||
| + | */ | ||
| + | XtSetLanguageProc(NULL, | ||
| + | |||
| + | /* | ||
| + | application init | ||
| + | */ | ||
| + | topLevel = XtVaAppInitialize( | ||
| + | & | ||
| + | "ITU demonstracni aplikace", | ||
| + | NULL, 0, /* command line option list */ | ||
| + | &argc, argv, /* command line args */ | ||
| + | NULL, /* for missing app-defaults file */ | ||
| + | NULL); | ||
| + | |||
| + | messagebox = XmCreateQuestionDialog(topLevel, | ||
| + | XtUnmanageChild(XmMessageBoxGetChild(messagebox, | ||
| + | XtUnmanageChild(XmMessageBoxGetChild(messagebox, | ||
| + | XtAddCallback(messagebox, | ||
| + | XtAddCallback(messagebox, | ||
| + | |||
| + | AllocColors(); | ||
| + | read_file(" | ||
| + | |||
| + | mainWin = XtVaCreateManagedWidget( | ||
| + | " | ||
| + | |||
| + | xmMainWindowWidgetClass, | ||
| + | topLevel, | ||
| + | XmNcommandWindowLocation, | ||
| + | NULL); | ||
| + | |||
| + | menu_bar = XmCreateMenuBar(mainWin, | ||
| + | XtVaSetValues(menu_bar, | ||
| + | XmNheight, | ||
| + | // | ||
| + | // | ||
| + | // | ||
| + | // | ||
| + | XmNspacing, | ||
| + | NULL); | ||
| + | |||
| + | XtManageChild(menu_bar); | ||
| + | | ||
| + | mainmenub[0] = XtVaCreateManagedWidget(" | ||
| + | xmCascadeButtonWidgetClass, | ||
| + | XmNborderColor, | ||
| + | XmNborderWidth, | ||
| + | NULL); | ||
| + | XtAddCallback(mainmenub[0], | ||
| + | |||
| + | mainmenub[1] = XtVaCreateManagedWidget(" | ||
| + | xmCascadeButtonWidgetClass, | ||
| + | XmNborderColor, | ||
| + | XmNborderWidth, | ||
| + | XmNmarginWidth, | ||
| + | NULL); | ||
| + | XtAddCallback(mainmenub[1], | ||
| + | |||
| + | mainmenub[2] = XtVaCreateManagedWidget(" | ||
| + | xmCascadeButtonWidgetClass, | ||
| + | XmNborderColor, | ||
| + | XmNborderWidth, | ||
| + | NULL); | ||
| + | XtAddCallback(mainmenub[2], | ||
| + | |||
| + | mainmenub[3] = XtVaCreateManagedWidget(" | ||
| + | xmCascadeButtonWidgetClass, | ||
| + | XmNborderColor, | ||
| + | XmNborderWidth, | ||
| + | NULL); | ||
| + | XtAddCallback(mainmenub[3], | ||
| + | |||
| + | //create toggle buttons | ||
| + | | ||
| + | toggle1 = XtCreateManagedWidget(" | ||
| + | XtAddCallback(toggle1, | ||
| + | toggle2 = XtCreateManagedWidget(" | ||
| + | XtAddCallback(toggle2, | ||
| + | | ||
| + | |||
| + | // | ||
| + | //menu creation | ||
| + | menu=XmCreatePopupMenu(mainWin," | ||
| + | tmpBtn = XtVaCreateManagedWidget( | ||
| + | " | ||
| + | xmPushButtonWidgetClass, | ||
| + | menu, | ||
| + | XmNbackground, | ||
| + | NULL); | ||
| + | |||
| + | XtAddCallback(tmpBtn, | ||
| + | tmpBtn = XtVaCreateManagedWidget( | ||
| + | " | ||
| + | xmPushButtonWidgetClass, | ||
| + | menu, | ||
| + | XmNbackground, | ||
| + | XmNforeground, | ||
| + | NULL); | ||
| + | |||
| + | XtAddCallback(tmpBtn, | ||
| + | tmpBtn = XtVaCreateManagedWidget( | ||
| + | " | ||
| + | xmPushButtonWidgetClass, | ||
| + | menu, | ||
| + | XmNbackground, | ||
| + | NULL); | ||
| + | |||
| + | XtAddCallback(tmpBtn, | ||
| + | tmpBtn = XtVaCreateManagedWidget( | ||
| + | " | ||
| + | xmPushButtonWidgetClass, | ||
| + | menu, | ||
| + | XmNbackground, | ||
| + | NULL); | ||
| + | XtAddCallback(tmpBtn, | ||
| + | |||
| + | |||
| + | pane = XtVaCreateManagedWidget(" | ||
| + | xmPanedWindowWidgetClass, | ||
| + | NULL); | ||
| + | /* | ||
| + | create text editor | ||
| + | */ | ||
| + | text = XmCreateScrolledText(pane, | ||
| + | XtVaSetValues(text, | ||
| + | XmNeditMode, | ||
| + | XmNcolumns, ncols, | ||
| + | XmNrows, 1, | ||
| + | XmNwordWrap, | ||
| + | XmNmaxLength, | ||
| + | XmNvalue, lines, | ||
| + | //XmNvalue, " | ||
| + | XmNheight, 200, | ||
| + | XmNwidth, 300, | ||
| + | XmNpaneMinimum, | ||
| + | NULL); | ||
| + | XtManageChild(text); | ||
| + | |||
| + | /* cursor position callback registration (for text widget)*/ | ||
| + | XtAddCallback(text, | ||
| + | |||
| + | /* | ||
| + | DrawArea init | ||
| + | */ | ||
| + | drawarea = XtVaCreateManagedWidget( | ||
| + | " | ||
| + | xmDrawingAreaWidgetClass, | ||
| + | pane, | ||
| + | XmNwidth, 200, | ||
| + | XmNheight, 200, | ||
| + | XmNpaneMinimum, | ||
| + | NULL); | ||
| + | |||
| + | XtAddCallback(drawarea, | ||
| + | XtAddCallback(drawarea, | ||
| + | XtAddCallback(drawarea, | ||
| + | | ||
| + | /* | ||
| + | Place widgets in mainWin | ||
| + | */ | ||
| + | XmMainWindowSetAreas(mainWin, | ||
| + | |||
| + | XtRealizeWidget(topLevel); | ||
| + | XtAppMainLoop(app_context); | ||
| + | } | ||
| + | |||
| + | // | ||
| + | |||
| + | void AllocColors() | ||
| + | { | ||
| + | XColor cl; | ||
| + | Colormap cmap; | ||
| + | Display *display = XOpenDisplay("" | ||
| + | cmap = DefaultColormap(display, | ||
| + | cl.red = 0x0; | ||
| + | if(XAllocColor(display, | ||
| + | fprintf(stderr," | ||
| + | cl_black = cl.pixel; | ||
| + | cl.red = 65535; | ||
| + | if(XAllocColor(display, | ||
| + | fprintf(stderr," | ||
| + | cl_orange = cl.pixel; | ||
| + | cl.red = 0x00; | ||
| + | if(XAllocColor(display, | ||
| + | fprintf(stderr," | ||
| + | cl_green = cl.pixel; | ||
| + | cl.red = 65535; | ||
| + | if(XAllocColor(display, | ||
| + | fprintf(stderr," | ||
| + | cl_white = cl.pixel; | ||
| + | cl.red = 65535; | ||
| + | if(XAllocColor(display, | ||
| + | fprintf(stderr," | ||
| + | cl_red = cl.pixel; | ||
| + | } | ||
| + | |||
| + | // | ||
| + | |||
| + | void read_file(const char *name) | ||
| + | { | ||
| + | int fd; | ||
| + | struct stat st; | ||
| + | char *p,*linep; | ||
| + | |||
| + | if ((fd = open(name, O_RDONLY)) == -1 || | ||
| + | | ||
| + | fprintf(stderr, | ||
| + | exit(1); | ||
| + | } | ||
| + | lines = XtMalloc(st.st_size+1); | ||
| + | if (read(fd, lines, st.st_size) != st.st_size) { | ||
| + | fprintf(stderr, | ||
| + | exit(1); | ||
| + | } | ||
| + | close(fd); | ||
| + | p = lines; | ||
| + | while (p < lines + st.st_size) { | ||
| + | linep = p; | ||
| + | if ((p = strchr(p, ' | ||
| + | if (p-linep > ncols) ncols = p-linep; | ||
| + | ++p; | ||
| + | ++nlines; | ||
| + | } | ||
| + | lines[st.st_size] = 0; | ||
| + | } | ||
| + | </ | ||