File: standalone-gtk.c

package info (click to toggle)
uim 1%3A1.8.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 38,956 kB
  • sloc: lisp: 356,804; ansic: 77,833; cpp: 27,846; sh: 12,460; makefile: 2,546; asm: 333; ruby: 288
file content (360 lines) | stat: -rw-r--r-- 10,027 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*

  toolbar-standalone-gtk.c: toolbar implementation with GTK+

  Copyright (c) 2003-2012 uim Project http://code.google.com/p/uim/

  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
  3. Neither the name of authors nor the names of its contributors
     may be used to endorse or promote products derived from this software
     without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  SUCH DAMAGE.

*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <locale.h>
#include <uim/gettext.h>
#include <gtk/gtk.h>
#include "uim/uim.h"

extern GtkWidget *uim_toolbar_standalone_new(void);

static gboolean toolbar_dragging = FALSE;
static gint window_drag_start_x = -1, window_drag_start_y = -1;
static gint pointer_drag_start_x = -1, pointer_drag_start_y = -1;
static void size_request_cb(GtkWidget *widget, GtkRequisition *req,
                            gpointer data);

#if GLIB_CHECK_VERSION(2, 6, 0)

static void
parse_options(gint argc, gchar **argv)
{

}

#endif

static void
delete_event(GtkWidget *widget, gpointer data)
{
  gtk_main_quit();
}

static gboolean
button_press_event_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
  GdkCursor *cursor;
  GtkWidget *toolbar;
  gint height, width;

  switch (event->type) {
  case GDK_BUTTON_PRESS:
    cursor = gdk_cursor_new(GDK_FLEUR);
    gdk_pointer_grab(gtk_widget_get_window(widget), FALSE,
		     GDK_BUTTON_RELEASE_MASK |
		     GDK_POINTER_MOTION_MASK,
		     NULL,
		     cursor, event->time);
    gdk_cursor_unref(cursor);

    gtk_window_get_position(GTK_WINDOW(widget),
		    	    &window_drag_start_x,
			    &window_drag_start_y);
    pointer_drag_start_x = (gint)event->x_root;
    pointer_drag_start_y = (gint)event->y_root;
    toolbar_dragging = TRUE;
    break;
  case GDK_2BUTTON_PRESS:
    toolbar = GTK_WIDGET(data);
#if GTK_CHECK_VERSION(2, 18, 0)
    if (gtk_widget_get_visible(toolbar)) {
#else
    if (GTK_WIDGET_VISIBLE(toolbar)) {
#endif
      gtk_window_get_size(GTK_WINDOW(widget), &width, &height);
      gtk_widget_hide(toolbar);
    } else {
      height = -1;
      gtk_widget_show(toolbar);
    }
#if GTK_CHECK_VERSION(2, 90, 0)
    {
      GtkRequisition minimum_size, natural_size;
      gtk_widget_get_preferred_size(widget, &minimum_size, &natural_size);
      if (height > 0)
        natural_size.height = height;
      else if (height == 0)
        natural_size.height = minimum_size.height;
      size_request_cb(widget, &natural_size, NULL);
    }
#else
    gtk_widget_set_size_request(widget, -1, height);
#endif
    break;
  default:
    break;
  }

  return FALSE;
}

static void
helper_win_set_position(GtkWidget *window, gint x, gint y)
{
  gint wx = x, wy = y;
  gint w, h, sc_w, sc_h;

  sc_w = gdk_screen_width();
  sc_h = gdk_screen_height();

#if GTK_CHECK_VERSION(2, 90, 0)
  w = gdk_window_get_width(gtk_widget_get_window(window));
  h = gdk_window_get_height(gtk_widget_get_window(window));
#else
  gdk_drawable_get_size(gtk_widget_get_window(window), &w, &h);
#endif

  if (wx < 0)
    wx = 0;
  else if (wx > sc_w - w)
    wx = sc_w - w;
  if (wy < 0)
    wy = 0;
  else if (wy > sc_h - h)
    wy = sc_h -h;

  g_object_set_data(G_OBJECT(window), "position_x", GINT_TO_POINTER(wx));
  g_object_set_data(G_OBJECT(window), "position_y", GINT_TO_POINTER(wy));
  gtk_window_move(GTK_WINDOW(window), wx, wy);
}

static gboolean
motion_notify_event_cb(GtkWidget *widget, GdkEventMotion *event, gpointer data)
{
  if (toolbar_dragging) {
    gint wx, wy;

    wx = window_drag_start_x + ((gint)event->x_root - pointer_drag_start_x);
    wy = window_drag_start_y + ((gint)event->y_root - pointer_drag_start_y);

    helper_win_set_position(widget, wx, wy);

    return TRUE;
  }

  return FALSE;
}

static gboolean
button_release_event_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
  if (!toolbar_dragging)
    return FALSE;

  gdk_pointer_ungrab(event->time);

  pointer_drag_start_x = -1;
  pointer_drag_start_y = -1;
  toolbar_dragging = FALSE;

  return FALSE;
}

#if GTK_CHECK_VERSION(2, 90, 0)
static gboolean
handle_draw_cb(GtkWidget *widget, cairo_t *cr)
{
  gtk_render_handle(gtk_widget_get_style_context(widget), cr,
		   0, 0,
		   gtk_widget_get_allocated_width(widget),
		   gtk_widget_get_allocated_height(widget));
  return FALSE;
}

