Gambas: Tabelle: Wiki-Tabelle Form1.class

' Gambas class file

$hTable AS NEW Variant[]
$iRows AS Integer
$iColumns AS Integer
$iCurRow AS Integer
$iCurColumn AS Integer

PUBLIC $sText AS String

PUBLIC SUB Form_Open()
  ME.Border = Window.Resizable
  ME.W = 900
  ME.H = 700
  ME.Center
  ME.Text = "Wiki-Tabelle"

  TableEditHide

  $iRows = 5
  $iColumns = 3
  TableMake
END

PUBLIC SUB Form_Resize()
  IF tbTableEdit.Visible = TRUE THEN TableEditSave

  pnAddRemove.Move(8, 8, ME.W - 16, 32)
  tlRows.Move(0, 0, 70, 32)
  sbRows.Move(70, 0, 70, 32)
  tlColumns.Move(160, 0, 70, 32)
  sbColumns.Move(240, 0, 70, 32)
  taTabelle.Move(8, ME.H - 258, ME.W - 16, 250)
  
  pnButtons.Move(8, taTabelle.Y - 80, ME.W - 16, 72)
  cbFirstLine.Move(0, 0, 230, 32)
  cbShort.Move(238, 0, 140, 32)
  cbPretty.Move(385, 0, 150, 32)
  
  btnTBMake.Move(0, 40, 180, 32)
  btnTBCopy.Move(188, 40, 180, 32)
  btnTBClear.Move(376, 40, 170, 32)
  btnClose.Move(pnButtons.W - 150, 40, 150, 32)

  tvTable.Move(8, pnAddRemove.H + 16, ME.W - 16, pnButtons.Y - pnAddRemove.H - 16)
END

PUBLIC SUB sbRows_Change()
  IF tbTableEdit.Visible = TRUE THEN TableEditSave
  $iRows = sbRows.Value
  TableMake
END

PUBLIC SUB sbColumns_Change()
  IF tbTableEdit.Visible = TRUE THEN TableEditSave
  $iColumns = sbColumns.Value
  TableMake
END

PUBLIC SUB TableMake()
  DIM iRow AS Integer
  DIM iColumn AS Integer
  
  'leere Zeilen löschen
  IF $hTable.Length > $iRows THEN
    tvTable.Rows.Count = $iRows
    FOR iRow = $iRows TO $hTable.Length - 1
      $hTable[iRow].Clear
      $hTable[iRow] = NULL
    NEXT
  ENDIF
  $hTable.Resize($iRows)
  
  'Spalten anpassen
  FOR iRow = 0 TO $iRows - 1
    IF $hTable[iRow] = NULL THEN $hTable[iRow] = NEW String[]
    $hTable[iRow].Resize($iColumns)
  NEXT
  
  tvTable.Columns.Count = $iColumns
  tvTable.Rows.Count = $iRows
  sbRows.Value = $iRows
  sbColumns.Value = $iColumns
  
  FOR iColumn = 0 TO $iColumns - 1
    tvTable.Columns[iColumn].Title = iColumn + 1
  NEXT
  
  cbFirstLine.Enabled = If($iRows = 1, FALSE, TRUE)
END

PUBLIC SUB tvTable_Data(Row AS Integer, Column AS Integer)
  IF Row MOD 2 THEN tvTable.Data.Background = &HCCFFCC&
  tvTable.Data.Text = $hTable[Row][Column]
END

PUBLIC SUB tvTable_Click()
  $iCurRow = tvTable.Row
  $iCurColumn = tvTable.Column
  TableEditShow
END

PUBLIC SUB TableEditShow()
  tvTable.MoveTo($iCurRow, $iCurColumn)

  WITH tvTable[$iCurRow, $iCurColumn]
    tbTableEdit.Text = .Text
    tbTableEdit.Move(tvTable.X + .X + 1, tvTable.Y + .Y, .W - 21, .H)
    btnTableEdit.Move(tvTable.X + .X + .W - 20, tvTable.Y + .Y, 20, .H)
  END WITH

  WITH btnTableEdit
    .Show
    .Raise
  END WITH

  WITH tbTableEdit
    .Background = If($iCurRow MOD 2, &HCCFFCC&, &HFFFFFF&)
    .Border = FALSE
    .Raise
    .Show
    .SetFocus
  END WITH
END

PUBLIC SUB TableEditSave()
  IF $iCurRow >= $iRows OR $iCurColumn >= $iColumns THEN RETURN
  IF $hTable[$iCurRow][$iCurColumn] <> tbTableEdit.Text THEN
    $hTable[$iCurRow][$iCurColumn] = tbTableEdit.Text
    tvTable.Refresh
  ENDIF
  TableEditHide
