runningwater 3 سال پیش
والد
کامیت
ec5a27f42e
3فایلهای تغییر یافته به همراه149 افزوده شده و 0 حذف شده
  1. 52 0
      05.xml_layout.c
  2. 16 0
      builder.css
  3. 81 0
      builder.ui

+ 52 - 0
05.xml_layout.c

@@ -0,0 +1,52 @@
+//
+// Created by 李晓明 on 2022/11/24.
+//
+#include <gtk/gtk.h>
+
+static void print_hello(GtkWidget *widget, gpointer data) {
+  g_print("Hello: %s\n", (char *) data);
+}
+
+int main(int argc, char **argv) {
+  GError *error = NULL;
+
+  gtk_init(&argc, &argv);
+
+  GtkCssProvider *css_provider = gtk_css_provider_new();
+  if (gtk_css_provider_load_from_path(css_provider, "builder.css", &error) == 0) {
+    g_printerr("Error loading css file: %s\n", error->message);
+    int err_code = error->code;
+    g_clear_error(&error);
+    return err_code;
+  }
+
+  GtkBuilder *builder = gtk_builder_new();
+  /* 加载样式 文件*/
+  if (gtk_builder_add_from_file(builder, "builder.ui", &error) == 0) {
+    g_printerr("Error loading builder file: %s\n", error->message);
+    int err_code = error->code;
+    g_clear_error(&error);
+    return err_code;
+  }
+
+  GObject *window = gtk_builder_get_object(builder, "window");
+  /* 使样式文件生效 */
+  gtk_style_context_add_provider_for_screen(gtk_window_get_screen(GTK_WINDOW(window)),
+                                            (GtkStyleProvider *) css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER);
+
+  g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
+
+  GObject *button1 = gtk_builder_get_object(builder, "button1");
+  g_signal_connect(button1, "clicked", G_CALLBACK(print_hello), "button1");
+
+  GObject *button2 = gtk_builder_get_object(builder, "button2");
+  g_signal_connect(button2, "clicked", G_CALLBACK(print_hello), "button2");
+
+  GObject *quit = gtk_builder_get_object(builder, "quit");
+  g_signal_connect(quit, "clicked", G_CALLBACK(gtk_main_quit), NULL);
+
+  g_object_unref(builder);
+
+  gtk_main();
+  return 0;
+}

+ 16 - 0
builder.css

@@ -0,0 +1,16 @@
+button {
+    background: none;
+    background-color: goldenrod;
+    border: solid 0.5px #2f2d2d;
+    border-radius: 0;
+    font-size: 22px;
+    color: gold;
+}
+
+#ok_button {
+    background-color: greenyellow;
+}
+
+.cancel-button-class {
+    background-color: red;
+}

+ 81 - 0
builder.ui

@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.40.0 -->
+<interface>
+  <requires lib="gtk+" version="3.24"/>
+  <!-- interface-css-provider-path builder.css -->
+  <object class="GtkWindow" id="window">
+    <property name="width-request">350</property>
+    <property name="height-request">100</property>
+    <property name="visible">True</property>
+    <property name="can-focus">False</property>
+    <property name="border-width">1</property>
+    <property name="title">Grid</property>
+    <child>
+      <!-- n-columns=3 n-rows=3 -->
+      <object class="GtkGrid" id="grid">
+        <property name="visible">True</property>
+        <property name="can-focus">False</property>
+        <property name="opacity">0.97999999999999998</property>
+        <property name="border-width">1</property>
+        <property name="row-homogeneous">True</property>
+        <child>
+          <object class="GtkButton" id="button1">
+            <property name="label">Button 1</property>
+            <property name="name">ok_button</property>
+            <property name="visible">True</property>
+            <property name="can-focus">False</property>
+            <property name="receives-default">False</property>
+          </object>
+          <packing>
+            <property name="left-attach">0</property>
+            <property name="top-attach">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkButton" id="button2">
+            <property name="label">Button 2</property>
+            <property name="name">cancel_button</property>
+            <property name="visible">True</property>
+            <property name="can-focus">False</property>
+            <property name="receives-default">False</property>
+            <style>
+              <class name="cancel-button-class"/>
+            </style>
+          </object>
+          <packing>
+            <property name="left-attach">1</property>
+            <property name="top-attach">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkButton" id="quit">
+            <property name="label">Quit</property>
+            <property name="visible">True</property>
+            <property name="can-focus">False</property>
+            <property name="receives-default">False</property>
+          </object>
+          <packing>
+            <property name="left-attach">0</property>
+            <property name="top-attach">1</property>
+            <property name="width">2</property>
+          </packing>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>