1 /* This entire file is licensed under MIT
2  *
3  * Copyright 2020 William Wold
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6  *
7  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10  */
11 
12 #include "gtk-layer-demo.h"
13 
14 static void
on_orientation_changed(GtkWindow * _window,WindowOrientation orientation,GtkWidget * box)15 on_orientation_changed (GtkWindow *_window, WindowOrientation orientation, GtkWidget *box)
16 {
17     (void)_window;
18 
19     switch (orientation) {
20         case WINDOW_ORIENTATION_HORIZONTAL:
21             gtk_orientable_set_orientation (GTK_ORIENTABLE (box), GTK_ORIENTATION_HORIZONTAL);
22             break;
23         case WINDOW_ORIENTATION_VERTICAL:
24             gtk_orientable_set_orientation (GTK_ORIENTABLE (box), GTK_ORIENTATION_VERTICAL);
25             break;
26         case WINDOW_ORIENTATION_NONE:
27             gtk_orientable_set_orientation (GTK_ORIENTABLE (box), GTK_ORIENTATION_VERTICAL);
28             break;
29     }
30 }
31 
32 typedef struct {
33     GtkLayerShellEdge edge;
34     GtkWindow *layer_window;
35 } MarginSpinButtonData;
36 
37 static void
on_margin_changed(GtkSpinButton * button,MarginSpinButtonData * data)38 on_margin_changed (GtkSpinButton *button, MarginSpinButtonData *data)
39 {
40     int value = gtk_spin_button_get_value (button);
41     gtk_layer_set_margin (data->layer_window, data->edge, value);
42 }
43 
44 static GtkWidget *
margin_spin_button_new(GtkWindow * layer_window,GtkLayerShellEdge edge,const char * tooltip,const int default_margins[GTK_LAYER_SHELL_EDGE_ENTRY_NUMBER])45 margin_spin_button_new (GtkWindow *layer_window,
46                         GtkLayerShellEdge edge,
47                         const char *tooltip,
48                         const int default_margins[GTK_LAYER_SHELL_EDGE_ENTRY_NUMBER])
49 {
50     GtkAdjustment *adjustment = gtk_adjustment_new (default_margins[edge], 0.0, 4000.0, 20.0, 50.0, 0.0);
51     GtkWidget *button = gtk_spin_button_new (adjustment, 0.5, 0);
52     gtk_widget_set_tooltip_text (button, tooltip);
53     MarginSpinButtonData *data = g_new0 (MarginSpinButtonData, 1);
54     *data = (MarginSpinButtonData) {
55         .edge = edge,
56         .layer_window = layer_window,
57     };
58     g_object_set_data_full(G_OBJECT (button), "value-changed_signal_data", data, (GDestroyNotify)g_free);
59     g_signal_connect (button, "value-changed", G_CALLBACK (on_margin_changed), data);
60     return button;
61 }
62 
63 static void
on_open_clicked(GtkWidget * _button,GtkWidget * popover)64 on_open_clicked (GtkWidget *_button, GtkWidget *popover)
65 {
66     (void)_button;
67 
68     gtk_popover_popup (GTK_POPOVER (popover));
69 }
70 
71 GtkWidget *
margin_control_new(GtkWindow * layer_window,const int default_margins[GTK_LAYER_SHELL_EDGE_ENTRY_NUMBER])72 margin_control_new (GtkWindow *layer_window, const int default_margins[GTK_LAYER_SHELL_EDGE_ENTRY_NUMBER])
73 {
74     const int spacing = 4;
75     GtkWidget *switch_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, spacing);
76     g_signal_connect (layer_window, "orientation-changed", G_CALLBACK (on_orientation_changed), switch_box);
77     {
78         GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, spacing);
79         gtk_box_pack_start (GTK_BOX (switch_box), vbox, FALSE, FALSE, 0);
80         {
81             GtkWidget *button = margin_spin_button_new (layer_window, GTK_LAYER_SHELL_EDGE_LEFT, "Left margin", default_margins);
82             gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
83         }
84         {
85             GtkWidget *button = margin_spin_button_new (layer_window, GTK_LAYER_SHELL_EDGE_RIGHT, "Right margin", default_margins);
86             gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
87         }
88     }
89     {
90         GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, spacing);
91         gtk_box_pack_start (GTK_BOX (switch_box), vbox, FALSE, FALSE, 0);
92         {
93             GtkWidget *button = margin_spin_button_new (layer_window, GTK_LAYER_SHELL_EDGE_TOP, "Top margin", default_margins);
94             gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
95         }
96         {
97             GtkWidget *button = margin_spin_button_new (layer_window, GTK_LAYER_SHELL_EDGE_BOTTOM, "Bottom margin", default_margins);
98             gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
99         }
100     }
101 
102     GtkWidget *open_button = gtk_button_new_with_label ("Set margin");
103     gtk_widget_set_tooltip_text (open_button, "Space to leave empty around surface");
104     GtkWidget *popover = gtk_popover_new (open_button);
105     gtk_popover_set_modal (GTK_POPOVER (popover), TRUE);
106     gtk_popover_set_constrain_to (GTK_POPOVER (popover), GTK_POPOVER_CONSTRAINT_WINDOW);
107     gtk_popover_set_position (GTK_POPOVER (popover), GTK_POS_BOTTOM);
108     gtk_container_add (GTK_CONTAINER (popover), switch_box);
109     gtk_widget_show_all (switch_box);
110     g_signal_connect (open_button, "clicked", G_CALLBACK (on_open_clicked), popover);
111     return open_button;
112 }
113