END

PUBLIC SUB TableEditHide()
  btnTableEdit.Hide
  tbTableEdit.Hide
END

PUBLIC SUB tbTableEdit_KeyPress()
  DIM iPos AS Integer
  
  SELECT Key.Code
  
    CASE Key.Up
      IF $iCurRow > 0 THEN
        iPos = tbTableEdit.Pos
        TableEditSave
        DEC $iCurRow
        TableEditShow
        tbTableEdit.Pos = iPos
      ENDIF

    CASE Key.Down
      IF $iCurRow < tvTable.Rows.Count - 1 THEN
        iPos = tbTableEdit.Pos
        TableEditSave
        INC $iCurRow
        TableEditShow
        tbTableEdit.Pos = iPos
      ENDIF

    CASE Key.Left
      IF $iCurColumn > 0 THEN
        IF tbTableEdit.Pos = 0 OR Key.Control = TRUE THEN
          TableEditSave
          DEC $iCurColumn
          TableEditShow
          STOP EVENT
          tbTableEdit.Pos = If(Key.Control = TRUE, 0, tbTableEdit.Length)
        ENDIF
      ENDIF

    CASE Key.Right
      IF $iCurColumn < tvTable.Columns.Count - 1 THEN
        IF tbTableEdit.Pos = tbTableEdit.Length OR Key.Control = TRUE THEN
          TableEditSave
          INC $iCurColumn
          TableEditShow
          STOP EVENT
          tbTableEdit.Pos = If(Key.Control = TRUE, tbTableEdit.Length, 0)
        ENDIF
      ENDIF

    CASE Key.Return
      TableEditSave
      IF $iCurColumn < tvTable.Columns.Count - 1 THEN
        INC $iCurColumn
      ELSE IF $iCurRow < tvTable.Rows.Count - 1 THEN
        INC $iCurRow
        $iCurColumn = 0
      ELSE
        $iCurRow = 0
        $iCurColumn = 0
      ENDIF
      TableEditShow
      tbTableEdit.Pos = tbTableEdit.Length

    CASE Key.Esc
      TableEditHide
      
  END SELECT 
END

PUBLIC SUB tvTable_MouseDown()
  IF tbTableEdit.Visible = TRUE THEN TableEditSave
END

PUBLIC SUB tvTable_MouseWheel()
  IF tbTableEdit.Visible = TRUE THEN TableEditSave
END

PUBLIC SUB btnTableEdit_Click()
  $sText = tbTableEdit.Text
  Form2.ShowDialog
  tbTableEdit.Text = $sText
END

PUBLIC SUB btnTBMake_Click()
  'Tabellenquelltext erstellen
  DIM iRow AS Integer
  DIM sTemp AS NEW String[]

  IF tbTableEdit.Visible = TRUE THEN TableEditSave

  FOR iRow = 0 TO $iRows - 1
    IF cbFirstLine.Value = TRUE AND iRow = 0 THEN 
      sTemp.Add("! " & $hTable[0].Join("\n! ") & "\n")
    ELSE
      IF cbShort.Value = TRUE THEN 
        sTemp.Add("| " & $hTable[iRow].Join(" || ") & "\n")
      ELSE
        sTemp.Add("| " & $hTable[iRow].Join("\n| ") & "\n")
      ENDIF
    ENDIF
  NEXT

  IF cbPretty.Value = TRUE THEN
    taTabelle.Text = "{|{{prettytable}}\n" & sTemp.Join("|-\n") & "|}\n"
  ELSE
    taTabelle.Text = "{|\n" & sTemp.Join("|-\n") & "|}\n"
  ENDIF
END

PUBLIC SUB btnTBCopy_Click()
  'Tabellenquelltext erstellen und in die Zwischenablage kopieren
  btnTBMake_Click
  taTabelle.Select(0, taTabelle.Length)
  taTabelle.Copy
  taTabelle.Select(0, 0)
END

PUBLIC SUB btnTBClear_Click()
  'alle Felder löschen
  DIM iRow AS Integer
  DIM iColumn AS Integer
  
  IF tbTableEdit.Visible = TRUE THEN TableEditSave

  taTabelle.Text = ""
  FOR iRow = 0 TO $iRows - 1
    FOR iColumn = 0 TO $iColumns - 1
      $hTable[iRow][iColumn] = ""
    NEXT
  NEXT
  tvTable.Refresh
END

PUBLIC SUB btnClose_Click()
  ME.Close
END