| 1 |
/* |
| 2 |
* Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras) |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
namespace Pantheon.Keyboard.Shortcuts { |
| 19 |
// stores a shortcut, converts to gsettings format and readable format |
| 20 |
// and checks for validity |
| 21 |
class Shortcut : GLib.Object { |
| 22 |
public Gdk.ModifierType modifiers; |
| 23 |
public uint accel_key; |
| 24 |
|
| 25 |
/* NEVER USED |
| 26 |
string SEPARATOR = " · "; |
| 27 |
*/ |
| 28 |
|
| 29 |
// constructors |
| 30 |
public Shortcut (uint key = 0, Gdk.ModifierType mod = (Gdk.ModifierType) 0) { |
| 31 |
accel_key = key; |
| 32 |
modifiers = mod; |
| 33 |
} |
| 34 |
|
| 35 |
public Shortcut.parse (string ? str) |
| 36 |
{ |
| 37 |
if (str == null) { |
| 38 |
accel_key = 0; |
| 39 |
modifiers = (Gdk.ModifierType) 0; |
| 40 |
return; |
| 41 |
} |
| 42 |
Gtk.accelerator_parse (str, out accel_key, out modifiers); |
| 43 |
} |
| 44 |
|
| 45 |
// converters |
| 46 |
public string to_gsettings () { |
| 47 |
if (!valid ()) |
| 48 |
return ""; |
| 49 |
return Gtk.accelerator_name (accel_key, modifiers); |
| 50 |
} |
| 51 |
|
| 52 |
/* NEVER USED |
| 53 |
public string to_readable () |
| 54 |
{ |
| 55 |
if (!valid()) |
| 56 |
return _("Disabled"); |
| 57 |
|
| 58 |
string tmp = ""; |
| 59 |
|
| 60 |
if ((modifiers & Gdk.ModifierType.SHIFT_MASK) > 0) |
| 61 |
tmp += "⇧" + SEPARATOR; |
| 62 |
if ((modifiers & Gdk.ModifierType.SUPER_MASK) > 0) |
| 63 |
tmp += "⌘" + SEPARATOR; |
| 64 |
if ((modifiers & Gdk.ModifierType.CONTROL_MASK) > 0) |
| 65 |
tmp += _("Ctrl") + SEPARATOR; |
| 66 |
if ((modifiers & Gdk.ModifierType.MOD1_MASK) > 0) |
| 67 |
tmp += "⎇" + SEPARATOR; |
| 68 |
if ((modifiers & Gdk.ModifierType.MOD2_MASK) > 0) |
| 69 |
tmp += "Mod2" + SEPARATOR; |
| 70 |
if ((modifiers & Gdk.ModifierType.MOD3_MASK) > 0) |
| 71 |
tmp += "Mod3" + SEPARATOR; |
| 72 |
if ((modifiers & Gdk.ModifierType.MOD4_MASK) > 0) |
| 73 |
tmp += "Mod4" + SEPARATOR; |
| 74 |
|
| 75 |
switch (accel_key) { |
| 76 |
|
| 77 |
case Gdk.Key.Tab: tmp += "↹"; break; |
| 78 |
case Gdk.Key.Up: tmp += "↑"; break; |
| 79 |
case Gdk.Key.Down: tmp += "↓"; break; |
| 80 |
case Gdk.Key.Left: tmp += "←"; break; |
| 81 |
case Gdk.Key.Right: tmp += "→"; break; |
| 82 |
default: |
| 83 |
tmp += Gtk.accelerator_get_label (accel_key, 0); |
| 84 |
break; |
| 85 |
} |
| 86 |
|
| 87 |
return tmp; |
| 88 |
} |
| 89 |
*/ |
| 90 |
|
| 91 |
public bool is_equal (Shortcut shortcut) { |
| 92 |
// debug("is equal? %d, %d : %d, %d",shortcut.modifiers, modifiers, (int)shortcut.accel_key, (int) accel_key); |
| 93 |
if (shortcut.modifiers == modifiers) { |
| 94 |
if (shortcut.accel_key == accel_key) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
} |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
// validator |
| 102 |
public bool valid () { |
| 103 |
if (accel_key == 0 || (modifiers == (Gdk.ModifierType) 0 && accel_key != Gdk.Key.Print)) |
| 104 |
return false; |
| 105 |
|
| 106 |
if (modifiers == Gdk.ModifierType.SHIFT_MASK) { |
| 107 |
if ((accel_key >= Gdk.Key.a && accel_key <= Gdk.Key.z) |
| 108 |
|| (accel_key >= Gdk.Key.A && accel_key <= Gdk.Key.Z) |
| 109 |
|| (accel_key >= Gdk.Key.@0 && accel_key <= Gdk.Key.@9) |
| 110 |
|| (accel_key >= Gdk.Key.kana_fullstop && accel_key <= Gdk.Key.semivoicedsound) |
| 111 |
|| (accel_key >= Gdk.Key.Arabic_comma && accel_key <= Gdk.Key.Arabic_sukun) |
| 112 |
|| (accel_key >= Gdk.Key.Serbian_dje && accel_key <= Gdk.Key.Cyrillic_HARDSIGN) |
| 113 |
|| (accel_key >= Gdk.Key.Greek_ALPHAaccent && accel_key <= Gdk.Key.Greek_omega) |
| 114 |
|| (accel_key >= Gdk.Key.hebrew_doublelowline && accel_key <= Gdk.Key.hebrew_taf) |
| 115 |
|| (accel_key >= Gdk.Key.Thai_kokai && accel_key <= Gdk.Key.Thai_lekkao) |
| 116 |
|| (accel_key >= Gdk.Key.Hangul && accel_key <= Gdk.Key.Hangul_Special) |
| 117 |
|| (accel_key >= Gdk.Key.Hangul_Kiyeog && accel_key <= Gdk.Key.Hangul_J_YeorinHieuh) |
| 118 |
|| (accel_key == Gdk.Key.Home) |
| 119 |
|| (accel_key == Gdk.Key.Left) |
| 120 |
|| (accel_key == Gdk.Key.Up) |
| 121 |
|| (accel_key == Gdk.Key.Right) |
| 122 |
|| (accel_key == Gdk.Key.Down) |
| 123 |
|| (accel_key == Gdk.Key.Page_Up) |
| 124 |
|| (accel_key == Gdk.Key.Page_Down) |
| 125 |
|| (accel_key == Gdk.Key.End) |
| 126 |
|| (accel_key == Gdk.Key.Tab) |
| 127 |
|| (accel_key == Gdk.Key.KP_Enter) |
| 128 |
|| (accel_key == Gdk.Key.Return)) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
} |