#else
static gboolean
handle_expose_event_cb(GtkWidget *widget, GdkEventExpose *event)
{
  GdkRectangle *rect = &event->area;

#if GTK_CHECK_VERSION(2, 18, 0)
  GtkAllocation allocation;
  gtk_widget_get_allocation(widget, &allocation);
  gtk_paint_handle(gtk_widget_get_style(widget), gtk_widget_get_window(widget),
		   GTK_STATE_NORMAL, GTK_SHADOW_OUT,
		   rect, widget, "handlebox",
		   allocation.x, allocation.y,
		   allocation.width, allocation.height,
		   GTK_ORIENTATION_VERTICAL);
#else
  gtk_paint_handle(widget->style, widget->window,
                   GTK_STATE_NORMAL, GTK_SHADOW_OUT,
                   rect, widget, "handlebox",
                   widget->allocation.x, widget->allocation.y,
                   widget->allocation.width, widget->allocation.height,
                   GTK_ORIENTATION_VERTICAL);
#endif

  return FALSE;
}
#endif

static void
size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer user_data)
{
  gint x, y;

#if GTK_CHECK_VERSION(2, 20, 0)
  if (gtk_widget_get_mapped(widget)) {
#else
  if (GTK_WIDGET_MAPPED(widget)) {
#endif
    gtk_window_get_position(GTK_WINDOW(widget), &x, &y);
    helper_win_set_position(widget, x, y);
  }
}

static void
size_request_cb(GtkWidget *widget, GtkRequisition *req, gpointer data)
{

#if GTK_CHECK_VERSION(2, 20, 0)
  if (gtk_widget_get_mapped(widget)) {
#else
  if (GTK_WIDGET_MAPPED(widget)) {
#endif
    gint width, height;
    gtk_window_get_size(GTK_WINDOW(widget), &width, &height);

    if (width > req->width) {
      gtk_window_resize(GTK_WINDOW(widget), req->width, req->height);
    }
  }
}

int
main(int argc, char *argv[])
{
  GtkWidget *toolbar;
  GtkWidget *window;
  GtkWidget *hbox;
  GtkWidget *handle;
  GtkWidget *frame;

  setlocale(LC_ALL, "");
  bindtextdomain(PACKAGE, LOCALEDIR);
  textdomain(PACKAGE);
  bind_textdomain_codeset(PACKAGE, "UTF-8");

  uim_init();

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_POPUP);

  gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DOCK);

  gtk_window_set_skip_taskbar_hint(GTK_WINDOW(window), TRUE);
  gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
  gtk_window_stick(GTK_WINDOW(window));

  gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);

  frame = gtk_frame_new(NULL);
  gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
  gtk_container_add(GTK_CONTAINER(window), frame);

  hbox = gtk_hbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(frame), hbox);

  handle = gtk_drawing_area_new();
  gtk_widget_set_size_request(handle, 8, -1);
  gtk_box_pack_start(GTK_BOX(hbox), handle, FALSE, FALSE, 0);

  toolbar = (GtkWidget*)uim_toolbar_standalone_new();
  gtk_box_pack_start(GTK_BOX(hbox), toolbar, FALSE, FALSE, 0);

#if GTK_CHECK_VERSION(2, 90, 0)
  g_signal_connect(G_OBJECT(handle), "draw",
		   G_CALLBACK(handle_draw_cb), NULL);
#else
  g_signal_connect(G_OBJECT(handle), "expose-event",
		   G_CALLBACK(handle_expose_event_cb), NULL);
#endif

  g_signal_connect(G_OBJECT(window), "delete_event",
		   G_CALLBACK(delete_event), NULL);
  g_signal_connect(G_OBJECT(window), "button-press-event",
		   G_CALLBACK(button_press_event_cb), toolbar);
  g_signal_connect(G_OBJECT(window), "button-release-event",
		   G_CALLBACK(button_release_event_cb), NULL);
  g_signal_connect(G_OBJECT(window), "motion-notify-event",
		   G_CALLBACK(motion_notify_event_cb), NULL);
  g_signal_connect(G_OBJECT(window), "size-allocate",
		   G_CALLBACK(size_allocate_cb), NULL);
#if !GTK_CHECK_VERSION(2, 90, 0)
  g_signal_connect(G_OBJECT(window), "size-request",
		   G_CALLBACK(size_request_cb), NULL);
#endif

  gtk_widget_show_all(GTK_WIDGET(window));

  if (argc > 1) {
    gint x, y;
    if (!gtk_window_parse_geometry(GTK_WINDOW(window), argv[1])) {

#if GLIB_CHECK_VERSION(2, 6, 0)
      parse_options(argc, argv);
#else
      g_warning(_("Unable to parse the geometry string '%s'"), argv[1]);
#endif
    }
    gtk_window_get_position(GTK_WINDOW(window), &x, &y);
    g_object_set_data(G_OBJECT(window), "position_x", GINT_TO_POINTER(x));
    g_object_set_data(G_OBJECT(window), "position_y", GINT_TO_POINTER(y));
  } else {
    gint x, y, w, h, sc_w, sc_h;
    gint panel_height = 32; /* FIXME! */

    gtk_window_get_size(GTK_WINDOW(window), &w, &h);
    sc_w = gdk_screen_width();
    sc_h = gdk_screen_height();

    x = sc_w - w;
    y = sc_h - h - panel_height; /* FIXME! */
    helper_win_set_position(window, x, y);
  }

  gtk_main();

  uim_quit();

  return 0;
}