diff options
Diffstat (limited to 'help')
-rw-r--r-- | help/sample-gtk.cpp | 175 | ||||
-rw-r--r-- | help/simple-text.c | 173 |
2 files changed, 175 insertions, 173 deletions
diff --git a/help/sample-gtk.cpp b/help/sample-gtk.cpp new file mode 100644 index 0000000..02238ed --- /dev/null +++ b/help/sample-gtk.cpp @@ -0,0 +1,175 @@ +#include "GTK3Test.h" + +/// GTK Widgets +GtkWidget *window; +GtkWidget *windowGrid; +GtkWidget *radioButton1, *radioButton2; +GtkWidget *textLabel; +GtkWidget *textBox; +GtkWidget *buttonSubmit; +GtkWidget *buttonCancel; +GtkWidget *textViewWindow; +GtkWidget *scrolledwindow; + +/// GDK properties +GdkRGBA color; + +void textViewPrint(const char* format, ...) +{ + va_list arglist; + /// initialize valist for format arguments + va_start(arglist, format); + char* str; + // g_print("--in-print--\n"); + // vprintf(format, arglist); + vsprintf(str, format, arglist); + // g_print("text: "); + // g_print(str); + // g_print("\n"); + va_end(arglist); + // g_print("--text-gen-done--\n"); + + GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textViewWindow)); + // g_print("--buffer-created--\n"); + gtk_text_buffer_insert_at_cursor(GTK_TEXT_BUFFER(buffer), str, strlen(str)); + // g_print("--text-buffered--\n"); + gtk_text_view_set_buffer(GTK_TEXT_VIEW(textViewWindow), buffer); + // g_print("--text-printed--\n"); +} + + void setRGB(GdkRGBA* color, int r, int g, int b) +{ + color->red = r; + color->green = g; + color->blue = b; + color->alpha = 1; +} + +static void button1(GtkWidget *widget, gpointer data) +{ + textViewPrint ("Button1\n"); +} + +static void submitClicked(GtkWidget *widget, gpointer data) +{ + g_print("Submitted\n"); + textViewPrint("Submitted\n"); + + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radioButton1))) + { + g_print("Radio 1 selected\n"); + textViewPrint("Radio 1 selected\n"); + } + else + { + g_print("Radio 2 selected\n"); + textViewPrint("Radio 2 selected\n"); + } + + GtkEntryBuffer *entryBuffer; + entryBuffer = gtk_entry_get_buffer(GTK_ENTRY(textBox)); + + g_print("Text in textbox: %s\n", gtk_entry_buffer_get_text(GTK_ENTRY_BUFFER(entryBuffer))); + textViewPrint("Text in textbox: %s\n", gtk_entry_buffer_get_text(GTK_ENTRY_BUFFER(entryBuffer))); + + int w,h; + gtk_window_get_size(GTK_WINDOW(window), &w, &h); + + g_print("Width: %d, Height: %d\n", w, h); + textViewPrint("Width: %i, Height: %i\n", w, h); +} + +static void activate(GtkApplication* app, gpointer user_data) +{ + /// Make window + window = gtk_application_window_new(app); + gtk_window_set_title(GTK_WINDOW(window), "Window"); + gtk_window_set_default_size(GTK_WINDOW(window), 200, 200); + + /// grid to hold multiple witgets + windowGrid = gtk_grid_new(); + /// Attach grid to window + gtk_container_add(GTK_CONTAINER(window), windowGrid); + + /// Make button + GtkWidget *button; + button = gtk_button_new_with_label ("Button1"); + g_signal_connect (button, "clicked", G_CALLBACK (button1), NULL); + + /** Place the first button in the grid cell (0, 0), and make it fill + * just 1 cell horizontally and vertically (ie no spanning) + */ + gtk_grid_attach(GTK_GRID(windowGrid), button, 0, 0, 1, 1); + + /// Make box for 2 radio buttons + // GtkWidget *radioBox; + // radioBox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2); // (orientation, number of rows) + //gtk_box_set_homogeneous (GTK_BOX(radioBox), TRUE); + + /// Make radio buttons + radioButton1 = gtk_radio_button_new_with_label(NULL, "radio1"); + radioButton2 = gtk_radio_button_new_with_label(NULL, "radio2"); + gtk_radio_button_set_group(GTK_RADIO_BUTTON(radioButton2), gtk_radio_button_get_group(GTK_RADIO_BUTTON(radioButton1))); + gtk_grid_attach(GTK_GRID(windowGrid), radioButton1, 0, 1, 1, 1); + gtk_grid_attach(GTK_GRID(windowGrid), radioButton2, 1, 1, 1, 1); + + /// Pack radio buttons in radio box + // gtk_box_pack_start(GTK_BOX (radioBox), radioButton1, FALSE, FALSE, 0); + // gtk_box_pack_start(GTK_BOX (radioBox), radioButton2, FALSE, FALSE, 0); + // gtk_box_pack_start (GTK_BOX(windowGrid), radioBox, FALSE, FALSE, 0); + + /// Make lable for text box + textLabel = gtk_label_new ("Enter something: "); + gtk_grid_attach(GTK_GRID(windowGrid), textLabel, 0, 2, 1, 1); + /// Spacing for widget + gtk_widget_set_margin_top(GTK_WIDGET(textLabel), 10); + gtk_widget_set_margin_bottom(GTK_WIDGET(textLabel), 10); + gtk_widget_set_margin_right(GTK_WIDGET(textLabel), 10); + gtk_widget_set_margin_left(GTK_WIDGET(textLabel), 10); + + /// Make text box + textBox = gtk_entry_new(); + gtk_entry_set_max_length(GTK_ENTRY(textBox), 0); + gtk_grid_attach(GTK_GRID(windowGrid), textBox, 1, 2, 1, 1); + + /// Make submit button + buttonSubmit = gtk_button_new_with_label ("Submit"); + g_signal_connect (buttonSubmit, "clicked", G_CALLBACK (submitClicked), NULL); + gtk_grid_attach(GTK_GRID(windowGrid), buttonSubmit, 0, 5, 1, 1); + setRGB(&color, 0, 1, 0); + gtk_widget_override_color(GTK_WIDGET(buttonSubmit), GTK_STATE_FLAG_NORMAL, &color); + + /// Make cancel button + buttonCancel = gtk_button_new_with_label ("Cancel"); + g_signal_connect_swapped (buttonCancel, "clicked", G_CALLBACK (gtk_widget_destroy), window); + gtk_grid_attach(GTK_GRID(windowGrid), buttonCancel, 1, 5, 1, 1); + setRGB(&color, 1, 0, 0); + gtk_widget_override_color(GTK_WIDGET(buttonCancel), GTK_STATE_FLAG_NORMAL, &color); + + /// Make text view widget + scrolledwindow = gtk_scrolled_window_new(NULL, NULL); + textViewWindow = gtk_text_view_new(); + gtk_container_add(GTK_CONTAINER(scrolledwindow), textViewWindow); + gtk_text_view_set_editable(GTK_TEXT_VIEW(textViewWindow), FALSE); + gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textViewWindow), TRUE); + gtk_text_view_set_overwrite(GTK_TEXT_VIEW(textViewWindow), FALSE); + //gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textViewWindow), GTK_WRAP_WORD); + gtk_widget_set_size_request(GTK_WIDGET(scrolledwindow), 2, 80); + gtk_grid_attach(GTK_GRID(windowGrid), scrolledwindow, 0, 6, 5, 5); + + gtk_widget_show_all (window); +} + +int main (int argc, char **argv) +{ + /// Make app + GtkApplication *app; + int status; + + app = gtk_application_new ("org.git.GTK", G_APPLICATION_FLAGS_NONE); + g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); + status = g_application_run (G_APPLICATION (app), argc, argv); + g_object_unref (app); + + return status; +} diff --git a/help/simple-text.c b/help/simple-text.c deleted file mode 100644 index d4a986c..0000000 --- a/help/simple-text.c +++ /dev/null @@ -1,173 +0,0 @@ -/* - * simple-text.c - demonstrate drawing of text strings on a window. All - * drawings are done in black color over a white background. - */ - -#include <X11/Xlib.h> - -#include <stdio.h> -#include <stdlib.h> /* getenv(), etc. */ -#include <unistd.h> /* sleep(), etc. */ - -/* - * function: create_simple_window. Creates a window with a white background - * in the given size. - * input: display, size of the window (in pixels), and location of the window - * (in pixels). - * output: the window's ID. - * notes: window is created with a black border, 2 pixels wide. - * the window is automatically mapped after its creation. - */ -Window -create_simple_window(Display* display, int width, int height, int x, int y) -{ - int screen_num = DefaultScreen(display); - int win_border_width = 2; - Window win; - - /* create a simple window, as a direct child of the screen's */ - /* root window. Use the screen's black and white colors as */ - /* the foreground and background colors of the window, */ - /* respectively. Place the new window's top-left corner at */ - /* the given 'x,y' coordinates. */ - win = XCreateSimpleWindow(display, RootWindow(display, screen_num), - x, y, width, height, win_border_width, - BlackPixel(display, screen_num), - WhitePixel(display, screen_num)); - - /* make the window actually appear on the screen. */ - XMapWindow(display, win); - - /* flush all pending requests to the X server. */ - XFlush(display); - - return win; -} - -GC -create_gc(Display* display, Window win, int reverse_video) -{ - GC gc; /* handle of newly created GC. */ - unsigned long valuemask = 0; /* which values in 'values' to */ - /* check when creating the GC. */ - XGCValues values; /* initial values for the GC. */ - unsigned int line_width = 2; /* line width for the GC. */ - int line_style = LineSolid; /* style for lines drawing and */ - int cap_style = CapButt; /* style of the line's edje and */ - int join_style = JoinBevel; /* joined lines. */ - int screen_num = DefaultScreen(display); - - gc = XCreateGC(display, win, valuemask, &values); - if (gc < 0) { - fprintf(stderr, "XCreateGC: \n"); - } - - /* allocate foreground and background colors for this GC. */ - if (reverse_video) { - XSetForeground(display, gc, WhitePixel(display, screen_num)); - XSetBackground(display, gc, BlackPixel(display, screen_num)); - } - else { - XSetForeground(display, gc, BlackPixel(display, screen_num)); - XSetBackground(display, gc, WhitePixel(display, screen_num)); - } - - /* define the style of lines that will be drawn using this GC. */ - XSetLineAttributes(display, gc, - line_width, line_style, cap_style, join_style); - - /* define the fill style for the GC. to be 'solid filling'. */ - XSetFillStyle(display, gc, FillSolid); - - return gc; -} - -void -main(int argc, char* argv[]) -{ - Display* display; /* pointer to X Display structure. */ - int screen_num; /* number of screen to place the window on. */ - Window win; /* pointer to the newly created window. */ - unsigned int display_width, - display_height; /* height and width of the X display. */ - unsigned int win_width, - win_height; /* height and width for the new window. */ - char *display_name = getenv("DISPLAY"); /* address of the X display. */ - GC gc; /* GC (graphics context) used for drawing */ - /* in our window. */ - XFontStruct* font_info; /* Font structure, used for drawing text. */ - char* font_name = "*-helvetica-*-12-*"; /* font to use for drawing text. */ - - /* open connection with the X server. */ - display = XOpenDisplay(display_name); - if (display == NULL) { - fprintf(stderr, "%s: cannot connect to X server '%s'\n", - argv[0], display_name); - exit(1); - } - - /* get the geometry of the default screen for our display. */ - screen_num = DefaultScreen(display); - display_width = DisplayWidth(display, screen_num); - display_height = DisplayHeight(display, screen_num); - - /* make the new window occupy 1/9 of the screen's size. */ - win_width = (display_width / 3); - win_height = (display_height / 3); - printf("window width - '%d'; height - '%d'\n", win_width, win_height); - - /* create a simple window, as a direct child of the screen's */ - /* root window. Use the screen's white color as the background */ - /* color of the window. Place the new window's top-left corner */ - /* at the given 'x,y' coordinates. */ - win = create_simple_window(display, win_width, win_height, 0, 0); - - /* allocate a new GC (graphics context) for drawing in the window. */ - gc = create_gc(display, win, 0); - XSync(display, False); - - /* try to load the given font. */ - font_info = XLoadQueryFont(display, font_name); - if (!font_info) { - fprintf(stderr, "XLoadQueryFont: failed loading font '%s'\n", font_name); - exit(-1); - } - - /* assign the given font to our GC. */ - XSetFont(display, gc, font_info->fid); - - { - /* variables used for drawing the text strings. */ - int x, y; - char* text_string; - int string_width; - int font_height; - - /* find the height of the characters drawn using this font. */ - font_height = font_info->ascent + font_info->descent; - - /* draw a "hello world" string on the top-left side of our window. */ - text_string = "hello world"; - x = 0; - y = font_height; - XDrawString(display, win, gc, x, y, text_string, strlen(text_string)); - - /* draw a "middle of the road" string in the middle of our window. */ - text_string = "middle of the road"; - /* find the width, in pixels, of the text that will be drawn using */ - /* the given font. */ - string_width = XTextWidth(font_info, text_string, strlen(text_string)); - x = (win_width - string_width) / 2; - y = (win_height + font_height) / 2; - XDrawString(display, win, gc, x, y, text_string, strlen(text_string)); - } - - /* flush all pending requests to the X server. */ - XFlush(display); - - /* make a delay for a short period. */ - sleep(4); - - /* close the connection to the X server. */ - XCloseDisplay(display); -} |