diff options
Diffstat (limited to 'tikzit/src/gtk/gtkhelpers.m')
-rw-r--r-- | tikzit/src/gtk/gtkhelpers.m | 275 |
1 files changed, 134 insertions, 141 deletions
diff --git a/tikzit/src/gtk/gtkhelpers.m b/tikzit/src/gtk/gtkhelpers.m index 164228c..c37077b 100644 --- a/tikzit/src/gtk/gtkhelpers.m +++ b/tikzit/src/gtk/gtkhelpers.m @@ -25,135 +25,9 @@ #import "gtkhelpers.h" #import <gdk/gdkkeysyms.h> -void gtk_table_adjust_attach (GtkTable *table, - GtkWidget *widget, - gint left_adjust, - gint right_adjust, - gint top_adjust, - gint bottom_adjust) { - guint top_attach; - guint bottom_attach; - guint left_attach; - guint right_attach; - GtkAttachOptions xoptions; - GtkAttachOptions yoptions; - guint xpadding; - guint ypadding; - - gtk_container_child_get (GTK_CONTAINER (table), widget, - "top-attach", &top_attach, - "bottom-attach", &bottom_attach, - "left-attach", &left_attach, - "right-attach", &right_attach, - "x-options", &xoptions, - "y-options", &yoptions, - "x-padding", &xpadding, - "y-padding", &ypadding, - NULL); - - g_object_ref (G_OBJECT (widget)); - gtk_container_remove (GTK_CONTAINER (table), widget); - gtk_table_attach (table, widget, - left_attach + left_adjust, - right_attach + right_adjust, - top_attach + top_adjust, - bottom_attach + bottom_adjust, - xoptions, - yoptions, - xpadding, - ypadding); - g_object_unref (G_OBJECT (widget)); -} - -/* - * Delete multiple table rows - */ -void gtk_table_delete_rows (GtkTable *table, guint firstRow, guint count) { - if (count == 0) { - return; - } - GtkContainer *tableC = GTK_CONTAINER (table); - - guint n_columns; - guint n_rows; - g_object_get (G_OBJECT (table), - "n-columns", &n_columns, - "n-rows", &n_rows, - NULL); - guint topBound = firstRow; - guint bottomBound = firstRow + count; - if (bottomBound > n_rows) { - bottomBound = n_rows; - count = bottomBound - topBound; - } - - GList *toBeDeleted = NULL; - GList *toBeShrunk = NULL; - /* indexed by top-attach */ - GPtrArray *toBeMoved = g_ptr_array_sized_new (n_rows - topBound); - g_ptr_array_set_size (toBeMoved, n_rows - topBound); - - GList *childIt = gtk_container_get_children (tableC); - - while (childIt) { - GtkWidget *widget = GTK_WIDGET (childIt->data); - guint top_attach; - guint bottom_attach; - gtk_container_child_get (tableC, widget, - "top-attach", &top_attach, - "bottom-attach", &bottom_attach, - NULL); - if (top_attach >= topBound && bottom_attach <= bottomBound) { - toBeDeleted = g_list_prepend (toBeDeleted, widget); - } else if (top_attach <= topBound && bottom_attach > topBound) { - toBeShrunk = g_list_prepend (toBeShrunk, widget); - } else if (top_attach > topBound) { - GList *rowList = (GList*)g_ptr_array_index (toBeMoved, top_attach - topBound); - rowList = g_list_prepend (rowList, widget); - g_ptr_array_index (toBeMoved, top_attach - topBound) = rowList; - } - childIt = childIt->next; - } - g_list_free (childIt); - - /* remove anything that is completely within the segment being deleted */ - while (toBeDeleted) { - gtk_container_remove (tableC, GTK_WIDGET (toBeDeleted->data)); - toBeDeleted = toBeDeleted->next; - } - g_list_free (toBeDeleted); - - /* shrink anything that spans the segment */ - while (toBeShrunk) { - GtkWidget *widget = GTK_WIDGET (toBeShrunk->data); - gtk_table_adjust_attach (table, widget, 0, 0, 0, -count); - toBeShrunk = toBeShrunk->next; - } - g_list_free (toBeShrunk); - - /* move everything below the segment being deleted up, in order */ - /* note that "n-rows" is not a valid "top-attach" */ - for (int offset = 0; offset < (n_rows - 1) - topBound; ++offset) { - GList *rowList = (GList *)g_ptr_array_index (toBeMoved, offset); - guint top_attach = offset + topBound; - guint overlap = bottomBound - top_attach; - while (rowList) { - GtkWidget *widget = GTK_WIDGET (rowList->data); - gtk_table_adjust_attach (table, widget, 0, 0, -offset, -(offset + overlap)); - rowList = rowList->next; - } - g_list_free (rowList); - g_ptr_array_index (toBeMoved, offset) = NULL; - } - - gtk_table_resize (table, n_rows - 1, n_columns); -} - -/* - * Delete a table row - */ -void gtk_table_delete_row (GtkTable *table, guint row) { - gtk_table_delete_rows (table, row, 1); +void release_obj (gpointer data) { + id obj = (id)data; + [obj release]; } NSString * gtk_editable_get_string (GtkEditable *editable, gint start, gint end) @@ -164,16 +38,6 @@ NSString * gtk_editable_get_string (GtkEditable *editable, gint start, gint end) return string; } -void gtk_entry_set_string (GtkEntry *entry, NSString *string) -{ - gtk_entry_set_text (entry, string == nil ? "" : [string UTF8String]); -} - -NSString * gtk_entry_get_string (GtkEntry *entry) -{ - return [NSString stringWithUTF8String:gtk_entry_get_text (entry)]; -} - GdkRectangle gdk_rectangle_from_ns_rect (NSRect box) { GdkRectangle rect; rect.x = box.origin.x; @@ -213,8 +77,6 @@ void gtk_action_set_detailed_label (GtkAction *action, const gchar *baseLabel, c * it simply allows the window contents to treat key events /before/ * accelerator keys come into play (this way widgets dont get deleted * when cutting text in an entry etc.). - * Creates a liststore suitable for comboboxes and such to - * chose from a variety of types. * * Returns: whether the event was handled */ @@ -241,4 +103,135 @@ tz_hijack_key_press (GtkWindow *win, return FALSE; } +GdkPixbuf * pixbuf_get_from_surface(cairo_surface_t *surface) { + cairo_surface_flush (surface); + + int width = cairo_image_surface_get_width (surface); + int height = cairo_image_surface_get_height (surface); + int stride = cairo_image_surface_get_stride (surface); + unsigned char *data = cairo_image_surface_get_data (surface); + + GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, + TRUE, + 8, + width, + height); + unsigned char *pbdata = gdk_pixbuf_get_pixels (pixbuf); + int pbstride = gdk_pixbuf_get_rowstride (pixbuf); + + for (int y = 0; y < height; ++y) { + uint32_t *line = (uint32_t*)(data + y*stride); + unsigned char *pbline = pbdata + (y*pbstride); + for (int x = 0; x < width; ++x) { + uint32_t pixel = *(line + x); + unsigned char *pbpixel = pbline + (x*4); + // NB: We should un-pre-mult the alpha here. + // However, in our world, alpha is always + // on or off, so it doesn't really matter + pbpixel[3] = ((pixel & 0xff000000) >> 24); + pbpixel[0] = ((pixel & 0x00ff0000) >> 16); + pbpixel[1] = ((pixel & 0x0000ff00) >> 8); + pbpixel[2] = (pixel & 0x000000ff); + } + } + + return pixbuf; +} + +/* This function mostly lifted from + * gtk+/gdk/gdkscreen.c:gdk_screen_get_monitor_at_window() + */ +static gint +get_appropriate_monitor (GdkScreen *screen, + gint x, + gint y, + gint w, + gint h) +{ + GdkRectangle rect; + gint area = 0; + gint monitor = -1; + gint num_monitors; + gint i; + + rect.x = x; + rect.y = y; + rect.width = w; + rect.height = h; + + num_monitors = gdk_screen_get_n_monitors (screen); + + for (i = 0; i < num_monitors; i++) + { + GdkRectangle geometry; + + gdk_screen_get_monitor_geometry (screen, i, &geometry); + + if (gdk_rectangle_intersect (&rect, &geometry, &geometry) && + geometry.width * geometry.height > area) + { + area = geometry.width * geometry.height; + monitor = i; + } + } + + if (monitor >= 0) + return monitor; + else + return gdk_screen_get_monitor_at_point (screen, + rect.x + rect.width / 2, + rect.y + rect.height / 2); +} + +/* This function mostly lifted from gimp_session_info_apply_geometry + * in gimp-2.6/app/widgets/gimpsessioninfo.c + */ +void tz_restore_window (GtkWindow *window, gint x, gint y, gint w, gint h) +{ + gint forced_w = w; + gint forced_h = h; + if (w <= 0 || h <= 0) { + gtk_window_get_default_size (window, &w, &h); + } + if (w <= 0 || h <= 0) { + gtk_window_get_size (window, &w, &h); + } + + GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (window)); + + gint monitor = 0; + if (w > 0 && h > 0) { + monitor = get_appropriate_monitor (screen, x, y, w, h); + } else { + monitor = gdk_screen_get_monitor_at_point (screen, x, y); + } + + GdkRectangle rect; + gdk_screen_get_monitor_geometry (screen, monitor, &rect); + + x = CLAMP (x, + rect.x, + rect.x + rect.width - (w > 0 ? w : 128)); + y = CLAMP (y, + rect.y, + rect.y + rect.height - (h > 0 ? h : 128)); + + gchar geom[32]; + g_snprintf (geom, sizeof (geom), "%+d%+d", x, y); + + gtk_window_parse_geometry (window, geom); + + if (forced_w > 0 && forced_h > 0) { + gtk_window_set_default_size (window, forced_w, forced_h); + } +} + +void label_set_bold (GtkLabel *label) { + PangoAttrList *attrs = pango_attr_list_new (); + pango_attr_list_insert (attrs, + pango_attr_weight_new (PANGO_WEIGHT_SEMIBOLD)); + gtk_label_set_attributes (label, attrs); + pango_attr_list_unref (attrs); +} + // vim:ft=objc:ts=8:et:sts=4:sw=4 |