1 /*
2  * markdown-gtk-compat.c - Part of the Geany Markdown plugin
3  *
4  * Copyright 2012 Matthew Brush <mbrush@codebrainz.ca>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  *
21  *
22  */
23 
24 #include <gtk/gtk.h>
25 #include "markdown-gtk-compat.h"
26 
markdown_gtk_table_new(guint rows,guint columns,gboolean homogeneous)27 GtkWidget *markdown_gtk_table_new(guint rows, guint columns, gboolean homogeneous)
28 {
29   GtkWidget *table;
30 
31 #if !GTK_CHECK_VERSION(3, 4, 0)
32   table = gtk_table_new(rows, columns, homogeneous);
33 #else
34   guint row_cnt, col_cnt;
35 
36   table = gtk_grid_new();
37 
38   gtk_grid_set_row_homogeneous(MARKDOWN_GTK_TABLE(table), homogeneous);
39   gtk_grid_set_column_homogeneous(MARKDOWN_GTK_TABLE(table), homogeneous);
40 
41   for (row_cnt=0; row_cnt < rows; row_cnt++)
42     gtk_grid_insert_row(MARKDOWN_GTK_TABLE(table), row_cnt);
43 
44   for (col_cnt=0; col_cnt < columns; col_cnt++)
45     gtk_grid_insert_column(MARKDOWN_GTK_TABLE(table), col_cnt);
46 #endif
47 
48   return table;
49 }
50 
markdown_gtk_table_attach(MarkdownGtkTable * table,GtkWidget * child,guint left_attach,guint right_attach,guint top_attach,guint bottom_attach,GtkAttachOptions xoptions,GtkAttachOptions yoptions)51 void markdown_gtk_table_attach(MarkdownGtkTable *table, GtkWidget *child,
52   guint left_attach, guint right_attach, guint top_attach, guint bottom_attach,
53   GtkAttachOptions xoptions, GtkAttachOptions yoptions)
54 {
55 #if !GTK_CHECK_VERSION(3, 4, 0)
56   gtk_table_attach(table, child, left_attach, right_attach, top_attach,
57     bottom_attach, xoptions, yoptions, 0, 0);
58 #else
59   gtk_grid_attach(table, child, left_attach, top_attach,
60     right_attach - left_attach, bottom_attach - top_attach);
61 #endif
62 }
63 
markdown_gtk_color_button_new_with_color(MarkdownColor * color)64 GtkWidget *markdown_gtk_color_button_new_with_color(MarkdownColor *color)
65 {
66   GtkWidget *btn;
67 
68   btn = gtk_color_button_new();
69 
70 #if !GTK_CHECK_VERSION(3, 0, 0)
71 {
72   GdkColor clr;
73   clr.red = color->red * 256;
74   clr.green = color->green * 256;
75   clr.blue = color->blue * 256;
76   gtk_color_button_set_color(GTK_COLOR_BUTTON(btn), &clr);
77 }
78 #else
79 {
80   GdkRGBA clr;
81   clr.red = color->red / 255.0;
82   clr.green = color->green / 255.0;
83   clr.blue = color->blue / 255.0;
84   clr.alpha = 1.0;
85 # if !GTK_CHECK_VERSION(3, 4, 0)
86   gtk_color_button_set_rgba(GTK_COLOR_BUTTON(btn), &clr);
87 # else
88   gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(btn), &clr);
89 # endif
90 }
91 #endif
92 
93   return btn;
94 }
95 
markdown_color_parse(const gchar * spec,MarkdownColor * color)96 gboolean markdown_color_parse(const gchar *spec, MarkdownColor *color)
97 {
98   gboolean result;
99   g_return_val_if_fail(spec && color, FALSE);
100 
101 #if !GTK_CHECK_VERSION(3, 0, 0)
102 {
103   GdkColor clr;
104   result = gdk_color_parse(spec, &clr);
105   if (result) {
106     color->red = clr.red / 256;
107     color->green = clr.green / 256;
108     color->blue = clr.blue / 256;
109   }
110 }
111 #else
112 {
113   GdkRGBA clr;
114   result = gdk_rgba_parse(&clr, spec);
115   if (result) {
116     color->red = (guint8) (clr.red * 255.0);
117     color->green = (guint8) (clr.green * 255.0);
118     color->blue = (guint8) (clr.blue * 255.0);
119   }
120 }
121 #endif
122 
123   return result;
124 }
125 
markdown_gtk_color_button_get_color(GtkColorButton * button,MarkdownColor * color)126 void markdown_gtk_color_button_get_color(GtkColorButton *button, MarkdownColor *color)
127 {
128   g_return_if_fail(button);
129   g_return_if_fail(color);
130 
131 #if !GTK_CHECK_VERSION(3, 0, 0)
132   GdkColor clr;
133   gtk_color_button_get_color(button, &clr);
134   color->red = clr.red / 256;
135   color->green = clr.green / 256;
136   color->blue = clr.blue / 256;
137 #else
138   GdkRGBA clr;
139 # if !GTK_CHECK_VERSION(3, 4, 0)
140   gtk_color_button_get_rgba(button, &clr);
141 # else
142   gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(button), &clr);
143 # endif
144   color->red = (guint8) (clr.red * 255.0);
145   color->green = (guint8) (clr.green * 255.0);
146   color->blue = (guint8) (clr.blue * 255.0);
147 #endif
148 }
149