Author Topic: SBGtk Calc  (Read 15409 times)

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
SBGtk Calc
« on: October 26, 2011, 07:22:47 PM »
This is a scripted calculator example using a Glade GUI designer XML project file.



calc.sb
Code: [Select]
' Gtk Calculator

DECLARE SUB DLL ALIAS "_idll" LIB "gtk-server"
DECLARE SUB VARPTR ALIAS "varptr" LIB "gtk-server"
DECLARE SUB REQUIRE ALIAS "_idll_require" LIB "gtk-server"
DECLARE SUB DEFINE ALIAS "_idll_define" LIB "gtk-server"

DEFINE "gtk_init NONE NONE 2 NULL NULL"
DEFINE "glade_init NONE NONE 0"
DEFINE "glade_xml_new NONE WIDGET 3 STRING NULL NULL"
DEFINE "glade_xml_signal_autoconnect NONE NONE 1 WIDGET"
DEFINE "glade_xml_get_widget NONE WIDGET 2 WIDGET STRING"
DEFINE "gtk_entry_get_text NONE STRING 1 WIDGET"
DEFINE "gtk_entry_set_text NONE NONE 2 WIDGET STRING"
DEFINE "gtk_button_get_label NONE STRING 1 WIDGET"
DEFINE "gtk_server_connect NONE STRING 3 STRING STRING STRING"
DEFINE "gtk_server_callback NONE STRING 1 STRING"
DEFINE "gtk_server_exit NONE NONE 0"


' Initialize calculator state
calculator_state = 0

' Initialize calculator cache
calculator_cache = 0

' Initialize last calculator action
calculator_action = 0

' Initialize MEM function
mem = 0

' Process the action
SUB Handle_Operator(entry)
  LOCAL value
  IF calculator_state = 0 THEN
    IF calculator_action = 1 THEN
      value = DLL("gtk_entry_get_text " & entry)
      calculator_cache += value
      DLL("gtk_entry_set_text " & entry & " " & calculator_cache)
    ELSE IF calculator_action = 2 THEN
      value = DLL("gtk_entry_get_text " & entry)
      calculator_cache -= value
      DLL("gtk_entry_set_text " & entry & " " & calculator_cache)
    ELSE IF calculator_action = 3 THEN
      value = DLL("gtk_entry_get_text " & entry)
      calculator_cache *= value
      DLL("gtk_entry_set_text " & entry & " " & calculator_cache)
    ELSE IF calculator_action = 4 THEN
      value = DLL("gtk_entry_get_text " & entry)
      IF value = 0 THEN
        DLL("gtk_entry_set_text " & entry & " ERROR")
      ELSE
        calculator_cache /= value
        DLL("gtk_entry_set_text " & entry & " " & calculator_cache)
      END IF
    END IF
  END IF
END SUB

' All button actions here
SUB Button_Memread(entry, widget)
  calculator_state = 0
  calculator_cache = DLL("gtk_entry_get_text " & entry)
  DLL("gtk_entry_set_text " & entry & " " & mem)
END SUB

SUB Button_Memadd(entry, widget)
  mem = DLL("gtk_entry_get_text " & entry)
END SUB

SUB Button_C(entry, widget)
  DLL("gtk_entry_set_text " & entry & " 0")
END SUB

SUB Button_CE(entry, widget)
  calculator_state = 0
  calculator_action = 0
  calculator_cache = 0
  mem = 0
  DLL("gtk_entry_set_text " & entry & " 0")
END SUB

SUB Button_Add(entry, widget)
    Handle_Operator(entry)
    calculator_action = 1
    calculator_state += 1
END SUB

SUB Button_Subtract(entry, widget)
    Handle_Operator(entry)
    calculator_action = 2
    calculator_state += 1
END SUB

SUB Button_Multiply(entry, widget)
    Handle_Operator(entry)
    calculator_action = 3
    calculator_state += 1
END SUB

SUB Button_Divide(entry, widget)
    Handle_Operator(entry)
    calculator_action = 4
    calculator_state += 1
END SUB

SUB Button_Equals(entry, widget)
    Handle_Operator(entry)
    calculator_action = 0
    calculator_state += 1
END SUB

SUB Button_Clicked(entry, widget)
    LOCAL button, text
    button = DLL("gtk_button_get_label " & widget)
    text = DLL("gtk_entry_get_text " & entry)
    IF text = "0" OR calculator_state > 0 THEN
      calculator_cache = text
      DLL("gtk_entry_set_text " & entry & " " & button)
    ELSE
      DLL("gtk_entry_set_text " & entry & " " & text & button)
    END IF
    calculator_state = 0
END SUB

