@@ -74,23 +74,29 @@ static void sepia_filter_apply(GtkWidget *widget, gpointer data)
7474 g_print ("Sepia Filter Has Been Applied\n" );
7575}
7676
77+ // struct for filter buttons
78+ typedef struct
79+ {
80+ const char * label ;
81+ void (* callback )(GtkWidget * , gpointer );
82+ } FilterButtonInfo ;
83+
84+ // connect filter buttons to callback functions
85+ void connect_filter_buttons (GtkWidget * image , const FilterButtonInfo * button_info , int num_buttons , GtkWidget * button_box )
86+ {
87+ for (int i = 0 ; i < num_buttons ; ++ i )
88+ {
89+ GtkWidget * button = gtk_button_new_with_label (button_info [i ].label );
90+ g_signal_connect (button , "clicked" , G_CALLBACK (button_info [i ].callback ), image );
91+ gtk_box_append (GTK_BOX (button_box ), button );
92+ }
93+ }
94+
7795static void activate (GtkApplication * app , gpointer user_data )
7896{
79- GtkWidget * window ;
80- GtkWidget * box ;
81- GtkWidget * image ;
82- GtkWidget * user_image_box ;
83- GtkWidget * filtered_image_box ;
84- GtkWidget * button_box0 ;
85- GtkWidget * button_box1 ;
86- GtkWidget * button0 ;
87- GtkWidget * button1 ;
88- GtkWidget * button2 ;
89- GtkWidget * button3 ;
90- GtkWidget * button4 ;
91- GtkWidget * button5 ;
92- GtkWidget * button6 ;
93- GtkWidget * button7 ;
97+ GtkWidget * window , * box , * image , * user_image_box , * filtered_image_box ,
98+ * button_box0 , * button_box1 , * button0 , * button1 , * button2 ,
99+ * button3 , * button4 , * button5 , * button6 , * button7 ;
94100 const char * css = "window {"
95101 " background-color: #FFFFE4;"
96102 "}"
@@ -113,6 +119,16 @@ static void activate(GtkApplication *app, gpointer user_data)
113119 " margin: 8px 5px;"
114120 " font-family: BigNoodleTitling;"
115121 "}" ;
122+ FilterButtonInfo filter_buttons [] = {
123+ {"Black And White Filter" , black_and_white_filter_apply },
124+ {"Bright Filter" , bright_filter_apply },
125+ {"Dark Filter" , dark_filter_apply },
126+ {"Emboss Filter" , emboss_filter_apply },
127+ {"Negative Filter" , negative_filter_apply },
128+ {"Pixelate Filter" , pixelate_filter_apply },
129+ {"RGB To Gray Filter" , rgb_to_gray_filter_apply },
130+ {"Sepia Filter" , sepia_filter_apply }};
131+ int num_filter_buttons = sizeof (filter_buttons ) / sizeof (filter_buttons [0 ]);
116132
117133 // create new window
118134 window = gtk_application_window_new (app );
@@ -168,33 +184,9 @@ static void activate(GtkApplication *app, gpointer user_data)
168184 // append button box to vertical box
169185 gtk_box_append (GTK_BOX (box ), button_box1 );
170186
171- // create filter buttons
172- button0 = gtk_button_new_with_label ("Black And White Filter" );
173- button1 = gtk_button_new_with_label ("Bright Filter" );
174- button2 = gtk_button_new_with_label ("Dark Filter" );
175- button3 = gtk_button_new_with_label ("Emboss Filter" );
176- button4 = gtk_button_new_with_label ("Negative Filter" );
177- button5 = gtk_button_new_with_label ("Pixelate Filter" );
178- button6 = gtk_button_new_with_label ("RGB To Gray Filter" );
179- button7 = gtk_button_new_with_label ("Sepia Filter" );
180- // connect buttons to callback functions
181- g_signal_connect (button0 , "clicked" , G_CALLBACK (black_and_white_filter_apply ), image );
182- g_signal_connect (button1 , "clicked" , G_CALLBACK (bright_filter_apply ), image );
183- g_signal_connect (button2 , "clicked" , G_CALLBACK (dark_filter_apply ), image );
184- g_signal_connect (button3 , "clicked" , G_CALLBACK (emboss_filter_apply ), image );
185- g_signal_connect (button4 , "clicked" , G_CALLBACK (negative_filter_apply ), image );
186- g_signal_connect (button5 , "clicked" , G_CALLBACK (pixelate_filter_apply ), image );
187- g_signal_connect (button6 , "clicked" , G_CALLBACK (rgb_to_gray_filter_apply ), image );
188- g_signal_connect (button7 , "clicked" , G_CALLBACK (sepia_filter_apply ), image );
189- // append buttons to button box
190- gtk_box_append (GTK_BOX (button_box0 ), button0 );
191- gtk_box_append (GTK_BOX (button_box0 ), button1 );
192- gtk_box_append (GTK_BOX (button_box0 ), button2 );
193- gtk_box_append (GTK_BOX (button_box0 ), button3 );
194- gtk_box_append (GTK_BOX (button_box1 ), button4 );
195- gtk_box_append (GTK_BOX (button_box1 ), button5 );
196- gtk_box_append (GTK_BOX (button_box1 ), button6 );
197- gtk_box_append (GTK_BOX (button_box1 ), button7 );
187+ // connect filter buttons to callback functions
188+ connect_filter_buttons (image , filter_buttons , num_filter_buttons / 2 , button_box0 );
189+ connect_filter_buttons (image , filter_buttons + num_filter_buttons / 2 , num_filter_buttons / 2 , button_box1 );
198190
199191 // create new CSS provider object and load CSS
200192 GtkCssProvider * provider = gtk_css_provider_new ();
0 commit comments