表示されません。実行中にウィンドウのテキスト内容が変化するので、ウィンドウサイズをテキストエクステントのサイズに合わせて調整したい。テキストエクステントのサイズが変更されない場合、ウィンドウは常に新しいテキストで正しく更新されます。はXResizeWindow後、描かれた新しいウィンドウの内容は、私はそれにいくつかのテキストを描画するウィンドウと<strong>カイロ</strong>を管理するために<strong>Xlibの</strong>を使用してLinuxでアプリケーションを書いている
しかし、ときにテキストの範囲の変更、およびウィンドウがそれに応じてサイズが変更されるので、ウィンドウがクリアされているが、新しいテキストが表示されることはありません。 XResizeWindowへの呼び出しがない場合にのみ、テキストが実際に表示されます。私が使用しているコードは
if (/* Text extent is changed */)
{
XResizeWindow (display, window, new_width, new_height);
cairo_xlib_surface_set_size (surface, new_width, new_height);
}
XClearWindow (display, window);
/* ... Cairo code to draw the text ... */
// cairo_surface_flush (surface);
// XFlush (display);
である私は、テキストの方法cairo_surface_flushとXFlush(例ではコメント)何も変更を描くカイロコードの後に追加しようとしました。
EDIT:私は二つのスレッド使用して問題を解決:コンテンツとウィンドウのリサイズを発行秒のスレッドを再描画するために公開するイベントプラスコードを聴くための通常のループを備えた最初のスレッドをし、 Exposeイベントを送信して、最初のスレッドを起動します。
この例では、ウィンドウは、ランダム幅と高さ毎に500ミリ秒をリサイズされ、プログレッシブカウンタはすべてのサイズ変更時に、その中に表示されます。私はC++ 11を使用し、使用してコンパイル:
#include <random>
#include <chrono>
#include <thread>
#include <string>
#include <X11/Xlib.h>
#include <cairo/cairo-xlib.h>
Display * d;
Window w;
cairo_surface_t * surface;
int width = 300, height = 300;
unsigned char counter = 0;
std::random_device rd;
std::knuth_b gen (rd());
std::uniform_int_distribution < > dist (150, 300);
void logic()
{
XEvent send_event;
send_event.type = Expose;
send_event.xexpose.window = w;
while (true)
{
std::this_thread::sleep_for (std::chrono::milliseconds (500));
++ counter;
width = dist (gen);
height = dist (gen);
cairo_xlib_surface_set_size (surface, width, height);
XResizeWindow (d, w, width, height);
XSendEvent (d, w, False, ExposureMask, & send_event);
XFlush (d);
}
}
int main ()
{
XInitThreads();
d = XOpenDisplay (NULL);
w = XCreateSimpleWindow (d, RootWindow (d, 0), 0, 0, width, height, 0, 0, 0x000000);
XMapWindow (d, w);
XSelectInput (d, w, ExposureMask | KeyPressMask);
surface = cairo_xlib_surface_create (d, w, DefaultVisual (d, 0), width, height);
cairo_t * cairo = cairo_create (surface);
cairo_select_font_face (cairo, "FreeSans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cairo, 40);
cairo_set_source_rgb (cairo, 0.8, 0.8, 0.8);
cairo_move_to (cairo, 40.0, 60.0);
cairo_show_text (cairo, std::to_string (counter).c_str());
XFlush (d);
std::thread T (logic);
XEvent event;
while (true)
{
XNextEvent (d, & event);
if (event.type == Expose)
{
XClearWindow (d, w);
cairo_move_to (cairo, 40.0, 60.0);
cairo_show_text (cairo, std::to_string (counter).c_str());
}
else if (event.type == KeyPress)
{
XCloseDisplay (d);
return 0;
}
}
}
しかし、問題はまま:
g++ -std=c++11 -o test test.cpp -lX11 -lcairo -lpthread
コードがあるだけつのスレッドを使用して同じ結果を得ることができるのですか?
あなたがテストできる自己完結型の例はありますか?イベントループの外観はどのようになっていますか?つまり、露出イベントでは何をしていますか? –
私は、動作を再現するコンパクトな自己完結型のコード例を提供します。 – disquisitiones