' Main program
DLL("gtk_init NULL NULL")
DLL("glade_init")
xml = DLL("glade_xml_new \"calc.glade\" NULL NULL")
DLL("glade_xml_signal_autoconnect " & xml)

' Calc_Exit
win = DLL("glade_xml_get_widget " &  xml & " window")
DLL("gtk_server_connect " & win & " delete-event window")

' Entry
ent = DLL("glade_xml_get_widget " & xml & " entry")

' Button_Clicked
but1 = DLL("glade_xml_get_widget " &  xml & " button1")
DLL("gtk_server_connect " & but1 & " clicked button1")
but2 = DLL("glade_xml_get_widget " &  xml & " button2")
DLL("gtk_server_connect " & but2 & " clicked button2")
but3 = DLL("glade_xml_get_widget " &  xml & " button3")
DLL("gtk_server_connect " & but3 & " clicked button3")
but4 = DLL("glade_xml_get_widget " &  xml & " button4")
DLL("gtk_server_connect " & but4 & " clicked button4")
but5 = DLL("glade_xml_get_widget " &  xml & " button5")
DLL("gtk_server_connect " & but5 & " clicked button5")
but6 = DLL("glade_xml_get_widget " &  xml & " button6")
DLL("gtk_server_connect " & but6 & " clicked button6")
but7 = DLL("glade_xml_get_widget " &  xml & " button7")
DLL("gtk_server_connect " & but7 & " clicked button7")
but8 = DLL("glade_xml_get_widget " &  xml & " button8")
DLL("gtk_server_connect " & but8 & " clicked button8")
but9 = DLL("glade_xml_get_widget " &  xml & " button9")
DLL("gtk_server_connect " & but9 & " clicked button9")
but0 = DLL("glade_xml_get_widget " &  xml & " button0")
DLL("gtk_server_connect " & but0 & " clicked button0")

' Button_Add
butADD = DLL("glade_xml_get_widget " &  xml & " buttonAdd")
DLL("gtk_server_connect " & butADD & " clicked buttonAdd")

' Button_Subtract
butSUB = DLL("glade_xml_get_widget " &  xml & " buttonMinus")
DLL("gtk_server_connect " & butSUB & " clicked buttonMinus")

' Button_Multiply
butMUL = DLL("glade_xml_get_widget " &  xml & " buttonMul")
DLL("gtk_server_connect " & butMUL & " clicked buttonMul")

' Button_Divide
butDIV = DLL("glade_xml_get_widget " &  xml & " buttonDiv")
DLL("gtk_server_connect " & butDIV & " clicked buttonDiv")

' Button_Equals
butEQ = DLL("glade_xml_get_widget " &  xml & " buttonEq")
DLL("gtk_server_connect " & butEQ & " clicked buttonEq")

' Button_C
butC = DLL("glade_xml_get_widget " &  xml & " buttonC")
DLL("gtk_server_connect " & butC & " clicked buttonC")

' Button_CE
butCE = DLL("glade_xml_get_widget " &  xml & " buttonCE")
DLL("gtk_server_connect " & butCE & " clicked buttonCE")

' Button_Memadd
memadd = DLL("glade_xml_get_widget " &  xml & " buttonMemadd")
DLL("gtk_server_connect " & memadd & " clicked buttonMemadd")

' Button_Memread
memread = DLL("glade_xml_get_widget " &  xml & " buttonMemread")
DLL("gtk_server_connect " & memread & " clicked buttonMemread")

REPEAT
  event = DLL("gtk_server_callback wait")
  IF event = "button1" THEN Button_Clicked(ent, but1)
  IF event = "button2" THEN Button_Clicked(ent, but2)
  IF event = "button3" THEN Button_Clicked(ent, but3)
  IF event = "button4" THEN Button_Clicked(ent, but4)
  IF event = "button5" THEN Button_Clicked(ent, but5)
  IF event = "button6" THEN Button_Clicked(ent, but6)
  IF event = "button7" THEN Button_Clicked(ent, but7)
  IF event = "button8" THEN Button_Clicked(ent, but8)
  IF event = "button9" THEN Button_Clicked(ent, but9)
  IF event = "button0" THEN Button_Clicked(ent, but0)
  IF event = "buttonAdd" THEN Button_Add(ent, butADD)
  IF event = "buttonMinus" THEN Button_Subtract(ent, butSUB)
  IF event = "buttonMul" THEN Button_Multiply(ent, butMUL)
  IF event = "buttonDiv" THEN Button_Divide(ent, butDIV)
  IF event = "buttonEq" THEN Button_Equals(ent, butEQ)
  IF event = "buttonC" THEN Button_C(ent, butC)
  IF event = "buttonCE" THEN Button_CE(ent, butCE)
  IF event = "buttonMemadd" THEN Button_Memadd(ent, memadd)
  IF event = "buttonMemread" THEN Button_Memread(ent, memread)
UNTIL event = "window"

DLL("gtk_server_exit")
END

calc.glade
Code: [Select]
<?xml version="1.0"?>
<glade-interface>
  <widget class="GtkWindow" id="window">
    <property name="width_request">250</property>
    <property name="height_request">225</property>
    <property name="visible">True</property>
    <property name="title" translatable="yes">SBGtk Calculator</property>
    <property name="resizable">False</property>
    <property name="window_position">center</property>
    <property name="default_width">264</property>
    <property name="default_height">248</property>
    <property name="icon_name">calc</property>
    <child>
      <widget class="GtkTable" id="table1">
        <property name="visible">True</property>
        <property name="n_rows">5</property>
        <property name="n_columns">5</property>
        <property name="homogeneous">True</property>
        <child>
          <widget class="GtkButton" id="buttonMemadd">
            <property name="label" translatable="yes">M+</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">3</property>
            <property name="right_attach">4</property>
            <property name="top_attach">4</property>
            <property name="bottom_attach">5</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkEntry" id="entry">
            <property name="visible">True</property>
            <property name="sensitive">False</property>
            <property name="editable">False</property>
            <property name="xalign">1</property>
          </widget>
          <packing>
            <property name="right_attach">5</property>
            <property name="x_padding">4</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button1">
            <property name="label" translatable="yes">1</property>
            <property name="width_request">0</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="top_attach">1</property>
            <property name="bottom_attach">2</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button3">
            <property name="label" translatable="yes">3</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">2</property>
            <property name="right_attach">3</property>
            <property name="top_attach">1</property>
            <property name="bottom_attach">2</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="buttonC">
            <property name="label" translatable="yes">C</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">3</property>
            <property name="right_attach">4</property>
            <property name="top_attach">1</property>
            <property name="bottom_attach">2</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="buttonCE">
            <property name="label" translatable="yes">CE</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">4</property>
            <property name="right_attach">5</property>
            <property name="top_attach">1</property>
            <property name="bottom_attach">2</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button4">
            <property name="label" translatable="yes">4</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="top_attach">2</property>
            <property name="bottom_attach">3</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button5">
            <property name="label" translatable="yes">5</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">1</property>
            <property name="right_attach">2</property>
            <property name="top_attach">2</property>
            <property name="bottom_attach">3</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button6">
            <property name="label" translatable="yes">6</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">2</property>
            <property name="right_attach">3</property>
            <property name="top_attach">2</property>
            <property name="bottom_attach">3</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="buttonAdd">
            <property name="label" translatable="yes">+</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">3</property>
            <property name="right_attach">4</property>
            <property name="top_attach">2</property>
            <property name="bottom_attach">3</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="buttonMinus">
            <property name="label" translatable="yes">-</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">4</property>
            <property name="right_attach">5</property>
            <property name="top_attach">2</property>
            <property name="bottom_attach">3</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button7">
            <property name="label" translatable="yes">7</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="top_attach">3</property>
            <property name="bottom_attach">4</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button8">
            <property name="label" translatable="yes">8</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">1</property>
            <property name="right_attach">2</property>
            <property name="top_attach">3</property>
            <property name="bottom_attach">4</property>
            <property name="x_padding">5</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button9">
            <property name="label" translatable="yes">9</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">2</property>
            <property name="right_attach">3</property>
            <property name="top_attach">3</property>
            <property name="bottom_attach">4</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="buttonMul">
            <property name="label" translatable="yes">*</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">3</property>
            <property name="right_attach">4</property>
            <property name="top_attach">3</property>
            <property name="bottom_attach">4</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="buttonDiv">
            <property name="label" translatable="yes">/</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">4</property>
            <property name="right_attach">5</property>
            <property name="top_attach">3</property>
            <property name="bottom_attach">4</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button0">
            <property name="label" translatable="yes">0</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="top_attach">4</property>
            <property name="bottom_attach">5</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="buttonMemread">
            <property name="label" translatable="yes">MR</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">4</property>
            <property name="right_attach">5</property>
            <property name="top_attach">4</property>
            <property name="bottom_attach">5</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="buttonEq">
            <property name="label" translatable="yes">=</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">1</property>
            <property name="right_attach">3</property>
            <property name="top_attach">4</property>
            <property name="bottom_attach">5</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
        <child>
          <widget class="GtkButton" id="button2">
            <property name="label" translatable="yes">2</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
          </widget>
          <packing>
            <property name="left_attach">1</property>
            <property name="right_attach">2</property>
            <property name="top_attach">1</property>
            <property name="bottom_attach">2</property>
            <property name="x_padding">4</property>
            <property name="y_padding">8</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>
« Last Edit: October 30, 2011, 06:46:51 AM by support »